2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2009-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng UST performance monitoring counters (perf-counters) integration.
10 #include <sys/types.h>
19 #include <sys/syscall.h>
20 #include <lttng/ust-events.h>
21 #include <lttng/ust-tracer.h>
22 #include <lttng/ringbuffer-config.h>
23 #include <urcu/system.h>
24 #include <urcu/arch.h>
25 #include <urcu/rculist.h>
28 #include <usterr-signal-safe.h>
30 #include <urcu/tls-compat.h>
31 #include "perf_event.h"
32 #include "lttng-tracer-core.h"
35 * We use a global perf counter key and iterate on per-thread RCU lists
36 * of fields in the fast path, even though this is not strictly speaking
37 * what would provide the best fast-path complexity, to ensure teardown
38 * of sessions vs thread exit is handled racelessly.
40 * Updates and traversals of thread_list are protected by UST lock.
41 * Updates to rcu_field_list are protected by UST lock.
44 struct lttng_perf_counter_thread_field
{
45 struct lttng_perf_counter_field
*field
; /* Back reference */
46 struct perf_event_mmap_page
*pc
;
47 struct cds_list_head thread_field_node
; /* Per-field list of thread fields (node) */
48 struct cds_list_head rcu_field_node
; /* RCU per-thread list of fields (node) */
52 struct lttng_perf_counter_thread
{
53 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
56 struct lttng_perf_counter_field
{
57 struct perf_event_attr attr
;
58 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
61 static pthread_key_t perf_counter_key
;
64 * lttng_perf_lock - Protect lttng-ust perf counter data structures
66 * Nests within the ust_lock, and therefore within the libc dl lock.
67 * Therefore, we need to fixup the TLS before nesting into this lock.
68 * Nests inside RCU bp read-side lock. Protects against concurrent
71 static pthread_mutex_t ust_perf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
74 * Cancel state when grabbing the ust_perf_mutex. Saved when locking,
75 * restored on unlock. Protected by ust_perf_mutex.
77 static int ust_perf_saved_cancelstate
;
80 * Track whether we are tracing from a signal handler nested on an
83 static DEFINE_URCU_TLS(int, ust_perf_mutex_nest
);
86 * Force a read (imply TLS fixup for dlopen) of TLS variables.
88 void lttng_ust_fixup_perf_counter_tls(void)
90 asm volatile ("" : : "m" (URCU_TLS(ust_perf_mutex_nest
)));
93 void lttng_perf_lock(void)
95 sigset_t sig_all_blocked
, orig_mask
;
98 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
100 ERR("pthread_setcancelstate: %s", strerror(ret
));
102 sigfillset(&sig_all_blocked
);
103 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
105 ERR("pthread_sigmask: %s", strerror(ret
));
107 if (!URCU_TLS(ust_perf_mutex_nest
)++) {
109 * Ensure the compiler don't move the store after the close()
110 * call in case close() would be marked as leaf.
113 pthread_mutex_lock(&ust_perf_mutex
);
114 ust_perf_saved_cancelstate
= oldstate
;
116 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
118 ERR("pthread_sigmask: %s", strerror(ret
));
122 void lttng_perf_unlock(void)
124 sigset_t sig_all_blocked
, orig_mask
;
125 int ret
, newstate
, oldstate
;
126 bool restore_cancel
= false;
128 sigfillset(&sig_all_blocked
);
129 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
131 ERR("pthread_sigmask: %s", strerror(ret
));
134 * Ensure the compiler don't move the store before the close()
135 * call, in case close() would be marked as leaf.
138 if (!--URCU_TLS(ust_perf_mutex_nest
)) {
139 newstate
= ust_perf_saved_cancelstate
;
140 restore_cancel
= true;
141 pthread_mutex_unlock(&ust_perf_mutex
);
143 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
145 ERR("pthread_sigmask: %s", strerror(ret
));
147 if (restore_cancel
) {
148 ret
= pthread_setcancelstate(newstate
, &oldstate
);
150 ERR("pthread_setcancelstate: %s", strerror(ret
));
156 size_t perf_counter_get_size(struct lttng_ctx_field
*field
, size_t offset
)
160 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
161 size
+= sizeof(uint64_t);
166 uint64_t read_perf_counter_syscall(
167 struct lttng_perf_counter_thread_field
*thread_field
)
171 if (caa_unlikely(thread_field
->fd
< 0))
174 if (caa_unlikely(read(thread_field
->fd
, &count
, sizeof(count
))
181 #if defined(__x86_64__) || defined(__i386__)
184 uint64_t rdpmc(unsigned int counter
)
186 unsigned int low
, high
;
188 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
190 return low
| ((uint64_t) high
) << 32;
194 bool has_rdpmc(struct perf_event_mmap_page
*pc
)
196 if (caa_unlikely(!pc
->cap_bit0_is_deprecated
))
198 /* Since Linux kernel 3.12. */
199 return pc
->cap_user_rdpmc
;
203 uint64_t arch_read_perf_counter(
204 struct lttng_perf_counter_thread_field
*thread_field
)
208 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
210 if (caa_unlikely(!pc
))
214 seq
= CMM_LOAD_SHARED(pc
->lock
);
218 if (caa_likely(has_rdpmc(pc
) && idx
)) {
221 pmcval
= rdpmc(idx
- 1);
222 /* Sign-extend the pmc register result. */
223 pmcval
<<= 64 - pc
->pmc_width
;
224 pmcval
>>= 64 - pc
->pmc_width
;
225 count
= pc
->offset
+ pmcval
;
227 /* Fall-back on system call if rdpmc cannot be used. */
228 return read_perf_counter_syscall(thread_field
);
231 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
237 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
239 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
243 return !has_rdpmc(pc
);
248 /* Generic (slow) implementation using a read system call. */
250 uint64_t arch_read_perf_counter(
251 struct lttng_perf_counter_thread_field
*thread_field
)
253 return read_perf_counter_syscall(thread_field
);
257 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
265 int sys_perf_event_open(struct perf_event_attr
*attr
,
266 pid_t pid
, int cpu
, int group_fd
,
269 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
274 int open_perf_fd(struct perf_event_attr
*attr
)
278 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
286 void close_perf_fd(int fd
)
295 perror("Error closing LTTng-UST perf memory mapping FD");
299 static void setup_perf(struct lttng_perf_counter_thread_field
*thread_field
)
303 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
304 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
305 if (perf_addr
== MAP_FAILED
)
307 thread_field
->pc
= perf_addr
;
309 if (!arch_perf_keep_fd(thread_field
)) {
310 close_perf_fd(thread_field
->fd
);
311 thread_field
->fd
= -1;
316 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
322 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
324 PERROR("Error in munmap");
330 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
332 struct lttng_perf_counter_thread
*perf_thread
;
333 sigset_t newmask
, oldmask
;
336 ret
= sigfillset(&newmask
);
339 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
342 /* Check again with signals disabled */
343 perf_thread
= pthread_getspecific(perf_counter_key
);
346 perf_thread
= zmalloc(sizeof(*perf_thread
));
349 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
350 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
354 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
361 struct lttng_perf_counter_thread_field
*
362 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
363 struct lttng_perf_counter_thread
*perf_thread
)
365 struct lttng_perf_counter_thread_field
*thread_field
;
366 sigset_t newmask
, oldmask
;
369 ret
= sigfillset(&newmask
);
372 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
375 /* Check again with signals disabled */
376 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
378 if (thread_field
->field
== perf_field
)
381 thread_field
= zmalloc(sizeof(*thread_field
));
384 thread_field
->field
= perf_field
;
385 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
386 if (thread_field
->fd
>= 0)
387 setup_perf(thread_field
);
389 * Note: thread_field->pc can be NULL if setup_perf() fails.
390 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
393 cds_list_add_rcu(&thread_field
->rcu_field_node
,
394 &perf_thread
->rcu_field_list
);
395 cds_list_add(&thread_field
->thread_field_node
,
396 &perf_field
->thread_field_list
);
399 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
406 struct lttng_perf_counter_thread_field
*
407 get_thread_field(struct lttng_perf_counter_field
*field
)
409 struct lttng_perf_counter_thread
*perf_thread
;
410 struct lttng_perf_counter_thread_field
*thread_field
;
412 perf_thread
= pthread_getspecific(perf_counter_key
);
414 perf_thread
= alloc_perf_counter_thread();
415 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
417 if (thread_field
->field
== field
)
420 /* perf_counter_thread_field not found, need to add one */
421 return add_thread_field(field
, perf_thread
);
425 uint64_t wrapper_perf_counter_read(struct lttng_ctx_field
*field
)
427 struct lttng_perf_counter_field
*perf_field
;
428 struct lttng_perf_counter_thread_field
*perf_thread_field
;
430 perf_field
= field
->u
.perf_counter
;
431 perf_thread_field
= get_thread_field(perf_field
);
432 return arch_read_perf_counter(perf_thread_field
);
436 void perf_counter_record(struct lttng_ctx_field
*field
,
437 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
438 struct lttng_channel
*chan
)
442 value
= wrapper_perf_counter_read(field
);
443 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
444 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
448 void perf_counter_get_value(struct lttng_ctx_field
*field
,
449 struct lttng_ctx_value
*value
)
451 value
->u
.s64
= wrapper_perf_counter_read(field
);
454 /* Called with perf lock held */
456 void lttng_destroy_perf_thread_field(
457 struct lttng_perf_counter_thread_field
*thread_field
)
459 close_perf_fd(thread_field
->fd
);
460 unmap_perf_page(thread_field
->pc
);
461 cds_list_del_rcu(&thread_field
->rcu_field_node
);
462 cds_list_del(&thread_field
->thread_field_node
);
467 void lttng_destroy_perf_thread_key(void *_key
)
469 struct lttng_perf_counter_thread
*perf_thread
= _key
;
470 struct lttng_perf_counter_thread_field
*pos
, *p
;
473 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
475 lttng_destroy_perf_thread_field(pos
);
480 /* Called with UST lock held */
482 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
484 struct lttng_perf_counter_field
*perf_field
;
485 struct lttng_perf_counter_thread_field
*pos
, *p
;
487 free((char *) field
->event_field
.name
);
488 perf_field
= field
->u
.perf_counter
;
490 * This put is performed when no threads can concurrently
491 * perform a "get" concurrently, thanks to urcu-bp grace
492 * period. Holding the lttng perf lock protects against
493 * concurrent modification of the per-thread thread field
497 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
499 lttng_destroy_perf_thread_field(pos
);
504 #ifdef __ARM_ARCH_7A__
507 int perf_get_exclude_kernel(void)
512 #else /* __ARM_ARCH_7A__ */
515 int perf_get_exclude_kernel(void)
520 #endif /* __ARM_ARCH_7A__ */
522 /* Called with UST lock held */
523 int lttng_add_perf_counter_to_ctx(uint32_t type
,
526 struct lttng_ctx
**ctx
)
528 struct lttng_ctx_field
*field
;
529 struct lttng_perf_counter_field
*perf_field
;
533 name_alloc
= strdup(name
);
536 goto name_alloc_error
;
538 perf_field
= zmalloc(sizeof(*perf_field
));
541 goto perf_field_alloc_error
;
543 field
= lttng_append_context(ctx
);
546 goto append_context_error
;
548 if (lttng_find_context(*ctx
, name_alloc
)) {
553 field
->destroy
= lttng_destroy_perf_counter_field
;
555 field
->event_field
.name
= name_alloc
;
556 field
->event_field
.type
.atype
= atype_integer
;
557 field
->event_field
.type
.u
.integer
.size
=
558 sizeof(uint64_t) * CHAR_BIT
;
559 field
->event_field
.type
.u
.integer
.alignment
=
560 lttng_alignof(uint64_t) * CHAR_BIT
;
561 field
->event_field
.type
.u
.integer
.signedness
=
562 lttng_is_signed_type(uint64_t);
563 field
->event_field
.type
.u
.integer
.reverse_byte_order
= 0;
564 field
->event_field
.type
.u
.integer
.base
= 10;
565 field
->event_field
.type
.u
.integer
.encoding
= lttng_encode_none
;
566 field
->get_size
= perf_counter_get_size
;
567 field
->record
= perf_counter_record
;
568 field
->get_value
= perf_counter_get_value
;
570 perf_field
->attr
.type
= type
;
571 perf_field
->attr
.config
= config
;
572 perf_field
->attr
.exclude_kernel
= perf_get_exclude_kernel();
573 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
574 field
->u
.perf_counter
= perf_field
;
576 /* Ensure that this perf counter can be used in this process. */
577 ret
= open_perf_fd(&perf_field
->attr
);
585 * Contexts can only be added before tracing is started, so we
586 * don't have to synchronize against concurrent threads using
590 lttng_context_update(*ctx
);
595 lttng_remove_context_field(ctx
, field
);
596 append_context_error
:
598 perf_field_alloc_error
:
604 int lttng_perf_counter_init(void)
608 ret
= pthread_key_create(&perf_counter_key
,
609 lttng_destroy_perf_thread_key
);
615 void lttng_perf_counter_exit(void)
619 ret
= pthread_key_delete(perf_counter_key
);
622 PERROR("Error in pthread_key_delete");