2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring buffer configuration header. Note: after declaring the standard inline
7 * functions, clients should also include linux/ringbuffer/api.h.
10 #ifndef _LTTNG_RING_BUFFER_CONFIG_H
11 #define _LTTNG_RING_BUFFER_CONFIG_H
14 #include "lttng/ust-tracer.h"
17 #include <urcu/arch.h>
19 #include "lttng/align.h"
20 #include <lttng/ust-compiler.h>
22 struct lttng_ust_lib_ring_buffer
;
24 struct lttng_ust_lib_ring_buffer_config
;
25 struct lttng_ust_lib_ring_buffer_ctx
;
26 struct lttng_ust_shm_handle
;
29 * Ring buffer client callbacks. Only used by slow path, never on fast path.
30 * For the fast path, record_header_size(), ring_buffer_clock_read() should be
31 * provided as inline functions too. These may simply return 0 if not used by
34 struct lttng_ust_lib_ring_buffer_client_cb
{
35 /* Mandatory callbacks */
37 /* A static inline version is also required for fast path */
38 uint64_t (*ring_buffer_clock_read
) (struct channel
*chan
);
39 size_t (*record_header_size
) (const struct lttng_ust_lib_ring_buffer_config
*config
,
40 struct channel
*chan
, size_t offset
,
41 size_t *pre_header_padding
,
42 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
45 /* Slow path only, at subbuffer switch */
46 size_t (*subbuffer_header_size
) (void);
47 void (*buffer_begin
) (struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
48 unsigned int subbuf_idx
,
49 struct lttng_ust_shm_handle
*handle
);
50 void (*buffer_end
) (struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
51 unsigned int subbuf_idx
, unsigned long data_size
,
52 struct lttng_ust_shm_handle
*handle
);
54 /* Optional callbacks (can be set to NULL) */
56 /* Called at buffer creation/finalize */
57 int (*buffer_create
) (struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
58 int cpu
, const char *name
,
59 struct lttng_ust_shm_handle
*handle
);
61 * Clients should guarantee that no new reader handle can be opened
64 void (*buffer_finalize
) (struct lttng_ust_lib_ring_buffer
*buf
,
66 struct lttng_ust_shm_handle
*handle
);
69 * Extract header length, payload length and timestamp from event
70 * record. Used by buffer iterators. Timestamp is only used by channel
73 void (*record_get
) (const struct lttng_ust_lib_ring_buffer_config
*config
,
74 struct channel
*chan
, struct lttng_ust_lib_ring_buffer
*buf
,
75 size_t offset
, size_t *header_len
,
76 size_t *payload_len
, uint64_t *timestamp
,
77 struct lttng_ust_shm_handle
*handle
);
79 * Offset and size of content size field in client.
81 void (*content_size_field
) (const struct lttng_ust_lib_ring_buffer_config
*config
,
82 size_t *offset
, size_t *length
);
83 void (*packet_size_field
) (const struct lttng_ust_lib_ring_buffer_config
*config
,
84 size_t *offset
, size_t *length
);
88 * Ring buffer instance configuration.
90 * Declare as "static const" within the client object to ensure the inline fast
91 * paths can be optimized.
95 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU :
96 * Per-cpu buffers with per-cpu synchronization. Tracing must be performed
97 * with preemption disabled (lib_ring_buffer_get_cpu() and
98 * lib_ring_buffer_put_cpu()).
100 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL :
101 * Per-cpu buffer with global synchronization. Tracing can be performed with
102 * preemption enabled, statistically stays on the local buffers.
104 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU :
105 * Should only be used for buffers belonging to a single thread or protected
106 * by mutual exclusion by the client. Note that periodical sub-buffer switch
107 * should be disabled in this kind of configuration.
109 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL :
110 * Global shared buffer with global synchronization.
114 * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu deferrable timers to poll the
115 * buffers and wake up readers if data is ready. Mainly useful for tracers which
116 * don't want to call into the wakeup code on the tracing path. Use in
117 * combination with "read_timer_interval" channel_create() argument.
119 * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is
120 * ready to read. Lower latencies before the reader is woken up. Mainly suitable
123 * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client
124 * has the responsibility to perform wakeups.
126 #define LTTNG_UST_RING_BUFFER_CONFIG_PADDING 20
128 enum lttng_ust_lib_ring_buffer_alloc_types
{
129 RING_BUFFER_ALLOC_PER_CPU
,
130 RING_BUFFER_ALLOC_GLOBAL
,
133 enum lttng_ust_lib_ring_buffer_sync_types
{
134 RING_BUFFER_SYNC_PER_CPU
, /* Wait-free */
135 RING_BUFFER_SYNC_GLOBAL
, /* Lock-free */
138 enum lttng_ust_lib_ring_buffer_mode_types
{
139 RING_BUFFER_OVERWRITE
= 0, /* Overwrite when buffer full */
140 RING_BUFFER_DISCARD
= 1, /* Discard when buffer full */
143 enum lttng_ust_lib_ring_buffer_output_types
{
146 RING_BUFFER_READ
, /* TODO */
147 RING_BUFFER_ITERATOR
,
151 enum lttng_ust_lib_ring_buffer_backend_types
{
153 RING_BUFFER_VMAP
, /* TODO */
154 RING_BUFFER_STATIC
, /* TODO */
157 enum lttng_ust_lib_ring_buffer_oops_types
{
158 RING_BUFFER_NO_OOPS_CONSISTENCY
,
159 RING_BUFFER_OOPS_CONSISTENCY
,
162 enum lttng_ust_lib_ring_buffer_ipi_types
{
163 RING_BUFFER_IPI_BARRIER
,
164 RING_BUFFER_NO_IPI_BARRIER
,
167 enum lttng_ust_lib_ring_buffer_wakeup_types
{
168 RING_BUFFER_WAKEUP_BY_TIMER
, /* wake up performed by timer */
169 RING_BUFFER_WAKEUP_BY_WRITER
, /*
170 * writer wakes up reader,
176 struct lttng_ust_lib_ring_buffer_config
{
177 enum lttng_ust_lib_ring_buffer_alloc_types alloc
;
178 enum lttng_ust_lib_ring_buffer_sync_types sync
;
179 enum lttng_ust_lib_ring_buffer_mode_types mode
;
180 enum lttng_ust_lib_ring_buffer_output_types output
;
181 enum lttng_ust_lib_ring_buffer_backend_types backend
;
182 enum lttng_ust_lib_ring_buffer_oops_types oops
;
183 enum lttng_ust_lib_ring_buffer_ipi_types ipi
;
184 enum lttng_ust_lib_ring_buffer_wakeup_types wakeup
;
186 * tsc_bits: timestamp bits saved at each record.
187 * 0 and 64 disable the timestamp compression scheme.
189 unsigned int tsc_bits
;
190 struct lttng_ust_lib_ring_buffer_client_cb cb
;
192 * client_type is used by the consumer process (which is in a
193 * different address space) to lookup the appropriate client
194 * callbacks and update the cb pointers.
198 const struct lttng_ust_lib_ring_buffer_client_cb
*cb_ptr
;
199 char padding
[LTTNG_UST_RING_BUFFER_CONFIG_PADDING
];
203 * ring buffer context
205 * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(),
206 * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and
207 * lib_ring_buffer_write().
209 * IMPORTANT: this structure is part of the ABI between the probe and
210 * UST. Fields need to be only added at the end, never reordered, never
213 #define LTTNG_UST_RING_BUFFER_CTX_PADDING \
214 (24 - sizeof(int) - sizeof(void *) - sizeof(void *))
215 struct lttng_ust_lib_ring_buffer_ctx
{
216 /* input received by lib_ring_buffer_reserve(), saved here. */
217 struct channel
*chan
; /* channel */
218 void *priv
; /* client private data */
219 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
220 size_t data_size
; /* size of payload */
221 int largest_align
; /*
222 * alignment of the largest element
225 int cpu
; /* processor id */
227 /* output from lib_ring_buffer_reserve() */
228 struct lttng_ust_lib_ring_buffer
*buf
; /*
229 * buffer corresponding to processor id
232 size_t slot_size
; /* size of the reserved slot */
233 unsigned long buf_offset
; /* offset following the record header */
234 unsigned long pre_offset
; /*
235 * Initial offset position _before_
236 * the record is written. Positioned
237 * prior to record header alignment
240 uint64_t tsc
; /* time-stamp counter value */
241 unsigned int rflags
; /* reservation flags */
243 * The field ctx_len is the length of struct
244 * lttng_ust_lib_ring_buffer_ctx as known by the user of
245 * lib_ring_buffer_ctx_init.
247 unsigned int ctx_len
;
248 void *ip
; /* caller ip address */
249 void *priv2
; /* 2nd priv data */
250 char padding2
[LTTNG_UST_RING_BUFFER_CTX_PADDING
];
252 * This is the end of the initial fields expected by the original ABI
253 * between probes and UST. Only the fields above can be used if
254 * ctx_len is 0. Use the value of ctx_len to find out which of the
255 * following fields may be used.
257 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
261 * lib_ring_buffer_ctx_init - initialize ring buffer context
262 * @ctx: ring buffer context to initialize
264 * @priv: client private data
265 * @data_size: size of record data payload
266 * @largest_align: largest alignment within data payload types
269 static inline lttng_ust_notrace
270 void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
271 struct channel
*chan
, void *priv
,
272 size_t data_size
, int largest_align
,
273 int cpu
, struct lttng_ust_shm_handle
*handle
,
276 void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
277 struct channel
*chan
, void *priv
,
278 size_t data_size
, int largest_align
,
279 int cpu
, struct lttng_ust_shm_handle
*handle
,
284 ctx
->data_size
= data_size
;
285 ctx
->largest_align
= largest_align
;
288 ctx
->handle
= handle
;
289 ctx
->ctx_len
= sizeof(struct lttng_ust_lib_ring_buffer_ctx
);
292 memset(ctx
->padding2
, 0, LTTNG_UST_RING_BUFFER_CTX_PADDING
);
298 * RING_BUFFER_RFLAG_FULL_TSC
300 * This flag is passed to record_header_size() and to the primitive used to
301 * write the record header. It indicates that the full 64-bit time value is
302 * needed in the record header. If this flag is not set, the record header needs
303 * only to contain "tsc_bits" bit of time value.
305 * Reservation flags can be added by the client, starting from
306 * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from
307 * record_header_size() to lib_ring_buffer_write_record_header().
309 #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0)
310 #define RING_BUFFER_RFLAG_END (1U << 1)
313 * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at
314 * compile-time. We have to duplicate the "config->align" information and the
315 * definition here because config->align is used both in the slow and fast
316 * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code.
318 #ifdef RING_BUFFER_ALIGN
320 # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */
323 * Calculate the offset needed to align the type.
324 * size_of_type must be non-zero.
326 static inline lttng_ust_notrace
327 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
);
329 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
331 return lttng_ust_offset_align(align_drift
, size_of_type
);
336 # define RING_BUFFER_ALIGN_ATTR __attribute__((packed))
339 * Calculate the offset needed to align the type.
340 * size_of_type must be non-zero.
342 static inline lttng_ust_notrace
343 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
);
345 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
353 * lib_ring_buffer_align_ctx - Align context offset on "alignment"
354 * @ctx: ring buffer context.
356 static inline lttng_ust_notrace
357 void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
360 void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
363 ctx
->buf_offset
+= lib_ring_buffer_align(ctx
->buf_offset
,
368 * lib_ring_buffer_check_config() returns 0 on success.
369 * Used internally to check for valid configurations at channel creation.
371 static inline lttng_ust_notrace
372 int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config
*config
,
373 unsigned int switch_timer_interval
,
374 unsigned int read_timer_interval
);
376 int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config
*config
,
377 unsigned int switch_timer_interval
,
378 unsigned int read_timer_interval
)
380 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
381 && config
->sync
== RING_BUFFER_SYNC_PER_CPU
382 && switch_timer_interval
)
387 #endif /* _LTTNG_RING_BUFFER_CONFIG_H */