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>
55 #define max(a, b) ((a) > (b) ? (a) : (b))
59 * Internal structure representing offsets to use at a sub-buffer switch.
61 struct switch_offsets
{
62 unsigned long begin
, end
, old
;
63 size_t pre_header_padding
, size
;
64 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
68 __thread
unsigned int lib_ring_buffer_nesting
;
71 void lib_ring_buffer_print_errors(struct channel
*chan
,
72 struct lib_ring_buffer
*buf
, int cpu
);
75 * Must be called under cpu hotplug protection.
77 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
79 struct channel
*chan
= shmp(buf
->backend
.chan
);
81 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
82 /* buf->commit_hot will be freed by shm teardown */
83 /* buf->commit_cold will be freed by shm teardown */
85 lib_ring_buffer_backend_free(&buf
->backend
);
89 * lib_ring_buffer_reset - Reset ring buffer to initial values.
92 * Effectively empty the ring buffer. Should be called when the buffer is not
93 * used for writing. The ring buffer can be opened for reading, but the reader
94 * should not be using the iterator concurrently with reset. The previous
95 * current iterator record is reset.
97 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
99 struct channel
*chan
= shmp(buf
->backend
.chan
);
100 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
104 * Reset iterator first. It will put the subbuffer if it currently holds
107 v_set(config
, &buf
->offset
, 0);
108 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
109 v_set(config
, &shmp(buf
->commit_hot
)[i
].cc
, 0);
110 v_set(config
, &shmp(buf
->commit_hot
)[i
].seq
, 0);
111 v_set(config
, &shmp(buf
->commit_cold
)[i
].cc_sb
, 0);
113 uatomic_set(&buf
->consumed
, 0);
114 uatomic_set(&buf
->record_disabled
, 0);
115 v_set(config
, &buf
->last_tsc
, 0);
116 lib_ring_buffer_backend_reset(&buf
->backend
);
117 /* Don't reset number of active readers */
118 v_set(config
, &buf
->records_lost_full
, 0);
119 v_set(config
, &buf
->records_lost_wrap
, 0);
120 v_set(config
, &buf
->records_lost_big
, 0);
121 v_set(config
, &buf
->records_count
, 0);
122 v_set(config
, &buf
->records_overrun
, 0);
127 * channel_reset - Reset channel to initial values.
130 * Effectively empty the channel. Should be called when the channel is not used
131 * for writing. The channel can be opened for reading, but the reader should not
132 * be using the iterator concurrently with reset. The previous current iterator
135 void channel_reset(struct channel
*chan
)
138 * Reset iterators first. Will put the subbuffer if held for reading.
140 uatomic_set(&chan
->record_disabled
, 0);
141 /* Don't reset commit_count_mask, still valid */
142 channel_backend_reset(&chan
->backend
);
143 /* Don't reset switch/read timer interval */
144 /* Don't reset notifiers and notifier enable bits */
145 /* Don't reset reader reference count */
149 * Must be called under cpu hotplug protection.
151 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
152 struct channel_backend
*chanb
, int cpu
,
153 struct shm_header
*shm_header
)
155 const struct lib_ring_buffer_config
*config
= chanb
->config
;
156 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
157 void *priv
= chanb
->priv
;
158 unsigned int num_subbuf
;
159 size_t subbuf_header_size
;
163 /* Test for cpu hotplug */
164 if (buf
->backend
.allocated
)
167 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
172 align_shm(shm_header
,
173 max(__alignof__(struct commit_counters_hot
),
174 __alignof__(struct commit_counters_cold
)));
175 set_shmp(&buf
->commit_hot
,
176 zalloc_shm(shm_header
,
177 sizeof(*buf
->commit_hot
) * chan
->backend
.num_subbuf
));
178 if (!shmp(buf
->commit_hot
)) {
183 align_shm(shm_header
, __alignof__(struct commit_counters_cold
));
184 set_shmp(&buf
->commit_cold
,
185 zalloc_shm(shm_header
,
186 sizeof(*buf
->commit_cold
) * chan
->backend
.num_subbuf
));
187 if (!shmp(buf
->commit_cold
)) {
192 num_subbuf
= chan
->backend
.num_subbuf
;
193 //init_waitqueue_head(&buf->read_wait);
196 * Write the subbuffer header for first subbuffer so we know the total
197 * duration of data gathering.
199 subbuf_header_size
= config
->cb
.subbuffer_header_size();
200 v_set(config
, &buf
->offset
, subbuf_header_size
);
201 subbuffer_id_clear_noref(config
, &shmp(buf
->backend
.buf_wsb
)[0].id
);
202 tsc
= config
->cb
.ring_buffer_clock_read(shmp(buf
->backend
.chan
));
203 config
->cb
.buffer_begin(buf
, tsc
, 0);
204 v_add(config
, subbuf_header_size
, &shmp(buf
->commit_hot
)[0].cc
);
206 if (config
->cb
.buffer_create
) {
207 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
211 buf
->backend
.allocated
= 1;
216 /* commit_cold will be freed by shm teardown */
218 /* commit_hot will be freed by shm teardown */
220 lib_ring_buffer_backend_free(&buf
->backend
);
224 static void switch_buffer_timer(unsigned long data
)
226 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
227 struct channel
*chan
= shmp(buf
->backend
.chan
);
228 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
231 * Only flush buffers periodically if readers are active.
233 if (uatomic_read(&buf
->active_readers
))
234 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
237 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
238 // mod_timer_pinned(&buf->switch_timer,
239 // jiffies + chan->switch_timer_interval);
241 // mod_timer(&buf->switch_timer,
242 // jiffies + chan->switch_timer_interval);
245 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
247 struct channel
*chan
= shmp(buf
->backend
.chan
);
248 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
250 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
253 //init_timer(&buf->switch_timer);
254 //buf->switch_timer.function = switch_buffer_timer;
255 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
256 //buf->switch_timer.data = (unsigned long)buf;
257 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
258 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
260 // add_timer(&buf->switch_timer);
261 buf
->switch_timer_enabled
= 1;
264 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
266 struct channel
*chan
= shmp(buf
->backend
.chan
);
268 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
272 //del_timer_sync(&buf->switch_timer);
273 buf
->switch_timer_enabled
= 0;
277 * Polling timer to check the channels for data.
279 static void read_buffer_timer(unsigned long data
)
281 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
282 struct channel
*chan
= shmp(buf
->backend
.chan
);
283 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
285 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
287 if (uatomic_read(&buf
->active_readers
)
288 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
290 //wake_up_interruptible(&buf->read_wait);
291 //wake_up_interruptible(&chan->read_wait);
295 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
296 // mod_timer_pinned(&buf->read_timer,
297 // jiffies + chan->read_timer_interval);
299 // mod_timer(&buf->read_timer,
300 // jiffies + chan->read_timer_interval);
303 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
305 struct channel
*chan
= shmp(buf
->backend
.chan
);
306 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
308 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
309 || !chan
->read_timer_interval
310 || buf
->read_timer_enabled
)
314 //init_timer(&buf->read_timer);
315 //buf->read_timer.function = read_buffer_timer;
316 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
317 //buf->read_timer.data = (unsigned long)buf;
319 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
320 // add_timer_on(&buf->read_timer, buf->backend.cpu);
322 // add_timer(&buf->read_timer);
323 buf
->read_timer_enabled
= 1;
326 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
328 struct channel
*chan
= shmp(buf
->backend
.chan
);
329 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
331 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
332 || !chan
->read_timer_interval
333 || !buf
->read_timer_enabled
)
337 //del_timer_sync(&buf->read_timer);
339 * do one more check to catch data that has been written in the last
342 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
344 //wake_up_interruptible(&buf->read_wait);
345 //wake_up_interruptible(&chan->read_wait);
347 buf
->read_timer_enabled
= 0;
350 static void channel_unregister_notifiers(struct channel
*chan
)
352 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
355 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
356 for_each_possible_cpu(cpu
) {
357 struct lib_ring_buffer
*buf
= &shmp(chan
->backend
.buf
)[cpu
];
359 lib_ring_buffer_stop_switch_timer(buf
);
360 lib_ring_buffer_stop_read_timer(buf
);
363 struct lib_ring_buffer
*buf
= shmp(chan
->backend
.buf
);
365 lib_ring_buffer_stop_switch_timer(buf
);
366 lib_ring_buffer_stop_read_timer(buf
);
368 channel_backend_unregister_notifiers(&chan
->backend
);
371 static void channel_free(struct shm_handle
*handle
)
373 struct shm_header
*header
= handle
->header
;
374 struct channel
*chan
= shmp(header
->chan
);
377 channel_backend_free(&chan
->backend
);
378 /* chan is freed by shm teardown */
379 ret
= munmap(header
, header
->shm_size
);
384 ret
= close(handle
->shmfd
);
392 * channel_create - Create channel.
393 * @config: ring buffer instance configuration
394 * @name: name of the channel
395 * @priv: ring buffer client private data
396 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
397 * address mapping. It is used only by RING_BUFFER_STATIC
398 * configuration. It can be set to NULL for other backends.
399 * @subbuf_size: subbuffer size
400 * @num_subbuf: number of subbuffers
401 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
402 * padding to let readers get those sub-buffers.
403 * Used for live streaming.
404 * @read_timer_interval: Time interval (in us) to wake up pending readers.
405 * @shmfd: shared memory file descriptor (output, needs to be closed by
409 * Returns NULL on failure.
411 struct shm_handle
*channel_create(const struct lib_ring_buffer_config
*config
,
412 const char *name
, void *priv
, void *buf_addr
,
414 size_t num_subbuf
, unsigned int switch_timer_interval
,
415 unsigned int read_timer_interval
)
418 struct channel
*chan
;
419 size_t shmsize
, bufshmsize
, bufshmalign
;
420 struct shm_header
*shm_header
;
421 unsigned long num_subbuf_alloc
;
422 struct shm_handle
*handle
;
424 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
425 read_timer_interval
))
428 handle
= zmalloc(sizeof(struct shm_handle
));
432 /* Calculate the shm allocation layout */
433 shmsize
= sizeof(struct shm_header
);
434 shmsize
+= offset_align(shmsize
, __alignof__(struct channel
));
435 shmsize
+= sizeof(struct channel
);
437 /* Per-cpu buffer size: control (prior to backend) */
438 shmsize
+= offset_align(shmsize
, __alignof__(struct lib_ring_buffer
));
439 bufshmsize
= sizeof(struct lib_ring_buffer
);
440 shmsize
+= bufshmsize
* num_possible_cpus();
442 /* Per-cpu buffer size: backend */
443 shmsize
+= offset_align(shmsize
, PAGE_SIZE
);
444 /* num_subbuf + 1 is the worse case */
445 num_subbuf_alloc
= num_subbuf
+ 1;
446 bufshmsize
= sizeof(struct lib_ring_buffer_backend_pages
*) * num_subbuf_alloc
;
447 bufshmsize
+= offset_align(bufshmsize
, PAGE_SIZE
);
448 bufshmsize
+= subbuf_size
* num_subbuf_alloc
;
449 bufshmsize
+= offset_align(bufshmsize
, __alignof__(struct lib_ring_buffer_backend_pages
));
450 bufshmsize
+= sizeof(struct lib_ring_buffer_backend_pages
) * num_subbuf_alloc
;
451 bufshmsize
+= offset_align(bufshmsize
, __alignof__(struct lib_ring_buffer_backend_subbuffer
));
452 bufshmsize
+= sizeof(struct lib_ring_buffer_backend_subbuffer
) * num_subbuf
;
453 bufshmsize
+= offset_align(bufshmsize
, PAGE_SIZE
);
454 shmsize
+= bufshmsize
* num_possible_cpus();
456 /* Per-cpu buffer size: control (after backend) */
457 shmsize
+= offset_align(shmsize
,
458 max(__alignof__(struct commit_counters_hot
),
459 __alignof__(struct commit_counters_cold
)));
460 bufshmsize
= sizeof(struct commit_counters_hot
) * num_subbuf
;
461 bufshmsize
+= offset_align(bufshmsize
, __alignof__(struct commit_counters_cold
));
462 bufshmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
463 shmsize
+= bufshmsize
* num_possible_cpus();
466 * Allocate shm, and immediately unlink its shm oject, keeping
467 * only the file descriptor as a reference to the object. If it
468 * already exists (caused by short race window during which the
469 * global object exists in a concurrent shm_open), simply retry.
472 shmfd
= shm_open("/ust-shm-tmp",
473 O_CREAT
| O_EXCL
| O_RDWR
, 0700);
474 } while (shmfd
< 0 && errno
== EEXIST
);
479 ret
= shm_unlink("/ust-shm-tmp");
481 PERROR("shm_unlink");
484 ret
= ftruncate(shmfd
, shmsize
);
487 goto error_ftruncate
;
490 shm_header
= mmap(NULL
, shmsize
, PROT_READ
| PROT_WRITE
,
491 MAP_SHARED
, shmfd
, 0);
492 if (shm_header
== MAP_FAILED
) {
497 shm_header
->magic
= SHM_MAGIC
;
498 shm_header
->major
= SHM_MAJOR
;
499 shm_header
->major
= SHM_MINOR
;
500 shm_header
->bits_per_long
= CAA_BITS_PER_LONG
;
501 shm_header
->shm_size
= shmsize
;
502 shm_header
->shm_allocated
= sizeof(struct shm_header
);
504 align_shm(shm_header
, __alignof__(struct channel
));
505 chan
= zalloc_shm(shm_header
, sizeof(struct channel
));
508 set_shmp(shm_header
->chan
, chan
);
510 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
511 subbuf_size
, num_subbuf
, shm_header
);
515 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
517 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
518 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
520 //init_waitqueue_head(&chan->read_wait);
521 //init_waitqueue_head(&chan->hp_wait);
523 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
525 * In case of non-hotplug cpu, if the ring-buffer is allocated
526 * in early initcall, it will not be notified of secondary cpus.
527 * In that off case, we need to allocate for all possible cpus.
529 for_each_possible_cpu(cpu
) {
530 struct lib_ring_buffer
*buf
= &shmp(chan
->backend
.buf
)[cpu
];
531 lib_ring_buffer_start_switch_timer(buf
);
532 lib_ring_buffer_start_read_timer(buf
);
535 struct lib_ring_buffer
*buf
= shmp(chan
->backend
.buf
);
537 lib_ring_buffer_start_switch_timer(buf
);
538 lib_ring_buffer_start_read_timer(buf
);
541 handle
->header
= shm_header
;
542 handle
->shmfd
= shmfd
;
546 ret
= munmap(shm_header
, shmsize
);
565 void channel_release(struct shm_handle
*handle
)
567 channel_free(handle
);
571 * channel_destroy - Finalize, wait for q.s. and destroy channel.
572 * @chan: channel to destroy
575 * Call "destroy" callback, finalize channels, decrement the channel
576 * reference count. Note that when readers have completed data
577 * consumption of finalized channels, get_subbuf() will return -ENODATA.
578 * They should release their handle at that point. Returns the private
581 void *channel_destroy(struct shm_handle
*handle
)
583 struct shm_header
*header
= handle
->header
;
584 struct channel
*chan
= shmp(header
->chan
);
585 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
589 channel_unregister_notifiers(chan
);
591 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
592 for_each_channel_cpu(cpu
, chan
) {
593 struct lib_ring_buffer
*buf
= &shmp(chan
->backend
.buf
)[cpu
];
595 if (config
->cb
.buffer_finalize
)
596 config
->cb
.buffer_finalize(buf
,
599 if (buf
->backend
.allocated
)
600 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
602 * Perform flush before writing to finalized.
605 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
606 //wake_up_interruptible(&buf->read_wait);
609 struct lib_ring_buffer
*buf
= shmp(chan
->backend
.buf
);
611 if (config
->cb
.buffer_finalize
)
612 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
613 if (buf
->backend
.allocated
)
614 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
616 * Perform flush before writing to finalized.
619 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
620 //wake_up_interruptible(&buf->read_wait);
622 CMM_ACCESS_ONCE(chan
->finalized
) = 1;
623 //wake_up_interruptible(&chan->hp_wait);
624 //wake_up_interruptible(&chan->read_wait);
626 * sessiond/consumer are keeping a reference on the shm file
627 * descriptor directly. No need to refcount.
629 channel_release(handle
);
630 priv
= chan
->backend
.priv
;
634 struct lib_ring_buffer
*channel_get_ring_buffer(
635 const struct lib_ring_buffer_config
*config
,
636 struct channel
*chan
, int cpu
)
638 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
639 return shmp(chan
->backend
.buf
);
641 return &shmp(chan
->backend
.buf
)[cpu
];
644 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
646 struct channel
*chan
= shmp(buf
->backend
.chan
);
648 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
654 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
656 struct channel
*chan
= shmp(buf
->backend
.chan
);
658 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
660 uatomic_dec(&buf
->active_readers
);
664 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
666 * @consumed: consumed count indicating the position where to read
667 * @produced: produced count, indicates position when to stop reading
669 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
670 * data to read at consumed position, or 0 if the get operation succeeds.
673 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
674 unsigned long *consumed
, unsigned long *produced
)
676 struct channel
*chan
= shmp(buf
->backend
.chan
);
677 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
678 unsigned long consumed_cur
, write_offset
;
681 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
683 * Read finalized before counters.
686 consumed_cur
= uatomic_read(&buf
->consumed
);
688 * No need to issue a memory barrier between consumed count read and
689 * write offset read, because consumed count can only change
690 * concurrently in overwrite mode, and we keep a sequence counter
691 * identifier derived from the write offset to check we are getting
692 * the same sub-buffer we are expecting (the sub-buffers are atomically
693 * "tagged" upon writes, tags are checked upon read).
695 write_offset
= v_read(config
, &buf
->offset
);
698 * Check that we are not about to read the same subbuffer in
699 * which the writer head is.
701 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
705 *consumed
= consumed_cur
;
706 *produced
= subbuf_trunc(write_offset
, chan
);
712 * The memory barriers __wait_event()/wake_up_interruptible() take care
713 * of "raw_spin_is_locked" memory ordering.
722 * lib_ring_buffer_put_snapshot - move consumed counter forward
724 * @consumed_new: new consumed count value
726 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
727 unsigned long consumed_new
)
729 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
730 struct channel
*chan
= shmp(bufb
->chan
);
731 unsigned long consumed
;
733 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
736 * Only push the consumed value forward.
737 * If the consumed cmpxchg fails, this is because we have been pushed by
738 * the writer in flight recorder mode.
740 consumed
= uatomic_read(&buf
->consumed
);
741 while ((long) consumed
- (long) consumed_new
< 0)
742 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
747 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
749 * @consumed: consumed count indicating the position where to read
751 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
752 * data to read at consumed position, or 0 if the get operation succeeds.
754 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
755 unsigned long consumed
)
757 struct channel
*chan
= shmp(buf
->backend
.chan
);
758 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
759 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
764 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
766 * Read finalized before counters.
769 consumed_cur
= uatomic_read(&buf
->consumed
);
770 consumed_idx
= subbuf_index(consumed
, chan
);
771 commit_count
= v_read(config
, &shmp(buf
->commit_cold
)[consumed_idx
].cc_sb
);
773 * Make sure we read the commit count before reading the buffer
774 * data and the write offset. Correct consumed offset ordering
775 * wrt commit count is insured by the use of cmpxchg to update
776 * the consumed offset.
779 * Local rmb to match the remote wmb to read the commit count
780 * before the buffer data and the write offset.
784 write_offset
= v_read(config
, &buf
->offset
);
787 * Check that the buffer we are getting is after or at consumed_cur
790 if ((long) subbuf_trunc(consumed
, chan
)
791 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
795 * Check that the subbuffer we are trying to consume has been
796 * already fully committed.
798 if (((commit_count
- chan
->backend
.subbuf_size
)
799 & chan
->commit_count_mask
)
800 - (buf_trunc(consumed_cur
, chan
)
801 >> chan
->backend
.num_subbuf_order
)
806 * Check that we are not about to read the same subbuffer in
807 * which the writer head is.
809 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
814 * Failure to get the subbuffer causes a busy-loop retry without going
815 * to a wait queue. These are caused by short-lived race windows where
816 * the writer is getting access to a subbuffer we were trying to get
817 * access to. Also checks that the "consumed" buffer count we are
818 * looking for matches the one contained in the subbuffer id.
820 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
821 consumed_idx
, buf_trunc_val(consumed
, chan
));
824 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
826 buf
->get_subbuf_consumed
= consumed
;
833 * The memory barriers __wait_event()/wake_up_interruptible() take care
834 * of "raw_spin_is_locked" memory ordering.
843 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
846 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
848 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
849 struct channel
*chan
= shmp(bufb
->chan
);
850 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
851 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
853 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
855 if (!buf
->get_subbuf
) {
857 * Reader puts a subbuffer it did not get.
859 CHAN_WARN_ON(chan
, 1);
862 consumed
= buf
->get_subbuf_consumed
;
866 * Clear the records_unread counter. (overruns counter)
867 * Can still be non-zero if a file reader simply grabbed the data
868 * without using iterators.
869 * Can be below zero if an iterator is used on a snapshot more than
872 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
873 v_add(config
, v_read(config
,
874 &shmp(bufb
->array
)[read_sb_bindex
]->records_unread
),
875 &bufb
->records_read
);
876 v_set(config
, &shmp(bufb
->array
)[read_sb_bindex
]->records_unread
, 0);
877 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
878 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
879 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
882 * Exchange the reader subbuffer with the one we put in its place in the
883 * writer subbuffer table. Expect the original consumed count. If
884 * update_read_sb_index fails, this is because the writer updated the
885 * subbuffer concurrently. We should therefore keep the subbuffer we
886 * currently have: it has become invalid to try reading this sub-buffer
887 * consumed count value anyway.
889 consumed_idx
= subbuf_index(consumed
, chan
);
890 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
891 consumed_idx
, buf_trunc_val(consumed
, chan
));
893 * update_read_sb_index return value ignored. Don't exchange sub-buffer
894 * if the writer concurrently updated it.
899 * cons_offset is an iterator on all subbuffer offsets between the reader
900 * position and the writer position. (inclusive)
903 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
904 struct channel
*chan
,
905 unsigned long cons_offset
,
908 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
909 unsigned long cons_idx
, commit_count
, commit_count_sb
;
911 cons_idx
= subbuf_index(cons_offset
, chan
);
912 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[cons_idx
].cc
);
913 commit_count_sb
= v_read(config
, &shmp(buf
->commit_cold
)[cons_idx
].cc_sb
);
915 if (subbuf_offset(commit_count
, chan
) != 0)
916 ERRMSG("ring buffer %s, cpu %d: "
917 "commit count in subbuffer %lu,\n"
918 "expecting multiples of %lu bytes\n"
919 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
920 chan
->backend
.name
, cpu
, cons_idx
,
921 chan
->backend
.subbuf_size
,
922 commit_count
, commit_count_sb
);
924 ERRMSG("ring buffer: %s, cpu %d: %lu bytes committed\n",
925 chan
->backend
.name
, cpu
, commit_count
);
929 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
930 struct channel
*chan
,
933 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
934 unsigned long write_offset
, cons_offset
;
937 * Can be called in the error path of allocation when
938 * trans_channel_data is not yet set.
943 * No need to order commit_count, write_offset and cons_offset reads
944 * because we execute at teardown when no more writer nor reader
945 * references are left.
947 write_offset
= v_read(config
, &buf
->offset
);
948 cons_offset
= uatomic_read(&buf
->consumed
);
949 if (write_offset
!= cons_offset
)
950 ERRMSG("ring buffer %s, cpu %d: "
951 "non-consumed data\n"
952 " [ %lu bytes written, %lu bytes read ]\n",
953 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
955 for (cons_offset
= uatomic_read(&buf
->consumed
);
956 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
959 cons_offset
= subbuf_align(cons_offset
, chan
))
960 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
965 void lib_ring_buffer_print_errors(struct channel
*chan
,
966 struct lib_ring_buffer
*buf
, int cpu
)
968 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
969 void *priv
= chan
->backend
.priv
;
971 ERRMSG("ring buffer %s, cpu %d: %lu records written, "
972 "%lu records overrun\n",
973 chan
->backend
.name
, cpu
,
974 v_read(config
, &buf
->records_count
),
975 v_read(config
, &buf
->records_overrun
));
977 if (v_read(config
, &buf
->records_lost_full
)
978 || v_read(config
, &buf
->records_lost_wrap
)
979 || v_read(config
, &buf
->records_lost_big
))
980 ERRMSG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
981 " [ %lu buffer full, %lu nest buffer wrap-around, "
982 "%lu event too big ]\n",
983 chan
->backend
.name
, cpu
,
984 v_read(config
, &buf
->records_lost_full
),
985 v_read(config
, &buf
->records_lost_wrap
),
986 v_read(config
, &buf
->records_lost_big
));
988 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
992 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
994 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
997 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
998 struct channel
*chan
,
999 struct switch_offsets
*offsets
,
1002 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1003 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1004 unsigned long commit_count
;
1006 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1009 * Order all writes to buffer before the commit count update that will
1010 * determine that the subbuffer is full.
1013 v_add(config
, config
->cb
.subbuffer_header_size(),
1014 &shmp(buf
->commit_hot
)[oldidx
].cc
);
1015 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[oldidx
].cc
);
1016 /* Check if the written buffer has to be delivered */
1017 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1018 commit_count
, oldidx
);
1019 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1020 offsets
->old
, commit_count
,
1021 config
->cb
.subbuffer_header_size());
1025 * lib_ring_buffer_switch_old_end: switch old subbuffer
1027 * Note : offset_old should never be 0 here. It is ok, because we never perform
1028 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1029 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1033 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1034 struct channel
*chan
,
1035 struct switch_offsets
*offsets
,
1038 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1039 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1040 unsigned long commit_count
, padding_size
, data_size
;
1042 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1043 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1044 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1047 * Order all writes to buffer before the commit count update that will
1048 * determine that the subbuffer is full.
1051 v_add(config
, padding_size
, &shmp(buf
->commit_hot
)[oldidx
].cc
);
1052 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[oldidx
].cc
);
1053 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1054 commit_count
, oldidx
);
1055 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1056 offsets
->old
, commit_count
,
1061 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1063 * This code can be executed unordered : writers may already have written to the
1064 * sub-buffer before this code gets executed, caution. The commit makes sure
1065 * that this code is executed before the deliver of this sub-buffer.
1068 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1069 struct channel
*chan
,
1070 struct switch_offsets
*offsets
,
1073 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1074 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1075 unsigned long commit_count
;
1077 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1080 * Order all writes to buffer before the commit count update that will
1081 * determine that the subbuffer is full.
1084 v_add(config
, config
->cb
.subbuffer_header_size(),
1085 &shmp(buf
->commit_hot
)[beginidx
].cc
);
1086 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[beginidx
].cc
);
1087 /* Check if the written buffer has to be delivered */
1088 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1089 commit_count
, beginidx
);
1090 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1091 offsets
->begin
, commit_count
,
1092 config
->cb
.subbuffer_header_size());
1096 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1098 * The only remaining threads could be the ones with pending commits. They will
1099 * have to do the deliver themselves.
1102 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1103 struct channel
*chan
,
1104 struct switch_offsets
*offsets
,
1107 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1108 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1109 unsigned long commit_count
, padding_size
, data_size
;
1111 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1112 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1113 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1116 * Order all writes to buffer before the commit count update that will
1117 * determine that the subbuffer is full.
1120 v_add(config
, padding_size
, &shmp(buf
->commit_hot
)[endidx
].cc
);
1121 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[endidx
].cc
);
1122 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1123 commit_count
, endidx
);
1124 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1125 offsets
->end
, commit_count
,
1132 * !0 if execution must be aborted.
1135 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1136 struct lib_ring_buffer
*buf
,
1137 struct channel
*chan
,
1138 struct switch_offsets
*offsets
,
1141 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1144 offsets
->begin
= v_read(config
, &buf
->offset
);
1145 offsets
->old
= offsets
->begin
;
1146 offsets
->switch_old_start
= 0;
1147 off
= subbuf_offset(offsets
->begin
, chan
);
1149 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1152 * Ensure we flush the header of an empty subbuffer when doing the
1153 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1154 * total data gathering duration even if there were no records saved
1155 * after the last buffer switch.
1156 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1157 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1158 * subbuffer header as appropriate.
1159 * The next record that reserves space will be responsible for
1160 * populating the following subbuffer header. We choose not to populate
1161 * the next subbuffer header here because we want to be able to use
1162 * SWITCH_ACTIVE for periodical buffer flush, which must
1163 * guarantee that all the buffer content (records and header
1164 * timestamps) are visible to the reader. This is required for
1165 * quiescence guarantees for the fusion merge.
1167 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1168 if (unlikely(off
== 0)) {
1170 * The client does not save any header information.
1171 * Don't switch empty subbuffer on finalize, because it
1172 * is invalid to deliver a completely empty subbuffer.
1174 if (!config
->cb
.subbuffer_header_size())
1177 * Need to write the subbuffer start header on finalize.
1179 offsets
->switch_old_start
= 1;
1181 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1183 return -1; /* we do not have to switch : buffer is empty */
1184 /* Note: old points to the next subbuf at offset 0 */
1185 offsets
->end
= offsets
->begin
;
1190 * Force a sub-buffer switch. This operation is completely reentrant : can be
1191 * called while tracing is active with absolutely no lock held.
1193 * Note, however, that as a v_cmpxchg is used for some atomic
1194 * operations, this function must be called from the CPU which owns the buffer
1195 * for a ACTIVE flush.
1197 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1199 struct channel
*chan
= shmp(buf
->backend
.chan
);
1200 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1201 struct switch_offsets offsets
;
1202 unsigned long oldidx
;
1208 * Perform retryable operations.
1211 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1213 return; /* Switch not needed */
1214 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1218 * Atomically update last_tsc. This update races against concurrent
1219 * atomic updates, but the race will always cause supplementary full TSC
1220 * records, never the opposite (missing a full TSC record when it would
1223 save_last_tsc(config
, buf
, tsc
);
1226 * Push the reader if necessary
1228 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1230 oldidx
= subbuf_index(offsets
.old
, chan
);
1231 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1234 * May need to populate header start on SWITCH_FLUSH.
1236 if (offsets
.switch_old_start
) {
1237 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1238 offsets
.old
+= config
->cb
.subbuffer_header_size();
1242 * Switch old subbuffer.
1244 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1250 * -ENOSPC if event size is too large for packet.
1251 * -ENOBUFS if there is currently not enough space in buffer for the event.
1252 * -EIO if data cannot be written into the buffer for any other reason.
1255 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1256 struct channel
*chan
,
1257 struct switch_offsets
*offsets
,
1258 struct lib_ring_buffer_ctx
*ctx
)
1260 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1261 unsigned long reserve_commit_diff
;
1263 offsets
->begin
= v_read(config
, &buf
->offset
);
1264 offsets
->old
= offsets
->begin
;
1265 offsets
->switch_new_start
= 0;
1266 offsets
->switch_new_end
= 0;
1267 offsets
->switch_old_end
= 0;
1268 offsets
->pre_header_padding
= 0;
1270 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1271 if ((int64_t) ctx
->tsc
== -EIO
)
1274 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1275 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1277 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1278 offsets
->switch_new_start
= 1; /* For offsets->begin */
1280 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1282 &offsets
->pre_header_padding
,
1285 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1288 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1289 offsets
->size
> chan
->backend
.subbuf_size
)) {
1290 offsets
->switch_old_end
= 1; /* For offsets->old */
1291 offsets
->switch_new_start
= 1; /* For offsets->begin */
1294 if (unlikely(offsets
->switch_new_start
)) {
1295 unsigned long sb_index
;
1298 * We are typically not filling the previous buffer completely.
1300 if (likely(offsets
->switch_old_end
))
1301 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1302 offsets
->begin
= offsets
->begin
1303 + config
->cb
.subbuffer_header_size();
1304 /* Test new buffer integrity */
1305 sb_index
= subbuf_index(offsets
->begin
, chan
);
1306 reserve_commit_diff
=
1307 (buf_trunc(offsets
->begin
, chan
)
1308 >> chan
->backend
.num_subbuf_order
)
1309 - ((unsigned long) v_read(config
,
1310 &shmp(buf
->commit_cold
)[sb_index
].cc_sb
)
1311 & chan
->commit_count_mask
);
1312 if (likely(reserve_commit_diff
== 0)) {
1313 /* Next subbuffer not being written to. */
1314 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1315 subbuf_trunc(offsets
->begin
, chan
)
1316 - subbuf_trunc((unsigned long)
1317 uatomic_read(&buf
->consumed
), chan
)
1318 >= chan
->backend
.buf_size
)) {
1320 * We do not overwrite non consumed buffers
1321 * and we are full : record is lost.
1323 v_inc(config
, &buf
->records_lost_full
);
1327 * Next subbuffer not being written to, and we
1328 * are either in overwrite mode or the buffer is
1329 * not full. It's safe to write in this new
1335 * Next subbuffer reserve offset does not match the
1336 * commit offset. Drop record in producer-consumer and
1337 * overwrite mode. Caused by either a writer OOPS or too
1338 * many nested writes over a reserve/commit pair.
1340 v_inc(config
, &buf
->records_lost_wrap
);
1344 config
->cb
.record_header_size(config
, chan
,
1346 &offsets
->pre_header_padding
,
1349 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1352 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1353 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1355 * Record too big for subbuffers, report error, don't
1356 * complete the sub-buffer switch.
1358 v_inc(config
, &buf
->records_lost_big
);
1362 * We just made a successful buffer switch and the
1363 * record fits in the new subbuffer. Let's write.
1368 * Record fits in the current buffer and we are not on a switch
1369 * boundary. It's safe to write.
1372 offsets
->end
= offsets
->begin
+ offsets
->size
;
1374 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1376 * The offset_end will fall at the very beginning of the next
1379 offsets
->switch_new_end
= 1; /* For offsets->begin */
1385 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1386 * @ctx: ring buffer context.
1388 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1389 * -EIO for other errors, else returns 0.
1390 * It will take care of sub-buffer switching.
1392 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1394 struct channel
*chan
= ctx
->chan
;
1395 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1396 struct lib_ring_buffer
*buf
;
1397 struct switch_offsets offsets
;
1400 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1401 buf
= &shmp(chan
->backend
.buf
)[ctx
->cpu
];
1403 buf
= shmp(chan
->backend
.buf
);
1409 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1413 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1418 * Atomically update last_tsc. This update races against concurrent
1419 * atomic updates, but the race will always cause supplementary full TSC
1420 * records, never the opposite (missing a full TSC record when it would
1423 save_last_tsc(config
, buf
, ctx
->tsc
);
1426 * Push the reader if necessary
1428 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1431 * Clear noref flag for this subbuffer.
1433 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1434 subbuf_index(offsets
.end
- 1, chan
));
1437 * Switch old subbuffer if needed.
1439 if (unlikely(offsets
.switch_old_end
)) {
1440 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1441 subbuf_index(offsets
.old
- 1, chan
));
1442 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1446 * Populate new subbuffer.
1448 if (unlikely(offsets
.switch_new_start
))
1449 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1451 if (unlikely(offsets
.switch_new_end
))
1452 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1454 ctx
->slot_size
= offsets
.size
;
1455 ctx
->pre_offset
= offsets
.begin
;
1456 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;