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
,
38 ltt_alignof(field
->type
.u
.basic
.integer
.alignment
/ CHAR_BIT
));
39 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
43 void overflow_callback(struct perf_event
*event
, int nmi
,
44 struct perf_sample_data
*data
,
50 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
52 struct perf_event
**events
= field
->u
.perf_counter
.e
;
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
);
64 int lttng_add_perf_counter_to_ctx(uint32_t type
,
66 struct lttng_ctx
**ctx
)
68 struct lttng_ctx_field
*field
;
69 struct perf_event
**events
;
70 struct perf_event_attr
*attr
;
74 events
= kzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
78 attr
= kzalloc(sizeof(*field
->u
.perf_counter
.attr
), GFP_KERNEL
);
85 attr
->config
= config
;
86 attr
->size
= sizeof(struct perf_event_attr
);
90 mutex_lock(&perf_counter_mutex
);
92 for_each_online_cpu(cpu
) {
93 events
[cpu
] = perf_event_create_kernel_counter(attr
,
94 cpu
, NULL
, overflow_callback
);
101 field
= lttng_append_context(ctx
);
106 field
->destroy
= lttng_destroy_perf_counter_field
;
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
;
120 list_add(&field
->u
.perf_counter
.head
, &perf_counter_contexts
);
121 mutex_unlock(&perf_counter_mutex
);
123 wrapper_vmalloc_sync_all();
127 for_each_online_cpu(cpu
) {
129 perf_event_release_kernel(events
[cpu
]);
131 mutex_unlock(&perf_counter_mutex
);
138 MODULE_LICENSE("GPL and additional rights");
139 MODULE_AUTHOR("Mathieu Desnoyers");
140 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");