1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3 * lttng-context-perf-counters.c
5 * LTTng performance monitoring counters (perf-counters) integration module.
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 <linux/cpu.h>
17 #include <lttng-events.h>
18 #include <include/ringbuffer/frontend_types.h>
19 #include <lttng-tracer.h>
22 size_t perf_counter_get_size(size_t offset
)
26 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
27 size
+= sizeof(uint64_t);
32 void perf_counter_record(struct lttng_ctx_field
*field
,
33 struct lib_ring_buffer_ctx
*ctx
,
34 struct lttng_channel
*chan
)
36 struct perf_event
*event
;
39 event
= field
->u
.perf_counter
->e
[ctx
->cpu
];
41 if (unlikely(event
->state
== PERF_EVENT_STATE_ERROR
)) {
44 event
->pmu
->read(event
);
45 value
= local64_read(&event
->count
);
49 * Perf chooses not to be clever and not to support enabling a
50 * perf counter before the cpu is brought up. Therefore, we need
51 * to support having events coming (e.g. scheduler events)
52 * before the counter is setup. Write an arbitrary 0 in this
57 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
58 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
62 void overflow_callback(struct perf_event
*event
,
63 struct perf_sample_data
*data
,
69 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
71 struct perf_event
**events
= field
->u
.perf_counter
->e
;
76 ret
= cpuhp_state_remove_instance(lttng_hp_online
,
77 &field
->u
.perf_counter
->cpuhp_online
.node
);
79 ret
= cpuhp_state_remove_instance(lttng_hp_prepare
,
80 &field
->u
.perf_counter
->cpuhp_prepare
.node
);
83 kfree(field
->event_field
.name
);
84 kfree(field
->u
.perf_counter
->attr
);
86 kfree(field
->u
.perf_counter
);
89 int lttng_cpuhp_perf_counter_online(unsigned int cpu
,
90 struct lttng_cpuhp_node
*node
)
92 struct lttng_perf_counter_field
*perf_field
=
93 container_of(node
, struct lttng_perf_counter_field
,
95 struct perf_event
**events
= perf_field
->e
;
96 struct perf_event_attr
*attr
= perf_field
->attr
;
97 struct perf_event
*pevent
;
99 pevent
= perf_event_create_kernel_counter(attr
,
100 cpu
, NULL
, overflow_callback
, NULL
);
101 if (!pevent
|| IS_ERR(pevent
))
103 if (pevent
->state
== PERF_EVENT_STATE_ERROR
) {
104 perf_event_release_kernel(pevent
);
107 barrier(); /* Create perf counter before setting event */
108 events
[cpu
] = pevent
;
112 int lttng_cpuhp_perf_counter_dead(unsigned int cpu
,
113 struct lttng_cpuhp_node
*node
)
115 struct lttng_perf_counter_field
*perf_field
=
116 container_of(node
, struct lttng_perf_counter_field
,
118 struct perf_event
**events
= perf_field
->e
;
119 struct perf_event
*pevent
;
121 pevent
= events
[cpu
];
123 barrier(); /* NULLify event before perf counter teardown */
124 perf_event_release_kernel(pevent
);
128 int lttng_add_perf_counter_to_ctx(uint32_t type
,
131 struct lttng_ctx
**ctx
)
133 struct lttng_ctx_field
*field
;
134 struct lttng_perf_counter_field
*perf_field
;
135 struct perf_event
**events
;
136 struct perf_event_attr
*attr
;
140 events
= kvzalloc(num_possible_cpus() * sizeof(*events
), GFP_KERNEL
);
144 attr
= kzalloc(sizeof(struct perf_event_attr
), GFP_KERNEL
);
151 attr
->config
= config
;
152 attr
->size
= sizeof(struct perf_event_attr
);
156 perf_field
= kzalloc(sizeof(struct lttng_perf_counter_field
), GFP_KERNEL
);
159 goto error_alloc_perf_field
;
161 perf_field
->e
= events
;
162 perf_field
->attr
= attr
;
164 name_alloc
= kstrdup(name
, GFP_KERNEL
);
167 goto name_alloc_error
;
170 field
= lttng_append_context(ctx
);
173 goto append_context_error
;
175 if (lttng_find_context(*ctx
, name_alloc
)) {
180 perf_field
->cpuhp_prepare
.component
= LTTNG_CONTEXT_PERF_COUNTERS
;
181 ret
= cpuhp_state_add_instance(lttng_hp_prepare
,
182 &perf_field
->cpuhp_prepare
.node
);
184 goto cpuhp_prepare_error
;
186 perf_field
->cpuhp_online
.component
= LTTNG_CONTEXT_PERF_COUNTERS
;
187 ret
= cpuhp_state_add_instance(lttng_hp_online
,
188 &perf_field
->cpuhp_online
.node
);
190 goto cpuhp_online_error
;
192 field
->destroy
= lttng_destroy_perf_counter_field
;
194 field
->event_field
.name
= name_alloc
;
195 field
->event_field
.type
.atype
= atype_integer
;
196 field
->event_field
.type
.u
.integer
.size
= sizeof(uint64_t) * CHAR_BIT
;
197 field
->event_field
.type
.u
.integer
.alignment
= lttng_alignof(uint64_t) * CHAR_BIT
;
198 field
->event_field
.type
.u
.integer
.signedness
= lttng_is_signed_type(uint64_t);
199 field
->event_field
.type
.u
.integer
.reverse_byte_order
= 0;
200 field
->event_field
.type
.u
.integer
.base
= 10;
201 field
->event_field
.type
.u
.integer
.encoding
= lttng_encode_none
;
202 field
->get_size
= perf_counter_get_size
;
203 field
->record
= perf_counter_record
;
204 field
->u
.perf_counter
= perf_field
;
205 lttng_context_update(*ctx
);
213 remove_ret
= cpuhp_state_remove_instance(lttng_hp_prepare
,
214 &perf_field
->cpuhp_prepare
.node
);
219 lttng_remove_context_field(ctx
, field
);
220 append_context_error
:
224 error_alloc_perf_field
: