Commit | Line | Data |
---|---|---|
c1ef86f0 | 1 | /* |
e92f3e28 | 2 | * lttng-context-vpid.c |
c1ef86f0 MD |
3 | * |
4 | * LTTng UST vpid context. | |
5 | * | |
e92f3e28 MD |
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 | |
c1ef86f0 MD |
21 | */ |
22 | ||
3fbec7dc | 23 | #define _LGPL_SOURCE |
b4051ad8 | 24 | #include <stddef.h> |
c1ef86f0 MD |
25 | #include <sys/types.h> |
26 | #include <unistd.h> | |
4318ae1b MD |
27 | #include <lttng/ust-events.h> |
28 | #include <lttng/ust-tracer.h> | |
29 | #include <lttng/ringbuffer-config.h> | |
c1ef86f0 | 30 | |
c1ef86f0 MD |
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 | |
98357ffd | 38 | pid_t wrapper_getvpid(void) |
c1ef86f0 | 39 | { |
98357ffd MD |
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; | |
c1ef86f0 MD |
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 | { | |
98357ffd | 56 | CMM_STORE_SHARED(cached_vpid, 0); |
c1ef86f0 | 57 | } |
c1ef86f0 MD |
58 | |
59 | static | |
53569322 | 60 | size_t vpid_get_size(struct lttng_ctx_field *field, size_t offset) |
c1ef86f0 MD |
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, | |
4cfec15c | 71 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
7dd08bec | 72 | struct lttng_channel *chan) |
c1ef86f0 | 73 | { |
98357ffd | 74 | pid_t vpid = wrapper_getvpid(); |
c1ef86f0 | 75 | |
98357ffd MD |
76 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vpid)); |
77 | chan->ops->event_write(ctx, &vpid, sizeof(vpid)); | |
c1ef86f0 MD |
78 | } |
79 | ||
77aa5901 MD |
80 | static |
81 | void vpid_get_value(struct lttng_ctx_field *field, | |
53569322 | 82 | struct lttng_ctx_value *value) |
77aa5901 | 83 | { |
98357ffd | 84 | value->u.s64 = wrapper_getvpid(); |
77aa5901 MD |
85 | } |
86 | ||
c1ef86f0 MD |
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; | |
218deb69 MD |
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; | |
c1ef86f0 MD |
106 | field->get_size = vpid_get_size; |
107 | field->record = vpid_record; | |
77aa5901 | 108 | field->get_value = vpid_get_value; |
b2cc986a | 109 | lttng_context_update(*ctx); |
c1ef86f0 MD |
110 | return 0; |
111 | } |