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