1558ee9851fb28ddf284f0cca0ef9453c1a207a7
[lttng-ust.git] / src / lib / lttng-ust / lttng-context-vpid.c
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 vpid context.
7 */
8
9 #define _LGPL_SOURCE
10 #include <limits.h>
11 #include <stddef.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <lttng/ust-events.h>
15 #include <lttng/ust-tracer.h>
16 #include <lttng/ust-ringbuffer-context.h>
17
18 #include "context-internal.h"
19
20 /*
21 * We cache the result to ensure we don't trigger a system call for
22 * each event.
23 */
24 static pid_t cached_vpid;
25
26 static inline
27 pid_t wrapper_getvpid(void)
28 {
29 pid_t vpid;
30
31 vpid = CMM_LOAD_SHARED(cached_vpid);
32 if (caa_unlikely(!vpid)) {
33 vpid = getpid();
34 CMM_STORE_SHARED(cached_vpid, vpid);
35 }
36 return vpid;
37 }
38
39 /*
40 * Upon fork or clone, the PID assigned to our thread is not the same as
41 * we kept in cache.
42 */
43 void lttng_context_vpid_reset(void)
44 {
45 CMM_STORE_SHARED(cached_vpid, 0);
46 }
47
48 static
49 size_t vpid_get_size(void *priv __attribute__((unused)),
50 size_t offset)
51 {
52 size_t size = 0;
53
54 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(pid_t));
55 size += sizeof(pid_t);
56 return size;
57 }
58
59 static
60 void vpid_record(void *priv __attribute__((unused)),
61 struct lttng_ust_ring_buffer_ctx *ctx,
62 struct lttng_ust_channel_buffer *chan)
63 {
64 pid_t vpid = wrapper_getvpid();
65
66 chan->ops->event_write(ctx, &vpid, sizeof(vpid), lttng_ust_rb_alignof(vpid));
67 }
68
69 static
70 void vpid_get_value(void *priv __attribute__((unused)),
71 struct lttng_ust_ctx_value *value)
72 {
73 value->u.s64 = wrapper_getvpid();
74 }
75
76 static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
77 lttng_ust_static_event_field("vpid",
78 lttng_ust_static_type_integer(sizeof(pid_t) * CHAR_BIT,
79 lttng_ust_rb_alignof(pid_t) * CHAR_BIT,
80 lttng_ust_is_signed_type(pid_t),
81 LTTNG_UST_BYTE_ORDER, 10),
82 false, false),
83 vpid_get_size,
84 vpid_record,
85 vpid_get_value,
86 NULL, NULL);
87
88 int lttng_add_vpid_to_ctx(struct lttng_ust_ctx **ctx)
89 {
90 int ret;
91
92 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
93 ret = -EEXIST;
94 goto error_find_context;
95 }
96 ret = lttng_ust_context_append(ctx, ctx_field);
97 if (ret)
98 return ret;
99 return 0;
100
101 error_find_context:
102 return ret;
103 }
This page took 0.031323 seconds and 3 git commands to generate.