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