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 |
3b402b40 MD |
24 | #include <sys/types.h> |
25 | #include <unistd.h> | |
4318ae1b MD |
26 | #include <lttng/ust-events.h> |
27 | #include <lttng/ust-tracer.h> | |
28 | #include <lttng/ringbuffer-config.h> | |
49c0da7d | 29 | #include <lttng/ust-tid.h> |
8c90a710 | 30 | #include <urcu/tls-compat.h> |
7dd08bec | 31 | #include "lttng-tracer-core.h" |
aab17bfe | 32 | |
3b402b40 MD |
33 | /* |
34 | * We cache the result to ensure we don't trigger a system call for | |
35 | * each event. | |
36 | */ | |
16adecf1 | 37 | static DEFINE_URCU_TLS(pid_t, cached_vtid); |
3b402b40 | 38 | |
a93bfc45 MD |
39 | /* |
40 | * Upon fork or clone, the TID assigned to our thread is not the same as | |
41 | * we kept in cache. Luckily, we are the only thread surviving in the | |
42 | * child process, so we can simply clear our cached version. | |
43 | */ | |
44 | void lttng_context_vtid_reset(void) | |
45 | { | |
98357ffd | 46 | CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0); |
a93bfc45 MD |
47 | } |
48 | ||
3b402b40 | 49 | static |
53569322 | 50 | size_t vtid_get_size(struct lttng_ctx_field *field, size_t offset) |
3b402b40 MD |
51 | { |
52 | size_t size = 0; | |
53 | ||
54 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); | |
55 | size += sizeof(pid_t); | |
56 | return size; | |
57 | } | |
58 | ||
98357ffd MD |
59 | static inline |
60 | pid_t wrapper_getvtid(void) | |
61 | { | |
62 | pid_t vtid; | |
63 | ||
64 | vtid = CMM_LOAD_SHARED(URCU_TLS(cached_vtid)); | |
65 | if (caa_unlikely(!vtid)) { | |
0f029713 | 66 | vtid = lttng_gettid(); |
98357ffd MD |
67 | CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid); |
68 | } | |
69 | return vtid; | |
70 | } | |
71 | ||
3b402b40 MD |
72 | static |
73 | void vtid_record(struct lttng_ctx_field *field, | |
4cfec15c | 74 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
7dd08bec | 75 | struct lttng_channel *chan) |
3b402b40 | 76 | { |
98357ffd MD |
77 | pid_t vtid = wrapper_getvtid(); |
78 | ||
79 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vtid)); | |
80 | chan->ops->event_write(ctx, &vtid, sizeof(vtid)); | |
3b402b40 MD |
81 | } |
82 | ||
77aa5901 MD |
83 | static |
84 | void vtid_get_value(struct lttng_ctx_field *field, | |
53569322 | 85 | struct lttng_ctx_value *value) |
77aa5901 | 86 | { |
98357ffd | 87 | value->u.s64 = wrapper_getvtid(); |
77aa5901 MD |
88 | } |
89 | ||
3b402b40 MD |
90 | int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx) |
91 | { | |
92 | struct lttng_ctx_field *field; | |
93 | ||
94 | field = lttng_append_context(ctx); | |
95 | if (!field) | |
96 | return -ENOMEM; | |
97 | if (lttng_find_context(*ctx, "vtid")) { | |
98 | lttng_remove_context_field(ctx, field); | |
99 | return -EEXIST; | |
100 | } | |
101 | field->event_field.name = "vtid"; | |
102 | field->event_field.type.atype = atype_integer; | |
103 | field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT; | |
104 | field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT; | |
105 | field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t); | |
106 | field->event_field.type.u.basic.integer.reverse_byte_order = 0; | |
107 | field->event_field.type.u.basic.integer.base = 10; | |
108 | field->event_field.type.u.basic.integer.encoding = lttng_encode_none; | |
109 | field->get_size = vtid_get_size; | |
110 | field->record = vtid_record; | |
77aa5901 | 111 | field->get_value = vtid_get_value; |
b2cc986a | 112 | lttng_context_update(*ctx); |
3b402b40 MD |
113 | return 0; |
114 | } | |
4158a15a MD |
115 | |
116 | /* | |
117 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
118 | */ | |
119 | void lttng_fixup_vtid_tls(void) | |
120 | { | |
8c90a710 | 121 | asm volatile ("" : : "m" (URCU_TLS(cached_vtid))); |
4158a15a | 122 | } |