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
23 #include <sys/types.h>
29 #include <sys/syscall.h>
30 #include <linux/perf_event.h>
31 #include <lttng/ust-events.h>
32 #include <lttng/ust-tracer.h>
33 #include <lttng/ringbuffer-config.h>
34 #include <urcu/system.h>
35 #include <urcu/arch.h>
36 #include <urcu/rculist.h>
39 #include <usterr-signal-safe.h>
41 #include "lttng-tracer-core.h"
44 * We use a global perf counter key and iterate on per-thread RCU lists
45 * of fields in the fast path, even though this is not strictly speaking
46 * what would provide the best fast-path complexity, to ensure teardown
47 * of sessions vs thread exit is handled racelessly.
49 * Updates and traversals of thread_list are protected by UST lock.
50 * Updates to rcu_field_list are protected by UST lock.
53 struct lttng_perf_counter_thread_field
{
54 struct lttng_perf_counter_field
*field
; /* Back reference */
55 struct perf_event_mmap_page
*pc
;
56 struct cds_list_head thread_field_node
; /* Per-field list of thread fields (node) */
57 struct cds_list_head rcu_field_node
; /* RCU per-thread list of fields (node) */
61 struct lttng_perf_counter_thread
{
62 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
65 struct lttng_perf_counter_field
{
66 struct perf_event_attr attr
;
67 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
70 static pthread_key_t perf_counter_key
;
73 size_t perf_counter_get_size(struct lttng_ctx_field
*field
, size_t offset
)
77 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
78 size
+= sizeof(uint64_t);
82 #if defined(__x86_64__) || defined(__i386__)
85 uint64_t rdpmc(unsigned int counter
)
87 unsigned int low
, high
;
89 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
91 return low
| ((uint64_t) high
) << 32;
94 static bool arch_perf_use_read(void)
99 #else /* defined(__x86_64__) || defined(__i386__) */
101 #error "Perf event counters are only supported on x86 so far."
103 #endif /* #else defined(__x86_64__) || defined(__i386__) */
106 uint64_t read_perf_counter(struct perf_event_mmap_page
*pc
)
111 if (caa_unlikely(!pc
))
115 seq
= CMM_LOAD_SHARED(pc
->lock
);
120 count
= pc
->offset
+ rdpmc(idx
- 1);
125 } while (CMM_LOAD_SHARED(pc
->lock
) != seq
);
131 int sys_perf_event_open(struct perf_event_attr
*attr
,
132 pid_t pid
, int cpu
, int group_fd
,
135 return syscall(SYS_perf_event_open
, attr
, pid
, cpu
,
140 int open_perf_fd(struct perf_event_attr
*attr
)
144 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
152 struct perf_event_mmap_page
*setup_perf(
153 struct lttng_perf_counter_thread_field
*thread_field
)
157 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
158 PROT_READ
, MAP_SHARED
, thread_field
->fd
, 0);
159 if (perf_addr
== MAP_FAILED
)
162 if (!arch_perf_use_read()) {
163 close_perf_fd(thread_field
->fd
);
164 thread_field
->fd
= -1;
172 void close_perf_fd(int fd
)
181 perror("Error closing LTTng-UST perf memory mapping FD");
185 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
191 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
193 PERROR("Error in munmap");
199 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
201 struct lttng_perf_counter_thread
*perf_thread
;
202 sigset_t newmask
, oldmask
;
205 ret
= sigfillset(&newmask
);
208 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
211 /* Check again with signals disabled */
212 perf_thread
= pthread_getspecific(perf_counter_key
);
215 perf_thread
= zmalloc(sizeof(*perf_thread
));
218 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
219 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
223 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
230 struct lttng_perf_counter_thread_field
*
231 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
232 struct lttng_perf_counter_thread
*perf_thread
)
234 struct lttng_perf_counter_thread_field
*thread_field
;
235 sigset_t newmask
, oldmask
;
238 ret
= sigfillset(&newmask
);
241 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
244 /* Check again with signals disabled */
245 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
247 if (thread_field
->field
== perf_field
)
250 thread_field
= zmalloc(sizeof(*thread_field
));
253 thread_field
->field
= perf_field
;
254 thread_field
->fd
= open_perf_fd(&perf_field
->attr
);
255 if (thread_field
->fd
>= 0)
256 thread_field
->pc
= setup_perf(thread_field
);
258 * Note: thread_field->pc can be NULL if setup_perf() fails.
259 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
262 cds_list_add_rcu(&thread_field
->rcu_field_node
,
263 &perf_thread
->rcu_field_list
);
264 cds_list_add(&thread_field
->thread_field_node
,
265 &perf_field
->thread_field_list
);
268 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
275 struct lttng_perf_counter_thread_field
*
276 get_thread_field(struct lttng_perf_counter_field
*field
)
278 struct lttng_perf_counter_thread
*perf_thread
;
279 struct lttng_perf_counter_thread_field
*thread_field
;
281 perf_thread
= pthread_getspecific(perf_counter_key
);
283 perf_thread
= alloc_perf_counter_thread();
284 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
286 if (thread_field
->field
== field
)
289 /* perf_counter_thread_field not found, need to add one */
290 return add_thread_field(field
, perf_thread
);
294 uint64_t wrapper_perf_counter_read(struct lttng_ctx_field
*field
)
296 struct lttng_perf_counter_field
*perf_field
;
297 struct lttng_perf_counter_thread_field
*perf_thread_field
;
299 perf_field
= field
->u
.perf_counter
;
300 perf_thread_field
= get_thread_field(perf_field
);
301 return read_perf_counter(perf_thread_field
->pc
);
305 void perf_counter_record(struct lttng_ctx_field
*field
,
306 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
307 struct lttng_channel
*chan
)
311 value
= wrapper_perf_counter_read(field
);
312 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
313 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
317 void perf_counter_get_value(struct lttng_ctx_field
*field
,
318 struct lttng_ctx_value
*value
)
322 v
= wrapper_perf_counter_read(field
);
326 /* Called with UST lock held */
328 void lttng_destroy_perf_thread_field(
329 struct lttng_perf_counter_thread_field
*thread_field
)
331 close_perf_fd(thread_field
->fd
);
332 unmap_perf_page(thread_field
->pc
);
333 cds_list_del_rcu(&thread_field
->rcu_field_node
);
334 cds_list_del(&thread_field
->thread_field_node
);
339 void lttng_destroy_perf_thread_key(void *_key
)
341 struct lttng_perf_counter_thread
*perf_thread
= _key
;
342 struct lttng_perf_counter_thread_field
*pos
, *p
;
345 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
347 lttng_destroy_perf_thread_field(pos
);
352 /* Called with UST lock held */
354 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
356 struct lttng_perf_counter_field
*perf_field
;
357 struct lttng_perf_counter_thread_field
*pos
, *p
;
359 free((char *) field
->event_field
.name
);
360 perf_field
= field
->u
.perf_counter
;
362 * This put is performed when no threads can concurrently
363 * perform a "get" concurrently, thanks to urcu-bp grace
366 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
368 lttng_destroy_perf_thread_field(pos
);
372 /* Called with UST lock held */
373 int lttng_add_perf_counter_to_ctx(uint32_t type
,
376 struct lttng_ctx
**ctx
)
378 struct lttng_ctx_field
*field
;
379 struct lttng_perf_counter_field
*perf_field
;
383 name_alloc
= strdup(name
);
386 goto name_alloc_error
;
388 perf_field
= zmalloc(sizeof(*perf_field
));
391 goto perf_field_alloc_error
;
393 field
= lttng_append_context(ctx
);
396 goto append_context_error
;
398 if (lttng_find_context(*ctx
, name_alloc
)) {
403 field
->destroy
= lttng_destroy_perf_counter_field
;
405 field
->event_field
.name
= name_alloc
;
406 field
->event_field
.type
.atype
= atype_integer
;
407 field
->event_field
.type
.u
.basic
.integer
.size
=
408 sizeof(uint64_t) * CHAR_BIT
;
409 field
->event_field
.type
.u
.basic
.integer
.alignment
=
410 lttng_alignof(uint64_t) * CHAR_BIT
;
411 field
->event_field
.type
.u
.basic
.integer
.signedness
=
412 lttng_is_signed_type(uint64_t);
413 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
414 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
415 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
416 field
->get_size
= perf_counter_get_size
;
417 field
->record
= perf_counter_record
;
418 field
->get_value
= perf_counter_get_value
;
420 perf_field
->attr
.type
= type
;
421 perf_field
->attr
.config
= config
;
422 perf_field
->attr
.exclude_kernel
= 1;
423 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
424 field
->u
.perf_counter
= perf_field
;
426 /* Ensure that this perf counter can be used in this process. */
427 ret
= open_perf_fd(&perf_field
->attr
);
435 * Contexts can only be added before tracing is started, so we
436 * don't have to synchronize against concurrent threads using
440 lttng_context_update(*ctx
);
445 lttng_remove_context_field(ctx
, field
);
446 append_context_error
:
448 perf_field_alloc_error
:
454 int lttng_perf_counter_init(void)
458 ret
= pthread_key_create(&perf_counter_key
,
459 lttng_destroy_perf_thread_key
);
465 void lttng_perf_counter_exit(void)
469 ret
= pthread_key_delete(perf_counter_key
);
472 PERROR("Error in pthread_key_delete");