Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / lttng-context-vtid.c
CommitLineData
3b402b40 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
3b402b40 3 *
e92f3e28
MD
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
c0c0989a 6 * LTTng UST vtid context.
3b402b40
MD
7 */
8
3fbec7dc 9#define _LGPL_SOURCE
b4051ad8 10#include <stddef.h>
3b402b40
MD
11#include <sys/types.h>
12#include <unistd.h>
4318ae1b
MD
13#include <lttng/ust-events.h>
14#include <lttng/ust-tracer.h>
15#include <lttng/ringbuffer-config.h>
49c0da7d 16#include <lttng/ust-tid.h>
8c90a710 17#include <urcu/tls-compat.h>
7dd08bec 18#include "lttng-tracer-core.h"
aab17bfe 19
3b402b40
MD
20/*
21 * We cache the result to ensure we don't trigger a system call for
22 * each event.
23 */
16adecf1 24static DEFINE_URCU_TLS(pid_t, cached_vtid);
3b402b40 25
a93bfc45
MD
26/*
27 * Upon fork or clone, the TID assigned to our thread is not the same as
28 * we kept in cache. Luckily, we are the only thread surviving in the
29 * child process, so we can simply clear our cached version.
30 */
31void lttng_context_vtid_reset(void)
32{
98357ffd 33 CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0);
a93bfc45
MD
34}
35
3b402b40 36static
53569322 37size_t vtid_get_size(struct lttng_ctx_field *field, size_t offset)
3b402b40
MD
38{
39 size_t size = 0;
40
41 size += lib_ring_buffer_align(offset, lttng_alignof(pid_t));
42 size += sizeof(pid_t);
43 return size;
44}
45
98357ffd
MD
46static inline
47pid_t wrapper_getvtid(void)
48{
49 pid_t vtid;
50
51 vtid = CMM_LOAD_SHARED(URCU_TLS(cached_vtid));
52 if (caa_unlikely(!vtid)) {
0f029713 53 vtid = lttng_gettid();
98357ffd
MD
54 CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid);
55 }
56 return vtid;
57}
58
3b402b40
MD
59static
60void vtid_record(struct lttng_ctx_field *field,
4cfec15c 61 struct lttng_ust_lib_ring_buffer_ctx *ctx,
7dd08bec 62 struct lttng_channel *chan)
3b402b40 63{
98357ffd
MD
64 pid_t vtid = wrapper_getvtid();
65
66 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vtid));
67 chan->ops->event_write(ctx, &vtid, sizeof(vtid));
3b402b40
MD
68}
69
77aa5901
MD
70static
71void vtid_get_value(struct lttng_ctx_field *field,
53569322 72 struct lttng_ctx_value *value)
77aa5901 73{
98357ffd 74 value->u.s64 = wrapper_getvtid();
77aa5901
MD
75}
76
3b402b40
MD
77int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx)
78{
79 struct lttng_ctx_field *field;
80
81 field = lttng_append_context(ctx);
82 if (!field)
83 return -ENOMEM;
84 if (lttng_find_context(*ctx, "vtid")) {
85 lttng_remove_context_field(ctx, field);
86 return -EEXIST;
87 }
88 field->event_field.name = "vtid";
89 field->event_field.type.atype = atype_integer;
218deb69
MD
90 field->event_field.type.u.integer.size = sizeof(pid_t) * CHAR_BIT;
91 field->event_field.type.u.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT;
92 field->event_field.type.u.integer.signedness = lttng_is_signed_type(pid_t);
93 field->event_field.type.u.integer.reverse_byte_order = 0;
94 field->event_field.type.u.integer.base = 10;
95 field->event_field.type.u.integer.encoding = lttng_encode_none;
3b402b40
MD
96 field->get_size = vtid_get_size;
97 field->record = vtid_record;
77aa5901 98 field->get_value = vtid_get_value;
b2cc986a 99 lttng_context_update(*ctx);
3b402b40
MD
100 return 0;
101}
4158a15a
MD
102
103/*
104 * Force a read (imply TLS fixup for dlopen) of TLS variables.
105 */
106void lttng_fixup_vtid_tls(void)
107{
8c90a710 108 asm volatile ("" : : "m" (URCU_TLS(cached_vtid)));
4158a15a 109}
This page took 0.035398 seconds and 4 git commands to generate.