Rename struct lttng_ust_channel_ops to struct lttng_ust_channel_buffer_ops
[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>
0466ac28 15#include <lttng/ringbuffer-context.h>
ae4b659d 16#include <ust-tid.h>
8c90a710 17#include <urcu/tls-compat.h>
fc80554e
MJ
18
19#include "context-internal.h"
7dd08bec 20#include "lttng-tracer-core.h"
aab17bfe 21
3b402b40
MD
22/*
23 * We cache the result to ensure we don't trigger a system call for
24 * each event.
25 */
16adecf1 26static DEFINE_URCU_TLS(pid_t, cached_vtid);
3b402b40 27
a93bfc45
MD
28/*
29 * Upon fork or clone, the TID assigned to our thread is not the same as
30 * we kept in cache. Luckily, we are the only thread surviving in the
31 * child process, so we can simply clear our cached version.
32 */
33void lttng_context_vtid_reset(void)
34{
98357ffd 35 CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0);
a93bfc45
MD
36}
37
3b402b40 38static
daacdbfc 39size_t vtid_get_size(struct lttng_ust_ctx_field *field, size_t offset)
3b402b40
MD
40{
41 size_t size = 0;
42
43 size += lib_ring_buffer_align(offset, lttng_alignof(pid_t));
44 size += sizeof(pid_t);
45 return size;
46}
47
98357ffd
MD
48static inline
49pid_t wrapper_getvtid(void)
50{
51 pid_t vtid;
52
53 vtid = CMM_LOAD_SHARED(URCU_TLS(cached_vtid));
54 if (caa_unlikely(!vtid)) {
0f029713 55 vtid = lttng_gettid();
98357ffd
MD
56 CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid);
57 }
58 return vtid;
59}
60
3b402b40 61static
daacdbfc 62void vtid_record(struct lttng_ust_ctx_field *field,
4cfec15c 63 struct lttng_ust_lib_ring_buffer_ctx *ctx,
e7bc0ef6 64 struct lttng_ust_channel_buffer *chan)
3b402b40 65{
98357ffd
MD
66 pid_t vtid = wrapper_getvtid();
67
68 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vtid));
69 chan->ops->event_write(ctx, &vtid, sizeof(vtid));
3b402b40
MD
70}
71
77aa5901 72static
daacdbfc
MD
73void vtid_get_value(struct lttng_ust_ctx_field *field,
74 struct lttng_ust_ctx_value *value)
77aa5901 75{
98357ffd 76 value->u.s64 = wrapper_getvtid();
77aa5901
MD
77}
78
daacdbfc 79int lttng_add_vtid_to_ctx(struct lttng_ust_ctx **ctx)
3b402b40 80{
daacdbfc 81 struct lttng_ust_ctx_field *field;
a084756d
MD
82 struct lttng_ust_type_common *type;
83 int ret;
3b402b40 84
a084756d
MD
85 type = lttng_ust_create_type_integer(sizeof(pid_t) * CHAR_BIT,
86 lttng_alignof(pid_t) * CHAR_BIT,
87 lttng_is_signed_type(pid_t),
88 BYTE_ORDER, 10);
89 if (!type)
3b402b40 90 return -ENOMEM;
a084756d
MD
91 field = lttng_append_context(ctx);
92 if (!field) {
93 ret = -ENOMEM;
94 goto error_context;
95 }
3b402b40 96 if (lttng_find_context(*ctx, "vtid")) {
a084756d
MD
97 ret = -EEXIST;
98 goto error_find_context;
3b402b40 99 }
a084756d
MD
100 field->event_field->name = strdup("vtid");
101 if (!field->event_field->name) {
102 ret = -ENOMEM;
103 goto error_name;
104 }
105 field->event_field->type = type;
3b402b40
MD
106 field->get_size = vtid_get_size;
107 field->record = vtid_record;
77aa5901 108 field->get_value = vtid_get_value;
b2cc986a 109 lttng_context_update(*ctx);
3b402b40 110 return 0;
a084756d
MD
111
112error_name:
113error_find_context:
114 lttng_remove_context_field(ctx, field);
115error_context:
116 lttng_ust_destroy_type(type);
117 return ret;
3b402b40 118}
4158a15a
MD
119
120/*
121 * Force a read (imply TLS fixup for dlopen) of TLS variables.
122 */
123void lttng_fixup_vtid_tls(void)
124{
8c90a710 125 asm volatile ("" : : "m" (URCU_TLS(cached_vtid)));
4158a15a 126}
This page took 0.0386 seconds and 4 git commands to generate.