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.
11 #include <sys/types.h>
20 #include <sys/syscall.h>
21 #include <lttng/ust-arch.h>
22 #include <lttng/ust-events.h>
23 #include <lttng/ust-tracer.h>
24 #include <lttng/ringbuffer-context.h>
25 #include <urcu/system.h>
26 #include <urcu/arch.h>
27 #include <urcu/rculist.h>
28 #include <ust-helper.h>
30 #include <usterr-signal-safe.h>
32 #include <urcu/tls-compat.h>
33 #include "perf_event.h"
35 #include "context-internal.h"
36 #include "lttng-tracer-core.h"
37 #include "ust-events-internal.h"
40 * We use a global perf counter key and iterate on per-thread RCU lists
41 * of fields in the fast path, even though this is not strictly speaking
42 * what would provide the best fast-path complexity, to ensure teardown
43 * of sessions vs thread exit is handled racelessly.
45 * Updates and traversals of thread_list are protected by UST lock.
46 * Updates to rcu_field_list are protected by UST lock.
49 struct lttng_perf_counter_thread_field
{
50 struct lttng_perf_counter_field
*field
; /* Back reference */
51 struct perf_event_mmap_page
*pc
;
52 struct cds_list_head thread_field_node
; /* Per-field list of thread fields (node) */
53 struct cds_list_head rcu_field_node
; /* RCU per-thread list of fields (node) */
57 struct lttng_perf_counter_thread
{
58 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
61 struct lttng_perf_counter_field
{
62 struct perf_event_attr attr
;
63 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
66 static pthread_key_t perf_counter_key
;
69 * lttng_perf_lock - Protect lttng-ust perf counter data structures
71 * Nests within the ust_lock, and therefore within the libc dl lock.
72 * Therefore, we need to fixup the TLS before nesting into this lock.
73 * Nests inside RCU bp read-side lock. Protects against concurrent
76 static pthread_mutex_t ust_perf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
79 * Cancel state when grabbing the ust_perf_mutex. Saved when locking,
80 * restored on unlock. Protected by ust_perf_mutex.
82 static int ust_perf_saved_cancelstate
;
85 * Track whether we are tracing from a signal handler nested on an
88 static DEFINE_URCU_TLS(int, ust_perf_mutex_nest
);
91 * Force a read (imply TLS fixup for dlopen) of TLS variables.
93 void lttng_ust_fixup_perf_counter_tls(void)
95 asm volatile ("" : : "m" (URCU_TLS(ust_perf_mutex_nest
)));
98 void lttng_perf_lock(void)
100 sigset_t sig_all_blocked
, orig_mask
;
103 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
105 ERR("pthread_setcancelstate: %s", strerror(ret
));
107 sigfillset(&sig_all_blocked
);
108 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
110 ERR("pthread_sigmask: %s", strerror(ret
));
112 if (!URCU_TLS(ust_perf_mutex_nest
)++) {
114 * Ensure the compiler don't move the store after the close()
115 * call in case close() would be marked as leaf.
118 pthread_mutex_lock(&ust_perf_mutex
);
119 ust_perf_saved_cancelstate
= oldstate
;
121 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
123 ERR("pthread_sigmask: %s", strerror(ret
));
127 void lttng_perf_unlock(void)
129 sigset_t sig_all_blocked
, orig_mask
;
130 int ret
, newstate
, oldstate
;
131 bool restore_cancel
= false;
133 sigfillset(&sig_all_blocked
);
134 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
136 ERR("pthread_sigmask: %s", strerror(ret
));
139 * Ensure the compiler don't move the store before the close()
140 * call, in case close() would be marked as leaf.
143 if (!--URCU_TLS(ust_perf_mutex_nest
)) {
144 newstate
= ust_perf_saved_cancelstate
;
145 restore_cancel
= true;
146 pthread_mutex_unlock(&ust_perf_mutex
);
148 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
150 ERR("pthread_sigmask: %s", strerror(ret
));
152 if (restore_cancel
) {
153 ret
= pthread_setcancelstate(newstate
, &oldstate
);
155 ERR("pthread_setcancelstate: %s", strerror(ret
));
161 size_t perf_counter_get_size(struct lttng_ust_ctx_field
*field
__attribute__((unused
)),
166 size
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
167 size
+= sizeof(uint64_t);
172 uint64_t read_perf_counter_syscall(
173 struct lttng_perf_counter_thread_field
*thread_field
)
177 if (caa_unlikely(thread_field
->fd
< 0))
180 if (caa_unlikely(read(thread_field
->fd
, &count
, sizeof(count
))
187 #if defined(LTTNG_UST_ARCH_X86)
190 uint64_t rdpmc(unsigned int counter
)
192 unsigned int low
, high
;
194 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
196 return low
| ((uint64_t) high
) << 32;
200 bool has_rdpmc(struct perf_event_mmap_page
*pc
)
202 if (caa_unlikely(!pc
->cap_bit0_is_deprecated
))
204 /* Since Linux kernel 3.12. */
205 return pc
->cap_user_rdpmc
;
209 uint64_t arch_read_perf_counter(
210 struct lttng_perf_counter_thread_field
*thread_field
)
214 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
216 if (caa_unlikely(!pc
))
220 seq
= CMM_LOAD_SHARED(pc
->lock
);
224 if (caa_likely(has_rdpmc(pc
) && idx
)) {
227 pmcval
= rdpmc(idx
- 1);
228 /* Sign-extend the pmc register result. */
229 pmcval
<<= 64 - pc
->pmc_width
;
230 pmcval
>>= 64 - pc
->pmc_width
;
231 count
= pc
->offset
+ pmcval
;
233 /* Fall-back on system call if rdpmc cannot be used. */
234 return read_perf_counter_syscall(thread_field
);
237 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
243 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
245 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
249 return !has_rdpmc(pc
);
254 /* Generic (slow) implementation using a read system call. */
256 uint64_t arch_read_perf_counter(
257 struct lttng_perf_counter_thread_field
*thread_field
)
259 return read_perf_counter_syscall(thread_field
);
263 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
271 int sys_perf_event_open(struct perf_event_attr
*attr
,
272 pid_t pid
, int cpu
, int group_fd
,
275 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
280 int open_perf_fd(struct perf_event_attr
*attr
)
284 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
292 void close_perf_fd(int fd
)
301 perror("Error closing LTTng-UST perf memory mapping FD");
305 static void setup_perf(struct lttng_perf_counter_thread_field
*thread_field
)
309 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
310 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
311 if (perf_addr
== MAP_FAILED
)
313 thread_field
->pc
= perf_addr
;
315 if (!arch_perf_keep_fd(thread_field
)) {
316 close_perf_fd(thread_field
->fd
);
317 thread_field
->fd
= -1;
322 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
328 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
330 PERROR("Error in munmap");
336 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
338 struct lttng_perf_counter_thread
*perf_thread
;
339 sigset_t newmask
, oldmask
;
342 ret
= sigfillset(&newmask
);
345 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
348 /* Check again with signals disabled */
349 perf_thread
= pthread_getspecific(perf_counter_key
);
352 perf_thread
= zmalloc(sizeof(*perf_thread
));
355 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
356 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
360 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
367 struct lttng_perf_counter_thread_field
*
368 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
369 struct lttng_perf_counter_thread
*perf_thread
)
371 struct lttng_perf_counter_thread_field
*thread_field
;
372 sigset_t newmask
, oldmask
;
375 ret
= sigfillset(&newmask
);
378 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
381 /* Check again with signals disabled */
382 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
384 if (thread_field
->field
== perf_field
)
387 thread_field
= zmalloc(sizeof(*thread_field
));
390 thread_field
->field
= perf_field
;
391 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
392 if (thread_field
->fd
>= 0)
393 setup_perf(thread_field
);
395 * Note: thread_field->pc can be NULL if setup_perf() fails.
396 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
399 cds_list_add_rcu(&thread_field
->rcu_field_node
,
400 &perf_thread
->rcu_field_list
);
401 cds_list_add(&thread_field
->thread_field_node
,
402 &perf_field
->thread_field_list
);
405 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
412 struct lttng_perf_counter_thread_field
*
413 get_thread_field(struct lttng_perf_counter_field
*field
)
415 struct lttng_perf_counter_thread
*perf_thread
;
416 struct lttng_perf_counter_thread_field
*thread_field
;
418 perf_thread
= pthread_getspecific(perf_counter_key
);
420 perf_thread
= alloc_perf_counter_thread();
421 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
423 if (thread_field
->field
== field
)
426 /* perf_counter_thread_field not found, need to add one */
427 return add_thread_field(field
, perf_thread
);
431 uint64_t wrapper_perf_counter_read(struct lttng_ust_ctx_field
*field
)
433 struct lttng_perf_counter_field
*perf_field
;
434 struct lttng_perf_counter_thread_field
*perf_thread_field
;
436 perf_field
= (struct lttng_perf_counter_field
*) field
->priv
;
437 perf_thread_field
= get_thread_field(perf_field
);
438 return arch_read_perf_counter(perf_thread_field
);
442 void perf_counter_record(struct lttng_ust_ctx_field
*field
,
443 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
444 struct lttng_ust_channel_buffer
*chan
)
448 value
= wrapper_perf_counter_read(field
);
449 chan
->ops
->event_write(ctx
, &value
, sizeof(value
), lttng_ust_rb_alignof(value
));
453 void perf_counter_get_value(struct lttng_ust_ctx_field
*field
,
454 struct lttng_ust_ctx_value
*value
)
456 value
->u
.s64
= wrapper_perf_counter_read(field
);
459 /* Called with perf lock held */
461 void lttng_destroy_perf_thread_field(
462 struct lttng_perf_counter_thread_field
*thread_field
)
464 close_perf_fd(thread_field
->fd
);
465 unmap_perf_page(thread_field
->pc
);
466 cds_list_del_rcu(&thread_field
->rcu_field_node
);
467 cds_list_del(&thread_field
->thread_field_node
);
472 void lttng_destroy_perf_thread_key(void *_key
)
474 struct lttng_perf_counter_thread
*perf_thread
= _key
;
475 struct lttng_perf_counter_thread_field
*pos
, *p
;
478 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
480 lttng_destroy_perf_thread_field(pos
);
485 /* Called with UST lock held */
487 void lttng_destroy_perf_counter_field(struct lttng_ust_ctx_field
*field
)
489 struct lttng_perf_counter_field
*perf_field
;
490 struct lttng_perf_counter_thread_field
*pos
, *p
;
492 free((char *) field
->event_field
->name
);
493 perf_field
= (struct lttng_perf_counter_field
*) field
->priv
;
495 * This put is performed when no threads can concurrently
496 * perform a "get" concurrently, thanks to urcu-bp grace
497 * period. Holding the lttng perf lock protects against
498 * concurrent modification of the per-thread thread field
502 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
504 lttng_destroy_perf_thread_field(pos
);
509 #ifdef LTTNG_UST_ARCH_ARMV7
512 int perf_get_exclude_kernel(void)
517 #else /* LTTNG_UST_ARCH_ARMV7 */
520 int perf_get_exclude_kernel(void)
525 #endif /* LTTNG_UST_ARCH_ARMV7 */
527 /* Called with UST lock held */
528 int lttng_add_perf_counter_to_ctx(uint32_t type
,
531 struct lttng_ust_ctx
**ctx
)
533 struct lttng_ust_ctx_field
*field
;
534 struct lttng_ust_type_common
*ust_type
;
535 struct lttng_perf_counter_field
*perf_field
;
539 name_alloc
= strdup(name
);
542 goto name_alloc_error
;
544 perf_field
= zmalloc(sizeof(*perf_field
));
547 goto perf_field_alloc_error
;
549 ust_type
= lttng_ust_create_type_integer(sizeof(uint64_t) * CHAR_BIT
,
550 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
551 lttng_ust_is_signed_type(uint64_t),
555 goto type_alloc_error
;
557 field
= lttng_append_context(ctx
);
560 goto append_context_error
;
562 if (lttng_find_context(*ctx
, name_alloc
)) {
567 field
->destroy
= lttng_destroy_perf_counter_field
;
569 field
->event_field
->name
= name_alloc
;
570 field
->event_field
->type
= ust_type
;
571 field
->get_size
= perf_counter_get_size
;
572 field
->record
= perf_counter_record
;
573 field
->get_value
= perf_counter_get_value
;
575 perf_field
->attr
.type
= type
;
576 perf_field
->attr
.config
= config
;
577 perf_field
->attr
.exclude_kernel
= perf_get_exclude_kernel();
578 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
579 field
->priv
= perf_field
;
581 /* Ensure that this perf counter can be used in this process. */
582 ret
= open_perf_fd(&perf_field
->attr
);
590 * Contexts can only be added before tracing is started, so we
591 * don't have to synchronize against concurrent threads using
595 lttng_context_update(*ctx
);
600 lttng_remove_context_field(ctx
, field
);
601 append_context_error
:
602 lttng_ust_destroy_type(ust_type
);
605 perf_field_alloc_error
:
611 int lttng_perf_counter_init(void)
615 ret
= pthread_key_create(&perf_counter_key
,
616 lttng_destroy_perf_thread_key
);
622 void lttng_perf_counter_exit(void)
626 ret
= pthread_key_delete(perf_counter_key
);
629 PERROR("Error in pthread_key_delete");