1 #ifndef _LINUX_RING_BUFFER_CONFIG_H
2 #define _LINUX_RING_BUFFER_CONFIG_H
5 * linux/ringbuffer/config.h
7 * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Ring buffer configuration header. Note: after declaring the standard inline
10 * functions, clients should also include linux/ringbuffer/api.h.
12 * Dual LGPL v2.1/GPL v2 license.
16 #include "ust/lttng-tracer.h"
17 #include "ust/usterr-signal-safe.h"
18 #include "ust/kcompat/kcompat.h"
19 #include "ust/align.h"
21 struct lttng_ust_lib_ring_buffer
;
23 struct lttng_ust_lib_ring_buffer_config
;
24 struct lttng_ust_lib_ring_buffer_ctx
;
25 struct lttng_ust_shm_handle
*handle
;
28 * Ring buffer client callbacks. Only used by slow path, never on fast path.
29 * For the fast path, record_header_size(), ring_buffer_clock_read() should be
30 * provided as inline functions too. These may simply return 0 if not used by
33 struct lttng_ust_lib_ring_buffer_client_cb
{
34 /* Mandatory callbacks */
36 /* A static inline version is also required for fast path */
37 u64 (*ring_buffer_clock_read
) (struct channel
*chan
);
38 size_t (*record_header_size
) (const struct lttng_ust_lib_ring_buffer_config
*config
,
39 struct channel
*chan
, size_t offset
,
40 size_t *pre_header_padding
,
41 struct lttng_ust_lib_ring_buffer_ctx
*ctx
);
43 /* Slow path only, at subbuffer switch */
44 size_t (*subbuffer_header_size
) (void);
45 void (*buffer_begin
) (struct lttng_ust_lib_ring_buffer
*buf
, u64 tsc
,
46 unsigned int subbuf_idx
,
47 struct lttng_ust_shm_handle
*handle
);
48 void (*buffer_end
) (struct lttng_ust_lib_ring_buffer
*buf
, u64 tsc
,
49 unsigned int subbuf_idx
, unsigned long data_size
,
50 struct lttng_ust_shm_handle
*handle
);
52 /* Optional callbacks (can be set to NULL) */
54 /* Called at buffer creation/finalize */
55 int (*buffer_create
) (struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
56 int cpu
, const char *name
,
57 struct lttng_ust_shm_handle
*handle
);
59 * Clients should guarantee that no new reader handle can be opened
62 void (*buffer_finalize
) (struct lttng_ust_lib_ring_buffer
*buf
,
64 struct lttng_ust_shm_handle
*handle
);
67 * Extract header length, payload length and timestamp from event
68 * record. Used by buffer iterators. Timestamp is only used by channel
71 void (*record_get
) (const struct lttng_ust_lib_ring_buffer_config
*config
,
72 struct channel
*chan
, struct lttng_ust_lib_ring_buffer
*buf
,
73 size_t offset
, size_t *header_len
,
74 size_t *payload_len
, u64
*timestamp
,
75 struct lttng_ust_shm_handle
*handle
);
79 * Ring buffer instance configuration.
81 * Declare as "static const" within the client object to ensure the inline fast
82 * paths can be optimized.
86 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU :
87 * Per-cpu buffers with per-cpu synchronization. Tracing must be performed
88 * with preemption disabled (lib_ring_buffer_get_cpu() and
89 * lib_ring_buffer_put_cpu()).
91 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL :
92 * Per-cpu buffer with global synchronization. Tracing can be performed with
93 * preemption enabled, statistically stays on the local buffers.
95 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU :
96 * Should only be used for buffers belonging to a single thread or protected
97 * by mutual exclusion by the client. Note that periodical sub-buffer switch
98 * should be disabled in this kind of configuration.
100 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL :
101 * Global shared buffer with global synchronization.
105 * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu deferrable timers to poll the
106 * buffers and wake up readers if data is ready. Mainly useful for tracers which
107 * don't want to call into the wakeup code on the tracing path. Use in
108 * combination with "read_timer_interval" channel_create() argument.
110 * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is
111 * ready to read. Lower latencies before the reader is woken up. Mainly suitable
114 * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client
115 * has the responsibility to perform wakeups.
117 struct lttng_ust_lib_ring_buffer_config
{
119 RING_BUFFER_ALLOC_PER_CPU
,
120 RING_BUFFER_ALLOC_GLOBAL
,
123 RING_BUFFER_SYNC_PER_CPU
, /* Wait-free */
124 RING_BUFFER_SYNC_GLOBAL
, /* Lock-free */
127 RING_BUFFER_OVERWRITE
, /* Overwrite when buffer full */
128 RING_BUFFER_DISCARD
, /* Discard when buffer full */
133 RING_BUFFER_READ
, /* TODO */
134 RING_BUFFER_ITERATOR
,
139 RING_BUFFER_VMAP
, /* TODO */
140 RING_BUFFER_STATIC
, /* TODO */
143 RING_BUFFER_NO_OOPS_CONSISTENCY
,
144 RING_BUFFER_OOPS_CONSISTENCY
,
147 RING_BUFFER_IPI_BARRIER
,
148 RING_BUFFER_NO_IPI_BARRIER
,
151 RING_BUFFER_WAKEUP_BY_TIMER
, /* wake up performed by timer */
152 RING_BUFFER_WAKEUP_BY_WRITER
, /*
153 * writer wakes up reader,
159 * tsc_bits: timestamp bits saved at each record.
160 * 0 and 64 disable the timestamp compression scheme.
162 unsigned int tsc_bits
;
163 struct lttng_ust_lib_ring_buffer_client_cb cb
;
167 * ring buffer context
169 * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(),
170 * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and
171 * lib_ring_buffer_write().
173 struct lttng_ust_lib_ring_buffer_ctx
{
174 /* input received by lib_ring_buffer_reserve(), saved here. */
175 struct channel
*chan
; /* channel */
176 void *priv
; /* client private data */
177 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
178 size_t data_size
; /* size of payload */
179 int largest_align
; /*
180 * alignment of the largest element
183 int cpu
; /* processor id */
185 /* output from lib_ring_buffer_reserve() */
186 struct lttng_ust_lib_ring_buffer
*buf
; /*
187 * buffer corresponding to processor id
190 size_t slot_size
; /* size of the reserved slot */
191 unsigned long buf_offset
; /* offset following the record header */
192 unsigned long pre_offset
; /*
193 * Initial offset position _before_
194 * the record is written. Positioned
195 * prior to record header alignment
198 u64 tsc
; /* time-stamp counter value */
199 unsigned int rflags
; /* reservation flags */
203 * lib_ring_buffer_ctx_init - initialize ring buffer context
204 * @ctx: ring buffer context to initialize
206 * @priv: client private data
207 * @data_size: size of record data payload
208 * @largest_align: largest alignment within data payload types
212 void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
213 struct channel
*chan
, void *priv
,
214 size_t data_size
, int largest_align
,
215 int cpu
, struct lttng_ust_shm_handle
*handle
)
219 ctx
->data_size
= data_size
;
220 ctx
->largest_align
= largest_align
;
223 ctx
->handle
= handle
;
229 * RING_BUFFER_RFLAG_FULL_TSC
231 * This flag is passed to record_header_size() and to the primitive used to
232 * write the record header. It indicates that the full 64-bit time value is
233 * needed in the record header. If this flag is not set, the record header needs
234 * only to contain "tsc_bits" bit of time value.
236 * Reservation flags can be added by the client, starting from
237 * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from
238 * record_header_size() to lib_ring_buffer_write_record_header().
240 #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0)
241 #define RING_BUFFER_RFLAG_END (1U << 1)
244 * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at
245 * compile-time. We have to duplicate the "config->align" information and the
246 * definition here because config->align is used both in the slow and fast
247 * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code.
249 #ifdef RING_BUFFER_ALIGN
251 # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */
254 * Calculate the offset needed to align the type.
255 * size_of_type must be non-zero.
258 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
260 return offset_align(align_drift
, size_of_type
);
265 # define RING_BUFFER_ALIGN_ATTR __attribute__((packed))
268 * Calculate the offset needed to align the type.
269 * size_of_type must be non-zero.
272 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
280 * lib_ring_buffer_align_ctx - Align context offset on "alignment"
281 * @ctx: ring buffer context.
284 void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
287 ctx
->buf_offset
+= lib_ring_buffer_align(ctx
->buf_offset
,
292 * lib_ring_buffer_check_config() returns 0 on success.
293 * Used internally to check for valid configurations at channel creation.
296 int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config
*config
,
297 unsigned int switch_timer_interval
,
298 unsigned int read_timer_interval
)
300 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
301 && config
->sync
== RING_BUFFER_SYNC_PER_CPU
302 && switch_timer_interval
)
307 #include <ust/vatomic.h>
309 #endif /* _LINUX_RING_BUFFER_CONFIG_H */