5e5ed145d944adf08be61755eaa32faefe3e52b4
[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 #define _LGPL_SOURCE
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <lttng/ust-events.h>
27 #include <lttng/ust-tracer.h>
28 #include <lttng/ringbuffer-config.h>
29
30 #ifdef __linux__
31 static inline
32 pid_t wrapper_getpid(void)
33 {
34 return getpid();
35 }
36
37 void lttng_context_vpid_reset(void)
38 {
39 }
40 #else
41 /*
42 * We cache the result to ensure we don't trigger a system call for
43 * each event.
44 */
45 static pid_t cached_vpid;
46
47 static inline
48 pid_t wrapper_getpid(void)
49 {
50 if (caa_unlikely(!cached_vpid))
51 cached_vpid = getpid();
52 return cached_vpid;
53 }
54
55 /*
56 * Upon fork or clone, the PID assigned to our thread is not the same as
57 * we kept in cache.
58 */
59 void lttng_context_vpid_reset(void)
60 {
61 cached_vpid = 0;
62 }
63 #endif
64
65 static
66 size_t vpid_get_size(struct lttng_ctx_field *field, size_t offset)
67 {
68 size_t size = 0;
69
70 size += lib_ring_buffer_align(offset, lttng_alignof(pid_t));
71 size += sizeof(pid_t);
72 return size;
73 }
74
75 static
76 void vpid_record(struct lttng_ctx_field *field,
77 struct lttng_ust_lib_ring_buffer_ctx *ctx,
78 struct lttng_channel *chan)
79 {
80 pid_t pid;
81
82 pid = wrapper_getpid();
83 lib_ring_buffer_align_ctx(ctx, lttng_alignof(pid));
84 chan->ops->event_write(ctx, &pid, sizeof(pid));
85 }
86
87 static
88 void vpid_get_value(struct lttng_ctx_field *field,
89 struct lttng_ctx_value *value)
90 {
91 pid_t pid;
92
93 pid = wrapper_getpid();
94 value->u.s64 = pid;
95 }
96
97 int lttng_add_vpid_to_ctx(struct lttng_ctx **ctx)
98 {
99 struct lttng_ctx_field *field;
100
101 field = lttng_append_context(ctx);
102 if (!field)
103 return -ENOMEM;
104 if (lttng_find_context(*ctx, "vpid")) {
105 lttng_remove_context_field(ctx, field);
106 return -EEXIST;
107 }
108 field->event_field.name = "vpid";
109 field->event_field.type.atype = atype_integer;
110 field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT;
111 field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT;
112 field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t);
113 field->event_field.type.u.basic.integer.reverse_byte_order = 0;
114 field->event_field.type.u.basic.integer.base = 10;
115 field->event_field.type.u.basic.integer.encoding = lttng_encode_none;
116 field->get_size = vpid_get_size;
117 field->record = vpid_record;
118 field->get_value = vpid_get_value;
119 lttng_context_update(*ctx);
120 return 0;
121 }
This page took 0.031216 seconds and 3 git commands to generate.