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> | |
0466ac28 | 15 | #include <lttng/ringbuffer-context.h> |
c1ef86f0 | 16 | |
fc80554e MJ |
17 | #include "context-internal.h" |
18 | ||
c1ef86f0 MD |
19 | /* |
20 | * We cache the result to ensure we don't trigger a system call for | |
21 | * each event. | |
22 | */ | |
23 | static pid_t cached_vpid; | |
24 | ||
25 | static inline | |
98357ffd | 26 | pid_t wrapper_getvpid(void) |
c1ef86f0 | 27 | { |
98357ffd MD |
28 | pid_t vpid; |
29 | ||
30 | vpid = CMM_LOAD_SHARED(cached_vpid); | |
31 | if (caa_unlikely(!vpid)) { | |
32 | vpid = getpid(); | |
33 | CMM_STORE_SHARED(cached_vpid, vpid); | |
34 | } | |
35 | return vpid; | |
c1ef86f0 MD |
36 | } |
37 | ||
38 | /* | |
39 | * Upon fork or clone, the PID assigned to our thread is not the same as | |
40 | * we kept in cache. | |
41 | */ | |
42 | void lttng_context_vpid_reset(void) | |
43 | { | |
98357ffd | 44 | CMM_STORE_SHARED(cached_vpid, 0); |
c1ef86f0 | 45 | } |
c1ef86f0 MD |
46 | |
47 | static | |
daacdbfc | 48 | size_t vpid_get_size(struct lttng_ust_ctx_field *field, size_t offset) |
c1ef86f0 MD |
49 | { |
50 | size_t size = 0; | |
51 | ||
52 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); | |
53 | size += sizeof(pid_t); | |
54 | return size; | |
55 | } | |
56 | ||
57 | static | |
daacdbfc | 58 | void vpid_record(struct lttng_ust_ctx_field *field, |
4cfec15c | 59 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
e7bc0ef6 | 60 | struct lttng_ust_channel_buffer *chan) |
c1ef86f0 | 61 | { |
98357ffd | 62 | pid_t vpid = wrapper_getvpid(); |
c1ef86f0 | 63 | |
98357ffd MD |
64 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vpid)); |
65 | chan->ops->event_write(ctx, &vpid, sizeof(vpid)); | |
c1ef86f0 MD |
66 | } |
67 | ||
77aa5901 | 68 | static |
daacdbfc MD |
69 | void vpid_get_value(struct lttng_ust_ctx_field *field, |
70 | struct lttng_ust_ctx_value *value) | |
77aa5901 | 71 | { |
98357ffd | 72 | value->u.s64 = wrapper_getvpid(); |
77aa5901 MD |
73 | } |
74 | ||
daacdbfc | 75 | int lttng_add_vpid_to_ctx(struct lttng_ust_ctx **ctx) |
c1ef86f0 | 76 | { |
daacdbfc | 77 | struct lttng_ust_ctx_field *field; |
a084756d MD |
78 | struct lttng_ust_type_common *type; |
79 | int ret; | |
c1ef86f0 | 80 | |
a084756d MD |
81 | type = lttng_ust_create_type_integer(sizeof(pid_t) * CHAR_BIT, |
82 | lttng_alignof(pid_t) * CHAR_BIT, | |
83 | lttng_is_signed_type(pid_t), | |
84 | BYTE_ORDER, 10); | |
85 | if (!type) | |
c1ef86f0 | 86 | return -ENOMEM; |
a084756d MD |
87 | field = lttng_append_context(ctx); |
88 | if (!field) { | |
89 | ret = -ENOMEM; | |
90 | goto error_context; | |
91 | } | |
c1ef86f0 | 92 | if (lttng_find_context(*ctx, "vpid")) { |
a084756d MD |
93 | ret = -EEXIST; |
94 | goto error_find_context; | |
c1ef86f0 | 95 | } |
a084756d MD |
96 | field->event_field->name = strdup("vpid"); |
97 | if (!field->event_field->name) { | |
98 | ret = -ENOMEM; | |
99 | goto error_name; | |
100 | } | |
101 | field->event_field->type = type; | |
c1ef86f0 MD |
102 | field->get_size = vpid_get_size; |
103 | field->record = vpid_record; | |
77aa5901 | 104 | field->get_value = vpid_get_value; |
b2cc986a | 105 | lttng_context_update(*ctx); |
c1ef86f0 | 106 | return 0; |
a084756d MD |
107 | |
108 | error_name: | |
109 | error_find_context: | |
110 | lttng_remove_context_field(ctx, field); | |
111 | error_context: | |
112 | lttng_ust_destroy_type(type); | |
113 | return ret; | |
c1ef86f0 | 114 | } |