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 "ltt-events.h"
16 #include "wrapper/ringbuffer/frontend_types.h"
17 #include "wrapper/vmalloc.h"
18 #include "ltt-tracer.h"
21 size_t perf_counter_get_size(size_t offset
)
25 size
+= lib_ring_buffer_align(offset
, ltt_alignof(uint64_t));
26 size
+= sizeof(uint64_t);
31 void perf_counter_record(struct lttng_ctx_field
*field
,
32 struct lib_ring_buffer_ctx
*ctx
,
33 struct ltt_channel
*chan
)
35 struct perf_event
*event
;
38 event
= field
->u
.perf_counter
.e
[ctx
->cpu
];
39 event
->pmu
->read(event
);
40 value
= local64_read(&event
->count
);
41 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(value
));
42 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
46 void overflow_callback(struct perf_event
*event
, int nmi
,
47 struct perf_sample_data
*data
,
53 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
55 struct perf_event
**events
= field
->u
.perf_counter
.e
;
59 for_each_online_cpu(cpu
)
60 perf_event_release_kernel(events
[cpu
]);
62 #ifdef CONFIG_HOTPLUG_CPU
63 unregister_cpu_notifier(&field
->u
.perf_counter
.nb
);
65 kfree(field
->event_field
.name
);
66 kfree(field
->u
.perf_counter
.attr
);
70 #ifdef CONFIG_HOTPLUG_CPU
73 * lttng_perf_counter_hp_callback - CPU hotplug callback
75 * @action: hotplug action to take
78 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
80 * We can setup perf counters when the cpu is online (up prepare seems to be too
84 int __cpuinit
lttng_perf_counter_cpu_hp_callback(struct notifier_block
*nb
,
88 unsigned int cpu
= (unsigned long) hcpu
;
89 struct lttng_ctx_field
*field
=
90 container_of(nb
, struct lttng_ctx_field
, u
.perf_counter
.nb
);
91 struct perf_event
**events
= field
->u
.perf_counter
.e
;
92 struct perf_event_attr
*attr
= field
->u
.perf_counter
.attr
;
95 if (!field
->u
.perf_counter
.hp_enable
)
100 case CPU_ONLINE_FROZEN
:
101 events
[cpu
] = perf_event_create_kernel_counter(attr
,
102 cpu
, NULL
, overflow_callback
);
106 case CPU_UP_CANCELED
:
107 case CPU_UP_CANCELED_FROZEN
:
109 case CPU_DEAD_FROZEN
:
110 perf_event_release_kernel(events
[cpu
]);
118 int lttng_add_perf_counter_to_ctx(uint32_t type
,
121 struct lttng_ctx
**ctx
)
123 struct lttng_ctx_field
*field
;
124 struct perf_event
**events
;
125 struct perf_event_attr
*attr
;
130 events
= kzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
134 attr
= kzalloc(sizeof(*field
->u
.perf_counter
.attr
), GFP_KERNEL
);
141 attr
->config
= config
;
142 attr
->size
= sizeof(struct perf_event_attr
);
146 name_alloc
= kstrdup(name
, GFP_KERNEL
);
149 goto name_alloc_error
;
152 field
= lttng_append_context(ctx
);
155 goto append_context_error
;
158 #ifdef CONFIG_HOTPLUG_CPU
159 field
->u
.perf_counter
.nb
.notifier_call
=
160 lttng_perf_counter_cpu_hp_callback
;
161 field
->u
.perf_counter
.nb
.priority
= 0;
162 register_cpu_notifier(&field
->u
.perf_counter
.nb
);
166 for_each_online_cpu(cpu
) {
167 events
[cpu
] = perf_event_create_kernel_counter(attr
,
168 cpu
, NULL
, overflow_callback
);
176 field
->destroy
= lttng_destroy_perf_counter_field
;
178 field
->event_field
.name
= name_alloc
;
179 field
->event_field
.type
.atype
= atype_integer
;
180 field
->event_field
.type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
181 field
->event_field
.type
.u
.basic
.integer
.alignment
= ltt_alignof(unsigned long) * CHAR_BIT
;
182 field
->event_field
.type
.u
.basic
.integer
.signedness
= is_signed_type(unsigned long);
183 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
184 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
185 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
186 field
->get_size
= perf_counter_get_size
;
187 field
->record
= perf_counter_record
;
188 field
->u
.perf_counter
.e
= events
;
189 field
->u
.perf_counter
.attr
= attr
;
190 field
->u
.perf_counter
.hp_enable
= 1;
192 wrapper_vmalloc_sync_all();
196 for_each_online_cpu(cpu
) {
198 perf_event_release_kernel(events
[cpu
]);
201 #ifdef CONFIG_HOTPLUG_CPU
202 unregister_cpu_notifier(&field
->u
.perf_counter
.nb
);
204 lttng_remove_context_field(ctx
, field
);
205 append_context_error
:
214 MODULE_LICENSE("GPL and additional rights");
215 MODULE_AUTHOR("Mathieu Desnoyers");
216 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");