Commit | Line | Data |
---|---|---|
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> | |
0466ac28 | 16 | #include <lttng/ringbuffer-context.h> |
ae4b659d | 17 | #include <ust-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 | 27 | static 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 | */ | |
34 | void lttng_context_vtid_reset(void) | |
35 | { | |
98357ffd | 36 | CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0); |
a93bfc45 MD |
37 | } |
38 | ||
3b402b40 | 39 | static |
daacdbfc | 40 | size_t vtid_get_size(struct lttng_ust_ctx_field *field, size_t offset) |
3b402b40 MD |
41 | { |
42 | size_t size = 0; | |
43 | ||
44 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); | |
45 | size += sizeof(pid_t); | |
46 | return size; | |
47 | } | |
48 | ||
98357ffd MD |
49 | static inline |
50 | pid_t wrapper_getvtid(void) | |
51 | { | |
52 | pid_t vtid; | |
53 | ||
54 | vtid = CMM_LOAD_SHARED(URCU_TLS(cached_vtid)); | |
55 | if (caa_unlikely(!vtid)) { | |
0f029713 | 56 | vtid = lttng_gettid(); |
98357ffd MD |
57 | CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid); |
58 | } | |
59 | return vtid; | |
60 | } | |
61 | ||
3b402b40 | 62 | static |
daacdbfc | 63 | void vtid_record(struct lttng_ust_ctx_field *field, |
4cfec15c | 64 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
e7bc0ef6 | 65 | struct lttng_ust_channel_buffer *chan) |
3b402b40 | 66 | { |
98357ffd MD |
67 | pid_t vtid = wrapper_getvtid(); |
68 | ||
69 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vtid)); | |
70 | chan->ops->event_write(ctx, &vtid, sizeof(vtid)); | |
3b402b40 MD |
71 | } |
72 | ||
77aa5901 | 73 | static |
daacdbfc MD |
74 | void vtid_get_value(struct lttng_ust_ctx_field *field, |
75 | struct lttng_ust_ctx_value *value) | |
77aa5901 | 76 | { |
98357ffd | 77 | value->u.s64 = wrapper_getvtid(); |
77aa5901 MD |
78 | } |
79 | ||
daacdbfc | 80 | int lttng_add_vtid_to_ctx(struct lttng_ust_ctx **ctx) |
3b402b40 | 81 | { |
daacdbfc | 82 | struct lttng_ust_ctx_field *field; |
a084756d MD |
83 | struct lttng_ust_type_common *type; |
84 | int ret; | |
3b402b40 | 85 | |
a084756d MD |
86 | type = lttng_ust_create_type_integer(sizeof(pid_t) * CHAR_BIT, |
87 | lttng_alignof(pid_t) * CHAR_BIT, | |
eae3c729 | 88 | lttng_ust_is_signed_type(pid_t), |
a084756d MD |
89 | BYTE_ORDER, 10); |
90 | if (!type) | |
3b402b40 | 91 | return -ENOMEM; |
a084756d MD |
92 | field = lttng_append_context(ctx); |
93 | if (!field) { | |
94 | ret = -ENOMEM; | |
95 | goto error_context; | |
96 | } | |
3b402b40 | 97 | if (lttng_find_context(*ctx, "vtid")) { |
a084756d MD |
98 | ret = -EEXIST; |
99 | goto error_find_context; | |
3b402b40 | 100 | } |
a084756d MD |
101 | field->event_field->name = strdup("vtid"); |
102 | if (!field->event_field->name) { | |
103 | ret = -ENOMEM; | |
104 | goto error_name; | |
105 | } | |
106 | field->event_field->type = type; | |
3b402b40 MD |
107 | field->get_size = vtid_get_size; |
108 | field->record = vtid_record; | |
77aa5901 | 109 | field->get_value = vtid_get_value; |
b2cc986a | 110 | lttng_context_update(*ctx); |
3b402b40 | 111 | return 0; |
a084756d MD |
112 | |
113 | error_name: | |
114 | error_find_context: | |
115 | lttng_remove_context_field(ctx, field); | |
116 | error_context: | |
117 | lttng_ust_destroy_type(type); | |
118 | return ret; | |
3b402b40 | 119 | } |
4158a15a MD |
120 | |
121 | /* | |
122 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
123 | */ | |
124 | void lttng_fixup_vtid_tls(void) | |
125 | { | |
8c90a710 | 126 | asm volatile ("" : : "m" (URCU_TLS(cached_vtid))); |
4158a15a | 127 | } |