Commit | Line | Data |
---|---|---|
a6352fd4 MD |
1 | #ifndef _LINUX_RING_BUFFER_CONFIG_H |
2 | #define _LINUX_RING_BUFFER_CONFIG_H | |
3 | ||
4 | /* | |
5 | * linux/ringbuffer/config.h | |
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 | * | |
a60d70e6 MD |
12 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
13 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. | |
14 | * | |
15 | * Permission is hereby granted to use or copy this program | |
16 | * for any purpose, provided the above notices are retained on all copies. | |
17 | * Permission to modify the code and to distribute modified code is granted, | |
18 | * provided the above notices are retained, and a notice that the code was | |
19 | * modified is included with the above copyright notice. | |
a6352fd4 MD |
20 | */ |
21 | ||
22 | #include <errno.h> | |
4318ae1b MD |
23 | #include "lttng/ust-tracer.h" |
24 | #include "lttng/usterr-signal-safe.h" | |
b728d87e MD |
25 | #include <stdint.h> |
26 | #include <stddef.h> | |
27 | #include <urcu/arch.h> | |
4318ae1b | 28 | #include "lttng/align.h" |
a6352fd4 | 29 | |
4cfec15c | 30 | struct lttng_ust_lib_ring_buffer; |
a6352fd4 | 31 | struct channel; |
4cfec15c MD |
32 | struct lttng_ust_lib_ring_buffer_config; |
33 | struct lttng_ust_lib_ring_buffer_ctx; | |
38fae1d3 | 34 | struct lttng_ust_shm_handle *handle; |
a6352fd4 MD |
35 | |
36 | /* | |
37 | * Ring buffer client callbacks. Only used by slow path, never on fast path. | |
38 | * For the fast path, record_header_size(), ring_buffer_clock_read() should be | |
39 | * provided as inline functions too. These may simply return 0 if not used by | |
40 | * the client. | |
41 | */ | |
4cfec15c | 42 | struct lttng_ust_lib_ring_buffer_client_cb { |
a6352fd4 MD |
43 | /* Mandatory callbacks */ |
44 | ||
45 | /* A static inline version is also required for fast path */ | |
2fed87ae | 46 | uint64_t (*ring_buffer_clock_read) (struct channel *chan); |
4cfec15c | 47 | size_t (*record_header_size) (const struct lttng_ust_lib_ring_buffer_config *config, |
a6352fd4 MD |
48 | struct channel *chan, size_t offset, |
49 | size_t *pre_header_padding, | |
4cfec15c | 50 | struct lttng_ust_lib_ring_buffer_ctx *ctx); |
a6352fd4 MD |
51 | |
52 | /* Slow path only, at subbuffer switch */ | |
53 | size_t (*subbuffer_header_size) (void); | |
2fed87ae | 54 | void (*buffer_begin) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 55 | unsigned int subbuf_idx, |
38fae1d3 | 56 | struct lttng_ust_shm_handle *handle); |
2fed87ae | 57 | void (*buffer_end) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 58 | unsigned int subbuf_idx, unsigned long data_size, |
38fae1d3 | 59 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
60 | |
61 | /* Optional callbacks (can be set to NULL) */ | |
62 | ||
63 | /* Called at buffer creation/finalize */ | |
4cfec15c | 64 | int (*buffer_create) (struct lttng_ust_lib_ring_buffer *buf, void *priv, |
1d498196 | 65 | int cpu, const char *name, |
38fae1d3 | 66 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
67 | /* |
68 | * Clients should guarantee that no new reader handle can be opened | |
69 | * after finalize. | |
70 | */ | |
4cfec15c | 71 | void (*buffer_finalize) (struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 72 | void *priv, int cpu, |
38fae1d3 | 73 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
74 | |
75 | /* | |
76 | * Extract header length, payload length and timestamp from event | |
77 | * record. Used by buffer iterators. Timestamp is only used by channel | |
78 | * iterator. | |
79 | */ | |
4cfec15c MD |
80 | void (*record_get) (const struct lttng_ust_lib_ring_buffer_config *config, |
81 | struct channel *chan, struct lttng_ust_lib_ring_buffer *buf, | |
a6352fd4 | 82 | size_t offset, size_t *header_len, |
2fed87ae | 83 | size_t *payload_len, uint64_t *timestamp, |
38fae1d3 | 84 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
85 | }; |
86 | ||
87 | /* | |
88 | * Ring buffer instance configuration. | |
89 | * | |
90 | * Declare as "static const" within the client object to ensure the inline fast | |
91 | * paths can be optimized. | |
92 | * | |
93 | * alloc/sync pairs: | |
94 | * | |
95 | * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU : | |
96 | * Per-cpu buffers with per-cpu synchronization. Tracing must be performed | |
97 | * with preemption disabled (lib_ring_buffer_get_cpu() and | |
98 | * lib_ring_buffer_put_cpu()). | |
99 | * | |
100 | * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL : | |
101 | * Per-cpu buffer with global synchronization. Tracing can be performed with | |
102 | * preemption enabled, statistically stays on the local buffers. | |
103 | * | |
104 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU : | |
105 | * Should only be used for buffers belonging to a single thread or protected | |
106 | * by mutual exclusion by the client. Note that periodical sub-buffer switch | |
107 | * should be disabled in this kind of configuration. | |
108 | * | |
109 | * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL : | |
110 | * Global shared buffer with global synchronization. | |
111 | * | |
112 | * wakeup: | |
113 | * | |
114 | * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu deferrable timers to poll the | |
115 | * buffers and wake up readers if data is ready. Mainly useful for tracers which | |
116 | * don't want to call into the wakeup code on the tracing path. Use in | |
117 | * combination with "read_timer_interval" channel_create() argument. | |
118 | * | |
119 | * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is | |
120 | * ready to read. Lower latencies before the reader is woken up. Mainly suitable | |
121 | * for drivers. | |
122 | * | |
123 | * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client | |
124 | * has the responsibility to perform wakeups. | |
125 | */ | |
4cfec15c | 126 | struct lttng_ust_lib_ring_buffer_config { |
a6352fd4 MD |
127 | enum { |
128 | RING_BUFFER_ALLOC_PER_CPU, | |
129 | RING_BUFFER_ALLOC_GLOBAL, | |
130 | } alloc; | |
131 | enum { | |
132 | RING_BUFFER_SYNC_PER_CPU, /* Wait-free */ | |
133 | RING_BUFFER_SYNC_GLOBAL, /* Lock-free */ | |
134 | } sync; | |
135 | enum { | |
136 | RING_BUFFER_OVERWRITE, /* Overwrite when buffer full */ | |
137 | RING_BUFFER_DISCARD, /* Discard when buffer full */ | |
138 | } mode; | |
139 | enum { | |
140 | RING_BUFFER_SPLICE, | |
141 | RING_BUFFER_MMAP, | |
142 | RING_BUFFER_READ, /* TODO */ | |
143 | RING_BUFFER_ITERATOR, | |
144 | RING_BUFFER_NONE, | |
145 | } output; | |
146 | enum { | |
147 | RING_BUFFER_PAGE, | |
148 | RING_BUFFER_VMAP, /* TODO */ | |
149 | RING_BUFFER_STATIC, /* TODO */ | |
150 | } backend; | |
151 | enum { | |
152 | RING_BUFFER_NO_OOPS_CONSISTENCY, | |
153 | RING_BUFFER_OOPS_CONSISTENCY, | |
154 | } oops; | |
155 | enum { | |
156 | RING_BUFFER_IPI_BARRIER, | |
157 | RING_BUFFER_NO_IPI_BARRIER, | |
158 | } ipi; | |
159 | enum { | |
160 | RING_BUFFER_WAKEUP_BY_TIMER, /* wake up performed by timer */ | |
161 | RING_BUFFER_WAKEUP_BY_WRITER, /* | |
162 | * writer wakes up reader, | |
163 | * not lock-free | |
164 | * (takes spinlock). | |
165 | */ | |
166 | } wakeup; | |
167 | /* | |
168 | * tsc_bits: timestamp bits saved at each record. | |
169 | * 0 and 64 disable the timestamp compression scheme. | |
170 | */ | |
171 | unsigned int tsc_bits; | |
4cfec15c | 172 | struct lttng_ust_lib_ring_buffer_client_cb cb; |
c1fca457 MD |
173 | /* |
174 | * client_type is used by the consumer process (which is in a | |
175 | * different address space) to lookup the appropriate client | |
176 | * callbacks and update the cb pointers. | |
177 | */ | |
178 | int client_type; | |
a6352fd4 MD |
179 | }; |
180 | ||
181 | /* | |
182 | * ring buffer context | |
183 | * | |
184 | * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(), | |
185 | * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and | |
186 | * lib_ring_buffer_write(). | |
187 | */ | |
4cfec15c | 188 | struct lttng_ust_lib_ring_buffer_ctx { |
a6352fd4 MD |
189 | /* input received by lib_ring_buffer_reserve(), saved here. */ |
190 | struct channel *chan; /* channel */ | |
191 | void *priv; /* client private data */ | |
38fae1d3 | 192 | struct lttng_ust_shm_handle *handle; /* shared-memory handle */ |
a6352fd4 MD |
193 | size_t data_size; /* size of payload */ |
194 | int largest_align; /* | |
195 | * alignment of the largest element | |
196 | * in the payload | |
197 | */ | |
198 | int cpu; /* processor id */ | |
199 | ||
200 | /* output from lib_ring_buffer_reserve() */ | |
4cfec15c | 201 | struct lttng_ust_lib_ring_buffer *buf; /* |
a6352fd4 MD |
202 | * buffer corresponding to processor id |
203 | * for this channel | |
204 | */ | |
205 | size_t slot_size; /* size of the reserved slot */ | |
206 | unsigned long buf_offset; /* offset following the record header */ | |
207 | unsigned long pre_offset; /* | |
208 | * Initial offset position _before_ | |
209 | * the record is written. Positioned | |
210 | * prior to record header alignment | |
211 | * padding. | |
212 | */ | |
2fed87ae | 213 | uint64_t tsc; /* time-stamp counter value */ |
a6352fd4 MD |
214 | unsigned int rflags; /* reservation flags */ |
215 | }; | |
216 | ||
217 | /** | |
218 | * lib_ring_buffer_ctx_init - initialize ring buffer context | |
219 | * @ctx: ring buffer context to initialize | |
220 | * @chan: channel | |
221 | * @priv: client private data | |
222 | * @data_size: size of record data payload | |
223 | * @largest_align: largest alignment within data payload types | |
224 | * @cpu: processor id | |
225 | */ | |
226 | static inline | |
4cfec15c | 227 | void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
a6352fd4 MD |
228 | struct channel *chan, void *priv, |
229 | size_t data_size, int largest_align, | |
38fae1d3 | 230 | int cpu, struct lttng_ust_shm_handle *handle) |
a6352fd4 MD |
231 | { |
232 | ctx->chan = chan; | |
233 | ctx->priv = priv; | |
234 | ctx->data_size = data_size; | |
235 | ctx->largest_align = largest_align; | |
236 | ctx->cpu = cpu; | |
237 | ctx->rflags = 0; | |
1d498196 | 238 | ctx->handle = handle; |
a6352fd4 MD |
239 | } |
240 | ||
241 | /* | |
242 | * Reservation flags. | |
243 | * | |
244 | * RING_BUFFER_RFLAG_FULL_TSC | |
245 | * | |
246 | * This flag is passed to record_header_size() and to the primitive used to | |
247 | * write the record header. It indicates that the full 64-bit time value is | |
248 | * needed in the record header. If this flag is not set, the record header needs | |
249 | * only to contain "tsc_bits" bit of time value. | |
250 | * | |
251 | * Reservation flags can be added by the client, starting from | |
252 | * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from | |
253 | * record_header_size() to lib_ring_buffer_write_record_header(). | |
254 | */ | |
255 | #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0) | |
256 | #define RING_BUFFER_RFLAG_END (1U << 1) | |
257 | ||
258 | /* | |
259 | * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at | |
260 | * compile-time. We have to duplicate the "config->align" information and the | |
261 | * definition here because config->align is used both in the slow and fast | |
262 | * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code. | |
263 | */ | |
264 | #ifdef RING_BUFFER_ALIGN | |
265 | ||
266 | # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */ | |
267 | ||
268 | /* | |
269 | * Calculate the offset needed to align the type. | |
270 | * size_of_type must be non-zero. | |
271 | */ | |
272 | static inline | |
273 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
274 | { | |
275 | return offset_align(align_drift, size_of_type); | |
276 | } | |
277 | ||
278 | #else | |
279 | ||
280 | # define RING_BUFFER_ALIGN_ATTR __attribute__((packed)) | |
281 | ||
282 | /* | |
283 | * Calculate the offset needed to align the type. | |
284 | * size_of_type must be non-zero. | |
285 | */ | |
286 | static inline | |
287 | unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type) | |
288 | { | |
289 | return 0; | |
290 | } | |
291 | ||
292 | #endif | |
293 | ||
294 | /** | |
295 | * lib_ring_buffer_align_ctx - Align context offset on "alignment" | |
296 | * @ctx: ring buffer context. | |
297 | */ | |
298 | static inline | |
4cfec15c | 299 | void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
a6352fd4 MD |
300 | size_t alignment) |
301 | { | |
302 | ctx->buf_offset += lib_ring_buffer_align(ctx->buf_offset, | |
303 | alignment); | |
304 | } | |
305 | ||
306 | /* | |
307 | * lib_ring_buffer_check_config() returns 0 on success. | |
308 | * Used internally to check for valid configurations at channel creation. | |
309 | */ | |
310 | static inline | |
4cfec15c | 311 | int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config, |
a6352fd4 MD |
312 | unsigned int switch_timer_interval, |
313 | unsigned int read_timer_interval) | |
314 | { | |
315 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL | |
316 | && config->sync == RING_BUFFER_SYNC_PER_CPU | |
317 | && switch_timer_interval) | |
318 | return -EINVAL; | |
319 | return 0; | |
320 | } | |
321 | ||
a6352fd4 | 322 | #endif /* _LINUX_RING_BUFFER_CONFIG_H */ |