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 |
c1ef86f0 MD |
24 | #include <sys/types.h> |
25 | #include <unistd.h> | |
4318ae1b MD |
26 | #include <lttng/ust-events.h> |
27 | #include <lttng/ust-tracer.h> | |
28 | #include <lttng/ringbuffer-config.h> | |
c1ef86f0 MD |
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 | { | |
b5a3dfa5 | 50 | if (caa_unlikely(!cached_vpid)) |
c1ef86f0 MD |
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 | |
53569322 | 66 | size_t vpid_get_size(struct lttng_ctx_field *field, size_t offset) |
c1ef86f0 MD |
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, | |
4cfec15c | 77 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
7dd08bec | 78 | struct lttng_channel *chan) |
c1ef86f0 MD |
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 | ||
77aa5901 MD |
87 | static |
88 | void vpid_get_value(struct lttng_ctx_field *field, | |
53569322 | 89 | struct lttng_ctx_value *value) |
77aa5901 MD |
90 | { |
91 | pid_t pid; | |
92 | ||
93 | pid = wrapper_getpid(); | |
53569322 | 94 | value->u.s64 = pid; |
77aa5901 MD |
95 | } |
96 | ||
c1ef86f0 MD |
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; | |
77aa5901 | 118 | field->get_value = vpid_get_value; |
b2cc986a | 119 | lttng_context_update(*ctx); |
c1ef86f0 MD |
120 | return 0; |
121 | } |