Introduce LTTNG_UST_MAP_POPULATE_POLICY environment variable
[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 <sys/types.h>
25 #include <unistd.h>
26 #include <lttng/ust-events.h>
27 #include <lttng/ust-tracer.h>
28 #include <lttng/ringbuffer-config.h>
29 #include <lttng/ust-tid.h>
30 #include <urcu/tls-compat.h>
31 #include "lttng-tracer-core.h"
32
33 /*
34 * We cache the result to ensure we don't trigger a system call for
35 * each event.
36 */
37 static DEFINE_URCU_TLS(pid_t, cached_vtid);
38
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 {
46 CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0);
47 }
48
49 static
50 size_t vtid_get_size(struct lttng_ctx_field *field, size_t offset)
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
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)) {
66 vtid = lttng_gettid();
67 CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid);
68 }
69 return vtid;
70 }
71
72 static
73 void vtid_record(struct lttng_ctx_field *field,
74 struct lttng_ust_lib_ring_buffer_ctx *ctx,
75 struct lttng_channel *chan)
76 {
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));
81 }
82
83 static
84 void vtid_get_value(struct lttng_ctx_field *field,
85 struct lttng_ctx_value *value)
86 {
87 value->u.s64 = wrapper_getvtid();
88 }
89
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;
111 field->get_value = vtid_get_value;
112 lttng_context_update(*ctx);
113 return 0;
114 }
115
116 /*
117 * Force a read (imply TLS fixup for dlopen) of TLS variables.
118 */
119 void lttng_fixup_vtid_tls(void)
120 {
121 asm volatile ("" : : "m" (URCU_TLS(cached_vtid)));
122 }
This page took 0.030765 seconds and 4 git commands to generate.