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>
32 #include <sys/syscall.h>
33 #include <lttng/ust-events.h>
34 #include <lttng/ust-tracer.h>
35 #include <lttng/ringbuffer-config.h>
36 #include <urcu/system.h>
37 #include <urcu/arch.h>
38 #include <urcu/rculist.h>
41 #include <usterr-signal-safe.h>
43 #include <urcu/tls-compat.h>
44 #include "perf_event.h"
45 #include "lttng-tracer-core.h"
48 * We use a global perf counter key and iterate on per-thread RCU lists
49 * of fields in the fast path, even though this is not strictly speaking
50 * what would provide the best fast-path complexity, to ensure teardown
51 * of sessions vs thread exit is handled racelessly.
53 * Updates and traversals of thread_list are protected by UST lock.
54 * Updates to rcu_field_list are protected by UST lock.
57 struct lttng_perf_counter_thread_field
{
58 struct lttng_perf_counter_field
*field
; /* Back reference */
59 struct perf_event_mmap_page
*pc
;
60 struct cds_list_head thread_field_node
; /* Per-field list of thread fields (node) */
61 struct cds_list_head rcu_field_node
; /* RCU per-thread list of fields (node) */
65 struct lttng_perf_counter_thread
{
66 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
69 struct lttng_perf_counter_field
{
70 struct perf_event_attr attr
;
71 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
74 static pthread_key_t perf_counter_key
;
77 * lttng_perf_lock - Protect lttng-ust perf counter data structures
79 * Nests within the ust_lock, and therefore within the libc dl lock.
80 * Therefore, we need to fixup the TLS before nesting into this lock.
81 * Nests inside RCU bp read-side lock. Protects against concurrent
84 static pthread_mutex_t ust_perf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
87 * Cancel state when grabbing the ust_perf_mutex. Saved when locking,
88 * restored on unlock. Protected by ust_perf_mutex.
90 static int ust_perf_saved_cancelstate
;
93 * Track whether we are tracing from a signal handler nested on an
96 static DEFINE_URCU_TLS(int, ust_perf_mutex_nest
);
99 * Force a read (imply TLS fixup for dlopen) of TLS variables.
101 void lttng_ust_fixup_perf_counter_tls(void)
103 asm volatile ("" : : "m" (URCU_TLS(ust_perf_mutex_nest
)));
106 void lttng_perf_lock(void)
108 sigset_t sig_all_blocked
, orig_mask
;
111 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
113 ERR("pthread_setcancelstate: %s", strerror(ret
));
115 sigfillset(&sig_all_blocked
);
116 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
118 ERR("pthread_sigmask: %s", strerror(ret
));
120 if (!URCU_TLS(ust_perf_mutex_nest
)++) {
122 * Ensure the compiler don't move the store after the close()
123 * call in case close() would be marked as leaf.
126 pthread_mutex_lock(&ust_perf_mutex
);
127 ust_perf_saved_cancelstate
= oldstate
;
129 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
131 ERR("pthread_sigmask: %s", strerror(ret
));
135 void lttng_perf_unlock(void)
137 sigset_t sig_all_blocked
, orig_mask
;
138 int ret
, newstate
, oldstate
;
139 bool restore_cancel
= false;
141 sigfillset(&sig_all_blocked
);
142 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
144 ERR("pthread_sigmask: %s", strerror(ret
));
147 * Ensure the compiler don't move the store before the close()
148 * call, in case close() would be marked as leaf.
151 if (!--URCU_TLS(ust_perf_mutex_nest
)) {
152 newstate
= ust_perf_saved_cancelstate
;
153 restore_cancel
= true;
154 pthread_mutex_unlock(&ust_perf_mutex
);
156 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
158 ERR("pthread_sigmask: %s", strerror(ret
));
160 if (restore_cancel
) {
161 ret
= pthread_setcancelstate(newstate
, &oldstate
);
163 ERR("pthread_setcancelstate: %s", strerror(ret
));
169 size_t perf_counter_get_size(struct lttng_ctx_field
*field
, size_t offset
)
173 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
174 size
+= sizeof(uint64_t);
179 uint64_t read_perf_counter_syscall(
180 struct lttng_perf_counter_thread_field
*thread_field
)
184 if (caa_unlikely(thread_field
->fd
< 0))
187 if (caa_unlikely(read(thread_field
->fd
, &count
, sizeof(count
))
194 #if defined(__x86_64__) || defined(__i386__)
197 uint64_t rdpmc(unsigned int counter
)
199 unsigned int low
, high
;
201 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
203 return low
| ((uint64_t) high
) << 32;
207 bool has_rdpmc(struct perf_event_mmap_page
*pc
)
209 if (caa_unlikely(!pc
->cap_bit0_is_deprecated
))
211 /* Since Linux kernel 3.12. */
212 return pc
->cap_user_rdpmc
;
216 uint64_t arch_read_perf_counter(
217 struct lttng_perf_counter_thread_field
*thread_field
)
221 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
223 if (caa_unlikely(!pc
))
227 seq
= CMM_LOAD_SHARED(pc
->lock
);
231 if (caa_likely(has_rdpmc(pc
) && idx
)) {
234 pmcval
= rdpmc(idx
- 1);
235 /* Sign-extend the pmc register result. */
236 pmcval
<<= 64 - pc
->pmc_width
;
237 pmcval
>>= 64 - pc
->pmc_width
;
238 count
= pc
->offset
+ pmcval
;
240 /* Fall-back on system call if rdpmc cannot be used. */
241 return read_perf_counter_syscall(thread_field
);
244 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
250 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
252 struct perf_event_mmap_page
*pc
= thread_field
->pc
;
256 return !has_rdpmc(pc
);
261 /* Generic (slow) implementation using a read system call. */
263 uint64_t arch_read_perf_counter(
264 struct lttng_perf_counter_thread_field
*thread_field
)
266 return read_perf_counter_syscall(thread_field
);
270 int arch_perf_keep_fd(struct lttng_perf_counter_thread_field
*thread_field
)
278 int sys_perf_event_open(struct perf_event_attr
*attr
,
279 pid_t pid
, int cpu
, int group_fd
,
282 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
287 int open_perf_fd(struct perf_event_attr
*attr
)
291 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
299 void close_perf_fd(int fd
)
308 perror("Error closing LTTng-UST perf memory mapping FD");
312 static void setup_perf(struct lttng_perf_counter_thread_field
*thread_field
)
316 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
317 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
318 if (perf_addr
== MAP_FAILED
)
320 thread_field
->pc
= perf_addr
;
322 if (!arch_perf_keep_fd(thread_field
)) {
323 close_perf_fd(thread_field
->fd
);
324 thread_field
->fd
= -1;
329 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
335 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
337 PERROR("Error in munmap");
343 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
345 struct lttng_perf_counter_thread
*perf_thread
;
346 sigset_t newmask
, oldmask
;
349 ret
= sigfillset(&newmask
);
352 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
355 /* Check again with signals disabled */
356 perf_thread
= pthread_getspecific(perf_counter_key
);
359 perf_thread
= zmalloc(sizeof(*perf_thread
));
362 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
363 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
367 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
374 struct lttng_perf_counter_thread_field
*
375 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
376 struct lttng_perf_counter_thread
*perf_thread
)
378 struct lttng_perf_counter_thread_field
*thread_field
;
379 sigset_t newmask
, oldmask
;
382 ret
= sigfillset(&newmask
);
385 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
388 /* Check again with signals disabled */
389 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
391 if (thread_field
->field
== perf_field
)
394 thread_field
= zmalloc(sizeof(*thread_field
));
397 thread_field
->field
= perf_field
;
398 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
399 if (thread_field
->fd
>= 0)
400 setup_perf(thread_field
);
402 * Note: thread_field->pc can be NULL if setup_perf() fails.
403 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
406 cds_list_add_rcu(&thread_field
->rcu_field_node
,
407 &perf_thread
->rcu_field_list
);
408 cds_list_add(&thread_field
->thread_field_node
,
409 &perf_field
->thread_field_list
);
412 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
419 struct lttng_perf_counter_thread_field
*
420 get_thread_field(struct lttng_perf_counter_field
*field
)
422 struct lttng_perf_counter_thread
*perf_thread
;
423 struct lttng_perf_counter_thread_field
*thread_field
;
425 perf_thread
= pthread_getspecific(perf_counter_key
);
427 perf_thread
= alloc_perf_counter_thread();
428 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
430 if (thread_field
->field
== field
)
433 /* perf_counter_thread_field not found, need to add one */
434 return add_thread_field(field
, perf_thread
);
438 uint64_t wrapper_perf_counter_read(struct lttng_ctx_field
*field
)
440 struct lttng_perf_counter_field
*perf_field
;
441 struct lttng_perf_counter_thread_field
*perf_thread_field
;
443 perf_field
= field
->u
.perf_counter
;
444 perf_thread_field
= get_thread_field(perf_field
);
445 return arch_read_perf_counter(perf_thread_field
);
449 void perf_counter_record(struct lttng_ctx_field
*field
,
450 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
451 struct lttng_channel
*chan
)
455 value
= wrapper_perf_counter_read(field
);
456 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
457 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
461 void perf_counter_get_value(struct lttng_ctx_field
*field
,
462 struct lttng_ctx_value
*value
)
464 value
->u
.s64
= wrapper_perf_counter_read(field
);
467 /* Called with perf lock held */
469 void lttng_destroy_perf_thread_field(
470 struct lttng_perf_counter_thread_field
*thread_field
)
472 close_perf_fd(thread_field
->fd
);
473 unmap_perf_page(thread_field
->pc
);
474 cds_list_del_rcu(&thread_field
->rcu_field_node
);
475 cds_list_del(&thread_field
->thread_field_node
);
480 void lttng_destroy_perf_thread_key(void *_key
)
482 struct lttng_perf_counter_thread
*perf_thread
= _key
;
483 struct lttng_perf_counter_thread_field
*pos
, *p
;
486 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
488 lttng_destroy_perf_thread_field(pos
);
493 /* Called with UST lock held */
495 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
497 struct lttng_perf_counter_field
*perf_field
;
498 struct lttng_perf_counter_thread_field
*pos
, *p
;
500 free((char *) field
->event_field
.name
);
501 perf_field
= field
->u
.perf_counter
;
503 * This put is performed when no threads can concurrently
504 * perform a "get" concurrently, thanks to urcu-bp grace
505 * period. Holding the lttng perf lock protects against
506 * concurrent modification of the per-thread thread field
510 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
512 lttng_destroy_perf_thread_field(pos
);
517 #ifdef __ARM_ARCH_7A__
520 int perf_get_exclude_kernel(void)
525 #else /* __ARM_ARCH_7A__ */
528 int perf_get_exclude_kernel(void)
533 #endif /* __ARM_ARCH_7A__ */
535 /* Called with UST lock held */
536 int lttng_add_perf_counter_to_ctx(uint32_t type
,
539 struct lttng_ctx
**ctx
)
541 struct lttng_ctx_field
*field
;
542 struct lttng_perf_counter_field
*perf_field
;
546 name_alloc
= strdup(name
);
549 goto name_alloc_error
;
551 perf_field
= zmalloc(sizeof(*perf_field
));
554 goto perf_field_alloc_error
;
556 field
= lttng_append_context(ctx
);
559 goto append_context_error
;
561 if (lttng_find_context(*ctx
, name_alloc
)) {
566 field
->destroy
= lttng_destroy_perf_counter_field
;
568 field
->event_field
.name
= name_alloc
;
569 field
->event_field
.type
.atype
= atype_integer
;
570 field
->event_field
.type
.u
.basic
.integer
.size
=
571 sizeof(uint64_t) * CHAR_BIT
;
572 field
->event_field
.type
.u
.basic
.integer
.alignment
=
573 lttng_alignof(uint64_t) * CHAR_BIT
;
574 field
->event_field
.type
.u
.basic
.integer
.signedness
=
575 lttng_is_signed_type(uint64_t);
576 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
577 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
578 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
579 field
->get_size
= perf_counter_get_size
;
580 field
->record
= perf_counter_record
;
581 field
->get_value
= perf_counter_get_value
;
583 perf_field
->attr
.type
= type
;
584 perf_field
->attr
.config
= config
;
585 perf_field
->attr
.exclude_kernel
= perf_get_exclude_kernel();
586 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
587 field
->u
.perf_counter
= perf_field
;
589 /* Ensure that this perf counter can be used in this process. */
590 ret
= open_perf_fd(&perf_field
->attr
);
598 * Contexts can only be added before tracing is started, so we
599 * don't have to synchronize against concurrent threads using
603 lttng_context_update(*ctx
);
608 lttng_remove_context_field(ctx
, field
);
609 append_context_error
:
611 perf_field_alloc_error
:
617 int lttng_perf_counter_init(void)
621 ret
= pthread_key_create(&perf_counter_key
,
622 lttng_destroy_perf_thread_key
);
628 void lttng_perf_counter_exit(void)
632 ret
= pthread_key_delete(perf_counter_key
);
635 PERROR("Error in pthread_key_delete");