2 * lttng-context-perf-counters.c
4 * LTTng UST performance monitoring counters (perf-counters) integration.
6 * Copyright (C) 2009-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
33 #include <sys/syscall.h>
34 #include <lttng/ust-events.h>
35 #include <lttng/ust-tracer.h>
36 #include <lttng/ringbuffer-config.h>
37 #include <urcu/system.h>
38 #include <urcu/arch.h>
39 #include <urcu/rculist.h>
42 #include <usterr-signal-safe.h>
44 #include <urcu/tls-compat.h>
45 #include "perf_event.h"
46 #include "lttng-tracer-core.h"
49 * We use a global perf counter key and iterate on per-thread RCU lists
50 * of fields in the fast path, even though this is not strictly speaking
51 * what would provide the best fast-path complexity, to ensure teardown
52 * of sessions vs thread exit is handled racelessly.
54 * Updates and traversals of thread_list are protected by UST lock.
55 * Updates to rcu_field_list are protected by UST lock.
58 struct lttng_perf_counter_thread_field
{
59 struct lttng_perf_counter_field
*field
; /* Back reference */
60 struct perf_event_mmap_page
*pc
;
61 struct cds_list_head thread_field_node
; /* Per-field list of thread fields (node) */
62 struct cds_list_head rcu_field_node
; /* RCU per-thread list of fields (node) */
66 struct lttng_perf_counter_thread
{
67 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
70 struct lttng_perf_counter_field
{
71 struct perf_event_attr attr
;
72 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
75 static pthread_key_t perf_counter_key
;
78 * lttng_perf_lock - Protect lttng-ust perf counter data structures
80 * Nests within the ust_lock, and therefore within the libc dl lock.
81 * Therefore, we need to fixup the TLS before nesting into this lock.
82 * Nests inside RCU bp read-side lock. Protects against concurrent
85 static pthread_mutex_t ust_perf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
88 * Cancel state when grabbing the ust_perf_mutex. Saved when locking,
89 * restored on unlock. Protected by ust_perf_mutex.
91 static int ust_perf_saved_cancelstate
;
94 * Track whether we are tracing from a signal handler nested on an
97 static DEFINE_URCU_TLS(int, ust_perf_mutex_nest
);
100 * Force a read (imply TLS fixup for dlopen) of TLS variables.
102 void lttng_ust_fixup_perf_counter_tls(void)
104 asm volatile ("" : : "m" (URCU_TLS(ust_perf_mutex_nest
)));
107 void lttng_perf_lock(void)
109 sigset_t sig_all_blocked
, orig_mask
;
112 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
114 ERR("pthread_setcancelstate: %s", strerror(ret
));
116 sigfillset(&sig_all_blocked
);
117 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
119 ERR("pthread_sigmask: %s", strerror(ret
));
121 if (!URCU_TLS(ust_perf_mutex_nest
)++) {
123 * Ensure the compiler don't move the store after the close()
124 * call in case close() would be marked as leaf.
127 pthread_mutex_lock(&ust_perf_mutex
);
128 ust_perf_saved_cancelstate
= oldstate
;
130 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
132 ERR("pthread_sigmask: %s", strerror(ret
));
136 void lttng_perf_unlock(void)
138 sigset_t sig_all_blocked
, orig_mask
;
139 int ret
, newstate
, oldstate
;
140 bool restore_cancel
= false;
142 sigfillset(&sig_all_blocked
);
143 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
145 ERR("pthread_sigmask: %s", strerror(ret
));
148 * Ensure the compiler don't move the store before the close()
149 * call, in case close() would be marked as leaf.
152 if (!--URCU_TLS(ust_perf_mutex_nest
)) {
153 newstate
= ust_perf_saved_cancelstate
;
154 restore_cancel
= true;
155 pthread_mutex_unlock(&ust_perf_mutex
);
157 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
159 ERR("pthread_sigmask: %s", strerror(ret
));
161 if (restore_cancel
) {
162 ret
= pthread_setcancelstate(newstate
, &oldstate
);
164 ERR("pthread_setcancelstate: %s", strerror(ret
));
170 size_t perf_counter_get_size(struct lttng_ctx_field
*field
, size_t offset
)
174 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
175 size
+= sizeof(uint64_t);
180 uint64_t read_perf_counter_syscall(
181 struct lttng_perf_counter_thread_field
*thread_field
)
185 if (caa_unlikely(thread_field
->fd
< 0))
188 if (caa_unlikely(read(thread_field
->fd
, &count
, sizeof(count
))
195 #if defined(__x86_64__) || defined(__i386__)
198 uint64_t rdpmc(unsigned int counter
)
200 unsigned int low
, high
;
202 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
204 return low
| ((uint64_t) high
) << 32;
208 bool has_rdpmc(struct perf_event_mmap_page
*pc
)
210 if (caa_unlikely(!pc
->cap_bit0_is_deprecated
))
212 /* Since Linux kernel 3.12. */
213 return pc
->cap_user_rdpmc
;
217 uint64_t arch_read_perf_counter(
218 struct lttng_perf_counter_thread_field
*thread_field
)
222 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
224 if (caa_unlikely(!pc
))
228 seq
= CMM_LOAD_SHARED(pc
->lock
);
232 if (caa_likely(has_rdpmc(pc
) && idx
)) {
235 pmcval
= rdpmc(idx
- 1);
236 /* Sign-extend the pmc register result. */
237 pmcval
<<= 64 - pc
->pmc_width
;
238 pmcval
>>= 64 - pc
->pmc_width
;
239 count
= pc
->offset
+ pmcval
;
241 /* Fall-back on system call if rdpmc cannot be used. */
242 return read_perf_counter_syscall(thread_field
);
245 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
251 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
253 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
257 return !has_rdpmc(pc
);
262 /* Generic (slow) implementation using a read system call. */
264 uint64_t arch_read_perf_counter(
265 struct lttng_perf_counter_thread_field
*thread_field
)
267 return read_perf_counter_syscall(thread_field
);
271 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
279 int sys_perf_event_open(struct perf_event_attr
*attr
,
280 pid_t pid
, int cpu
, int group_fd
,
283 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
288 int open_perf_fd(struct perf_event_attr
*attr
)
292 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
300 void close_perf_fd(int fd
)
309 perror("Error closing LTTng-UST perf memory mapping FD");
313 static void setup_perf(struct lttng_perf_counter_thread_field
*thread_field
)
317 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
318 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
319 if (perf_addr
== MAP_FAILED
)
321 thread_field
->pc
= perf_addr
;
323 if (!arch_perf_keep_fd(thread_field
)) {
324 close_perf_fd(thread_field
->fd
);
325 thread_field
->fd
= -1;
330 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
336 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
338 PERROR("Error in munmap");
344 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
346 struct lttng_perf_counter_thread
*perf_thread
;
347 sigset_t newmask
, oldmask
;
350 ret
= sigfillset(&newmask
);
353 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
356 /* Check again with signals disabled */
357 perf_thread
= pthread_getspecific(perf_counter_key
);
360 perf_thread
= zmalloc(sizeof(*perf_thread
));
363 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
364 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
368 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
375 struct lttng_perf_counter_thread_field
*
376 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
377 struct lttng_perf_counter_thread
*perf_thread
)
379 struct lttng_perf_counter_thread_field
*thread_field
;
380 sigset_t newmask
, oldmask
;
383 ret
= sigfillset(&newmask
);
386 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
389 /* Check again with signals disabled */
390 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
392 if (thread_field
->field
== perf_field
)
395 thread_field
= zmalloc(sizeof(*thread_field
));
398 thread_field
->field
= perf_field
;
399 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
400 if (thread_field
->fd
>= 0)
401 setup_perf(thread_field
);
403 * Note: thread_field->pc can be NULL if setup_perf() fails.
404 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
407 cds_list_add_rcu(&thread_field
->rcu_field_node
,
408 &perf_thread
->rcu_field_list
);
409 cds_list_add(&thread_field
->thread_field_node
,
410 &perf_field
->thread_field_list
);
413 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
420 struct lttng_perf_counter_thread_field
*
421 get_thread_field(struct lttng_perf_counter_field
*field
)
423 struct lttng_perf_counter_thread
*perf_thread
;
424 struct lttng_perf_counter_thread_field
*thread_field
;
426 perf_thread
= pthread_getspecific(perf_counter_key
);
428 perf_thread
= alloc_perf_counter_thread();
429 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
431 if (thread_field
->field
== field
)
434 /* perf_counter_thread_field not found, need to add one */
435 return add_thread_field(field
, perf_thread
);
439 uint64_t wrapper_perf_counter_read(struct lttng_ctx_field
*field
)
441 struct lttng_perf_counter_field
*perf_field
;
442 struct lttng_perf_counter_thread_field
*perf_thread_field
;
444 perf_field
= field
->u
.perf_counter
;
445 perf_thread_field
= get_thread_field(perf_field
);
446 return arch_read_perf_counter(perf_thread_field
);
450 void perf_counter_record(struct lttng_ctx_field
*field
,
451 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
452 struct lttng_channel
*chan
)
456 value
= wrapper_perf_counter_read(field
);
457 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
458 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
462 void perf_counter_get_value(struct lttng_ctx_field
*field
,
463 struct lttng_ctx_value
*value
)
465 value
->u
.s64
= wrapper_perf_counter_read(field
);
468 /* Called with perf lock held */
470 void lttng_destroy_perf_thread_field(
471 struct lttng_perf_counter_thread_field
*thread_field
)
473 close_perf_fd(thread_field
->fd
);
474 unmap_perf_page(thread_field
->pc
);
475 cds_list_del_rcu(&thread_field
->rcu_field_node
);
476 cds_list_del(&thread_field
->thread_field_node
);
481 void lttng_destroy_perf_thread_key(void *_key
)
483 struct lttng_perf_counter_thread
*perf_thread
= _key
;
484 struct lttng_perf_counter_thread_field
*pos
, *p
;
487 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
489 lttng_destroy_perf_thread_field(pos
);
494 /* Called with UST lock held */
496 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
498 struct lttng_perf_counter_field
*perf_field
;
499 struct lttng_perf_counter_thread_field
*pos
, *p
;
501 free((char *) field
->event_field
.name
);
502 perf_field
= field
->u
.perf_counter
;
504 * This put is performed when no threads can concurrently
505 * perform a "get" concurrently, thanks to urcu-bp grace
506 * period. Holding the lttng perf lock protects against
507 * concurrent modification of the per-thread thread field
511 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
513 lttng_destroy_perf_thread_field(pos
);
518 #ifdef __ARM_ARCH_7A__
521 int perf_get_exclude_kernel(void)
526 #else /* __ARM_ARCH_7A__ */
529 int perf_get_exclude_kernel(void)
534 #endif /* __ARM_ARCH_7A__ */
536 /* Called with UST lock held */
537 int lttng_add_perf_counter_to_ctx(uint32_t type
,
540 struct lttng_ctx
**ctx
)
542 struct lttng_ctx_field
*field
;
543 struct lttng_perf_counter_field
*perf_field
;
547 name_alloc
= strdup(name
);
550 goto name_alloc_error
;
552 perf_field
= zmalloc(sizeof(*perf_field
));
555 goto perf_field_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
.atype
= atype_integer
;
571 field
->event_field
.type
.u
.integer
.size
=
572 sizeof(uint64_t) * CHAR_BIT
;
573 field
->event_field
.type
.u
.integer
.alignment
=
574 lttng_alignof(uint64_t) * CHAR_BIT
;
575 field
->event_field
.type
.u
.integer
.signedness
=
576 lttng_is_signed_type(uint64_t);
577 field
->event_field
.type
.u
.integer
.reverse_byte_order
= 0;
578 field
->event_field
.type
.u
.integer
.base
= 10;
579 field
->event_field
.type
.u
.integer
.encoding
= lttng_encode_none
;
580 field
->get_size
= perf_counter_get_size
;
581 field
->record
= perf_counter_record
;
582 field
->get_value
= perf_counter_get_value
;
584 perf_field
->attr
.type
= type
;
585 perf_field
->attr
.config
= config
;
586 perf_field
->attr
.exclude_kernel
= perf_get_exclude_kernel();
587 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
588 field
->u
.perf_counter
= perf_field
;
590 /* Ensure that this perf counter can be used in this process. */
591 ret
= open_perf_fd(&perf_field
->attr
);
599 * Contexts can only be added before tracing is started, so we
600 * don't have to synchronize against concurrent threads using
604 lttng_context_update(*ctx
);
609 lttng_remove_context_field(ctx
, field
);
610 append_context_error
:
612 perf_field_alloc_error
:
618 int lttng_perf_counter_init(void)
622 ret
= pthread_key_create(&perf_counter_key
,
623 lttng_destroy_perf_thread_key
);
629 void lttng_perf_counter_exit(void)
633 ret
= pthread_key_delete(perf_counter_key
);
636 PERROR("Error in pthread_key_delete");