2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2010-2021 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring buffer context header.
9 #ifndef _LTTNG_RING_BUFFER_CONTEXT_H
10 #define _LTTNG_RING_BUFFER_CONTEXT_H
15 #include <urcu/arch.h>
18 #include <lttng/ust-tracer.h>
19 #include <lttng/ust-utils.h>
20 #include <lttng/ust-compiler.h>
22 struct lttng_ust_lib_ring_buffer
;
23 struct lttng_ust_lib_ring_buffer_channel
;
24 struct lttng_ust_lib_ring_buffer_ctx
;
29 * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(),
30 * lib_ring_buffer_try_discard_reserve(), lttng_ust_lib_ring_buffer_align_ctx() and
31 * lib_ring_buffer_write().
33 * IMPORTANT: this structure is part of the ABI between the probe and
34 * UST. Fields need to be only added at the end, never reordered, never
37 * The field @struct_size should be used to determine the size of the
38 * structure. It should be queried before using additional fields added
39 * at the end of the structure.
41 struct lttng_ust_lib_ring_buffer_ctx
{
42 uint32_t struct_size
; /* Size of this structure. */
44 /* input received by lib_ring_buffer_reserve(). */
45 struct lttng_ust_lib_ring_buffer_channel
*chan
; /* channel */
46 void *priv
; /* client private data */
47 size_t data_size
; /* size of payload */
49 * alignment of the largest element
53 /* output from lib_ring_buffer_reserve() */
54 int reserve_cpu
; /* processor id updated by the reserve */
55 size_t slot_size
; /* size of the reserved slot */
56 unsigned long buf_offset
; /* offset following the record header */
57 unsigned long pre_offset
; /*
58 * Initial offset position _before_
59 * the record is written. Positioned
60 * prior to record header alignment
63 uint64_t tsc
; /* time-stamp counter value */
64 unsigned int rflags
; /* reservation flags */
65 void *ip
; /* caller ip address */
67 struct lttng_ust_lib_ring_buffer
*buf
; /*
68 * buffer corresponding to processor id
71 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
73 /* End of base ABI. Fields below should be used after checking struct_size. */
77 * lttng_ust_lib_ring_buffer_ctx_init - initialize ring buffer context
78 * @ctx: ring buffer context to initialize
80 * @priv: client private data
81 * @data_size: size of record data payload
82 * @largest_align: largest alignment within data payload types
84 static inline lttng_ust_notrace
85 void lttng_ust_lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
86 struct lttng_ust_lib_ring_buffer_channel
*chan
,
87 void *priv
, size_t data_size
, int largest_align
);
89 void lttng_ust_lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
90 struct lttng_ust_lib_ring_buffer_channel
*chan
,
91 void *priv
, size_t data_size
, int largest_align
)
93 ctx
->struct_size
= sizeof(struct lttng_ust_lib_ring_buffer_ctx
);
96 ctx
->data_size
= data_size
;
97 ctx
->reserve_cpu
= -1;
98 ctx
->largest_align
= largest_align
;
104 * We need to define LTTNG_UST_RING_BUFFER_ALIGN_ATTR so it is known early at
105 * compile-time. We have to duplicate the "config->align" information and the
106 * definition here because config->align is used both in the slow and fast
107 * paths, but LTTNG_UST_RING_BUFFER_ALIGN_ATTR is only available for the client
110 #ifdef LTTNG_UST_RING_BUFFER_NATURAL_ALIGN
112 # define LTTNG_UST_RING_BUFFER_ALIGN_ATTR /* Default arch alignment */
115 * lttng_ust_lib_ring_buffer_align - Calculate the offset needed to align the type.
116 * @align_drift: object offset from an "alignment"-aligned address.
117 * @size_of_type: Must be non-zero, power of 2.
119 static inline lttng_ust_notrace
120 unsigned int lttng_ust_lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
);
122 unsigned int lttng_ust_lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
124 return lttng_ust_offset_align(align_drift
, size_of_type
);
129 # define LTTNG_UST_RING_BUFFER_ALIGN_ATTR __attribute__((packed))
132 * lttng_ust_lib_ring_buffer_align - Calculate the offset needed to align the type.
133 * @align_drift: object offset from an "alignment"-aligned address.
134 * @size_of_type: Must be non-zero, power of 2.
136 static inline lttng_ust_notrace
137 unsigned int lttng_ust_lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
);
139 unsigned int lttng_ust_lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
142 * On architectures with efficient unaligned memory access, the content
143 * of the ringbuffer is packed and so the offset is always zero.
151 * lttng_ust_lib_ring_buffer_align_ctx - Align context offset on "alignment"
152 * @ctx: ring buffer context.
154 static inline lttng_ust_notrace
155 void lttng_ust_lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
158 void lttng_ust_lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
161 ctx
->buf_offset
+= lttng_ust_lib_ring_buffer_align(ctx
->buf_offset
,
165 #endif /* _LTTNG_RING_BUFFER_CONTEXT_H */