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 size_t perf_counter_get_size(size_t offset
)
31 size
+= lib_ring_buffer_align(offset
, ltt_alignof(uint64_t));
32 size
+= sizeof(uint64_t);
37 void perf_counter_record(struct lttng_ctx_field
*field
,
38 struct lib_ring_buffer_ctx
*ctx
,
39 struct ltt_channel
*chan
)
41 struct perf_event
*event
;
44 event
= field
->u
.perf_counter
.e
[ctx
->cpu
];
45 event
->pmu
->read(event
);
46 value
= local64_read(&event
->count
);
47 lib_ring_buffer_align_ctx(ctx
, ltt_alignof(value
));
48 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
52 void overflow_callback(struct perf_event
*event
, int nmi
,
53 struct perf_sample_data
*data
,
59 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
61 struct perf_event
**events
= field
->u
.perf_counter
.e
;
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
);
73 int lttng_add_perf_counter_to_ctx(uint32_t type
,
75 struct lttng_ctx
**ctx
)
77 struct lttng_ctx_field
*field
;
78 struct perf_event
**events
;
79 struct perf_event_attr
*attr
;
83 events
= kzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
87 attr
= kzalloc(sizeof(*field
->u
.perf_counter
.attr
), GFP_KERNEL
);
94 attr
->config
= config
;
95 attr
->size
= sizeof(struct perf_event_attr
);
99 mutex_lock(&perf_counter_mutex
);
101 for_each_online_cpu(cpu
) {
102 events
[cpu
] = perf_event_create_kernel_counter(attr
,
103 cpu
, NULL
, overflow_callback
);
110 field
= lttng_append_context(ctx
);
115 field
->destroy
= lttng_destroy_perf_counter_field
;
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
;
125 field
->get_size
= perf_counter_get_size
;
126 field
->record
= perf_counter_record
;
127 field
->u
.perf_counter
.e
= events
;
128 field
->u
.perf_counter
.attr
= attr
;
130 list_add(&field
->u
.perf_counter
.head
, &perf_counter_contexts
);
131 mutex_unlock(&perf_counter_mutex
);
133 wrapper_vmalloc_sync_all();
137 for_each_online_cpu(cpu
) {
139 perf_event_release_kernel(events
[cpu
]);
141 mutex_unlock(&perf_counter_mutex
);
148 MODULE_LICENSE("GPL and additional rights");
149 MODULE_AUTHOR("Mathieu Desnoyers");
150 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");