Commit | Line | Data |
---|---|---|
3b402b40 | 1 | /* |
e92f3e28 | 2 | * lttng-context-vtid.c |
3b402b40 MD |
3 | * |
4 | * LTTng UST vtid context. | |
5 | * | |
e92f3e28 MD |
6 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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 | |
3b402b40 MD |
21 | */ |
22 | ||
3fbec7dc | 23 | #define _LGPL_SOURCE |
b4051ad8 | 24 | #include <stddef.h> |
3b402b40 MD |
25 | #include <sys/types.h> |
26 | #include <unistd.h> | |
4318ae1b MD |
27 | #include <lttng/ust-events.h> |
28 | #include <lttng/ust-tracer.h> | |
29 | #include <lttng/ringbuffer-config.h> | |
49c0da7d | 30 | #include <lttng/ust-tid.h> |
8c90a710 | 31 | #include <urcu/tls-compat.h> |
7dd08bec | 32 | #include "lttng-tracer-core.h" |
aab17bfe | 33 | |
3b402b40 MD |
34 | /* |
35 | * We cache the result to ensure we don't trigger a system call for | |
36 | * each event. | |
37 | */ | |
16adecf1 | 38 | static DEFINE_URCU_TLS(pid_t, cached_vtid); |
3b402b40 | 39 | |
a93bfc45 MD |
40 | /* |
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. | |
44 | */ | |
45 | void lttng_context_vtid_reset(void) | |
46 | { | |
98357ffd | 47 | CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0); |
a93bfc45 MD |
48 | } |
49 | ||
3b402b40 | 50 | static |
53569322 | 51 | size_t vtid_get_size(struct lttng_ctx_field *field, size_t offset) |
3b402b40 MD |
52 | { |
53 | size_t size = 0; | |
54 | ||
55 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); | |
56 | size += sizeof(pid_t); | |
57 | return size; | |
58 | } | |
59 | ||
98357ffd MD |
60 | static inline |
61 | pid_t wrapper_getvtid(void) | |
62 | { | |
63 | pid_t vtid; | |
64 | ||
65 | vtid = CMM_LOAD_SHARED(URCU_TLS(cached_vtid)); | |
66 | if (caa_unlikely(!vtid)) { | |
0f029713 | 67 | vtid = lttng_gettid(); |
98357ffd MD |
68 | CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid); |
69 | } | |
70 | return vtid; | |
71 | } | |
72 | ||
3b402b40 MD |
73 | static |
74 | void vtid_record(struct lttng_ctx_field *field, | |
4cfec15c | 75 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
7dd08bec | 76 | struct lttng_channel *chan) |
3b402b40 | 77 | { |
98357ffd MD |
78 | pid_t vtid = wrapper_getvtid(); |
79 | ||
80 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vtid)); | |
81 | chan->ops->event_write(ctx, &vtid, sizeof(vtid)); | |
3b402b40 MD |
82 | } |
83 | ||
77aa5901 MD |
84 | static |
85 | void vtid_get_value(struct lttng_ctx_field *field, | |
53569322 | 86 | struct lttng_ctx_value *value) |
77aa5901 | 87 | { |
98357ffd | 88 | value->u.s64 = wrapper_getvtid(); |
77aa5901 MD |
89 | } |
90 | ||
3b402b40 MD |
91 | int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx) |
92 | { | |
93 | struct lttng_ctx_field *field; | |
94 | ||
95 | field = lttng_append_context(ctx); | |
96 | if (!field) | |
97 | return -ENOMEM; | |
98 | if (lttng_find_context(*ctx, "vtid")) { | |
99 | lttng_remove_context_field(ctx, field); | |
100 | return -EEXIST; | |
101 | } | |
102 | field->event_field.name = "vtid"; | |
103 | field->event_field.type.atype = atype_integer; | |
218deb69 MD |
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; | |
3b402b40 MD |
110 | field->get_size = vtid_get_size; |
111 | field->record = vtid_record; | |
77aa5901 | 112 | field->get_value = vtid_get_value; |
b2cc986a | 113 | lttng_context_update(*ctx); |
3b402b40 MD |
114 | return 0; |
115 | } | |
4158a15a MD |
116 | |
117 | /* | |
118 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
119 | */ | |
120 | void lttng_fixup_vtid_tls(void) | |
121 | { | |
8c90a710 | 122 | asm volatile ("" : : "m" (URCU_TLS(cached_vtid))); |
4158a15a | 123 | } |