2 * lttng-context-perf-counters.c
4 * LTTng performance monitoring counters (perf-counters) integration module.
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/perf_event.h>
26 #include <linux/list.h>
27 #include <linux/string.h>
28 #include "lttng-events.h"
29 #include "wrapper/ringbuffer/frontend_types.h"
30 #include "wrapper/vmalloc.h"
31 #include "wrapper/perf.h"
32 #include "lttng-tracer.h"
35 size_t perf_counter_get_size(size_t offset
)
39 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
40 size
+= sizeof(uint64_t);
45 void perf_counter_record(struct lttng_ctx_field
*field
,
46 struct lib_ring_buffer_ctx
*ctx
,
47 struct lttng_channel
*chan
)
49 struct perf_event
*event
;
52 event
= field
->u
.perf_counter
->e
[ctx
->cpu
];
54 if (unlikely(event
->state
== PERF_EVENT_STATE_ERROR
)) {
57 event
->pmu
->read(event
);
58 value
= local64_read(&event
->count
);
62 * Perf chooses not to be clever and not to support enabling a
63 * perf counter before the cpu is brought up. Therefore, we need
64 * to support having events coming (e.g. scheduler events)
65 * before the counter is setup. Write an arbitrary 0 in this
70 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
71 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
74 #if defined(CONFIG_PERF_EVENTS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,99))
76 void overflow_callback(struct perf_event
*event
,
77 struct perf_sample_data
*data
,
83 void overflow_callback(struct perf_event
*event
, int nmi
,
84 struct perf_sample_data
*data
,
91 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
93 struct perf_event
**events
= field
->u
.perf_counter
->e
;
97 for_each_online_cpu(cpu
)
98 perf_event_release_kernel(events
[cpu
]);
100 #ifdef CONFIG_HOTPLUG_CPU
101 unregister_cpu_notifier(&field
->u
.perf_counter
->nb
);
103 kfree(field
->event_field
.name
);
104 kfree(field
->u
.perf_counter
->attr
);
106 kfree(field
->u
.perf_counter
);
109 #ifdef CONFIG_HOTPLUG_CPU
112 * lttng_perf_counter_hp_callback - CPU hotplug callback
113 * @nb: notifier block
114 * @action: hotplug action to take
117 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
119 * We can setup perf counters when the cpu is online (up prepare seems to be too
123 int __cpuinit
lttng_perf_counter_cpu_hp_callback(struct notifier_block
*nb
,
124 unsigned long action
,
127 unsigned int cpu
= (unsigned long) hcpu
;
128 struct lttng_perf_counter_field
*perf_field
=
129 container_of(nb
, struct lttng_perf_counter_field
, nb
);
130 struct perf_event
**events
= perf_field
->e
;
131 struct perf_event_attr
*attr
= perf_field
->attr
;
132 struct perf_event
*pevent
;
134 if (!perf_field
->hp_enable
)
139 case CPU_ONLINE_FROZEN
:
140 pevent
= wrapper_perf_event_create_kernel_counter(attr
,
141 cpu
, NULL
, overflow_callback
);
142 if (!pevent
|| IS_ERR(pevent
))
144 if (pevent
->state
== PERF_EVENT_STATE_ERROR
) {
145 perf_event_release_kernel(pevent
);
148 barrier(); /* Create perf counter before setting event */
149 events
[cpu
] = pevent
;
151 case CPU_UP_CANCELED
:
152 case CPU_UP_CANCELED_FROZEN
:
154 case CPU_DEAD_FROZEN
:
155 pevent
= events
[cpu
];
157 barrier(); /* NULLify event before perf counter teardown */
158 perf_event_release_kernel(pevent
);
166 int lttng_add_perf_counter_to_ctx(uint32_t type
,
169 struct lttng_ctx
**ctx
)
171 struct lttng_ctx_field
*field
;
172 struct lttng_perf_counter_field
*perf_field
;
173 struct perf_event
**events
;
174 struct perf_event_attr
*attr
;
179 events
= kzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
183 attr
= kzalloc(sizeof(struct perf_event_attr
), GFP_KERNEL
);
190 attr
->config
= config
;
191 attr
->size
= sizeof(struct perf_event_attr
);
195 perf_field
= kzalloc(sizeof(struct lttng_perf_counter_field
), GFP_KERNEL
);
198 goto error_alloc_perf_field
;
200 perf_field
->e
= events
;
201 perf_field
->attr
= attr
;
203 name_alloc
= kstrdup(name
, GFP_KERNEL
);
206 goto name_alloc_error
;
209 field
= lttng_append_context(ctx
);
212 goto append_context_error
;
214 if (lttng_find_context(*ctx
, name_alloc
)) {
219 #ifdef CONFIG_HOTPLUG_CPU
220 perf_field
->nb
.notifier_call
=
221 lttng_perf_counter_cpu_hp_callback
;
222 perf_field
->nb
.priority
= 0;
223 register_cpu_notifier(&perf_field
->nb
);
227 for_each_online_cpu(cpu
) {
228 events
[cpu
] = wrapper_perf_event_create_kernel_counter(attr
,
229 cpu
, NULL
, overflow_callback
);
230 if (!events
[cpu
] || IS_ERR(events
[cpu
])) {
234 if (events
[cpu
]->state
== PERF_EVENT_STATE_ERROR
) {
241 field
->destroy
= lttng_destroy_perf_counter_field
;
243 field
->event_field
.name
= name_alloc
;
244 field
->event_field
.type
.atype
= atype_integer
;
245 field
->event_field
.type
.u
.basic
.integer
.size
= sizeof(uint64_t) * CHAR_BIT
;
246 field
->event_field
.type
.u
.basic
.integer
.alignment
= lttng_alignof(uint64_t) * CHAR_BIT
;
247 field
->event_field
.type
.u
.basic
.integer
.signedness
= is_signed_type(uint64_t);
248 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
249 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
250 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
251 field
->get_size
= perf_counter_get_size
;
252 field
->record
= perf_counter_record
;
253 field
->u
.perf_counter
= perf_field
;
254 perf_field
->hp_enable
= 1;
256 wrapper_vmalloc_sync_all();
261 for_each_online_cpu(cpu
) {
262 if (events
[cpu
] && !IS_ERR(events
[cpu
]))
263 perf_event_release_kernel(events
[cpu
]);
266 #ifdef CONFIG_HOTPLUG_CPU
267 unregister_cpu_notifier(&perf_field
->nb
);
270 lttng_remove_context_field(ctx
, field
);
271 append_context_error
:
275 error_alloc_perf_field
:
282 MODULE_LICENSE("GPL and additional rights");
283 MODULE_AUTHOR("Mathieu Desnoyers");
284 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");