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