2 * ring_buffer_frontend.c
4 * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
7 * recorder (overwrite) modes. See thesis:
9 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
10 * dissertation, Ecole Polytechnique de Montreal.
11 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
13 * - Algorithm presentation in Chapter 5:
14 * "Lockless Multi-Core High-Throughput Buffering".
15 * - Algorithm formal verification in Section 8.6:
16 * "Formal verification of LTTng"
19 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
21 * Inspired from LTT and RelayFS:
22 * Karim Yaghmour <karim@opersys.com>
23 * Tom Zanussi <zanussi@us.ibm.com>
24 * Bob Wisniewski <bob@watson.ibm.com>
26 * Bob Wisniewski <bob@watson.ibm.com>
28 * Buffer reader semantic :
31 * while buffer is not finalized and empty
33 * - if return value != 0, continue
34 * - splice one subbuffer worth of data to a pipe
35 * - splice the data from pipe to disk/network
38 * Dual LGPL v2.1/GPL v2 license.
41 #include <sys/types.h>
45 #include <urcu/compiler.h>
49 #include <ust/ringbuffer-config.h>
55 #define max(a, b) ((a) > (b) ? (a) : (b))
59 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
60 * close(2) to close the fd returned by shm_open.
61 * shm_unlink releases the shared memory object name.
62 * ftruncate(2) sets the size of the memory object.
63 * mmap/munmap maps the shared memory obj to a virtual address in the
64 * calling proceess (should be done both in libust and consumer).
65 * See shm_overview(7) for details.
66 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
69 * Since we don't need to access the object using its name, we can
70 * immediately shm_unlink(3) it, and only keep the handle with its file
75 * Internal structure representing offsets to use at a sub-buffer switch.
77 struct switch_offsets
{
78 unsigned long begin
, end
, old
;
79 size_t pre_header_padding
, size
;
80 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
84 __thread
unsigned int lib_ring_buffer_nesting
;
87 void lib_ring_buffer_print_errors(struct channel
*chan
,
88 struct lib_ring_buffer
*buf
, int cpu
,
89 struct shm_handle
*handle
);
92 * Must be called under cpu hotplug protection.
94 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
,
95 struct shm_handle
*handle
)
97 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
99 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
, handle
);
100 /* buf->commit_hot will be freed by shm teardown */
101 /* buf->commit_cold will be freed by shm teardown */
103 lib_ring_buffer_backend_free(&buf
->backend
);
107 * lib_ring_buffer_reset - Reset ring buffer to initial values.
110 * Effectively empty the ring buffer. Should be called when the buffer is not
111 * used for writing. The ring buffer can be opened for reading, but the reader
112 * should not be using the iterator concurrently with reset. The previous
113 * current iterator record is reset.
115 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
,
116 struct shm_handle
*handle
)
118 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
119 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
123 * Reset iterator first. It will put the subbuffer if it currently holds
126 v_set(config
, &buf
->offset
, 0);
127 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
128 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
129 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
130 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
132 uatomic_set(&buf
->consumed
, 0);
133 uatomic_set(&buf
->record_disabled
, 0);
134 v_set(config
, &buf
->last_tsc
, 0);
135 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
136 /* Don't reset number of active readers */
137 v_set(config
, &buf
->records_lost_full
, 0);
138 v_set(config
, &buf
->records_lost_wrap
, 0);
139 v_set(config
, &buf
->records_lost_big
, 0);
140 v_set(config
, &buf
->records_count
, 0);
141 v_set(config
, &buf
->records_overrun
, 0);
146 * channel_reset - Reset channel to initial values.
149 * Effectively empty the channel. Should be called when the channel is not used
150 * for writing. The channel can be opened for reading, but the reader should not
151 * be using the iterator concurrently with reset. The previous current iterator
154 void channel_reset(struct channel
*chan
)
157 * Reset iterators first. Will put the subbuffer if held for reading.
159 uatomic_set(&chan
->record_disabled
, 0);
160 /* Don't reset commit_count_mask, still valid */
161 channel_backend_reset(&chan
->backend
);
162 /* Don't reset switch/read timer interval */
163 /* Don't reset notifiers and notifier enable bits */
164 /* Don't reset reader reference count */
168 * Must be called under cpu hotplug protection.
170 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
171 struct channel_backend
*chanb
, int cpu
,
172 struct shm_handle
*handle
,
173 struct shm_object
*shmobj
)
175 const struct lib_ring_buffer_config
*config
= chanb
->config
;
176 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
177 void *priv
= chanb
->priv
;
178 unsigned int num_subbuf
;
179 size_t subbuf_header_size
;
183 /* Test for cpu hotplug */
184 if (buf
->backend
.allocated
)
187 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
188 cpu
, handle
, shmobj
);
192 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
193 set_shmp(buf
->commit_hot
,
195 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
196 if (!shmp(handle
, buf
->commit_hot
)) {
201 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
202 set_shmp(buf
->commit_cold
,
204 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
205 if (!shmp(handle
, buf
->commit_cold
)) {
210 num_subbuf
= chan
->backend
.num_subbuf
;
211 //init_waitqueue_head(&buf->read_wait);
214 * Write the subbuffer header for first subbuffer so we know the total
215 * duration of data gathering.
217 subbuf_header_size
= config
->cb
.subbuffer_header_size();
218 v_set(config
, &buf
->offset
, subbuf_header_size
);
219 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
220 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
221 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
222 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
224 if (config
->cb
.buffer_create
) {
225 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
229 buf
->backend
.allocated
= 1;
234 /* commit_cold will be freed by shm teardown */
236 /* commit_hot will be freed by shm teardown */
238 lib_ring_buffer_backend_free(&buf
->backend
);
243 static void switch_buffer_timer(unsigned long data
)
245 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
246 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
247 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
250 * Only flush buffers periodically if readers are active.
252 if (uatomic_read(&buf
->active_readers
))
253 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
256 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
257 // mod_timer_pinned(&buf->switch_timer,
258 // jiffies + chan->switch_timer_interval);
260 // mod_timer(&buf->switch_timer,
261 // jiffies + chan->switch_timer_interval);
265 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
,
266 struct shm_handle
*handle
)
268 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
269 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
271 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
274 //init_timer(&buf->switch_timer);
275 //buf->switch_timer.function = switch_buffer_timer;
276 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
277 //buf->switch_timer.data = (unsigned long)buf;
278 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
279 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
281 // add_timer(&buf->switch_timer);
282 buf
->switch_timer_enabled
= 1;
285 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
,
286 struct shm_handle
*handle
)
288 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
290 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
294 //del_timer_sync(&buf->switch_timer);
295 buf
->switch_timer_enabled
= 0;
300 * Polling timer to check the channels for data.
302 static void read_buffer_timer(unsigned long data
)
304 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
305 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
306 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
308 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
310 if (uatomic_read(&buf
->active_readers
)
311 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
313 //wake_up_interruptible(&buf->read_wait);
314 //wake_up_interruptible(&chan->read_wait);
318 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
319 // mod_timer_pinned(&buf->read_timer,
320 // jiffies + chan->read_timer_interval);
322 // mod_timer(&buf->read_timer,
323 // jiffies + chan->read_timer_interval);
327 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
,
328 struct shm_handle
*handle
)
330 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
331 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
333 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
334 || !chan
->read_timer_interval
335 || buf
->read_timer_enabled
)
339 //init_timer(&buf->read_timer);
340 //buf->read_timer.function = read_buffer_timer;
341 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
342 //buf->read_timer.data = (unsigned long)buf;
344 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
345 // add_timer_on(&buf->read_timer, buf->backend.cpu);
347 // add_timer(&buf->read_timer);
348 buf
->read_timer_enabled
= 1;
351 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
,
352 struct shm_handle
*handle
)
354 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
355 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
357 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
358 || !chan
->read_timer_interval
359 || !buf
->read_timer_enabled
)
363 //del_timer_sync(&buf->read_timer);
365 * do one more check to catch data that has been written in the last
368 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
370 //wake_up_interruptible(&buf->read_wait);
371 //wake_up_interruptible(&chan->read_wait);
373 buf
->read_timer_enabled
= 0;
376 static void channel_unregister_notifiers(struct channel
*chan
,
377 struct shm_handle
*handle
)
379 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
382 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
383 for_each_possible_cpu(cpu
) {
384 struct lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
386 lib_ring_buffer_stop_switch_timer(buf
, handle
);
387 lib_ring_buffer_stop_read_timer(buf
, handle
);
390 struct lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
392 lib_ring_buffer_stop_switch_timer(buf
, handle
);
393 lib_ring_buffer_stop_read_timer(buf
, handle
);
395 //channel_backend_unregister_notifiers(&chan->backend);
398 static void channel_free(struct channel
*chan
, struct shm_handle
*handle
)
402 channel_backend_free(&chan
->backend
, handle
);
403 /* chan is freed by shm teardown */
404 shm_object_table_destroy(handle
->table
);
409 * channel_create - Create channel.
410 * @config: ring buffer instance configuration
411 * @name: name of the channel
412 * @priv: ring buffer client private data
413 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
414 * address mapping. It is used only by RING_BUFFER_STATIC
415 * configuration. It can be set to NULL for other backends.
416 * @subbuf_size: subbuffer size
417 * @num_subbuf: number of subbuffers
418 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
419 * padding to let readers get those sub-buffers.
420 * Used for live streaming.
421 * @read_timer_interval: Time interval (in us) to wake up pending readers.
424 * Returns NULL on failure.
426 struct shm_handle
*channel_create(const struct lib_ring_buffer_config
*config
,
427 const char *name
, void *priv
, void *buf_addr
,
429 size_t num_subbuf
, unsigned int switch_timer_interval
,
430 unsigned int read_timer_interval
)
434 struct channel
*chan
;
435 struct shm_handle
*handle
;
436 struct shm_object
*shmobj
;
438 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
439 read_timer_interval
))
442 handle
= zmalloc(sizeof(struct shm_handle
));
446 /* Allocate table for channel + per-cpu buffers */
447 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
449 goto error_table_alloc
;
451 /* Calculate the shm allocation layout */
452 shmsize
= sizeof(struct channel
);
453 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
454 shmsize
+= sizeof(struct lib_ring_buffer_shmp
) * num_possible_cpus();
456 shmsize
+= sizeof(struct lib_ring_buffer_shmp
);
458 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
461 set_shmp(handle
->chan
, zalloc_shm(shmobj
, sizeof(struct channel
)));
462 chan
= shmp(handle
, handle
->chan
);
466 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
467 subbuf_size
, num_subbuf
, handle
);
469 goto error_backend_init
;
471 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
473 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
474 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
476 //init_waitqueue_head(&chan->read_wait);
477 //init_waitqueue_head(&chan->hp_wait);
479 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
481 * In case of non-hotplug cpu, if the ring-buffer is allocated
482 * in early initcall, it will not be notified of secondary cpus.
483 * In that off case, we need to allocate for all possible cpus.
485 for_each_possible_cpu(cpu
) {
486 struct lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
487 lib_ring_buffer_start_switch_timer(buf
, handle
);
488 lib_ring_buffer_start_read_timer(buf
, handle
);
491 struct lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
493 lib_ring_buffer_start_switch_timer(buf
, handle
);
494 lib_ring_buffer_start_read_timer(buf
, handle
);
501 shm_object_table_destroy(handle
->table
);
508 void channel_release(struct channel
*chan
, struct shm_handle
*handle
)
510 channel_free(chan
, handle
);
514 * channel_destroy - Finalize, wait for q.s. and destroy channel.
515 * @chan: channel to destroy
518 * Call "destroy" callback, finalize channels, decrement the channel
519 * reference count. Note that when readers have completed data
520 * consumption of finalized channels, get_subbuf() will return -ENODATA.
521 * They should release their handle at that point. Returns the private
524 void *channel_destroy(struct channel
*chan
, struct shm_handle
*handle
)
526 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
530 channel_unregister_notifiers(chan
, handle
);
532 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
533 for_each_channel_cpu(cpu
, chan
) {
534 struct lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
536 if (config
->cb
.buffer_finalize
)
537 config
->cb
.buffer_finalize(buf
,
540 if (buf
->backend
.allocated
)
541 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
,
544 * Perform flush before writing to finalized.
547 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
548 //wake_up_interruptible(&buf->read_wait);
551 struct lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
553 if (config
->cb
.buffer_finalize
)
554 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1, handle
);
555 if (buf
->backend
.allocated
)
556 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
,
559 * Perform flush before writing to finalized.
562 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
563 //wake_up_interruptible(&buf->read_wait);
565 CMM_ACCESS_ONCE(chan
->finalized
) = 1;
566 //wake_up_interruptible(&chan->hp_wait);
567 //wake_up_interruptible(&chan->read_wait);
569 * sessiond/consumer are keeping a reference on the shm file
570 * descriptor directly. No need to refcount.
572 priv
= chan
->backend
.priv
;
573 channel_release(chan
, handle
);
577 struct lib_ring_buffer
*channel_get_ring_buffer(
578 const struct lib_ring_buffer_config
*config
,
579 struct channel
*chan
, int cpu
,
580 struct shm_handle
*handle
)
582 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
583 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
585 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
588 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
,
589 struct shm_handle
*handle
)
591 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
593 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
599 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
,
600 struct shm_handle
*handle
)
602 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
604 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
606 uatomic_dec(&buf
->active_readers
);
610 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
612 * @consumed: consumed count indicating the position where to read
613 * @produced: produced count, indicates position when to stop reading
615 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
616 * data to read at consumed position, or 0 if the get operation succeeds.
619 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
620 unsigned long *consumed
, unsigned long *produced
,
621 struct shm_handle
*handle
)
623 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
624 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
625 unsigned long consumed_cur
, write_offset
;
628 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
630 * Read finalized before counters.
633 consumed_cur
= uatomic_read(&buf
->consumed
);
635 * No need to issue a memory barrier between consumed count read and
636 * write offset read, because consumed count can only change
637 * concurrently in overwrite mode, and we keep a sequence counter
638 * identifier derived from the write offset to check we are getting
639 * the same sub-buffer we are expecting (the sub-buffers are atomically
640 * "tagged" upon writes, tags are checked upon read).
642 write_offset
= v_read(config
, &buf
->offset
);
645 * Check that we are not about to read the same subbuffer in
646 * which the writer head is.
648 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
652 *consumed
= consumed_cur
;
653 *produced
= subbuf_trunc(write_offset
, chan
);
659 * The memory barriers __wait_event()/wake_up_interruptible() take care
660 * of "raw_spin_is_locked" memory ordering.
669 * lib_ring_buffer_put_snapshot - move consumed counter forward
671 * @consumed_new: new consumed count value
673 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
674 unsigned long consumed_new
,
675 struct shm_handle
*handle
)
677 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
678 struct channel
*chan
= shmp(handle
, bufb
->chan
);
679 unsigned long consumed
;
681 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
684 * Only push the consumed value forward.
685 * If the consumed cmpxchg fails, this is because we have been pushed by
686 * the writer in flight recorder mode.
688 consumed
= uatomic_read(&buf
->consumed
);
689 while ((long) consumed
- (long) consumed_new
< 0)
690 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
695 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
697 * @consumed: consumed count indicating the position where to read
699 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
700 * data to read at consumed position, or 0 if the get operation succeeds.
702 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
703 unsigned long consumed
,
704 struct shm_handle
*handle
)
706 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
707 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
708 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
713 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
715 * Read finalized before counters.
718 consumed_cur
= uatomic_read(&buf
->consumed
);
719 consumed_idx
= subbuf_index(consumed
, chan
);
720 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
722 * Make sure we read the commit count before reading the buffer
723 * data and the write offset. Correct consumed offset ordering
724 * wrt commit count is insured by the use of cmpxchg to update
725 * the consumed offset.
728 * Local rmb to match the remote wmb to read the commit count
729 * before the buffer data and the write offset.
733 write_offset
= v_read(config
, &buf
->offset
);
736 * Check that the buffer we are getting is after or at consumed_cur
739 if ((long) subbuf_trunc(consumed
, chan
)
740 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
744 * Check that the subbuffer we are trying to consume has been
745 * already fully committed.
747 if (((commit_count
- chan
->backend
.subbuf_size
)
748 & chan
->commit_count_mask
)
749 - (buf_trunc(consumed_cur
, chan
)
750 >> chan
->backend
.num_subbuf_order
)
755 * Check that we are not about to read the same subbuffer in
756 * which the writer head is.
758 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
763 * Failure to get the subbuffer causes a busy-loop retry without going
764 * to a wait queue. These are caused by short-lived race windows where
765 * the writer is getting access to a subbuffer we were trying to get
766 * access to. Also checks that the "consumed" buffer count we are
767 * looking for matches the one contained in the subbuffer id.
769 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
770 consumed_idx
, buf_trunc_val(consumed
, chan
),
774 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
776 buf
->get_subbuf_consumed
= consumed
;
783 * The memory barriers __wait_event()/wake_up_interruptible() take care
784 * of "raw_spin_is_locked" memory ordering.
793 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
796 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
,
797 struct shm_handle
*handle
)
799 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
800 struct channel
*chan
= shmp(handle
, bufb
->chan
);
801 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
802 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
804 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
806 if (!buf
->get_subbuf
) {
808 * Reader puts a subbuffer it did not get.
810 CHAN_WARN_ON(chan
, 1);
813 consumed
= buf
->get_subbuf_consumed
;
817 * Clear the records_unread counter. (overruns counter)
818 * Can still be non-zero if a file reader simply grabbed the data
819 * without using iterators.
820 * Can be below zero if an iterator is used on a snapshot more than
823 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
824 v_add(config
, v_read(config
,
825 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
826 &bufb
->records_read
);
827 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
828 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
829 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
830 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
833 * Exchange the reader subbuffer with the one we put in its place in the
834 * writer subbuffer table. Expect the original consumed count. If
835 * update_read_sb_index fails, this is because the writer updated the
836 * subbuffer concurrently. We should therefore keep the subbuffer we
837 * currently have: it has become invalid to try reading this sub-buffer
838 * consumed count value anyway.
840 consumed_idx
= subbuf_index(consumed
, chan
);
841 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
842 consumed_idx
, buf_trunc_val(consumed
, chan
),
845 * update_read_sb_index return value ignored. Don't exchange sub-buffer
846 * if the writer concurrently updated it.
851 * cons_offset is an iterator on all subbuffer offsets between the reader
852 * position and the writer position. (inclusive)
855 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
856 struct channel
*chan
,
857 unsigned long cons_offset
,
859 struct shm_handle
*handle
)
861 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
862 unsigned long cons_idx
, commit_count
, commit_count_sb
;
864 cons_idx
= subbuf_index(cons_offset
, chan
);
865 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
866 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
868 if (subbuf_offset(commit_count
, chan
) != 0)
869 ERRMSG("ring buffer %s, cpu %d: "
870 "commit count in subbuffer %lu,\n"
871 "expecting multiples of %lu bytes\n"
872 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
873 chan
->backend
.name
, cpu
, cons_idx
,
874 chan
->backend
.subbuf_size
,
875 commit_count
, commit_count_sb
);
877 ERRMSG("ring buffer: %s, cpu %d: %lu bytes committed\n",
878 chan
->backend
.name
, cpu
, commit_count
);
882 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
883 struct channel
*chan
,
885 struct shm_handle
*handle
)
887 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
888 unsigned long write_offset
, cons_offset
;
891 * Can be called in the error path of allocation when
892 * trans_channel_data is not yet set.
897 * No need to order commit_count, write_offset and cons_offset reads
898 * because we execute at teardown when no more writer nor reader
899 * references are left.
901 write_offset
= v_read(config
, &buf
->offset
);
902 cons_offset
= uatomic_read(&buf
->consumed
);
903 if (write_offset
!= cons_offset
)
904 ERRMSG("ring buffer %s, cpu %d: "
905 "non-consumed data\n"
906 " [ %lu bytes written, %lu bytes read ]\n",
907 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
909 for (cons_offset
= uatomic_read(&buf
->consumed
);
910 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
913 cons_offset
= subbuf_align(cons_offset
, chan
))
914 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
919 void lib_ring_buffer_print_errors(struct channel
*chan
,
920 struct lib_ring_buffer
*buf
, int cpu
,
921 struct shm_handle
*handle
)
923 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
924 void *priv
= chan
->backend
.priv
;
926 ERRMSG("ring buffer %s, cpu %d: %lu records written, "
927 "%lu records overrun\n",
928 chan
->backend
.name
, cpu
,
929 v_read(config
, &buf
->records_count
),
930 v_read(config
, &buf
->records_overrun
));
932 if (v_read(config
, &buf
->records_lost_full
)
933 || v_read(config
, &buf
->records_lost_wrap
)
934 || v_read(config
, &buf
->records_lost_big
))
935 ERRMSG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
936 " [ %lu buffer full, %lu nest buffer wrap-around, "
937 "%lu event too big ]\n",
938 chan
->backend
.name
, cpu
,
939 v_read(config
, &buf
->records_lost_full
),
940 v_read(config
, &buf
->records_lost_wrap
),
941 v_read(config
, &buf
->records_lost_big
));
943 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
947 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
949 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
952 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
953 struct channel
*chan
,
954 struct switch_offsets
*offsets
,
956 struct shm_handle
*handle
)
958 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
959 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
960 unsigned long commit_count
;
962 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
965 * Order all writes to buffer before the commit count update that will
966 * determine that the subbuffer is full.
969 v_add(config
, config
->cb
.subbuffer_header_size(),
970 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
971 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
972 /* Check if the written buffer has to be delivered */
973 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
974 commit_count
, oldidx
, handle
);
975 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
976 offsets
->old
, commit_count
,
977 config
->cb
.subbuffer_header_size(),
982 * lib_ring_buffer_switch_old_end: switch old subbuffer
984 * Note : offset_old should never be 0 here. It is ok, because we never perform
985 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
986 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
990 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
991 struct channel
*chan
,
992 struct switch_offsets
*offsets
,
994 struct shm_handle
*handle
)
996 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
997 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
998 unsigned long commit_count
, padding_size
, data_size
;
1000 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1001 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1002 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1006 * Order all writes to buffer before the commit count update that will
1007 * determine that the subbuffer is full.
1010 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1011 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1012 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1013 commit_count
, oldidx
, handle
);
1014 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1015 offsets
->old
, commit_count
,
1016 padding_size
, handle
);
1020 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1022 * This code can be executed unordered : writers may already have written to the
1023 * sub-buffer before this code gets executed, caution. The commit makes sure
1024 * that this code is executed before the deliver of this sub-buffer.
1027 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1028 struct channel
*chan
,
1029 struct switch_offsets
*offsets
,
1031 struct shm_handle
*handle
)
1033 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1034 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1035 unsigned long commit_count
;
1037 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1040 * Order all writes to buffer before the commit count update that will
1041 * determine that the subbuffer is full.
1044 v_add(config
, config
->cb
.subbuffer_header_size(),
1045 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1046 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1047 /* Check if the written buffer has to be delivered */
1048 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1049 commit_count
, beginidx
, handle
);
1050 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1051 offsets
->begin
, commit_count
,
1052 config
->cb
.subbuffer_header_size(),
1057 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1059 * The only remaining threads could be the ones with pending commits. They will
1060 * have to do the deliver themselves.
1063 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1064 struct channel
*chan
,
1065 struct switch_offsets
*offsets
,
1067 struct shm_handle
*handle
)
1069 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1070 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1071 unsigned long commit_count
, padding_size
, data_size
;
1073 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1074 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1075 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1079 * Order all writes to buffer before the commit count update that will
1080 * determine that the subbuffer is full.
1083 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1084 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1085 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1086 commit_count
, endidx
, handle
);
1087 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1088 offsets
->end
, commit_count
,
1089 padding_size
, handle
);
1095 * !0 if execution must be aborted.
1098 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1099 struct lib_ring_buffer
*buf
,
1100 struct channel
*chan
,
1101 struct switch_offsets
*offsets
,
1104 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1107 offsets
->begin
= v_read(config
, &buf
->offset
);
1108 offsets
->old
= offsets
->begin
;
1109 offsets
->switch_old_start
= 0;
1110 off
= subbuf_offset(offsets
->begin
, chan
);
1112 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1115 * Ensure we flush the header of an empty subbuffer when doing the
1116 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1117 * total data gathering duration even if there were no records saved
1118 * after the last buffer switch.
1119 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1120 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1121 * subbuffer header as appropriate.
1122 * The next record that reserves space will be responsible for
1123 * populating the following subbuffer header. We choose not to populate
1124 * the next subbuffer header here because we want to be able to use
1125 * SWITCH_ACTIVE for periodical buffer flush, which must
1126 * guarantee that all the buffer content (records and header
1127 * timestamps) are visible to the reader. This is required for
1128 * quiescence guarantees for the fusion merge.
1130 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1131 if (unlikely(off
== 0)) {
1133 * The client does not save any header information.
1134 * Don't switch empty subbuffer on finalize, because it
1135 * is invalid to deliver a completely empty subbuffer.
1137 if (!config
->cb
.subbuffer_header_size())
1140 * Need to write the subbuffer start header on finalize.
1142 offsets
->switch_old_start
= 1;
1144 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1146 return -1; /* we do not have to switch : buffer is empty */
1147 /* Note: old points to the next subbuf at offset 0 */
1148 offsets
->end
= offsets
->begin
;
1153 * Force a sub-buffer switch. This operation is completely reentrant : can be
1154 * called while tracing is active with absolutely no lock held.
1156 * Note, however, that as a v_cmpxchg is used for some atomic
1157 * operations, this function must be called from the CPU which owns the buffer
1158 * for a ACTIVE flush.
1160 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
,
1161 struct shm_handle
*handle
)
1163 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1164 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1165 struct switch_offsets offsets
;
1166 unsigned long oldidx
;
1172 * Perform retryable operations.
1175 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1177 return; /* Switch not needed */
1178 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1182 * Atomically update last_tsc. This update races against concurrent
1183 * atomic updates, but the race will always cause supplementary full TSC
1184 * records, never the opposite (missing a full TSC record when it would
1187 save_last_tsc(config
, buf
, tsc
);
1190 * Push the reader if necessary
1192 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1194 oldidx
= subbuf_index(offsets
.old
, chan
);
1195 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1198 * May need to populate header start on SWITCH_FLUSH.
1200 if (offsets
.switch_old_start
) {
1201 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1202 offsets
.old
+= config
->cb
.subbuffer_header_size();
1206 * Switch old subbuffer.
1208 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1214 * -ENOSPC if event size is too large for packet.
1215 * -ENOBUFS if there is currently not enough space in buffer for the event.
1216 * -EIO if data cannot be written into the buffer for any other reason.
1219 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1220 struct channel
*chan
,
1221 struct switch_offsets
*offsets
,
1222 struct lib_ring_buffer_ctx
*ctx
)
1224 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1225 struct shm_handle
*handle
= ctx
->handle
;
1226 unsigned long reserve_commit_diff
;
1228 offsets
->begin
= v_read(config
, &buf
->offset
);
1229 offsets
->old
= offsets
->begin
;
1230 offsets
->switch_new_start
= 0;
1231 offsets
->switch_new_end
= 0;
1232 offsets
->switch_old_end
= 0;
1233 offsets
->pre_header_padding
= 0;
1235 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1236 if ((int64_t) ctx
->tsc
== -EIO
)
1239 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1240 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1242 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1243 offsets
->switch_new_start
= 1; /* For offsets->begin */
1245 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1247 &offsets
->pre_header_padding
,
1250 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1253 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1254 offsets
->size
> chan
->backend
.subbuf_size
)) {
1255 offsets
->switch_old_end
= 1; /* For offsets->old */
1256 offsets
->switch_new_start
= 1; /* For offsets->begin */
1259 if (unlikely(offsets
->switch_new_start
)) {
1260 unsigned long sb_index
;
1263 * We are typically not filling the previous buffer completely.
1265 if (likely(offsets
->switch_old_end
))
1266 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1267 offsets
->begin
= offsets
->begin
1268 + config
->cb
.subbuffer_header_size();
1269 /* Test new buffer integrity */
1270 sb_index
= subbuf_index(offsets
->begin
, chan
);
1271 reserve_commit_diff
=
1272 (buf_trunc(offsets
->begin
, chan
)
1273 >> chan
->backend
.num_subbuf_order
)
1274 - ((unsigned long) v_read(config
,
1275 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1276 & chan
->commit_count_mask
);
1277 if (likely(reserve_commit_diff
== 0)) {
1278 /* Next subbuffer not being written to. */
1279 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1280 subbuf_trunc(offsets
->begin
, chan
)
1281 - subbuf_trunc((unsigned long)
1282 uatomic_read(&buf
->consumed
), chan
)
1283 >= chan
->backend
.buf_size
)) {
1285 * We do not overwrite non consumed buffers
1286 * and we are full : record is lost.
1288 v_inc(config
, &buf
->records_lost_full
);
1292 * Next subbuffer not being written to, and we
1293 * are either in overwrite mode or the buffer is
1294 * not full. It's safe to write in this new
1300 * Next subbuffer reserve offset does not match the
1301 * commit offset. Drop record in producer-consumer and
1302 * overwrite mode. Caused by either a writer OOPS or too
1303 * many nested writes over a reserve/commit pair.
1305 v_inc(config
, &buf
->records_lost_wrap
);
1309 config
->cb
.record_header_size(config
, chan
,
1311 &offsets
->pre_header_padding
,
1314 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1317 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1318 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1320 * Record too big for subbuffers, report error, don't
1321 * complete the sub-buffer switch.
1323 v_inc(config
, &buf
->records_lost_big
);
1327 * We just made a successful buffer switch and the
1328 * record fits in the new subbuffer. Let's write.
1333 * Record fits in the current buffer and we are not on a switch
1334 * boundary. It's safe to write.
1337 offsets
->end
= offsets
->begin
+ offsets
->size
;
1339 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1341 * The offset_end will fall at the very beginning of the next
1344 offsets
->switch_new_end
= 1; /* For offsets->begin */
1350 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1351 * @ctx: ring buffer context.
1353 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1354 * -EIO for other errors, else returns 0.
1355 * It will take care of sub-buffer switching.
1357 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1359 struct channel
*chan
= ctx
->chan
;
1360 struct shm_handle
*handle
= ctx
->handle
;
1361 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1362 struct lib_ring_buffer
*buf
;
1363 struct switch_offsets offsets
;
1366 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1367 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1369 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1375 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1379 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1384 * Atomically update last_tsc. This update races against concurrent
1385 * atomic updates, but the race will always cause supplementary full TSC
1386 * records, never the opposite (missing a full TSC record when it would
1389 save_last_tsc(config
, buf
, ctx
->tsc
);
1392 * Push the reader if necessary
1394 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1397 * Clear noref flag for this subbuffer.
1399 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1400 subbuf_index(offsets
.end
- 1, chan
),
1404 * Switch old subbuffer if needed.
1406 if (unlikely(offsets
.switch_old_end
)) {
1407 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1408 subbuf_index(offsets
.old
- 1, chan
),
1410 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1414 * Populate new subbuffer.
1416 if (unlikely(offsets
.switch_new_start
))
1417 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1419 if (unlikely(offsets
.switch_new_end
))
1420 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1422 ctx
->slot_size
= offsets
.size
;
1423 ctx
->pre_offset
= offsets
.begin
;
1424 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;