cbd3bd265a2db2eda39d507d0bc56fd5b626ef95
[lttng-ust.git] / liblttng-ust / lttng-context-vtid.c
1 /*
2 * lttng-context-vtid.c
3 *
4 * LTTng UST vtid context.
5 *
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
21 */
22
23 #define _LGPL_SOURCE
24 #include <stddef.h>
25 #include <sys/types.h>
26 #include <unistd.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"
33
34 /*
35 * We cache the result to ensure we don't trigger a system call for
36 * each event.
37 */
38 static DEFINE_URCU_TLS(pid_t, cached_vtid);
39
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 {
47 CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0);
48 }
49
50 static
51 size_t vtid_get_size(struct lttng_ctx_field *field, size_t offset)
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
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)) {
67 vtid = lttng_gettid();
68 CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid);
69 }
70 return vtid;
71 }
72
73 static
74 void vtid_record(struct lttng_ctx_field *field,
75 struct lttng_ust_lib_ring_buffer_ctx *ctx,
76 struct lttng_channel *chan)
77 {
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));
82 }
83
84 static
85 void vtid_get_value(struct lttng_ctx_field *field,
86 struct lttng_ctx_value *value)
87 {
88 value->u.s64 = wrapper_getvtid();
89 }
90
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;
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);
114 return 0;
115 }
116
117 /*
118 * Force a read (imply TLS fixup for dlopen) of TLS variables.
119 */
120 void lttng_fixup_vtid_tls(void)
121 {
122 asm volatile ("" : : "m" (URCU_TLS(cached_vtid)));
123 }
This page took 0.031309 seconds and 3 git commands to generate.