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 "../ltt-events.h"
15 #include "../wrapper/ringbuffer/frontend_types.h"
16 #include "../wrapper/vmalloc.h"
17 #include "../ltt-tracer.h"
20 * TODO: Add CPU hotplug support.
23 static DEFINE_MUTEX(perf_counter_mutex
);
24 static LIST_HEAD(perf_counter_contexts
);
27 void perf_counter_record(struct lttng_ctx_field
*field
,
28 struct lib_ring_buffer_ctx
*ctx
,
29 struct ltt_channel
*chan
)
31 struct perf_event
*event
;
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
, ltt_alignof(value
));
38 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
42 void overflow_callback(struct perf_event
*event
, int nmi
,
43 struct perf_sample_data
*data
,
49 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
51 struct perf_event
**events
= field
->u
.perf_counter
.e
;
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
);
63 int lttng_add_perf_counter_to_ctx(uint32_t type
,
65 struct lttng_ctx
**ctx
)
67 struct lttng_ctx_field
*field
;
68 struct perf_event
**events
;
69 struct perf_event_attr
*attr
;
73 events
= kzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
77 attr
= kzalloc(sizeof(*field
->u
.perf_counter
.attr
), GFP_KERNEL
);
84 attr
->config
= config
;
85 attr
->size
= sizeof(struct perf_event_attr
);
89 mutex_lock(&perf_counter_mutex
);
91 for_each_online_cpu(cpu
) {
92 events
[cpu
] = perf_event_create_kernel_counter(attr
,
93 cpu
, NULL
, overflow_callback
);
100 field
= lttng_append_context(ctx
);
105 field
->destroy
= lttng_destroy_perf_counter_field
;
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
;
111 field
->type
.u
.basic
.integer
.signedness
= is_signed_type(unsigned long);
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
;
119 list_add(&field
->u
.perf_counter
.head
, &perf_counter_contexts
);
120 mutex_unlock(&perf_counter_mutex
);
122 wrapper_vmalloc_sync_all();
126 for_each_online_cpu(cpu
) {
128 perf_event_release_kernel(events
[cpu
]);
130 mutex_unlock(&perf_counter_mutex
);
137 MODULE_LICENSE("GPL and additional rights");
138 MODULE_AUTHOR("Mathieu Desnoyers");
139 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");