1 #ifndef _LTTNG_RING_BUFFER_CONFIG_H
2 #define _LTTNG_RING_BUFFER_CONFIG_H
5 * lttng/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 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
24 #include "lttng/ust-tracer.h"
27 #include <urcu/arch.h>
29 #include "lttng/align.h"
30 #include <lttng/ust-compiler.h>
32 struct lttng_ust_lib_ring_buffer
;
34 struct lttng_ust_lib_ring_buffer_config
;
35 struct lttng_ust_lib_ring_buffer_ctx
;
36 struct lttng_ust_shm_handle
;
39 * Ring buffer client callbacks. Only used by slow path, never on fast path.
40 * For the fast path, record_header_size(), ring_buffer_clock_read() should be
41 * provided as inline functions too. These may simply return 0 if not used by
44 struct lttng_ust_lib_ring_buffer_client_cb
{
45 /* Mandatory callbacks */
47 /* A static inline version is also required for fast path */
48 uint64_t (*ring_buffer_clock_read
) (struct channel
*chan
);
49 size_t (*record_header_size
) (const struct lttng_ust_lib_ring_buffer_config
*config
,
50 struct channel
*chan
, size_t offset
,
51 size_t *pre_header_padding
,
52 struct lttng_ust_lib_ring_buffer_ctx
*ctx
);
54 /* Slow path only, at subbuffer switch */
55 size_t (*subbuffer_header_size
) (void);
56 void (*buffer_begin
) (struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
57 unsigned int subbuf_idx
,
58 struct lttng_ust_shm_handle
*handle
);
59 void (*buffer_end
) (struct lttng_ust_lib_ring_buffer
*buf
, uint64_t tsc
,
60 unsigned int subbuf_idx
, unsigned long data_size
,
61 struct lttng_ust_shm_handle
*handle
);
63 /* Optional callbacks (can be set to NULL) */
65 /* Called at buffer creation/finalize */
66 int (*buffer_create
) (struct lttng_ust_lib_ring_buffer
*buf
, void *priv
,
67 int cpu
, const char *name
,
68 struct lttng_ust_shm_handle
*handle
);
70 * Clients should guarantee that no new reader handle can be opened
73 void (*buffer_finalize
) (struct lttng_ust_lib_ring_buffer
*buf
,
75 struct lttng_ust_shm_handle
*handle
);
78 * Extract header length, payload length and timestamp from event
79 * record. Used by buffer iterators. Timestamp is only used by channel
82 void (*record_get
) (const struct lttng_ust_lib_ring_buffer_config
*config
,
83 struct channel
*chan
, struct lttng_ust_lib_ring_buffer
*buf
,
84 size_t offset
, size_t *header_len
,
85 size_t *payload_len
, uint64_t *timestamp
,
86 struct lttng_ust_shm_handle
*handle
);
90 * Ring buffer instance configuration.
92 * Declare as "static const" within the client object to ensure the inline fast
93 * paths can be optimized.
97 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU :
98 * Per-cpu buffers with per-cpu synchronization. Tracing must be performed
99 * with preemption disabled (lib_ring_buffer_get_cpu() and
100 * lib_ring_buffer_put_cpu()).
102 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL :
103 * Per-cpu buffer with global synchronization. Tracing can be performed with
104 * preemption enabled, statistically stays on the local buffers.
106 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU :
107 * Should only be used for buffers belonging to a single thread or protected
108 * by mutual exclusion by the client. Note that periodical sub-buffer switch
109 * should be disabled in this kind of configuration.
111 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL :
112 * Global shared buffer with global synchronization.
116 * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu deferrable timers to poll the
117 * buffers and wake up readers if data is ready. Mainly useful for tracers which
118 * don't want to call into the wakeup code on the tracing path. Use in
119 * combination with "read_timer_interval" channel_create() argument.
121 * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is
122 * ready to read. Lower latencies before the reader is woken up. Mainly suitable
125 * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client
126 * has the responsibility to perform wakeups.
128 #define LTTNG_UST_RING_BUFFER_CONFIG_PADDING 32
129 struct lttng_ust_lib_ring_buffer_config
{
131 RING_BUFFER_ALLOC_PER_CPU
,
132 RING_BUFFER_ALLOC_GLOBAL
,
135 RING_BUFFER_SYNC_PER_CPU
, /* Wait-free */
136 RING_BUFFER_SYNC_GLOBAL
, /* Lock-free */
139 RING_BUFFER_OVERWRITE
, /* Overwrite when buffer full */
140 RING_BUFFER_DISCARD
, /* Discard when buffer full */
145 RING_BUFFER_READ
, /* TODO */
146 RING_BUFFER_ITERATOR
,
151 RING_BUFFER_VMAP
, /* TODO */
152 RING_BUFFER_STATIC
, /* TODO */
155 RING_BUFFER_NO_OOPS_CONSISTENCY
,
156 RING_BUFFER_OOPS_CONSISTENCY
,
159 RING_BUFFER_IPI_BARRIER
,
160 RING_BUFFER_NO_IPI_BARRIER
,
163 RING_BUFFER_WAKEUP_BY_TIMER
, /* wake up performed by timer */
164 RING_BUFFER_WAKEUP_BY_WRITER
, /*
165 * writer wakes up reader,
171 * tsc_bits: timestamp bits saved at each record.
172 * 0 and 64 disable the timestamp compression scheme.
174 unsigned int tsc_bits
;
175 struct lttng_ust_lib_ring_buffer_client_cb cb
;
177 * client_type is used by the consumer process (which is in a
178 * different address space) to lookup the appropriate client
179 * callbacks and update the cb pointers.
182 char padding
[LTTNG_UST_RING_BUFFER_CONFIG_PADDING
];
186 * ring buffer context
188 * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(),
189 * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and
190 * lib_ring_buffer_write().
192 * IMPORTANT: this structure is part of the ABI between the probe and
193 * UST. Fields need to be only added at the end, never reordered, never
196 #define LTTNG_UST_RING_BUFFER_CTX_PADDING 24
197 struct lttng_ust_lib_ring_buffer_ctx
{
198 /* input received by lib_ring_buffer_reserve(), saved here. */
199 struct channel
*chan
; /* channel */
200 void *priv
; /* client private data */
201 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
202 size_t data_size
; /* size of payload */
203 int largest_align
; /*
204 * alignment of the largest element
207 int cpu
; /* processor id */
209 /* output from lib_ring_buffer_reserve() */
210 struct lttng_ust_lib_ring_buffer
*buf
; /*
211 * buffer corresponding to processor id
214 size_t slot_size
; /* size of the reserved slot */
215 unsigned long buf_offset
; /* offset following the record header */
216 unsigned long pre_offset
; /*
217 * Initial offset position _before_
218 * the record is written. Positioned
219 * prior to record header alignment
222 uint64_t tsc
; /* time-stamp counter value */
223 unsigned int rflags
; /* reservation flags */
224 char padding
[LTTNG_UST_RING_BUFFER_CTX_PADDING
];
228 * lib_ring_buffer_ctx_init - initialize ring buffer context
229 * @ctx: ring buffer context to initialize
231 * @priv: client private data
232 * @data_size: size of record data payload
233 * @largest_align: largest alignment within data payload types
236 static inline lttng_ust_notrace
237 void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
238 struct channel
*chan
, void *priv
,
239 size_t data_size
, int largest_align
,
240 int cpu
, struct lttng_ust_shm_handle
*handle
);
242 void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
243 struct channel
*chan
, void *priv
,
244 size_t data_size
, int largest_align
,
245 int cpu
, struct lttng_ust_shm_handle
*handle
)
249 ctx
->data_size
= data_size
;
250 ctx
->largest_align
= largest_align
;
253 ctx
->handle
= handle
;
254 memset(ctx
->padding
, 0, LTTNG_UST_RING_BUFFER_CTX_PADDING
);
260 * RING_BUFFER_RFLAG_FULL_TSC
262 * This flag is passed to record_header_size() and to the primitive used to
263 * write the record header. It indicates that the full 64-bit time value is
264 * needed in the record header. If this flag is not set, the record header needs
265 * only to contain "tsc_bits" bit of time value.
267 * Reservation flags can be added by the client, starting from
268 * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from
269 * record_header_size() to lib_ring_buffer_write_record_header().
271 #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0)
272 #define RING_BUFFER_RFLAG_END (1U << 1)
275 * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at
276 * compile-time. We have to duplicate the "config->align" information and the
277 * definition here because config->align is used both in the slow and fast
278 * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code.
280 #ifdef RING_BUFFER_ALIGN
282 # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */
285 * Calculate the offset needed to align the type.
286 * size_of_type must be non-zero.
288 static inline lttng_ust_notrace
289 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
);
291 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
293 return offset_align(align_drift
, size_of_type
);
298 # define RING_BUFFER_ALIGN_ATTR __attribute__((packed))
301 * Calculate the offset needed to align the type.
302 * size_of_type must be non-zero.
304 static inline lttng_ust_notrace
305 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
);
307 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
315 * lib_ring_buffer_align_ctx - Align context offset on "alignment"
316 * @ctx: ring buffer context.
318 static inline lttng_ust_notrace
319 void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
322 void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
325 ctx
->buf_offset
+= lib_ring_buffer_align(ctx
->buf_offset
,
330 * lib_ring_buffer_check_config() returns 0 on success.
331 * Used internally to check for valid configurations at channel creation.
333 static inline lttng_ust_notrace
334 int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config
*config
,
335 unsigned int switch_timer_interval
,
336 unsigned int read_timer_interval
);
338 int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config
*config
,
339 unsigned int switch_timer_interval
,
340 unsigned int read_timer_interval
)
342 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
343 && config
->sync
== RING_BUFFER_SYNC_PER_CPU
344 && switch_timer_interval
)
349 #endif /* _LTTNG_RING_BUFFER_CONFIG_H */