Commit | Line | Data |
---|---|---|
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> | |
14 | #include "../ltt-events.h" | |
15 | #include "../wrapper/ringbuffer/frontend_types.h" | |
16 | #include "../wrapper/vmalloc.h" | |
17 | #include "../ltt-tracer.h" | |
18 | ||
19 | /* | |
20 | * TODO: Add CPU hotplug support. | |
21 | */ | |
22 | ||
23 | static DEFINE_MUTEX(perf_counter_mutex); | |
24 | static LIST_HEAD(perf_counter_contexts); | |
25 | ||
26 | static | |
27 | void perf_counter_record(struct lttng_ctx_field *field, | |
28 | struct lib_ring_buffer_ctx *ctx, | |
29 | struct ltt_channel *chan) | |
30 | { | |
31 | struct perf_event *event; | |
32 | uint64_t value; | |
33 | ||
34 | event = field->u.perf_counter.e[ctx->cpu]; | |
35 | event->pmu->read(event); | |
36 | value = local64_read(&event->count); | |
37 | lib_ring_buffer_align_ctx(ctx, | |
38 | ltt_alignof(field->type.u.basic.integer.alignment / CHAR_BIT)); | |
39 | chan->ops->event_write(ctx, &value, sizeof(value)); | |
40 | } | |
41 | ||
42 | static | |
43 | void overflow_callback(struct perf_event *event, int nmi, | |
44 | struct perf_sample_data *data, | |
45 | struct pt_regs *regs) | |
46 | { | |
47 | } | |
48 | ||
2dccf128 MD |
49 | static |
50 | void lttng_destroy_perf_counter_field(struct lttng_ctx_field *field) | |
51 | { | |
52 | struct perf_event **events = field->u.perf_counter.e; | |
53 | int cpu; | |
54 | ||
55 | mutex_lock(&perf_counter_mutex); | |
56 | list_del(&field->u.perf_counter.head); | |
57 | for_each_online_cpu(cpu) | |
58 | perf_event_release_kernel(events[cpu]); | |
59 | mutex_unlock(&perf_counter_mutex); | |
60 | kfree(field->u.perf_counter.attr); | |
61 | kfree(events); | |
62 | } | |
63 | ||
833ad6a0 MD |
64 | int lttng_add_perf_counter_to_ctx(uint32_t type, |
65 | uint64_t config, | |
2dccf128 | 66 | struct lttng_ctx **ctx) |
833ad6a0 MD |
67 | { |
68 | struct lttng_ctx_field *field; | |
69 | struct perf_event **events; | |
70 | struct perf_event_attr *attr; | |
71 | int ret; | |
72 | int cpu; | |
73 | ||
74 | events = kzalloc(num_possible_cpus() * sizeof(*events), GFP_KERNEL); | |
75 | if (!events) | |
76 | return -ENOMEM; | |
77 | ||
78 | attr = kzalloc(sizeof(*field->u.perf_counter.attr), GFP_KERNEL); | |
79 | if (!attr) { | |
80 | ret = -ENOMEM; | |
81 | goto error_attr; | |
82 | } | |
83 | ||
84 | attr->type = type; | |
85 | attr->config = config; | |
86 | attr->size = sizeof(struct perf_event_attr); | |
87 | attr->pinned = 1; | |
88 | attr->disabled = 0; | |
89 | ||
90 | mutex_lock(&perf_counter_mutex); | |
91 | ||
92 | for_each_online_cpu(cpu) { | |
93 | events[cpu] = perf_event_create_kernel_counter(attr, | |
94 | cpu, NULL, overflow_callback); | |
95 | if (!events[cpu]) { | |
96 | ret = -EINVAL; | |
97 | goto error; | |
98 | } | |
99 | } | |
100 | ||
2dccf128 MD |
101 | field = lttng_append_context(ctx); |
102 | if (!field) { | |
103 | ret = -ENOMEM; | |
104 | goto error; | |
833ad6a0 | 105 | } |
2dccf128 | 106 | field->destroy = lttng_destroy_perf_counter_field; |
833ad6a0 MD |
107 | |
108 | field->name = "dummyname";//TODO: lookup_counter_name(type, config); | |
109 | field->type.atype = atype_integer; | |
110 | field->type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT; | |
111 | field->type.u.basic.integer.alignment = ltt_alignof(unsigned long) * CHAR_BIT; | |
112 | field->type.u.basic.integer.signedness = 0; | |
113 | field->type.u.basic.integer.reverse_byte_order = 0; | |
114 | field->type.u.basic.integer.base = 10; | |
115 | field->type.u.basic.integer.encoding = lttng_encode_none; | |
116 | field->callback = perf_counter_record; | |
117 | field->u.perf_counter.e = events; | |
118 | field->u.perf_counter.attr = attr; | |
119 | ||
120 | list_add(&field->u.perf_counter.head, &perf_counter_contexts); | |
121 | mutex_unlock(&perf_counter_mutex); | |
122 | ||
123 | wrapper_vmalloc_sync_all(); | |
124 | return 0; | |
125 | ||
126 | error: | |
127 | for_each_online_cpu(cpu) { | |
128 | if (events[cpu]) | |
129 | perf_event_release_kernel(events[cpu]); | |
130 | } | |
131 | mutex_unlock(&perf_counter_mutex); | |
132 | kfree(attr); | |
133 | error_attr: | |
134 | kfree(events); | |
135 | return ret; | |
136 | } | |
137 | ||
833ad6a0 MD |
138 | MODULE_LICENSE("GPL and additional rights"); |
139 | MODULE_AUTHOR("Mathieu Desnoyers"); | |
140 | MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support"); |