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, | |
e56bb47c MD |
60 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
61 | void *client_ctx); | |
a6352fd4 MD |
62 | |
63 | /* Slow path only, at subbuffer switch */ | |
64 | size_t (*subbuffer_header_size) (void); | |
2fed87ae | 65 | void (*buffer_begin) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 66 | unsigned int subbuf_idx, |
38fae1d3 | 67 | struct lttng_ust_shm_handle *handle); |
2fed87ae | 68 | void (*buffer_end) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 69 | unsigned int subbuf_idx, unsigned long data_size, |
38fae1d3 | 70 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
71 | |
72 | /* Optional callbacks (can be set to NULL) */ | |
73 | ||
74 | /* Called at buffer creation/finalize */ | |
4cfec15c | 75 | int (*buffer_create) (struct lttng_ust_lib_ring_buffer *buf, void *priv, |
1d498196 | 76 | int cpu, const char *name, |
38fae1d3 | 77 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
78 | /* |
79 | * Clients should guarantee that no new reader handle can be opened | |
80 | * after finalize. | |
81 | */ | |
4cfec15c | 82 | void (*buffer_finalize) (struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 83 | void *priv, int cpu, |
38fae1d3 | 84 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
85 | |
86 | /* | |
87 | * Extract header length, payload length and timestamp from event | |
88 | * record. Used by buffer iterators. Timestamp is only used by channel | |
89 | * iterator. | |
90 | */ | |
4cfec15c MD |
91 | void (*record_get) (const struct lttng_ust_lib_ring_buffer_config *config, |
92 | struct channel *chan, struct lttng_ust_lib_ring_buffer *buf, | |
a6352fd4 | 93 | size_t offset, size_t *header_len, |
2fed87ae | 94 | size_t *payload_len, uint64_t *timestamp, |
38fae1d3 | 95 | struct lttng_ust_shm_handle *handle); |
a9ff648c MD |
96 | /* |
97 | * Offset and size of content size field in client. | |
98 | */ | |
99 | void (*content_size_field) (const struct lttng_ust_lib_ring_buffer_config *config, | |
100 | size_t *offset, size_t *length); | |
101 | void (*packet_size_field) (const struct lttng_ust_lib_ring_buffer_config *config, | |
102 | size_t *offset, size_t *length); | |
a6352fd4 MD |
103 | }; |
104 | ||
105 | /* | |
106 | * Ring buffer instance configuration. | |
107 | * | |
108 | * Declare as "static const" within the client object to ensure the inline fast | |
109 | * paths can be optimized. | |
110 | * | |
111 | * alloc/sync pairs: | |
112 | * | |
113 | * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU : | |
114 | * Per-cpu buffers with per-cpu synchronization. Tracing must be performed | |
115 | * with preemption disabled (lib_ring_buffer_get_cpu() and | |
116 | * lib_ring_buffer_put_cpu()). | |
117 | * | |
118 | * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL : | |
119 | * Per-cpu buffer with global synchronization. Tracing can be performed with | |
120 | * preemption enabled, statistically stays on the local buffers. | |
121 | * | |
122 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU : | |
123 | * Should only be used for buffers belonging to a single thread or protected | |
124 | * by mutual exclusion by the client. Note that periodical sub-buffer switch | |
125 | * should be disabled in this kind of configuration. | |
126 | * | |
127 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL : | |
128 | * Global shared buffer with global synchronization. | |
129 | * | |
130 | * wakeup: | |
131 | * | |
132 | * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu deferrable timers to poll the | |
133 | * buffers and wake up readers if data is ready. Mainly useful for tracers which | |
134 | * don't want to call into the wakeup code on the tracing path. Use in | |
135 | * combination with "read_timer_interval" channel_create() argument. | |
136 | * | |
137 | * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is | |
138 | * ready to read. Lower latencies before the reader is woken up. Mainly suitable | |
139 | * for drivers. | |
140 | * | |
141 | * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client | |
142 | * has the responsibility to perform wakeups. | |
143 | */ | |
82b9bde8 | 144 | #define LTTNG_UST_RING_BUFFER_CONFIG_PADDING 20 |
46d52200 ZT |
145 | |
146 | enum lttng_ust_lib_ring_buffer_alloc_types { | |
147 | RING_BUFFER_ALLOC_PER_CPU, | |
148 | RING_BUFFER_ALLOC_GLOBAL, | |
149 | }; | |
150 | ||
151 | enum lttng_ust_lib_ring_buffer_sync_types { | |
152 | RING_BUFFER_SYNC_PER_CPU, /* Wait-free */ | |
153 | RING_BUFFER_SYNC_GLOBAL, /* Lock-free */ | |
154 | }; | |
155 | ||
156 | enum lttng_ust_lib_ring_buffer_mode_types { | |
a9ff648c MD |
157 | RING_BUFFER_OVERWRITE = 0, /* Overwrite when buffer full */ |
158 | RING_BUFFER_DISCARD = 1, /* Discard when buffer full */ | |
46d52200 ZT |
159 | }; |
160 | ||
161 | enum lttng_ust_lib_ring_buffer_output_types { | |
162 | RING_BUFFER_SPLICE, | |
163 | RING_BUFFER_MMAP, | |
164 | RING_BUFFER_READ, /* TODO */ | |
165 | RING_BUFFER_ITERATOR, | |
166 | RING_BUFFER_NONE, | |
167 | }; | |
168 | ||
169 | enum lttng_ust_lib_ring_buffer_backend_types { | |
170 | RING_BUFFER_PAGE, | |
171 | RING_BUFFER_VMAP, /* TODO */ | |
172 | RING_BUFFER_STATIC, /* TODO */ | |
173 | }; | |
174 | ||
175 | enum lttng_ust_lib_ring_buffer_oops_types { | |
176 | RING_BUFFER_NO_OOPS_CONSISTENCY, | |
177 | RING_BUFFER_OOPS_CONSISTENCY, | |
178 | }; | |
179 | ||
180 | enum lttng_ust_lib_ring_buffer_ipi_types { | |
181 | RING_BUFFER_IPI_BARRIER, | |
182 | RING_BUFFER_NO_IPI_BARRIER, | |
183 | }; | |
184 | ||
185 | enum lttng_ust_lib_ring_buffer_wakeup_types { | |
186 | RING_BUFFER_WAKEUP_BY_TIMER, /* wake up performed by timer */ | |
187 | RING_BUFFER_WAKEUP_BY_WRITER, /* | |
188 | * writer wakes up reader, | |
189 | * not lock-free | |
190 | * (takes spinlock). | |
191 | */ | |
192 | }; | |
193 | ||
4cfec15c | 194 | struct lttng_ust_lib_ring_buffer_config { |
46d52200 ZT |
195 | enum lttng_ust_lib_ring_buffer_alloc_types alloc; |
196 | enum lttng_ust_lib_ring_buffer_sync_types sync; | |
197 | enum lttng_ust_lib_ring_buffer_mode_types mode; | |
198 | enum lttng_ust_lib_ring_buffer_output_types output; | |
199 | enum lttng_ust_lib_ring_buffer_backend_types backend; | |
200 | enum lttng_ust_lib_ring_buffer_oops_types oops; | |
201 | enum lttng_ust_lib_ring_buffer_ipi_types ipi; | |
202 | enum lttng_ust_lib_ring_buffer_wakeup_types wakeup; | |
a6352fd4 MD |
203 | /* |
204 | * tsc_bits: timestamp bits saved at each record. | |
205 | * 0 and 64 disable the timestamp compression scheme. | |
206 | */ | |
207 | unsigned int tsc_bits; | |
4cfec15c | 208 | struct lttng_ust_lib_ring_buffer_client_cb cb; |
c1fca457 MD |
209 | /* |
210 | * client_type is used by the consumer process (which is in a | |
211 | * different address space) to lookup the appropriate client | |
212 | * callbacks and update the cb pointers. | |
213 | */ | |
214 | int client_type; | |
82b9bde8 JD |
215 | int _unused1; |
216 | const struct lttng_ust_lib_ring_buffer_client_cb *cb_ptr; | |
2e707419 | 217 | char padding[LTTNG_UST_RING_BUFFER_CONFIG_PADDING]; |
a6352fd4 MD |
218 | }; |
219 | ||
220 | /* | |
221 | * ring buffer context | |
222 | * | |
223 | * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(), | |
224 | * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and | |
225 | * lib_ring_buffer_write(). | |
180901e6 MD |
226 | * |
227 | * IMPORTANT: this structure is part of the ABI between the probe and | |
228 | * UST. Fields need to be only added at the end, never reordered, never | |
229 | * removed. | |
a6352fd4 | 230 | */ |
96f85541 | 231 | #define LTTNG_UST_RING_BUFFER_CTX_PADDING \ |
53569322 | 232 | (24 - sizeof(int) - sizeof(void *) - sizeof(void *)) |
4cfec15c | 233 | struct lttng_ust_lib_ring_buffer_ctx { |
a6352fd4 MD |
234 | /* input received by lib_ring_buffer_reserve(), saved here. */ |
235 | struct channel *chan; /* channel */ | |
236 | void *priv; /* client private data */ | |
38fae1d3 | 237 | struct lttng_ust_shm_handle *handle; /* shared-memory handle */ |
a6352fd4 MD |
238 | size_t data_size; /* size of payload */ |
239 | int largest_align; /* | |
240 | * alignment of the largest element | |
241 | * in the payload | |
242 | */ | |
243 | int cpu; /* processor id */ | |
244 | ||
245 | /* output from lib_ring_buffer_reserve() */ | |
4cfec15c | 246 | struct lttng_ust_lib_ring_buffer *buf; /* |
a6352fd4 MD |
247 | * buffer corresponding to processor id |
248 | * for this channel | |
249 | */ | |
250 | size_t slot_size; /* size of the reserved slot */ | |
251 | unsigned long buf_offset; /* offset following the record header */ | |
252 | unsigned long pre_offset; /* | |
253 | * Initial offset position _before_ | |
254 | * the record is written. Positioned | |
255 | * prior to record header alignment | |
256 | * padding. | |
257 | */ | |
2fed87ae | 258 | uint64_t tsc; /* time-stamp counter value */ |
a6352fd4 | 259 | unsigned int rflags; /* reservation flags */ |
82cd0cfd MD |
260 | /* |
261 | * The field ctx_len is the length of struct | |
262 | * lttng_ust_lib_ring_buffer_ctx as known by the user of | |
263 | * lib_ring_buffer_ctx_init. | |
264 | */ | |
265 | unsigned int ctx_len; | |
96f85541 | 266 | void *ip; /* caller ip address */ |
53569322 | 267 | void *priv2; /* 2nd priv data */ |
96f85541 | 268 | char padding2[LTTNG_UST_RING_BUFFER_CTX_PADDING]; |
82cd0cfd MD |
269 | /* |
270 | * This is the end of the initial fields expected by the original ABI | |
271 | * between probes and UST. Only the fields above can be used if | |
272 | * ctx_len is 0. Use the value of ctx_len to find out which of the | |
273 | * following fields may be used. | |
274 | */ | |
a3492932 | 275 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
a6352fd4 MD |
276 | }; |
277 | ||
278 | /** | |
279 | * lib_ring_buffer_ctx_init - initialize ring buffer context | |
280 | * @ctx: ring buffer context to initialize | |
281 | * @chan: channel | |
282 | * @priv: client private data | |
283 | * @data_size: size of record data payload | |
284 | * @largest_align: largest alignment within data payload types | |
285 | * @cpu: processor id | |
286 | */ | |
a8909ba5 PW |
287 | static inline lttng_ust_notrace |
288 | void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
289 | struct channel *chan, void *priv, | |
290 | size_t data_size, int largest_align, | |
53569322 MD |
291 | int cpu, struct lttng_ust_shm_handle *handle, |
292 | void *priv2); | |
a6352fd4 | 293 | static inline |
4cfec15c | 294 | void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
a6352fd4 MD |
295 | struct channel *chan, void *priv, |
296 | size_t data_size, int largest_align, | |
53569322 MD |
297 | int cpu, struct lttng_ust_shm_handle *handle, |
298 | void *priv2) | |
a6352fd4 MD |
299 | { |
300 | ctx->chan = chan; | |
301 | ctx->priv = priv; | |
302 | ctx->data_size = data_size; | |
303 | ctx->largest_align = largest_align; | |
304 | ctx->cpu = cpu; | |
305 | ctx->rflags = 0; | |
1d498196 | 306 | ctx->handle = handle; |
82cd0cfd | 307 | ctx->ctx_len = sizeof(struct lttng_ust_lib_ring_buffer_ctx); |
96f85541 | 308 | ctx->ip = 0; |
53569322 | 309 | ctx->priv2 = priv2; |
96f85541 | 310 | memset(ctx->padding2, 0, LTTNG_UST_RING_BUFFER_CTX_PADDING); |
a6352fd4 MD |
311 | } |
312 | ||
313 | /* | |
314 | * Reservation flags. | |
315 | * | |
316 | * RING_BUFFER_RFLAG_FULL_TSC | |
317 | * | |
318 | * This flag is passed to record_header_size() and to the primitive used to | |
319 | * write the record header. It indicates that the full 64-bit time value is | |
320 | * needed in the record header. If this flag is not set, the record header needs | |
321 | * only to contain "tsc_bits" bit of time value. | |
322 | * | |
323 | * Reservation flags can be added by the client, starting from | |
324 | * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from | |
325 | * record_header_size() to lib_ring_buffer_write_record_header(). | |
326 | */ | |
327 | #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0) | |
328 | #define RING_BUFFER_RFLAG_END (1U << 1) | |
329 | ||
330 | /* | |
331 | * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at | |
332 | * compile-time. We have to duplicate the "config->align" information and the | |
333 | * definition here because config->align is used both in the slow and fast | |
334 | * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code. | |
335 | */ | |
336 | #ifdef RING_BUFFER_ALIGN | |
337 | ||
338 | # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */ | |
339 | ||
340 | /* | |
341 | * Calculate the offset needed to align the type. | |
342 | * size_of_type must be non-zero. | |
343 | */ | |
a8909ba5 PW |
344 | static inline lttng_ust_notrace |
345 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type); | |
a6352fd4 MD |
346 | static inline |
347 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
348 | { | |
b72687b8 | 349 | return lttng_ust_offset_align(align_drift, size_of_type); |
a6352fd4 MD |
350 | } |
351 | ||
352 | #else | |
353 | ||
354 | # define RING_BUFFER_ALIGN_ATTR __attribute__((packed)) | |
355 | ||
356 | /* | |
357 | * Calculate the offset needed to align the type. | |
358 | * size_of_type must be non-zero. | |
359 | */ | |
a8909ba5 PW |
360 | static inline lttng_ust_notrace |
361 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type); | |
a6352fd4 MD |
362 | static inline |
363 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
364 | { | |
365 | return 0; | |
366 | } | |
367 | ||
368 | #endif | |
369 | ||
370 | /** | |
371 | * lib_ring_buffer_align_ctx - Align context offset on "alignment" | |
372 | * @ctx: ring buffer context. | |
373 | */ | |
a8909ba5 PW |
374 | static inline lttng_ust_notrace |
375 | void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
376 | size_t alignment); | |
a6352fd4 | 377 | static inline |
4cfec15c | 378 | void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
a6352fd4 MD |
379 | size_t alignment) |
380 | { | |
381 | ctx->buf_offset += lib_ring_buffer_align(ctx->buf_offset, | |
382 | alignment); | |
383 | } | |
384 | ||
385 | /* | |
386 | * lib_ring_buffer_check_config() returns 0 on success. | |
387 | * Used internally to check for valid configurations at channel creation. | |
388 | */ | |
a8909ba5 PW |
389 | static inline lttng_ust_notrace |
390 | int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config, | |
391 | unsigned int switch_timer_interval, | |
392 | unsigned int read_timer_interval); | |
a6352fd4 | 393 | static inline |
4cfec15c | 394 | int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config, |
a6352fd4 MD |
395 | unsigned int switch_timer_interval, |
396 | unsigned int read_timer_interval) | |
397 | { | |
398 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL | |
399 | && config->sync == RING_BUFFER_SYNC_PER_CPU | |
400 | && switch_timer_interval) | |
401 | return -EINVAL; | |
402 | return 0; | |
403 | } | |
404 | ||
e92f3e28 | 405 | #endif /* _LTTNG_RING_BUFFER_CONFIG_H */ |