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
,
49 int lttng_add_perf_counter_to_ctx(uint32_t type
,
51 struct lttng_ctx
*ctx
)
53 struct lttng_ctx_field
*field
;
54 struct perf_event
**events
;
55 struct perf_event_attr
*attr
;
59 events
= kzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
63 attr
= kzalloc(sizeof(*field
->u
.perf_counter
.attr
), GFP_KERNEL
);
70 attr
->config
= config
;
71 attr
->size
= sizeof(struct perf_event_attr
);
75 mutex_lock(&perf_counter_mutex
);
77 for_each_online_cpu(cpu
) {
78 events
[cpu
] = perf_event_create_kernel_counter(attr
,
79 cpu
, NULL
, overflow_callback
);
87 if (ctx
->nr_fields
> ctx
->allocated_fields
) {
88 struct lttng_ctx_field
*new_fields
;
90 ctx
->allocated_fields
= min_t(size_t, 1, 2 * ctx
->allocated_fields
);
91 new_fields
= kzalloc(ctx
->allocated_fields
* sizeof(struct lttng_ctx_field
), GFP_KERNEL
);
97 memcpy(new_fields
, ctx
->fields
, ctx
->nr_fields
- 1);
99 ctx
->fields
= new_fields
;
101 field
= &ctx
->fields
[ctx
->nr_fields
- 1];
103 field
->name
= "dummyname";//TODO: lookup_counter_name(type, config);
104 field
->type
.atype
= atype_integer
;
105 field
->type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
106 field
->type
.u
.basic
.integer
.alignment
= ltt_alignof(unsigned long) * CHAR_BIT
;
107 field
->type
.u
.basic
.integer
.signedness
= 0;
108 field
->type
.u
.basic
.integer
.reverse_byte_order
= 0;
109 field
->type
.u
.basic
.integer
.base
= 10;
110 field
->type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
111 field
->callback
= perf_counter_record
;
112 field
->u
.perf_counter
.e
= events
;
113 field
->u
.perf_counter
.attr
= attr
;
115 list_add(&field
->u
.perf_counter
.head
, &perf_counter_contexts
);
116 mutex_unlock(&perf_counter_mutex
);
118 wrapper_vmalloc_sync_all();
122 for_each_online_cpu(cpu
) {
124 perf_event_release_kernel(events
[cpu
]);
126 mutex_unlock(&perf_counter_mutex
);
133 struct lttng_ctx
*lttng_create_perf_counter_ctx(void)
135 return kzalloc(sizeof(struct lttng_ctx
), GFP_KERNEL
);
138 void lttng_destroy_perf_counter_ctx(struct lttng_ctx
*ctx
)
142 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
143 struct perf_event
**events
= ctx
->fields
[i
].u
.perf_counter
.e
;
146 mutex_lock(&perf_counter_mutex
);
147 list_del(&ctx
->fields
[i
].u
.perf_counter
.head
);
148 for_each_online_cpu(cpu
)
149 perf_event_release_kernel(events
[cpu
]);
150 mutex_unlock(&perf_counter_mutex
);
151 kfree(ctx
->fields
[i
].u
.perf_counter
.attr
);
158 MODULE_LICENSE("GPL and additional rights");
159 MODULE_AUTHOR("Mathieu Desnoyers");
160 MODULE_DESCRIPTION("Linux Trace Toolkit Perf Support");
This page took 0.041523 seconds and 4 git commands to generate.