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. | |
a6352fd4 MD |
21 | */ |
22 | ||
23 | #include <errno.h> | |
4318ae1b | 24 | #include "lttng/ust-tracer.h" |
b728d87e MD |
25 | #include <stdint.h> |
26 | #include <stddef.h> | |
27 | #include <urcu/arch.h> | |
2e707419 | 28 | #include <string.h> |
4318ae1b | 29 | #include "lttng/align.h" |
a8909ba5 | 30 | #include <lttng/ust-compiler.h> |
a6352fd4 | 31 | |
4cfec15c | 32 | struct lttng_ust_lib_ring_buffer; |
a6352fd4 | 33 | struct channel; |
4cfec15c MD |
34 | struct lttng_ust_lib_ring_buffer_config; |
35 | struct lttng_ust_lib_ring_buffer_ctx; | |
5a821cd6 | 36 | struct lttng_ust_shm_handle; |
a6352fd4 MD |
37 | |
38 | /* | |
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 | |
42 | * the client. | |
43 | */ | |
4cfec15c | 44 | struct lttng_ust_lib_ring_buffer_client_cb { |
a6352fd4 MD |
45 | /* Mandatory callbacks */ |
46 | ||
47 | /* A static inline version is also required for fast path */ | |
2fed87ae | 48 | uint64_t (*ring_buffer_clock_read) (struct channel *chan); |
4cfec15c | 49 | size_t (*record_header_size) (const struct lttng_ust_lib_ring_buffer_config *config, |
a6352fd4 MD |
50 | struct channel *chan, size_t offset, |
51 | size_t *pre_header_padding, | |
4cfec15c | 52 | struct lttng_ust_lib_ring_buffer_ctx *ctx); |
a6352fd4 MD |
53 | |
54 | /* Slow path only, at subbuffer switch */ | |
55 | size_t (*subbuffer_header_size) (void); | |
2fed87ae | 56 | void (*buffer_begin) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 57 | unsigned int subbuf_idx, |
38fae1d3 | 58 | struct lttng_ust_shm_handle *handle); |
2fed87ae | 59 | void (*buffer_end) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 60 | unsigned int subbuf_idx, unsigned long data_size, |
38fae1d3 | 61 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
62 | |
63 | /* Optional callbacks (can be set to NULL) */ | |
64 | ||
65 | /* Called at buffer creation/finalize */ | |
4cfec15c | 66 | int (*buffer_create) (struct lttng_ust_lib_ring_buffer *buf, void *priv, |
1d498196 | 67 | int cpu, const char *name, |
38fae1d3 | 68 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
69 | /* |
70 | * Clients should guarantee that no new reader handle can be opened | |
71 | * after finalize. | |
72 | */ | |
4cfec15c | 73 | void (*buffer_finalize) (struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 74 | void *priv, int cpu, |
38fae1d3 | 75 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
76 | |
77 | /* | |
78 | * Extract header length, payload length and timestamp from event | |
79 | * record. Used by buffer iterators. Timestamp is only used by channel | |
80 | * iterator. | |
81 | */ | |
4cfec15c MD |
82 | void (*record_get) (const struct lttng_ust_lib_ring_buffer_config *config, |
83 | struct channel *chan, struct lttng_ust_lib_ring_buffer *buf, | |
a6352fd4 | 84 | size_t offset, size_t *header_len, |
2fed87ae | 85 | size_t *payload_len, uint64_t *timestamp, |
38fae1d3 | 86 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
87 | }; |
88 | ||
89 | /* | |
90 | * Ring buffer instance configuration. | |
91 | * | |
92 | * Declare as "static const" within the client object to ensure the inline fast | |
93 | * paths can be optimized. | |
94 | * | |
95 | * alloc/sync pairs: | |
96 | * | |
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()). | |
101 | * | |
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. | |
105 | * | |
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. | |
110 | * | |
111 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL : | |
112 | * Global shared buffer with global synchronization. | |
113 | * | |
114 | * wakeup: | |
115 | * | |
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. | |
120 | * | |
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 | |
123 | * for drivers. | |
124 | * | |
125 | * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client | |
126 | * has the responsibility to perform wakeups. | |
127 | */ | |
2e707419 | 128 | #define LTTNG_UST_RING_BUFFER_CONFIG_PADDING 32 |
4cfec15c | 129 | struct lttng_ust_lib_ring_buffer_config { |
a6352fd4 MD |
130 | enum { |
131 | RING_BUFFER_ALLOC_PER_CPU, | |
132 | RING_BUFFER_ALLOC_GLOBAL, | |
133 | } alloc; | |
134 | enum { | |
135 | RING_BUFFER_SYNC_PER_CPU, /* Wait-free */ | |
136 | RING_BUFFER_SYNC_GLOBAL, /* Lock-free */ | |
137 | } sync; | |
138 | enum { | |
139 | RING_BUFFER_OVERWRITE, /* Overwrite when buffer full */ | |
140 | RING_BUFFER_DISCARD, /* Discard when buffer full */ | |
141 | } mode; | |
142 | enum { | |
143 | RING_BUFFER_SPLICE, | |
144 | RING_BUFFER_MMAP, | |
145 | RING_BUFFER_READ, /* TODO */ | |
146 | RING_BUFFER_ITERATOR, | |
147 | RING_BUFFER_NONE, | |
148 | } output; | |
149 | enum { | |
150 | RING_BUFFER_PAGE, | |
151 | RING_BUFFER_VMAP, /* TODO */ | |
152 | RING_BUFFER_STATIC, /* TODO */ | |
153 | } backend; | |
154 | enum { | |
155 | RING_BUFFER_NO_OOPS_CONSISTENCY, | |
156 | RING_BUFFER_OOPS_CONSISTENCY, | |
157 | } oops; | |
158 | enum { | |
159 | RING_BUFFER_IPI_BARRIER, | |
160 | RING_BUFFER_NO_IPI_BARRIER, | |
161 | } ipi; | |
162 | enum { | |
163 | RING_BUFFER_WAKEUP_BY_TIMER, /* wake up performed by timer */ | |
164 | RING_BUFFER_WAKEUP_BY_WRITER, /* | |
165 | * writer wakes up reader, | |
166 | * not lock-free | |
167 | * (takes spinlock). | |
168 | */ | |
169 | } wakeup; | |
170 | /* | |
171 | * tsc_bits: timestamp bits saved at each record. | |
172 | * 0 and 64 disable the timestamp compression scheme. | |
173 | */ | |
174 | unsigned int tsc_bits; | |
4cfec15c | 175 | struct lttng_ust_lib_ring_buffer_client_cb cb; |
c1fca457 MD |
176 | /* |
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. | |
180 | */ | |
181 | int client_type; | |
2e707419 | 182 | char padding[LTTNG_UST_RING_BUFFER_CONFIG_PADDING]; |
a6352fd4 MD |
183 | }; |
184 | ||
185 | /* | |
186 | * ring buffer context | |
187 | * | |
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(). | |
180901e6 MD |
191 | * |
192 | * IMPORTANT: this structure is part of the ABI between the probe and | |
193 | * UST. Fields need to be only added at the end, never reordered, never | |
194 | * removed. | |
a6352fd4 | 195 | */ |
2e707419 | 196 | #define LTTNG_UST_RING_BUFFER_CTX_PADDING 24 |
4cfec15c | 197 | struct lttng_ust_lib_ring_buffer_ctx { |
a6352fd4 MD |
198 | /* input received by lib_ring_buffer_reserve(), saved here. */ |
199 | struct channel *chan; /* channel */ | |
200 | void *priv; /* client private data */ | |
38fae1d3 | 201 | struct lttng_ust_shm_handle *handle; /* shared-memory handle */ |
a6352fd4 MD |
202 | size_t data_size; /* size of payload */ |
203 | int largest_align; /* | |
204 | * alignment of the largest element | |
205 | * in the payload | |
206 | */ | |
207 | int cpu; /* processor id */ | |
208 | ||
209 | /* output from lib_ring_buffer_reserve() */ | |
4cfec15c | 210 | struct lttng_ust_lib_ring_buffer *buf; /* |
a6352fd4 MD |
211 | * buffer corresponding to processor id |
212 | * for this channel | |
213 | */ | |
214 | size_t slot_size; /* size of the reserved slot */ | |
215 | unsigned long buf_offset; /* offset following the record header */ | |
216 | unsigned long pre_offset; /* | |
217 | * Initial offset position _before_ | |
218 | * the record is written. Positioned | |
219 | * prior to record header alignment | |
220 | * padding. | |
221 | */ | |
2fed87ae | 222 | uint64_t tsc; /* time-stamp counter value */ |
a6352fd4 | 223 | unsigned int rflags; /* reservation flags */ |
2e707419 | 224 | char padding[LTTNG_UST_RING_BUFFER_CTX_PADDING]; |
a6352fd4 MD |
225 | }; |
226 | ||
227 | /** | |
228 | * lib_ring_buffer_ctx_init - initialize ring buffer context | |
229 | * @ctx: ring buffer context to initialize | |
230 | * @chan: channel | |
231 | * @priv: client private data | |
232 | * @data_size: size of record data payload | |
233 | * @largest_align: largest alignment within data payload types | |
234 | * @cpu: processor id | |
235 | */ | |
a8909ba5 PW |
236 | static inline lttng_ust_notrace |
237 | void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
238 | struct channel *chan, void *priv, | |
239 | size_t data_size, int largest_align, | |
240 | int cpu, struct lttng_ust_shm_handle *handle); | |
a6352fd4 | 241 | static inline |
4cfec15c | 242 | void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
a6352fd4 MD |
243 | struct channel *chan, void *priv, |
244 | size_t data_size, int largest_align, | |
38fae1d3 | 245 | int cpu, struct lttng_ust_shm_handle *handle) |
a6352fd4 MD |
246 | { |
247 | ctx->chan = chan; | |
248 | ctx->priv = priv; | |
249 | ctx->data_size = data_size; | |
250 | ctx->largest_align = largest_align; | |
251 | ctx->cpu = cpu; | |
252 | ctx->rflags = 0; | |
1d498196 | 253 | ctx->handle = handle; |
2e707419 | 254 | memset(ctx->padding, 0, LTTNG_UST_RING_BUFFER_CTX_PADDING); |
a6352fd4 MD |
255 | } |
256 | ||
257 | /* | |
258 | * Reservation flags. | |
259 | * | |
260 | * RING_BUFFER_RFLAG_FULL_TSC | |
261 | * | |
262 | * This flag is passed to record_header_size() and to the primitive used to | |
263 | * write the record header. It indicates that the full 64-bit time value is | |
264 | * needed in the record header. If this flag is not set, the record header needs | |
265 | * only to contain "tsc_bits" bit of time value. | |
266 | * | |
267 | * Reservation flags can be added by the client, starting from | |
268 | * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from | |
269 | * record_header_size() to lib_ring_buffer_write_record_header(). | |
270 | */ | |
271 | #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0) | |
272 | #define RING_BUFFER_RFLAG_END (1U << 1) | |
273 | ||
274 | /* | |
275 | * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at | |
276 | * compile-time. We have to duplicate the "config->align" information and the | |
277 | * definition here because config->align is used both in the slow and fast | |
278 | * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code. | |
279 | */ | |
280 | #ifdef RING_BUFFER_ALIGN | |
281 | ||
282 | # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */ | |
283 | ||
284 | /* | |
285 | * Calculate the offset needed to align the type. | |
286 | * size_of_type must be non-zero. | |
287 | */ | |
a8909ba5 PW |
288 | static inline lttng_ust_notrace |
289 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type); | |
a6352fd4 MD |
290 | static inline |
291 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
292 | { | |
293 | return offset_align(align_drift, size_of_type); | |
294 | } | |
295 | ||
296 | #else | |
297 | ||
298 | # define RING_BUFFER_ALIGN_ATTR __attribute__((packed)) | |
299 | ||
300 | /* | |
301 | * Calculate the offset needed to align the type. | |
302 | * size_of_type must be non-zero. | |
303 | */ | |
a8909ba5 PW |
304 | static inline lttng_ust_notrace |
305 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type); | |
a6352fd4 MD |
306 | static inline |
307 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
308 | { | |
309 | return 0; | |
310 | } | |
311 | ||
312 | #endif | |
313 | ||
314 | /** | |
315 | * lib_ring_buffer_align_ctx - Align context offset on "alignment" | |
316 | * @ctx: ring buffer context. | |
317 | */ | |
a8909ba5 PW |
318 | static inline lttng_ust_notrace |
319 | void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
320 | size_t alignment); | |
a6352fd4 | 321 | static inline |
4cfec15c | 322 | void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
a6352fd4 MD |
323 | size_t alignment) |
324 | { | |
325 | ctx->buf_offset += lib_ring_buffer_align(ctx->buf_offset, | |
326 | alignment); | |
327 | } | |
328 | ||
329 | /* | |
330 | * lib_ring_buffer_check_config() returns 0 on success. | |
331 | * Used internally to check for valid configurations at channel creation. | |
332 | */ | |
a8909ba5 PW |
333 | static inline lttng_ust_notrace |
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); | |
a6352fd4 | 337 | static inline |
4cfec15c | 338 | int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config, |
a6352fd4 MD |
339 | unsigned int switch_timer_interval, |
340 | unsigned int read_timer_interval) | |
341 | { | |
342 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL | |
343 | && config->sync == RING_BUFFER_SYNC_PER_CPU | |
344 | && switch_timer_interval) | |
345 | return -EINVAL; | |
346 | return 0; | |
347 | } | |
348 | ||
e92f3e28 | 349 | #endif /* _LTTNG_RING_BUFFER_CONFIG_H */ |