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>
43 #include <urcu/compiler.h>
53 * Internal structure representing offsets to use at a sub-buffer switch.
55 struct switch_offsets
{
56 unsigned long begin
, end
, old
;
57 size_t pre_header_padding
, size
;
58 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
62 __thread
unsigned int lib_ring_buffer_nesting
;
65 void lib_ring_buffer_print_errors(struct channel
*chan
,
66 struct lib_ring_buffer
*buf
, int cpu
);
69 * Must be called under cpu hotplug protection.
71 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
73 struct channel
*chan
= shmp(buf
->backend
.chan
);
75 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
76 free(shmp(buf
->commit_hot
));
77 free(shmp(buf
->commit_cold
));
79 lib_ring_buffer_backend_free(&buf
->backend
);
83 * lib_ring_buffer_reset - Reset ring buffer to initial values.
86 * Effectively empty the ring buffer. Should be called when the buffer is not
87 * used for writing. The ring buffer can be opened for reading, but the reader
88 * should not be using the iterator concurrently with reset. The previous
89 * current iterator record is reset.
91 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
93 struct channel
*chan
= shmp(buf
->backend
.chan
);
94 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
98 * Reset iterator first. It will put the subbuffer if it currently holds
101 v_set(config
, &buf
->offset
, 0);
102 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
103 v_set(config
, &shmp(buf
->commit_hot
)[i
].cc
, 0);
104 v_set(config
, &shmp(buf
->commit_hot
)[i
].seq
, 0);
105 v_set(config
, &shmp(buf
->commit_cold
)[i
].cc_sb
, 0);
107 uatomic_set(&buf
->consumed
, 0);
108 uatomic_set(&buf
->record_disabled
, 0);
109 v_set(config
, &buf
->last_tsc
, 0);
110 lib_ring_buffer_backend_reset(&buf
->backend
);
111 /* Don't reset number of active readers */
112 v_set(config
, &buf
->records_lost_full
, 0);
113 v_set(config
, &buf
->records_lost_wrap
, 0);
114 v_set(config
, &buf
->records_lost_big
, 0);
115 v_set(config
, &buf
->records_count
, 0);
116 v_set(config
, &buf
->records_overrun
, 0);
121 * channel_reset - Reset channel to initial values.
124 * Effectively empty the channel. Should be called when the channel is not used
125 * for writing. The channel can be opened for reading, but the reader should not
126 * be using the iterator concurrently with reset. The previous current iterator
129 void channel_reset(struct channel
*chan
)
132 * Reset iterators first. Will put the subbuffer if held for reading.
134 uatomic_set(&chan
->record_disabled
, 0);
135 /* Don't reset commit_count_mask, still valid */
136 channel_backend_reset(&chan
->backend
);
137 /* Don't reset switch/read timer interval */
138 /* Don't reset notifiers and notifier enable bits */
139 /* Don't reset reader reference count */
143 * Must be called under cpu hotplug protection.
145 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
146 struct channel_backend
*chanb
, int cpu
,
147 struct shm_header
*shm_header
)
149 const struct lib_ring_buffer_config
*config
= chanb
->config
;
150 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
151 void *priv
= chanb
->priv
;
152 unsigned int num_subbuf
;
153 size_t subbuf_header_size
;
157 /* Test for cpu hotplug */
158 if (buf
->backend
.allocated
)
161 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
166 set_shmp(&buf
->commit_hot
,
167 zalloc_shm(shm_header
,
168 sizeof(*buf
->commit_hot
) * chan
->backend
.num_subbuf
));
169 if (!shmp(buf
->commit_hot
)) {
174 set_shmp(&buf
->commit_cold
,
175 zalloc_shm(shm_header
,
176 sizeof(*buf
->commit_cold
) * chan
->backend
.num_subbuf
));
177 if (!shmp(buf
->commit_cold
)) {
182 num_subbuf
= chan
->backend
.num_subbuf
;
183 //init_waitqueue_head(&buf->read_wait);
186 * Write the subbuffer header for first subbuffer so we know the total
187 * duration of data gathering.
189 subbuf_header_size
= config
->cb
.subbuffer_header_size();
190 v_set(config
, &buf
->offset
, subbuf_header_size
);
191 subbuffer_id_clear_noref(config
, &shmp(buf
->backend
.buf_wsb
)[0].id
);
192 tsc
= config
->cb
.ring_buffer_clock_read(shmp(buf
->backend
.chan
));
193 config
->cb
.buffer_begin(buf
, tsc
, 0);
194 v_add(config
, subbuf_header_size
, &shmp(buf
->commit_hot
)[0].cc
);
196 if (config
->cb
.buffer_create
) {
197 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
201 buf
->backend
.allocated
= 1;
206 /* commit_cold will be freed by shm teardown */
208 /* commit_hot will be freed by shm teardown */
210 lib_ring_buffer_backend_free(&buf
->backend
);
214 static void switch_buffer_timer(unsigned long data
)
216 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
217 struct channel
*chan
= shmp(buf
->backend
.chan
);
218 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
221 * Only flush buffers periodically if readers are active.
223 if (uatomic_read(&buf
->active_readers
))
224 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
227 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
228 // mod_timer_pinned(&buf->switch_timer,
229 // jiffies + chan->switch_timer_interval);
231 // mod_timer(&buf->switch_timer,
232 // jiffies + chan->switch_timer_interval);
235 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
237 struct channel
*chan
= shmp(buf
->backend
.chan
);
238 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
240 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
243 //init_timer(&buf->switch_timer);
244 //buf->switch_timer.function = switch_buffer_timer;
245 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
246 //buf->switch_timer.data = (unsigned long)buf;
247 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
248 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
250 // add_timer(&buf->switch_timer);
251 buf
->switch_timer_enabled
= 1;
254 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
256 struct channel
*chan
= shmp(buf
->backend
.chan
);
258 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
262 //del_timer_sync(&buf->switch_timer);
263 buf
->switch_timer_enabled
= 0;
267 * Polling timer to check the channels for data.
269 static void read_buffer_timer(unsigned long data
)
271 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
272 struct channel
*chan
= shmp(buf
->backend
.chan
);
273 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
275 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
277 if (uatomic_read(&buf
->active_readers
)
278 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
280 //wake_up_interruptible(&buf->read_wait);
281 //wake_up_interruptible(&chan->read_wait);
285 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
286 // mod_timer_pinned(&buf->read_timer,
287 // jiffies + chan->read_timer_interval);
289 // mod_timer(&buf->read_timer,
290 // jiffies + chan->read_timer_interval);
293 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
295 struct channel
*chan
= shmp(buf
->backend
.chan
);
296 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
298 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
299 || !chan
->read_timer_interval
300 || buf
->read_timer_enabled
)
304 //init_timer(&buf->read_timer);
305 //buf->read_timer.function = read_buffer_timer;
306 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
307 //buf->read_timer.data = (unsigned long)buf;
309 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
310 // add_timer_on(&buf->read_timer, buf->backend.cpu);
312 // add_timer(&buf->read_timer);
313 buf
->read_timer_enabled
= 1;
316 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
318 struct channel
*chan
= shmp(buf
->backend
.chan
);
319 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
321 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
322 || !chan
->read_timer_interval
323 || !buf
->read_timer_enabled
)
327 //del_timer_sync(&buf->read_timer);
329 * do one more check to catch data that has been written in the last
332 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
334 //wake_up_interruptible(&buf->read_wait);
335 //wake_up_interruptible(&chan->read_wait);
337 buf
->read_timer_enabled
= 0;
340 static void channel_unregister_notifiers(struct channel
*chan
)
342 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
345 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
346 for_each_possible_cpu(cpu
) {
347 struct lib_ring_buffer
*buf
= &shmp(chan
->backend
.buf
)[cpu
];
349 lib_ring_buffer_stop_switch_timer(buf
);
350 lib_ring_buffer_stop_read_timer(buf
);
353 struct lib_ring_buffer
*buf
= shmp(chan
->backend
.buf
);
355 lib_ring_buffer_stop_switch_timer(buf
);
356 lib_ring_buffer_stop_read_timer(buf
);
358 channel_backend_unregister_notifiers(&chan
->backend
);
361 static void channel_free(struct channel
*chan
)
363 channel_backend_free(&chan
->backend
);
368 * channel_create - Create channel.
369 * @config: ring buffer instance configuration
370 * @name: name of the channel
371 * @priv: ring buffer client private data
372 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
373 * address mapping. It is used only by RING_BUFFER_STATIC
374 * configuration. It can be set to NULL for other backends.
375 * @subbuf_size: subbuffer size
376 * @num_subbuf: number of subbuffers
377 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
378 * padding to let readers get those sub-buffers.
379 * Used for live streaming.
380 * @read_timer_interval: Time interval (in us) to wake up pending readers.
381 * @shmid: shared memory ID (output)
384 * Returns NULL on failure.
386 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
387 const char *name
, void *priv
, void *buf_addr
,
389 size_t num_subbuf
, unsigned int switch_timer_interval
,
390 unsigned int read_timer_interval
,
394 struct channel
*chan
;
395 size_t shmsize
, bufshmsize
;
396 struct shm_header
*shm_header
;
397 unsigned long num_subbuf_alloc
;
399 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
400 read_timer_interval
))
403 /* Calculate the shm allocation layout */
404 shmsize
= sizeof(struct shm_header
);
405 shmsize
+= sizeof(struct channel
);
407 /* Per-cpu buffer size: control (prior to backend) */
408 bufshmsize
= sizeof(struct lib_ring_buffer
);
409 shmsize
+= bufshmsize
* num_possible_cpus();
411 /* Per-cpu buffer size: backend */
412 /* num_subbuf + 1 is the worse case */
413 num_subbuf_alloc
= num_subbuf
+ 1;
414 bufshmsize
= sizeof(struct lib_ring_buffer_backend_pages
*) * num_subbuf_alloc
;
415 bufshmsize
+= subbuf_size
* (num_subbuf_alloc
);
416 bufshmsize
+= (sizeof(struct lib_ring_buffer_backend_pages
) + subbuf_size
) * num_subbuf_alloc
;
417 bufshmsize
+= sizeof(struct lib_ring_buffer_backend_subbuffer
) * num_subbuf
;
418 shmsize
+= bufshmsize
* num_possible_cpus();
420 /* Per-cpu buffer size: control (after backend) */
421 bufshmsize
+= sizeof(struct commit_counters_hot
) * num_subbuf
;
422 bufshmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
425 *shmid
= shmget(getpid(), shmsize
, IPC_CREAT
| IPC_EXCL
| 0700);
428 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
434 shm_header
= shmat(*shmid
, NULL
, 0);
435 if (shm_header
== (void *) -1) {
440 /* Already mark the shared memory for destruction. This will occur only
441 * when all users have detached.
443 ret
= shmctl(*shmid
, IPC_RMID
, NULL
);
449 shm_header
->magic
= SHM_MAGIC
;
450 shm_header
->major
= SHM_MAJOR
;
451 shm_header
->major
= SHM_MINOR
;
452 shm_header
->bits_per_long
= CAA_BITS_PER_LONG
;
453 shm_header
->shm_size
= shmsize
;
454 shm_header
->shm_allocated
= sizeof(struct shm_header
);
456 chan
= zalloc_shm(shm_header
, sizeof(struct channel
));
459 set_shmp(shm_header
->chan
, chan
);
461 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
462 subbuf_size
, num_subbuf
, shm_header
);
466 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
468 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
469 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
470 urcu_ref_init(&chan
->ref
);
472 //init_waitqueue_head(&chan->read_wait);
473 //init_waitqueue_head(&chan->hp_wait);
475 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
477 * In case of non-hotplug cpu, if the ring-buffer is allocated
478 * in early initcall, it will not be notified of secondary cpus.
479 * In that off case, we need to allocate for all possible cpus.
481 for_each_possible_cpu(cpu
) {
482 struct lib_ring_buffer
*buf
= &shmp(chan
->backend
.buf
)[cpu
];
483 lib_ring_buffer_start_switch_timer(buf
);
484 lib_ring_buffer_start_read_timer(buf
);
487 struct lib_ring_buffer
*buf
= shmp(chan
->backend
.buf
);
489 lib_ring_buffer_start_switch_timer(buf
);
490 lib_ring_buffer_start_read_timer(buf
);
496 ret
= shmctl(*shmid
, IPC_RMID
, NULL
);
504 void channel_release(struct urcu_ref
*ref
)
506 struct channel
*chan
= caa_container_of(ref
, struct channel
, ref
);
511 * channel_destroy - Finalize, wait for q.s. and destroy channel.
512 * @chan: channel to destroy
515 * Call "destroy" callback, finalize channels, wait for readers to release their
516 * reference, then destroy ring buffer data. Note that when readers have
517 * completed data consumption of finalized channels, get_subbuf() will return
518 * -ENODATA. They should release their handle at that point.
519 * Returns the private data pointer.
521 void *channel_destroy(struct channel
*chan
)
524 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
527 channel_unregister_notifiers(chan
);
529 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
530 for_each_channel_cpu(cpu
, chan
) {
531 struct lib_ring_buffer
*buf
= &shmp(chan
->backend
.buf
)[cpu
];
533 if (config
->cb
.buffer_finalize
)
534 config
->cb
.buffer_finalize(buf
,
537 if (buf
->backend
.allocated
)
538 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
540 * Perform flush before writing to finalized.
543 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
544 //wake_up_interruptible(&buf->read_wait);
547 struct lib_ring_buffer
*buf
= shmp(chan
->backend
.buf
);
549 if (config
->cb
.buffer_finalize
)
550 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
551 if (buf
->backend
.allocated
)
552 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
554 * Perform flush before writing to finalized.
557 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
558 //wake_up_interruptible(&buf->read_wait);
560 CMM_ACCESS_ONCE(chan
->finalized
) = 1;
561 //wake_up_interruptible(&chan->hp_wait);
562 //wake_up_interruptible(&chan->read_wait);
563 urcu_ref_put(&chan
->ref
, channel_release
);
564 priv
= chan
->backend
.priv
;
568 struct lib_ring_buffer
*channel_get_ring_buffer(
569 const struct lib_ring_buffer_config
*config
,
570 struct channel
*chan
, int cpu
)
572 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
573 return shmp(chan
->backend
.buf
);
575 return &shmp(chan
->backend
.buf
)[cpu
];
578 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
580 struct channel
*chan
= shmp(buf
->backend
.chan
);
582 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
584 urcu_ref_get(&chan
->ref
);
589 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
591 struct channel
*chan
= shmp(buf
->backend
.chan
);
593 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
595 uatomic_dec(&buf
->active_readers
);
596 urcu_ref_put(&chan
->ref
, channel_release
);
600 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
602 * @consumed: consumed count indicating the position where to read
603 * @produced: produced count, indicates position when to stop reading
605 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
606 * data to read at consumed position, or 0 if the get operation succeeds.
609 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
610 unsigned long *consumed
, unsigned long *produced
)
612 struct channel
*chan
= shmp(buf
->backend
.chan
);
613 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
614 unsigned long consumed_cur
, write_offset
;
617 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
619 * Read finalized before counters.
622 consumed_cur
= uatomic_read(&buf
->consumed
);
624 * No need to issue a memory barrier between consumed count read and
625 * write offset read, because consumed count can only change
626 * concurrently in overwrite mode, and we keep a sequence counter
627 * identifier derived from the write offset to check we are getting
628 * the same sub-buffer we are expecting (the sub-buffers are atomically
629 * "tagged" upon writes, tags are checked upon read).
631 write_offset
= v_read(config
, &buf
->offset
);
634 * Check that we are not about to read the same subbuffer in
635 * which the writer head is.
637 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
641 *consumed
= consumed_cur
;
642 *produced
= subbuf_trunc(write_offset
, chan
);
648 * The memory barriers __wait_event()/wake_up_interruptible() take care
649 * of "raw_spin_is_locked" memory ordering.
658 * lib_ring_buffer_put_snapshot - move consumed counter forward
660 * @consumed_new: new consumed count value
662 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
663 unsigned long consumed_new
)
665 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
666 struct channel
*chan
= shmp(bufb
->chan
);
667 unsigned long consumed
;
669 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
672 * Only push the consumed value forward.
673 * If the consumed cmpxchg fails, this is because we have been pushed by
674 * the writer in flight recorder mode.
676 consumed
= uatomic_read(&buf
->consumed
);
677 while ((long) consumed
- (long) consumed_new
< 0)
678 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
683 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
685 * @consumed: consumed count indicating the position where to read
687 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
688 * data to read at consumed position, or 0 if the get operation succeeds.
690 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
691 unsigned long consumed
)
693 struct channel
*chan
= shmp(buf
->backend
.chan
);
694 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
695 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
700 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
702 * Read finalized before counters.
705 consumed_cur
= uatomic_read(&buf
->consumed
);
706 consumed_idx
= subbuf_index(consumed
, chan
);
707 commit_count
= v_read(config
, &shmp(buf
->commit_cold
)[consumed_idx
].cc_sb
);
709 * Make sure we read the commit count before reading the buffer
710 * data and the write offset. Correct consumed offset ordering
711 * wrt commit count is insured by the use of cmpxchg to update
712 * the consumed offset.
715 * Local rmb to match the remote wmb to read the commit count
716 * before the buffer data and the write offset.
720 write_offset
= v_read(config
, &buf
->offset
);
723 * Check that the buffer we are getting is after or at consumed_cur
726 if ((long) subbuf_trunc(consumed
, chan
)
727 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
731 * Check that the subbuffer we are trying to consume has been
732 * already fully committed.
734 if (((commit_count
- chan
->backend
.subbuf_size
)
735 & chan
->commit_count_mask
)
736 - (buf_trunc(consumed_cur
, chan
)
737 >> chan
->backend
.num_subbuf_order
)
742 * Check that we are not about to read the same subbuffer in
743 * which the writer head is.
745 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
750 * Failure to get the subbuffer causes a busy-loop retry without going
751 * to a wait queue. These are caused by short-lived race windows where
752 * the writer is getting access to a subbuffer we were trying to get
753 * access to. Also checks that the "consumed" buffer count we are
754 * looking for matches the one contained in the subbuffer id.
756 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
757 consumed_idx
, buf_trunc_val(consumed
, chan
));
760 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
762 buf
->get_subbuf_consumed
= consumed
;
769 * The memory barriers __wait_event()/wake_up_interruptible() take care
770 * of "raw_spin_is_locked" memory ordering.
779 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
782 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
784 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
785 struct channel
*chan
= shmp(bufb
->chan
);
786 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
787 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
789 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
791 if (!buf
->get_subbuf
) {
793 * Reader puts a subbuffer it did not get.
795 CHAN_WARN_ON(chan
, 1);
798 consumed
= buf
->get_subbuf_consumed
;
802 * Clear the records_unread counter. (overruns counter)
803 * Can still be non-zero if a file reader simply grabbed the data
804 * without using iterators.
805 * Can be below zero if an iterator is used on a snapshot more than
808 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
809 v_add(config
, v_read(config
,
810 &shmp(bufb
->array
)[read_sb_bindex
]->records_unread
),
811 &bufb
->records_read
);
812 v_set(config
, &shmp(bufb
->array
)[read_sb_bindex
]->records_unread
, 0);
813 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
814 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
815 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
818 * Exchange the reader subbuffer with the one we put in its place in the
819 * writer subbuffer table. Expect the original consumed count. If
820 * update_read_sb_index fails, this is because the writer updated the
821 * subbuffer concurrently. We should therefore keep the subbuffer we
822 * currently have: it has become invalid to try reading this sub-buffer
823 * consumed count value anyway.
825 consumed_idx
= subbuf_index(consumed
, chan
);
826 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
827 consumed_idx
, buf_trunc_val(consumed
, chan
));
829 * update_read_sb_index return value ignored. Don't exchange sub-buffer
830 * if the writer concurrently updated it.
835 * cons_offset is an iterator on all subbuffer offsets between the reader
836 * position and the writer position. (inclusive)
839 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
840 struct channel
*chan
,
841 unsigned long cons_offset
,
844 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
845 unsigned long cons_idx
, commit_count
, commit_count_sb
;
847 cons_idx
= subbuf_index(cons_offset
, chan
);
848 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[cons_idx
].cc
);
849 commit_count_sb
= v_read(config
, &shmp(buf
->commit_cold
)[cons_idx
].cc_sb
);
851 if (subbuf_offset(commit_count
, chan
) != 0)
852 ERRMSG("ring buffer %s, cpu %d: "
853 "commit count in subbuffer %lu,\n"
854 "expecting multiples of %lu bytes\n"
855 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
856 chan
->backend
.name
, cpu
, cons_idx
,
857 chan
->backend
.subbuf_size
,
858 commit_count
, commit_count_sb
);
860 ERRMSG("ring buffer: %s, cpu %d: %lu bytes committed\n",
861 chan
->backend
.name
, cpu
, commit_count
);
865 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
866 struct channel
*chan
,
869 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
870 unsigned long write_offset
, cons_offset
;
873 * Can be called in the error path of allocation when
874 * trans_channel_data is not yet set.
879 * No need to order commit_count, write_offset and cons_offset reads
880 * because we execute at teardown when no more writer nor reader
881 * references are left.
883 write_offset
= v_read(config
, &buf
->offset
);
884 cons_offset
= uatomic_read(&buf
->consumed
);
885 if (write_offset
!= cons_offset
)
886 ERRMSG("ring buffer %s, cpu %d: "
887 "non-consumed data\n"
888 " [ %lu bytes written, %lu bytes read ]\n",
889 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
891 for (cons_offset
= uatomic_read(&buf
->consumed
);
892 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
895 cons_offset
= subbuf_align(cons_offset
, chan
))
896 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
901 void lib_ring_buffer_print_errors(struct channel
*chan
,
902 struct lib_ring_buffer
*buf
, int cpu
)
904 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
905 void *priv
= chan
->backend
.priv
;
907 ERRMSG("ring buffer %s, cpu %d: %lu records written, "
908 "%lu records overrun\n",
909 chan
->backend
.name
, cpu
,
910 v_read(config
, &buf
->records_count
),
911 v_read(config
, &buf
->records_overrun
));
913 if (v_read(config
, &buf
->records_lost_full
)
914 || v_read(config
, &buf
->records_lost_wrap
)
915 || v_read(config
, &buf
->records_lost_big
))
916 ERRMSG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
917 " [ %lu buffer full, %lu nest buffer wrap-around, "
918 "%lu event too big ]\n",
919 chan
->backend
.name
, cpu
,
920 v_read(config
, &buf
->records_lost_full
),
921 v_read(config
, &buf
->records_lost_wrap
),
922 v_read(config
, &buf
->records_lost_big
));
924 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
928 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
930 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
933 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
934 struct channel
*chan
,
935 struct switch_offsets
*offsets
,
938 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
939 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
940 unsigned long commit_count
;
942 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
945 * Order all writes to buffer before the commit count update that will
946 * determine that the subbuffer is full.
949 v_add(config
, config
->cb
.subbuffer_header_size(),
950 &shmp(buf
->commit_hot
)[oldidx
].cc
);
951 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[oldidx
].cc
);
952 /* Check if the written buffer has to be delivered */
953 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
954 commit_count
, oldidx
);
955 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
956 offsets
->old
, commit_count
,
957 config
->cb
.subbuffer_header_size());
961 * lib_ring_buffer_switch_old_end: switch old subbuffer
963 * Note : offset_old should never be 0 here. It is ok, because we never perform
964 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
965 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
969 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
970 struct channel
*chan
,
971 struct switch_offsets
*offsets
,
974 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
975 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
976 unsigned long commit_count
, padding_size
, data_size
;
978 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
979 padding_size
= chan
->backend
.subbuf_size
- data_size
;
980 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
983 * Order all writes to buffer before the commit count update that will
984 * determine that the subbuffer is full.
987 v_add(config
, padding_size
, &shmp(buf
->commit_hot
)[oldidx
].cc
);
988 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[oldidx
].cc
);
989 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
990 commit_count
, oldidx
);
991 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
992 offsets
->old
, commit_count
,
997 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
999 * This code can be executed unordered : writers may already have written to the
1000 * sub-buffer before this code gets executed, caution. The commit makes sure
1001 * that this code is executed before the deliver of this sub-buffer.
1004 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1005 struct channel
*chan
,
1006 struct switch_offsets
*offsets
,
1009 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1010 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1011 unsigned long commit_count
;
1013 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1016 * Order all writes to buffer before the commit count update that will
1017 * determine that the subbuffer is full.
1020 v_add(config
, config
->cb
.subbuffer_header_size(),
1021 &shmp(buf
->commit_hot
)[beginidx
].cc
);
1022 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[beginidx
].cc
);
1023 /* Check if the written buffer has to be delivered */
1024 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1025 commit_count
, beginidx
);
1026 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1027 offsets
->begin
, commit_count
,
1028 config
->cb
.subbuffer_header_size());
1032 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1034 * The only remaining threads could be the ones with pending commits. They will
1035 * have to do the deliver themselves.
1038 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1039 struct channel
*chan
,
1040 struct switch_offsets
*offsets
,
1043 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1044 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1045 unsigned long commit_count
, padding_size
, data_size
;
1047 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1048 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1049 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1052 * Order all writes to buffer before the commit count update that will
1053 * determine that the subbuffer is full.
1056 v_add(config
, padding_size
, &shmp(buf
->commit_hot
)[endidx
].cc
);
1057 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[endidx
].cc
);
1058 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1059 commit_count
, endidx
);
1060 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1061 offsets
->end
, commit_count
,
1068 * !0 if execution must be aborted.
1071 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1072 struct lib_ring_buffer
*buf
,
1073 struct channel
*chan
,
1074 struct switch_offsets
*offsets
,
1077 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1080 offsets
->begin
= v_read(config
, &buf
->offset
);
1081 offsets
->old
= offsets
->begin
;
1082 offsets
->switch_old_start
= 0;
1083 off
= subbuf_offset(offsets
->begin
, chan
);
1085 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1088 * Ensure we flush the header of an empty subbuffer when doing the
1089 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1090 * total data gathering duration even if there were no records saved
1091 * after the last buffer switch.
1092 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1093 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1094 * subbuffer header as appropriate.
1095 * The next record that reserves space will be responsible for
1096 * populating the following subbuffer header. We choose not to populate
1097 * the next subbuffer header here because we want to be able to use
1098 * SWITCH_ACTIVE for periodical buffer flush, which must
1099 * guarantee that all the buffer content (records and header
1100 * timestamps) are visible to the reader. This is required for
1101 * quiescence guarantees for the fusion merge.
1103 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1104 if (unlikely(off
== 0)) {
1106 * The client does not save any header information.
1107 * Don't switch empty subbuffer on finalize, because it
1108 * is invalid to deliver a completely empty subbuffer.
1110 if (!config
->cb
.subbuffer_header_size())
1113 * Need to write the subbuffer start header on finalize.
1115 offsets
->switch_old_start
= 1;
1117 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1119 return -1; /* we do not have to switch : buffer is empty */
1120 /* Note: old points to the next subbuf at offset 0 */
1121 offsets
->end
= offsets
->begin
;
1126 * Force a sub-buffer switch. This operation is completely reentrant : can be
1127 * called while tracing is active with absolutely no lock held.
1129 * Note, however, that as a v_cmpxchg is used for some atomic
1130 * operations, this function must be called from the CPU which owns the buffer
1131 * for a ACTIVE flush.
1133 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1135 struct channel
*chan
= shmp(buf
->backend
.chan
);
1136 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1137 struct switch_offsets offsets
;
1138 unsigned long oldidx
;
1144 * Perform retryable operations.
1147 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1149 return; /* Switch not needed */
1150 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1154 * Atomically update last_tsc. This update races against concurrent
1155 * atomic updates, but the race will always cause supplementary full TSC
1156 * records, never the opposite (missing a full TSC record when it would
1159 save_last_tsc(config
, buf
, tsc
);
1162 * Push the reader if necessary
1164 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1166 oldidx
= subbuf_index(offsets
.old
, chan
);
1167 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1170 * May need to populate header start on SWITCH_FLUSH.
1172 if (offsets
.switch_old_start
) {
1173 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1174 offsets
.old
+= config
->cb
.subbuffer_header_size();
1178 * Switch old subbuffer.
1180 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1186 * -ENOSPC if event size is too large for packet.
1187 * -ENOBUFS if there is currently not enough space in buffer for the event.
1188 * -EIO if data cannot be written into the buffer for any other reason.
1191 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1192 struct channel
*chan
,
1193 struct switch_offsets
*offsets
,
1194 struct lib_ring_buffer_ctx
*ctx
)
1196 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1197 unsigned long reserve_commit_diff
;
1199 offsets
->begin
= v_read(config
, &buf
->offset
);
1200 offsets
->old
= offsets
->begin
;
1201 offsets
->switch_new_start
= 0;
1202 offsets
->switch_new_end
= 0;
1203 offsets
->switch_old_end
= 0;
1204 offsets
->pre_header_padding
= 0;
1206 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1207 if ((int64_t) ctx
->tsc
== -EIO
)
1210 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1211 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1213 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1214 offsets
->switch_new_start
= 1; /* For offsets->begin */
1216 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1218 &offsets
->pre_header_padding
,
1221 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1224 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1225 offsets
->size
> chan
->backend
.subbuf_size
)) {
1226 offsets
->switch_old_end
= 1; /* For offsets->old */
1227 offsets
->switch_new_start
= 1; /* For offsets->begin */
1230 if (unlikely(offsets
->switch_new_start
)) {
1231 unsigned long sb_index
;
1234 * We are typically not filling the previous buffer completely.
1236 if (likely(offsets
->switch_old_end
))
1237 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1238 offsets
->begin
= offsets
->begin
1239 + config
->cb
.subbuffer_header_size();
1240 /* Test new buffer integrity */
1241 sb_index
= subbuf_index(offsets
->begin
, chan
);
1242 reserve_commit_diff
=
1243 (buf_trunc(offsets
->begin
, chan
)
1244 >> chan
->backend
.num_subbuf_order
)
1245 - ((unsigned long) v_read(config
,
1246 &shmp(buf
->commit_cold
)[sb_index
].cc_sb
)
1247 & chan
->commit_count_mask
);
1248 if (likely(reserve_commit_diff
== 0)) {
1249 /* Next subbuffer not being written to. */
1250 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1251 subbuf_trunc(offsets
->begin
, chan
)
1252 - subbuf_trunc((unsigned long)
1253 uatomic_read(&buf
->consumed
), chan
)
1254 >= chan
->backend
.buf_size
)) {
1256 * We do not overwrite non consumed buffers
1257 * and we are full : record is lost.
1259 v_inc(config
, &buf
->records_lost_full
);
1263 * Next subbuffer not being written to, and we
1264 * are either in overwrite mode or the buffer is
1265 * not full. It's safe to write in this new
1271 * Next subbuffer reserve offset does not match the
1272 * commit offset. Drop record in producer-consumer and
1273 * overwrite mode. Caused by either a writer OOPS or too
1274 * many nested writes over a reserve/commit pair.
1276 v_inc(config
, &buf
->records_lost_wrap
);
1280 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
)) {
1291 * Record too big for subbuffers, report error, don't
1292 * complete the sub-buffer switch.
1294 v_inc(config
, &buf
->records_lost_big
);
1298 * We just made a successful buffer switch and the
1299 * record fits in the new subbuffer. Let's write.
1304 * Record fits in the current buffer and we are not on a switch
1305 * boundary. It's safe to write.
1308 offsets
->end
= offsets
->begin
+ offsets
->size
;
1310 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1312 * The offset_end will fall at the very beginning of the next
1315 offsets
->switch_new_end
= 1; /* For offsets->begin */
1321 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1322 * @ctx: ring buffer context.
1324 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1325 * -EIO for other errors, else returns 0.
1326 * It will take care of sub-buffer switching.
1328 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1330 struct channel
*chan
= ctx
->chan
;
1331 const struct lib_ring_buffer_config
*config
= chan
->backend
.config
;
1332 struct lib_ring_buffer
*buf
;
1333 struct switch_offsets offsets
;
1336 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1337 buf
= &shmp(chan
->backend
.buf
)[ctx
->cpu
];
1339 buf
= shmp(chan
->backend
.buf
);
1345 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1349 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1354 * Atomically update last_tsc. This update races against concurrent
1355 * atomic updates, but the race will always cause supplementary full TSC
1356 * records, never the opposite (missing a full TSC record when it would
1359 save_last_tsc(config
, buf
, ctx
->tsc
);
1362 * Push the reader if necessary
1364 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1367 * Clear noref flag for this subbuffer.
1369 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1370 subbuf_index(offsets
.end
- 1, chan
));
1373 * Switch old subbuffer if needed.
1375 if (unlikely(offsets
.switch_old_end
)) {
1376 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1377 subbuf_index(offsets
.old
- 1, chan
));
1378 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1382 * Populate new subbuffer.
1384 if (unlikely(offsets
.switch_new_start
))
1385 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1387 if (unlikely(offsets
.switch_new_end
))
1388 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1390 ctx
->slot_size
= offsets
.size
;
1391 ctx
->pre_offset
= offsets
.begin
;
1392 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;