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) */
60 struct lttng_perf_counter_thread
{
61 struct cds_list_head rcu_field_list
; /* RCU per-thread list of fields */
64 struct lttng_perf_counter_field
{
65 struct perf_event_attr attr
;
66 struct cds_list_head thread_field_list
; /* Per-field list of thread fields */
69 static pthread_key_t perf_counter_key
;
72 size_t perf_counter_get_size(struct lttng_ctx_field
*field
, size_t offset
)
76 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint64_t));
77 size
+= sizeof(uint64_t);
81 #if defined(__x86_64__) || defined(__i386__)
84 uint64_t rdpmc(unsigned int counter
)
86 unsigned int low
, high
;
88 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
90 return low
| ((uint64_t) high
) << 32;
93 #else /* defined(__x86_64__) || defined(__i386__) */
95 #error "Perf event counters are only supported on x86 so far."
97 #endif /* #else defined(__x86_64__) || defined(__i386__) */
100 uint64_t read_perf_counter(struct perf_event_mmap_page
*pc
)
105 if (caa_unlikely(!pc
))
109 seq
= CMM_LOAD_SHARED(pc
->lock
);
116 pmcval
= rdpmc(idx
- 1);
117 /* Sign-extend the pmc register result. */
118 pmcval
<<= 64 - pc
->pmc_width
;
119 pmcval
>>= 64 - pc
->pmc_width
;
120 count
= pc
->offset
+ pmcval
;
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 struct perf_event_mmap_page
*setup_perf(struct perf_event_attr
*attr
)
145 fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
149 perf_addr
= mmap(NULL
, sizeof(struct perf_event_mmap_page
),
150 PROT_READ
, MAP_SHARED
, fd
, 0);
151 if (perf_addr
== MAP_FAILED
)
155 perror("Error closing LTTng-UST perf memory mapping FD");
161 void unmap_perf_page(struct perf_event_mmap_page
*pc
)
167 ret
= munmap(pc
, sizeof(struct perf_event_mmap_page
));
169 PERROR("Error in munmap");
175 struct lttng_perf_counter_thread
*alloc_perf_counter_thread(void)
177 struct lttng_perf_counter_thread
*perf_thread
;
178 sigset_t newmask
, oldmask
;
181 ret
= sigfillset(&newmask
);
184 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
187 /* Check again with signals disabled */
188 perf_thread
= pthread_getspecific(perf_counter_key
);
191 perf_thread
= zmalloc(sizeof(*perf_thread
));
194 CDS_INIT_LIST_HEAD(&perf_thread
->rcu_field_list
);
195 ret
= pthread_setspecific(perf_counter_key
, perf_thread
);
199 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
206 struct lttng_perf_counter_thread_field
*
207 add_thread_field(struct lttng_perf_counter_field
*perf_field
,
208 struct lttng_perf_counter_thread
*perf_thread
)
210 struct lttng_perf_counter_thread_field
*thread_field
;
211 sigset_t newmask
, oldmask
;
214 ret
= sigfillset(&newmask
);
217 ret
= pthread_sigmask(SIG_BLOCK
, &newmask
, &oldmask
);
220 /* Check again with signals disabled */
221 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
223 if (thread_field
->field
== perf_field
)
226 thread_field
= zmalloc(sizeof(*thread_field
));
229 thread_field
->field
= perf_field
;
230 thread_field
->pc
= setup_perf(&perf_field
->attr
);
231 /* Note: thread_field->pc can be NULL if setup_perf() fails. */
233 cds_list_add_rcu(&thread_field
->rcu_field_node
,
234 &perf_thread
->rcu_field_list
);
235 cds_list_add(&thread_field
->thread_field_node
,
236 &perf_field
->thread_field_list
);
239 ret
= pthread_sigmask(SIG_SETMASK
, &oldmask
, NULL
);
246 struct lttng_perf_counter_thread_field
*
247 get_thread_field(struct lttng_perf_counter_field
*field
)
249 struct lttng_perf_counter_thread
*perf_thread
;
250 struct lttng_perf_counter_thread_field
*thread_field
;
252 perf_thread
= pthread_getspecific(perf_counter_key
);
254 perf_thread
= alloc_perf_counter_thread();
255 cds_list_for_each_entry_rcu(thread_field
, &perf_thread
->rcu_field_list
,
257 if (thread_field
->field
== field
)
260 /* perf_counter_thread_field not found, need to add one */
261 return add_thread_field(field
, perf_thread
);
265 uint64_t wrapper_perf_counter_read(struct lttng_ctx_field
*field
)
267 struct lttng_perf_counter_field
*perf_field
;
268 struct lttng_perf_counter_thread_field
*perf_thread_field
;
270 perf_field
= field
->u
.perf_counter
;
271 perf_thread_field
= get_thread_field(perf_field
);
272 return read_perf_counter(perf_thread_field
->pc
);
276 void perf_counter_record(struct lttng_ctx_field
*field
,
277 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
278 struct lttng_channel
*chan
)
282 value
= wrapper_perf_counter_read(field
);
283 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(value
));
284 chan
->ops
->event_write(ctx
, &value
, sizeof(value
));
288 void perf_counter_get_value(struct lttng_ctx_field
*field
,
289 struct lttng_ctx_value
*value
)
293 v
= wrapper_perf_counter_read(field
);
297 /* Called with UST lock held */
299 void lttng_destroy_perf_thread_field(
300 struct lttng_perf_counter_thread_field
*thread_field
)
302 unmap_perf_page(thread_field
->pc
);
303 cds_list_del_rcu(&thread_field
->rcu_field_node
);
304 cds_list_del(&thread_field
->thread_field_node
);
309 void lttng_destroy_perf_thread_key(void *_key
)
311 struct lttng_perf_counter_thread
*perf_thread
= _key
;
312 struct lttng_perf_counter_thread_field
*pos
, *p
;
315 cds_list_for_each_entry_safe(pos
, p
, &perf_thread
->rcu_field_list
,
317 lttng_destroy_perf_thread_field(pos
);
322 /* Called with UST lock held */
324 void lttng_destroy_perf_counter_field(struct lttng_ctx_field
*field
)
326 struct lttng_perf_counter_field
*perf_field
;
327 struct lttng_perf_counter_thread_field
*pos
, *p
;
329 free((char *) field
->event_field
.name
);
330 perf_field
= field
->u
.perf_counter
;
332 * This put is performed when no threads can concurrently
333 * perform a "get" concurrently, thanks to urcu-bp grace
336 cds_list_for_each_entry_safe(pos
, p
, &perf_field
->thread_field_list
,
338 lttng_destroy_perf_thread_field(pos
);
342 /* Called with UST lock held */
343 int lttng_add_perf_counter_to_ctx(uint32_t type
,
346 struct lttng_ctx
**ctx
)
348 struct lttng_ctx_field
*field
;
349 struct lttng_perf_counter_field
*perf_field
;
350 struct perf_event_mmap_page
*tmp_pc
;
354 name_alloc
= strdup(name
);
357 goto name_alloc_error
;
359 perf_field
= zmalloc(sizeof(*perf_field
));
362 goto perf_field_alloc_error
;
364 field
= lttng_append_context(ctx
);
367 goto append_context_error
;
369 if (lttng_find_context(*ctx
, name_alloc
)) {
374 field
->destroy
= lttng_destroy_perf_counter_field
;
376 field
->event_field
.name
= name_alloc
;
377 field
->event_field
.type
.atype
= atype_integer
;
378 field
->event_field
.type
.u
.basic
.integer
.size
=
379 sizeof(uint64_t) * CHAR_BIT
;
380 field
->event_field
.type
.u
.basic
.integer
.alignment
=
381 lttng_alignof(uint64_t) * CHAR_BIT
;
382 field
->event_field
.type
.u
.basic
.integer
.signedness
=
383 lttng_is_signed_type(uint64_t);
384 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
385 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
386 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
387 field
->get_size
= perf_counter_get_size
;
388 field
->record
= perf_counter_record
;
389 field
->get_value
= perf_counter_get_value
;
391 perf_field
->attr
.type
= type
;
392 perf_field
->attr
.config
= config
;
393 perf_field
->attr
.exclude_kernel
= 1;
394 CDS_INIT_LIST_HEAD(&perf_field
->thread_field_list
);
395 field
->u
.perf_counter
= perf_field
;
397 /* Ensure that this perf counter can be used in this process. */
398 tmp_pc
= setup_perf(&perf_field
->attr
);
403 unmap_perf_page(tmp_pc
);
406 * Contexts can only be added before tracing is started, so we
407 * don't have to synchronize against concurrent threads using
411 lttng_context_update(*ctx
);
416 lttng_remove_context_field(ctx
, field
);
417 append_context_error
:
419 perf_field_alloc_error
:
425 int lttng_perf_counter_init(void)
429 ret
= pthread_key_create(&perf_counter_key
,
430 lttng_destroy_perf_thread_key
);
436 void lttng_perf_counter_exit(void)
440 ret
= pthread_key_delete(perf_counter_key
);
443 PERROR("Error in pthread_key_delete");