949529c606c253d6fdf272360658370ad818b2d7
[lttng-ust.git] / liblttng-ust / lttng-context-vpid.c
1 /*
2 * lttng-context-vpid.c
3 *
4 * LTTng UST vpid context.
5 *
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <lttng/ust-events.h>
26 #include <lttng/ust-tracer.h>
27 #include <lttng/ringbuffer-config.h>
28
29 #ifdef __linux__
30 static inline
31 pid_t wrapper_getpid(void)
32 {
33 return getpid();
34 }
35
36 void lttng_context_vpid_reset(void)
37 {
38 }
39 #else
40 /*
41 * We cache the result to ensure we don't trigger a system call for
42 * each event.
43 */
44 static pid_t cached_vpid;
45
46 static inline
47 pid_t wrapper_getpid(void)
48 {
49 if (caa_unlikely(!cached_vpid))
50 cached_vpid = getpid();
51 return cached_vpid;
52 }
53
54 /*
55 * Upon fork or clone, the PID assigned to our thread is not the same as
56 * we kept in cache.
57 */
58 void lttng_context_vpid_reset(void)
59 {
60 cached_vpid = 0;
61 }
62 #endif
63
64 static
65 size_t vpid_get_size(size_t offset)
66 {
67 size_t size = 0;
68
69 size += lib_ring_buffer_align(offset, lttng_alignof(pid_t));
70 size += sizeof(pid_t);
71 return size;
72 }
73
74 static
75 void vpid_record(struct lttng_ctx_field *field,
76 struct lttng_ust_lib_ring_buffer_ctx *ctx,
77 struct lttng_channel *chan)
78 {
79 pid_t pid;
80
81 pid = wrapper_getpid();
82 lib_ring_buffer_align_ctx(ctx, lttng_alignof(pid));
83 chan->ops->event_write(ctx, &pid, sizeof(pid));
84 }
85
86 static
87 void vpid_get_value(struct lttng_ctx_field *field,
88 union lttng_ctx_value *value)
89 {
90 pid_t pid;
91
92 pid = wrapper_getpid();
93 value->s64 = pid;
94 }
95
96 int lttng_add_vpid_to_ctx(struct lttng_ctx **ctx)
97 {
98 struct lttng_ctx_field *field;
99
100 field = lttng_append_context(ctx);
101 if (!field)
102 return -ENOMEM;
103 if (lttng_find_context(*ctx, "vpid")) {
104 lttng_remove_context_field(ctx, field);
105 return -EEXIST;
106 }
107 field->event_field.name = "vpid";
108 field->event_field.type.atype = atype_integer;
109 field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT;
110 field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT;
111 field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t);
112 field->event_field.type.u.basic.integer.reverse_byte_order = 0;
113 field->event_field.type.u.basic.integer.base = 10;
114 field->event_field.type.u.basic.integer.encoding = lttng_encode_none;
115 field->get_size = vpid_get_size;
116 field->record = vpid_record;
117 field->get_value = vpid_get_value;
118 return 0;
119 }
This page took 0.033188 seconds and 3 git commands to generate.