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>
26 #include <ust-helper.h>
28 #include <usterr-signal-safe.h>
30 #include <urcu/tls-compat.h>
31 #include "perf_event.h"
33 #include "context-internal.h"
34 #include "lttng-tracer-core.h"
35 #include "ust-events-internal.h"
38 * We use a global perf counter key and iterate on per-thread RCU lists
39 * of fields in the fast path, even though this is not strictly speaking
40 * what would provide the best fast-path complexity, to ensure teardown
41 * of sessions vs thread exit is handled racelessly.
43 * Updates and traversals of thread_list are protected by UST lock.
44 * Updates to rcu_field_list are protected by UST lock.
47 struct lttng_perf_counter_thread_field
{
48 struct lttng_perf_counter_field
*field
; /* Back reference */
49 struct perf_event_mmap_page
*pc
;
50 struct cds_list_head thread_field_node
; /* Per-field list of thread fields (node) */
51 struct cds_list_head rcu_field_node
; /* RCU per-thread list of fields (node) */
55 struct lttng_perf_counter_thread
{
56 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
59 struct lttng_perf_counter_field
{
60 struct perf_event_attr attr
;
61 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
64 static pthread_key_t perf_counter_key
;
67 * lttng_perf_lock - Protect lttng-ust perf counter data structures
69 * Nests within the ust_lock, and therefore within the libc dl lock.
70 * Therefore, we need to fixup the TLS before nesting into this lock.
71 * Nests inside RCU bp read-side lock. Protects against concurrent
74 static pthread_mutex_t ust_perf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
77 * Cancel state when grabbing the ust_perf_mutex. Saved when locking,
78 * restored on unlock. Protected by ust_perf_mutex.
80 static int ust_perf_saved_cancelstate
;
83 * Track whether we are tracing from a signal handler nested on an
86 static DEFINE_URCU_TLS(int, ust_perf_mutex_nest
);
89 * Force a read (imply TLS fixup for dlopen) of TLS variables.
91 void lttng_ust_fixup_perf_counter_tls(void)
93 asm volatile ("" : : "m" (URCU_TLS(ust_perf_mutex_nest
)));
96 void lttng_perf_lock(void)
98 sigset_t sig_all_blocked
, orig_mask
;
101 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
103 ERR("pthread_setcancelstate: %s", strerror(ret
));
105 sigfillset(&sig_all_blocked
);
106 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
108 ERR("pthread_sigmask: %s", strerror(ret
));
110 if (!URCU_TLS(ust_perf_mutex_nest
)++) {
112 * Ensure the compiler don't move the store after the close()
113 * call in case close() would be marked as leaf.
116 pthread_mutex_lock(&ust_perf_mutex
);
117 ust_perf_saved_cancelstate
= oldstate
;
119 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
121 ERR("pthread_sigmask: %s", strerror(ret
));
125 void lttng_perf_unlock(void)
127 sigset_t sig_all_blocked
, orig_mask
;
128 int ret
, newstate
, oldstate
;
129 bool restore_cancel
= false;
131 sigfillset(&sig_all_blocked
);
132 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
134 ERR("pthread_sigmask: %s", strerror(ret
));
137 * Ensure the compiler don't move the store before the close()
138 * call, in case close() would be marked as leaf.
141 if (!--URCU_TLS(ust_perf_mutex_nest
)) {
142 newstate
= ust_perf_saved_cancelstate
;
143 restore_cancel
= true;
144 pthread_mutex_unlock(&ust_perf_mutex
);
146 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
148 ERR("pthread_sigmask: %s", strerror(ret
));
150 if (restore_cancel
) {
151 ret
= pthread_setcancelstate(newstate
, &oldstate
);
153 ERR("pthread_setcancelstate: %s", strerror(ret
));
159 size_t perf_counter_get_size(struct lttng_ctx_field
*field
, size_t offset
)
163 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
164 size
+= sizeof(uint64_t);
169 uint64_t read_perf_counter_syscall(
170 struct lttng_perf_counter_thread_field
*thread_field
)
174 if (caa_unlikely(thread_field
->fd
< 0))
177 if (caa_unlikely(read(thread_field
->fd
, &count
, sizeof(count
))
184 #if defined(__x86_64__) || defined(__i386__)
187 uint64_t rdpmc(unsigned int counter
)
189 unsigned int low
, high
;
191 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
193 return low
| ((uint64_t) high
) << 32;
197 bool has_rdpmc(struct perf_event_mmap_page
*pc
)
199 if (caa_unlikely(!pc
->cap_bit0_is_deprecated
))
201 /* Since Linux kernel 3.12. */
202 return pc
->cap_user_rdpmc
;
206 uint64_t arch_read_perf_counter(
207 struct lttng_perf_counter_thread_field
*thread_field
)
211 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
213 if (caa_unlikely(!pc
))
217 seq
= CMM_LOAD_SHARED(pc
->lock
);
221 if (caa_likely(has_rdpmc(pc
) && idx
)) {
224 pmcval
= rdpmc(idx
- 1);
225 /* Sign-extend the pmc register result. */
226 pmcval
<<= 64 - pc
->pmc_width
;
227 pmcval
>>= 64 - pc
->pmc_width
;
228 count
= pc
->offset
+ pmcval
;
230 /* Fall-back on system call if rdpmc cannot be used. */
231 return read_perf_counter_syscall(thread_field
);
234 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
240 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
242 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
246 return !has_rdpmc(pc
);
251 /* Generic (slow) implementation using a read system call. */
253 uint64_t arch_read_perf_counter(
254 struct lttng_perf_counter_thread_field
*thread_field
)
256 return read_perf_counter_syscall(thread_field
);
260 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
268 int sys_perf_event_open(struct perf_event_attr
*attr
,
269 pid_t pid
, int cpu
, int group_fd
,
272 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
277 int open_perf_fd(struct perf_event_attr
*attr
)
281 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
289 void close_perf_fd(int fd
)
298 perror("Error closing LTTng-UST perf memory mapping FD");
302 static void setup_perf(struct lttng_perf_counter_thread_field
*thread_field
)
306 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
307 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
308 if (perf_addr
== MAP_FAILED
)
310 thread_field
->pc
= perf_addr
;
312 if (!arch_perf_keep_fd(thread_field
)) {
313 close_perf_fd(thread_field
->fd
);
314 thread_field
->fd
= -1;
319 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
325 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
327 PERROR("Error in munmap");
333 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
335 struct lttng_perf_counter_thread
*perf_thread
;
336 sigset_t newmask
, oldmask
;
339 ret
= sigfillset(&newmask
);
342 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
345 /* Check again with signals disabled */
346 perf_thread
= pthread_getspecific(perf_counter_key
);
349 perf_thread
= zmalloc(sizeof(*perf_thread
));
352 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
353 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
357 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
364 struct lttng_perf_counter_thread_field
*
365 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
366 struct lttng_perf_counter_thread
*perf_thread
)
368 struct lttng_perf_counter_thread_field
*thread_field
;
369 sigset_t newmask
, oldmask
;
372 ret
= sigfillset(&newmask
);
375 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
378 /* Check again with signals disabled */
379 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
381 if (thread_field
->field
== perf_field
)
384 thread_field
= zmalloc(sizeof(*thread_field
));
387 thread_field
->field
= perf_field
;
388 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
389 if (thread_field
->fd
>= 0)
390 setup_perf(thread_field
);
392 * Note: thread_field->pc can be NULL if setup_perf() fails.
393 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
396 cds_list_add_rcu(&thread_field
->rcu_field_node
,
397 &perf_thread
->rcu_field_list
);
398 cds_list_add(&thread_field
->thread_field_node
,
399 &perf_field
->thread_field_list
);
402 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
409 struct lttng_perf_counter_thread_field
*
410 get_thread_field(struct lttng_perf_counter_field
*field
)
412 struct lttng_perf_counter_thread
*perf_thread
;
413 struct lttng_perf_counter_thread_field
*thread_field
;
415 perf_thread
= pthread_getspecific(perf_counter_key
);
417 perf_thread
= alloc_perf_counter_thread();
418 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
420 if (thread_field
->field
== field
)
423 /* perf_counter_thread_field not found, need to add one */
424 return add_thread_field(field
, perf_thread
);
428 uint64_t wrapper_perf_counter_read(struct lttng_ctx_field
*field
)
430 struct lttng_perf_counter_field
*perf_field
;
431 struct lttng_perf_counter_thread_field
*perf_thread_field
;
433 perf_field
= field
->u
.perf_counter
;
434 perf_thread_field
= get_thread_field(perf_field
);
435 return arch_read_perf_counter(perf_thread_field
);
439 void perf_counter_record(struct lttng_ctx_field
*field
,
440 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
441 struct lttng_channel
*chan
)
445 value
= wrapper_perf_counter_read(field
);
446 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
447 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
451 void perf_counter_get_value(struct lttng_ctx_field
*field
,
452 struct lttng_ctx_value
*value
)
454 value
->u
.s64
= wrapper_perf_counter_read(field
);
457 /* Called with perf lock held */
459 void lttng_destroy_perf_thread_field(
460 struct lttng_perf_counter_thread_field
*thread_field
)
462 close_perf_fd(thread_field
->fd
);
463 unmap_perf_page(thread_field
->pc
);
464 cds_list_del_rcu(&thread_field
->rcu_field_node
);
465 cds_list_del(&thread_field
->thread_field_node
);
470 void lttng_destroy_perf_thread_key(void *_key
)
472 struct lttng_perf_counter_thread
*perf_thread
= _key
;
473 struct lttng_perf_counter_thread_field
*pos
, *p
;
476 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
478 lttng_destroy_perf_thread_field(pos
);
483 /* Called with UST lock held */
485 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
487 struct lttng_perf_counter_field
*perf_field
;
488 struct lttng_perf_counter_thread_field
*pos
, *p
;
490 free((char *) field
->event_field
.name
);
491 perf_field
= field
->u
.perf_counter
;
493 * This put is performed when no threads can concurrently
494 * perform a "get" concurrently, thanks to urcu-bp grace
495 * period. Holding the lttng perf lock protects against
496 * concurrent modification of the per-thread thread field
500 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
502 lttng_destroy_perf_thread_field(pos
);
507 #ifdef __ARM_ARCH_7A__
510 int perf_get_exclude_kernel(void)
515 #else /* __ARM_ARCH_7A__ */
518 int perf_get_exclude_kernel(void)
523 #endif /* __ARM_ARCH_7A__ */
525 /* Called with UST lock held */
526 int lttng_add_perf_counter_to_ctx(uint32_t type
,
529 struct lttng_ctx
**ctx
)
531 struct lttng_ctx_field
*field
;
532 struct lttng_perf_counter_field
*perf_field
;
536 name_alloc
= strdup(name
);
539 goto name_alloc_error
;
541 perf_field
= zmalloc(sizeof(*perf_field
));
544 goto perf_field_alloc_error
;
546 field
= lttng_append_context(ctx
);
549 goto append_context_error
;
551 if (lttng_find_context(*ctx
, name_alloc
)) {
556 field
->destroy
= lttng_destroy_perf_counter_field
;
558 field
->event_field
.name
= name_alloc
;
559 field
->event_field
.type
.atype
= atype_integer
;
560 field
->event_field
.type
.u
.integer
.size
=
561 sizeof(uint64_t) * CHAR_BIT
;
562 field
->event_field
.type
.u
.integer
.alignment
=
563 lttng_alignof(uint64_t) * CHAR_BIT
;
564 field
->event_field
.type
.u
.integer
.signedness
=
565 lttng_is_signed_type(uint64_t);
566 field
->event_field
.type
.u
.integer
.reverse_byte_order
= 0;
567 field
->event_field
.type
.u
.integer
.base
= 10;
568 field
->event_field
.type
.u
.integer
.encoding
= lttng_encode_none
;
569 field
->get_size
= perf_counter_get_size
;
570 field
->record
= perf_counter_record
;
571 field
->get_value
= perf_counter_get_value
;
573 perf_field
->attr
.type
= type
;
574 perf_field
->attr
.config
= config
;
575 perf_field
->attr
.exclude_kernel
= perf_get_exclude_kernel();
576 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
577 field
->u
.perf_counter
= perf_field
;
579 /* Ensure that this perf counter can be used in this process. */
580 ret
= open_perf_fd(&perf_field
->attr
);
588 * Contexts can only be added before tracing is started, so we
589 * don't have to synchronize against concurrent threads using
593 lttng_context_update(*ctx
);
598 lttng_remove_context_field(ctx
, field
);
599 append_context_error
:
601 perf_field_alloc_error
:
607 int lttng_perf_counter_init(void)
611 ret
= pthread_key_create(&perf_counter_key
,
612 lttng_destroy_perf_thread_key
);
618 void lttng_perf_counter_exit(void)
622 ret
= pthread_key_delete(perf_counter_key
);
625 PERROR("Error in pthread_key_delete");