1 #ifndef _LIB_RING_BUFFER_CONFIG_H
2 #define _LIB_RING_BUFFER_CONFIG_H
5 * lib/ringbuffer/config.h
7 * Ring buffer configuration header. Note: after declaring the standard inline
8 * functions, clients should also include linux/ringbuffer/api.h.
10 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; only
15 * version 2.1 of the License.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <linux/types.h>
28 #include <linux/percpu.h>
30 #include "../../lttng-tracer-core.h"
32 struct lib_ring_buffer
;
34 struct lib_ring_buffer_config
;
35 struct lib_ring_buffer_ctx
;
38 * Ring buffer client callbacks. Only used by slow path, never on fast path.
39 * For the fast path, record_header_size(), ring_buffer_clock_read() should be
40 * provided as inline functions too. These may simply return 0 if not used by
43 struct lib_ring_buffer_client_cb
{
44 /* Mandatory callbacks */
46 /* A static inline version is also required for fast path */
47 u64 (*ring_buffer_clock_read
) (struct channel
*chan
);
48 size_t (*record_header_size
) (const struct lib_ring_buffer_config
*config
,
49 struct channel
*chan
, size_t offset
,
50 size_t *pre_header_padding
,
51 struct lib_ring_buffer_ctx
*ctx
);
53 /* Slow path only, at subbuffer switch */
54 size_t (*subbuffer_header_size
) (void);
55 void (*buffer_begin
) (struct lib_ring_buffer
*buf
, u64 tsc
,
56 unsigned int subbuf_idx
);
57 void (*buffer_end
) (struct lib_ring_buffer
*buf
, u64 tsc
,
58 unsigned int subbuf_idx
, unsigned long data_size
);
60 /* Optional callbacks (can be set to NULL) */
62 /* Called at buffer creation/finalize */
63 int (*buffer_create
) (struct lib_ring_buffer
*buf
, void *priv
,
64 int cpu
, const char *name
);
66 * Clients should guarantee that no new reader handle can be opened
69 void (*buffer_finalize
) (struct lib_ring_buffer
*buf
, void *priv
, int cpu
);
72 * Extract header length, payload length and timestamp from event
73 * record. Used by buffer iterators. Timestamp is only used by channel
76 void (*record_get
) (const struct lib_ring_buffer_config
*config
,
77 struct channel
*chan
, struct lib_ring_buffer
*buf
,
78 size_t offset
, size_t *header_len
,
79 size_t *payload_len
, u64
*timestamp
);
83 * Ring buffer instance configuration.
85 * Declare as "static const" within the client object to ensure the inline fast
86 * paths can be optimized.
90 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU :
91 * Per-cpu buffers with per-cpu synchronization. Tracing must be performed
92 * with preemption disabled (lib_ring_buffer_get_cpu() and
93 * lib_ring_buffer_put_cpu()).
95 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL :
96 * Per-cpu buffer with global synchronization. Tracing can be performed with
97 * preemption enabled, statistically stays on the local buffers.
99 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU :
100 * Should only be used for buffers belonging to a single thread or protected
101 * by mutual exclusion by the client. Note that periodical sub-buffer switch
102 * should be disabled in this kind of configuration.
104 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL :
105 * Global shared buffer with global synchronization.
109 * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu deferrable timers to poll the
110 * buffers and wake up readers if data is ready. Mainly useful for tracers which
111 * don't want to call into the wakeup code on the tracing path. Use in
112 * combination with "read_timer_interval" channel_create() argument.
114 * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is
115 * ready to read. Lower latencies before the reader is woken up. Mainly suitable
118 * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client
119 * has the responsibility to perform wakeups.
121 struct lib_ring_buffer_config
{
123 RING_BUFFER_ALLOC_PER_CPU
,
124 RING_BUFFER_ALLOC_GLOBAL
,
127 RING_BUFFER_SYNC_PER_CPU
, /* Wait-free */
128 RING_BUFFER_SYNC_GLOBAL
, /* Lock-free */
131 RING_BUFFER_OVERWRITE
, /* Overwrite when buffer full */
132 RING_BUFFER_DISCARD
, /* Discard when buffer full */
137 RING_BUFFER_READ
, /* TODO */
138 RING_BUFFER_ITERATOR
,
143 RING_BUFFER_VMAP
, /* TODO */
144 RING_BUFFER_STATIC
, /* TODO */
147 RING_BUFFER_NO_OOPS_CONSISTENCY
,
148 RING_BUFFER_OOPS_CONSISTENCY
,
151 RING_BUFFER_IPI_BARRIER
,
152 RING_BUFFER_NO_IPI_BARRIER
,
155 RING_BUFFER_WAKEUP_BY_TIMER
, /* wake up performed by timer */
156 RING_BUFFER_WAKEUP_BY_WRITER
, /*
157 * writer wakes up reader,
163 * tsc_bits: timestamp bits saved at each record.
164 * 0 and 64 disable the timestamp compression scheme.
166 unsigned int tsc_bits
;
167 struct lib_ring_buffer_client_cb cb
;
171 * ring buffer context
173 * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(),
174 * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and
175 * lib_ring_buffer_write().
177 struct lib_ring_buffer_ctx
{
178 /* input received by lib_ring_buffer_reserve(), saved here. */
179 struct channel
*chan
; /* channel */
180 void *priv
; /* client private data */
181 size_t data_size
; /* size of payload */
182 int largest_align
; /*
183 * alignment of the largest element
186 int cpu
; /* processor id */
188 /* output from lib_ring_buffer_reserve() */
189 struct lib_ring_buffer
*buf
; /*
190 * buffer corresponding to processor id
193 size_t slot_size
; /* size of the reserved slot */
194 unsigned long buf_offset
; /* offset following the record header */
195 unsigned long pre_offset
; /*
196 * Initial offset position _before_
197 * the record is written. Positioned
198 * prior to record header alignment
201 u64 tsc
; /* time-stamp counter value */
202 unsigned int rflags
; /* reservation flags */
206 * lib_ring_buffer_ctx_init - initialize ring buffer context
207 * @ctx: ring buffer context to initialize
209 * @priv: client private data
210 * @data_size: size of record data payload
211 * @largest_align: largest alignment within data payload types
215 void lib_ring_buffer_ctx_init(struct lib_ring_buffer_ctx
*ctx
,
216 struct channel
*chan
, void *priv
,
217 size_t data_size
, int largest_align
,
222 ctx
->data_size
= data_size
;
223 ctx
->largest_align
= largest_align
;
231 * RING_BUFFER_RFLAG_FULL_TSC
233 * This flag is passed to record_header_size() and to the primitive used to
234 * write the record header. It indicates that the full 64-bit time value is
235 * needed in the record header. If this flag is not set, the record header needs
236 * only to contain "tsc_bits" bit of time value.
238 * Reservation flags can be added by the client, starting from
239 * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from
240 * record_header_size() to lib_ring_buffer_write_record_header().
242 #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0)
243 #define RING_BUFFER_RFLAG_END (1U << 1)
245 #ifndef LTTNG_TRACER_CORE_H
246 #error "lttng-tracer-core.h is needed for RING_BUFFER_ALIGN define"
250 * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at
251 * compile-time. We have to duplicate the "config->align" information and the
252 * definition here because config->align is used both in the slow and fast
253 * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code.
255 #ifdef RING_BUFFER_ALIGN
257 # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */
260 * Calculate the offset needed to align the type.
261 * size_of_type must be non-zero.
264 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
266 return offset_align(align_drift
, size_of_type
);
271 # define RING_BUFFER_ALIGN_ATTR __attribute__((packed))
274 * Calculate the offset needed to align the type.
275 * size_of_type must be non-zero.
278 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
286 * lib_ring_buffer_align_ctx - Align context offset on "alignment"
287 * @ctx: ring buffer context.
290 void lib_ring_buffer_align_ctx(struct lib_ring_buffer_ctx
*ctx
,
293 ctx
->buf_offset
+= lib_ring_buffer_align(ctx
->buf_offset
,
298 * lib_ring_buffer_check_config() returns 0 on success.
299 * Used internally to check for valid configurations at channel creation.
302 int lib_ring_buffer_check_config(const struct lib_ring_buffer_config
*config
,
303 unsigned int switch_timer_interval
,
304 unsigned int read_timer_interval
)
306 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
307 && config
->sync
== RING_BUFFER_SYNC_PER_CPU
308 && switch_timer_interval
)
313 #include "../../wrapper/ringbuffer/vatomic.h"
315 #endif /* _LIB_RING_BUFFER_CONFIG_H */