Compat layer for gettid
[lttng-ust.git] / liblttng-ust / lttng-context-vtid.c
CommitLineData
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
23#include <sys/types.h>
24#include <unistd.h>
4318ae1b
MD
25#include <lttng/ust-events.h>
26#include <lttng/ust-tracer.h>
27#include <lttng/ringbuffer-config.h>
4158a15a 28#include "ltt-tracer-core.h"
d9ecffa9 29#include <lttng/ust-tid.h>
aab17bfe
MD
30
31#ifdef __linux__
32#include <syscall.h>
33#endif
34
35#if defined(_syscall0)
36_syscall0(pid_t, gettid)
37#elif defined(__NR_gettid)
38static inline pid_t gettid(void)
39{
40 return syscall(__NR_gettid);
41}
42#else
43#warning "use pid as tid"
44static inline pid_t gettid(void)
45{
46 return getpid();
47}
48#endif
d9ecffa9 49======= end
3b402b40
MD
50
51/*
52 * We cache the result to ensure we don't trigger a system call for
53 * each event.
54 */
55static __thread pid_t cached_vtid;
56
a93bfc45
MD
57/*
58 * Upon fork or clone, the TID assigned to our thread is not the same as
59 * we kept in cache. Luckily, we are the only thread surviving in the
60 * child process, so we can simply clear our cached version.
61 */
62void lttng_context_vtid_reset(void)
63{
64 cached_vtid = 0;
65}
66
3b402b40
MD
67static
68size_t vtid_get_size(size_t offset)
69{
70 size_t size = 0;
71
72 size += lib_ring_buffer_align(offset, lttng_alignof(pid_t));
73 size += sizeof(pid_t);
74 return size;
75}
76
77static
78void vtid_record(struct lttng_ctx_field *field,
4cfec15c 79 struct lttng_ust_lib_ring_buffer_ctx *ctx,
3b402b40
MD
80 struct ltt_channel *chan)
81{
b5a3dfa5 82 if (caa_unlikely(!cached_vtid))
3b402b40
MD
83 cached_vtid = gettid();
84 lib_ring_buffer_align_ctx(ctx, lttng_alignof(cached_vtid));
85 chan->ops->event_write(ctx, &cached_vtid, sizeof(cached_vtid));
86}
87
88int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx)
89{
90 struct lttng_ctx_field *field;
91
92 field = lttng_append_context(ctx);
93 if (!field)
94 return -ENOMEM;
95 if (lttng_find_context(*ctx, "vtid")) {
96 lttng_remove_context_field(ctx, field);
97 return -EEXIST;
98 }
99 field->event_field.name = "vtid";
100 field->event_field.type.atype = atype_integer;
101 field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT;
102 field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT;
103 field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t);
104 field->event_field.type.u.basic.integer.reverse_byte_order = 0;
105 field->event_field.type.u.basic.integer.base = 10;
106 field->event_field.type.u.basic.integer.encoding = lttng_encode_none;
107 field->get_size = vtid_get_size;
108 field->record = vtid_record;
109 return 0;
110}
4158a15a
MD
111
112/*
113 * Force a read (imply TLS fixup for dlopen) of TLS variables.
114 */
115void lttng_fixup_vtid_tls(void)
116{
117 asm volatile ("" : : "m" (cached_vtid));
118}
This page took 0.028271 seconds and 4 git commands to generate.