lttng_ust_init_thread: initialise cached context values
[lttng-ust.git] / src / lib / lttng-ust / lttng-context-vtid.c
CommitLineData
3b402b40 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
3b402b40 3 *
e92f3e28
MD
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
c0c0989a 6 * LTTng UST vtid context.
3b402b40
MD
7 */
8
3fbec7dc 9#define _LGPL_SOURCE
9af5d97a 10#include <limits.h>
b4051ad8 11#include <stddef.h>
3b402b40
MD
12#include <sys/types.h>
13#include <unistd.h>
4318ae1b
MD
14#include <lttng/ust-events.h>
15#include <lttng/ust-tracer.h>
0b4b8811 16#include <lttng/ust-ringbuffer-context.h>
9d315d6d 17#include "common/compat/tid.h"
8c90a710 18#include <urcu/tls-compat.h>
fc80554e
MJ
19
20#include "context-internal.h"
7dd08bec 21#include "lttng-tracer-core.h"
aab17bfe 22
3b402b40
MD
23/*
24 * We cache the result to ensure we don't trigger a system call for
25 * each event.
26 */
16adecf1 27static DEFINE_URCU_TLS(pid_t, cached_vtid);
3b402b40 28
a93bfc45
MD
29/*
30 * Upon fork or clone, the TID assigned to our thread is not the same as
31 * we kept in cache. Luckily, we are the only thread surviving in the
32 * child process, so we can simply clear our cached version.
33 */
34void lttng_context_vtid_reset(void)
35{
98357ffd 36 CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0);
a93bfc45
MD
37}
38
3b402b40 39static
4e48b5d2 40size_t vtid_get_size(void *priv __attribute__((unused)),
b2e37d27 41 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
2208d8b5 42 size_t offset)
3b402b40
MD
43{
44 size_t size = 0;
45
b5457df5 46 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(pid_t));
3b402b40
MD
47 size += sizeof(pid_t);
48 return size;
49}
50
98357ffd
MD
51static inline
52pid_t wrapper_getvtid(void)
53{
54 pid_t vtid;
55
56 vtid = CMM_LOAD_SHARED(URCU_TLS(cached_vtid));
57 if (caa_unlikely(!vtid)) {
0f029713 58 vtid = lttng_gettid();
98357ffd
MD
59 CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid);
60 }
61 return vtid;
62}
63
3b402b40 64static
4e48b5d2 65void vtid_record(void *priv __attribute__((unused)),
b2e37d27
MD
66 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
67 struct lttng_ust_ring_buffer_ctx *ctx,
68 struct lttng_ust_channel_buffer *chan)
3b402b40 69{
98357ffd
MD
70 pid_t vtid = wrapper_getvtid();
71
8936b6c0 72 chan->ops->event_write(ctx, &vtid, sizeof(vtid), lttng_ust_rb_alignof(vtid));
3b402b40
MD
73}
74
77aa5901 75static
4e48b5d2 76void vtid_get_value(void *priv __attribute__((unused)),
b2e37d27 77 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
daacdbfc 78 struct lttng_ust_ctx_value *value)
77aa5901 79{
98357ffd 80 value->u.s64 = wrapper_getvtid();
77aa5901
MD
81}
82
4e48b5d2
MD
83static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
84 lttng_ust_static_event_field("vtid",
85 lttng_ust_static_type_integer(sizeof(pid_t) * CHAR_BIT,
86 lttng_ust_rb_alignof(pid_t) * CHAR_BIT,
87 lttng_ust_is_signed_type(pid_t),
baa8acf3 88 LTTNG_UST_BYTE_ORDER, 10),
4e48b5d2
MD
89 false, false),
90 vtid_get_size,
91 vtid_record,
92 vtid_get_value,
93 NULL, NULL);
94
daacdbfc 95int lttng_add_vtid_to_ctx(struct lttng_ust_ctx **ctx)
3b402b40 96{
a084756d 97 int ret;
3b402b40 98
4e48b5d2 99 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
a084756d
MD
100 ret = -EEXIST;
101 goto error_find_context;
3b402b40 102 }
4e48b5d2
MD
103 ret = lttng_ust_context_append(ctx, ctx_field);
104 if (ret)
105 return ret;
3b402b40 106 return 0;
a084756d 107
a084756d 108error_find_context:
a084756d 109 return ret;
3b402b40 110}
4158a15a
MD
111
112/*
a9fd951a 113 * Force a read (imply TLS allocation for dlopen) of TLS variables.
4158a15a 114 */
c246521d 115void lttng_ust_vtid_init_thread(int flags)
4158a15a 116{
8c90a710 117 asm volatile ("" : : "m" (URCU_TLS(cached_vtid)));
c246521d
NL
118 if (flags & LTTNG_UST_INIT_THREAD_CONTEXT_CACHE)
119 (void)wrapper_getvtid();
4158a15a 120}
This page took 0.044612 seconds and 4 git commands to generate.