API refactoring: introduce probe context
[lttng-ust.git] / src / lib / lttng-ust / lttng-context-vpid.c
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng UST vpid context.
7 */
8
9#define _LGPL_SOURCE
10#include <limits.h>
11#include <stddef.h>
12#include <sys/types.h>
13#include <unistd.h>
14#include <lttng/ust-events.h>
15#include <lttng/ust-tracer.h>
16#include <lttng/ust-ringbuffer-context.h>
17
18#include "context-internal.h"
19
20/*
21 * We cache the result to ensure we don't trigger a system call for
22 * each event.
23 */
24static pid_t cached_vpid;
25
26static inline
27pid_t wrapper_getvpid(void)
28{
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;
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 */
43void lttng_context_vpid_reset(void)
44{
45 CMM_STORE_SHARED(cached_vpid, 0);
46}
47
48static
49size_t vpid_get_size(void *priv __attribute__((unused)),
50 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
51 size_t offset)
52{
53 size_t size = 0;
54
55 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(pid_t));
56 size += sizeof(pid_t);
57 return size;
58}
59
60static
61void vpid_record(void *priv __attribute__((unused)),
62 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
63 struct lttng_ust_ring_buffer_ctx *ctx,
64 struct lttng_ust_channel_buffer *chan)
65{
66 pid_t vpid = wrapper_getvpid();
67
68 chan->ops->event_write(ctx, &vpid, sizeof(vpid), lttng_ust_rb_alignof(vpid));
69}
70
71static
72void vpid_get_value(void *priv __attribute__((unused)),
73 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
74 struct lttng_ust_ctx_value *value)
75{
76 value->u.s64 = wrapper_getvpid();
77}
78
79static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
80 lttng_ust_static_event_field("vpid",
81 lttng_ust_static_type_integer(sizeof(pid_t) * CHAR_BIT,
82 lttng_ust_rb_alignof(pid_t) * CHAR_BIT,
83 lttng_ust_is_signed_type(pid_t),
84 LTTNG_UST_BYTE_ORDER, 10),
85 false, false),
86 vpid_get_size,
87 vpid_record,
88 vpid_get_value,
89 NULL, NULL);
90
91int lttng_add_vpid_to_ctx(struct lttng_ust_ctx **ctx)
92{
93 int ret;
94
95 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
96 ret = -EEXIST;
97 goto error_find_context;
98 }
99 ret = lttng_ust_context_append(ctx, ctx_field);
100 if (ret)
101 return ret;
102 return 0;
103
104error_find_context:
105 return ret;
106}
This page took 0.02258 seconds and 4 git commands to generate.