Add cpu hotplug support for perf counters
[lttng-modules.git] / lttng-context-perf-counters.c
1 /*
2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * LTTng performance monitoring counters (perf-counters) integration module.
6 *
7 * Dual LGPL v2.1/GPL v2 license.
8 */
9
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"
19
20 static
21 size_t perf_counter_get_size(size_t offset)
22 {
23 size_t size = 0;
24
25 size += lib_ring_buffer_align(offset, ltt_alignof(uint64_t));
26 size += sizeof(uint64_t);
27 return size;
28 }
29
30 static
31 void perf_counter_record(struct lttng_ctx_field *field,
32 struct lib_ring_buffer_ctx *ctx,
33 struct ltt_channel *chan)
34 {
35 struct perf_event *event;
36 uint64_t value;
37
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));
43 }
44
45 static
46 void overflow_callback(struct perf_event *event, int nmi,
47 struct perf_sample_data *data,
48 struct pt_regs *regs)
49 {
50 }
51
52 static
53 void lttng_destroy_perf_counter_field(struct lttng_ctx_field *field)
54 {
55 struct perf_event **events = field->u.perf_counter.e;
56 int cpu;
57
58 get_online_cpus();
59 for_each_online_cpu(cpu)
60 perf_event_release_kernel(events[cpu]);
61 put_online_cpus();
62 #ifdef CONFIG_HOTPLUG_CPU
63 unregister_cpu_notifier(&field->u.perf_counter.nb);
64 #endif
65 kfree(field->event_field.name);
66 kfree(field->u.perf_counter.attr);
67 kfree(events);
68 }
69
70 #ifdef CONFIG_HOTPLUG_CPU
71
72 /**
73 * lttng_perf_counter_hp_callback - CPU hotplug callback
74 * @nb: notifier block
75 * @action: hotplug action to take
76 * @hcpu: CPU number
77 *
78 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
79 *
80 * We can setup perf counters when the cpu is online (up prepare seems to be too
81 * soon).
82 */
83 static
84 int __cpuinit lttng_perf_counter_cpu_hp_callback(struct notifier_block *nb,
85 unsigned long action,
86 void *hcpu)
87 {
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;
93
94
95 if (!field->u.perf_counter.hp_enable)
96 return NOTIFY_OK;
97
98 switch (action) {
99 case CPU_ONLINE:
100 case CPU_ONLINE_FROZEN:
101 events[cpu] = perf_event_create_kernel_counter(attr,
102 cpu, NULL, overflow_callback);
103 if (!events[cpu])
104 return NOTIFY_BAD;
105 break;
106 case CPU_UP_CANCELED:
107 case CPU_UP_CANCELED_FROZEN:
108 case CPU_DEAD:
109 case CPU_DEAD_FROZEN:
110 perf_event_release_kernel(events[cpu]);
111 break;
112 }
113 return NOTIFY_OK;
114 }
115
116 #endif
117
118 int lttng_add_perf_counter_to_ctx(uint32_t type,
119 uint64_t config,
120 const char *name,
121 struct lttng_ctx **ctx)
122 {
123 struct lttng_ctx_field *field;
124 struct perf_event **events;
125 struct perf_event_attr *attr;
126 int ret;
127 int cpu;
128 char *name_alloc;
129
130 events = kzalloc(num_possible_cpus() * sizeof(*events), GFP_KERNEL);
131 if (!events)
132 return -ENOMEM;
133
134 attr = kzalloc(sizeof(*field->u.perf_counter.attr), GFP_KERNEL);
135 if (!attr) {
136 ret = -ENOMEM;
137 goto error_attr;
138 }
139
140 attr->type = type;
141 attr->config = config;
142 attr->size = sizeof(struct perf_event_attr);
143 attr->pinned = 1;
144 attr->disabled = 0;
145
146 name_alloc = kstrdup(name, GFP_KERNEL);
147 if (!name_alloc) {
148 ret = -ENOMEM;
149 goto name_alloc_error;
150 }
151
152 field = lttng_append_context(ctx);
153 if (!field) {
154 ret = -ENOMEM;
155 goto append_context_error;
156 }
157
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);
163 #endif
164
165 get_online_cpus();
166 for_each_online_cpu(cpu) {
167 events[cpu] = perf_event_create_kernel_counter(attr,
168 cpu, NULL, overflow_callback);
169 if (!events[cpu]) {
170 ret = -EINVAL;
171 goto counter_error;
172 }
173 }
174 put_online_cpus();
175
176 field->destroy = lttng_destroy_perf_counter_field;
177
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;
191
192 wrapper_vmalloc_sync_all();
193 return 0;
194
195 counter_error:
196 for_each_online_cpu(cpu) {
197 if (events[cpu])
198 perf_event_release_kernel(events[cpu]);
199 }
200 put_online_cpus();
201 #ifdef CONFIG_HOTPLUG_CPU
202 unregister_cpu_notifier(&field->u.perf_counter.nb);
203 #endif
204 lttng_remove_context_field(ctx, field);
205 append_context_error:
206 kfree(name_alloc);
207 name_alloc_error:
208 kfree(attr);
209 error_attr:
210 kfree(events);
211 return ret;
212 }
213
214 MODULE_LICENSE("GPL and additional rights");
215 MODULE_AUTHOR("Mathieu Desnoyers");
216 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");
This page took 0.034254 seconds and 5 git commands to generate.