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 <urcu/system.h>
26 #include <urcu/arch.h>
27 #include <urcu/rculist.h>
28 #include "common/macros.h"
30 #include "common/logging.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 "lib/lttng-ust/events.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 */
65 struct lttng_ust_event_field
*event_field
;
68 static pthread_key_t perf_counter_key
;
71 * lttng_perf_lock - Protect lttng-ust perf counter data structures
73 * Nests within the ust_lock, and therefore within the libc dl lock.
74 * Therefore, we need to fixup the TLS before nesting into this lock.
75 * Nests inside RCU bp read-side lock. Protects against concurrent
78 static pthread_mutex_t ust_perf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
81 * Cancel state when grabbing the ust_perf_mutex. Saved when locking,
82 * restored on unlock. Protected by ust_perf_mutex.
84 static int ust_perf_saved_cancelstate
;
87 * Track whether we are tracing from a signal handler nested on an
90 static DEFINE_URCU_TLS(int, ust_perf_mutex_nest
);
93 * Force a read (imply TLS fixup for dlopen) of TLS variables.
95 void lttng_ust_fixup_perf_counter_tls(void)
97 asm volatile ("" : : "m" (URCU_TLS(ust_perf_mutex_nest
)));
100 void lttng_perf_lock(void)
102 sigset_t sig_all_blocked
, orig_mask
;
105 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
107 ERR("pthread_setcancelstate: %s", strerror(ret
));
109 sigfillset(&sig_all_blocked
);
110 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
112 ERR("pthread_sigmask: %s", strerror(ret
));
114 if (!URCU_TLS(ust_perf_mutex_nest
)++) {
116 * Ensure the compiler don't move the store after the close()
117 * call in case close() would be marked as leaf.
120 pthread_mutex_lock(&ust_perf_mutex
);
121 ust_perf_saved_cancelstate
= oldstate
;
123 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
125 ERR("pthread_sigmask: %s", strerror(ret
));
129 void lttng_perf_unlock(void)
131 sigset_t sig_all_blocked
, orig_mask
;
132 int ret
, newstate
, oldstate
;
133 bool restore_cancel
= false;
135 sigfillset(&sig_all_blocked
);
136 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
138 ERR("pthread_sigmask: %s", strerror(ret
));
141 * Ensure the compiler don't move the store before the close()
142 * call, in case close() would be marked as leaf.
145 if (!--URCU_TLS(ust_perf_mutex_nest
)) {
146 newstate
= ust_perf_saved_cancelstate
;
147 restore_cancel
= true;
148 pthread_mutex_unlock(&ust_perf_mutex
);
150 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
152 ERR("pthread_sigmask: %s", strerror(ret
));
154 if (restore_cancel
) {
155 ret
= pthread_setcancelstate(newstate
, &oldstate
);
157 ERR("pthread_setcancelstate: %s", strerror(ret
));
163 size_t perf_counter_get_size(void *priv
__attribute__((unused
)),
168 size
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(uint64_t));
169 size
+= sizeof(uint64_t);
174 uint64_t read_perf_counter_syscall(
175 struct lttng_perf_counter_thread_field
*thread_field
)
179 if (caa_unlikely(thread_field
->fd
< 0))
182 if (caa_unlikely(read(thread_field
->fd
, &count
, sizeof(count
))
189 #if defined(LTTNG_UST_ARCH_X86)
192 uint64_t rdpmc(unsigned int counter
)
194 unsigned int low
, high
;
196 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
198 return low
| ((uint64_t) high
) << 32;
202 bool has_rdpmc(struct perf_event_mmap_page
*pc
)
204 if (caa_unlikely(!pc
->cap_bit0_is_deprecated
))
206 /* Since Linux kernel 3.12. */
207 return pc
->cap_user_rdpmc
;
211 uint64_t arch_read_perf_counter(
212 struct lttng_perf_counter_thread_field
*thread_field
)
216 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
218 if (caa_unlikely(!pc
))
222 seq
= CMM_LOAD_SHARED(pc
->lock
);
226 if (caa_likely(has_rdpmc(pc
) && idx
)) {
229 pmcval
= rdpmc(idx
- 1);
230 /* Sign-extend the pmc register result. */
231 pmcval
<<= 64 - pc
->pmc_width
;
232 pmcval
>>= 64 - pc
->pmc_width
;
233 count
= pc
->offset
+ pmcval
;
235 /* Fall-back on system call if rdpmc cannot be used. */
236 return read_perf_counter_syscall(thread_field
);
239 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
245 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
247 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
251 return !has_rdpmc(pc
);
256 /* Generic (slow) implementation using a read system call. */
258 uint64_t arch_read_perf_counter(
259 struct lttng_perf_counter_thread_field
*thread_field
)
261 return read_perf_counter_syscall(thread_field
);
265 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
__attribute__((unused
)))
273 int sys_perf_event_open(struct perf_event_attr
*attr
,
274 pid_t pid
, int cpu
, int group_fd
,
277 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
282 int open_perf_fd(struct perf_event_attr
*attr
)
286 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
294 void close_perf_fd(int fd
)
303 perror("Error closing LTTng-UST perf memory mapping FD");
307 static void setup_perf(struct lttng_perf_counter_thread_field
*thread_field
)
311 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
312 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
313 if (perf_addr
== MAP_FAILED
)
315 thread_field
->pc
= perf_addr
;
317 if (!arch_perf_keep_fd(thread_field
)) {
318 close_perf_fd(thread_field
->fd
);
319 thread_field
->fd
= -1;
324 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
330 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
332 PERROR("Error in munmap");
338 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
340 struct lttng_perf_counter_thread
*perf_thread
;
341 sigset_t newmask
, oldmask
;
344 ret
= sigfillset(&newmask
);
347 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
350 /* Check again with signals disabled */
351 perf_thread
= pthread_getspecific(perf_counter_key
);
354 perf_thread
= zmalloc(sizeof(*perf_thread
));
357 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
358 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
362 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
369 struct lttng_perf_counter_thread_field
*
370 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
371 struct lttng_perf_counter_thread
*perf_thread
)
373 struct lttng_perf_counter_thread_field
*thread_field
;
374 sigset_t newmask
, oldmask
;
377 ret
= sigfillset(&newmask
);
380 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
383 /* Check again with signals disabled */
384 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
386 if (thread_field
->field
== perf_field
)
389 thread_field
= zmalloc(sizeof(*thread_field
));
392 thread_field
->field
= perf_field
;
393 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
394 if (thread_field
->fd
>= 0)
395 setup_perf(thread_field
);
397 * Note: thread_field->pc can be NULL if setup_perf() fails.
398 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
401 cds_list_add_rcu(&thread_field
->rcu_field_node
,
402 &perf_thread
->rcu_field_list
);
403 cds_list_add(&thread_field
->thread_field_node
,
404 &perf_field
->thread_field_list
);
407 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
414 struct lttng_perf_counter_thread_field
*
415 get_thread_field(struct lttng_perf_counter_field
*field
)
417 struct lttng_perf_counter_thread
*perf_thread
;
418 struct lttng_perf_counter_thread_field
*thread_field
;
420 perf_thread
= pthread_getspecific(perf_counter_key
);
422 perf_thread
= alloc_perf_counter_thread();
423 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
425 if (thread_field
->field
== field
)
428 /* perf_counter_thread_field not found, need to add one */
429 return add_thread_field(field
, perf_thread
);
433 uint64_t wrapper_perf_counter_read(void *priv
)
435 struct lttng_perf_counter_field
*perf_field
;
436 struct lttng_perf_counter_thread_field
*perf_thread_field
;
438 perf_field
= (struct lttng_perf_counter_field
*) priv
;
439 perf_thread_field
= get_thread_field(perf_field
);
440 return arch_read_perf_counter(perf_thread_field
);
444 void perf_counter_record(void *priv
,
445 struct lttng_ust_ring_buffer_ctx
*ctx
,
446 struct lttng_ust_channel_buffer
*chan
)
450 value
= wrapper_perf_counter_read(priv
);
451 chan
->ops
->event_write(ctx
, &value
, sizeof(value
), lttng_ust_rb_alignof(value
));
455 void perf_counter_get_value(void *priv
,
456 struct lttng_ust_ctx_value
*value
)
458 value
->u
.s64
= wrapper_perf_counter_read(priv
);
461 /* Called with perf lock held */
463 void lttng_destroy_perf_thread_field(
464 struct lttng_perf_counter_thread_field
*thread_field
)
466 close_perf_fd(thread_field
->fd
);
467 unmap_perf_page(thread_field
->pc
);
468 cds_list_del_rcu(&thread_field
->rcu_field_node
);
469 cds_list_del(&thread_field
->thread_field_node
);
474 void lttng_destroy_perf_thread_key(void *_key
)
476 struct lttng_perf_counter_thread
*perf_thread
= _key
;
477 struct lttng_perf_counter_thread_field
*pos
, *p
;
480 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
482 lttng_destroy_perf_thread_field(pos
);
487 /* Called with UST lock held */
489 void lttng_destroy_perf_counter_ctx_field(void *priv
)
491 struct lttng_perf_counter_field
*perf_field
;
492 struct lttng_perf_counter_thread_field
*pos
, *p
;
494 perf_field
= (struct lttng_perf_counter_field
*) priv
;
495 free(perf_field
->name
);
497 * This put is performed when no threads can concurrently
498 * perform a "get" concurrently, thanks to urcu-bp grace
499 * period. Holding the lttng perf lock protects against
500 * concurrent modification of the per-thread thread field
504 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
506 lttng_destroy_perf_thread_field(pos
);
508 free(perf_field
->event_field
);
512 #ifdef LTTNG_UST_ARCH_ARMV7
515 int perf_get_exclude_kernel(void)
520 #else /* LTTNG_UST_ARCH_ARMV7 */
523 int perf_get_exclude_kernel(void)
528 #endif /* LTTNG_UST_ARCH_ARMV7 */
530 static const struct lttng_ust_type_common
*ust_type
=
531 lttng_ust_static_type_integer(sizeof(uint64_t) * CHAR_BIT
,
532 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
533 lttng_ust_is_signed_type(uint64_t),
534 LTTNG_UST_BYTE_ORDER
, 10);
536 /* Called with UST lock held */
537 int lttng_add_perf_counter_to_ctx(uint32_t type
,
540 struct lttng_ust_ctx
**ctx
)
542 struct lttng_ust_ctx_field ctx_field
;
543 struct lttng_ust_event_field
*event_field
;
544 struct lttng_perf_counter_field
*perf_field
;
548 if (lttng_find_context(*ctx
, name
)) {
552 name_alloc
= strdup(name
);
555 goto name_alloc_error
;
557 event_field
= zmalloc(sizeof(*event_field
));
560 goto event_field_alloc_error
;
562 event_field
->name
= name_alloc
;
563 event_field
->type
= ust_type
;
565 perf_field
= zmalloc(sizeof(*perf_field
));
568 goto perf_field_alloc_error
;
570 perf_field
->attr
.type
= type
;
571 perf_field
->attr
.config
= config
;
572 perf_field
->attr
.exclude_kernel
= perf_get_exclude_kernel();
573 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
574 perf_field
->name
= name_alloc
;
575 perf_field
->event_field
= event_field
;
577 /* Ensure that this perf counter can be used in this process. */
578 ret
= open_perf_fd(&perf_field
->attr
);
585 ctx_field
.event_field
= event_field
;
586 ctx_field
.get_size
= perf_counter_get_size
;
587 ctx_field
.record
= perf_counter_record
;
588 ctx_field
.get_value
= perf_counter_get_value
;
589 ctx_field
.destroy
= lttng_destroy_perf_counter_ctx_field
;
590 ctx_field
.priv
= perf_field
;
592 ret
= lttng_ust_context_append(ctx
, &ctx_field
);
595 goto append_context_error
;
599 append_context_error
:
602 perf_field_alloc_error
:
604 event_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");