Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only) |
9f36eaed | 2 | * |
24591303 | 3 | * ringbuffer/config.h |
f3bc08c5 MD |
4 | * |
5 | * Ring buffer configuration header. Note: after declaring the standard inline | |
6 | * functions, clients should also include linux/ringbuffer/api.h. | |
7 | * | |
886d51a3 | 8 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
f3bc08c5 MD |
9 | */ |
10 | ||
9f36eaed MJ |
11 | #ifndef _LIB_RING_BUFFER_CONFIG_H |
12 | #define _LIB_RING_BUFFER_CONFIG_H | |
13 | ||
f3bc08c5 MD |
14 | #include <linux/types.h> |
15 | #include <linux/percpu.h> | |
a071f25d | 16 | #include <lttng/align.h> |
2df37e95 | 17 | #include <lttng/tracer-core.h> |
f3bc08c5 MD |
18 | |
19 | struct lib_ring_buffer; | |
20 | struct channel; | |
21 | struct lib_ring_buffer_config; | |
22 | struct lib_ring_buffer_ctx; | |
23 | ||
24 | /* | |
25 | * Ring buffer client callbacks. Only used by slow path, never on fast path. | |
26 | * For the fast path, record_header_size(), ring_buffer_clock_read() should be | |
27 | * provided as inline functions too. These may simply return 0 if not used by | |
28 | * the client. | |
29 | */ | |
30 | struct lib_ring_buffer_client_cb { | |
31 | /* Mandatory callbacks */ | |
32 | ||
33 | /* A static inline version is also required for fast path */ | |
34 | u64 (*ring_buffer_clock_read) (struct channel *chan); | |
35 | size_t (*record_header_size) (const struct lib_ring_buffer_config *config, | |
36 | struct channel *chan, size_t offset, | |
f3bc08c5 | 37 | size_t *pre_header_padding, |
cc62f29e MD |
38 | struct lib_ring_buffer_ctx *ctx, |
39 | void *client_ctx); | |
f3bc08c5 MD |
40 | |
41 | /* Slow path only, at subbuffer switch */ | |
42 | size_t (*subbuffer_header_size) (void); | |
43 | void (*buffer_begin) (struct lib_ring_buffer *buf, u64 tsc, | |
44 | unsigned int subbuf_idx); | |
45 | void (*buffer_end) (struct lib_ring_buffer *buf, u64 tsc, | |
46 | unsigned int subbuf_idx, unsigned long data_size); | |
47 | ||
48 | /* Optional callbacks (can be set to NULL) */ | |
49 | ||
50 | /* Called at buffer creation/finalize */ | |
51 | int (*buffer_create) (struct lib_ring_buffer *buf, void *priv, | |
52 | int cpu, const char *name); | |
53 | /* | |
54 | * Clients should guarantee that no new reader handle can be opened | |
55 | * after finalize. | |
56 | */ | |
57 | void (*buffer_finalize) (struct lib_ring_buffer *buf, void *priv, int cpu); | |
58 | ||
59 | /* | |
60 | * Extract header length, payload length and timestamp from event | |
61 | * record. Used by buffer iterators. Timestamp is only used by channel | |
62 | * iterator. | |
63 | */ | |
64 | void (*record_get) (const struct lib_ring_buffer_config *config, | |
65 | struct channel *chan, struct lib_ring_buffer *buf, | |
66 | size_t offset, size_t *header_len, | |
67 | size_t *payload_len, u64 *timestamp); | |
68 | }; | |
69 | ||
70 | /* | |
71 | * Ring buffer instance configuration. | |
72 | * | |
73 | * Declare as "static const" within the client object to ensure the inline fast | |
74 | * paths can be optimized. | |
75 | * | |
76 | * alloc/sync pairs: | |
77 | * | |
78 | * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU : | |
79 | * Per-cpu buffers with per-cpu synchronization. Tracing must be performed | |
80 | * with preemption disabled (lib_ring_buffer_get_cpu() and | |
81 | * lib_ring_buffer_put_cpu()). | |
82 | * | |
83 | * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL : | |
84 | * Per-cpu buffer with global synchronization. Tracing can be performed with | |
85 | * preemption enabled, statistically stays on the local buffers. | |
86 | * | |
87 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU : | |
88 | * Should only be used for buffers belonging to a single thread or protected | |
89 | * by mutual exclusion by the client. Note that periodical sub-buffer switch | |
90 | * should be disabled in this kind of configuration. | |
91 | * | |
92 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL : | |
93 | * Global shared buffer with global synchronization. | |
94 | * | |
95 | * wakeup: | |
96 | * | |
da9f3fb7 | 97 | * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu timers to poll the |
f3bc08c5 MD |
98 | * buffers and wake up readers if data is ready. Mainly useful for tracers which |
99 | * don't want to call into the wakeup code on the tracing path. Use in | |
100 | * combination with "read_timer_interval" channel_create() argument. | |
101 | * | |
102 | * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is | |
103 | * ready to read. Lower latencies before the reader is woken up. Mainly suitable | |
fbd4d558 MD |
104 | * for drivers. Going through an "irq_work" allows triggering this type of wakeup |
105 | * even from NMI context: the wakeup will be slightly delayed until the next | |
106 | * interrupts are handled. | |
f3bc08c5 MD |
107 | * |
108 | * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client | |
109 | * has the responsibility to perform wakeups. | |
110 | */ | |
111 | struct lib_ring_buffer_config { | |
112 | enum { | |
113 | RING_BUFFER_ALLOC_PER_CPU, | |
114 | RING_BUFFER_ALLOC_GLOBAL, | |
115 | } alloc; | |
116 | enum { | |
117 | RING_BUFFER_SYNC_PER_CPU, /* Wait-free */ | |
118 | RING_BUFFER_SYNC_GLOBAL, /* Lock-free */ | |
119 | } sync; | |
120 | enum { | |
121 | RING_BUFFER_OVERWRITE, /* Overwrite when buffer full */ | |
122 | RING_BUFFER_DISCARD, /* Discard when buffer full */ | |
123 | } mode; | |
124 | enum { | |
125 | RING_BUFFER_SPLICE, | |
126 | RING_BUFFER_MMAP, | |
127 | RING_BUFFER_READ, /* TODO */ | |
128 | RING_BUFFER_ITERATOR, | |
129 | RING_BUFFER_NONE, | |
130 | } output; | |
131 | enum { | |
132 | RING_BUFFER_PAGE, | |
133 | RING_BUFFER_VMAP, /* TODO */ | |
134 | RING_BUFFER_STATIC, /* TODO */ | |
135 | } backend; | |
136 | enum { | |
137 | RING_BUFFER_NO_OOPS_CONSISTENCY, | |
138 | RING_BUFFER_OOPS_CONSISTENCY, | |
139 | } oops; | |
140 | enum { | |
141 | RING_BUFFER_IPI_BARRIER, | |
142 | RING_BUFFER_NO_IPI_BARRIER, | |
143 | } ipi; | |
144 | enum { | |
145 | RING_BUFFER_WAKEUP_BY_TIMER, /* wake up performed by timer */ | |
146 | RING_BUFFER_WAKEUP_BY_WRITER, /* | |
fbd4d558 MD |
147 | * writer wakes up reader through |
148 | * irq_work. | |
f3bc08c5 MD |
149 | */ |
150 | } wakeup; | |
151 | /* | |
152 | * tsc_bits: timestamp bits saved at each record. | |
153 | * 0 and 64 disable the timestamp compression scheme. | |
154 | */ | |
155 | unsigned int tsc_bits; | |
156 | struct lib_ring_buffer_client_cb cb; | |
157 | }; | |
158 | ||
159 | /* | |
160 | * ring buffer context | |
161 | * | |
162 | * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(), | |
163 | * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and | |
164 | * lib_ring_buffer_write(). | |
165 | */ | |
166 | struct lib_ring_buffer_ctx { | |
167 | /* input received by lib_ring_buffer_reserve(), saved here. */ | |
168 | struct channel *chan; /* channel */ | |
169 | void *priv; /* client private data */ | |
170 | size_t data_size; /* size of payload */ | |
171 | int largest_align; /* | |
172 | * alignment of the largest element | |
173 | * in the payload | |
174 | */ | |
175 | int cpu; /* processor id */ | |
176 | ||
177 | /* output from lib_ring_buffer_reserve() */ | |
178 | struct lib_ring_buffer *buf; /* | |
179 | * buffer corresponding to processor id | |
180 | * for this channel | |
181 | */ | |
182 | size_t slot_size; /* size of the reserved slot */ | |
183 | unsigned long buf_offset; /* offset following the record header */ | |
184 | unsigned long pre_offset; /* | |
185 | * Initial offset position _before_ | |
186 | * the record is written. Positioned | |
187 | * prior to record header alignment | |
188 | * padding. | |
189 | */ | |
190 | u64 tsc; /* time-stamp counter value */ | |
191 | unsigned int rflags; /* reservation flags */ | |
85a07c33 MD |
192 | /* Cache backend pages pointer chasing. */ |
193 | struct lib_ring_buffer_backend_pages *backend_pages; | |
f3bc08c5 MD |
194 | }; |
195 | ||
196 | /** | |
197 | * lib_ring_buffer_ctx_init - initialize ring buffer context | |
198 | * @ctx: ring buffer context to initialize | |
199 | * @chan: channel | |
200 | * @priv: client private data | |
3cae7f22 | 201 | * @data_size: size of record data payload. It must be greater than 0. |
f3bc08c5 MD |
202 | * @largest_align: largest alignment within data payload types |
203 | * @cpu: processor id | |
204 | */ | |
205 | static inline | |
206 | void lib_ring_buffer_ctx_init(struct lib_ring_buffer_ctx *ctx, | |
207 | struct channel *chan, void *priv, | |
208 | size_t data_size, int largest_align, | |
209 | int cpu) | |
210 | { | |
211 | ctx->chan = chan; | |
212 | ctx->priv = priv; | |
213 | ctx->data_size = data_size; | |
214 | ctx->largest_align = largest_align; | |
215 | ctx->cpu = cpu; | |
64c796d8 | 216 | ctx->rflags = 0; |
85a07c33 | 217 | ctx->backend_pages = NULL; |
f3bc08c5 MD |
218 | } |
219 | ||
220 | /* | |
221 | * Reservation flags. | |
222 | * | |
223 | * RING_BUFFER_RFLAG_FULL_TSC | |
224 | * | |
225 | * This flag is passed to record_header_size() and to the primitive used to | |
226 | * write the record header. It indicates that the full 64-bit time value is | |
227 | * needed in the record header. If this flag is not set, the record header needs | |
228 | * only to contain "tsc_bits" bit of time value. | |
229 | * | |
230 | * Reservation flags can be added by the client, starting from | |
231 | * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from | |
232 | * record_header_size() to lib_ring_buffer_write_record_header(). | |
233 | */ | |
234 | #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0) | |
235 | #define RING_BUFFER_RFLAG_END (1U << 1) | |
236 | ||
ae090dc5 | 237 | #ifndef LTTNG_TRACER_CORE_H |
2df37e95 | 238 | #error "lttng/tracer-core.h is needed for RING_BUFFER_ALIGN define" |
ae090dc5 MD |
239 | #endif |
240 | ||
f3bc08c5 MD |
241 | /* |
242 | * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at | |
243 | * compile-time. We have to duplicate the "config->align" information and the | |
244 | * definition here because config->align is used both in the slow and fast | |
245 | * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code. | |
246 | */ | |
247 | #ifdef RING_BUFFER_ALIGN | |
248 | ||
249 | # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */ | |
250 | ||
251 | /* | |
252 | * Calculate the offset needed to align the type. | |
253 | * size_of_type must be non-zero. | |
254 | */ | |
255 | static inline | |
256 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
257 | { | |
258 | return offset_align(align_drift, size_of_type); | |
259 | } | |
260 | ||
261 | #else | |
262 | ||
263 | # define RING_BUFFER_ALIGN_ATTR __attribute__((packed)) | |
264 | ||
265 | /* | |
266 | * Calculate the offset needed to align the type. | |
267 | * size_of_type must be non-zero. | |
268 | */ | |
269 | static inline | |
270 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
271 | { | |
272 | return 0; | |
273 | } | |
274 | ||
275 | #endif | |
276 | ||
277 | /** | |
278 | * lib_ring_buffer_align_ctx - Align context offset on "alignment" | |
279 | * @ctx: ring buffer context. | |
280 | */ | |
281 | static inline | |
282 | void lib_ring_buffer_align_ctx(struct lib_ring_buffer_ctx *ctx, | |
283 | size_t alignment) | |
284 | { | |
285 | ctx->buf_offset += lib_ring_buffer_align(ctx->buf_offset, | |
286 | alignment); | |
287 | } | |
288 | ||
289 | /* | |
290 | * lib_ring_buffer_check_config() returns 0 on success. | |
291 | * Used internally to check for valid configurations at channel creation. | |
292 | */ | |
293 | static inline | |
294 | int lib_ring_buffer_check_config(const struct lib_ring_buffer_config *config, | |
295 | unsigned int switch_timer_interval, | |
296 | unsigned int read_timer_interval) | |
297 | { | |
298 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL | |
299 | && config->sync == RING_BUFFER_SYNC_PER_CPU | |
300 | && switch_timer_interval) | |
301 | return -EINVAL; | |
302 | return 0; | |
303 | } | |
304 | ||
24591303 | 305 | #include <ringbuffer/vatomic.h> |
f3bc08c5 | 306 | |
886d51a3 | 307 | #endif /* _LIB_RING_BUFFER_CONFIG_H */ |