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/ust-ringbuffer-context.h>
25 #include <lttng/ust-cancelstate.h>
26 #include <urcu/system.h>
27 #include <urcu/arch.h>
28 #include <urcu/rculist.h>
29 #include "common/macros.h"
31 #include "common/logging.h"
33 #include <urcu/tls-compat.h>
34 #include "perf_event.h"
36 #include "context-internal.h"
37 #include "lttng-tracer-core.h"
38 #include "lib/lttng-ust/events.h"
41 * We use a global perf counter key and iterate on per-thread RCU lists
42 * of fields in the fast path, even though this is not strictly speaking
43 * what would provide the best fast-path complexity, to ensure teardown
44 * of sessions vs thread exit is handled racelessly.
46 * Updates and traversals of thread_list are protected by UST lock.
47 * Updates to rcu_field_list are protected by UST lock.
50 struct lttng_perf_counter_thread_field
{
51 struct lttng_perf_counter_field
*field
; /* Back reference */
52 struct perf_event_mmap_page
*pc
;
53 struct cds_list_head thread_field_node
; /* Per-field list of thread fields (node) */
54 struct cds_list_head rcu_field_node
; /* RCU per-thread list of fields (node) */
58 struct lttng_perf_counter_thread
{
59 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
62 struct lttng_perf_counter_field
{
63 struct perf_event_attr attr
;
64 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
66 struct lttng_ust_event_field
*event_field
;
69 static pthread_key_t perf_counter_key
;
72 * lttng_perf_lock - Protect lttng-ust perf counter data structures
74 * Nests within the ust_lock, and therefore within the libc dl lock.
75 * Therefore, we need to allocate the TLS before nesting into this lock.
76 * Nests inside RCU bp read-side lock. Protects against concurrent
79 static pthread_mutex_t ust_perf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
82 * Track whether we are tracing from a signal handler nested on an
85 static DEFINE_URCU_TLS(int, ust_perf_mutex_nest
);
88 * Force a read (imply TLS allocation for dlopen) of TLS variables.
90 void lttng_ust_perf_counter_alloc_tls(void)
92 __asm__
__volatile__ ("" : : "m" (URCU_TLS(ust_perf_mutex_nest
)));
95 void lttng_perf_lock(void)
97 sigset_t sig_all_blocked
, orig_mask
;
100 if (lttng_ust_cancelstate_disable_push()) {
101 ERR("lttng_ust_cancelstate_disable_push");
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
);
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
;
127 sigfillset(&sig_all_blocked
);
128 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
130 ERR("pthread_sigmask: %s", strerror(ret
));
133 * Ensure the compiler don't move the store before the close()
134 * call, in case close() would be marked as leaf.
137 if (!--URCU_TLS(ust_perf_mutex_nest
)) {
138 pthread_mutex_unlock(&ust_perf_mutex
);
140 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
142 ERR("pthread_sigmask: %s", strerror(ret
));
144 if (lttng_ust_cancelstate_disable_pop()) {
145 ERR("lttng_ust_cancelstate_disable_pop");
150 size_t perf_counter_get_size(void *priv
__attribute__((unused
)),
151 struct lttng_ust_probe_ctx
*probe_ctx
__attribute__((unused
)),
156 size
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
157 size
+= sizeof(uint64_t);
162 uint64_t read_perf_counter_syscall(
163 struct lttng_perf_counter_thread_field
*thread_field
)
167 if (caa_unlikely(thread_field
->fd
< 0))
170 if (caa_unlikely(read(thread_field
->fd
, &count
, sizeof(count
))
177 #if defined(LTTNG_UST_ARCH_X86)
180 uint64_t rdpmc(unsigned int counter
)
182 unsigned int low
, high
;
184 __asm__
__volatile__("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
186 return low
| ((uint64_t) high
) << 32;
190 bool has_rdpmc(struct perf_event_mmap_page
*pc
)
192 if (caa_unlikely(!pc
->cap_bit0_is_deprecated
))
194 /* Since Linux kernel 3.12. */
195 return pc
->cap_user_rdpmc
;
199 uint64_t arch_read_perf_counter(
200 struct lttng_perf_counter_thread_field
*thread_field
)
204 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
206 if (caa_unlikely(!pc
))
210 seq
= CMM_LOAD_SHARED(pc
->lock
);
214 if (caa_likely(has_rdpmc(pc
) && idx
)) {
217 pmcval
= rdpmc(idx
- 1);
218 /* Sign-extend the pmc register result. */
219 pmcval
<<= 64 - pc
->pmc_width
;
220 pmcval
>>= 64 - pc
->pmc_width
;
221 count
= pc
->offset
+ pmcval
;
223 /* Fall-back on system call if rdpmc cannot be used. */
224 return read_perf_counter_syscall(thread_field
);
227 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
233 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
235 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
239 return !has_rdpmc(pc
);
244 /* Generic (slow) implementation using a read system call. */
246 uint64_t arch_read_perf_counter(
247 struct lttng_perf_counter_thread_field
*thread_field
)
249 return read_perf_counter_syscall(thread_field
);
253 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
__attribute__((unused
)))
261 int sys_perf_event_open(struct perf_event_attr
*attr
,
262 pid_t pid
, int cpu
, int group_fd
,
265 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
270 int open_perf_fd(struct perf_event_attr
*attr
)
274 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
282 void close_perf_fd(int fd
)
291 perror("Error closing LTTng-UST perf memory mapping FD");
295 static void setup_perf(struct lttng_perf_counter_thread_field
*thread_field
)
299 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
300 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
301 if (perf_addr
== MAP_FAILED
)
303 thread_field
->pc
= perf_addr
;
305 if (!arch_perf_keep_fd(thread_field
)) {
306 close_perf_fd(thread_field
->fd
);
307 thread_field
->fd
= -1;
312 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
318 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
320 PERROR("Error in munmap");
326 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
328 struct lttng_perf_counter_thread
*perf_thread
;
329 sigset_t newmask
, oldmask
;
332 ret
= sigfillset(&newmask
);
335 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
338 /* Check again with signals disabled */
339 perf_thread
= pthread_getspecific(perf_counter_key
);
342 perf_thread
= zmalloc(sizeof(*perf_thread
));
345 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
346 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
350 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
357 struct lttng_perf_counter_thread_field
*
358 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
359 struct lttng_perf_counter_thread
*perf_thread
)
361 struct lttng_perf_counter_thread_field
*thread_field
;
362 sigset_t newmask
, oldmask
;
365 ret
= sigfillset(&newmask
);
368 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
371 /* Check again with signals disabled */
372 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
374 if (thread_field
->field
== perf_field
)
377 thread_field
= zmalloc(sizeof(*thread_field
));
380 thread_field
->field
= perf_field
;
381 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
382 if (thread_field
->fd
>= 0)
383 setup_perf(thread_field
);
385 * Note: thread_field->pc can be NULL if setup_perf() fails.
386 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
389 cds_list_add_rcu(&thread_field
->rcu_field_node
,
390 &perf_thread
->rcu_field_list
);
391 cds_list_add(&thread_field
->thread_field_node
,
392 &perf_field
->thread_field_list
);
395 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
402 struct lttng_perf_counter_thread_field
*
403 get_thread_field(struct lttng_perf_counter_field
*field
)
405 struct lttng_perf_counter_thread
*perf_thread
;
406 struct lttng_perf_counter_thread_field
*thread_field
;
408 perf_thread
= pthread_getspecific(perf_counter_key
);
410 perf_thread
= alloc_perf_counter_thread();
411 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
413 if (thread_field
->field
== field
)
416 /* perf_counter_thread_field not found, need to add one */
417 return add_thread_field(field
, perf_thread
);
421 uint64_t wrapper_perf_counter_read(void *priv
)
423 struct lttng_perf_counter_field
*perf_field
;
424 struct lttng_perf_counter_thread_field
*perf_thread_field
;
426 perf_field
= (struct lttng_perf_counter_field
*) priv
;
427 perf_thread_field
= get_thread_field(perf_field
);
428 return arch_read_perf_counter(perf_thread_field
);
432 void perf_counter_record(void *priv
,
433 struct lttng_ust_probe_ctx
*probe_ctx
__attribute__((unused
)),
434 struct lttng_ust_ring_buffer_ctx
*ctx
,
435 struct lttng_ust_channel_buffer
*chan
)
439 value
= wrapper_perf_counter_read(priv
);
440 chan
->ops
->event_write(ctx
, &value
, sizeof(value
), lttng_ust_rb_alignof(value
));
444 void perf_counter_get_value(void *priv
,
445 struct lttng_ust_probe_ctx
*probe_ctx
__attribute__((unused
)),
446 struct lttng_ust_ctx_value
*value
)
448 value
->u
.u64
= wrapper_perf_counter_read(priv
);
451 /* Called with perf lock held */
453 void lttng_destroy_perf_thread_field(
454 struct lttng_perf_counter_thread_field
*thread_field
)
456 close_perf_fd(thread_field
->fd
);
457 unmap_perf_page(thread_field
->pc
);
458 cds_list_del_rcu(&thread_field
->rcu_field_node
);
459 cds_list_del(&thread_field
->thread_field_node
);
464 void lttng_destroy_perf_thread_key(void *_key
)
466 struct lttng_perf_counter_thread
*perf_thread
= _key
;
467 struct lttng_perf_counter_thread_field
*pos
, *p
;
470 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
472 lttng_destroy_perf_thread_field(pos
);
477 /* Called with UST lock held */
479 void lttng_destroy_perf_counter_ctx_field(void *priv
)
481 struct lttng_perf_counter_field
*perf_field
;
482 struct lttng_perf_counter_thread_field
*pos
, *p
;
484 perf_field
= (struct lttng_perf_counter_field
*) priv
;
485 free(perf_field
->name
);
487 * This put is performed when no threads can concurrently
488 * perform a "get" concurrently, thanks to urcu-bp grace
489 * period. Holding the lttng perf lock protects against
490 * concurrent modification of the per-thread thread field
494 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
496 lttng_destroy_perf_thread_field(pos
);
498 free(perf_field
->event_field
);
502 #ifdef LTTNG_UST_ARCH_ARMV7
505 int perf_get_exclude_kernel(void)
510 #else /* LTTNG_UST_ARCH_ARMV7 */
513 int perf_get_exclude_kernel(void)
518 #endif /* LTTNG_UST_ARCH_ARMV7 */
520 static const struct lttng_ust_type_common
*ust_type
=
521 lttng_ust_static_type_integer(sizeof(uint64_t) * CHAR_BIT
,
522 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
523 lttng_ust_is_signed_type(uint64_t),
524 LTTNG_UST_BYTE_ORDER
, 10);
526 /* Called with UST lock held */
527 int lttng_add_perf_counter_to_ctx(uint32_t type
,
530 struct lttng_ust_ctx
**ctx
)
532 struct lttng_ust_ctx_field ctx_field
;
533 struct lttng_ust_event_field
*event_field
;
534 struct lttng_perf_counter_field
*perf_field
;
538 if (lttng_find_context(*ctx
, name
)) {
542 name_alloc
= strdup(name
);
545 goto name_alloc_error
;
547 event_field
= zmalloc(sizeof(*event_field
));
550 goto event_field_alloc_error
;
552 event_field
->name
= name_alloc
;
553 event_field
->type
= ust_type
;
555 perf_field
= zmalloc(sizeof(*perf_field
));
558 goto perf_field_alloc_error
;
560 perf_field
->attr
.type
= type
;
561 perf_field
->attr
.config
= config
;
562 perf_field
->attr
.exclude_kernel
= perf_get_exclude_kernel();
563 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
564 perf_field
->name
= name_alloc
;
565 perf_field
->event_field
= event_field
;
567 /* Ensure that this perf counter can be used in this process. */
568 ret
= open_perf_fd(&perf_field
->attr
);
575 ctx_field
.event_field
= event_field
;
576 ctx_field
.get_size
= perf_counter_get_size
;
577 ctx_field
.record
= perf_counter_record
;
578 ctx_field
.get_value
= perf_counter_get_value
;
579 ctx_field
.destroy
= lttng_destroy_perf_counter_ctx_field
;
580 ctx_field
.priv
= perf_field
;
582 ret
= lttng_ust_context_append(ctx
, &ctx_field
);
585 goto append_context_error
;
589 append_context_error
:
592 perf_field_alloc_error
:
594 event_field_alloc_error
:
601 int lttng_perf_counter_init(void)
605 ret
= pthread_key_create(&perf_counter_key
,
606 lttng_destroy_perf_thread_key
);
612 void lttng_perf_counter_exit(void)
616 ret
= pthread_key_delete(perf_counter_key
);
619 PERROR("Error in pthread_key_delete");