Add cpu hotplug support for perf counters
[lttng-modules.git] / lttng-context-perf-counters.c
CommitLineData
833ad6a0
MD
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>
c24a0d71
MD
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"
833ad6a0 19
f1676205
MD
20static
21size_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
833ad6a0
MD
30static
31void 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);
9e7e4892 41 lib_ring_buffer_align_ctx(ctx, ltt_alignof(value));
833ad6a0
MD
42 chan->ops->event_write(ctx, &value, sizeof(value));
43}
44
45static
46void overflow_callback(struct perf_event *event, int nmi,
47 struct perf_sample_data *data,
48 struct pt_regs *regs)
49{
50}
51
2dccf128
MD
52static
53void 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
8289661d 58 get_online_cpus();
2dccf128
MD
59 for_each_online_cpu(cpu)
60 perf_event_release_kernel(events[cpu]);
8289661d
MD
61 put_online_cpus();
62#ifdef CONFIG_HOTPLUG_CPU
63 unregister_cpu_notifier(&field->u.perf_counter.nb);
64#endif
c24a0d71 65 kfree(field->event_field.name);
2dccf128
MD
66 kfree(field->u.perf_counter.attr);
67 kfree(events);
68}
69
8289661d
MD
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 */
83static
84int __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
833ad6a0
MD
118int lttng_add_perf_counter_to_ctx(uint32_t type,
119 uint64_t config,
c24a0d71 120 const char *name,
2dccf128 121 struct lttng_ctx **ctx)
833ad6a0
MD
122{
123 struct lttng_ctx_field *field;
124 struct perf_event **events;
125 struct perf_event_attr *attr;
126 int ret;
127 int cpu;
c24a0d71 128 char *name_alloc;
833ad6a0
MD
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
c24a0d71 146 name_alloc = kstrdup(name, GFP_KERNEL);
bef96e48
MD
147 if (!name_alloc) {
148 ret = -ENOMEM;
c24a0d71 149 goto name_alloc_error;
bef96e48 150 }
c24a0d71 151
2dccf128
MD
152 field = lttng_append_context(ctx);
153 if (!field) {
154 ret = -ENOMEM;
8289661d 155 goto append_context_error;
833ad6a0 156 }
8289661d
MD
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
2dccf128 176 field->destroy = lttng_destroy_perf_counter_field;
833ad6a0 177
c24a0d71 178 field->event_field.name = name_alloc;
8070f5c0
MD
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;
f1676205
MD
186 field->get_size = perf_counter_get_size;
187 field->record = perf_counter_record;
833ad6a0
MD
188 field->u.perf_counter.e = events;
189 field->u.perf_counter.attr = attr;
8289661d 190 field->u.perf_counter.hp_enable = 1;
833ad6a0
MD
191
192 wrapper_vmalloc_sync_all();
193 return 0;
194
8289661d 195counter_error:
833ad6a0
MD
196 for_each_online_cpu(cpu) {
197 if (events[cpu])
198 perf_event_release_kernel(events[cpu]);
199 }
8289661d
MD
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);
205append_context_error:
206 kfree(name_alloc);
207name_alloc_error:
833ad6a0
MD
208 kfree(attr);
209error_attr:
210 kfree(events);
211 return ret;
212}
213
833ad6a0
MD
214MODULE_LICENSE("GPL and additional rights");
215MODULE_AUTHOR("Mathieu Desnoyers");
216MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");
This page took 0.031696 seconds and 4 git commands to generate.