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 * 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
);
91 * Must be called under cpu hotplug protection.
93 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
95 struct channel
*chan
= shmp(buf
->backend
.chan
);
97 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
98 /* buf->commit_hot will be freed by shm teardown */
99 /* buf->commit_cold will be freed by shm teardown */
101 lib_ring_buffer_backend_free(&buf
->backend
);
105 * lib_ring_buffer_reset - Reset ring buffer to initial values.
108 * Effectively empty the ring buffer. Should be called when the buffer is not
109 * used for writing. The ring buffer can be opened for reading, but the reader
110 * should not be using the iterator concurrently with reset. The previous
111 * current iterator record is reset.
113 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
115 struct channel
*chan
= shmp(buf
->backend
.chan
);
116 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
120 * Reset iterator first. It will put the subbuffer if it currently holds
123 v_set(config
, &buf
->offset
, 0);
124 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
125 v_set(config
, &shmp(buf
->commit_hot
)[i
].cc
, 0);
126 v_set(config
, &shmp(buf
->commit_hot
)[i
].seq
, 0);
127 v_set(config
, &shmp(buf
->commit_cold
)[i
].cc_sb
, 0);
129 uatomic_set(&buf
->consumed
, 0);
130 uatomic_set(&buf
->record_disabled
, 0);
131 v_set(config
, &buf
->last_tsc
, 0);
132 lib_ring_buffer_backend_reset(&buf
->backend
);
133 /* Don't reset number of active readers */
134 v_set(config
, &buf
->records_lost_full
, 0);
135 v_set(config
, &buf
->records_lost_wrap
, 0);
136 v_set(config
, &buf
->records_lost_big
, 0);
137 v_set(config
, &buf
->records_count
, 0);
138 v_set(config
, &buf
->records_overrun
, 0);
143 * channel_reset - Reset channel to initial values.
146 * Effectively empty the channel. Should be called when the channel is not used
147 * for writing. The channel can be opened for reading, but the reader should not
148 * be using the iterator concurrently with reset. The previous current iterator
151 void channel_reset(struct channel
*chan
)
154 * Reset iterators first. Will put the subbuffer if held for reading.
156 uatomic_set(&chan
->record_disabled
, 0);
157 /* Don't reset commit_count_mask, still valid */
158 channel_backend_reset(&chan
->backend
);
159 /* Don't reset switch/read timer interval */
160 /* Don't reset notifiers and notifier enable bits */
161 /* Don't reset reader reference count */
165 * Must be called under cpu hotplug protection.
167 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
168 struct channel_backend
*chanb
, int cpu
,
169 struct shm_header
*shm_header
)
171 const struct lib_ring_buffer_config
*config
= chanb
->config
;
172 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
173 void *priv
= chanb
->priv
;
174 unsigned int num_subbuf
;
175 size_t subbuf_header_size
;
179 /* Test for cpu hotplug */
180 if (buf
->backend
.allocated
)
183 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
188 align_shm(shm_header
,
189 max(__alignof__(struct commit_counters_hot
),
190 __alignof__(struct commit_counters_cold
)));
191 set_shmp(&buf
->commit_hot
,
192 zalloc_shm(shm_header
,
193 sizeof(*buf
->commit_hot
) * chan
->backend
.num_subbuf
));
194 if (!shmp(buf
->commit_hot
)) {
199 align_shm(shm_header
, __alignof__(struct commit_counters_cold
));
200 set_shmp(&buf
->commit_cold
,
201 zalloc_shm(shm_header
,
202 sizeof(*buf
->commit_cold
) * chan
->backend
.num_subbuf
));
203 if (!shmp(buf
->commit_cold
)) {
208 num_subbuf
= chan
->backend
.num_subbuf
;
209 //init_waitqueue_head(&buf->read_wait);
212 * Write the subbuffer header for first subbuffer so we know the total
213 * duration of data gathering.
215 subbuf_header_size
= config
->cb
.subbuffer_header_size();
216 v_set(config
, &buf
->offset
, subbuf_header_size
);
217 subbuffer_id_clear_noref(config
, &shmp(buf
->backend
.buf_wsb
)[0].id
);
218 tsc
= config
->cb
.ring_buffer_clock_read(shmp(buf
->backend
.chan
));
219 config
->cb
.buffer_begin(buf
, tsc
, 0);
220 v_add(config
, subbuf_header_size
, &shmp(buf
->commit_hot
)[0].cc
);
222 if (config
->cb
.buffer_create
) {
223 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
227 buf
->backend
.allocated
= 1;
232 /* commit_cold will be freed by shm teardown */
234 /* commit_hot will be freed by shm teardown */
236 lib_ring_buffer_backend_free(&buf
->backend
);
240 static void switch_buffer_timer(unsigned long data
)
242 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
243 struct channel
*chan
= shmp(buf
->backend
.chan
);
244 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
247 * Only flush buffers periodically if readers are active.
249 if (uatomic_read(&buf
->active_readers
))
250 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
253 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
254 // mod_timer_pinned(&buf->switch_timer,
255 // jiffies + chan->switch_timer_interval);
257 // mod_timer(&buf->switch_timer,
258 // jiffies + chan->switch_timer_interval);
261 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
263 struct channel
*chan
= shmp(buf
->backend
.chan
);
264 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
266 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
269 //init_timer(&buf->switch_timer);
270 //buf->switch_timer.function = switch_buffer_timer;
271 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
272 //buf->switch_timer.data = (unsigned long)buf;
273 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
274 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
276 // add_timer(&buf->switch_timer);
277 buf
->switch_timer_enabled
= 1;
280 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
282 struct channel
*chan
= shmp(buf
->backend
.chan
);
284 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
288 //del_timer_sync(&buf->switch_timer);
289 buf
->switch_timer_enabled
= 0;
293 * Polling timer to check the channels for data.
295 static void read_buffer_timer(unsigned long data
)
297 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
298 struct channel
*chan
= shmp(buf
->backend
.chan
);
299 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
301 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
303 if (uatomic_read(&buf
->active_readers
)
304 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
306 //wake_up_interruptible(&buf->read_wait);
307 //wake_up_interruptible(&chan->read_wait);
311 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
312 // mod_timer_pinned(&buf->read_timer,
313 // jiffies + chan->read_timer_interval);
315 // mod_timer(&buf->read_timer,
316 // jiffies + chan->read_timer_interval);
319 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
321 struct channel
*chan
= shmp(buf
->backend
.chan
);
322 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
324 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
325 || !chan
->read_timer_interval
326 || buf
->read_timer_enabled
)
330 //init_timer(&buf->read_timer);
331 //buf->read_timer.function = read_buffer_timer;
332 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
333 //buf->read_timer.data = (unsigned long)buf;
335 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
336 // add_timer_on(&buf->read_timer, buf->backend.cpu);
338 // add_timer(&buf->read_timer);
339 buf
->read_timer_enabled
= 1;
342 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
344 struct channel
*chan
= shmp(buf
->backend
.chan
);
345 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
347 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
348 || !chan
->read_timer_interval
349 || !buf
->read_timer_enabled
)
353 //del_timer_sync(&buf->read_timer);
355 * do one more check to catch data that has been written in the last
358 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
360 //wake_up_interruptible(&buf->read_wait);
361 //wake_up_interruptible(&chan->read_wait);
363 buf
->read_timer_enabled
= 0;
366 static void channel_unregister_notifiers(struct channel
*chan
)
368 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
371 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
372 for_each_possible_cpu(cpu
) {
373 struct lib_ring_buffer
*buf
= &shmp(chan
->backend
.buf
)[cpu
];
375 lib_ring_buffer_stop_switch_timer(buf
);
376 lib_ring_buffer_stop_read_timer(buf
);
379 struct lib_ring_buffer
*buf
= shmp(chan
->backend
.buf
);
381 lib_ring_buffer_stop_switch_timer(buf
);
382 lib_ring_buffer_stop_read_timer(buf
);
384 channel_backend_unregister_notifiers(&chan
->backend
);
387 static void channel_free(struct shm_handle
*handle
)
389 struct shm_header
*header
= handle
->header
;
390 struct channel
*chan
= shmp(header
->chan
);
393 channel_backend_free(&chan
->backend
);
394 /* chan is freed by shm teardown */
395 ret
= munmap(header
, header
->shm_size
);
400 ret
= close(handle
->shmfd
);
408 * channel_create - Create channel.
409 * @config: ring buffer instance configuration
410 * @name: name of the channel
411 * @priv: ring buffer client private data
412 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
413 * address mapping. It is used only by RING_BUFFER_STATIC
414 * configuration. It can be set to NULL for other backends.
415 * @subbuf_size: subbuffer size
416 * @num_subbuf: number of subbuffers
417 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
418 * padding to let readers get those sub-buffers.
419 * Used for live streaming.
420 * @read_timer_interval: Time interval (in us) to wake up pending readers.
421 * @shmfd: shared memory file descriptor (output, needs to be closed by
425 * Returns NULL on failure.
427 struct shm_handle
*channel_create(const struct lib_ring_buffer_config
*config
,
428 const char *name
, void *priv
, void *buf_addr
,
430 size_t num_subbuf
, unsigned int switch_timer_interval
,
431 unsigned int read_timer_interval
)
434 struct channel
*chan
;
435 size_t shmsize
, bufshmsize
, bufshmalign
;
436 struct shm_header
*shm_header
;
437 unsigned long num_subbuf_alloc
;
438 struct shm_handle
*handle
;
440 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
441 read_timer_interval
))
444 handle
= zmalloc(sizeof(struct shm_handle
));
448 /* Calculate the shm allocation layout */
449 shmsize
= sizeof(struct shm_header
);
450 shmsize
+= offset_align(shmsize
, __alignof__(struct channel
));
451 shmsize
+= sizeof(struct channel
);
453 /* Per-cpu buffer size: control (prior to backend) */
454 shmsize
+= offset_align(shmsize
, __alignof__(struct lib_ring_buffer
));
455 bufshmsize
= sizeof(struct lib_ring_buffer
);
456 shmsize
+= bufshmsize
* num_possible_cpus();
458 /* Per-cpu buffer size: backend */
459 shmsize
+= offset_align(shmsize
, PAGE_SIZE
);
460 /* num_subbuf + 1 is the worse case */
461 num_subbuf_alloc
= num_subbuf
+ 1;
462 bufshmsize
= sizeof(struct lib_ring_buffer_backend_pages
*) * num_subbuf_alloc
;
463 bufshmsize
+= offset_align(bufshmsize
, PAGE_SIZE
);
464 bufshmsize
+= subbuf_size
* num_subbuf_alloc
;
465 bufshmsize
+= offset_align(bufshmsize
, __alignof__(struct lib_ring_buffer_backend_pages
));
466 bufshmsize
+= sizeof(struct lib_ring_buffer_backend_pages
) * num_subbuf_alloc
;
467 bufshmsize
+= offset_align(bufshmsize
, __alignof__(struct lib_ring_buffer_backend_subbuffer
));
468 bufshmsize
+= sizeof(struct lib_ring_buffer_backend_subbuffer
) * num_subbuf
;
469 bufshmsize
+= offset_align(bufshmsize
, PAGE_SIZE
);
470 shmsize
+= bufshmsize
* num_possible_cpus();
472 /* Per-cpu buffer size: control (after backend) */
473 shmsize
+= offset_align(shmsize
,
474 max(__alignof__(struct commit_counters_hot
),
475 __alignof__(struct commit_counters_cold
)));
476 bufshmsize
= sizeof(struct commit_counters_hot
) * num_subbuf
;
477 bufshmsize
+= offset_align(bufshmsize
, __alignof__(struct commit_counters_cold
));
478 bufshmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
479 shmsize
+= bufshmsize
* num_possible_cpus();
482 * Allocate shm, and immediately unlink its shm oject, keeping
483 * only the file descriptor as a reference to the object. If it
484 * already exists (caused by short race window during which the
485 * global object exists in a concurrent shm_open), simply retry.
488 shmfd
= shm_open("/ust-shm-tmp",
489 O_CREAT
| O_EXCL
| O_RDWR
, 0700);
490 } while (shmfd
< 0 && errno
== EEXIST
);
495 ret
= shm_unlink("/ust-shm-tmp");
497 PERROR("shm_unlink");
500 ret
= ftruncate(shmfd
, shmsize
);
503 goto error_ftruncate
;
506 shm_header
= mmap(NULL
, shmsize
, PROT_READ
| PROT_WRITE
,
507 MAP_SHARED
, shmfd
, 0);
508 if (shm_header
== MAP_FAILED
) {
513 shm_header
->magic
= SHM_MAGIC
;
514 shm_header
->major
= SHM_MAJOR
;
515 shm_header
->major
= SHM_MINOR
;
516 shm_header
->bits_per_long
= CAA_BITS_PER_LONG
;
517 shm_header
->shm_size
= shmsize
;
518 shm_header
->shm_allocated
= sizeof(struct shm_header
);
520 align_shm(shm_header
, __alignof__(struct channel
));
521 chan
= zalloc_shm(shm_header
, sizeof(struct channel
));
524 set_shmp(shm_header
->chan
, chan
);
526 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
527 subbuf_size
, num_subbuf
, shm_header
);
531 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
533 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
534 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
536 //init_waitqueue_head(&chan->read_wait);
537 //init_waitqueue_head(&chan->hp_wait);
539 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
541 * In case of non-hotplug cpu, if the ring-buffer is allocated
542 * in early initcall, it will not be notified of secondary cpus.
543 * In that off case, we need to allocate for all possible cpus.
545 for_each_possible_cpu(cpu
) {
546 struct lib_ring_buffer
*buf
= &shmp(chan
->backend
.buf
)[cpu
];
547 lib_ring_buffer_start_switch_timer(buf
);
548 lib_ring_buffer_start_read_timer(buf
);
551 struct lib_ring_buffer
*buf
= shmp(chan
->backend
.buf
);
553 lib_ring_buffer_start_switch_timer(buf
);
554 lib_ring_buffer_start_read_timer(buf
);
557 handle
->header
= shm_header
;
558 handle
->shmfd
= shmfd
;
562 ret
= munmap(shm_header
, shmsize
);
581 void channel_release(struct shm_handle
*handle
)
583 channel_free(handle
);
587 * channel_destroy - Finalize, wait for q.s. and destroy channel.
588 * @chan: channel to destroy
591 * Call "destroy" callback, finalize channels, decrement the channel
592 * reference count. Note that when readers have completed data
593 * consumption of finalized channels, get_subbuf() will return -ENODATA.
594 * They should release their handle at that point. Returns the private
597 void *channel_destroy(struct shm_handle
*handle
)
599 struct shm_header
*header
= handle
->header
;
600 struct channel
*chan
= shmp(header
->chan
);
601 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
605 channel_unregister_notifiers(chan
);
607 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
608 for_each_channel_cpu(cpu
, chan
) {
609 struct lib_ring_buffer
*buf
= &shmp(chan
->backend
.buf
)[cpu
];
611 if (config
->cb
.buffer_finalize
)
612 config
->cb
.buffer_finalize(buf
,
615 if (buf
->backend
.allocated
)
616 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
618 * Perform flush before writing to finalized.
621 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
622 //wake_up_interruptible(&buf->read_wait);
625 struct lib_ring_buffer
*buf
= shmp(chan
->backend
.buf
);
627 if (config
->cb
.buffer_finalize
)
628 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
629 if (buf
->backend
.allocated
)
630 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
632 * Perform flush before writing to finalized.
635 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
636 //wake_up_interruptible(&buf->read_wait);
638 CMM_ACCESS_ONCE(chan
->finalized
) = 1;
639 //wake_up_interruptible(&chan->hp_wait);
640 //wake_up_interruptible(&chan->read_wait);
642 * sessiond/consumer are keeping a reference on the shm file
643 * descriptor directly. No need to refcount.
645 channel_release(handle
);
646 priv
= chan
->backend
.priv
;
650 struct lib_ring_buffer
*channel_get_ring_buffer(
651 const struct lib_ring_buffer_config
*config
,
652 struct channel
*chan
, int cpu
)
654 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
655 return shmp(chan
->backend
.buf
);
657 return &shmp(chan
->backend
.buf
)[cpu
];
660 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
662 struct channel
*chan
= shmp(buf
->backend
.chan
);
664 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
670 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
672 struct channel
*chan
= shmp(buf
->backend
.chan
);
674 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
676 uatomic_dec(&buf
->active_readers
);
680 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
682 * @consumed: consumed count indicating the position where to read
683 * @produced: produced count, indicates position when to stop reading
685 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
686 * data to read at consumed position, or 0 if the get operation succeeds.
689 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
690 unsigned long *consumed
, unsigned long *produced
)
692 struct channel
*chan
= shmp(buf
->backend
.chan
);
693 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
694 unsigned long consumed_cur
, write_offset
;
697 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
699 * Read finalized before counters.
702 consumed_cur
= uatomic_read(&buf
->consumed
);
704 * No need to issue a memory barrier between consumed count read and
705 * write offset read, because consumed count can only change
706 * concurrently in overwrite mode, and we keep a sequence counter
707 * identifier derived from the write offset to check we are getting
708 * the same sub-buffer we are expecting (the sub-buffers are atomically
709 * "tagged" upon writes, tags are checked upon read).
711 write_offset
= v_read(config
, &buf
->offset
);
714 * Check that we are not about to read the same subbuffer in
715 * which the writer head is.
717 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
721 *consumed
= consumed_cur
;
722 *produced
= subbuf_trunc(write_offset
, chan
);
728 * The memory barriers __wait_event()/wake_up_interruptible() take care
729 * of "raw_spin_is_locked" memory ordering.
738 * lib_ring_buffer_put_snapshot - move consumed counter forward
740 * @consumed_new: new consumed count value
742 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
743 unsigned long consumed_new
)
745 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
746 struct channel
*chan
= shmp(bufb
->chan
);
747 unsigned long consumed
;
749 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
752 * Only push the consumed value forward.
753 * If the consumed cmpxchg fails, this is because we have been pushed by
754 * the writer in flight recorder mode.
756 consumed
= uatomic_read(&buf
->consumed
);
757 while ((long) consumed
- (long) consumed_new
< 0)
758 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
763 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
765 * @consumed: consumed count indicating the position where to read
767 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
768 * data to read at consumed position, or 0 if the get operation succeeds.
770 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
771 unsigned long consumed
)
773 struct channel
*chan
= shmp(buf
->backend
.chan
);
774 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
775 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
780 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
782 * Read finalized before counters.
785 consumed_cur
= uatomic_read(&buf
->consumed
);
786 consumed_idx
= subbuf_index(consumed
, chan
);
787 commit_count
= v_read(config
, &shmp(buf
->commit_cold
)[consumed_idx
].cc_sb
);
789 * Make sure we read the commit count before reading the buffer
790 * data and the write offset. Correct consumed offset ordering
791 * wrt commit count is insured by the use of cmpxchg to update
792 * the consumed offset.
795 * Local rmb to match the remote wmb to read the commit count
796 * before the buffer data and the write offset.
800 write_offset
= v_read(config
, &buf
->offset
);
803 * Check that the buffer we are getting is after or at consumed_cur
806 if ((long) subbuf_trunc(consumed
, chan
)
807 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
811 * Check that the subbuffer we are trying to consume has been
812 * already fully committed.
814 if (((commit_count
- chan
->backend
.subbuf_size
)
815 & chan
->commit_count_mask
)
816 - (buf_trunc(consumed_cur
, chan
)
817 >> chan
->backend
.num_subbuf_order
)
822 * Check that we are not about to read the same subbuffer in
823 * which the writer head is.
825 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
830 * Failure to get the subbuffer causes a busy-loop retry without going
831 * to a wait queue. These are caused by short-lived race windows where
832 * the writer is getting access to a subbuffer we were trying to get
833 * access to. Also checks that the "consumed" buffer count we are
834 * looking for matches the one contained in the subbuffer id.
836 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
837 consumed_idx
, buf_trunc_val(consumed
, chan
));
840 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
842 buf
->get_subbuf_consumed
= consumed
;
849 * The memory barriers __wait_event()/wake_up_interruptible() take care
850 * of "raw_spin_is_locked" memory ordering.
859 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
862 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
864 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
865 struct channel
*chan
= shmp(bufb
->chan
);
866 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
867 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
869 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
871 if (!buf
->get_subbuf
) {
873 * Reader puts a subbuffer it did not get.
875 CHAN_WARN_ON(chan
, 1);
878 consumed
= buf
->get_subbuf_consumed
;
882 * Clear the records_unread counter. (overruns counter)
883 * Can still be non-zero if a file reader simply grabbed the data
884 * without using iterators.
885 * Can be below zero if an iterator is used on a snapshot more than
888 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
889 v_add(config
, v_read(config
,
890 &shmp(bufb
->array
)[read_sb_bindex
]->records_unread
),
891 &bufb
->records_read
);
892 v_set(config
, &shmp(bufb
->array
)[read_sb_bindex
]->records_unread
, 0);
893 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
894 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
895 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
898 * Exchange the reader subbuffer with the one we put in its place in the
899 * writer subbuffer table. Expect the original consumed count. If
900 * update_read_sb_index fails, this is because the writer updated the
901 * subbuffer concurrently. We should therefore keep the subbuffer we
902 * currently have: it has become invalid to try reading this sub-buffer
903 * consumed count value anyway.
905 consumed_idx
= subbuf_index(consumed
, chan
);
906 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
907 consumed_idx
, buf_trunc_val(consumed
, chan
));
909 * update_read_sb_index return value ignored. Don't exchange sub-buffer
910 * if the writer concurrently updated it.
915 * cons_offset is an iterator on all subbuffer offsets between the reader
916 * position and the writer position. (inclusive)
919 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
920 struct channel
*chan
,
921 unsigned long cons_offset
,
924 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
925 unsigned long cons_idx
, commit_count
, commit_count_sb
;
927 cons_idx
= subbuf_index(cons_offset
, chan
);
928 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[cons_idx
].cc
);
929 commit_count_sb
= v_read(config
, &shmp(buf
->commit_cold
)[cons_idx
].cc_sb
);
931 if (subbuf_offset(commit_count
, chan
) != 0)
932 ERRMSG("ring buffer %s, cpu %d: "
933 "commit count in subbuffer %lu,\n"
934 "expecting multiples of %lu bytes\n"
935 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
936 chan
->backend
.name
, cpu
, cons_idx
,
937 chan
->backend
.subbuf_size
,
938 commit_count
, commit_count_sb
);
940 ERRMSG("ring buffer: %s, cpu %d: %lu bytes committed\n",
941 chan
->backend
.name
, cpu
, commit_count
);
945 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
946 struct channel
*chan
,
949 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
950 unsigned long write_offset
, cons_offset
;
953 * Can be called in the error path of allocation when
954 * trans_channel_data is not yet set.
959 * No need to order commit_count, write_offset and cons_offset reads
960 * because we execute at teardown when no more writer nor reader
961 * references are left.
963 write_offset
= v_read(config
, &buf
->offset
);
964 cons_offset
= uatomic_read(&buf
->consumed
);
965 if (write_offset
!= cons_offset
)
966 ERRMSG("ring buffer %s, cpu %d: "
967 "non-consumed data\n"
968 " [ %lu bytes written, %lu bytes read ]\n",
969 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
971 for (cons_offset
= uatomic_read(&buf
->consumed
);
972 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
975 cons_offset
= subbuf_align(cons_offset
, chan
))
976 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
981 void lib_ring_buffer_print_errors(struct channel
*chan
,
982 struct lib_ring_buffer
*buf
, int cpu
)
984 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
985 void *priv
= chan
->backend
.priv
;
987 ERRMSG("ring buffer %s, cpu %d: %lu records written, "
988 "%lu records overrun\n",
989 chan
->backend
.name
, cpu
,
990 v_read(config
, &buf
->records_count
),
991 v_read(config
, &buf
->records_overrun
));
993 if (v_read(config
, &buf
->records_lost_full
)
994 || v_read(config
, &buf
->records_lost_wrap
)
995 || v_read(config
, &buf
->records_lost_big
))
996 ERRMSG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
997 " [ %lu buffer full, %lu nest buffer wrap-around, "
998 "%lu event too big ]\n",
999 chan
->backend
.name
, cpu
,
1000 v_read(config
, &buf
->records_lost_full
),
1001 v_read(config
, &buf
->records_lost_wrap
),
1002 v_read(config
, &buf
->records_lost_big
));
1004 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1008 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1010 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1013 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1014 struct channel
*chan
,
1015 struct switch_offsets
*offsets
,
1018 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1019 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1020 unsigned long commit_count
;
1022 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1025 * Order all writes to buffer before the commit count update that will
1026 * determine that the subbuffer is full.
1029 v_add(config
, config
->cb
.subbuffer_header_size(),
1030 &shmp(buf
->commit_hot
)[oldidx
].cc
);
1031 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[oldidx
].cc
);
1032 /* Check if the written buffer has to be delivered */
1033 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1034 commit_count
, oldidx
);
1035 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1036 offsets
->old
, commit_count
,
1037 config
->cb
.subbuffer_header_size());
1041 * lib_ring_buffer_switch_old_end: switch old subbuffer
1043 * Note : offset_old should never be 0 here. It is ok, because we never perform
1044 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1045 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1049 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1050 struct channel
*chan
,
1051 struct switch_offsets
*offsets
,
1054 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1055 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1056 unsigned long commit_count
, padding_size
, data_size
;
1058 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1059 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1060 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1063 * Order all writes to buffer before the commit count update that will
1064 * determine that the subbuffer is full.
1067 v_add(config
, padding_size
, &shmp(buf
->commit_hot
)[oldidx
].cc
);
1068 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[oldidx
].cc
);
1069 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1070 commit_count
, oldidx
);
1071 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1072 offsets
->old
, commit_count
,
1077 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1079 * This code can be executed unordered : writers may already have written to the
1080 * sub-buffer before this code gets executed, caution. The commit makes sure
1081 * that this code is executed before the deliver of this sub-buffer.
1084 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1085 struct channel
*chan
,
1086 struct switch_offsets
*offsets
,
1089 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1090 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1091 unsigned long commit_count
;
1093 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1096 * Order all writes to buffer before the commit count update that will
1097 * determine that the subbuffer is full.
1100 v_add(config
, config
->cb
.subbuffer_header_size(),
1101 &shmp(buf
->commit_hot
)[beginidx
].cc
);
1102 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[beginidx
].cc
);
1103 /* Check if the written buffer has to be delivered */
1104 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1105 commit_count
, beginidx
);
1106 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1107 offsets
->begin
, commit_count
,
1108 config
->cb
.subbuffer_header_size());
1112 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1114 * The only remaining threads could be the ones with pending commits. They will
1115 * have to do the deliver themselves.
1118 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1119 struct channel
*chan
,
1120 struct switch_offsets
*offsets
,
1123 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1124 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1125 unsigned long commit_count
, padding_size
, data_size
;
1127 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1128 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1129 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1132 * Order all writes to buffer before the commit count update that will
1133 * determine that the subbuffer is full.
1136 v_add(config
, padding_size
, &shmp(buf
->commit_hot
)[endidx
].cc
);
1137 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[endidx
].cc
);
1138 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1139 commit_count
, endidx
);
1140 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1141 offsets
->end
, commit_count
,
1148 * !0 if execution must be aborted.
1151 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1152 struct lib_ring_buffer
*buf
,
1153 struct channel
*chan
,
1154 struct switch_offsets
*offsets
,
1157 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1160 offsets
->begin
= v_read(config
, &buf
->offset
);
1161 offsets
->old
= offsets
->begin
;
1162 offsets
->switch_old_start
= 0;
1163 off
= subbuf_offset(offsets
->begin
, chan
);
1165 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1168 * Ensure we flush the header of an empty subbuffer when doing the
1169 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1170 * total data gathering duration even if there were no records saved
1171 * after the last buffer switch.
1172 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1173 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1174 * subbuffer header as appropriate.
1175 * The next record that reserves space will be responsible for
1176 * populating the following subbuffer header. We choose not to populate
1177 * the next subbuffer header here because we want to be able to use
1178 * SWITCH_ACTIVE for periodical buffer flush, which must
1179 * guarantee that all the buffer content (records and header
1180 * timestamps) are visible to the reader. This is required for
1181 * quiescence guarantees for the fusion merge.
1183 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1184 if (unlikely(off
== 0)) {
1186 * The client does not save any header information.
1187 * Don't switch empty subbuffer on finalize, because it
1188 * is invalid to deliver a completely empty subbuffer.
1190 if (!config
->cb
.subbuffer_header_size())
1193 * Need to write the subbuffer start header on finalize.
1195 offsets
->switch_old_start
= 1;
1197 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1199 return -1; /* we do not have to switch : buffer is empty */
1200 /* Note: old points to the next subbuf at offset 0 */
1201 offsets
->end
= offsets
->begin
;
1206 * Force a sub-buffer switch. This operation is completely reentrant : can be
1207 * called while tracing is active with absolutely no lock held.
1209 * Note, however, that as a v_cmpxchg is used for some atomic
1210 * operations, this function must be called from the CPU which owns the buffer
1211 * for a ACTIVE flush.
1213 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1215 struct channel
*chan
= shmp(buf
->backend
.chan
);
1216 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1217 struct switch_offsets offsets
;
1218 unsigned long oldidx
;
1224 * Perform retryable operations.
1227 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1229 return; /* Switch not needed */
1230 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1234 * Atomically update last_tsc. This update races against concurrent
1235 * atomic updates, but the race will always cause supplementary full TSC
1236 * records, never the opposite (missing a full TSC record when it would
1239 save_last_tsc(config
, buf
, tsc
);
1242 * Push the reader if necessary
1244 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1246 oldidx
= subbuf_index(offsets
.old
, chan
);
1247 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1250 * May need to populate header start on SWITCH_FLUSH.
1252 if (offsets
.switch_old_start
) {
1253 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1254 offsets
.old
+= config
->cb
.subbuffer_header_size();
1258 * Switch old subbuffer.
1260 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1266 * -ENOSPC if event size is too large for packet.
1267 * -ENOBUFS if there is currently not enough space in buffer for the event.
1268 * -EIO if data cannot be written into the buffer for any other reason.
1271 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1272 struct channel
*chan
,
1273 struct switch_offsets
*offsets
,
1274 struct lib_ring_buffer_ctx
*ctx
)
1276 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1277 unsigned long reserve_commit_diff
;
1279 offsets
->begin
= v_read(config
, &buf
->offset
);
1280 offsets
->old
= offsets
->begin
;
1281 offsets
->switch_new_start
= 0;
1282 offsets
->switch_new_end
= 0;
1283 offsets
->switch_old_end
= 0;
1284 offsets
->pre_header_padding
= 0;
1286 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1287 if ((int64_t) ctx
->tsc
== -EIO
)
1290 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1291 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1293 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1294 offsets
->switch_new_start
= 1; /* For offsets->begin */
1296 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1298 &offsets
->pre_header_padding
,
1301 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1304 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1305 offsets
->size
> chan
->backend
.subbuf_size
)) {
1306 offsets
->switch_old_end
= 1; /* For offsets->old */
1307 offsets
->switch_new_start
= 1; /* For offsets->begin */
1310 if (unlikely(offsets
->switch_new_start
)) {
1311 unsigned long sb_index
;
1314 * We are typically not filling the previous buffer completely.
1316 if (likely(offsets
->switch_old_end
))
1317 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1318 offsets
->begin
= offsets
->begin
1319 + config
->cb
.subbuffer_header_size();
1320 /* Test new buffer integrity */
1321 sb_index
= subbuf_index(offsets
->begin
, chan
);
1322 reserve_commit_diff
=
1323 (buf_trunc(offsets
->begin
, chan
)
1324 >> chan
->backend
.num_subbuf_order
)
1325 - ((unsigned long) v_read(config
,
1326 &shmp(buf
->commit_cold
)[sb_index
].cc_sb
)
1327 & chan
->commit_count_mask
);
1328 if (likely(reserve_commit_diff
== 0)) {
1329 /* Next subbuffer not being written to. */
1330 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1331 subbuf_trunc(offsets
->begin
, chan
)
1332 - subbuf_trunc((unsigned long)
1333 uatomic_read(&buf
->consumed
), chan
)
1334 >= chan
->backend
.buf_size
)) {
1336 * We do not overwrite non consumed buffers
1337 * and we are full : record is lost.
1339 v_inc(config
, &buf
->records_lost_full
);
1343 * Next subbuffer not being written to, and we
1344 * are either in overwrite mode or the buffer is
1345 * not full. It's safe to write in this new
1351 * Next subbuffer reserve offset does not match the
1352 * commit offset. Drop record in producer-consumer and
1353 * overwrite mode. Caused by either a writer OOPS or too
1354 * many nested writes over a reserve/commit pair.
1356 v_inc(config
, &buf
->records_lost_wrap
);
1360 config
->cb
.record_header_size(config
, chan
,
1362 &offsets
->pre_header_padding
,
1365 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1368 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1369 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1371 * Record too big for subbuffers, report error, don't
1372 * complete the sub-buffer switch.
1374 v_inc(config
, &buf
->records_lost_big
);
1378 * We just made a successful buffer switch and the
1379 * record fits in the new subbuffer. Let's write.
1384 * Record fits in the current buffer and we are not on a switch
1385 * boundary. It's safe to write.
1388 offsets
->end
= offsets
->begin
+ offsets
->size
;
1390 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1392 * The offset_end will fall at the very beginning of the next
1395 offsets
->switch_new_end
= 1; /* For offsets->begin */
1401 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1402 * @ctx: ring buffer context.
1404 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1405 * -EIO for other errors, else returns 0.
1406 * It will take care of sub-buffer switching.
1408 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1410 struct channel
*chan
= ctx
->chan
;
1411 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1412 struct lib_ring_buffer
*buf
;
1413 struct switch_offsets offsets
;
1416 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1417 buf
= &shmp(chan
->backend
.buf
)[ctx
->cpu
];
1419 buf
= shmp(chan
->backend
.buf
);
1425 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1429 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1434 * Atomically update last_tsc. This update races against concurrent
1435 * atomic updates, but the race will always cause supplementary full TSC
1436 * records, never the opposite (missing a full TSC record when it would
1439 save_last_tsc(config
, buf
, ctx
->tsc
);
1442 * Push the reader if necessary
1444 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1447 * Clear noref flag for this subbuffer.
1449 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1450 subbuf_index(offsets
.end
- 1, chan
));
1453 * Switch old subbuffer if needed.
1455 if (unlikely(offsets
.switch_old_end
)) {
1456 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1457 subbuf_index(offsets
.old
- 1, chan
));
1458 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1462 * Populate new subbuffer.
1464 if (unlikely(offsets
.switch_new_start
))
1465 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1467 if (unlikely(offsets
.switch_new_end
))
1468 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1470 ctx
->slot_size
= offsets
.size
;
1471 ctx
->pre_offset
= offsets
.begin
;
1472 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;