Namespace 'lttng_alignof' to 'lttng_ust_rb_alignof'
[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 <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/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(struct lttng_ust_ctx_field *field, size_t offset)
50 {
51 size_t size = 0;
52
53 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(pid_t));
54 size += sizeof(pid_t);
55 return size;
56 }
57
58 static
59 void vpid_record(struct lttng_ust_ctx_field *field,
60 struct lttng_ust_lib_ring_buffer_ctx *ctx,
61 struct lttng_ust_channel_buffer *chan)
62 {
63 pid_t vpid = wrapper_getvpid();
64
65 lttng_ust_lib_ring_buffer_align_ctx(ctx, lttng_ust_rb_alignof(vpid));
66 chan->ops->event_write(ctx, &vpid, sizeof(vpid));
67 }
68
69 static
70 void vpid_get_value(struct lttng_ust_ctx_field *field,
71 struct lttng_ust_ctx_value *value)
72 {
73 value->u.s64 = wrapper_getvpid();
74 }
75
76 int lttng_add_vpid_to_ctx(struct lttng_ust_ctx **ctx)
77 {
78 struct lttng_ust_ctx_field *field;
79 struct lttng_ust_type_common *type;
80 int ret;
81
82 type = lttng_ust_create_type_integer(sizeof(pid_t) * CHAR_BIT,
83 lttng_ust_rb_alignof(pid_t) * CHAR_BIT,
84 lttng_ust_is_signed_type(pid_t),
85 BYTE_ORDER, 10);
86 if (!type)
87 return -ENOMEM;
88 field = lttng_append_context(ctx);
89 if (!field) {
90 ret = -ENOMEM;
91 goto error_context;
92 }
93 if (lttng_find_context(*ctx, "vpid")) {
94 ret = -EEXIST;
95 goto error_find_context;
96 }
97 field->event_field->name = strdup("vpid");
98 if (!field->event_field->name) {
99 ret = -ENOMEM;
100 goto error_name;
101 }
102 field->event_field->type = type;
103 field->get_size = vpid_get_size;
104 field->record = vpid_record;
105 field->get_value = vpid_get_value;
106 lttng_context_update(*ctx);
107 return 0;
108
109 error_name:
110 error_find_context:
111 lttng_remove_context_field(ctx, field);
112 error_context:
113 lttng_ust_destroy_type(type);
114 return ret;
115 }
This page took 0.031968 seconds and 5 git commands to generate.