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