2 * lttng-context-perf-counters.c
4 * LTTng UST performance monitoring counters (perf-counters) integration.
6 * Copyright (C) 2009-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <sys/types.h>
29 #include <sys/syscall.h>
30 #include <linux/perf_event.h>
31 #include <lttng/ust-events.h>
32 #include <lttng/ust-tracer.h>
33 #include <lttng/ringbuffer-config.h>
34 #include <urcu/system.h>
35 #include <urcu/arch.h>
36 #include <urcu/rculist.h>
39 #include <usterr-signal-safe.h>
41 #include "lttng-tracer-core.h"
44 * We use a global perf counter key and iterate on per-thread RCU lists
45 * of fields in the fast path, even though this is not strictly speaking
46 * what would provide the best fast-path complexity, to ensure teardown
47 * of sessions vs thread exit is handled racelessly.
49 * Updates and traversals of thread_list are protected by UST lock.
50 * Updates to rcu_field_list are protected by UST lock.
53 struct lttng_perf_counter_thread_field
{
54 struct lttng_perf_counter_field
*field
; /* Back reference */
55 struct perf_event_mmap_page
*pc
;
56 struct cds_list_head thread_field_node
; /* Per-field list of thread fields (node) */
57 struct cds_list_head rcu_field_node
; /* RCU per-thread list of fields (node) */
60 struct lttng_perf_counter_thread
{
61 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
64 struct lttng_perf_counter_field
{
65 struct perf_event_attr attr
;
66 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
69 static pthread_key_t perf_counter_key
;
72 size_t perf_counter_get_size(size_t offset
)
76 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
77 size
+= sizeof(uint64_t);
81 #if defined(__x86_64__) || defined(__i386__)
84 uint64_t rdpmc(unsigned int counter
)
86 unsigned int low
, high
;
88 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
90 return low
| ((uint64_t) high
) << 32;
93 #else /* defined(__x86_64__) || defined(__i386__) */
95 #error "Perf event counters are only supported on x86 so far."
97 #endif /* #else defined(__x86_64__) || defined(__i386__) */
100 uint64_t read_perf_counter(struct perf_event_mmap_page
*pc
)
105 if (caa_unlikely(!pc
))
109 seq
= CMM_LOAD_SHARED(pc
->lock
);
114 count
= pc
->offset
+ rdpmc(idx
- 1);
119 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
125 int sys_perf_event_open(struct perf_event_attr
*attr
,
126 pid_t pid
, int cpu
, int group_fd
,
129 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
134 struct perf_event_mmap_page
*setup_perf(struct perf_event_attr
*attr
)
139 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
143 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
144 PROT_READ
, MAP_SHARED
, fd
, 0);
145 if (perf_addr
== MAP_FAILED
)
152 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
158 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
160 PERROR("Error in munmap");
166 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
168 struct lttng_perf_counter_thread
*perf_thread
;
169 sigset_t newmask
, oldmask
;
172 ret
= sigfillset(&newmask
);
175 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
178 /* Check again with signals disabled */
179 perf_thread
= pthread_getspecific(perf_counter_key
);
182 perf_thread
= zmalloc(sizeof(*perf_thread
));
185 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
186 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
190 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
197 struct lttng_perf_counter_thread_field
*
198 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
199 struct lttng_perf_counter_thread
*perf_thread
)
201 struct lttng_perf_counter_thread_field
*thread_field
;
202 sigset_t newmask
, oldmask
;
205 ret
= sigfillset(&newmask
);
208 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
211 /* Check again with signals disabled */
212 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
214 if (thread_field
->field
== perf_field
)
217 thread_field
= zmalloc(sizeof(*thread_field
));
220 thread_field
->field
= perf_field
;
221 thread_field
->pc
= setup_perf(&perf_field
->attr
);
222 /* Note: thread_field->pc can be NULL if setup_perf() fails. */
224 cds_list_add_rcu(&thread_field
->rcu_field_node
,
225 &perf_thread
->rcu_field_list
);
226 cds_list_add(&thread_field
->thread_field_node
,
227 &perf_field
->thread_field_list
);
230 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
237 struct lttng_perf_counter_thread_field
*
238 get_thread_field(struct lttng_perf_counter_field
*field
)
240 struct lttng_perf_counter_thread
*perf_thread
;
241 struct lttng_perf_counter_thread_field
*thread_field
;
243 perf_thread
= pthread_getspecific(perf_counter_key
);
245 perf_thread
= alloc_perf_counter_thread();
246 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
248 if (thread_field
->field
== field
)
251 /* perf_counter_thread_field not found, need to add one */
252 return add_thread_field(field
, perf_thread
);
256 uint64_t wrapper_perf_counter_read(struct lttng_ctx_field
*field
)
258 struct lttng_perf_counter_field
*perf_field
;
259 struct lttng_perf_counter_thread_field
*perf_thread_field
;
261 perf_field
= field
->u
.perf_counter
;
262 perf_thread_field
= get_thread_field(perf_field
);
263 return read_perf_counter(perf_thread_field
->pc
);
267 void perf_counter_record(struct lttng_ctx_field
*field
,
268 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
269 struct lttng_channel
*chan
)
273 value
= wrapper_perf_counter_read(field
);
274 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
275 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
279 void perf_counter_get_value(struct lttng_ctx_field
*field
,
280 union lttng_ctx_value
*value
)
284 v
= wrapper_perf_counter_read(field
);
288 /* Called with UST lock held */
290 void lttng_destroy_perf_thread_field(
291 struct lttng_perf_counter_thread_field
*thread_field
)
293 unmap_perf_page(thread_field
->pc
);
294 cds_list_del_rcu(&thread_field
->rcu_field_node
);
295 cds_list_del(&thread_field
->thread_field_node
);
300 void lttng_destroy_perf_thread_key(void *_key
)
302 struct lttng_perf_counter_thread
*perf_thread
= _key
;
303 struct lttng_perf_counter_thread_field
*pos
, *p
;
306 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
308 lttng_destroy_perf_thread_field(pos
);
313 /* Called with UST lock held */
315 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
317 struct lttng_perf_counter_field
*perf_field
;
318 struct lttng_perf_counter_thread_field
*pos
, *p
;
320 free((char *) field
->event_field
.name
);
321 perf_field
= field
->u
.perf_counter
;
323 * This put is performed when no threads can concurrently
324 * perform a "get" concurrently, thanks to urcu-bp grace
327 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
329 lttng_destroy_perf_thread_field(pos
);
333 /* Called with UST lock held */
334 int lttng_add_perf_counter_to_ctx(uint32_t type
,
337 struct lttng_ctx
**ctx
)
339 struct lttng_ctx_field
*field
;
340 struct lttng_perf_counter_field
*perf_field
;
341 struct perf_event_mmap_page
*tmp_pc
;
345 name_alloc
= strdup(name
);
348 goto name_alloc_error
;
350 perf_field
= zmalloc(sizeof(*perf_field
));
353 goto perf_field_alloc_error
;
355 field
= lttng_append_context(ctx
);
358 goto append_context_error
;
360 if (lttng_find_context(*ctx
, name_alloc
)) {
365 field
->destroy
= lttng_destroy_perf_counter_field
;
367 field
->event_field
.name
= name_alloc
;
368 field
->event_field
.type
.atype
= atype_integer
;
369 field
->event_field
.type
.u
.basic
.integer
.size
=
370 sizeof(uint64_t) * CHAR_BIT
;
371 field
->event_field
.type
.u
.basic
.integer
.alignment
=
372 lttng_alignof(uint64_t) * CHAR_BIT
;
373 field
->event_field
.type
.u
.basic
.integer
.signedness
=
374 lttng_is_signed_type(uint64_t);
375 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
376 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
377 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
378 field
->get_size
= perf_counter_get_size
;
379 field
->record
= perf_counter_record
;
380 field
->get_value
= perf_counter_get_value
;
382 perf_field
->attr
.type
= type
;
383 perf_field
->attr
.config
= config
;
384 perf_field
->attr
.exclude_kernel
= 1;
385 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
386 field
->u
.perf_counter
= perf_field
;
388 /* Ensure that this perf counter can be used in this process. */
389 tmp_pc
= setup_perf(&perf_field
->attr
);
394 unmap_perf_page(tmp_pc
);
397 * Contexts can only be added before tracing is started, so we
398 * don't have to synchronize against concurrent threads using
406 lttng_remove_context_field(ctx
, field
);
407 append_context_error
:
409 perf_field_alloc_error
:
415 int lttng_perf_counter_init(void)
419 ret
= pthread_key_create(&perf_counter_key
,
420 lttng_destroy_perf_thread_key
);
426 void lttng_perf_counter_exit(void)
430 ret
= pthread_key_delete(perf_counter_key
);
433 PERROR("Error in pthread_key_delete");