Fix 3.8 kernel support: namespace lttng_is_signed_type()
[lttng-modules.git] / lttng-context-perf-counters.c
1 /*
2 * lttng-context-perf-counters.c
3 *
4 * LTTng performance monitoring counters (perf-counters) integration module.
5 *
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
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"
33
34 static
35 size_t perf_counter_get_size(size_t offset)
36 {
37 size_t size = 0;
38
39 size += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
40 size += sizeof(uint64_t);
41 return size;
42 }
43
44 static
45 void perf_counter_record(struct lttng_ctx_field *field,
46 struct lib_ring_buffer_ctx *ctx,
47 struct lttng_channel *chan)
48 {
49 struct perf_event *event;
50 uint64_t value;
51
52 event = field->u.perf_counter->e[ctx->cpu];
53 if (likely(event)) {
54 if (unlikely(event->state == PERF_EVENT_STATE_ERROR)) {
55 value = 0;
56 } else {
57 event->pmu->read(event);
58 value = local64_read(&event->count);
59 }
60 } else {
61 /*
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
66 * case.
67 */
68 value = 0;
69 }
70 lib_ring_buffer_align_ctx(ctx, lttng_alignof(value));
71 chan->ops->event_write(ctx, &value, sizeof(value));
72 }
73
74 #if defined(CONFIG_PERF_EVENTS) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,99))
75 static
76 void overflow_callback(struct perf_event *event,
77 struct perf_sample_data *data,
78 struct pt_regs *regs)
79 {
80 }
81 #else
82 static
83 void overflow_callback(struct perf_event *event, int nmi,
84 struct perf_sample_data *data,
85 struct pt_regs *regs)
86 {
87 }
88 #endif
89
90 static
91 void lttng_destroy_perf_counter_field(struct lttng_ctx_field *field)
92 {
93 struct perf_event **events = field->u.perf_counter->e;
94 int cpu;
95
96 get_online_cpus();
97 for_each_online_cpu(cpu)
98 perf_event_release_kernel(events[cpu]);
99 put_online_cpus();
100 #ifdef CONFIG_HOTPLUG_CPU
101 unregister_cpu_notifier(&field->u.perf_counter->nb);
102 #endif
103 kfree(field->event_field.name);
104 kfree(field->u.perf_counter->attr);
105 kfree(events);
106 kfree(field->u.perf_counter);
107 }
108
109 #ifdef CONFIG_HOTPLUG_CPU
110
111 /**
112 * lttng_perf_counter_hp_callback - CPU hotplug callback
113 * @nb: notifier block
114 * @action: hotplug action to take
115 * @hcpu: CPU number
116 *
117 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
118 *
119 * We can setup perf counters when the cpu is online (up prepare seems to be too
120 * soon).
121 */
122 static
123 int __cpuinit lttng_perf_counter_cpu_hp_callback(struct notifier_block *nb,
124 unsigned long action,
125 void *hcpu)
126 {
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;
133
134 if (!perf_field->hp_enable)
135 return NOTIFY_OK;
136
137 switch (action) {
138 case CPU_ONLINE:
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))
143 return NOTIFY_BAD;
144 if (pevent->state == PERF_EVENT_STATE_ERROR) {
145 perf_event_release_kernel(pevent);
146 return NOTIFY_BAD;
147 }
148 barrier(); /* Create perf counter before setting event */
149 events[cpu] = pevent;
150 break;
151 case CPU_UP_CANCELED:
152 case CPU_UP_CANCELED_FROZEN:
153 case CPU_DEAD:
154 case CPU_DEAD_FROZEN:
155 pevent = events[cpu];
156 events[cpu] = NULL;
157 barrier(); /* NULLify event before perf counter teardown */
158 perf_event_release_kernel(pevent);
159 break;
160 }
161 return NOTIFY_OK;
162 }
163
164 #endif
165
166 int lttng_add_perf_counter_to_ctx(uint32_t type,
167 uint64_t config,
168 const char *name,
169 struct lttng_ctx **ctx)
170 {
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;
175 int ret;
176 int cpu;
177 char *name_alloc;
178
179 events = kzalloc(num_possible_cpus() * sizeof(*events), GFP_KERNEL);
180 if (!events)
181 return -ENOMEM;
182
183 attr = kzalloc(sizeof(struct perf_event_attr), GFP_KERNEL);
184 if (!attr) {
185 ret = -ENOMEM;
186 goto error_attr;
187 }
188
189 attr->type = type;
190 attr->config = config;
191 attr->size = sizeof(struct perf_event_attr);
192 attr->pinned = 1;
193 attr->disabled = 0;
194
195 perf_field = kzalloc(sizeof(struct lttng_perf_counter_field), GFP_KERNEL);
196 if (!perf_field) {
197 ret = -ENOMEM;
198 goto error_alloc_perf_field;
199 }
200 perf_field->e = events;
201 perf_field->attr = attr;
202
203 name_alloc = kstrdup(name, GFP_KERNEL);
204 if (!name_alloc) {
205 ret = -ENOMEM;
206 goto name_alloc_error;
207 }
208
209 field = lttng_append_context(ctx);
210 if (!field) {
211 ret = -ENOMEM;
212 goto append_context_error;
213 }
214 if (lttng_find_context(*ctx, name_alloc)) {
215 ret = -EEXIST;
216 goto find_error;
217 }
218
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);
224 #endif
225
226 get_online_cpus();
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])) {
231 ret = -EINVAL;
232 goto counter_error;
233 }
234 if (events[cpu]->state == PERF_EVENT_STATE_ERROR) {
235 ret = -EBUSY;
236 goto counter_busy;
237 }
238 }
239 put_online_cpus();
240
241 field->destroy = lttng_destroy_perf_counter_field;
242
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 = lttng_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;
255
256 wrapper_vmalloc_sync_all();
257 return 0;
258
259 counter_busy:
260 counter_error:
261 for_each_online_cpu(cpu) {
262 if (events[cpu] && !IS_ERR(events[cpu]))
263 perf_event_release_kernel(events[cpu]);
264 }
265 put_online_cpus();
266 #ifdef CONFIG_HOTPLUG_CPU
267 unregister_cpu_notifier(&perf_field->nb);
268 #endif
269 find_error:
270 lttng_remove_context_field(ctx, field);
271 append_context_error:
272 kfree(name_alloc);
273 name_alloc_error:
274 kfree(perf_field);
275 error_alloc_perf_field:
276 kfree(attr);
277 error_attr:
278 kfree(events);
279 return ret;
280 }
281
282 MODULE_LICENSE("GPL and additional rights");
283 MODULE_AUTHOR("Mathieu Desnoyers");
284 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");
This page took 0.035609 seconds and 5 git commands to generate.