Commit | Line | Data |
---|---|---|
852c2936 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
852c2936 | 3 | * |
e92f3e28 MD |
4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
c0c0989a | 6 | * Ring buffer backend (internal helpers). |
852c2936 MD |
7 | */ |
8 | ||
c0c0989a MJ |
9 | #ifndef _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H |
10 | #define _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H | |
11 | ||
b4051ad8 | 12 | #include <stddef.h> |
fb31eb73 | 13 | #include <stdint.h> |
14641deb MD |
14 | #include <unistd.h> |
15 | #include <urcu/compiler.h> | |
16 | ||
4318ae1b | 17 | #include <lttng/ringbuffer-config.h> |
4931a13e MD |
18 | #include "backend_types.h" |
19 | #include "frontend_types.h" | |
a6352fd4 | 20 | #include "shm.h" |
852c2936 MD |
21 | |
22 | /* Ring buffer backend API presented to the frontend */ | |
23 | ||
24 | /* Ring buffer and channel backend create/free */ | |
25 | ||
ddabe860 | 26 | __attribute__((visibility("hidden"))) |
4cfec15c | 27 | int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb, |
5198080d MJ |
28 | struct channel_backend *chan, |
29 | int cpu, | |
38fae1d3 | 30 | struct lttng_ust_shm_handle *handle, |
1d498196 | 31 | struct shm_object *shmobj); |
ddabe860 MJ |
32 | |
33 | __attribute__((visibility("hidden"))) | |
852c2936 | 34 | void channel_backend_unregister_notifiers(struct channel_backend *chanb); |
ddabe860 MJ |
35 | |
36 | __attribute__((visibility("hidden"))) | |
4cfec15c | 37 | void lib_ring_buffer_backend_free(struct lttng_ust_lib_ring_buffer_backend *bufb); |
ddabe860 MJ |
38 | |
39 | __attribute__((visibility("hidden"))) | |
852c2936 MD |
40 | int channel_backend_init(struct channel_backend *chanb, |
41 | const char *name, | |
4cfec15c | 42 | const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f | 43 | size_t subbuf_size, |
a9ff648c | 44 | size_t num_subbuf, struct lttng_ust_shm_handle *handle, |
5ea386c3 | 45 | const int *stream_fds); |
ddabe860 MJ |
46 | |
47 | __attribute__((visibility("hidden"))) | |
1d498196 | 48 | void channel_backend_free(struct channel_backend *chanb, |
38fae1d3 | 49 | struct lttng_ust_shm_handle *handle); |
852c2936 | 50 | |
ddabe860 | 51 | __attribute__((visibility("hidden"))) |
4cfec15c | 52 | void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb, |
38fae1d3 | 53 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
54 | |
55 | __attribute__((visibility("hidden"))) | |
852c2936 MD |
56 | void channel_backend_reset(struct channel_backend *chanb); |
57 | ||
ddabe860 | 58 | __attribute__((visibility("hidden"))) |
852c2936 | 59 | int lib_ring_buffer_backend_init(void); |
ddabe860 MJ |
60 | |
61 | __attribute__((visibility("hidden"))) | |
852c2936 MD |
62 | void lib_ring_buffer_backend_exit(void); |
63 | ||
ddabe860 | 64 | __attribute__((visibility("hidden"))) |
4cfec15c | 65 | extern void _lib_ring_buffer_write(struct lttng_ust_lib_ring_buffer_backend *bufb, |
852c2936 MD |
66 | size_t offset, const void *src, size_t len, |
67 | ssize_t pagecpy); | |
68 | ||
69 | /* | |
70 | * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be | |
71 | * exchanged atomically. | |
72 | * | |
73 | * Top half word, except lowest bit, belongs to "offset", which is used to keep | |
74 | * to count the produced buffers. For overwrite mode, this provides the | |
75 | * consumer with the capacity to read subbuffers in order, handling the | |
76 | * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit | |
77 | * systems) concurrently with a single execution of get_subbuf (between offset | |
78 | * sampling and subbuffer ID exchange). | |
79 | */ | |
80 | ||
14641deb | 81 | #define HALF_ULONG_BITS (CAA_BITS_PER_LONG >> 1) |
852c2936 MD |
82 | |
83 | #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1) | |
84 | #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT) | |
85 | #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1)) | |
86 | /* | |
87 | * Lowest bit of top word half belongs to noref. Used only for overwrite mode. | |
88 | */ | |
89 | #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1) | |
90 | #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT) | |
91 | #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT | |
92 | /* | |
93 | * In overwrite mode: lowest half of word is used for index. | |
94 | * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit. | |
95 | * In producer-consumer mode: whole word used for index. | |
96 | */ | |
97 | #define SB_ID_INDEX_SHIFT 0 | |
98 | #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT) | |
99 | #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1) | |
100 | ||
101 | /* | |
102 | * Construct the subbuffer id from offset, index and noref. Use only the index | |
103 | * for producer-consumer mode (offset and noref are only used in overwrite | |
104 | * mode). | |
105 | */ | |
106 | static inline | |
4cfec15c | 107 | unsigned long subbuffer_id(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
108 | unsigned long offset, unsigned long noref, |
109 | unsigned long index) | |
110 | { | |
111 | if (config->mode == RING_BUFFER_OVERWRITE) | |
112 | return (offset << SB_ID_OFFSET_SHIFT) | |
113 | | (noref << SB_ID_NOREF_SHIFT) | |
114 | | index; | |
115 | else | |
116 | return index; | |
117 | } | |
118 | ||
119 | /* | |
120 | * Compare offset with the offset contained within id. Return 1 if the offset | |
121 | * bits are identical, else 0. | |
122 | */ | |
123 | static inline | |
4cfec15c | 124 | int subbuffer_id_compare_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
125 | unsigned long id, unsigned long offset) |
126 | { | |
127 | return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT); | |
128 | } | |
129 | ||
130 | static inline | |
4cfec15c | 131 | unsigned long subbuffer_id_get_index(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
132 | unsigned long id) |
133 | { | |
134 | if (config->mode == RING_BUFFER_OVERWRITE) | |
135 | return id & SB_ID_INDEX_MASK; | |
136 | else | |
137 | return id; | |
138 | } | |
139 | ||
140 | static inline | |
4cfec15c | 141 | unsigned long subbuffer_id_is_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
142 | unsigned long id) |
143 | { | |
144 | if (config->mode == RING_BUFFER_OVERWRITE) | |
145 | return !!(id & SB_ID_NOREF_MASK); | |
146 | else | |
147 | return 1; | |
148 | } | |
149 | ||
150 | /* | |
151 | * Only used by reader on subbuffer ID it has exclusive access to. No volatile | |
152 | * needed. | |
153 | */ | |
154 | static inline | |
4cfec15c | 155 | void subbuffer_id_set_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
156 | unsigned long *id) |
157 | { | |
158 | if (config->mode == RING_BUFFER_OVERWRITE) | |
159 | *id |= SB_ID_NOREF_MASK; | |
160 | } | |
161 | ||
162 | static inline | |
4cfec15c | 163 | void subbuffer_id_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
164 | unsigned long *id, unsigned long offset) |
165 | { | |
166 | unsigned long tmp; | |
167 | ||
168 | if (config->mode == RING_BUFFER_OVERWRITE) { | |
169 | tmp = *id; | |
170 | tmp &= ~SB_ID_OFFSET_MASK; | |
171 | tmp |= offset << SB_ID_OFFSET_SHIFT; | |
172 | tmp |= SB_ID_NOREF_MASK; | |
173 | /* Volatile store, read concurrently by readers. */ | |
14641deb | 174 | CMM_ACCESS_ONCE(*id) = tmp; |
852c2936 MD |
175 | } |
176 | } | |
177 | ||
178 | /* No volatile access, since already used locally */ | |
179 | static inline | |
4cfec15c | 180 | void subbuffer_id_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
181 | unsigned long *id) |
182 | { | |
183 | if (config->mode == RING_BUFFER_OVERWRITE) | |
184 | *id &= ~SB_ID_NOREF_MASK; | |
185 | } | |
186 | ||
187 | /* | |
188 | * For overwrite mode, cap the number of subbuffers per buffer to: | |
189 | * 2^16 on 32-bit architectures | |
190 | * 2^32 on 64-bit architectures | |
191 | * This is required to fit in the index part of the ID. Return 0 on success, | |
192 | * -EPERM on failure. | |
193 | */ | |
194 | static inline | |
4cfec15c | 195 | int subbuffer_id_check_index(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
196 | unsigned long num_subbuf) |
197 | { | |
198 | if (config->mode == RING_BUFFER_OVERWRITE) | |
199 | return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0; | |
200 | else | |
201 | return 0; | |
202 | } | |
203 | ||
a3492932 MD |
204 | static inline |
205 | int lib_ring_buffer_backend_get_pages(const struct lttng_ust_lib_ring_buffer_config *config, | |
206 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
207 | struct lttng_ust_lib_ring_buffer_backend_pages **backend_pages) | |
208 | { | |
209 | struct lttng_ust_lib_ring_buffer_backend *bufb = &ctx->buf->backend; | |
210 | struct channel_backend *chanb = &ctx->chan->backend; | |
211 | struct lttng_ust_shm_handle *handle = ctx->handle; | |
212 | size_t sbidx; | |
213 | size_t offset = ctx->buf_offset; | |
214 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; | |
215 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
216 | unsigned long sb_bindex, id; | |
217 | struct lttng_ust_lib_ring_buffer_backend_pages *_backend_pages; | |
218 | ||
219 | offset &= chanb->buf_size - 1; | |
220 | sbidx = offset >> chanb->subbuf_size_order; | |
221 | wsb = shmp_index(handle, bufb->buf_wsb, sbidx); | |
222 | if (caa_unlikely(!wsb)) | |
223 | return -1; | |
224 | id = wsb->id; | |
225 | sb_bindex = subbuffer_id_get_index(config, id); | |
226 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
227 | if (caa_unlikely(!rpages)) | |
228 | return -1; | |
229 | CHAN_WARN_ON(ctx->chan, | |
230 | config->mode == RING_BUFFER_OVERWRITE | |
231 | && subbuffer_id_is_noref(config, id)); | |
232 | _backend_pages = shmp(handle, rpages->shmp); | |
233 | if (caa_unlikely(!_backend_pages)) | |
234 | return -1; | |
235 | *backend_pages = _backend_pages; | |
236 | return 0; | |
237 | } | |
238 | ||
239 | /* Get backend pages from cache. */ | |
240 | static inline | |
241 | struct lttng_ust_lib_ring_buffer_backend_pages * | |
242 | lib_ring_buffer_get_backend_pages_from_ctx(const struct lttng_ust_lib_ring_buffer_config *config, | |
243 | struct lttng_ust_lib_ring_buffer_ctx *ctx) | |
244 | { | |
a3492932 MD |
245 | return ctx->backend_pages; |
246 | } | |
247 | ||
9c995331 MD |
248 | /* |
249 | * The ring buffer can count events recorded and overwritten per buffer, | |
250 | * but it is disabled by default due to its performance overhead. | |
251 | */ | |
252 | #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS | |
852c2936 | 253 | static inline |
4cfec15c | 254 | void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config, |
15500a1b | 255 | const struct lttng_ust_lib_ring_buffer_ctx *ctx, |
4cfec15c | 256 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
38fae1d3 | 257 | unsigned long idx, struct lttng_ust_shm_handle *handle) |
852c2936 | 258 | { |
15500a1b | 259 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
852c2936 | 260 | |
15500a1b MD |
261 | backend_pages = lib_ring_buffer_get_backend_pages_from_ctx(config, ctx); |
262 | if (caa_unlikely(!backend_pages)) { | |
263 | if (lib_ring_buffer_backend_get_pages(config, ctx, &backend_pages)) | |
264 | return; | |
265 | } | |
266 | v_inc(config, &backend_pages->records_commit); | |
852c2936 | 267 | } |
9c995331 MD |
268 | #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */ |
269 | static inline | |
270 | void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config, | |
15500a1b | 271 | const struct lttng_ust_lib_ring_buffer_ctx *ctx, |
9c995331 MD |
272 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
273 | unsigned long idx, struct lttng_ust_shm_handle *handle) | |
274 | { | |
275 | } | |
276 | #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */ | |
852c2936 MD |
277 | |
278 | /* | |
279 | * Reader has exclusive subbuffer access for record consumption. No need to | |
280 | * perform the decrement atomically. | |
281 | */ | |
282 | static inline | |
4cfec15c MD |
283 | void subbuffer_consume_record(const struct lttng_ust_lib_ring_buffer_config *config, |
284 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
38fae1d3 | 285 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
286 | { |
287 | unsigned long sb_bindex; | |
5198080d | 288 | struct lttng_ust_lib_ring_buffer_channel *chan; |
15500a1b MD |
289 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages_shmp; |
290 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 MD |
291 | |
292 | sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
15500a1b MD |
293 | chan = shmp(handle, bufb->chan); |
294 | if (!chan) | |
295 | return; | |
296 | pages_shmp = shmp_index(handle, bufb->array, sb_bindex); | |
297 | if (!pages_shmp) | |
298 | return; | |
299 | backend_pages = shmp(handle, pages_shmp->shmp); | |
300 | if (!backend_pages) | |
301 | return; | |
302 | CHAN_WARN_ON(chan, !v_read(config, &backend_pages->records_unread)); | |
852c2936 | 303 | /* Non-atomic decrement protected by exclusive subbuffer access */ |
15500a1b | 304 | _v_dec(config, &backend_pages->records_unread); |
852c2936 MD |
305 | v_inc(config, &bufb->records_read); |
306 | } | |
307 | ||
308 | static inline | |
309 | unsigned long subbuffer_get_records_count( | |
4cfec15c MD |
310 | const struct lttng_ust_lib_ring_buffer_config *config, |
311 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 312 | unsigned long idx, |
38fae1d3 | 313 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
314 | { |
315 | unsigned long sb_bindex; | |
15500a1b MD |
316 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
317 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
318 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 | 319 | |
15500a1b MD |
320 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
321 | if (!wsb) | |
322 | return 0; | |
323 | sb_bindex = subbuffer_id_get_index(config, wsb->id); | |
324 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
325 | if (!rpages) | |
326 | return 0; | |
327 | backend_pages = shmp(handle, rpages->shmp); | |
328 | if (!backend_pages) | |
329 | return 0; | |
330 | return v_read(config, &backend_pages->records_commit); | |
852c2936 MD |
331 | } |
332 | ||
333 | /* | |
334 | * Must be executed at subbuffer delivery when the writer has _exclusive_ | |
3d1aec25 MD |
335 | * subbuffer access. See lib_ring_buffer_check_deliver() for details. |
336 | * lib_ring_buffer_get_records_count() must be called to get the records | |
337 | * count before this function, because it resets the records_commit | |
338 | * count. | |
852c2936 MD |
339 | */ |
340 | static inline | |
341 | unsigned long subbuffer_count_records_overrun( | |
4cfec15c MD |
342 | const struct lttng_ust_lib_ring_buffer_config *config, |
343 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 344 | unsigned long idx, |
38fae1d3 | 345 | struct lttng_ust_shm_handle *handle) |
852c2936 | 346 | { |
852c2936 | 347 | unsigned long overruns, sb_bindex; |
15500a1b MD |
348 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
349 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
350 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 | 351 | |
15500a1b MD |
352 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
353 | if (!wsb) | |
354 | return 0; | |
355 | sb_bindex = subbuffer_id_get_index(config, wsb->id); | |
356 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
357 | if (!rpages) | |
358 | return 0; | |
359 | backend_pages = shmp(handle, rpages->shmp); | |
360 | if (!backend_pages) | |
361 | return 0; | |
362 | overruns = v_read(config, &backend_pages->records_unread); | |
363 | v_set(config, &backend_pages->records_unread, | |
364 | v_read(config, &backend_pages->records_commit)); | |
365 | v_set(config, &backend_pages->records_commit, 0); | |
852c2936 MD |
366 | |
367 | return overruns; | |
368 | } | |
369 | ||
370 | static inline | |
4cfec15c MD |
371 | void subbuffer_set_data_size(const struct lttng_ust_lib_ring_buffer_config *config, |
372 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
852c2936 | 373 | unsigned long idx, |
1d498196 | 374 | unsigned long data_size, |
38fae1d3 | 375 | struct lttng_ust_shm_handle *handle) |
852c2936 | 376 | { |
852c2936 | 377 | unsigned long sb_bindex; |
15500a1b MD |
378 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
379 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
380 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 | 381 | |
15500a1b MD |
382 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
383 | if (!wsb) | |
384 | return; | |
385 | sb_bindex = subbuffer_id_get_index(config, wsb->id); | |
386 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
387 | if (!rpages) | |
388 | return; | |
389 | backend_pages = shmp(handle, rpages->shmp); | |
390 | if (!backend_pages) | |
391 | return; | |
392 | backend_pages->data_size = data_size; | |
852c2936 MD |
393 | } |
394 | ||
395 | static inline | |
396 | unsigned long subbuffer_get_read_data_size( | |
4cfec15c MD |
397 | const struct lttng_ust_lib_ring_buffer_config *config, |
398 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
38fae1d3 | 399 | struct lttng_ust_shm_handle *handle) |
852c2936 | 400 | { |
852c2936 | 401 | unsigned long sb_bindex; |
15500a1b MD |
402 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages_shmp; |
403 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 MD |
404 | |
405 | sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
15500a1b MD |
406 | pages_shmp = shmp_index(handle, bufb->array, sb_bindex); |
407 | if (!pages_shmp) | |
408 | return 0; | |
409 | backend_pages = shmp(handle, pages_shmp->shmp); | |
410 | if (!backend_pages) | |
411 | return 0; | |
412 | return backend_pages->data_size; | |
852c2936 MD |
413 | } |
414 | ||
415 | static inline | |
416 | unsigned long subbuffer_get_data_size( | |
4cfec15c MD |
417 | const struct lttng_ust_lib_ring_buffer_config *config, |
418 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 419 | unsigned long idx, |
38fae1d3 | 420 | struct lttng_ust_shm_handle *handle) |
852c2936 | 421 | { |
852c2936 | 422 | unsigned long sb_bindex; |
15500a1b MD |
423 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
424 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
425 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 | 426 | |
15500a1b MD |
427 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
428 | if (!wsb) | |
429 | return 0; | |
430 | sb_bindex = subbuffer_id_get_index(config, wsb->id); | |
431 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
432 | if (!rpages) | |
433 | return 0; | |
434 | backend_pages = shmp(handle, rpages->shmp); | |
435 | if (!backend_pages) | |
436 | return 0; | |
437 | return backend_pages->data_size; | |
852c2936 MD |
438 | } |
439 | ||
1ff31389 JD |
440 | static inline |
441 | void subbuffer_inc_packet_count(const struct lttng_ust_lib_ring_buffer_config *config, | |
442 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
443 | unsigned long idx, struct lttng_ust_shm_handle *handle) | |
444 | { | |
15500a1b MD |
445 | struct lttng_ust_lib_ring_buffer_backend_counts *counts; |
446 | ||
447 | counts = shmp_index(handle, bufb->buf_cnt, idx); | |
448 | if (!counts) | |
449 | return; | |
450 | counts->seq_cnt++; | |
1ff31389 JD |
451 | } |
452 | ||
852c2936 MD |
453 | /** |
454 | * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by | |
455 | * writer. | |
456 | */ | |
457 | static inline | |
4cfec15c MD |
458 | void lib_ring_buffer_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
459 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 460 | unsigned long idx, |
38fae1d3 | 461 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
462 | { |
463 | unsigned long id, new_id; | |
15500a1b | 464 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
852c2936 MD |
465 | |
466 | if (config->mode != RING_BUFFER_OVERWRITE) | |
467 | return; | |
468 | ||
469 | /* | |
470 | * Performing a volatile access to read the sb_pages, because we want to | |
471 | * read a coherent version of the pointer and the associated noref flag. | |
472 | */ | |
15500a1b MD |
473 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
474 | if (!wsb) | |
475 | return; | |
476 | id = CMM_ACCESS_ONCE(wsb->id); | |
852c2936 MD |
477 | for (;;) { |
478 | /* This check is called on the fast path for each record. */ | |
b5a3dfa5 | 479 | if (caa_likely(!subbuffer_id_is_noref(config, id))) { |
852c2936 MD |
480 | /* |
481 | * Store after load dependency ordering the writes to | |
482 | * the subbuffer after load and test of the noref flag | |
483 | * matches the memory barrier implied by the cmpxchg() | |
484 | * in update_read_sb_index(). | |
485 | */ | |
486 | return; /* Already writing to this buffer */ | |
487 | } | |
488 | new_id = id; | |
489 | subbuffer_id_clear_noref(config, &new_id); | |
15500a1b | 490 | new_id = uatomic_cmpxchg(&wsb->id, id, new_id); |
b5a3dfa5 | 491 | if (caa_likely(new_id == id)) |
852c2936 MD |
492 | break; |
493 | id = new_id; | |
494 | } | |
495 | } | |
496 | ||
497 | /** | |
498 | * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset, | |
499 | * called by writer. | |
500 | */ | |
501 | static inline | |
4cfec15c MD |
502 | void lib_ring_buffer_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
503 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 504 | unsigned long idx, unsigned long offset, |
38fae1d3 | 505 | struct lttng_ust_shm_handle *handle) |
852c2936 | 506 | { |
15500a1b | 507 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
5198080d | 508 | struct lttng_ust_lib_ring_buffer_channel *chan; |
15500a1b | 509 | |
852c2936 MD |
510 | if (config->mode != RING_BUFFER_OVERWRITE) |
511 | return; | |
512 | ||
15500a1b MD |
513 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
514 | if (!wsb) | |
515 | return; | |
852c2936 MD |
516 | /* |
517 | * Because ring_buffer_set_noref() is only called by a single thread | |
518 | * (the one which updated the cc_sb value), there are no concurrent | |
519 | * updates to take care of: other writers have not updated cc_sb, so | |
520 | * they cannot set the noref flag, and concurrent readers cannot modify | |
521 | * the pointer because the noref flag is not set yet. | |
522 | * The smp_wmb() in ring_buffer_commit() takes care of ordering writes | |
523 | * to the subbuffer before this set noref operation. | |
524 | * subbuffer_set_noref() uses a volatile store to deal with concurrent | |
525 | * readers of the noref flag. | |
526 | */ | |
15500a1b MD |
527 | chan = shmp(handle, bufb->chan); |
528 | if (!chan) | |
529 | return; | |
530 | CHAN_WARN_ON(chan, subbuffer_id_is_noref(config, wsb->id)); | |
852c2936 MD |
531 | /* |
532 | * Memory barrier that ensures counter stores are ordered before set | |
533 | * noref and offset. | |
534 | */ | |
14641deb | 535 | cmm_smp_mb(); |
15500a1b | 536 | subbuffer_id_set_noref_offset(config, &wsb->id, offset); |
852c2936 MD |
537 | } |
538 | ||
539 | /** | |
540 | * update_read_sb_index - Read-side subbuffer index update. | |
541 | */ | |
542 | static inline | |
4cfec15c MD |
543 | int update_read_sb_index(const struct lttng_ust_lib_ring_buffer_config *config, |
544 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
852c2936 MD |
545 | struct channel_backend *chanb, |
546 | unsigned long consumed_idx, | |
1d498196 | 547 | unsigned long consumed_count, |
38fae1d3 | 548 | struct lttng_ust_shm_handle *handle) |
852c2936 | 549 | { |
15500a1b | 550 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
852c2936 MD |
551 | unsigned long old_id, new_id; |
552 | ||
15500a1b MD |
553 | wsb = shmp_index(handle, bufb->buf_wsb, consumed_idx); |
554 | if (caa_unlikely(!wsb)) | |
555 | return -EPERM; | |
556 | ||
852c2936 | 557 | if (config->mode == RING_BUFFER_OVERWRITE) { |
5198080d | 558 | struct lttng_ust_lib_ring_buffer_channel *chan; |
15500a1b | 559 | |
852c2936 MD |
560 | /* |
561 | * Exchange the target writer subbuffer with our own unused | |
14641deb | 562 | * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the |
852c2936 MD |
563 | * old_wpage, because the value read will be confirmed by the |
564 | * following cmpxchg(). | |
565 | */ | |
15500a1b | 566 | old_id = wsb->id; |
b5a3dfa5 | 567 | if (caa_unlikely(!subbuffer_id_is_noref(config, old_id))) |
852c2936 MD |
568 | return -EAGAIN; |
569 | /* | |
570 | * Make sure the offset count we are expecting matches the one | |
571 | * indicated by the writer. | |
572 | */ | |
b5a3dfa5 | 573 | if (caa_unlikely(!subbuffer_id_compare_offset(config, old_id, |
852c2936 MD |
574 | consumed_count))) |
575 | return -EAGAIN; | |
15500a1b MD |
576 | chan = shmp(handle, bufb->chan); |
577 | if (caa_unlikely(!chan)) | |
578 | return -EPERM; | |
579 | CHAN_WARN_ON(chan, !subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
852c2936 MD |
580 | subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id, |
581 | consumed_count); | |
15500a1b | 582 | new_id = uatomic_cmpxchg(&wsb->id, old_id, bufb->buf_rsb.id); |
b5a3dfa5 | 583 | if (caa_unlikely(old_id != new_id)) |
852c2936 MD |
584 | return -EAGAIN; |
585 | bufb->buf_rsb.id = new_id; | |
586 | } else { | |
587 | /* No page exchange, use the writer page directly */ | |
15500a1b | 588 | bufb->buf_rsb.id = wsb->id; |
852c2936 MD |
589 | } |
590 | return 0; | |
591 | } | |
592 | ||
0d4aa2df MD |
593 | #ifndef inline_memcpy |
594 | #define inline_memcpy(dest, src, n) memcpy(dest, src, n) | |
595 | #endif | |
596 | ||
51b8f2fa MD |
597 | static inline __attribute__((always_inline)) |
598 | void lttng_inline_memcpy(void *dest, const void *src, | |
599 | unsigned long len) | |
600 | { | |
601 | switch (len) { | |
602 | case 1: | |
603 | *(uint8_t *) dest = *(const uint8_t *) src; | |
604 | break; | |
605 | case 2: | |
606 | *(uint16_t *) dest = *(const uint16_t *) src; | |
607 | break; | |
608 | case 4: | |
609 | *(uint32_t *) dest = *(const uint32_t *) src; | |
610 | break; | |
611 | case 8: | |
612 | *(uint64_t *) dest = *(const uint64_t *) src; | |
613 | break; | |
614 | default: | |
615 | inline_memcpy(dest, src, len); | |
616 | } | |
617 | } | |
618 | ||
852c2936 MD |
619 | /* |
620 | * Use the architecture-specific memcpy implementation for constant-sized | |
621 | * inputs, but rely on an inline memcpy for length statically unknown. | |
622 | * The function call to memcpy is just way too expensive for a fast path. | |
623 | */ | |
624 | #define lib_ring_buffer_do_copy(config, dest, src, len) \ | |
625 | do { \ | |
626 | size_t __len = (len); \ | |
627 | if (__builtin_constant_p(len)) \ | |
628 | memcpy(dest, src, __len); \ | |
629 | else \ | |
51b8f2fa | 630 | lttng_inline_memcpy(dest, src, __len); \ |
852c2936 MD |
631 | } while (0) |
632 | ||
a44c74d9 MD |
633 | /* |
634 | * write len bytes to dest with c | |
635 | */ | |
636 | static inline | |
637 | void lib_ring_buffer_do_memset(char *dest, int c, unsigned long len) | |
638 | { | |
639 | unsigned long i; | |
640 | ||
641 | for (i = 0; i < len; i++) | |
642 | dest[i] = c; | |
643 | } | |
644 | ||
b728d87e MD |
645 | /* arch-agnostic implementation */ |
646 | ||
bfd26582 | 647 | static inline int lttng_ust_fls(unsigned int x) |
b728d87e MD |
648 | { |
649 | int r = 32; | |
650 | ||
651 | if (!x) | |
652 | return 0; | |
653 | if (!(x & 0xFFFF0000U)) { | |
654 | x <<= 16; | |
655 | r -= 16; | |
656 | } | |
657 | if (!(x & 0xFF000000U)) { | |
658 | x <<= 8; | |
659 | r -= 8; | |
660 | } | |
661 | if (!(x & 0xF0000000U)) { | |
662 | x <<= 4; | |
663 | r -= 4; | |
664 | } | |
665 | if (!(x & 0xC0000000U)) { | |
666 | x <<= 2; | |
667 | r -= 2; | |
668 | } | |
669 | if (!(x & 0x80000000U)) { | |
e2bd33a5 | 670 | /* No need to bit shift on last operation */ |
b728d87e MD |
671 | r -= 1; |
672 | } | |
673 | return r; | |
674 | } | |
675 | ||
676 | static inline int get_count_order(unsigned int count) | |
677 | { | |
678 | int order; | |
679 | ||
bfd26582 | 680 | order = lttng_ust_fls(count) - 1; |
b728d87e MD |
681 | if (count & (count - 1)) |
682 | order++; | |
683 | return order; | |
684 | } | |
685 | ||
e92f3e28 | 686 | #endif /* _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H */ |