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 */
67 static pthread_key_t perf_counter_key
;
70 * lttng_perf_lock - Protect lttng-ust perf counter data structures
72 * Nests within the ust_lock, and therefore within the libc dl lock.
73 * Therefore, we need to fixup the TLS before nesting into this lock.
74 * Nests inside RCU bp read-side lock. Protects against concurrent
77 static pthread_mutex_t ust_perf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
80 * Cancel state when grabbing the ust_perf_mutex. Saved when locking,
81 * restored on unlock. Protected by ust_perf_mutex.
83 static int ust_perf_saved_cancelstate
;
86 * Track whether we are tracing from a signal handler nested on an
89 static DEFINE_URCU_TLS(int, ust_perf_mutex_nest
);
92 * Force a read (imply TLS fixup for dlopen) of TLS variables.
94 void lttng_ust_fixup_perf_counter_tls(void)
96 asm volatile ("" : : "m" (URCU_TLS(ust_perf_mutex_nest
)));
99 void lttng_perf_lock(void)
101 sigset_t sig_all_blocked
, orig_mask
;
104 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
106 ERR("pthread_setcancelstate: %s", strerror(ret
));
108 sigfillset(&sig_all_blocked
);
109 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
111 ERR("pthread_sigmask: %s", strerror(ret
));
113 if (!URCU_TLS(ust_perf_mutex_nest
)++) {
115 * Ensure the compiler don't move the store after the close()
116 * call in case close() would be marked as leaf.
119 pthread_mutex_lock(&ust_perf_mutex
);
120 ust_perf_saved_cancelstate
= oldstate
;
122 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
124 ERR("pthread_sigmask: %s", strerror(ret
));
128 void lttng_perf_unlock(void)
130 sigset_t sig_all_blocked
, orig_mask
;
131 int ret
, newstate
, oldstate
;
132 bool restore_cancel
= false;
134 sigfillset(&sig_all_blocked
);
135 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
137 ERR("pthread_sigmask: %s", strerror(ret
));
140 * Ensure the compiler don't move the store before the close()
141 * call, in case close() would be marked as leaf.
144 if (!--URCU_TLS(ust_perf_mutex_nest
)) {
145 newstate
= ust_perf_saved_cancelstate
;
146 restore_cancel
= true;
147 pthread_mutex_unlock(&ust_perf_mutex
);
149 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
151 ERR("pthread_sigmask: %s", strerror(ret
));
153 if (restore_cancel
) {
154 ret
= pthread_setcancelstate(newstate
, &oldstate
);
156 ERR("pthread_setcancelstate: %s", strerror(ret
));
162 size_t perf_counter_get_size(void *priv
__attribute__((unused
)),
167 size
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
168 size
+= sizeof(uint64_t);
173 uint64_t read_perf_counter_syscall(
174 struct lttng_perf_counter_thread_field
*thread_field
)
178 if (caa_unlikely(thread_field
->fd
< 0))
181 if (caa_unlikely(read(thread_field
->fd
, &count
, sizeof(count
))
188 #if defined(LTTNG_UST_ARCH_X86)
191 uint64_t rdpmc(unsigned int counter
)
193 unsigned int low
, high
;
195 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
197 return low
| ((uint64_t) high
) << 32;
201 bool has_rdpmc(struct perf_event_mmap_page
*pc
)
203 if (caa_unlikely(!pc
->cap_bit0_is_deprecated
))
205 /* Since Linux kernel 3.12. */
206 return pc
->cap_user_rdpmc
;
210 uint64_t arch_read_perf_counter(
211 struct lttng_perf_counter_thread_field
*thread_field
)
215 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
217 if (caa_unlikely(!pc
))
221 seq
= CMM_LOAD_SHARED(pc
->lock
);
225 if (caa_likely(has_rdpmc(pc
) && idx
)) {
228 pmcval
= rdpmc(idx
- 1);
229 /* Sign-extend the pmc register result. */
230 pmcval
<<= 64 - pc
->pmc_width
;
231 pmcval
>>= 64 - pc
->pmc_width
;
232 count
= pc
->offset
+ pmcval
;
234 /* Fall-back on system call if rdpmc cannot be used. */
235 return read_perf_counter_syscall(thread_field
);
238 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
244 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
246 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
250 return !has_rdpmc(pc
);
255 /* Generic (slow) implementation using a read system call. */
257 uint64_t arch_read_perf_counter(
258 struct lttng_perf_counter_thread_field
*thread_field
)
260 return read_perf_counter_syscall(thread_field
);
264 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
272 int sys_perf_event_open(struct perf_event_attr
*attr
,
273 pid_t pid
, int cpu
, int group_fd
,
276 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
281 int open_perf_fd(struct perf_event_attr
*attr
)
285 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
293 void close_perf_fd(int fd
)
302 perror("Error closing LTTng-UST perf memory mapping FD");
306 static void setup_perf(struct lttng_perf_counter_thread_field
*thread_field
)
310 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
311 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
312 if (perf_addr
== MAP_FAILED
)
314 thread_field
->pc
= perf_addr
;
316 if (!arch_perf_keep_fd(thread_field
)) {
317 close_perf_fd(thread_field
->fd
);
318 thread_field
->fd
= -1;
323 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
329 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
331 PERROR("Error in munmap");
337 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
339 struct lttng_perf_counter_thread
*perf_thread
;
340 sigset_t newmask
, oldmask
;
343 ret
= sigfillset(&newmask
);
346 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
349 /* Check again with signals disabled */
350 perf_thread
= pthread_getspecific(perf_counter_key
);
353 perf_thread
= zmalloc(sizeof(*perf_thread
));
356 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
357 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
361 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
368 struct lttng_perf_counter_thread_field
*
369 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
370 struct lttng_perf_counter_thread
*perf_thread
)
372 struct lttng_perf_counter_thread_field
*thread_field
;
373 sigset_t newmask
, oldmask
;
376 ret
= sigfillset(&newmask
);
379 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
382 /* Check again with signals disabled */
383 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
385 if (thread_field
->field
== perf_field
)
388 thread_field
= zmalloc(sizeof(*thread_field
));
391 thread_field
->field
= perf_field
;
392 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
393 if (thread_field
->fd
>= 0)
394 setup_perf(thread_field
);
396 * Note: thread_field->pc can be NULL if setup_perf() fails.
397 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
400 cds_list_add_rcu(&thread_field
->rcu_field_node
,
401 &perf_thread
->rcu_field_list
);
402 cds_list_add(&thread_field
->thread_field_node
,
403 &perf_field
->thread_field_list
);
406 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
413 struct lttng_perf_counter_thread_field
*
414 get_thread_field(struct lttng_perf_counter_field
*field
)
416 struct lttng_perf_counter_thread
*perf_thread
;
417 struct lttng_perf_counter_thread_field
*thread_field
;
419 perf_thread
= pthread_getspecific(perf_counter_key
);
421 perf_thread
= alloc_perf_counter_thread();
422 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
424 if (thread_field
->field
== field
)
427 /* perf_counter_thread_field not found, need to add one */
428 return add_thread_field(field
, perf_thread
);
432 uint64_t wrapper_perf_counter_read(void *priv
)
434 struct lttng_perf_counter_field
*perf_field
;
435 struct lttng_perf_counter_thread_field
*perf_thread_field
;
437 perf_field
= (struct lttng_perf_counter_field
*) priv
;
438 perf_thread_field
= get_thread_field(perf_field
);
439 return arch_read_perf_counter(perf_thread_field
);
443 void perf_counter_record(void *priv
,
444 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
445 struct lttng_ust_channel_buffer
*chan
)
449 value
= wrapper_perf_counter_read(priv
);
450 chan
->ops
->event_write(ctx
, &value
, sizeof(value
), lttng_ust_rb_alignof(value
));
454 void perf_counter_get_value(void *priv
,
455 struct lttng_ust_ctx_value
*value
)
457 value
->u
.s64
= wrapper_perf_counter_read(priv
);
460 /* Called with perf lock held */
462 void lttng_destroy_perf_thread_field(
463 struct lttng_perf_counter_thread_field
*thread_field
)
465 close_perf_fd(thread_field
->fd
);
466 unmap_perf_page(thread_field
->pc
);
467 cds_list_del_rcu(&thread_field
->rcu_field_node
);
468 cds_list_del(&thread_field
->thread_field_node
);
473 void lttng_destroy_perf_thread_key(void *_key
)
475 struct lttng_perf_counter_thread
*perf_thread
= _key
;
476 struct lttng_perf_counter_thread_field
*pos
, *p
;
479 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
481 lttng_destroy_perf_thread_field(pos
);
486 /* Called with UST lock held */
488 void lttng_destroy_perf_counter_ctx_field(void *priv
)
490 struct lttng_perf_counter_field
*perf_field
;
491 struct lttng_perf_counter_thread_field
*pos
, *p
;
493 perf_field
= (struct lttng_perf_counter_field
*) priv
;
494 free(perf_field
->name
);
496 * This put is performed when no threads can concurrently
497 * perform a "get" concurrently, thanks to urcu-bp grace
498 * period. Holding the lttng perf lock protects against
499 * concurrent modification of the per-thread thread field
503 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
505 lttng_destroy_perf_thread_field(pos
);
510 #ifdef LTTNG_UST_ARCH_ARMV7
513 int perf_get_exclude_kernel(void)
518 #else /* LTTNG_UST_ARCH_ARMV7 */
521 int perf_get_exclude_kernel(void)
526 #endif /* LTTNG_UST_ARCH_ARMV7 */
528 static const struct lttng_ust_type_common
*ust_type
=
529 lttng_ust_static_type_integer(sizeof(uint64_t) * CHAR_BIT
,
530 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
531 lttng_ust_is_signed_type(uint64_t),
534 /* Called with UST lock held */
535 int lttng_add_perf_counter_to_ctx(uint32_t type
,
538 struct lttng_ust_ctx
**ctx
)
540 struct lttng_ust_ctx_field ctx_field
;
541 struct lttng_ust_event_field
*event_field
;
542 struct lttng_perf_counter_field
*perf_field
;
546 if (lttng_find_context(*ctx
, name
)) {
550 name_alloc
= strdup(name
);
553 goto name_alloc_error
;
555 event_field
= zmalloc(sizeof(*event_field
));
558 goto event_field_alloc_error
;
560 event_field
->name
= name_alloc
;
561 event_field
->type
= ust_type
;
563 perf_field
= zmalloc(sizeof(*perf_field
));
566 goto perf_field_alloc_error
;
568 perf_field
->attr
.type
= type
;
569 perf_field
->attr
.config
= config
;
570 perf_field
->attr
.exclude_kernel
= perf_get_exclude_kernel();
571 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
572 perf_field
->name
= name_alloc
;
574 /* Ensure that this perf counter can be used in this process. */
575 ret
= open_perf_fd(&perf_field
->attr
);
582 ctx_field
.event_field
= event_field
;
583 ctx_field
.get_size
= perf_counter_get_size
;
584 ctx_field
.record
= perf_counter_record
;
585 ctx_field
.get_value
= perf_counter_get_value
;
586 ctx_field
.destroy
= lttng_destroy_perf_counter_ctx_field
;
587 ctx_field
.priv
= perf_field
;
589 ret
= lttng_ust_context_append(ctx
, &ctx_field
);
592 goto append_context_error
;
596 append_context_error
:
599 perf_field_alloc_error
:
601 event_field_alloc_error
:
608 int lttng_perf_counter_init(void)
612 ret
= pthread_key_create(&perf_counter_key
,
613 lttng_destroy_perf_thread_key
);
619 void lttng_perf_counter_exit(void)
623 ret
= pthread_key_delete(perf_counter_key
);
626 PERROR("Error in pthread_key_delete");