Commit | Line | Data |
---|---|---|
e92f3e28 MD |
1 | #ifndef _LTTNG_RING_BUFFER_CONFIG_H |
2 | #define _LTTNG_RING_BUFFER_CONFIG_H | |
a6352fd4 MD |
3 | |
4 | /* | |
e92f3e28 | 5 | * lttng/ringbuffer-config.h |
a6352fd4 MD |
6 | * |
7 | * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * | |
9 | * Ring buffer configuration header. Note: after declaring the standard inline | |
10 | * functions, clients should also include linux/ringbuffer/api.h. | |
11 | * | |
e92f3e28 MD |
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: | |
a60d70e6 | 18 | * |
e92f3e28 MD |
19 | * The above copyright notice and this permission notice shall be included in |
20 | * all copies or substantial portions of the Software. | |
d2428e87 MD |
21 | * |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
28 | * SOFTWARE. | |
a6352fd4 MD |
29 | */ |
30 | ||
31 | #include <errno.h> | |
4318ae1b | 32 | #include "lttng/ust-tracer.h" |
b728d87e MD |
33 | #include <stdint.h> |
34 | #include <stddef.h> | |
35 | #include <urcu/arch.h> | |
2e707419 | 36 | #include <string.h> |
4318ae1b | 37 | #include "lttng/align.h" |
a8909ba5 | 38 | #include <lttng/ust-compiler.h> |
a6352fd4 | 39 | |
4cfec15c | 40 | struct lttng_ust_lib_ring_buffer; |
a6352fd4 | 41 | struct channel; |
4cfec15c MD |
42 | struct lttng_ust_lib_ring_buffer_config; |
43 | struct lttng_ust_lib_ring_buffer_ctx; | |
5a821cd6 | 44 | struct lttng_ust_shm_handle; |
a6352fd4 MD |
45 | |
46 | /* | |
47 | * Ring buffer client callbacks. Only used by slow path, never on fast path. | |
48 | * For the fast path, record_header_size(), ring_buffer_clock_read() should be | |
49 | * provided as inline functions too. These may simply return 0 if not used by | |
50 | * the client. | |
51 | */ | |
4cfec15c | 52 | struct lttng_ust_lib_ring_buffer_client_cb { |
a6352fd4 MD |
53 | /* Mandatory callbacks */ |
54 | ||
55 | /* A static inline version is also required for fast path */ | |
2fed87ae | 56 | uint64_t (*ring_buffer_clock_read) (struct channel *chan); |
4cfec15c | 57 | size_t (*record_header_size) (const struct lttng_ust_lib_ring_buffer_config *config, |
a6352fd4 MD |
58 | struct channel *chan, size_t offset, |
59 | size_t *pre_header_padding, | |
4cfec15c | 60 | struct lttng_ust_lib_ring_buffer_ctx *ctx); |
a6352fd4 MD |
61 | |
62 | /* Slow path only, at subbuffer switch */ | |
63 | size_t (*subbuffer_header_size) (void); | |
2fed87ae | 64 | void (*buffer_begin) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 65 | unsigned int subbuf_idx, |
38fae1d3 | 66 | struct lttng_ust_shm_handle *handle); |
2fed87ae | 67 | void (*buffer_end) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 68 | unsigned int subbuf_idx, unsigned long data_size, |
38fae1d3 | 69 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
70 | |
71 | /* Optional callbacks (can be set to NULL) */ | |
72 | ||
73 | /* Called at buffer creation/finalize */ | |
4cfec15c | 74 | int (*buffer_create) (struct lttng_ust_lib_ring_buffer *buf, void *priv, |
1d498196 | 75 | int cpu, const char *name, |
38fae1d3 | 76 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
77 | /* |
78 | * Clients should guarantee that no new reader handle can be opened | |
79 | * after finalize. | |
80 | */ | |
4cfec15c | 81 | void (*buffer_finalize) (struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 82 | void *priv, int cpu, |
38fae1d3 | 83 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
84 | |
85 | /* | |
86 | * Extract header length, payload length and timestamp from event | |
87 | * record. Used by buffer iterators. Timestamp is only used by channel | |
88 | * iterator. | |
89 | */ | |
4cfec15c MD |
90 | void (*record_get) (const struct lttng_ust_lib_ring_buffer_config *config, |
91 | struct channel *chan, struct lttng_ust_lib_ring_buffer *buf, | |
a6352fd4 | 92 | size_t offset, size_t *header_len, |
2fed87ae | 93 | size_t *payload_len, uint64_t *timestamp, |
38fae1d3 | 94 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
95 | }; |
96 | ||
97 | /* | |
98 | * Ring buffer instance configuration. | |
99 | * | |
100 | * Declare as "static const" within the client object to ensure the inline fast | |
101 | * paths can be optimized. | |
102 | * | |
103 | * alloc/sync pairs: | |
104 | * | |
105 | * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU : | |
106 | * Per-cpu buffers with per-cpu synchronization. Tracing must be performed | |
107 | * with preemption disabled (lib_ring_buffer_get_cpu() and | |
108 | * lib_ring_buffer_put_cpu()). | |
109 | * | |
110 | * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL : | |
111 | * Per-cpu buffer with global synchronization. Tracing can be performed with | |
112 | * preemption enabled, statistically stays on the local buffers. | |
113 | * | |
114 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU : | |
115 | * Should only be used for buffers belonging to a single thread or protected | |
116 | * by mutual exclusion by the client. Note that periodical sub-buffer switch | |
117 | * should be disabled in this kind of configuration. | |
118 | * | |
119 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL : | |
120 | * Global shared buffer with global synchronization. | |
121 | * | |
122 | * wakeup: | |
123 | * | |
124 | * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu deferrable timers to poll the | |
125 | * buffers and wake up readers if data is ready. Mainly useful for tracers which | |
126 | * don't want to call into the wakeup code on the tracing path. Use in | |
127 | * combination with "read_timer_interval" channel_create() argument. | |
128 | * | |
129 | * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is | |
130 | * ready to read. Lower latencies before the reader is woken up. Mainly suitable | |
131 | * for drivers. | |
132 | * | |
133 | * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client | |
134 | * has the responsibility to perform wakeups. | |
135 | */ | |
82b9bde8 | 136 | #define LTTNG_UST_RING_BUFFER_CONFIG_PADDING 20 |
46d52200 ZT |
137 | |
138 | enum lttng_ust_lib_ring_buffer_alloc_types { | |
139 | RING_BUFFER_ALLOC_PER_CPU, | |
140 | RING_BUFFER_ALLOC_GLOBAL, | |
141 | }; | |
142 | ||
143 | enum lttng_ust_lib_ring_buffer_sync_types { | |
144 | RING_BUFFER_SYNC_PER_CPU, /* Wait-free */ | |
145 | RING_BUFFER_SYNC_GLOBAL, /* Lock-free */ | |
146 | }; | |
147 | ||
148 | enum lttng_ust_lib_ring_buffer_mode_types { | |
149 | RING_BUFFER_OVERWRITE, /* Overwrite when buffer full */ | |
150 | RING_BUFFER_DISCARD, /* Discard when buffer full */ | |
151 | }; | |
152 | ||
153 | enum lttng_ust_lib_ring_buffer_output_types { | |
154 | RING_BUFFER_SPLICE, | |
155 | RING_BUFFER_MMAP, | |
156 | RING_BUFFER_READ, /* TODO */ | |
157 | RING_BUFFER_ITERATOR, | |
158 | RING_BUFFER_NONE, | |
159 | }; | |
160 | ||
161 | enum lttng_ust_lib_ring_buffer_backend_types { | |
162 | RING_BUFFER_PAGE, | |
163 | RING_BUFFER_VMAP, /* TODO */ | |
164 | RING_BUFFER_STATIC, /* TODO */ | |
165 | }; | |
166 | ||
167 | enum lttng_ust_lib_ring_buffer_oops_types { | |
168 | RING_BUFFER_NO_OOPS_CONSISTENCY, | |
169 | RING_BUFFER_OOPS_CONSISTENCY, | |
170 | }; | |
171 | ||
172 | enum lttng_ust_lib_ring_buffer_ipi_types { | |
173 | RING_BUFFER_IPI_BARRIER, | |
174 | RING_BUFFER_NO_IPI_BARRIER, | |
175 | }; | |
176 | ||
177 | enum lttng_ust_lib_ring_buffer_wakeup_types { | |
178 | RING_BUFFER_WAKEUP_BY_TIMER, /* wake up performed by timer */ | |
179 | RING_BUFFER_WAKEUP_BY_WRITER, /* | |
180 | * writer wakes up reader, | |
181 | * not lock-free | |
182 | * (takes spinlock). | |
183 | */ | |
184 | }; | |
185 | ||
4cfec15c | 186 | struct lttng_ust_lib_ring_buffer_config { |
46d52200 ZT |
187 | enum lttng_ust_lib_ring_buffer_alloc_types alloc; |
188 | enum lttng_ust_lib_ring_buffer_sync_types sync; | |
189 | enum lttng_ust_lib_ring_buffer_mode_types mode; | |
190 | enum lttng_ust_lib_ring_buffer_output_types output; | |
191 | enum lttng_ust_lib_ring_buffer_backend_types backend; | |
192 | enum lttng_ust_lib_ring_buffer_oops_types oops; | |
193 | enum lttng_ust_lib_ring_buffer_ipi_types ipi; | |
194 | enum lttng_ust_lib_ring_buffer_wakeup_types wakeup; | |
a6352fd4 MD |
195 | /* |
196 | * tsc_bits: timestamp bits saved at each record. | |
197 | * 0 and 64 disable the timestamp compression scheme. | |
198 | */ | |
199 | unsigned int tsc_bits; | |
4cfec15c | 200 | struct lttng_ust_lib_ring_buffer_client_cb cb; |
c1fca457 MD |
201 | /* |
202 | * client_type is used by the consumer process (which is in a | |
203 | * different address space) to lookup the appropriate client | |
204 | * callbacks and update the cb pointers. | |
205 | */ | |
206 | int client_type; | |
82b9bde8 JD |
207 | int _unused1; |
208 | const struct lttng_ust_lib_ring_buffer_client_cb *cb_ptr; | |
2e707419 | 209 | char padding[LTTNG_UST_RING_BUFFER_CONFIG_PADDING]; |
a6352fd4 MD |
210 | }; |
211 | ||
212 | /* | |
213 | * ring buffer context | |
214 | * | |
215 | * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(), | |
216 | * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and | |
217 | * lib_ring_buffer_write(). | |
180901e6 MD |
218 | * |
219 | * IMPORTANT: this structure is part of the ABI between the probe and | |
220 | * UST. Fields need to be only added at the end, never reordered, never | |
221 | * removed. | |
a6352fd4 | 222 | */ |
96f85541 MD |
223 | #define LTTNG_UST_RING_BUFFER_CTX_PADDING \ |
224 | (24 - sizeof(int) - sizeof(void *)) | |
4cfec15c | 225 | struct lttng_ust_lib_ring_buffer_ctx { |
a6352fd4 MD |
226 | /* input received by lib_ring_buffer_reserve(), saved here. */ |
227 | struct channel *chan; /* channel */ | |
228 | void *priv; /* client private data */ | |
38fae1d3 | 229 | struct lttng_ust_shm_handle *handle; /* shared-memory handle */ |
a6352fd4 MD |
230 | size_t data_size; /* size of payload */ |
231 | int largest_align; /* | |
232 | * alignment of the largest element | |
233 | * in the payload | |
234 | */ | |
235 | int cpu; /* processor id */ | |
236 | ||
237 | /* output from lib_ring_buffer_reserve() */ | |
4cfec15c | 238 | struct lttng_ust_lib_ring_buffer *buf; /* |
a6352fd4 MD |
239 | * buffer corresponding to processor id |
240 | * for this channel | |
241 | */ | |
242 | size_t slot_size; /* size of the reserved slot */ | |
243 | unsigned long buf_offset; /* offset following the record header */ | |
244 | unsigned long pre_offset; /* | |
245 | * Initial offset position _before_ | |
246 | * the record is written. Positioned | |
247 | * prior to record header alignment | |
248 | * padding. | |
249 | */ | |
2fed87ae | 250 | uint64_t tsc; /* time-stamp counter value */ |
a6352fd4 | 251 | unsigned int rflags; /* reservation flags */ |
96f85541 MD |
252 | unsigned int padding1; /* padding to realign on pointer */ |
253 | void *ip; /* caller ip address */ | |
254 | char padding2[LTTNG_UST_RING_BUFFER_CTX_PADDING]; | |
a6352fd4 MD |
255 | }; |
256 | ||
257 | /** | |
258 | * lib_ring_buffer_ctx_init - initialize ring buffer context | |
259 | * @ctx: ring buffer context to initialize | |
260 | * @chan: channel | |
261 | * @priv: client private data | |
262 | * @data_size: size of record data payload | |
263 | * @largest_align: largest alignment within data payload types | |
264 | * @cpu: processor id | |
265 | */ | |
a8909ba5 PW |
266 | static inline lttng_ust_notrace |
267 | void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
268 | struct channel *chan, void *priv, | |
269 | size_t data_size, int largest_align, | |
270 | int cpu, struct lttng_ust_shm_handle *handle); | |
a6352fd4 | 271 | static inline |
4cfec15c | 272 | void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
a6352fd4 MD |
273 | struct channel *chan, void *priv, |
274 | size_t data_size, int largest_align, | |
38fae1d3 | 275 | int cpu, struct lttng_ust_shm_handle *handle) |
a6352fd4 MD |
276 | { |
277 | ctx->chan = chan; | |
278 | ctx->priv = priv; | |
279 | ctx->data_size = data_size; | |
280 | ctx->largest_align = largest_align; | |
281 | ctx->cpu = cpu; | |
282 | ctx->rflags = 0; | |
1d498196 | 283 | ctx->handle = handle; |
96f85541 MD |
284 | ctx->padding1 = 0; |
285 | ctx->ip = 0; | |
286 | memset(ctx->padding2, 0, LTTNG_UST_RING_BUFFER_CTX_PADDING); | |
a6352fd4 MD |
287 | } |
288 | ||
289 | /* | |
290 | * Reservation flags. | |
291 | * | |
292 | * RING_BUFFER_RFLAG_FULL_TSC | |
293 | * | |
294 | * This flag is passed to record_header_size() and to the primitive used to | |
295 | * write the record header. It indicates that the full 64-bit time value is | |
296 | * needed in the record header. If this flag is not set, the record header needs | |
297 | * only to contain "tsc_bits" bit of time value. | |
298 | * | |
299 | * Reservation flags can be added by the client, starting from | |
300 | * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from | |
301 | * record_header_size() to lib_ring_buffer_write_record_header(). | |
302 | */ | |
303 | #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0) | |
304 | #define RING_BUFFER_RFLAG_END (1U << 1) | |
305 | ||
306 | /* | |
307 | * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at | |
308 | * compile-time. We have to duplicate the "config->align" information and the | |
309 | * definition here because config->align is used both in the slow and fast | |
310 | * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code. | |
311 | */ | |
312 | #ifdef RING_BUFFER_ALIGN | |
313 | ||
314 | # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */ | |
315 | ||
316 | /* | |
317 | * Calculate the offset needed to align the type. | |
318 | * size_of_type must be non-zero. | |
319 | */ | |
a8909ba5 PW |
320 | static inline lttng_ust_notrace |
321 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type); | |
a6352fd4 MD |
322 | static inline |
323 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
324 | { | |
325 | return offset_align(align_drift, size_of_type); | |
326 | } | |
327 | ||
328 | #else | |
329 | ||
330 | # define RING_BUFFER_ALIGN_ATTR __attribute__((packed)) | |
331 | ||
332 | /* | |
333 | * Calculate the offset needed to align the type. | |
334 | * size_of_type must be non-zero. | |
335 | */ | |
a8909ba5 PW |
336 | static inline lttng_ust_notrace |
337 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type); | |
a6352fd4 MD |
338 | static inline |
339 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
340 | { | |
341 | return 0; | |
342 | } | |
343 | ||
344 | #endif | |
345 | ||
346 | /** | |
347 | * lib_ring_buffer_align_ctx - Align context offset on "alignment" | |
348 | * @ctx: ring buffer context. | |
349 | */ | |
a8909ba5 PW |
350 | static inline lttng_ust_notrace |
351 | void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
352 | size_t alignment); | |
a6352fd4 | 353 | static inline |
4cfec15c | 354 | void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
a6352fd4 MD |
355 | size_t alignment) |
356 | { | |
357 | ctx->buf_offset += lib_ring_buffer_align(ctx->buf_offset, | |
358 | alignment); | |
359 | } | |
360 | ||
361 | /* | |
362 | * lib_ring_buffer_check_config() returns 0 on success. | |
363 | * Used internally to check for valid configurations at channel creation. | |
364 | */ | |
a8909ba5 PW |
365 | static inline lttng_ust_notrace |
366 | int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config, | |
367 | unsigned int switch_timer_interval, | |
368 | unsigned int read_timer_interval); | |
a6352fd4 | 369 | static inline |
4cfec15c | 370 | int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config, |
a6352fd4 MD |
371 | unsigned int switch_timer_interval, |
372 | unsigned int read_timer_interval) | |
373 | { | |
374 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL | |
375 | && config->sync == RING_BUFFER_SYNC_PER_CPU | |
376 | && switch_timer_interval) | |
377 | return -EINVAL; | |
378 | return 0; | |
379 | } | |
380 | ||
e92f3e28 | 381 | #endif /* _LTTNG_RING_BUFFER_CONFIG_H */ |