Refactoring: context structures
[lttng-ust.git] / liblttng-ust / lttng-context-vtid.c
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng UST vtid context.
7 */
8
9#define _LGPL_SOURCE
10#include <stddef.h>
11#include <sys/types.h>
12#include <unistd.h>
13#include <lttng/ust-events.h>
14#include <lttng/ust-tracer.h>
15#include <lttng/ringbuffer-config.h>
16#include <ust-tid.h>
17#include <urcu/tls-compat.h>
18
19#include "context-internal.h"
20#include "lttng-tracer-core.h"
21
22/*
23 * We cache the result to ensure we don't trigger a system call for
24 * each event.
25 */
26static DEFINE_URCU_TLS(pid_t, cached_vtid);
27
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{
35 CMM_STORE_SHARED(URCU_TLS(cached_vtid), 0);
36}
37
38static
39size_t vtid_get_size(struct lttng_ust_ctx_field *field, size_t offset)
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
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)) {
55 vtid = lttng_gettid();
56 CMM_STORE_SHARED(URCU_TLS(cached_vtid), vtid);
57 }
58 return vtid;
59}
60
61static
62void vtid_record(struct lttng_ust_ctx_field *field,
63 struct lttng_ust_lib_ring_buffer_ctx *ctx,
64 struct lttng_channel *chan)
65{
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));
70}
71
72static
73void vtid_get_value(struct lttng_ust_ctx_field *field,
74 struct lttng_ust_ctx_value *value)
75{
76 value->u.s64 = wrapper_getvtid();
77}
78
79int lttng_add_vtid_to_ctx(struct lttng_ust_ctx **ctx)
80{
81 struct lttng_ust_ctx_field *field;
82
83 field = lttng_append_context(ctx);
84 if (!field)
85 return -ENOMEM;
86 if (lttng_find_context(*ctx, "vtid")) {
87 lttng_remove_context_field(ctx, field);
88 return -EEXIST;
89 }
90 field->event_field->name = "vtid";
91 field->event_field->type.atype = atype_integer;
92 field->event_field->type.u.integer.size = sizeof(pid_t) * CHAR_BIT;
93 field->event_field->type.u.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT;
94 field->event_field->type.u.integer.signedness = lttng_is_signed_type(pid_t);
95 field->event_field->type.u.integer.reverse_byte_order = 0;
96 field->event_field->type.u.integer.base = 10;
97 field->event_field->type.u.integer.encoding = lttng_encode_none;
98 field->get_size = vtid_get_size;
99 field->record = vtid_record;
100 field->get_value = vtid_get_value;
101 lttng_context_update(*ctx);
102 return 0;
103}
104
105/*
106 * Force a read (imply TLS fixup for dlopen) of TLS variables.
107 */
108void lttng_fixup_vtid_tls(void)
109{
110 asm volatile ("" : : "m" (URCU_TLS(cached_vtid)));
111}
This page took 0.030447 seconds and 4 git commands to generate.