2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng UST vtid context.
12 #include <sys/types.h>
14 #include <lttng/ust-events.h>
15 #include <lttng/ust-tracer.h>
16 #include <lttng/ust-ringbuffer-context.h>
17 #include "common/compat/tid.h"
18 #include <urcu/tls-compat.h>
20 #include "context-internal.h"
21 #include "lttng-tracer-core.h"
24 * We cache the result to ensure we don't trigger a system call for
27 static DEFINE_URCU_TLS(pid_t
, cached_vtid
);
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.
34 void lttng_context_vtid_reset(void)
36 CMM_STORE_SHARED(URCU_TLS(cached_vtid
), 0);
40 size_t vtid_get_size(void *priv
__attribute__((unused
)),
41 struct lttng_ust_probe_ctx
*probe_ctx
__attribute__((unused
)),
46 size
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(pid_t
));
47 size
+= sizeof(pid_t
);
52 pid_t
wrapper_getvtid(void)
56 vtid
= CMM_LOAD_SHARED(URCU_TLS(cached_vtid
));
57 if (caa_unlikely(!vtid
)) {
58 vtid
= lttng_gettid();
59 CMM_STORE_SHARED(URCU_TLS(cached_vtid
), vtid
);
65 void vtid_record(void *priv
__attribute__((unused
)),
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
)
70 pid_t vtid
= wrapper_getvtid();
72 chan
->ops
->event_write(ctx
, &vtid
, sizeof(vtid
), lttng_ust_rb_alignof(vtid
));
76 void vtid_get_value(void *priv
__attribute__((unused
)),
77 struct lttng_ust_probe_ctx
*probe_ctx
__attribute__((unused
)),
78 struct lttng_ust_ctx_value
*value
)
80 value
->u
.s64
= wrapper_getvtid();
83 static 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
),
88 LTTNG_UST_BYTE_ORDER
, 10),
95 int lttng_add_vtid_to_ctx(struct lttng_ust_ctx
**ctx
)
99 if (lttng_find_context(*ctx
, ctx_field
->event_field
->name
)) {
101 goto error_find_context
;
103 ret
= lttng_ust_context_append(ctx
, ctx_field
);
113 * Force a read (imply TLS allocation for dlopen) of TLS variables.
115 void lttng_vtid_alloc_tls(void)
117 __asm__
__volatile__ ("" : : "m" (URCU_TLS(cached_vtid
)));