2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring buffer backend (internal helpers).
9 #ifndef _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
10 #define _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
15 #include <urcu/compiler.h>
17 #include <lttng/ringbuffer-config.h>
18 #include "backend_types.h"
19 #include "frontend_types.h"
22 /* Ring buffer backend API presented to the frontend */
24 /* Ring buffer and channel backend create/free */
26 __attribute__((visibility("hidden")))
27 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
28 struct channel_backend
*chan
, int cpu
,
29 struct lttng_ust_shm_handle
*handle
,
30 struct shm_object
*shmobj
);
32 __attribute__((visibility("hidden")))
33 void channel_backend_unregister_notifiers(struct channel_backend
*chanb
);
35 __attribute__((visibility("hidden")))
36 void lib_ring_buffer_backend_free(struct lttng_ust_lib_ring_buffer_backend
*bufb
);
38 __attribute__((visibility("hidden")))
39 int channel_backend_init(struct channel_backend
*chanb
,
41 const struct lttng_ust_lib_ring_buffer_config
*config
,
43 size_t num_subbuf
, struct lttng_ust_shm_handle
*handle
,
44 const int *stream_fds
);
46 __attribute__((visibility("hidden")))
47 void channel_backend_free(struct channel_backend
*chanb
,
48 struct lttng_ust_shm_handle
*handle
);
50 __attribute__((visibility("hidden")))
51 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
52 struct lttng_ust_shm_handle
*handle
);
54 __attribute__((visibility("hidden")))
55 void channel_backend_reset(struct channel_backend
*chanb
);
57 __attribute__((visibility("hidden")))
58 int lib_ring_buffer_backend_init(void);
60 __attribute__((visibility("hidden")))
61 void lib_ring_buffer_backend_exit(void);
63 __attribute__((visibility("hidden")))
64 extern void _lib_ring_buffer_write(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
65 size_t offset
, const void *src
, size_t len
,
69 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
70 * exchanged atomically.
72 * Top half word, except lowest bit, belongs to "offset", which is used to keep
73 * to count the produced buffers. For overwrite mode, this provides the
74 * consumer with the capacity to read subbuffers in order, handling the
75 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
76 * systems) concurrently with a single execution of get_subbuf (between offset
77 * sampling and subbuffer ID exchange).
80 #define HALF_ULONG_BITS (CAA_BITS_PER_LONG >> 1)
82 #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
83 #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
84 #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
86 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
88 #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
89 #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
90 #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
92 * In overwrite mode: lowest half of word is used for index.
93 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
94 * In producer-consumer mode: whole word used for index.
96 #define SB_ID_INDEX_SHIFT 0
97 #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
98 #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
101 * Construct the subbuffer id from offset, index and noref. Use only the index
102 * for producer-consumer mode (offset and noref are only used in overwrite
106 unsigned long subbuffer_id(const struct lttng_ust_lib_ring_buffer_config
*config
,
107 unsigned long offset
, unsigned long noref
,
110 if (config
->mode
== RING_BUFFER_OVERWRITE
)
111 return (offset
<< SB_ID_OFFSET_SHIFT
)
112 | (noref
<< SB_ID_NOREF_SHIFT
)
119 * Compare offset with the offset contained within id. Return 1 if the offset
120 * bits are identical, else 0.
123 int subbuffer_id_compare_offset(const struct lttng_ust_lib_ring_buffer_config
*config
,
124 unsigned long id
, unsigned long offset
)
126 return (id
& SB_ID_OFFSET_MASK
) == (offset
<< SB_ID_OFFSET_SHIFT
);
130 unsigned long subbuffer_id_get_index(const struct lttng_ust_lib_ring_buffer_config
*config
,
133 if (config
->mode
== RING_BUFFER_OVERWRITE
)
134 return id
& SB_ID_INDEX_MASK
;
140 unsigned long subbuffer_id_is_noref(const struct lttng_ust_lib_ring_buffer_config
*config
,
143 if (config
->mode
== RING_BUFFER_OVERWRITE
)
144 return !!(id
& SB_ID_NOREF_MASK
);
150 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
154 void subbuffer_id_set_noref(const struct lttng_ust_lib_ring_buffer_config
*config
,
157 if (config
->mode
== RING_BUFFER_OVERWRITE
)
158 *id
|= SB_ID_NOREF_MASK
;
162 void subbuffer_id_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config
*config
,
163 unsigned long *id
, unsigned long offset
)
167 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
169 tmp
&= ~SB_ID_OFFSET_MASK
;
170 tmp
|= offset
<< SB_ID_OFFSET_SHIFT
;
171 tmp
|= SB_ID_NOREF_MASK
;
172 /* Volatile store, read concurrently by readers. */
173 CMM_ACCESS_ONCE(*id
) = tmp
;
177 /* No volatile access, since already used locally */
179 void subbuffer_id_clear_noref(const struct lttng_ust_lib_ring_buffer_config
*config
,
182 if (config
->mode
== RING_BUFFER_OVERWRITE
)
183 *id
&= ~SB_ID_NOREF_MASK
;
187 * For overwrite mode, cap the number of subbuffers per buffer to:
188 * 2^16 on 32-bit architectures
189 * 2^32 on 64-bit architectures
190 * This is required to fit in the index part of the ID. Return 0 on success,
194 int subbuffer_id_check_index(const struct lttng_ust_lib_ring_buffer_config
*config
,
195 unsigned long num_subbuf
)
197 if (config
->mode
== RING_BUFFER_OVERWRITE
)
198 return (num_subbuf
> (1UL << HALF_ULONG_BITS
)) ? -EPERM
: 0;
204 int lib_ring_buffer_backend_get_pages(const struct lttng_ust_lib_ring_buffer_config
*config
,
205 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
206 struct lttng_ust_lib_ring_buffer_backend_pages
**backend_pages
)
208 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &ctx
->buf
->backend
;
209 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
210 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
212 size_t offset
= ctx
->buf_offset
;
213 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
214 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
215 unsigned long sb_bindex
, id
;
216 struct lttng_ust_lib_ring_buffer_backend_pages
*_backend_pages
;
218 offset
&= chanb
->buf_size
- 1;
219 sbidx
= offset
>> chanb
->subbuf_size_order
;
220 wsb
= shmp_index(handle
, bufb
->buf_wsb
, sbidx
);
221 if (caa_unlikely(!wsb
))
224 sb_bindex
= subbuffer_id_get_index(config
, id
);
225 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
226 if (caa_unlikely(!rpages
))
228 CHAN_WARN_ON(ctx
->chan
,
229 config
->mode
== RING_BUFFER_OVERWRITE
230 && subbuffer_id_is_noref(config
, id
));
231 _backend_pages
= shmp(handle
, rpages
->shmp
);
232 if (caa_unlikely(!_backend_pages
))
234 *backend_pages
= _backend_pages
;
238 /* Get backend pages from cache. */
240 struct lttng_ust_lib_ring_buffer_backend_pages
*
241 lib_ring_buffer_get_backend_pages_from_ctx(const struct lttng_ust_lib_ring_buffer_config
*config
,
242 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
244 return ctx
->backend_pages
;
248 * The ring buffer can count events recorded and overwritten per buffer,
249 * but it is disabled by default due to its performance overhead.
251 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
253 void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config
*config
,
254 const struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
255 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
256 unsigned long idx
, struct lttng_ust_shm_handle
*handle
)
258 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
260 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
261 if (caa_unlikely(!backend_pages
)) {
262 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
265 v_inc(config
, &backend_pages
->records_commit
);
267 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
269 void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config
*config
,
270 const struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
271 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
272 unsigned long idx
, struct lttng_ust_shm_handle
*handle
)
275 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
278 * Reader has exclusive subbuffer access for record consumption. No need to
279 * perform the decrement atomically.
282 void subbuffer_consume_record(const struct lttng_ust_lib_ring_buffer_config
*config
,
283 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
284 struct lttng_ust_shm_handle
*handle
)
286 unsigned long sb_bindex
;
287 struct channel
*chan
;
288 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*pages_shmp
;
289 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
291 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
292 chan
= shmp(handle
, bufb
->chan
);
295 pages_shmp
= shmp_index(handle
, bufb
->array
, sb_bindex
);
298 backend_pages
= shmp(handle
, pages_shmp
->shmp
);
301 CHAN_WARN_ON(chan
, !v_read(config
, &backend_pages
->records_unread
));
302 /* Non-atomic decrement protected by exclusive subbuffer access */
303 _v_dec(config
, &backend_pages
->records_unread
);
304 v_inc(config
, &bufb
->records_read
);
308 unsigned long subbuffer_get_records_count(
309 const struct lttng_ust_lib_ring_buffer_config
*config
,
310 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
312 struct lttng_ust_shm_handle
*handle
)
314 unsigned long sb_bindex
;
315 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
316 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
317 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
319 wsb
= shmp_index(handle
, bufb
->buf_wsb
, idx
);
322 sb_bindex
= subbuffer_id_get_index(config
, wsb
->id
);
323 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
326 backend_pages
= shmp(handle
, rpages
->shmp
);
329 return v_read(config
, &backend_pages
->records_commit
);
333 * Must be executed at subbuffer delivery when the writer has _exclusive_
334 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
335 * lib_ring_buffer_get_records_count() must be called to get the records
336 * count before this function, because it resets the records_commit
340 unsigned long subbuffer_count_records_overrun(
341 const struct lttng_ust_lib_ring_buffer_config
*config
,
342 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
344 struct lttng_ust_shm_handle
*handle
)
346 unsigned long overruns
, sb_bindex
;
347 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
348 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
349 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
351 wsb
= shmp_index(handle
, bufb
->buf_wsb
, idx
);
354 sb_bindex
= subbuffer_id_get_index(config
, wsb
->id
);
355 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
358 backend_pages
= shmp(handle
, rpages
->shmp
);
361 overruns
= v_read(config
, &backend_pages
->records_unread
);
362 v_set(config
, &backend_pages
->records_unread
,
363 v_read(config
, &backend_pages
->records_commit
));
364 v_set(config
, &backend_pages
->records_commit
, 0);
370 void subbuffer_set_data_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
371 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
373 unsigned long data_size
,
374 struct lttng_ust_shm_handle
*handle
)
376 unsigned long sb_bindex
;
377 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
378 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
379 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
381 wsb
= shmp_index(handle
, bufb
->buf_wsb
, idx
);
384 sb_bindex
= subbuffer_id_get_index(config
, wsb
->id
);
385 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
388 backend_pages
= shmp(handle
, rpages
->shmp
);
391 backend_pages
->data_size
= data_size
;
395 unsigned long subbuffer_get_read_data_size(
396 const struct lttng_ust_lib_ring_buffer_config
*config
,
397 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
398 struct lttng_ust_shm_handle
*handle
)
400 unsigned long sb_bindex
;
401 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*pages_shmp
;
402 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
404 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
405 pages_shmp
= shmp_index(handle
, bufb
->array
, sb_bindex
);
408 backend_pages
= shmp(handle
, pages_shmp
->shmp
);
411 return backend_pages
->data_size
;
415 unsigned long subbuffer_get_data_size(
416 const struct lttng_ust_lib_ring_buffer_config
*config
,
417 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
419 struct lttng_ust_shm_handle
*handle
)
421 unsigned long sb_bindex
;
422 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
423 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
424 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
426 wsb
= shmp_index(handle
, bufb
->buf_wsb
, idx
);
429 sb_bindex
= subbuffer_id_get_index(config
, wsb
->id
);
430 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
433 backend_pages
= shmp(handle
, rpages
->shmp
);
436 return backend_pages
->data_size
;
440 void subbuffer_inc_packet_count(const struct lttng_ust_lib_ring_buffer_config
*config
,
441 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
442 unsigned long idx
, struct lttng_ust_shm_handle
*handle
)
444 struct lttng_ust_lib_ring_buffer_backend_counts
*counts
;
446 counts
= shmp_index(handle
, bufb
->buf_cnt
, idx
);
453 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
457 void lib_ring_buffer_clear_noref(const struct lttng_ust_lib_ring_buffer_config
*config
,
458 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
460 struct lttng_ust_shm_handle
*handle
)
462 unsigned long id
, new_id
;
463 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
465 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
469 * Performing a volatile access to read the sb_pages, because we want to
470 * read a coherent version of the pointer and the associated noref flag.
472 wsb
= shmp_index(handle
, bufb
->buf_wsb
, idx
);
475 id
= CMM_ACCESS_ONCE(wsb
->id
);
477 /* This check is called on the fast path for each record. */
478 if (caa_likely(!subbuffer_id_is_noref(config
, id
))) {
480 * Store after load dependency ordering the writes to
481 * the subbuffer after load and test of the noref flag
482 * matches the memory barrier implied by the cmpxchg()
483 * in update_read_sb_index().
485 return; /* Already writing to this buffer */
488 subbuffer_id_clear_noref(config
, &new_id
);
489 new_id
= uatomic_cmpxchg(&wsb
->id
, id
, new_id
);
490 if (caa_likely(new_id
== id
))
497 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
501 void lib_ring_buffer_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config
*config
,
502 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
503 unsigned long idx
, unsigned long offset
,
504 struct lttng_ust_shm_handle
*handle
)
506 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
507 struct channel
*chan
;
509 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
512 wsb
= shmp_index(handle
, bufb
->buf_wsb
, idx
);
516 * Because ring_buffer_set_noref() is only called by a single thread
517 * (the one which updated the cc_sb value), there are no concurrent
518 * updates to take care of: other writers have not updated cc_sb, so
519 * they cannot set the noref flag, and concurrent readers cannot modify
520 * the pointer because the noref flag is not set yet.
521 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
522 * to the subbuffer before this set noref operation.
523 * subbuffer_set_noref() uses a volatile store to deal with concurrent
524 * readers of the noref flag.
526 chan
= shmp(handle
, bufb
->chan
);
529 CHAN_WARN_ON(chan
, subbuffer_id_is_noref(config
, wsb
->id
));
531 * Memory barrier that ensures counter stores are ordered before set
535 subbuffer_id_set_noref_offset(config
, &wsb
->id
, offset
);
539 * update_read_sb_index - Read-side subbuffer index update.
542 int update_read_sb_index(const struct lttng_ust_lib_ring_buffer_config
*config
,
543 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
544 struct channel_backend
*chanb
,
545 unsigned long consumed_idx
,
546 unsigned long consumed_count
,
547 struct lttng_ust_shm_handle
*handle
)
549 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
550 unsigned long old_id
, new_id
;
552 wsb
= shmp_index(handle
, bufb
->buf_wsb
, consumed_idx
);
553 if (caa_unlikely(!wsb
))
556 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
557 struct channel
*chan
;
560 * Exchange the target writer subbuffer with our own unused
561 * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the
562 * old_wpage, because the value read will be confirmed by the
563 * following cmpxchg().
566 if (caa_unlikely(!subbuffer_id_is_noref(config
, old_id
)))
569 * Make sure the offset count we are expecting matches the one
570 * indicated by the writer.
572 if (caa_unlikely(!subbuffer_id_compare_offset(config
, old_id
,
575 chan
= shmp(handle
, bufb
->chan
);
576 if (caa_unlikely(!chan
))
578 CHAN_WARN_ON(chan
, !subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
579 subbuffer_id_set_noref_offset(config
, &bufb
->buf_rsb
.id
,
581 new_id
= uatomic_cmpxchg(&wsb
->id
, old_id
, bufb
->buf_rsb
.id
);
582 if (caa_unlikely(old_id
!= new_id
))
584 bufb
->buf_rsb
.id
= new_id
;
586 /* No page exchange, use the writer page directly */
587 bufb
->buf_rsb
.id
= wsb
->id
;
592 #ifndef inline_memcpy
593 #define inline_memcpy(dest, src, n) memcpy(dest, src, n)
596 static inline __attribute__((always_inline
))
597 void lttng_inline_memcpy(void *dest
, const void *src
,
602 *(uint8_t *) dest
= *(const uint8_t *) src
;
605 *(uint16_t *) dest
= *(const uint16_t *) src
;
608 *(uint32_t *) dest
= *(const uint32_t *) src
;
611 *(uint64_t *) dest
= *(const uint64_t *) src
;
614 inline_memcpy(dest
, src
, len
);
619 * Use the architecture-specific memcpy implementation for constant-sized
620 * inputs, but rely on an inline memcpy for length statically unknown.
621 * The function call to memcpy is just way too expensive for a fast path.
623 #define lib_ring_buffer_do_copy(config, dest, src, len) \
625 size_t __len = (len); \
626 if (__builtin_constant_p(len)) \
627 memcpy(dest, src, __len); \
629 lttng_inline_memcpy(dest, src, __len); \
633 * write len bytes to dest with c
636 void lib_ring_buffer_do_memset(char *dest
, int c
, unsigned long len
)
640 for (i
= 0; i
< len
; i
++)
644 /* arch-agnostic implementation */
646 static inline int lttng_ust_fls(unsigned int x
)
652 if (!(x
& 0xFFFF0000U
)) {
656 if (!(x
& 0xFF000000U
)) {
660 if (!(x
& 0xF0000000U
)) {
664 if (!(x
& 0xC0000000U
)) {
668 if (!(x
& 0x80000000U
)) {
669 /* No need to bit shift on last operation */
675 static inline int get_count_order(unsigned int count
)
679 order
= lttng_ust_fls(count
) - 1;
680 if (count
& (count
- 1))
685 #endif /* _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H */