cleanup: rename template headers
[lttng-ust.git] / liblttng-ust / lttng-context-perf-counters.c
CommitLineData
d58d1454 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
d58d1454
MD
3 *
4 * Copyright (C) 2009-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
c0c0989a 6 * LTTng UST performance monitoring counters (perf-counters) integration.
d58d1454
MD
7 */
8
3fbec7dc 9#define _LGPL_SOURCE
9af5d97a 10#include <limits.h>
d58d1454
MD
11#include <sys/types.h>
12#include <unistd.h>
13#include <string.h>
14#include <stdlib.h>
15#include <stdio.h>
649fb6b3 16#include <stdbool.h>
b4051ad8 17#include <stddef.h>
fb31eb73 18#include <stdint.h>
d58d1454
MD
19#include <sys/mman.h>
20#include <sys/syscall.h>
2eba8e39 21#include <lttng/ust-arch.h>
d58d1454
MD
22#include <lttng/ust-events.h>
23#include <lttng/ust-tracer.h>
0466ac28 24#include <lttng/ringbuffer-context.h>
d58d1454
MD
25#include <urcu/system.h>
26#include <urcu/arch.h>
27#include <urcu/rculist.h>
864a1eda 28#include <ust-helper.h>
d58d1454
MD
29#include <urcu/ref.h>
30#include <usterr-signal-safe.h>
31#include <signal.h>
20142124 32#include <urcu/tls-compat.h>
77d7fa98 33#include "perf_event.h"
fc80554e
MJ
34
35#include "context-internal.h"
d58d1454 36#include "lttng-tracer-core.h"
bd8c1787 37#include "ust-events-internal.h"
d58d1454
MD
38
39/*
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.
44 *
45 * Updates and traversals of thread_list are protected by UST lock.
46 * Updates to rcu_field_list are protected by UST lock.
47 */
48
49struct 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) */
b9389e6e 54 int fd; /* Perf FD */
d58d1454
MD
55};
56
57struct lttng_perf_counter_thread {
58 struct cds_list_head rcu_field_list; /* RCU per-thread list of fields */
59};
60
61struct lttng_perf_counter_field {
62 struct perf_event_attr attr;
63 struct cds_list_head thread_field_list; /* Per-field list of thread fields */
64};
65
66static pthread_key_t perf_counter_key;
67
20142124
MD
68/*
69 * lttng_perf_lock - Protect lttng-ust perf counter data structures
70 *
71 * Nests within the ust_lock, and therefore within the libc dl lock.
72 * Therefore, we need to fixup the TLS before nesting into this lock.
73 * Nests inside RCU bp read-side lock. Protects against concurrent
74 * fork.
75 */
76static pthread_mutex_t ust_perf_mutex = PTHREAD_MUTEX_INITIALIZER;
77
78/*
79 * Cancel state when grabbing the ust_perf_mutex. Saved when locking,
80 * restored on unlock. Protected by ust_perf_mutex.
81 */
82static int ust_perf_saved_cancelstate;
83
84/*
85 * Track whether we are tracing from a signal handler nested on an
86 * application thread.
87 */
88static DEFINE_URCU_TLS(int, ust_perf_mutex_nest);
89
90/*
91 * Force a read (imply TLS fixup for dlopen) of TLS variables.
92 */
93void lttng_ust_fixup_perf_counter_tls(void)
94{
95 asm volatile ("" : : "m" (URCU_TLS(ust_perf_mutex_nest)));
96}
97
98void lttng_perf_lock(void)
99{
100 sigset_t sig_all_blocked, orig_mask;
101 int ret, oldstate;
102
103 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
104 if (ret) {
105 ERR("pthread_setcancelstate: %s", strerror(ret));
106 }
107 sigfillset(&sig_all_blocked);
108 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
109 if (ret) {
110 ERR("pthread_sigmask: %s", strerror(ret));
111 }
112 if (!URCU_TLS(ust_perf_mutex_nest)++) {
113 /*
114 * Ensure the compiler don't move the store after the close()
115 * call in case close() would be marked as leaf.
116 */
117 cmm_barrier();
118 pthread_mutex_lock(&ust_perf_mutex);
119 ust_perf_saved_cancelstate = oldstate;
120 }
121 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
122 if (ret) {
123 ERR("pthread_sigmask: %s", strerror(ret));
124 }
125}
126
127void lttng_perf_unlock(void)
128{
129 sigset_t sig_all_blocked, orig_mask;
130 int ret, newstate, oldstate;
131 bool restore_cancel = false;
132
133 sigfillset(&sig_all_blocked);
134 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
135 if (ret) {
136 ERR("pthread_sigmask: %s", strerror(ret));
137 }
138 /*
139 * Ensure the compiler don't move the store before the close()
140 * call, in case close() would be marked as leaf.
141 */
142 cmm_barrier();
143 if (!--URCU_TLS(ust_perf_mutex_nest)) {
144 newstate = ust_perf_saved_cancelstate;
145 restore_cancel = true;
146 pthread_mutex_unlock(&ust_perf_mutex);
147 }
148 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
149 if (ret) {
150 ERR("pthread_sigmask: %s", strerror(ret));
151 }
152 if (restore_cancel) {
153 ret = pthread_setcancelstate(newstate, &oldstate);
154 if (ret) {
155 ERR("pthread_setcancelstate: %s", strerror(ret));
156 }
157 }
158}
159
d58d1454 160static
daacdbfc 161size_t perf_counter_get_size(struct lttng_ust_ctx_field *field, size_t offset)
d58d1454
MD
162{
163 size_t size = 0;
164
dc325c1d 165 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uint64_t));
d58d1454
MD
166 size += sizeof(uint64_t);
167 return size;
168}
169
a3a8d943
MD
170static
171uint64_t read_perf_counter_syscall(
172 struct lttng_perf_counter_thread_field *thread_field)
173{
174 uint64_t count;
175
176 if (caa_unlikely(thread_field->fd < 0))
177 return 0;
178
179 if (caa_unlikely(read(thread_field->fd, &count, sizeof(count))
180 < sizeof(count)))
181 return 0;
182
183 return count;
184}
185
2eba8e39 186#if defined(LTTNG_UST_ARCH_X86)
d58d1454
MD
187
188static
189uint64_t rdpmc(unsigned int counter)
190{
191 unsigned int low, high;
192
193 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
194
195 return low | ((uint64_t) high) << 32;
196}
197
77d7fa98
MD
198static
199bool has_rdpmc(struct perf_event_mmap_page *pc)
200{
201 if (caa_unlikely(!pc->cap_bit0_is_deprecated))
202 return false;
203 /* Since Linux kernel 3.12. */
204 return pc->cap_user_rdpmc;
205}
206
d58d1454 207static
a3a8d943 208uint64_t arch_read_perf_counter(
d286ad50 209 struct lttng_perf_counter_thread_field *thread_field)
d58d1454
MD
210{
211 uint32_t seq, idx;
212 uint64_t count;
d286ad50 213 struct perf_event_mmap_page *pc = thread_field->pc;
d58d1454
MD
214
215 if (caa_unlikely(!pc))
216 return 0;
217
218 do {
219 seq = CMM_LOAD_SHARED(pc->lock);
220 cmm_barrier();
221
222 idx = pc->index;
77d7fa98 223 if (caa_likely(has_rdpmc(pc) && idx)) {
4f58f54f
MD
224 int64_t pmcval;
225
226 pmcval = rdpmc(idx - 1);
227 /* Sign-extend the pmc register result. */
228 pmcval <<= 64 - pc->pmc_width;
229 pmcval >>= 64 - pc->pmc_width;
230 count = pc->offset + pmcval;
231 } else {
a3a8d943
MD
232 /* Fall-back on system call if rdpmc cannot be used. */
233 return read_perf_counter_syscall(thread_field);
4f58f54f 234 }
d58d1454
MD
235 cmm_barrier();
236 } while (CMM_LOAD_SHARED(pc->lock) != seq);
237
238 return count;
239}
240
d286ad50 241static
a3a8d943 242int arch_perf_keep_fd(struct lttng_perf_counter_thread_field *thread_field)
d286ad50 243{
a3a8d943 244 struct perf_event_mmap_page *pc = thread_field->pc;
d286ad50 245
a3a8d943 246 if (!pc)
d286ad50 247 return 0;
77d7fa98 248 return !has_rdpmc(pc);
a3a8d943 249}
d286ad50 250
a3a8d943 251#else
d286ad50 252
a3a8d943
MD
253/* Generic (slow) implementation using a read system call. */
254static
255uint64_t arch_read_perf_counter(
256 struct lttng_perf_counter_thread_field *thread_field)
257{
258 return read_perf_counter_syscall(thread_field);
d286ad50
JD
259}
260
a3a8d943
MD
261static
262int arch_perf_keep_fd(struct lttng_perf_counter_thread_field *thread_field)
263{
264 return 1;
265}
d286ad50 266
a3a8d943 267#endif
d286ad50 268
d58d1454
MD
269static
270int sys_perf_event_open(struct perf_event_attr *attr,
271 pid_t pid, int cpu, int group_fd,
272 unsigned long flags)
273{
274 return syscall(SYS_perf_event_open, attr, pid, cpu,
275 group_fd, flags);
276}
277
278static
b9389e6e 279int open_perf_fd(struct perf_event_attr *attr)
d58d1454 280{
b9389e6e 281 int fd;
d58d1454
MD
282
283 fd = sys_perf_event_open(attr, 0, -1, -1, 0);
284 if (fd < 0)
b9389e6e
JD
285 return -1;
286
287 return fd;
288}
289
d286ad50
JD
290static
291void close_perf_fd(int fd)
292{
293 int ret;
294
295 if (fd < 0)
296 return;
297
298 ret = close(fd);
299 if (ret) {
300 perror("Error closing LTTng-UST perf memory mapping FD");
301 }
302}
303
77d7fa98 304static void setup_perf(struct lttng_perf_counter_thread_field *thread_field)
b9389e6e
JD
305{
306 void *perf_addr;
d58d1454
MD
307
308 perf_addr = mmap(NULL, sizeof(struct perf_event_mmap_page),
b9389e6e 309 PROT_READ, MAP_SHARED, thread_field->fd, 0);
d58d1454 310 if (perf_addr == MAP_FAILED)
b9389e6e 311 perf_addr = NULL;
77d7fa98 312 thread_field->pc = perf_addr;
b9389e6e 313
a3a8d943 314 if (!arch_perf_keep_fd(thread_field)) {
b9389e6e
JD
315 close_perf_fd(thread_field->fd);
316 thread_field->fd = -1;
6c2125af 317 }
d58d1454
MD
318}
319
320static
321void unmap_perf_page(struct perf_event_mmap_page *pc)
322{
323 int ret;
324
325 if (!pc)
326 return;
327 ret = munmap(pc, sizeof(struct perf_event_mmap_page));
328 if (ret < 0) {
329 PERROR("Error in munmap");
330 abort();
331 }
332}
333
334static
335struct lttng_perf_counter_thread *alloc_perf_counter_thread(void)
336{
337 struct lttng_perf_counter_thread *perf_thread;
338 sigset_t newmask, oldmask;
339 int ret;
340
341 ret = sigfillset(&newmask);
342 if (ret)
343 abort();
344 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
345 if (ret)
346 abort();
347 /* Check again with signals disabled */
348 perf_thread = pthread_getspecific(perf_counter_key);
349 if (perf_thread)
350 goto skip;
351 perf_thread = zmalloc(sizeof(*perf_thread));
352 if (!perf_thread)
353 abort();
354 CDS_INIT_LIST_HEAD(&perf_thread->rcu_field_list);
355 ret = pthread_setspecific(perf_counter_key, perf_thread);
356 if (ret)
357 abort();
358skip:
359 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
360 if (ret)
361 abort();
362 return perf_thread;
363}
364
365static
366struct lttng_perf_counter_thread_field *
367 add_thread_field(struct lttng_perf_counter_field *perf_field,
368 struct lttng_perf_counter_thread *perf_thread)
369{
370 struct lttng_perf_counter_thread_field *thread_field;
371 sigset_t newmask, oldmask;
372 int ret;
373
374 ret = sigfillset(&newmask);
375 if (ret)
376 abort();
377 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
378 if (ret)
379 abort();
380 /* Check again with signals disabled */
381 cds_list_for_each_entry_rcu(thread_field, &perf_thread->rcu_field_list,
382 rcu_field_node) {
383 if (thread_field->field == perf_field)
384 goto skip;
385 }
386 thread_field = zmalloc(sizeof(*thread_field));
387 if (!thread_field)
388 abort();
389 thread_field->field = perf_field;
b9389e6e
JD
390 thread_field->fd = open_perf_fd(&perf_field->attr);
391 if (thread_field->fd >= 0)
77d7fa98 392 setup_perf(thread_field);
b9389e6e
JD
393 /*
394 * Note: thread_field->pc can be NULL if setup_perf() fails.
395 * Also, thread_field->fd can be -1 if open_perf_fd() fails.
396 */
20142124 397 lttng_perf_lock();
d58d1454
MD
398 cds_list_add_rcu(&thread_field->rcu_field_node,
399 &perf_thread->rcu_field_list);
400 cds_list_add(&thread_field->thread_field_node,
401 &perf_field->thread_field_list);
20142124 402 lttng_perf_unlock();
d58d1454
MD
403skip:
404 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
405 if (ret)
406 abort();
407 return thread_field;
408}
409
410static
411struct lttng_perf_counter_thread_field *
412 get_thread_field(struct lttng_perf_counter_field *field)
413{
414 struct lttng_perf_counter_thread *perf_thread;
415 struct lttng_perf_counter_thread_field *thread_field;
416
417 perf_thread = pthread_getspecific(perf_counter_key);
418 if (!perf_thread)
419 perf_thread = alloc_perf_counter_thread();
420 cds_list_for_each_entry_rcu(thread_field, &perf_thread->rcu_field_list,
421 rcu_field_node) {
422 if (thread_field->field == field)
423 return thread_field;
424 }
425 /* perf_counter_thread_field not found, need to add one */
426 return add_thread_field(field, perf_thread);
427}
428
429static
daacdbfc 430uint64_t wrapper_perf_counter_read(struct lttng_ust_ctx_field *field)
d58d1454
MD
431{
432 struct lttng_perf_counter_field *perf_field;
433 struct lttng_perf_counter_thread_field *perf_thread_field;
434
daacdbfc 435 perf_field = (struct lttng_perf_counter_field *) field->priv;
d58d1454 436 perf_thread_field = get_thread_field(perf_field);
a3a8d943 437 return arch_read_perf_counter(perf_thread_field);
d58d1454
MD
438}
439
440static
daacdbfc 441void perf_counter_record(struct lttng_ust_ctx_field *field,
d58d1454 442 struct lttng_ust_lib_ring_buffer_ctx *ctx,
e7bc0ef6 443 struct lttng_ust_channel_buffer *chan)
d58d1454
MD
444{
445 uint64_t value;
446
447 value = wrapper_perf_counter_read(field);
8936b6c0 448 chan->ops->event_write(ctx, &value, sizeof(value), lttng_ust_rb_alignof(value));
d58d1454
MD
449}
450
451static
daacdbfc
MD
452void perf_counter_get_value(struct lttng_ust_ctx_field *field,
453 struct lttng_ust_ctx_value *value)
d58d1454 454{
6e9ac4ae 455 value->u.s64 = wrapper_perf_counter_read(field);
d58d1454
MD
456}
457
20142124 458/* Called with perf lock held */
d58d1454
MD
459static
460void lttng_destroy_perf_thread_field(
461 struct lttng_perf_counter_thread_field *thread_field)
462{
b9389e6e 463 close_perf_fd(thread_field->fd);
d58d1454
MD
464 unmap_perf_page(thread_field->pc);
465 cds_list_del_rcu(&thread_field->rcu_field_node);
466 cds_list_del(&thread_field->thread_field_node);
467 free(thread_field);
468}
469
470static
471void lttng_destroy_perf_thread_key(void *_key)
472{
473 struct lttng_perf_counter_thread *perf_thread = _key;
474 struct lttng_perf_counter_thread_field *pos, *p;
475
20142124 476 lttng_perf_lock();
d58d1454
MD
477 cds_list_for_each_entry_safe(pos, p, &perf_thread->rcu_field_list,
478 rcu_field_node)
479 lttng_destroy_perf_thread_field(pos);
20142124 480 lttng_perf_unlock();
d58d1454
MD
481 free(perf_thread);
482}
483
484/* Called with UST lock held */
485static
daacdbfc 486void lttng_destroy_perf_counter_field(struct lttng_ust_ctx_field *field)
d58d1454
MD
487{
488 struct lttng_perf_counter_field *perf_field;
489 struct lttng_perf_counter_thread_field *pos, *p;
490
daacdbfc
MD
491 free((char *) field->event_field->name);
492 perf_field = (struct lttng_perf_counter_field *) field->priv;
d58d1454
MD
493 /*
494 * This put is performed when no threads can concurrently
495 * perform a "get" concurrently, thanks to urcu-bp grace
20142124
MD
496 * period. Holding the lttng perf lock protects against
497 * concurrent modification of the per-thread thread field
498 * list.
d58d1454 499 */
20142124 500 lttng_perf_lock();
d58d1454
MD
501 cds_list_for_each_entry_safe(pos, p, &perf_field->thread_field_list,
502 thread_field_node)
503 lttng_destroy_perf_thread_field(pos);
20142124 504 lttng_perf_unlock();
d58d1454
MD
505 free(perf_field);
506}
507
2eba8e39 508#ifdef LTTNG_UST_ARCH_ARMV7
d286ad50
JD
509
510static
511int perf_get_exclude_kernel(void)
512{
513 return 0;
514}
515
2eba8e39 516#else /* LTTNG_UST_ARCH_ARMV7 */
d286ad50
JD
517
518static
519int perf_get_exclude_kernel(void)
520{
521 return 1;
522}
523
2eba8e39 524#endif /* LTTNG_UST_ARCH_ARMV7 */
d286ad50 525
d58d1454
MD
526/* Called with UST lock held */
527int lttng_add_perf_counter_to_ctx(uint32_t type,
528 uint64_t config,
529 const char *name,
daacdbfc 530 struct lttng_ust_ctx **ctx)
d58d1454 531{
daacdbfc 532 struct lttng_ust_ctx_field *field;
a084756d 533 struct lttng_ust_type_common *ust_type;
d58d1454 534 struct lttng_perf_counter_field *perf_field;
d58d1454
MD
535 char *name_alloc;
536 int ret;
537
538 name_alloc = strdup(name);
539 if (!name_alloc) {
540 ret = -ENOMEM;
541 goto name_alloc_error;
542 }
543 perf_field = zmalloc(sizeof(*perf_field));
544 if (!perf_field) {
545 ret = -ENOMEM;
546 goto perf_field_alloc_error;
547 }
a084756d 548 ust_type = lttng_ust_create_type_integer(sizeof(uint64_t) * CHAR_BIT,
dc325c1d 549 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT,
eae3c729 550 lttng_ust_is_signed_type(uint64_t),
a084756d 551 BYTE_ORDER, 10);
ea8efbbe 552 if (!ust_type) {
a084756d
MD
553 ret = -ENOMEM;
554 goto type_alloc_error;
555 }
d58d1454
MD
556 field = lttng_append_context(ctx);
557 if (!field) {
558 ret = -ENOMEM;
559 goto append_context_error;
560 }
561 if (lttng_find_context(*ctx, name_alloc)) {
562 ret = -EEXIST;
563 goto find_error;
564 }
565
566 field->destroy = lttng_destroy_perf_counter_field;
567
daacdbfc 568 field->event_field->name = name_alloc;
a084756d 569 field->event_field->type = ust_type;
d58d1454
MD
570 field->get_size = perf_counter_get_size;
571 field->record = perf_counter_record;
572 field->get_value = perf_counter_get_value;
573
574 perf_field->attr.type = type;
575 perf_field->attr.config = config;
d286ad50 576 perf_field->attr.exclude_kernel = perf_get_exclude_kernel();
d58d1454 577 CDS_INIT_LIST_HEAD(&perf_field->thread_field_list);
daacdbfc 578 field->priv = perf_field;
d58d1454
MD
579
580 /* Ensure that this perf counter can be used in this process. */
b9389e6e
JD
581 ret = open_perf_fd(&perf_field->attr);
582 if (ret < 0) {
d58d1454
MD
583 ret = -ENODEV;
584 goto setup_error;
585 }
b9389e6e 586 close_perf_fd(ret);
d58d1454
MD
587
588 /*
589 * Contexts can only be added before tracing is started, so we
590 * don't have to synchronize against concurrent threads using
591 * the field here.
592 */
593
b2cc986a 594 lttng_context_update(*ctx);
d58d1454
MD
595 return 0;
596
597setup_error:
598find_error:
599 lttng_remove_context_field(ctx, field);
600append_context_error:
a084756d
MD
601 lttng_ust_destroy_type(ust_type);
602type_alloc_error:
d58d1454
MD
603 free(perf_field);
604perf_field_alloc_error:
605 free(name_alloc);
606name_alloc_error:
607 return ret;
608}
609
610int lttng_perf_counter_init(void)
611{
612 int ret;
613
614 ret = pthread_key_create(&perf_counter_key,
615 lttng_destroy_perf_thread_key);
616 if (ret)
617 ret = -ret;
618 return ret;
619}
620
621void lttng_perf_counter_exit(void)
622{
623 int ret;
624
625 ret = pthread_key_delete(perf_counter_key);
626 if (ret) {
627 errno = ret;
628 PERROR("Error in pthread_key_delete");
629 }
630}
This page took 0.057842 seconds and 4 git commands to generate.