1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3 * lttng-context-perf-counters.c
5 * LTTng performance monitoring counters (perf-counters) integration module.
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/perf_event.h>
13 #include <linux/list.h>
14 #include <linux/string.h>
15 #include <linux/cpu.h>
17 #include <lttng-events.h>
18 #include <wrapper/ringbuffer/frontend_types.h>
19 #include <lttng-tracer.h>
22 size_t perf_counter_get_size(size_t offset
)
26 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
27 size
+= sizeof(uint64_t);
32 void perf_counter_record(struct lttng_ctx_field
*field
,
33 struct lib_ring_buffer_ctx
*ctx
,
34 struct lttng_channel
*chan
)
36 struct perf_event
*event
;
39 event
= field
->u
.perf_counter
->e
[ctx
->cpu
];
41 if (unlikely(event
->state
== PERF_EVENT_STATE_ERROR
)) {
44 event
->pmu
->read(event
);
45 value
= local64_read(&event
->count
);
49 * Perf chooses not to be clever and not to support enabling a
50 * perf counter before the cpu is brought up. Therefore, we need
51 * to support having events coming (e.g. scheduler events)
52 * before the counter is setup. Write an arbitrary 0 in this
57 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
58 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
62 void overflow_callback(struct perf_event
*event
,
63 struct perf_sample_data
*data
,
69 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
71 struct perf_event
**events
= field
->u
.perf_counter
->e
;
73 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
77 ret
= cpuhp_state_remove_instance(lttng_hp_online
,
78 &field
->u
.perf_counter
->cpuhp_online
.node
);
80 ret
= cpuhp_state_remove_instance(lttng_hp_prepare
,
81 &field
->u
.perf_counter
->cpuhp_prepare
.node
);
84 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
89 for_each_online_cpu(cpu
)
90 perf_event_release_kernel(events
[cpu
]);
92 #ifdef CONFIG_HOTPLUG_CPU
93 unregister_cpu_notifier(&field
->u
.perf_counter
->nb
);
96 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
97 kfree(field
->event_field
.name
);
98 kfree(field
->u
.perf_counter
->attr
);
100 kfree(field
->u
.perf_counter
);
103 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
105 int lttng_cpuhp_perf_counter_online(unsigned int cpu
,
106 struct lttng_cpuhp_node
*node
)
108 struct lttng_perf_counter_field
*perf_field
=
109 container_of(node
, struct lttng_perf_counter_field
,
111 struct perf_event
**events
= perf_field
->e
;
112 struct perf_event_attr
*attr
= perf_field
->attr
;
113 struct perf_event
*pevent
;
115 pevent
= perf_event_create_kernel_counter(attr
,
116 cpu
, NULL
, overflow_callback
, NULL
);
117 if (!pevent
|| IS_ERR(pevent
))
119 if (pevent
->state
== PERF_EVENT_STATE_ERROR
) {
120 perf_event_release_kernel(pevent
);
123 barrier(); /* Create perf counter before setting event */
124 events
[cpu
] = pevent
;
128 int lttng_cpuhp_perf_counter_dead(unsigned int cpu
,
129 struct lttng_cpuhp_node
*node
)
131 struct lttng_perf_counter_field
*perf_field
=
132 container_of(node
, struct lttng_perf_counter_field
,
134 struct perf_event
**events
= perf_field
->e
;
135 struct perf_event
*pevent
;
137 pevent
= events
[cpu
];
139 barrier(); /* NULLify event before perf counter teardown */
140 perf_event_release_kernel(pevent
);
144 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
146 #ifdef CONFIG_HOTPLUG_CPU
149 * lttng_perf_counter_hp_callback - CPU hotplug callback
150 * @nb: notifier block
151 * @action: hotplug action to take
154 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
156 * We can setup perf counters when the cpu is online (up prepare seems to be too
160 int lttng_perf_counter_cpu_hp_callback(struct notifier_block
*nb
,
161 unsigned long action
,
164 unsigned int cpu
= (unsigned long) hcpu
;
165 struct lttng_perf_counter_field
*perf_field
=
166 container_of(nb
, struct lttng_perf_counter_field
, nb
);
167 struct perf_event
**events
= perf_field
->e
;
168 struct perf_event_attr
*attr
= perf_field
->attr
;
169 struct perf_event
*pevent
;
171 if (!perf_field
->hp_enable
)
176 case CPU_ONLINE_FROZEN
:
177 pevent
= perf_event_create_kernel_counter(attr
,
178 cpu
, NULL
, overflow_callback
, NULL
);
179 if (!pevent
|| IS_ERR(pevent
))
181 if (pevent
->state
== PERF_EVENT_STATE_ERROR
) {
182 perf_event_release_kernel(pevent
);
185 barrier(); /* Create perf counter before setting event */
186 events
[cpu
] = pevent
;
188 case CPU_UP_CANCELED
:
189 case CPU_UP_CANCELED_FROZEN
:
191 case CPU_DEAD_FROZEN
:
192 pevent
= events
[cpu
];
194 barrier(); /* NULLify event before perf counter teardown */
195 perf_event_release_kernel(pevent
);
203 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
205 int lttng_add_perf_counter_to_ctx(uint32_t type
,
208 struct lttng_ctx
**ctx
)
210 struct lttng_ctx_field
*field
;
211 struct lttng_perf_counter_field
*perf_field
;
212 struct perf_event
**events
;
213 struct perf_event_attr
*attr
;
217 events
= kvzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
221 attr
= kzalloc(sizeof(struct perf_event_attr
), GFP_KERNEL
);
228 attr
->config
= config
;
229 attr
->size
= sizeof(struct perf_event_attr
);
233 perf_field
= kzalloc(sizeof(struct lttng_perf_counter_field
), GFP_KERNEL
);
236 goto error_alloc_perf_field
;
238 perf_field
->e
= events
;
239 perf_field
->attr
= attr
;
241 name_alloc
= kstrdup(name
, GFP_KERNEL
);
244 goto name_alloc_error
;
247 field
= lttng_append_context(ctx
);
250 goto append_context_error
;
252 if (lttng_find_context(*ctx
, name_alloc
)) {
257 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
259 perf_field
->cpuhp_prepare
.component
= LTTNG_CONTEXT_PERF_COUNTERS
;
260 ret
= cpuhp_state_add_instance(lttng_hp_prepare
,
261 &perf_field
->cpuhp_prepare
.node
);
263 goto cpuhp_prepare_error
;
265 perf_field
->cpuhp_online
.component
= LTTNG_CONTEXT_PERF_COUNTERS
;
266 ret
= cpuhp_state_add_instance(lttng_hp_online
,
267 &perf_field
->cpuhp_online
.node
);
269 goto cpuhp_online_error
;
271 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
275 #ifdef CONFIG_HOTPLUG_CPU
276 perf_field
->nb
.notifier_call
=
277 lttng_perf_counter_cpu_hp_callback
;
278 perf_field
->nb
.priority
= 0;
279 register_cpu_notifier(&perf_field
->nb
);
282 for_each_online_cpu(cpu
) {
283 events
[cpu
] = perf_event_create_kernel_counter(attr
,
284 cpu
, NULL
, overflow_callback
, NULL
);
285 if (!events
[cpu
] || IS_ERR(events
[cpu
])) {
289 if (events
[cpu
]->state
== PERF_EVENT_STATE_ERROR
) {
295 perf_field
->hp_enable
= 1;
297 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
299 field
->destroy
= lttng_destroy_perf_counter_field
;
301 field
->event_field
.name
= name_alloc
;
302 field
->event_field
.type
.atype
= atype_integer
;
303 field
->event_field
.type
.u
.integer
.size
= sizeof(uint64_t) * CHAR_BIT
;
304 field
->event_field
.type
.u
.integer
.alignment
= lttng_alignof(uint64_t) * CHAR_BIT
;
305 field
->event_field
.type
.u
.integer
.signedness
= lttng_is_signed_type(uint64_t);
306 field
->event_field
.type
.u
.integer
.reverse_byte_order
= 0;
307 field
->event_field
.type
.u
.integer
.base
= 10;
308 field
->event_field
.type
.u
.integer
.encoding
= lttng_encode_none
;
309 field
->get_size
= perf_counter_get_size
;
310 field
->record
= perf_counter_record
;
311 field
->u
.perf_counter
= perf_field
;
312 lttng_context_update(*ctx
);
316 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
321 remove_ret
= cpuhp_state_remove_instance(lttng_hp_prepare
,
322 &perf_field
->cpuhp_prepare
.node
);
326 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
332 for_each_online_cpu(cpu
) {
333 if (events
[cpu
] && !IS_ERR(events
[cpu
]))
334 perf_event_release_kernel(events
[cpu
]);
337 #ifdef CONFIG_HOTPLUG_CPU
338 unregister_cpu_notifier(&perf_field
->nb
);
341 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
343 lttng_remove_context_field(ctx
, field
);
344 append_context_error
:
348 error_alloc_perf_field
: