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"
32 #include "lttng-tracer-core.h"
33 #include "ust-events-internal.h"
36 * We use a global perf counter key and iterate on per-thread RCU lists
37 * of fields in the fast path, even though this is not strictly speaking
38 * what would provide the best fast-path complexity, to ensure teardown
39 * of sessions vs thread exit is handled racelessly.
41 * Updates and traversals of thread_list are protected by UST lock.
42 * Updates to rcu_field_list are protected by UST lock.
45 struct lttng_perf_counter_thread_field
{
46 struct lttng_perf_counter_field
*field
; /* Back reference */
47 struct perf_event_mmap_page
*pc
;
48 struct cds_list_head thread_field_node
; /* Per-field list of thread fields (node) */
49 struct cds_list_head rcu_field_node
; /* RCU per-thread list of fields (node) */
53 struct lttng_perf_counter_thread
{
54 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
57 struct lttng_perf_counter_field
{
58 struct perf_event_attr attr
;
59 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
62 static pthread_key_t perf_counter_key
;
65 * lttng_perf_lock - Protect lttng-ust perf counter data structures
67 * Nests within the ust_lock, and therefore within the libc dl lock.
68 * Therefore, we need to fixup the TLS before nesting into this lock.
69 * Nests inside RCU bp read-side lock. Protects against concurrent
72 static pthread_mutex_t ust_perf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
75 * Cancel state when grabbing the ust_perf_mutex. Saved when locking,
76 * restored on unlock. Protected by ust_perf_mutex.
78 static int ust_perf_saved_cancelstate
;
81 * Track whether we are tracing from a signal handler nested on an
84 static DEFINE_URCU_TLS(int, ust_perf_mutex_nest
);
87 * Force a read (imply TLS fixup for dlopen) of TLS variables.
89 void lttng_ust_fixup_perf_counter_tls(void)
91 asm volatile ("" : : "m" (URCU_TLS(ust_perf_mutex_nest
)));
94 void lttng_perf_lock(void)
96 sigset_t sig_all_blocked
, orig_mask
;
99 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
101 ERR("pthread_setcancelstate: %s", strerror(ret
));
103 sigfillset(&sig_all_blocked
);
104 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
106 ERR("pthread_sigmask: %s", strerror(ret
));
108 if (!URCU_TLS(ust_perf_mutex_nest
)++) {
110 * Ensure the compiler don't move the store after the close()
111 * call in case close() would be marked as leaf.
114 pthread_mutex_lock(&ust_perf_mutex
);
115 ust_perf_saved_cancelstate
= oldstate
;
117 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
119 ERR("pthread_sigmask: %s", strerror(ret
));
123 void lttng_perf_unlock(void)
125 sigset_t sig_all_blocked
, orig_mask
;
126 int ret
, newstate
, oldstate
;
127 bool restore_cancel
= false;
129 sigfillset(&sig_all_blocked
);
130 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
132 ERR("pthread_sigmask: %s", strerror(ret
));
135 * Ensure the compiler don't move the store before the close()
136 * call, in case close() would be marked as leaf.
139 if (!--URCU_TLS(ust_perf_mutex_nest
)) {
140 newstate
= ust_perf_saved_cancelstate
;
141 restore_cancel
= true;
142 pthread_mutex_unlock(&ust_perf_mutex
);
144 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
146 ERR("pthread_sigmask: %s", strerror(ret
));
148 if (restore_cancel
) {
149 ret
= pthread_setcancelstate(newstate
, &oldstate
);
151 ERR("pthread_setcancelstate: %s", strerror(ret
));
157 size_t perf_counter_get_size(struct lttng_ctx_field
*field
, size_t offset
)
161 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
162 size
+= sizeof(uint64_t);
167 uint64_t read_perf_counter_syscall(
168 struct lttng_perf_counter_thread_field
*thread_field
)
172 if (caa_unlikely(thread_field
->fd
< 0))
175 if (caa_unlikely(read(thread_field
->fd
, &count
, sizeof(count
))
182 #if defined(__x86_64__) || defined(__i386__)
185 uint64_t rdpmc(unsigned int counter
)
187 unsigned int low
, high
;
189 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
191 return low
| ((uint64_t) high
) << 32;
195 bool has_rdpmc(struct perf_event_mmap_page
*pc
)
197 if (caa_unlikely(!pc
->cap_bit0_is_deprecated
))
199 /* Since Linux kernel 3.12. */
200 return pc
->cap_user_rdpmc
;
204 uint64_t arch_read_perf_counter(
205 struct lttng_perf_counter_thread_field
*thread_field
)
209 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
211 if (caa_unlikely(!pc
))
215 seq
= CMM_LOAD_SHARED(pc
->lock
);
219 if (caa_likely(has_rdpmc(pc
) && idx
)) {
222 pmcval
= rdpmc(idx
- 1);
223 /* Sign-extend the pmc register result. */
224 pmcval
<<= 64 - pc
->pmc_width
;
225 pmcval
>>= 64 - pc
->pmc_width
;
226 count
= pc
->offset
+ pmcval
;
228 /* Fall-back on system call if rdpmc cannot be used. */
229 return read_perf_counter_syscall(thread_field
);
232 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
238 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
240 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
244 return !has_rdpmc(pc
);
249 /* Generic (slow) implementation using a read system call. */
251 uint64_t arch_read_perf_counter(
252 struct lttng_perf_counter_thread_field
*thread_field
)
254 return read_perf_counter_syscall(thread_field
);
258 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
266 int sys_perf_event_open(struct perf_event_attr
*attr
,
267 pid_t pid
, int cpu
, int group_fd
,
270 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
275 int open_perf_fd(struct perf_event_attr
*attr
)
279 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
287 void close_perf_fd(int fd
)
296 perror("Error closing LTTng-UST perf memory mapping FD");
300 static void setup_perf(struct lttng_perf_counter_thread_field
*thread_field
)
304 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
305 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
306 if (perf_addr
== MAP_FAILED
)
308 thread_field
->pc
= perf_addr
;
310 if (!arch_perf_keep_fd(thread_field
)) {
311 close_perf_fd(thread_field
->fd
);
312 thread_field
->fd
= -1;
317 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
323 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
325 PERROR("Error in munmap");
331 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
333 struct lttng_perf_counter_thread
*perf_thread
;
334 sigset_t newmask
, oldmask
;
337 ret
= sigfillset(&newmask
);
340 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
343 /* Check again with signals disabled */
344 perf_thread
= pthread_getspecific(perf_counter_key
);
347 perf_thread
= zmalloc(sizeof(*perf_thread
));
350 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
351 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
355 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
362 struct lttng_perf_counter_thread_field
*
363 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
364 struct lttng_perf_counter_thread
*perf_thread
)
366 struct lttng_perf_counter_thread_field
*thread_field
;
367 sigset_t newmask
, oldmask
;
370 ret
= sigfillset(&newmask
);
373 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
376 /* Check again with signals disabled */
377 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
379 if (thread_field
->field
== perf_field
)
382 thread_field
= zmalloc(sizeof(*thread_field
));
385 thread_field
->field
= perf_field
;
386 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
387 if (thread_field
->fd
>= 0)
388 setup_perf(thread_field
);
390 * Note: thread_field->pc can be NULL if setup_perf() fails.
391 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
394 cds_list_add_rcu(&thread_field
->rcu_field_node
,
395 &perf_thread
->rcu_field_list
);
396 cds_list_add(&thread_field
->thread_field_node
,
397 &perf_field
->thread_field_list
);
400 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
407 struct lttng_perf_counter_thread_field
*
408 get_thread_field(struct lttng_perf_counter_field
*field
)
410 struct lttng_perf_counter_thread
*perf_thread
;
411 struct lttng_perf_counter_thread_field
*thread_field
;
413 perf_thread
= pthread_getspecific(perf_counter_key
);
415 perf_thread
= alloc_perf_counter_thread();
416 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
418 if (thread_field
->field
== field
)
421 /* perf_counter_thread_field not found, need to add one */
422 return add_thread_field(field
, perf_thread
);
426 uint64_t wrapper_perf_counter_read(struct lttng_ctx_field
*field
)
428 struct lttng_perf_counter_field
*perf_field
;
429 struct lttng_perf_counter_thread_field
*perf_thread_field
;
431 perf_field
= field
->u
.perf_counter
;
432 perf_thread_field
= get_thread_field(perf_field
);
433 return arch_read_perf_counter(perf_thread_field
);
437 void perf_counter_record(struct lttng_ctx_field
*field
,
438 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
439 struct lttng_channel
*chan
)
443 value
= wrapper_perf_counter_read(field
);
444 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
445 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
449 void perf_counter_get_value(struct lttng_ctx_field
*field
,
450 struct lttng_ctx_value
*value
)
452 value
->u
.s64
= wrapper_perf_counter_read(field
);
455 /* Called with perf lock held */
457 void lttng_destroy_perf_thread_field(
458 struct lttng_perf_counter_thread_field
*thread_field
)
460 close_perf_fd(thread_field
->fd
);
461 unmap_perf_page(thread_field
->pc
);
462 cds_list_del_rcu(&thread_field
->rcu_field_node
);
463 cds_list_del(&thread_field
->thread_field_node
);
468 void lttng_destroy_perf_thread_key(void *_key
)
470 struct lttng_perf_counter_thread
*perf_thread
= _key
;
471 struct lttng_perf_counter_thread_field
*pos
, *p
;
474 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
476 lttng_destroy_perf_thread_field(pos
);
481 /* Called with UST lock held */
483 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
485 struct lttng_perf_counter_field
*perf_field
;
486 struct lttng_perf_counter_thread_field
*pos
, *p
;
488 free((char *) field
->event_field
.name
);
489 perf_field
= field
->u
.perf_counter
;
491 * This put is performed when no threads can concurrently
492 * perform a "get" concurrently, thanks to urcu-bp grace
493 * period. Holding the lttng perf lock protects against
494 * concurrent modification of the per-thread thread field
498 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
500 lttng_destroy_perf_thread_field(pos
);
505 #ifdef __ARM_ARCH_7A__
508 int perf_get_exclude_kernel(void)
513 #else /* __ARM_ARCH_7A__ */
516 int perf_get_exclude_kernel(void)
521 #endif /* __ARM_ARCH_7A__ */
523 /* Called with UST lock held */
524 int lttng_add_perf_counter_to_ctx(uint32_t type
,
527 struct lttng_ctx
**ctx
)
529 struct lttng_ctx_field
*field
;
530 struct lttng_perf_counter_field
*perf_field
;
534 name_alloc
= strdup(name
);
537 goto name_alloc_error
;
539 perf_field
= zmalloc(sizeof(*perf_field
));
542 goto perf_field_alloc_error
;
544 field
= lttng_append_context(ctx
);
547 goto append_context_error
;
549 if (lttng_find_context(*ctx
, name_alloc
)) {
554 field
->destroy
= lttng_destroy_perf_counter_field
;
556 field
->event_field
.name
= name_alloc
;
557 field
->event_field
.type
.atype
= atype_integer
;
558 field
->event_field
.type
.u
.integer
.size
=
559 sizeof(uint64_t) * CHAR_BIT
;
560 field
->event_field
.type
.u
.integer
.alignment
=
561 lttng_alignof(uint64_t) * CHAR_BIT
;
562 field
->event_field
.type
.u
.integer
.signedness
=
563 lttng_is_signed_type(uint64_t);
564 field
->event_field
.type
.u
.integer
.reverse_byte_order
= 0;
565 field
->event_field
.type
.u
.integer
.base
= 10;
566 field
->event_field
.type
.u
.integer
.encoding
= lttng_encode_none
;
567 field
->get_size
= perf_counter_get_size
;
568 field
->record
= perf_counter_record
;
569 field
->get_value
= perf_counter_get_value
;
571 perf_field
->attr
.type
= type
;
572 perf_field
->attr
.config
= config
;
573 perf_field
->attr
.exclude_kernel
= perf_get_exclude_kernel();
574 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
575 field
->u
.perf_counter
= perf_field
;
577 /* Ensure that this perf counter can be used in this process. */
578 ret
= open_perf_fd(&perf_field
->attr
);
586 * Contexts can only be added before tracing is started, so we
587 * don't have to synchronize against concurrent threads using
591 lttng_context_update(*ctx
);
596 lttng_remove_context_field(ctx
, field
);
597 append_context_error
:
599 perf_field_alloc_error
:
605 int lttng_perf_counter_init(void)
609 ret
= pthread_key_create(&perf_counter_key
,
610 lttng_destroy_perf_thread_key
);
616 void lttng_perf_counter_exit(void)
620 ret
= pthread_key_delete(perf_counter_key
);
623 PERROR("Error in pthread_key_delete");