Commit | Line | Data |
---|---|---|
886d51a3 MD |
1 | #ifndef _LIB_RING_BUFFER_CONFIG_H |
2 | #define _LIB_RING_BUFFER_CONFIG_H | |
f3bc08c5 MD |
3 | |
4 | /* | |
886d51a3 | 5 | * lib/ringbuffer/config.h |
f3bc08c5 MD |
6 | * |
7 | * Ring buffer configuration header. Note: after declaring the standard inline | |
8 | * functions, clients should also include linux/ringbuffer/api.h. | |
9 | * | |
886d51a3 MD |
10 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
11 | * | |
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. | |
16 | * | |
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. | |
21 | * | |
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 | |
f3bc08c5 MD |
25 | */ |
26 | ||
27 | #include <linux/types.h> | |
28 | #include <linux/percpu.h> | |
6c29ebe7 | 29 | #include "../align.h" |
ae090dc5 | 30 | #include "../../lttng-tracer-core.h" |
f3bc08c5 MD |
31 | |
32 | struct lib_ring_buffer; | |
33 | struct channel; | |
34 | struct lib_ring_buffer_config; | |
35 | struct lib_ring_buffer_ctx; | |
36 | ||
37 | /* | |
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 | |
41 | * the client. | |
42 | */ | |
43 | struct lib_ring_buffer_client_cb { | |
44 | /* Mandatory callbacks */ | |
45 | ||
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, | |
f3bc08c5 | 50 | size_t *pre_header_padding, |
f3bc08c5 MD |
51 | struct lib_ring_buffer_ctx *ctx); |
52 | ||
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); | |
59 | ||
60 | /* Optional callbacks (can be set to NULL) */ | |
61 | ||
62 | /* Called at buffer creation/finalize */ | |
63 | int (*buffer_create) (struct lib_ring_buffer *buf, void *priv, | |
64 | int cpu, const char *name); | |
65 | /* | |
66 | * Clients should guarantee that no new reader handle can be opened | |
67 | * after finalize. | |
68 | */ | |
69 | void (*buffer_finalize) (struct lib_ring_buffer *buf, void *priv, int cpu); | |
70 | ||
71 | /* | |
72 | * Extract header length, payload length and timestamp from event | |
73 | * record. Used by buffer iterators. Timestamp is only used by channel | |
74 | * iterator. | |
75 | */ | |
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); | |
80 | }; | |
81 | ||
82 | /* | |
83 | * Ring buffer instance configuration. | |
84 | * | |
85 | * Declare as "static const" within the client object to ensure the inline fast | |
86 | * paths can be optimized. | |
87 | * | |
88 | * alloc/sync pairs: | |
89 | * | |
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()). | |
94 | * | |
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. | |
98 | * | |
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. | |
103 | * | |
104 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL : | |
105 | * Global shared buffer with global synchronization. | |
106 | * | |
107 | * wakeup: | |
108 | * | |
da9f3fb7 | 109 | * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu timers to poll the |
f3bc08c5 MD |
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. | |
113 | * | |
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 | |
116 | * for drivers. | |
117 | * | |
118 | * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client | |
119 | * has the responsibility to perform wakeups. | |
120 | */ | |
121 | struct lib_ring_buffer_config { | |
122 | enum { | |
123 | RING_BUFFER_ALLOC_PER_CPU, | |
124 | RING_BUFFER_ALLOC_GLOBAL, | |
125 | } alloc; | |
126 | enum { | |
127 | RING_BUFFER_SYNC_PER_CPU, /* Wait-free */ | |
128 | RING_BUFFER_SYNC_GLOBAL, /* Lock-free */ | |
129 | } sync; | |
130 | enum { | |
131 | RING_BUFFER_OVERWRITE, /* Overwrite when buffer full */ | |
132 | RING_BUFFER_DISCARD, /* Discard when buffer full */ | |
133 | } mode; | |
134 | enum { | |
135 | RING_BUFFER_SPLICE, | |
136 | RING_BUFFER_MMAP, | |
137 | RING_BUFFER_READ, /* TODO */ | |
138 | RING_BUFFER_ITERATOR, | |
139 | RING_BUFFER_NONE, | |
140 | } output; | |
141 | enum { | |
142 | RING_BUFFER_PAGE, | |
143 | RING_BUFFER_VMAP, /* TODO */ | |
144 | RING_BUFFER_STATIC, /* TODO */ | |
145 | } backend; | |
146 | enum { | |
147 | RING_BUFFER_NO_OOPS_CONSISTENCY, | |
148 | RING_BUFFER_OOPS_CONSISTENCY, | |
149 | } oops; | |
150 | enum { | |
151 | RING_BUFFER_IPI_BARRIER, | |
152 | RING_BUFFER_NO_IPI_BARRIER, | |
153 | } ipi; | |
154 | enum { | |
155 | RING_BUFFER_WAKEUP_BY_TIMER, /* wake up performed by timer */ | |
156 | RING_BUFFER_WAKEUP_BY_WRITER, /* | |
157 | * writer wakes up reader, | |
158 | * not lock-free | |
159 | * (takes spinlock). | |
160 | */ | |
161 | } wakeup; | |
162 | /* | |
163 | * tsc_bits: timestamp bits saved at each record. | |
164 | * 0 and 64 disable the timestamp compression scheme. | |
165 | */ | |
166 | unsigned int tsc_bits; | |
167 | struct lib_ring_buffer_client_cb cb; | |
168 | }; | |
169 | ||
170 | /* | |
171 | * ring buffer context | |
172 | * | |
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(). | |
176 | */ | |
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 | |
184 | * in the payload | |
185 | */ | |
186 | int cpu; /* processor id */ | |
187 | ||
188 | /* output from lib_ring_buffer_reserve() */ | |
189 | struct lib_ring_buffer *buf; /* | |
190 | * buffer corresponding to processor id | |
191 | * for this channel | |
192 | */ | |
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 | |
199 | * padding. | |
200 | */ | |
201 | u64 tsc; /* time-stamp counter value */ | |
202 | unsigned int rflags; /* reservation flags */ | |
203 | }; | |
204 | ||
205 | /** | |
206 | * lib_ring_buffer_ctx_init - initialize ring buffer context | |
207 | * @ctx: ring buffer context to initialize | |
208 | * @chan: channel | |
209 | * @priv: client private data | |
3cae7f22 | 210 | * @data_size: size of record data payload. It must be greater than 0. |
f3bc08c5 MD |
211 | * @largest_align: largest alignment within data payload types |
212 | * @cpu: processor id | |
213 | */ | |
214 | static inline | |
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, | |
218 | int cpu) | |
219 | { | |
220 | ctx->chan = chan; | |
221 | ctx->priv = priv; | |
222 | ctx->data_size = data_size; | |
223 | ctx->largest_align = largest_align; | |
224 | ctx->cpu = cpu; | |
64c796d8 | 225 | ctx->rflags = 0; |
f3bc08c5 MD |
226 | } |
227 | ||
228 | /* | |
229 | * Reservation flags. | |
230 | * | |
231 | * RING_BUFFER_RFLAG_FULL_TSC | |
232 | * | |
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. | |
237 | * | |
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(). | |
241 | */ | |
242 | #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0) | |
243 | #define RING_BUFFER_RFLAG_END (1U << 1) | |
244 | ||
ae090dc5 MD |
245 | #ifndef LTTNG_TRACER_CORE_H |
246 | #error "lttng-tracer-core.h is needed for RING_BUFFER_ALIGN define" | |
247 | #endif | |
248 | ||
f3bc08c5 MD |
249 | /* |
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. | |
254 | */ | |
255 | #ifdef RING_BUFFER_ALIGN | |
256 | ||
257 | # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */ | |
258 | ||
259 | /* | |
260 | * Calculate the offset needed to align the type. | |
261 | * size_of_type must be non-zero. | |
262 | */ | |
263 | static inline | |
264 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
265 | { | |
266 | return offset_align(align_drift, size_of_type); | |
267 | } | |
268 | ||
269 | #else | |
270 | ||
271 | # define RING_BUFFER_ALIGN_ATTR __attribute__((packed)) | |
272 | ||
273 | /* | |
274 | * Calculate the offset needed to align the type. | |
275 | * size_of_type must be non-zero. | |
276 | */ | |
277 | static inline | |
278 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
279 | { | |
280 | return 0; | |
281 | } | |
282 | ||
283 | #endif | |
284 | ||
285 | /** | |
286 | * lib_ring_buffer_align_ctx - Align context offset on "alignment" | |
287 | * @ctx: ring buffer context. | |
288 | */ | |
289 | static inline | |
290 | void lib_ring_buffer_align_ctx(struct lib_ring_buffer_ctx *ctx, | |
291 | size_t alignment) | |
292 | { | |
293 | ctx->buf_offset += lib_ring_buffer_align(ctx->buf_offset, | |
294 | alignment); | |
295 | } | |
296 | ||
297 | /* | |
298 | * lib_ring_buffer_check_config() returns 0 on success. | |
299 | * Used internally to check for valid configurations at channel creation. | |
300 | */ | |
301 | static inline | |
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) | |
305 | { | |
306 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL | |
307 | && config->sync == RING_BUFFER_SYNC_PER_CPU | |
308 | && switch_timer_interval) | |
309 | return -EINVAL; | |
310 | return 0; | |
311 | } | |
312 | ||
313 | #include "../../wrapper/ringbuffer/vatomic.h" | |
314 | ||
886d51a3 | 315 | #endif /* _LIB_RING_BUFFER_CONFIG_H */ |