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/ringbuffer-context.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(struct lttng_ust_ctx_field
*field
, size_t offset
)
44 size
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(pid_t
));
45 size
+= sizeof(pid_t
);
50 pid_t
wrapper_getvtid(void)
54 vtid
= CMM_LOAD_SHARED(URCU_TLS(cached_vtid
));
55 if (caa_unlikely(!vtid
)) {
56 vtid
= lttng_gettid();
57 CMM_STORE_SHARED(URCU_TLS(cached_vtid
), vtid
);
63 void vtid_record(struct lttng_ust_ctx_field
*field
,
64 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
65 struct lttng_ust_channel_buffer
*chan
)
67 pid_t vtid
= wrapper_getvtid();
69 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_alignof(vtid
));
70 chan
->ops
->event_write(ctx
, &vtid
, sizeof(vtid
));
74 void vtid_get_value(struct lttng_ust_ctx_field
*field
,
75 struct lttng_ust_ctx_value
*value
)
77 value
->u
.s64
= wrapper_getvtid();
80 int lttng_add_vtid_to_ctx(struct lttng_ust_ctx
**ctx
)
82 struct lttng_ust_ctx_field
*field
;
83 struct lttng_ust_type_common
*type
;
86 type
= lttng_ust_create_type_integer(sizeof(pid_t
) * CHAR_BIT
,
87 lttng_alignof(pid_t
) * CHAR_BIT
,
88 lttng_ust_is_signed_type(pid_t
),
92 field
= lttng_append_context(ctx
);
97 if (lttng_find_context(*ctx
, "vtid")) {
99 goto error_find_context
;
101 field
->event_field
->name
= strdup("vtid");
102 if (!field
->event_field
->name
) {
106 field
->event_field
->type
= type
;
107 field
->get_size
= vtid_get_size
;
108 field
->record
= vtid_record
;
109 field
->get_value
= vtid_get_value
;
110 lttng_context_update(*ctx
);
115 lttng_remove_context_field(ctx
, field
);
117 lttng_ust_destroy_type(type
);
122 * Force a read (imply TLS fixup for dlopen) of TLS variables.
124 void lttng_fixup_vtid_tls(void)
126 asm volatile ("" : : "m" (URCU_TLS(cached_vtid
)));