Commit | Line | Data |
---|---|---|
c1ef86f0 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
c1ef86f0 | 3 | * |
e92f3e28 MD |
4 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
c0c0989a | 6 | * LTTng UST vpid context. |
c1ef86f0 MD |
7 | */ |
8 | ||
3fbec7dc | 9 | #define _LGPL_SOURCE |
9af5d97a | 10 | #include <limits.h> |
b4051ad8 | 11 | #include <stddef.h> |
c1ef86f0 MD |
12 | #include <sys/types.h> |
13 | #include <unistd.h> | |
4318ae1b MD |
14 | #include <lttng/ust-events.h> |
15 | #include <lttng/ust-tracer.h> | |
0466ac28 | 16 | #include <lttng/ringbuffer-context.h> |
c1ef86f0 | 17 | |
fc80554e MJ |
18 | #include "context-internal.h" |
19 | ||
c1ef86f0 MD |
20 | /* |
21 | * We cache the result to ensure we don't trigger a system call for | |
22 | * each event. | |
23 | */ | |
24 | static pid_t cached_vpid; | |
25 | ||
26 | static inline | |
98357ffd | 27 | pid_t wrapper_getvpid(void) |
c1ef86f0 | 28 | { |
98357ffd MD |
29 | pid_t vpid; |
30 | ||
31 | vpid = CMM_LOAD_SHARED(cached_vpid); | |
32 | if (caa_unlikely(!vpid)) { | |
33 | vpid = getpid(); | |
34 | CMM_STORE_SHARED(cached_vpid, vpid); | |
35 | } | |
36 | return vpid; | |
c1ef86f0 MD |
37 | } |
38 | ||
39 | /* | |
40 | * Upon fork or clone, the PID assigned to our thread is not the same as | |
41 | * we kept in cache. | |
42 | */ | |
43 | void lttng_context_vpid_reset(void) | |
44 | { | |
98357ffd | 45 | CMM_STORE_SHARED(cached_vpid, 0); |
c1ef86f0 | 46 | } |
c1ef86f0 MD |
47 | |
48 | static | |
4e48b5d2 | 49 | size_t vpid_get_size(void *priv __attribute__((unused)), |
2208d8b5 | 50 | size_t offset) |
c1ef86f0 MD |
51 | { |
52 | size_t size = 0; | |
53 | ||
dc325c1d | 54 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(pid_t)); |
c1ef86f0 MD |
55 | size += sizeof(pid_t); |
56 | return size; | |
57 | } | |
58 | ||
59 | static | |
4e48b5d2 | 60 | void vpid_record(void *priv __attribute__((unused)), |
4cfec15c | 61 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
e7bc0ef6 | 62 | struct lttng_ust_channel_buffer *chan) |
c1ef86f0 | 63 | { |
98357ffd | 64 | pid_t vpid = wrapper_getvpid(); |
c1ef86f0 | 65 | |
8936b6c0 | 66 | chan->ops->event_write(ctx, &vpid, sizeof(vpid), lttng_ust_rb_alignof(vpid)); |
c1ef86f0 MD |
67 | } |
68 | ||
77aa5901 | 69 | static |
4e48b5d2 | 70 | void vpid_get_value(void *priv __attribute__((unused)), |
daacdbfc | 71 | struct lttng_ust_ctx_value *value) |
77aa5901 | 72 | { |
98357ffd | 73 | value->u.s64 = wrapper_getvpid(); |
77aa5901 MD |
74 | } |
75 | ||
4e48b5d2 MD |
76 | static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field( |
77 | lttng_ust_static_event_field("vpid", | |
78 | lttng_ust_static_type_integer(sizeof(pid_t) * CHAR_BIT, | |
79 | lttng_ust_rb_alignof(pid_t) * CHAR_BIT, | |
80 | lttng_ust_is_signed_type(pid_t), | |
81 | BYTE_ORDER, 10), | |
82 | false, false), | |
83 | vpid_get_size, | |
84 | vpid_record, | |
85 | vpid_get_value, | |
86 | NULL, NULL); | |
87 | ||
daacdbfc | 88 | int lttng_add_vpid_to_ctx(struct lttng_ust_ctx **ctx) |
c1ef86f0 | 89 | { |
a084756d | 90 | int ret; |
c1ef86f0 | 91 | |
4e48b5d2 | 92 | if (lttng_find_context(*ctx, ctx_field->event_field->name)) { |
a084756d MD |
93 | ret = -EEXIST; |
94 | goto error_find_context; | |
c1ef86f0 | 95 | } |
4e48b5d2 MD |
96 | ret = lttng_ust_context_append(ctx, ctx_field); |
97 | if (ret) | |
98 | return ret; | |
c1ef86f0 | 99 | return 0; |
a084756d | 100 | |
a084756d | 101 | error_find_context: |
a084756d | 102 | return ret; |
c1ef86f0 | 103 | } |