2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * LTTng performance monitoring counters (perf-counters) integration module.
7 * Dual LGPL v2.1/GPL v2 license.
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 "lttng-events.h"
16 #include "wrapper/ringbuffer/frontend_types.h"
17 #include "wrapper/vmalloc.h"
18 #include "wrapper/perf.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
));
61 #if defined(CONFIG_PERF_EVENTS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,99))
63 void overflow_callback(struct perf_event
*event
,
64 struct perf_sample_data
*data
,
70 void overflow_callback(struct perf_event
*event
, int nmi
,
71 struct perf_sample_data
*data
,
78 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
80 struct perf_event
**events
= field
->u
.perf_counter
->e
;
84 for_each_online_cpu(cpu
)
85 perf_event_release_kernel(events
[cpu
]);
87 #ifdef CONFIG_HOTPLUG_CPU
88 unregister_cpu_notifier(&field
->u
.perf_counter
->nb
);
90 kfree(field
->event_field
.name
);
91 kfree(field
->u
.perf_counter
->attr
);
93 kfree(field
->u
.perf_counter
);
96 #ifdef CONFIG_HOTPLUG_CPU
99 * lttng_perf_counter_hp_callback - CPU hotplug callback
100 * @nb: notifier block
101 * @action: hotplug action to take
104 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
106 * We can setup perf counters when the cpu is online (up prepare seems to be too
110 int __cpuinit
lttng_perf_counter_cpu_hp_callback(struct notifier_block
*nb
,
111 unsigned long action
,
114 unsigned int cpu
= (unsigned long) hcpu
;
115 struct lttng_perf_counter_field
*perf_field
=
116 container_of(nb
, struct lttng_perf_counter_field
, nb
);
117 struct perf_event
**events
= perf_field
->e
;
118 struct perf_event_attr
*attr
= perf_field
->attr
;
119 struct perf_event
*pevent
;
121 if (!perf_field
->hp_enable
)
126 case CPU_ONLINE_FROZEN
:
127 pevent
= wrapper_perf_event_create_kernel_counter(attr
,
128 cpu
, NULL
, overflow_callback
);
129 if (!pevent
|| IS_ERR(pevent
))
131 if (pevent
->state
== PERF_EVENT_STATE_ERROR
) {
132 perf_event_release_kernel(pevent
);
135 barrier(); /* Create perf counter before setting event */
136 events
[cpu
] = pevent
;
138 case CPU_UP_CANCELED
:
139 case CPU_UP_CANCELED_FROZEN
:
141 case CPU_DEAD_FROZEN
:
142 pevent
= events
[cpu
];
144 barrier(); /* NULLify event before perf counter teardown */
145 perf_event_release_kernel(pevent
);
153 int lttng_add_perf_counter_to_ctx(uint32_t type
,
156 struct lttng_ctx
**ctx
)
158 struct lttng_ctx_field
*field
;
159 struct lttng_perf_counter_field
*perf_field
;
160 struct perf_event
**events
;
161 struct perf_event_attr
*attr
;
166 events
= kzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
170 attr
= kzalloc(sizeof(struct perf_event_attr
), GFP_KERNEL
);
177 attr
->config
= config
;
178 attr
->size
= sizeof(struct perf_event_attr
);
182 perf_field
= kzalloc(sizeof(struct lttng_perf_counter_field
), GFP_KERNEL
);
185 goto error_alloc_perf_field
;
187 perf_field
->e
= events
;
188 perf_field
->attr
= attr
;
190 name_alloc
= kstrdup(name
, GFP_KERNEL
);
193 goto name_alloc_error
;
196 field
= lttng_append_context(ctx
);
199 goto append_context_error
;
201 if (lttng_find_context(*ctx
, name_alloc
)) {
206 #ifdef CONFIG_HOTPLUG_CPU
207 perf_field
->nb
.notifier_call
=
208 lttng_perf_counter_cpu_hp_callback
;
209 perf_field
->nb
.priority
= 0;
210 register_cpu_notifier(&perf_field
->nb
);
214 for_each_online_cpu(cpu
) {
215 events
[cpu
] = wrapper_perf_event_create_kernel_counter(attr
,
216 cpu
, NULL
, overflow_callback
);
217 if (!events
[cpu
] || IS_ERR(events
[cpu
])) {
221 if (events
[cpu
]->state
== PERF_EVENT_STATE_ERROR
) {
228 field
->destroy
= lttng_destroy_perf_counter_field
;
230 field
->event_field
.name
= name_alloc
;
231 field
->event_field
.type
.atype
= atype_integer
;
232 field
->event_field
.type
.u
.basic
.integer
.size
= sizeof(uint64_t) * CHAR_BIT
;
233 field
->event_field
.type
.u
.basic
.integer
.alignment
= lttng_alignof(uint64_t) * CHAR_BIT
;
234 field
->event_field
.type
.u
.basic
.integer
.signedness
= is_signed_type(uint64_t);
235 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
236 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
237 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
238 field
->get_size
= perf_counter_get_size
;
239 field
->record
= perf_counter_record
;
240 field
->u
.perf_counter
= perf_field
;
241 perf_field
->hp_enable
= 1;
243 wrapper_vmalloc_sync_all();
248 for_each_online_cpu(cpu
) {
249 if (events
[cpu
] && !IS_ERR(events
[cpu
]))
250 perf_event_release_kernel(events
[cpu
]);
253 #ifdef CONFIG_HOTPLUG_CPU
254 unregister_cpu_notifier(&perf_field
->nb
);
257 lttng_remove_context_field(ctx
, field
);
258 append_context_error
:
262 error_alloc_perf_field
:
269 MODULE_LICENSE("GPL and additional rights");
270 MODULE_AUTHOR("Mathieu Desnoyers");
271 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");