2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng UST vtid context.
11 #include <sys/types.h>
13 #include <lttng/ust-events.h>
14 #include <lttng/ust-tracer.h>
15 #include <lttng/ringbuffer-config.h>
17 #include <urcu/tls-compat.h>
18 #include "lttng-tracer-core.h"
21 * We cache the result to ensure we don't trigger a system call for
24 static DEFINE_URCU_TLS(pid_t
, cached_vtid
);
27 * Upon fork or clone, the TID assigned to our thread is not the same as
28 * we kept in cache. Luckily, we are the only thread surviving in the
29 * child process, so we can simply clear our cached version.
31 void lttng_context_vtid_reset(void)
33 CMM_STORE_SHARED(URCU_TLS(cached_vtid
), 0);
37 size_t vtid_get_size(struct lttng_ctx_field
*field
, size_t offset
)
41 size
+= lib_ring_buffer_align(offset
, lttng_alignof(pid_t
));
42 size
+= sizeof(pid_t
);
47 pid_t
wrapper_getvtid(void)
51 vtid
= CMM_LOAD_SHARED(URCU_TLS(cached_vtid
));
52 if (caa_unlikely(!vtid
)) {
53 vtid
= lttng_gettid();
54 CMM_STORE_SHARED(URCU_TLS(cached_vtid
), vtid
);
60 void vtid_record(struct lttng_ctx_field
*field
,
61 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
62 struct lttng_channel
*chan
)
64 pid_t vtid
= wrapper_getvtid();
66 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(vtid
));
67 chan
->ops
->event_write(ctx
, &vtid
, sizeof(vtid
));
71 void vtid_get_value(struct lttng_ctx_field
*field
,
72 struct lttng_ctx_value
*value
)
74 value
->u
.s64
= wrapper_getvtid();
77 int lttng_add_vtid_to_ctx(struct lttng_ctx
**ctx
)
79 struct lttng_ctx_field
*field
;
81 field
= lttng_append_context(ctx
);
84 if (lttng_find_context(*ctx
, "vtid")) {
85 lttng_remove_context_field(ctx
, field
);
88 field
->event_field
.name
= "vtid";
89 field
->event_field
.type
.atype
= atype_integer
;
90 field
->event_field
.type
.u
.integer
.size
= sizeof(pid_t
) * CHAR_BIT
;
91 field
->event_field
.type
.u
.integer
.alignment
= lttng_alignof(pid_t
) * CHAR_BIT
;
92 field
->event_field
.type
.u
.integer
.signedness
= lttng_is_signed_type(pid_t
);
93 field
->event_field
.type
.u
.integer
.reverse_byte_order
= 0;
94 field
->event_field
.type
.u
.integer
.base
= 10;
95 field
->event_field
.type
.u
.integer
.encoding
= lttng_encode_none
;
96 field
->get_size
= vtid_get_size
;
97 field
->record
= vtid_record
;
98 field
->get_value
= vtid_get_value
;
99 lttng_context_update(*ctx
);
104 * Force a read (imply TLS fixup for dlopen) of TLS variables.
106 void lttng_fixup_vtid_tls(void)
108 asm volatile ("" : : "m" (URCU_TLS(cached_vtid
)));