Move context symbols to private header
[lttng-ust.git] / liblttng-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 <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
17 #include "context-internal.h"
18
19 /*
20 * We cache the result to ensure we don't trigger a system call for
21 * each event.
22 */
23 static pid_t cached_vpid;
24
25 static inline
26 pid_t wrapper_getvpid(void)
27 {
28 pid_t vpid;
29
30 vpid = CMM_LOAD_SHARED(cached_vpid);
31 if (caa_unlikely(!vpid)) {
32 vpid = getpid();
33 CMM_STORE_SHARED(cached_vpid, vpid);
34 }
35 return vpid;
36 }
37
38 /*
39 * Upon fork or clone, the PID assigned to our thread is not the same as
40 * we kept in cache.
41 */
42 void lttng_context_vpid_reset(void)
43 {
44 CMM_STORE_SHARED(cached_vpid, 0);
45 }
46
47 static
48 size_t vpid_get_size(struct lttng_ctx_field *field, size_t offset)
49 {
50 size_t size = 0;
51
52 size += lib_ring_buffer_align(offset, lttng_alignof(pid_t));
53 size += sizeof(pid_t);
54 return size;
55 }
56
57 static
58 void vpid_record(struct lttng_ctx_field *field,
59 struct lttng_ust_lib_ring_buffer_ctx *ctx,
60 struct lttng_channel *chan)
61 {
62 pid_t vpid = wrapper_getvpid();
63
64 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vpid));
65 chan->ops->event_write(ctx, &vpid, sizeof(vpid));
66 }
67
68 static
69 void vpid_get_value(struct lttng_ctx_field *field,
70 struct lttng_ctx_value *value)
71 {
72 value->u.s64 = wrapper_getvpid();
73 }
74
75 int lttng_add_vpid_to_ctx(struct lttng_ctx **ctx)
76 {
77 struct lttng_ctx_field *field;
78
79 field = lttng_append_context(ctx);
80 if (!field)
81 return -ENOMEM;
82 if (lttng_find_context(*ctx, "vpid")) {
83 lttng_remove_context_field(ctx, field);
84 return -EEXIST;
85 }
86 field->event_field.name = "vpid";
87 field->event_field.type.atype = atype_integer;
88 field->event_field.type.u.integer.size = sizeof(pid_t) * CHAR_BIT;
89 field->event_field.type.u.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT;
90 field->event_field.type.u.integer.signedness = lttng_is_signed_type(pid_t);
91 field->event_field.type.u.integer.reverse_byte_order = 0;
92 field->event_field.type.u.integer.base = 10;
93 field->event_field.type.u.integer.encoding = lttng_encode_none;
94 field->get_size = vpid_get_size;
95 field->record = vpid_record;
96 field->get_value = vpid_get_value;
97 lttng_context_update(*ctx);
98 return 0;
99 }
This page took 0.031859 seconds and 5 git commands to generate.