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 #define LTTNG_UST_RING_BUFFER_CTX_PADDING 24
193 struct lttng_ust_lib_ring_buffer_ctx
{
194 /* input received by lib_ring_buffer_reserve(), saved here. */
195 struct channel
*chan
; /* channel */
196 void *priv
; /* client private data */
197 struct lttng_ust_shm_handle
*handle
; /* shared-memory handle */
198 size_t data_size
; /* size of payload */
199 int largest_align
; /*
200 * alignment of the largest element
203 int cpu
; /* processor id */
205 /* output from lib_ring_buffer_reserve() */
206 struct lttng_ust_lib_ring_buffer
*buf
; /*
207 * buffer corresponding to processor id
210 size_t slot_size
; /* size of the reserved slot */
211 unsigned long buf_offset
; /* offset following the record header */
212 unsigned long pre_offset
; /*
213 * Initial offset position _before_
214 * the record is written. Positioned
215 * prior to record header alignment
218 uint64_t tsc
; /* time-stamp counter value */
219 unsigned int rflags
; /* reservation flags */
220 char padding
[LTTNG_UST_RING_BUFFER_CTX_PADDING
];
224 * lib_ring_buffer_ctx_init - initialize ring buffer context
225 * @ctx: ring buffer context to initialize
227 * @priv: client private data
228 * @data_size: size of record data payload
229 * @largest_align: largest alignment within data payload types
232 static inline lttng_ust_notrace
233 void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
234 struct channel
*chan
, void *priv
,
235 size_t data_size
, int largest_align
,
236 int cpu
, struct lttng_ust_shm_handle
*handle
);
238 void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
239 struct channel
*chan
, void *priv
,
240 size_t data_size
, int largest_align
,
241 int cpu
, struct lttng_ust_shm_handle
*handle
)
245 ctx
->data_size
= data_size
;
246 ctx
->largest_align
= largest_align
;
249 ctx
->handle
= handle
;
250 memset(ctx
->padding
, 0, LTTNG_UST_RING_BUFFER_CTX_PADDING
);
256 * RING_BUFFER_RFLAG_FULL_TSC
258 * This flag is passed to record_header_size() and to the primitive used to
259 * write the record header. It indicates that the full 64-bit time value is
260 * needed in the record header. If this flag is not set, the record header needs
261 * only to contain "tsc_bits" bit of time value.
263 * Reservation flags can be added by the client, starting from
264 * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from
265 * record_header_size() to lib_ring_buffer_write_record_header().
267 #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0)
268 #define RING_BUFFER_RFLAG_END (1U << 1)
271 * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at
272 * compile-time. We have to duplicate the "config->align" information and the
273 * definition here because config->align is used both in the slow and fast
274 * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code.
276 #ifdef RING_BUFFER_ALIGN
278 # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */
281 * Calculate the offset needed to align the type.
282 * size_of_type must be non-zero.
284 static inline lttng_ust_notrace
285 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
);
287 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
289 return offset_align(align_drift
, size_of_type
);
294 # define RING_BUFFER_ALIGN_ATTR __attribute__((packed))
297 * Calculate the offset needed to align the type.
298 * size_of_type must be non-zero.
300 static inline lttng_ust_notrace
301 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
);
303 unsigned int lib_ring_buffer_align(size_t align_drift
, size_t size_of_type
)
311 * lib_ring_buffer_align_ctx - Align context offset on "alignment"
312 * @ctx: ring buffer context.
314 static inline lttng_ust_notrace
315 void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
318 void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
321 ctx
->buf_offset
+= lib_ring_buffer_align(ctx
->buf_offset
,
326 * lib_ring_buffer_check_config() returns 0 on success.
327 * Used internally to check for valid configurations at channel creation.
329 static inline lttng_ust_notrace
330 int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config
*config
,
331 unsigned int switch_timer_interval
,
332 unsigned int read_timer_interval
);
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 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
339 && config
->sync
== RING_BUFFER_SYNC_PER_CPU
340 && switch_timer_interval
)
345 #endif /* _LTTNG_RING_BUFFER_CONFIG_H */