2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * LTTng performance monitoring counters (perf-counters) integration module.
7 * Dual LGPL v2.1/GPL v2 license.
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/perf_event.h>
13 #include <linux/list.h>
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"
21 * TODO: Add CPU hotplug support.
24 static DEFINE_MUTEX(perf_counter_mutex
);
25 static LIST_HEAD(perf_counter_contexts
);
28 size_t perf_counter_get_size(size_t offset
)
32 size
+= lib_ring_buffer_align(offset
, ltt_alignof(uint64_t));
33 size
+= sizeof(uint64_t);
38 void perf_counter_record(struct lttng_ctx_field
*field
,
39 struct lib_ring_buffer_ctx
*ctx
,
40 struct ltt_channel
*chan
)
42 struct perf_event
*event
;
45 event
= field
->u
.perf_counter
.e
[ctx
->cpu
];
46 event
->pmu
->read(event
);
47 value
= local64_read(&event
->count
);
48 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(value
));
49 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
53 void overflow_callback(struct perf_event
*event
, int nmi
,
54 struct perf_sample_data
*data
,
60 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
62 struct perf_event
**events
= field
->u
.perf_counter
.e
;
65 mutex_lock(&perf_counter_mutex
);
66 list_del(&field
->u
.perf_counter
.head
);
67 for_each_online_cpu(cpu
)
68 perf_event_release_kernel(events
[cpu
]);
69 mutex_unlock(&perf_counter_mutex
);
70 kfree(field
->event_field
.name
);
71 kfree(field
->u
.perf_counter
.attr
);
75 int lttng_add_perf_counter_to_ctx(uint32_t type
,
78 struct lttng_ctx
**ctx
)
80 struct lttng_ctx_field
*field
;
81 struct perf_event
**events
;
82 struct perf_event_attr
*attr
;
87 events
= kzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
91 attr
= kzalloc(sizeof(*field
->u
.perf_counter
.attr
), GFP_KERNEL
);
98 attr
->config
= config
;
99 attr
->size
= sizeof(struct perf_event_attr
);
103 mutex_lock(&perf_counter_mutex
);
105 for_each_online_cpu(cpu
) {
106 events
[cpu
] = perf_event_create_kernel_counter(attr
,
107 cpu
, NULL
, overflow_callback
);
110 goto name_alloc_error
;
114 name_alloc
= kstrdup(name
, GFP_KERNEL
);
117 goto name_alloc_error
;
120 field
= lttng_append_context(ctx
);
125 field
->destroy
= lttng_destroy_perf_counter_field
;
127 field
->event_field
.name
= name_alloc
;
128 field
->event_field
.type
.atype
= atype_integer
;
129 field
->event_field
.type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
130 field
->event_field
.type
.u
.basic
.integer
.alignment
= ltt_alignof(unsigned long) * CHAR_BIT
;
131 field
->event_field
.type
.u
.basic
.integer
.signedness
= is_signed_type(unsigned long);
132 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
133 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
134 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
135 field
->get_size
= perf_counter_get_size
;
136 field
->record
= perf_counter_record
;
137 field
->u
.perf_counter
.e
= events
;
138 field
->u
.perf_counter
.attr
= attr
;
140 list_add(&field
->u
.perf_counter
.head
, &perf_counter_contexts
);
141 mutex_unlock(&perf_counter_mutex
);
143 wrapper_vmalloc_sync_all();
149 for_each_online_cpu(cpu
) {
151 perf_event_release_kernel(events
[cpu
]);
153 mutex_unlock(&perf_counter_mutex
);
160 MODULE_LICENSE("GPL and additional rights");
161 MODULE_AUTHOR("Mathieu Desnoyers");
162 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");