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 |
b4051ad8 | 10 | #include <stddef.h> |
c1ef86f0 MD |
11 | #include <sys/types.h> |
12 | #include <unistd.h> | |
4318ae1b MD |
13 | #include <lttng/ust-events.h> |
14 | #include <lttng/ust-tracer.h> | |
15 | #include <lttng/ringbuffer-config.h> | |
c1ef86f0 | 16 | |
c1ef86f0 MD |
17 | /* |
18 | * We cache the result to ensure we don't trigger a system call for | |
19 | * each event. | |
20 | */ | |
21 | static pid_t cached_vpid; | |
22 | ||
23 | static inline | |
98357ffd | 24 | pid_t wrapper_getvpid(void) |
c1ef86f0 | 25 | { |
98357ffd MD |
26 | pid_t vpid; |
27 | ||
28 | vpid = CMM_LOAD_SHARED(cached_vpid); | |
29 | if (caa_unlikely(!vpid)) { | |
30 | vpid = getpid(); | |
31 | CMM_STORE_SHARED(cached_vpid, vpid); | |
32 | } | |
33 | return vpid; | |
c1ef86f0 MD |
34 | } |
35 | ||
36 | /* | |
37 | * Upon fork or clone, the PID assigned to our thread is not the same as | |
38 | * we kept in cache. | |
39 | */ | |
40 | void lttng_context_vpid_reset(void) | |
41 | { | |
98357ffd | 42 | CMM_STORE_SHARED(cached_vpid, 0); |
c1ef86f0 | 43 | } |
c1ef86f0 MD |
44 | |
45 | static | |
53569322 | 46 | size_t vpid_get_size(struct lttng_ctx_field *field, size_t offset) |
c1ef86f0 MD |
47 | { |
48 | size_t size = 0; | |
49 | ||
50 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); | |
51 | size += sizeof(pid_t); | |
52 | return size; | |
53 | } | |
54 | ||
55 | static | |
56 | void vpid_record(struct lttng_ctx_field *field, | |
4cfec15c | 57 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
7dd08bec | 58 | struct lttng_channel *chan) |
c1ef86f0 | 59 | { |
98357ffd | 60 | pid_t vpid = wrapper_getvpid(); |
c1ef86f0 | 61 | |
98357ffd MD |
62 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vpid)); |
63 | chan->ops->event_write(ctx, &vpid, sizeof(vpid)); | |
c1ef86f0 MD |
64 | } |
65 | ||
77aa5901 MD |
66 | static |
67 | void vpid_get_value(struct lttng_ctx_field *field, | |
53569322 | 68 | struct lttng_ctx_value *value) |
77aa5901 | 69 | { |
98357ffd | 70 | value->u.s64 = wrapper_getvpid(); |
77aa5901 MD |
71 | } |
72 | ||
c1ef86f0 MD |
73 | int lttng_add_vpid_to_ctx(struct lttng_ctx **ctx) |
74 | { | |
75 | struct lttng_ctx_field *field; | |
76 | ||
77 | field = lttng_append_context(ctx); | |
78 | if (!field) | |
79 | return -ENOMEM; | |
80 | if (lttng_find_context(*ctx, "vpid")) { | |
81 | lttng_remove_context_field(ctx, field); | |
82 | return -EEXIST; | |
83 | } | |
84 | field->event_field.name = "vpid"; | |
85 | field->event_field.type.atype = atype_integer; | |
218deb69 MD |
86 | field->event_field.type.u.integer.size = sizeof(pid_t) * CHAR_BIT; |
87 | field->event_field.type.u.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT; | |
88 | field->event_field.type.u.integer.signedness = lttng_is_signed_type(pid_t); | |
89 | field->event_field.type.u.integer.reverse_byte_order = 0; | |
90 | field->event_field.type.u.integer.base = 10; | |
91 | field->event_field.type.u.integer.encoding = lttng_encode_none; | |
c1ef86f0 MD |
92 | field->get_size = vpid_get_size; |
93 | field->record = vpid_record; | |
77aa5901 | 94 | field->get_value = vpid_get_value; |
b2cc986a | 95 | lttng_context_update(*ctx); |
c1ef86f0 MD |
96 | return 0; |
97 | } |