4 * LTTng UST vtid context.
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
27 #include <lttng/ust-events.h>
28 #include <lttng/ust-tracer.h>
29 #include <lttng/ringbuffer-config.h>
30 #include <lttng/ust-tid.h>
31 #include <urcu/tls-compat.h>
32 #include "lttng-tracer-core.h"
35 * We cache the result to ensure we don't trigger a system call for
38 static DEFINE_URCU_TLS(pid_t
, cached_vtid
);
41 * Upon fork or clone, the TID assigned to our thread is not the same as
42 * we kept in cache. Luckily, we are the only thread surviving in the
43 * child process, so we can simply clear our cached version.
45 void lttng_context_vtid_reset(void)
47 CMM_STORE_SHARED(URCU_TLS(cached_vtid
), 0);
51 size_t vtid_get_size(struct lttng_ctx_field
*field
, size_t offset
)
55 size
+= lib_ring_buffer_align(offset
, lttng_alignof(pid_t
));
56 size
+= sizeof(pid_t
);
61 pid_t
wrapper_getvtid(void)
65 vtid
= CMM_LOAD_SHARED(URCU_TLS(cached_vtid
));
66 if (caa_unlikely(!vtid
)) {
67 vtid
= lttng_gettid();
68 CMM_STORE_SHARED(URCU_TLS(cached_vtid
), vtid
);
74 void vtid_record(struct lttng_ctx_field
*field
,
75 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
76 struct lttng_channel
*chan
)
78 pid_t vtid
= wrapper_getvtid();
80 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(vtid
));
81 chan
->ops
->event_write(ctx
, &vtid
, sizeof(vtid
));
85 void vtid_get_value(struct lttng_ctx_field
*field
,
86 struct lttng_ctx_value
*value
)
88 value
->u
.s64
= wrapper_getvtid();
91 int lttng_add_vtid_to_ctx(struct lttng_ctx
**ctx
)
93 struct lttng_ctx_field
*field
;
95 field
= lttng_append_context(ctx
);
98 if (lttng_find_context(*ctx
, "vtid")) {
99 lttng_remove_context_field(ctx
, field
);
102 field
->event_field
.name
= "vtid";
103 field
->event_field
.type
.atype
= atype_integer
;
104 field
->event_field
.type
.u
.integer
.size
= sizeof(pid_t
) * CHAR_BIT
;
105 field
->event_field
.type
.u
.integer
.alignment
= lttng_alignof(pid_t
) * CHAR_BIT
;
106 field
->event_field
.type
.u
.integer
.signedness
= lttng_is_signed_type(pid_t
);
107 field
->event_field
.type
.u
.integer
.reverse_byte_order
= 0;
108 field
->event_field
.type
.u
.integer
.base
= 10;
109 field
->event_field
.type
.u
.integer
.encoding
= lttng_encode_none
;
110 field
->get_size
= vtid_get_size
;
111 field
->record
= vtid_record
;
112 field
->get_value
= vtid_get_value
;
113 lttng_context_update(*ctx
);
118 * Force a read (imply TLS fixup for dlopen) of TLS variables.
120 void lttng_fixup_vtid_tls(void)
122 asm volatile ("" : : "m" (URCU_TLS(cached_vtid
)));