2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring Buffer Library Synchronization Header (types).
8 * See ring_buffer_frontend.c for more information on wait-free algorithms.
11 #ifndef _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
12 #define _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
16 #include <time.h> /* for timer_t */
18 #include <urcu/list.h>
19 #include <urcu/uatomic.h>
21 #include <lttng/ust-ringbuffer-context.h>
22 #include "ringbuffer-config.h"
23 #include "common/logging.h"
24 #include "backend_types.h"
25 #include "shm_internal.h"
26 #include "shm_types.h"
29 #define LIB_RING_BUFFER_MAX_NESTING 5
32 * A switch is done during tracing or as a final flush after tracing (so it
33 * won't write in the new sub-buffer).
35 enum switch_mode
{ SWITCH_ACTIVE
, SWITCH_FLUSH
};
37 /* channel: collection of per-cpu ring buffers. */
38 #define RB_CHANNEL_PADDING 32
39 struct lttng_ust_ring_buffer_channel
{
41 unsigned long commit_count_mask
; /*
42 * Commit count mask, removing
43 * the MSBs corresponding to
44 * bits used to represent the
48 unsigned long switch_timer_interval
; /* Buffer flush (us) */
50 int switch_timer_enabled
;
52 unsigned long read_timer_interval
; /* Reader wakeup (us) */
54 int read_timer_enabled
;
56 int finalized
; /* Has channel been finalized */
57 size_t priv_data_offset
; /* Offset of private data channel config */
58 unsigned int nr_streams
; /* Number of streams */
59 struct lttng_ust_shm_handle
*handle
;
60 /* Extended options. */
63 int32_t blocking_timeout_ms
;
64 void *priv
; /* Private data pointer. */
66 char padding
[RB_CHANNEL_PADDING
];
69 * Associated backend contains a variable-length array. Needs to
72 struct channel_backend backend
; /* Associated backend */
73 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
75 /* Per-subbuffer commit counters used on the hot path */
76 #define RB_COMMIT_COUNT_HOT_PADDING 16
77 struct commit_counters_hot
{
78 union v_atomic cc
; /* Commit counter */
79 union v_atomic seq
; /* Consecutive commits */
80 char padding
[RB_COMMIT_COUNT_HOT_PADDING
];
81 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
83 /* Per-subbuffer commit counters used only on cold paths */
84 #define RB_COMMIT_COUNT_COLD_PADDING 24
85 struct commit_counters_cold
{
86 union v_atomic cc_sb
; /* Incremented _once_ at sb switch */
87 char padding
[RB_COMMIT_COUNT_COLD_PADDING
];
88 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
90 /* ring buffer state */
91 #define RB_CRASH_DUMP_ABI_LEN 256
92 #define RB_RING_BUFFER_PADDING 60
94 #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
97 * The 128-bit magic number is xor'd in the process data so it does not
98 * cause a false positive when searching for buffers by scanning memory.
99 * The actual magic number is:
100 * 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, 0x7B, 0xF1,
101 * 0x77, 0xBF, 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17,
103 #define RB_CRASH_DUMP_ABI_MAGIC_XOR \
105 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, \
106 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, \
107 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, \
108 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
111 #define RB_CRASH_ENDIAN 0x1234
113 #define RB_CRASH_DUMP_ABI_MAJOR 0
114 #define RB_CRASH_DUMP_ABI_MINOR 0
116 enum lttng_crash_type
{
117 LTTNG_CRASH_TYPE_UST
= 0,
118 LTTNG_CRASH_TYPE_KERNEL
= 1,
121 struct lttng_crash_abi
{
122 uint8_t magic
[RB_CRASH_DUMP_ABI_MAGIC_LEN
];
123 uint64_t mmap_length
; /* Overall length of crash record */
125 * { 0x12, 0x34 }: big endian
126 * { 0x34, 0x12 }: little endian
128 uint16_t major
; /* Major number. */
129 uint16_t minor
; /* Minor number. */
130 uint8_t word_size
; /* Word size (bytes). */
131 uint8_t layout_type
; /* enum lttng_crash_type */
134 uint32_t prod_offset
;
135 uint32_t consumed_offset
;
136 uint32_t commit_hot_array
;
137 uint32_t commit_hot_seq
;
138 uint32_t buf_wsb_array
;
141 uint32_t sb_array_shmp_offset
;
142 uint32_t sb_backend_p_offset
;
143 uint32_t content_size
;
144 uint32_t packet_size
;
145 } __attribute__((packed
)) offset
;
148 uint8_t consumed_offset
;
149 uint8_t commit_hot_seq
;
151 uint8_t sb_array_shmp_offset
;
152 uint8_t sb_backend_p_offset
;
153 uint8_t content_size
;
155 } __attribute__((packed
)) length
;
157 uint32_t commit_hot_array
;
158 uint32_t buf_wsb_array
;
160 } __attribute__((packed
)) stride
;
162 uint64_t buf_size
; /* Size of the buffer */
163 uint64_t subbuf_size
; /* Sub-buffer size */
164 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
165 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
166 } __attribute__((packed
));
168 struct lttng_ust_ring_buffer
{
169 /* First 32 bytes are for the buffer crash dump ABI */
170 struct lttng_crash_abi crash_abi
;
172 /* 32 bytes cache-hot cacheline */
173 union v_atomic
__attribute__((aligned(32))) offset
;
174 /* Current offset in the buffer */
175 DECLARE_SHMP(struct commit_counters_hot
, commit_hot
);
176 /* Commit count per sub-buffer */
178 * Current offset in the buffer
179 * standard atomic access (shared)
182 /* End of cache-hot 32 bytes cacheline */
184 union v_atomic last_tsc
; /*
185 * Last timestamp written in the buffer.
188 struct lttng_ust_ring_buffer_backend backend
;
189 /* Associated backend */
191 DECLARE_SHMP(struct commit_counters_cold
, commit_cold
);
192 /* Commit count per sub-buffer */
193 DECLARE_SHMP(uint64_t, ts_end
); /*
194 * timestamp_end per sub-buffer.
195 * Time is sampled by the
196 * switch_*_end() callbacks
197 * which are the last space
198 * reservation performed in the
199 * sub-buffer before it can be
200 * fully committed and
201 * delivered. This time value is
202 * then read by the deliver
203 * callback, performed by the
204 * last commit before the buffer
207 long active_readers
; /*
208 * Active readers count
209 * standard atomic access (shared)
211 /* Dropped records */
212 union v_atomic records_lost_full
; /* Buffer full */
213 union v_atomic records_lost_wrap
; /* Nested wrap-around */
214 union v_atomic records_lost_big
; /* Events too big */
215 union v_atomic records_count
; /* Number of records written */
216 union v_atomic records_overrun
; /* Number of overwritten records */
217 //wait_queue_head_t read_wait; /* reader buffer-level wait queue */
218 int finalized
; /* buffer has been finalized */
219 unsigned long get_subbuf_consumed
; /* Read-side consumed */
220 unsigned long prod_snapshot
; /* Producer count snapshot */
221 unsigned long cons_snapshot
; /* Consumer count snapshot */
222 unsigned int get_subbuf
:1; /* Sub-buffer being held by reader */
223 /* shmp pointer to self */
224 DECLARE_SHMP(struct lttng_ust_ring_buffer
, self
);
225 char padding
[RB_RING_BUFFER_PADDING
];
226 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
229 * ring buffer private context
231 * Private context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(),
232 * lib_ring_buffer_try_discard_reserve(), lttng_ust_ring_buffer_align_ctx() and
233 * lib_ring_buffer_write().
235 * This context is allocated on an internal shadow-stack by a successful reserve
236 * operation, used by align/write, and freed by commit.
239 struct lttng_ust_ring_buffer_ctx_private
{
240 /* input received by lib_ring_buffer_reserve(). */
241 struct lttng_ust_ring_buffer_ctx
*pub
;
242 struct lttng_ust_ring_buffer_channel
*chan
; /* channel */
244 /* output from lib_ring_buffer_reserve() */
245 int reserve_cpu
; /* processor id updated by the reserve */
246 size_t slot_size
; /* size of the reserved slot */
247 unsigned long buf_offset
; /* offset following the record header */
248 unsigned long pre_offset
; /*
249 * Initial offset position _before_
250 * the record is written. Positioned
251 * prior to record header alignment
254 uint64_t tsc
; /* time-stamp counter value */
255 unsigned int rflags
; /* reservation flags */
257 struct lttng_ust_ring_buffer
*buf
; /*
258 * buffer corresponding to processor id
261 struct lttng_ust_ring_buffer_backend_pages
*backend_pages
;
265 void *channel_get_private_config(struct lttng_ust_ring_buffer_channel
*chan
)
267 return ((char *) chan
) + chan
->priv_data_offset
;
271 void *channel_get_private(struct lttng_ust_ring_buffer_channel
*chan
)
273 return chan
->u
.s
.priv
;
277 void channel_set_private(struct lttng_ust_ring_buffer_channel
*chan
, void *priv
)
279 chan
->u
.s
.priv
= priv
;
282 #ifndef __rb_same_type
283 #define __rb_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
287 * Issue warnings and disable channels upon internal error.
288 * Can receive struct lttng_ust_ring_buffer or struct lttng_ust_ring_buffer_backend
291 #define CHAN_WARN_ON(c, cond) \
293 struct lttng_ust_ring_buffer_channel *__chan; \
294 int _____ret = caa_unlikely(cond); \
296 if (__rb_same_type(*(c), struct channel_backend)) \
297 __chan = caa_container_of((void *) (c), \
298 struct lttng_ust_ring_buffer_channel, \
300 else if (__rb_same_type(*(c), \
301 struct lttng_ust_ring_buffer_channel)) \
302 __chan = (void *) (c); \
305 uatomic_inc(&__chan->record_disabled); \
308 _____ret = _____ret; /* For clang "unused result". */ \
312 * lttng_ust_ring_buffer_align_ctx - Align context offset on "alignment"
313 * @ctx: ring buffer context.
316 void lttng_ust_ring_buffer_align_ctx(struct lttng_ust_ring_buffer_ctx
*ctx
,
320 void lttng_ust_ring_buffer_align_ctx(struct lttng_ust_ring_buffer_ctx
*ctx
,
323 struct lttng_ust_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
325 ctx_private
->buf_offset
+= lttng_ust_ring_buffer_align(ctx_private
->buf_offset
,
329 #endif /* _LTTNG_RING_BUFFER_FRONTEND_TYPES_H */