Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
9f36eaed | 2 | * |
886d51a3 | 3 | * lttng-context-perf-counters.c |
833ad6a0 MD |
4 | * |
5 | * LTTng performance monitoring counters (perf-counters) integration module. | |
6 | * | |
886d51a3 | 7 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
833ad6a0 MD |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/perf_event.h> | |
13 | #include <linux/list.h> | |
c24a0d71 | 14 | #include <linux/string.h> |
5ca7b8a3 | 15 | #include <linux/cpu.h> |
c50bdba2 | 16 | #include <linux/mm.h> |
241ae9a8 MD |
17 | #include <lttng-events.h> |
18 | #include <wrapper/ringbuffer/frontend_types.h> | |
241ae9a8 | 19 | #include <lttng-tracer.h> |
833ad6a0 | 20 | |
f1676205 MD |
21 | static |
22 | size_t perf_counter_get_size(size_t offset) | |
23 | { | |
24 | size_t size = 0; | |
25 | ||
a90917c3 | 26 | size += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
f1676205 MD |
27 | size += sizeof(uint64_t); |
28 | return size; | |
29 | } | |
30 | ||
833ad6a0 MD |
31 | static |
32 | void perf_counter_record(struct lttng_ctx_field *field, | |
33 | struct lib_ring_buffer_ctx *ctx, | |
a90917c3 | 34 | struct lttng_channel *chan) |
833ad6a0 MD |
35 | { |
36 | struct perf_event *event; | |
37 | uint64_t value; | |
38 | ||
2001023e | 39 | event = field->u.perf_counter->e[ctx->cpu]; |
0478c519 | 40 | if (likely(event)) { |
7b745a96 MD |
41 | if (unlikely(event->state == PERF_EVENT_STATE_ERROR)) { |
42 | value = 0; | |
43 | } else { | |
44 | event->pmu->read(event); | |
45 | value = local64_read(&event->count); | |
46 | } | |
f91fd73b MD |
47 | } else { |
48 | /* | |
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 | |
53 | * case. | |
54 | */ | |
55 | value = 0; | |
56 | } | |
a90917c3 | 57 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(value)); |
833ad6a0 MD |
58 | chan->ops->event_write(ctx, &value, sizeof(value)); |
59 | } | |
60 | ||
90f5546c MD |
61 | #if defined(CONFIG_PERF_EVENTS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,99)) |
62 | static | |
63 | void overflow_callback(struct perf_event *event, | |
64 | struct perf_sample_data *data, | |
65 | struct pt_regs *regs) | |
66 | { | |
67 | } | |
68 | #else | |
833ad6a0 MD |
69 | static |
70 | void overflow_callback(struct perf_event *event, int nmi, | |
71 | struct perf_sample_data *data, | |
72 | struct pt_regs *regs) | |
73 | { | |
74 | } | |
90f5546c | 75 | #endif |
833ad6a0 | 76 | |
2dccf128 MD |
77 | static |
78 | void lttng_destroy_perf_counter_field(struct lttng_ctx_field *field) | |
79 | { | |
2001023e | 80 | struct perf_event **events = field->u.perf_counter->e; |
2dccf128 | 81 | |
1e367326 MD |
82 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
83 | { | |
84 | int ret; | |
85 | ||
86 | ret = cpuhp_state_remove_instance(lttng_hp_online, | |
87 | &field->u.perf_counter->cpuhp_online.node); | |
88 | WARN_ON(ret); | |
89 | ret = cpuhp_state_remove_instance(lttng_hp_prepare, | |
90 | &field->u.perf_counter->cpuhp_prepare.node); | |
91 | WARN_ON(ret); | |
92 | } | |
93 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ | |
94 | { | |
95 | int cpu; | |
96 | ||
97 | get_online_cpus(); | |
98 | for_each_online_cpu(cpu) | |
99 | perf_event_release_kernel(events[cpu]); | |
100 | put_online_cpus(); | |
8289661d | 101 | #ifdef CONFIG_HOTPLUG_CPU |
1e367326 | 102 | unregister_cpu_notifier(&field->u.perf_counter->nb); |
8289661d | 103 | #endif |
1e367326 MD |
104 | } |
105 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ | |
c24a0d71 | 106 | kfree(field->event_field.name); |
2001023e | 107 | kfree(field->u.perf_counter->attr); |
c50bdba2 | 108 | kvfree(events); |
2001023e | 109 | kfree(field->u.perf_counter); |
2dccf128 MD |
110 | } |
111 | ||
1e367326 MD |
112 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
113 | ||
114 | int lttng_cpuhp_perf_counter_online(unsigned int cpu, | |
115 | struct lttng_cpuhp_node *node) | |
116 | { | |
117 | struct lttng_perf_counter_field *perf_field = | |
118 | container_of(node, struct lttng_perf_counter_field, | |
119 | cpuhp_online); | |
120 | struct perf_event **events = perf_field->e; | |
121 | struct perf_event_attr *attr = perf_field->attr; | |
122 | struct perf_event *pevent; | |
123 | ||
1cacbdc9 MD |
124 | pevent = perf_event_create_kernel_counter(attr, |
125 | cpu, NULL, overflow_callback, NULL); | |
1e367326 MD |
126 | if (!pevent || IS_ERR(pevent)) |
127 | return -EINVAL; | |
128 | if (pevent->state == PERF_EVENT_STATE_ERROR) { | |
129 | perf_event_release_kernel(pevent); | |
130 | return -EINVAL; | |
131 | } | |
132 | barrier(); /* Create perf counter before setting event */ | |
133 | events[cpu] = pevent; | |
134 | return 0; | |
135 | } | |
136 | ||
137 | int lttng_cpuhp_perf_counter_dead(unsigned int cpu, | |
138 | struct lttng_cpuhp_node *node) | |
139 | { | |
140 | struct lttng_perf_counter_field *perf_field = | |
141 | container_of(node, struct lttng_perf_counter_field, | |
142 | cpuhp_prepare); | |
143 | struct perf_event **events = perf_field->e; | |
144 | struct perf_event *pevent; | |
145 | ||
146 | pevent = events[cpu]; | |
147 | events[cpu] = NULL; | |
148 | barrier(); /* NULLify event before perf counter teardown */ | |
149 | perf_event_release_kernel(pevent); | |
150 | return 0; | |
151 | } | |
152 | ||
153 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ | |
154 | ||
8289661d MD |
155 | #ifdef CONFIG_HOTPLUG_CPU |
156 | ||
157 | /** | |
158 | * lttng_perf_counter_hp_callback - CPU hotplug callback | |
159 | * @nb: notifier block | |
160 | * @action: hotplug action to take | |
161 | * @hcpu: CPU number | |
162 | * | |
163 | * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) | |
164 | * | |
165 | * We can setup perf counters when the cpu is online (up prepare seems to be too | |
166 | * soon). | |
167 | */ | |
168 | static | |
e8f071d5 | 169 | int lttng_perf_counter_cpu_hp_callback(struct notifier_block *nb, |
8289661d MD |
170 | unsigned long action, |
171 | void *hcpu) | |
172 | { | |
173 | unsigned int cpu = (unsigned long) hcpu; | |
2001023e MD |
174 | struct lttng_perf_counter_field *perf_field = |
175 | container_of(nb, struct lttng_perf_counter_field, nb); | |
176 | struct perf_event **events = perf_field->e; | |
177 | struct perf_event_attr *attr = perf_field->attr; | |
f91fd73b | 178 | struct perf_event *pevent; |
8289661d | 179 | |
2001023e | 180 | if (!perf_field->hp_enable) |
8289661d MD |
181 | return NOTIFY_OK; |
182 | ||
183 | switch (action) { | |
184 | case CPU_ONLINE: | |
185 | case CPU_ONLINE_FROZEN: | |
1cacbdc9 MD |
186 | pevent = perf_event_create_kernel_counter(attr, |
187 | cpu, NULL, overflow_callback, NULL); | |
0478c519 | 188 | if (!pevent || IS_ERR(pevent)) |
8289661d | 189 | return NOTIFY_BAD; |
7b745a96 MD |
190 | if (pevent->state == PERF_EVENT_STATE_ERROR) { |
191 | perf_event_release_kernel(pevent); | |
192 | return NOTIFY_BAD; | |
193 | } | |
f91fd73b MD |
194 | barrier(); /* Create perf counter before setting event */ |
195 | events[cpu] = pevent; | |
8289661d MD |
196 | break; |
197 | case CPU_UP_CANCELED: | |
198 | case CPU_UP_CANCELED_FROZEN: | |
199 | case CPU_DEAD: | |
200 | case CPU_DEAD_FROZEN: | |
f91fd73b MD |
201 | pevent = events[cpu]; |
202 | events[cpu] = NULL; | |
203 | barrier(); /* NULLify event before perf counter teardown */ | |
204 | perf_event_release_kernel(pevent); | |
8289661d MD |
205 | break; |
206 | } | |
207 | return NOTIFY_OK; | |
208 | } | |
209 | ||
210 | #endif | |
211 | ||
1e367326 MD |
212 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
213 | ||
833ad6a0 MD |
214 | int lttng_add_perf_counter_to_ctx(uint32_t type, |
215 | uint64_t config, | |
c24a0d71 | 216 | const char *name, |
2dccf128 | 217 | struct lttng_ctx **ctx) |
833ad6a0 MD |
218 | { |
219 | struct lttng_ctx_field *field; | |
2001023e | 220 | struct lttng_perf_counter_field *perf_field; |
833ad6a0 MD |
221 | struct perf_event **events; |
222 | struct perf_event_attr *attr; | |
223 | int ret; | |
c24a0d71 | 224 | char *name_alloc; |
833ad6a0 | 225 | |
c50bdba2 | 226 | events = kvzalloc(num_possible_cpus() * sizeof(*events), GFP_KERNEL); |
833ad6a0 MD |
227 | if (!events) |
228 | return -ENOMEM; | |
229 | ||
2001023e | 230 | attr = kzalloc(sizeof(struct perf_event_attr), GFP_KERNEL); |
833ad6a0 MD |
231 | if (!attr) { |
232 | ret = -ENOMEM; | |
233 | goto error_attr; | |
234 | } | |
235 | ||
236 | attr->type = type; | |
237 | attr->config = config; | |
238 | attr->size = sizeof(struct perf_event_attr); | |
239 | attr->pinned = 1; | |
240 | attr->disabled = 0; | |
241 | ||
2001023e MD |
242 | perf_field = kzalloc(sizeof(struct lttng_perf_counter_field), GFP_KERNEL); |
243 | if (!perf_field) { | |
244 | ret = -ENOMEM; | |
245 | goto error_alloc_perf_field; | |
246 | } | |
247 | perf_field->e = events; | |
248 | perf_field->attr = attr; | |
249 | ||
c24a0d71 | 250 | name_alloc = kstrdup(name, GFP_KERNEL); |
bef96e48 MD |
251 | if (!name_alloc) { |
252 | ret = -ENOMEM; | |
c24a0d71 | 253 | goto name_alloc_error; |
bef96e48 | 254 | } |
c24a0d71 | 255 | |
2dccf128 MD |
256 | field = lttng_append_context(ctx); |
257 | if (!field) { | |
258 | ret = -ENOMEM; | |
8289661d | 259 | goto append_context_error; |
833ad6a0 | 260 | } |
2001023e | 261 | if (lttng_find_context(*ctx, name_alloc)) { |
44252f0f MD |
262 | ret = -EEXIST; |
263 | goto find_error; | |
264 | } | |
8289661d | 265 | |
1e367326 MD |
266 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
267 | ||
268 | perf_field->cpuhp_prepare.component = LTTNG_CONTEXT_PERF_COUNTERS; | |
269 | ret = cpuhp_state_add_instance(lttng_hp_prepare, | |
270 | &perf_field->cpuhp_prepare.node); | |
271 | if (ret) | |
272 | goto cpuhp_prepare_error; | |
273 | ||
274 | perf_field->cpuhp_online.component = LTTNG_CONTEXT_PERF_COUNTERS; | |
275 | ret = cpuhp_state_add_instance(lttng_hp_online, | |
276 | &perf_field->cpuhp_online.node); | |
277 | if (ret) | |
278 | goto cpuhp_online_error; | |
279 | ||
280 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ | |
281 | { | |
282 | int cpu; | |
283 | ||
8289661d | 284 | #ifdef CONFIG_HOTPLUG_CPU |
1e367326 MD |
285 | perf_field->nb.notifier_call = |
286 | lttng_perf_counter_cpu_hp_callback; | |
287 | perf_field->nb.priority = 0; | |
288 | register_cpu_notifier(&perf_field->nb); | |
8289661d | 289 | #endif |
1e367326 MD |
290 | get_online_cpus(); |
291 | for_each_online_cpu(cpu) { | |
1cacbdc9 MD |
292 | events[cpu] = perf_event_create_kernel_counter(attr, |
293 | cpu, NULL, overflow_callback, NULL); | |
1e367326 MD |
294 | if (!events[cpu] || IS_ERR(events[cpu])) { |
295 | ret = -EINVAL; | |
296 | goto counter_error; | |
297 | } | |
298 | if (events[cpu]->state == PERF_EVENT_STATE_ERROR) { | |
299 | ret = -EBUSY; | |
300 | goto counter_busy; | |
301 | } | |
7b745a96 | 302 | } |
1e367326 MD |
303 | put_online_cpus(); |
304 | perf_field->hp_enable = 1; | |
8289661d | 305 | } |
1e367326 | 306 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
8289661d | 307 | |
2dccf128 | 308 | field->destroy = lttng_destroy_perf_counter_field; |
833ad6a0 | 309 | |
c24a0d71 | 310 | field->event_field.name = name_alloc; |
8070f5c0 | 311 | field->event_field.type.atype = atype_integer; |
ceabb767 MD |
312 | field->event_field.type.u.integer.size = sizeof(uint64_t) * CHAR_BIT; |
313 | field->event_field.type.u.integer.alignment = lttng_alignof(uint64_t) * CHAR_BIT; | |
314 | field->event_field.type.u.integer.signedness = lttng_is_signed_type(uint64_t); | |
315 | field->event_field.type.u.integer.reverse_byte_order = 0; | |
316 | field->event_field.type.u.integer.base = 10; | |
317 | field->event_field.type.u.integer.encoding = lttng_encode_none; | |
f1676205 MD |
318 | field->get_size = perf_counter_get_size; |
319 | field->record = perf_counter_record; | |
2001023e | 320 | field->u.perf_counter = perf_field; |
a9dd15da | 321 | lttng_context_update(*ctx); |
833ad6a0 | 322 | |
833ad6a0 MD |
323 | return 0; |
324 | ||
1e367326 MD |
325 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
326 | cpuhp_online_error: | |
327 | { | |
328 | int remove_ret; | |
329 | ||
330 | remove_ret = cpuhp_state_remove_instance(lttng_hp_prepare, | |
331 | &perf_field->cpuhp_prepare.node); | |
332 | WARN_ON(remove_ret); | |
333 | } | |
334 | cpuhp_prepare_error: | |
335 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ | |
7b745a96 | 336 | counter_busy: |
8289661d | 337 | counter_error: |
ce4a2f0c MD |
338 | { |
339 | int cpu; | |
340 | ||
341 | for_each_online_cpu(cpu) { | |
342 | if (events[cpu] && !IS_ERR(events[cpu])) | |
343 | perf_event_release_kernel(events[cpu]); | |
344 | } | |
345 | put_online_cpus(); | |
8289661d | 346 | #ifdef CONFIG_HOTPLUG_CPU |
ce4a2f0c | 347 | unregister_cpu_notifier(&perf_field->nb); |
8289661d | 348 | #endif |
ce4a2f0c | 349 | } |
1e367326 | 350 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
44252f0f | 351 | find_error: |
8289661d MD |
352 | lttng_remove_context_field(ctx, field); |
353 | append_context_error: | |
354 | kfree(name_alloc); | |
355 | name_alloc_error: | |
2001023e MD |
356 | kfree(perf_field); |
357 | error_alloc_perf_field: | |
833ad6a0 MD |
358 | kfree(attr); |
359 | error_attr: | |
c50bdba2 | 360 | kvfree(events); |
833ad6a0 MD |
361 | return ret; |
362 | } |