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