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>
50 #include <lttng/ringbuffer-config.h>
57 #define max(a, b) ((a) > (b) ? (a) : (b))
61 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
62 * close(2) to close the fd returned by shm_open.
63 * shm_unlink releases the shared memory object name.
64 * ftruncate(2) sets the size of the memory object.
65 * mmap/munmap maps the shared memory obj to a virtual address in the
66 * calling proceess (should be done both in libust and consumer).
67 * See shm_overview(7) for details.
68 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
71 * Since we don't need to access the object using its name, we can
72 * immediately shm_unlink(3) it, and only keep the handle with its file
77 * Internal structure representing offsets to use at a sub-buffer switch.
79 struct switch_offsets
{
80 unsigned long begin
, end
, old
;
81 size_t pre_header_padding
, size
;
82 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
86 __thread
unsigned int lib_ring_buffer_nesting
;
89 * TODO: this is unused. Errors are saved within the ring buffer.
90 * Eventually, allow consumerd to print these errors.
93 void lib_ring_buffer_print_errors(struct channel
*chan
,
94 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
95 struct lttng_ust_shm_handle
*handle
);
98 * lib_ring_buffer_reset - Reset ring buffer to initial values.
101 * Effectively empty the ring buffer. Should be called when the buffer is not
102 * used for writing. The ring buffer can be opened for reading, but the reader
103 * should not be using the iterator concurrently with reset. The previous
104 * current iterator record is reset.
106 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
107 struct lttng_ust_shm_handle
*handle
)
109 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
110 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
114 * Reset iterator first. It will put the subbuffer if it currently holds
117 v_set(config
, &buf
->offset
, 0);
118 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
119 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
120 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
121 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
123 uatomic_set(&buf
->consumed
, 0);
124 uatomic_set(&buf
->record_disabled
, 0);
125 v_set(config
, &buf
->last_tsc
, 0);
126 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
127 /* Don't reset number of active readers */
128 v_set(config
, &buf
->records_lost_full
, 0);
129 v_set(config
, &buf
->records_lost_wrap
, 0);
130 v_set(config
, &buf
->records_lost_big
, 0);
131 v_set(config
, &buf
->records_count
, 0);
132 v_set(config
, &buf
->records_overrun
, 0);
137 * channel_reset - Reset channel to initial values.
140 * Effectively empty the channel. Should be called when the channel is not used
141 * for writing. The channel can be opened for reading, but the reader should not
142 * be using the iterator concurrently with reset. The previous current iterator
145 void channel_reset(struct channel
*chan
)
148 * Reset iterators first. Will put the subbuffer if held for reading.
150 uatomic_set(&chan
->record_disabled
, 0);
151 /* Don't reset commit_count_mask, still valid */
152 channel_backend_reset(&chan
->backend
);
153 /* Don't reset switch/read timer interval */
154 /* Don't reset notifiers and notifier enable bits */
155 /* Don't reset reader reference count */
159 * Must be called under cpu hotplug protection.
161 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
162 struct channel_backend
*chanb
, int cpu
,
163 struct lttng_ust_shm_handle
*handle
,
164 struct shm_object
*shmobj
)
166 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
167 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
168 void *priv
= channel_get_private(chan
);
169 size_t subbuf_header_size
;
173 /* Test for cpu hotplug */
174 if (buf
->backend
.allocated
)
177 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
178 cpu
, handle
, shmobj
);
182 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
183 set_shmp(buf
->commit_hot
,
185 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
186 if (!shmp(handle
, buf
->commit_hot
)) {
191 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
192 set_shmp(buf
->commit_cold
,
194 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
195 if (!shmp(handle
, buf
->commit_cold
)) {
201 * Write the subbuffer header for first subbuffer so we know the total
202 * duration of data gathering.
204 subbuf_header_size
= config
->cb
.subbuffer_header_size();
205 v_set(config
, &buf
->offset
, subbuf_header_size
);
206 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
207 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
208 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
209 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
211 if (config
->cb
.buffer_create
) {
212 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
216 buf
->backend
.allocated
= 1;
221 /* commit_cold will be freed by shm teardown */
223 /* commit_hot will be freed by shm teardown */
229 static void switch_buffer_timer(unsigned long data
)
231 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
232 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
233 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
236 * Only flush buffers periodically if readers are active.
238 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
239 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
242 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
243 // mod_timer_pinned(&buf->switch_timer,
244 // jiffies + chan->switch_timer_interval);
246 // mod_timer(&buf->switch_timer,
247 // jiffies + chan->switch_timer_interval);
251 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
252 struct lttng_ust_shm_handle
*handle
)
254 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
255 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
257 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
260 //init_timer(&buf->switch_timer);
261 //buf->switch_timer.function = switch_buffer_timer;
262 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
263 //buf->switch_timer.data = (unsigned long)buf;
264 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
265 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
267 // add_timer(&buf->switch_timer);
268 buf
->switch_timer_enabled
= 1;
271 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
272 struct lttng_ust_shm_handle
*handle
)
274 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
276 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
280 //del_timer_sync(&buf->switch_timer);
281 buf
->switch_timer_enabled
= 0;
286 * Polling timer to check the channels for data.
288 static void read_buffer_timer(unsigned long data
)
290 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
291 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
292 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
294 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
296 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
297 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
299 //wake_up_interruptible(&buf->read_wait);
300 //wake_up_interruptible(&chan->read_wait);
304 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
305 // mod_timer_pinned(&buf->read_timer,
306 // jiffies + chan->read_timer_interval);
308 // mod_timer(&buf->read_timer,
309 // jiffies + chan->read_timer_interval);
313 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
314 struct lttng_ust_shm_handle
*handle
)
316 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
317 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
319 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
320 || !chan
->read_timer_interval
321 || buf
->read_timer_enabled
)
325 //init_timer(&buf->read_timer);
326 //buf->read_timer.function = read_buffer_timer;
327 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
328 //buf->read_timer.data = (unsigned long)buf;
330 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
331 // add_timer_on(&buf->read_timer, buf->backend.cpu);
333 // add_timer(&buf->read_timer);
334 buf
->read_timer_enabled
= 1;
337 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
338 struct lttng_ust_shm_handle
*handle
)
340 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
341 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
343 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
344 || !chan
->read_timer_interval
345 || !buf
->read_timer_enabled
)
349 //del_timer_sync(&buf->read_timer);
351 * do one more check to catch data that has been written in the last
354 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
356 //wake_up_interruptible(&buf->read_wait);
357 //wake_up_interruptible(&chan->read_wait);
359 buf
->read_timer_enabled
= 0;
362 static void channel_unregister_notifiers(struct channel
*chan
,
363 struct lttng_ust_shm_handle
*handle
)
365 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
368 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
369 for_each_possible_cpu(cpu
) {
370 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
372 lib_ring_buffer_stop_switch_timer(buf
, handle
);
373 lib_ring_buffer_stop_read_timer(buf
, handle
);
376 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
378 lib_ring_buffer_stop_switch_timer(buf
, handle
);
379 lib_ring_buffer_stop_read_timer(buf
, handle
);
381 //channel_backend_unregister_notifiers(&chan->backend);
384 static void channel_free(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
388 channel_backend_free(&chan
->backend
, handle
);
389 /* chan is freed by shm teardown */
390 shm_object_table_destroy(handle
->table
);
395 * channel_create - Create channel.
396 * @config: ring buffer instance configuration
397 * @name: name of the channel
398 * @priv_data: ring buffer client private data area pointer (output)
399 * @priv_data_size: length, in bytes, of the private data area.
400 * @priv_data_init: initialization data for private data.
401 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
402 * address mapping. It is used only by RING_BUFFER_STATIC
403 * configuration. It can be set to NULL for other backends.
404 * @subbuf_size: subbuffer size
405 * @num_subbuf: number of subbuffers
406 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
407 * padding to let readers get those sub-buffers.
408 * Used for live streaming.
409 * @read_timer_interval: Time interval (in us) to wake up pending readers.
412 * Returns NULL on failure.
414 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
417 size_t priv_data_align
,
418 size_t priv_data_size
,
419 void *priv_data_init
,
420 void *buf_addr
, size_t subbuf_size
,
421 size_t num_subbuf
, unsigned int switch_timer_interval
,
422 unsigned int read_timer_interval
,
423 int *shm_fd
, int *wait_fd
, uint64_t *memory_map_size
)
426 size_t shmsize
, chansize
;
427 struct channel
*chan
;
428 struct lttng_ust_shm_handle
*handle
;
429 struct shm_object
*shmobj
;
432 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
433 read_timer_interval
))
436 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
440 /* Allocate table for channel + per-cpu buffers */
441 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
443 goto error_table_alloc
;
445 /* Calculate the shm allocation layout */
446 shmsize
= sizeof(struct channel
);
447 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
448 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
449 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * num_possible_cpus();
451 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
);
453 shmsize
+= offset_align(shmsize
, priv_data_align
);
454 shmsize
+= priv_data_size
;
456 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
459 /* struct channel is at object 0, offset 0 (hardcoded) */
460 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
461 assert(handle
->chan
._ref
.index
== 0);
462 assert(handle
->chan
._ref
.offset
== 0);
463 chan
= shmp(handle
, handle
->chan
);
467 /* space for private data */
468 if (priv_data_size
) {
469 DECLARE_SHMP(void, priv_data_alloc
);
471 align_shm(shmobj
, priv_data_align
);
472 chan
->priv_data_offset
= shmobj
->allocated_len
;
473 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
474 if (!shmp(handle
, priv_data_alloc
))
476 *priv_data
= channel_get_private(chan
);
477 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
479 chan
->priv_data_offset
= -1;
483 ret
= channel_backend_init(&chan
->backend
, name
, config
,
484 subbuf_size
, num_subbuf
, handle
);
486 goto error_backend_init
;
488 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
490 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
491 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
493 //init_waitqueue_head(&chan->read_wait);
494 //init_waitqueue_head(&chan->hp_wait);
496 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
498 * In case of non-hotplug cpu, if the ring-buffer is allocated
499 * in early initcall, it will not be notified of secondary cpus.
500 * In that off case, we need to allocate for all possible cpus.
502 for_each_possible_cpu(cpu
) {
503 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
504 lib_ring_buffer_start_switch_timer(buf
, handle
);
505 lib_ring_buffer_start_read_timer(buf
, handle
);
508 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
510 lib_ring_buffer_start_switch_timer(buf
, handle
);
511 lib_ring_buffer_start_read_timer(buf
, handle
);
513 ref
= &handle
->chan
._ref
;
514 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
, memory_map_size
);
519 shm_object_table_destroy(handle
->table
);
525 struct lttng_ust_shm_handle
*channel_handle_create(int shm_fd
, int wait_fd
,
526 uint64_t memory_map_size
)
528 struct lttng_ust_shm_handle
*handle
;
529 struct shm_object
*object
;
531 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
535 /* Allocate table for channel + per-cpu buffers */
536 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
538 goto error_table_alloc
;
539 /* Add channel object */
540 object
= shm_object_table_append_shadow(handle
->table
,
541 shm_fd
, wait_fd
, memory_map_size
);
543 goto error_table_object
;
544 /* struct channel is at object 0, offset 0 (hardcoded) */
545 handle
->chan
._ref
.index
= 0;
546 handle
->chan
._ref
.offset
= 0;
550 shm_object_table_destroy(handle
->table
);
556 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
557 int shm_fd
, int wait_fd
, uint64_t memory_map_size
)
559 struct shm_object
*object
;
561 /* Add stream object */
562 object
= shm_object_table_append_shadow(handle
->table
,
563 shm_fd
, wait_fd
, memory_map_size
);
570 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
573 channel_free(chan
, handle
, shadow
);
577 * channel_destroy - Finalize, wait for q.s. and destroy channel.
578 * @chan: channel to destroy
581 * Call "destroy" callback, finalize channels, decrement the channel
582 * reference count. Note that when readers have completed data
583 * consumption of finalized channels, get_subbuf() will return -ENODATA.
584 * They should release their handle at that point.
586 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
590 channel_release(chan
, handle
, shadow
);
594 channel_unregister_notifiers(chan
, handle
);
597 * Note: the consumer takes care of finalizing and switching the
602 * sessiond/consumer are keeping a reference on the shm file
603 * descriptor directly. No need to refcount.
605 channel_release(chan
, handle
, shadow
);
609 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
610 const struct lttng_ust_lib_ring_buffer_config
*config
,
611 struct channel
*chan
, int cpu
,
612 struct lttng_ust_shm_handle
*handle
,
613 int *shm_fd
, int *wait_fd
,
614 uint64_t *memory_map_size
)
618 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
619 ref
= &chan
->backend
.buf
[0].shmp
._ref
;
620 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
622 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
624 if (cpu
>= num_possible_cpus())
626 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
627 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
629 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
633 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
634 struct lttng_ust_shm_handle
*handle
,
638 if (uatomic_cmpxchg(&buf
->active_shadow_readers
, 0, 1) != 0)
643 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
649 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
650 struct lttng_ust_shm_handle
*handle
,
653 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
656 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_shadow_readers
) != 1);
658 uatomic_dec(&buf
->active_shadow_readers
);
661 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
663 uatomic_dec(&buf
->active_readers
);
667 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
669 * @consumed: consumed count indicating the position where to read
670 * @produced: produced count, indicates position when to stop reading
672 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
673 * data to read at consumed position, or 0 if the get operation succeeds.
676 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
677 unsigned long *consumed
, unsigned long *produced
,
678 struct lttng_ust_shm_handle
*handle
)
680 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
681 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
682 unsigned long consumed_cur
, write_offset
;
685 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
687 * Read finalized before counters.
690 consumed_cur
= uatomic_read(&buf
->consumed
);
692 * No need to issue a memory barrier between consumed count read and
693 * write offset read, because consumed count can only change
694 * concurrently in overwrite mode, and we keep a sequence counter
695 * identifier derived from the write offset to check we are getting
696 * the same sub-buffer we are expecting (the sub-buffers are atomically
697 * "tagged" upon writes, tags are checked upon read).
699 write_offset
= v_read(config
, &buf
->offset
);
702 * Check that we are not about to read the same subbuffer in
703 * which the writer head is.
705 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
709 *consumed
= consumed_cur
;
710 *produced
= subbuf_trunc(write_offset
, chan
);
716 * The memory barriers __wait_event()/wake_up_interruptible() take care
717 * of "raw_spin_is_locked" memory ordering.
726 * lib_ring_buffer_put_snapshot - move consumed counter forward
728 * @consumed_new: new consumed count value
730 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
731 unsigned long consumed_new
,
732 struct lttng_ust_shm_handle
*handle
)
734 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
735 struct channel
*chan
= shmp(handle
, bufb
->chan
);
736 unsigned long consumed
;
738 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
739 && uatomic_read(&buf
->active_shadow_readers
) != 1);
742 * Only push the consumed value forward.
743 * If the consumed cmpxchg fails, this is because we have been pushed by
744 * the writer in flight recorder mode.
746 consumed
= uatomic_read(&buf
->consumed
);
747 while ((long) consumed
- (long) consumed_new
< 0)
748 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
753 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
755 * @consumed: consumed count indicating the position where to read
757 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
758 * data to read at consumed position, or 0 if the get operation succeeds.
760 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
761 unsigned long consumed
,
762 struct lttng_ust_shm_handle
*handle
)
764 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
765 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
766 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
771 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
773 * Read finalized before counters.
776 consumed_cur
= uatomic_read(&buf
->consumed
);
777 consumed_idx
= subbuf_index(consumed
, chan
);
778 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
780 * Make sure we read the commit count before reading the buffer
781 * data and the write offset. Correct consumed offset ordering
782 * wrt commit count is insured by the use of cmpxchg to update
783 * the consumed offset.
786 * Local rmb to match the remote wmb to read the commit count
787 * before the buffer data and the write offset.
791 write_offset
= v_read(config
, &buf
->offset
);
794 * Check that the buffer we are getting is after or at consumed_cur
797 if ((long) subbuf_trunc(consumed
, chan
)
798 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
802 * Check that the subbuffer we are trying to consume has been
803 * already fully committed.
805 if (((commit_count
- chan
->backend
.subbuf_size
)
806 & chan
->commit_count_mask
)
807 - (buf_trunc(consumed_cur
, chan
)
808 >> chan
->backend
.num_subbuf_order
)
813 * Check that we are not about to read the same subbuffer in
814 * which the writer head is.
816 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
821 * Failure to get the subbuffer causes a busy-loop retry without going
822 * to a wait queue. These are caused by short-lived race windows where
823 * the writer is getting access to a subbuffer we were trying to get
824 * access to. Also checks that the "consumed" buffer count we are
825 * looking for matches the one contained in the subbuffer id.
827 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
828 consumed_idx
, buf_trunc_val(consumed
, chan
),
832 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
834 buf
->get_subbuf_consumed
= consumed
;
841 * The memory barriers __wait_event()/wake_up_interruptible() take care
842 * of "raw_spin_is_locked" memory ordering.
851 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
854 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
855 struct lttng_ust_shm_handle
*handle
)
857 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
858 struct channel
*chan
= shmp(handle
, bufb
->chan
);
859 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
860 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
862 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
863 && uatomic_read(&buf
->active_shadow_readers
) != 1);
865 if (!buf
->get_subbuf
) {
867 * Reader puts a subbuffer it did not get.
869 CHAN_WARN_ON(chan
, 1);
872 consumed
= buf
->get_subbuf_consumed
;
876 * Clear the records_unread counter. (overruns counter)
877 * Can still be non-zero if a file reader simply grabbed the data
878 * without using iterators.
879 * Can be below zero if an iterator is used on a snapshot more than
882 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
883 v_add(config
, v_read(config
,
884 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
885 &bufb
->records_read
);
886 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
887 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
888 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
889 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
892 * Exchange the reader subbuffer with the one we put in its place in the
893 * writer subbuffer table. Expect the original consumed count. If
894 * update_read_sb_index fails, this is because the writer updated the
895 * subbuffer concurrently. We should therefore keep the subbuffer we
896 * currently have: it has become invalid to try reading this sub-buffer
897 * consumed count value anyway.
899 consumed_idx
= subbuf_index(consumed
, chan
);
900 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
901 consumed_idx
, buf_trunc_val(consumed
, chan
),
904 * update_read_sb_index return value ignored. Don't exchange sub-buffer
905 * if the writer concurrently updated it.
910 * cons_offset is an iterator on all subbuffer offsets between the reader
911 * position and the writer position. (inclusive)
914 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
915 struct channel
*chan
,
916 unsigned long cons_offset
,
918 struct lttng_ust_shm_handle
*handle
)
920 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
921 unsigned long cons_idx
, commit_count
, commit_count_sb
;
923 cons_idx
= subbuf_index(cons_offset
, chan
);
924 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
925 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
927 if (subbuf_offset(commit_count
, chan
) != 0)
928 DBG("ring buffer %s, cpu %d: "
929 "commit count in subbuffer %lu,\n"
930 "expecting multiples of %lu bytes\n"
931 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
932 chan
->backend
.name
, cpu
, cons_idx
,
933 chan
->backend
.subbuf_size
,
934 commit_count
, commit_count_sb
);
936 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
937 chan
->backend
.name
, cpu
, commit_count
);
941 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
942 struct channel
*chan
,
944 struct lttng_ust_shm_handle
*handle
)
946 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
947 unsigned long write_offset
, cons_offset
;
950 * Can be called in the error path of allocation when
951 * trans_channel_data is not yet set.
956 * No need to order commit_count, write_offset and cons_offset reads
957 * because we execute at teardown when no more writer nor reader
958 * references are left.
960 write_offset
= v_read(config
, &buf
->offset
);
961 cons_offset
= uatomic_read(&buf
->consumed
);
962 if (write_offset
!= cons_offset
)
963 DBG("ring buffer %s, cpu %d: "
964 "non-consumed data\n"
965 " [ %lu bytes written, %lu bytes read ]\n",
966 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
968 for (cons_offset
= uatomic_read(&buf
->consumed
);
969 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
972 cons_offset
= subbuf_align(cons_offset
, chan
))
973 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
978 void lib_ring_buffer_print_errors(struct channel
*chan
,
979 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
980 struct lttng_ust_shm_handle
*handle
)
982 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
983 void *priv
= channel_get_private(chan
);
985 DBG("ring buffer %s, cpu %d: %lu records written, "
986 "%lu records overrun\n",
987 chan
->backend
.name
, cpu
,
988 v_read(config
, &buf
->records_count
),
989 v_read(config
, &buf
->records_overrun
));
991 if (v_read(config
, &buf
->records_lost_full
)
992 || v_read(config
, &buf
->records_lost_wrap
)
993 || v_read(config
, &buf
->records_lost_big
))
994 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
995 " [ %lu buffer full, %lu nest buffer wrap-around, "
996 "%lu event too big ]\n",
997 chan
->backend
.name
, cpu
,
998 v_read(config
, &buf
->records_lost_full
),
999 v_read(config
, &buf
->records_lost_wrap
),
1000 v_read(config
, &buf
->records_lost_big
));
1002 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1006 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1008 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1011 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1012 struct channel
*chan
,
1013 struct switch_offsets
*offsets
,
1015 struct lttng_ust_shm_handle
*handle
)
1017 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1018 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1019 unsigned long commit_count
;
1021 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1024 * Order all writes to buffer before the commit count update that will
1025 * determine that the subbuffer is full.
1028 v_add(config
, config
->cb
.subbuffer_header_size(),
1029 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1030 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1031 /* Check if the written buffer has to be delivered */
1032 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1033 commit_count
, oldidx
, handle
);
1034 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1035 offsets
->old
, commit_count
,
1036 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 lttng_ust_lib_ring_buffer
*buf
,
1050 struct channel
*chan
,
1051 struct switch_offsets
*offsets
,
1053 struct lttng_ust_shm_handle
*handle
)
1055 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1056 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1057 unsigned long commit_count
, padding_size
, data_size
;
1059 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1060 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1061 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1065 * Order all writes to buffer before the commit count update that will
1066 * determine that the subbuffer is full.
1069 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1070 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1071 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1072 commit_count
, oldidx
, handle
);
1073 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1074 offsets
->old
, commit_count
,
1075 padding_size
, handle
);
1079 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1081 * This code can be executed unordered : writers may already have written to the
1082 * sub-buffer before this code gets executed, caution. The commit makes sure
1083 * that this code is executed before the deliver of this sub-buffer.
1086 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1087 struct channel
*chan
,
1088 struct switch_offsets
*offsets
,
1090 struct lttng_ust_shm_handle
*handle
)
1092 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1093 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1094 unsigned long commit_count
;
1096 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1099 * Order all writes to buffer before the commit count update that will
1100 * determine that the subbuffer is full.
1103 v_add(config
, config
->cb
.subbuffer_header_size(),
1104 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1105 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1106 /* Check if the written buffer has to be delivered */
1107 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1108 commit_count
, beginidx
, handle
);
1109 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1110 offsets
->begin
, commit_count
,
1111 config
->cb
.subbuffer_header_size(),
1116 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1118 * The only remaining threads could be the ones with pending commits. They will
1119 * have to do the deliver themselves.
1122 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1123 struct channel
*chan
,
1124 struct switch_offsets
*offsets
,
1126 struct lttng_ust_shm_handle
*handle
)
1128 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1129 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1130 unsigned long commit_count
, padding_size
, data_size
;
1132 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1133 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1134 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1138 * Order all writes to buffer before the commit count update that will
1139 * determine that the subbuffer is full.
1142 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1143 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1144 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1145 commit_count
, endidx
, handle
);
1146 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1147 offsets
->end
, commit_count
,
1148 padding_size
, handle
);
1154 * !0 if execution must be aborted.
1157 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1158 struct lttng_ust_lib_ring_buffer
*buf
,
1159 struct channel
*chan
,
1160 struct switch_offsets
*offsets
,
1163 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1166 offsets
->begin
= v_read(config
, &buf
->offset
);
1167 offsets
->old
= offsets
->begin
;
1168 offsets
->switch_old_start
= 0;
1169 off
= subbuf_offset(offsets
->begin
, chan
);
1171 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1174 * Ensure we flush the header of an empty subbuffer when doing the
1175 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1176 * total data gathering duration even if there were no records saved
1177 * after the last buffer switch.
1178 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1179 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1180 * subbuffer header as appropriate.
1181 * The next record that reserves space will be responsible for
1182 * populating the following subbuffer header. We choose not to populate
1183 * the next subbuffer header here because we want to be able to use
1184 * SWITCH_ACTIVE for periodical buffer flush, which must
1185 * guarantee that all the buffer content (records and header
1186 * timestamps) are visible to the reader. This is required for
1187 * quiescence guarantees for the fusion merge.
1189 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1190 if (caa_unlikely(off
== 0)) {
1192 * The client does not save any header information.
1193 * Don't switch empty subbuffer on finalize, because it
1194 * is invalid to deliver a completely empty subbuffer.
1196 if (!config
->cb
.subbuffer_header_size())
1199 * Need to write the subbuffer start header on finalize.
1201 offsets
->switch_old_start
= 1;
1203 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1205 return -1; /* we do not have to switch : buffer is empty */
1206 /* Note: old points to the next subbuf at offset 0 */
1207 offsets
->end
= offsets
->begin
;
1212 * Force a sub-buffer switch. This operation is completely reentrant : can be
1213 * called while tracing is active with absolutely no lock held.
1215 * Note, however, that as a v_cmpxchg is used for some atomic
1216 * operations, this function must be called from the CPU which owns the buffer
1217 * for a ACTIVE flush.
1219 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1220 struct lttng_ust_shm_handle
*handle
)
1222 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1223 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1224 struct switch_offsets offsets
;
1225 unsigned long oldidx
;
1231 * Perform retryable operations.
1234 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1236 return; /* Switch not needed */
1237 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1241 * Atomically update last_tsc. This update races against concurrent
1242 * atomic updates, but the race will always cause supplementary full TSC
1243 * records, never the opposite (missing a full TSC record when it would
1246 save_last_tsc(config
, buf
, tsc
);
1249 * Push the reader if necessary
1251 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1253 oldidx
= subbuf_index(offsets
.old
, chan
);
1254 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1257 * May need to populate header start on SWITCH_FLUSH.
1259 if (offsets
.switch_old_start
) {
1260 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1261 offsets
.old
+= config
->cb
.subbuffer_header_size();
1265 * Switch old subbuffer.
1267 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1273 * -ENOSPC if event size is too large for packet.
1274 * -ENOBUFS if there is currently not enough space in buffer for the event.
1275 * -EIO if data cannot be written into the buffer for any other reason.
1278 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1279 struct channel
*chan
,
1280 struct switch_offsets
*offsets
,
1281 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1283 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1284 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1285 unsigned long reserve_commit_diff
;
1287 offsets
->begin
= v_read(config
, &buf
->offset
);
1288 offsets
->old
= offsets
->begin
;
1289 offsets
->switch_new_start
= 0;
1290 offsets
->switch_new_end
= 0;
1291 offsets
->switch_old_end
= 0;
1292 offsets
->pre_header_padding
= 0;
1294 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1295 if ((int64_t) ctx
->tsc
== -EIO
)
1298 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1299 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1301 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1302 offsets
->switch_new_start
= 1; /* For offsets->begin */
1304 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1306 &offsets
->pre_header_padding
,
1309 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1312 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1313 offsets
->size
> chan
->backend
.subbuf_size
)) {
1314 offsets
->switch_old_end
= 1; /* For offsets->old */
1315 offsets
->switch_new_start
= 1; /* For offsets->begin */
1318 if (caa_unlikely(offsets
->switch_new_start
)) {
1319 unsigned long sb_index
;
1322 * We are typically not filling the previous buffer completely.
1324 if (caa_likely(offsets
->switch_old_end
))
1325 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1326 offsets
->begin
= offsets
->begin
1327 + config
->cb
.subbuffer_header_size();
1328 /* Test new buffer integrity */
1329 sb_index
= subbuf_index(offsets
->begin
, chan
);
1330 reserve_commit_diff
=
1331 (buf_trunc(offsets
->begin
, chan
)
1332 >> chan
->backend
.num_subbuf_order
)
1333 - ((unsigned long) v_read(config
,
1334 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1335 & chan
->commit_count_mask
);
1336 if (caa_likely(reserve_commit_diff
== 0)) {
1337 /* Next subbuffer not being written to. */
1338 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1339 subbuf_trunc(offsets
->begin
, chan
)
1340 - subbuf_trunc((unsigned long)
1341 uatomic_read(&buf
->consumed
), chan
)
1342 >= chan
->backend
.buf_size
)) {
1344 * We do not overwrite non consumed buffers
1345 * and we are full : record is lost.
1347 v_inc(config
, &buf
->records_lost_full
);
1351 * Next subbuffer not being written to, and we
1352 * are either in overwrite mode or the buffer is
1353 * not full. It's safe to write in this new
1359 * Next subbuffer reserve offset does not match the
1360 * commit offset. Drop record in producer-consumer and
1361 * overwrite mode. Caused by either a writer OOPS or too
1362 * many nested writes over a reserve/commit pair.
1364 v_inc(config
, &buf
->records_lost_wrap
);
1368 config
->cb
.record_header_size(config
, chan
,
1370 &offsets
->pre_header_padding
,
1373 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1376 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1377 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1379 * Record too big for subbuffers, report error, don't
1380 * complete the sub-buffer switch.
1382 v_inc(config
, &buf
->records_lost_big
);
1386 * We just made a successful buffer switch and the
1387 * record fits in the new subbuffer. Let's write.
1392 * Record fits in the current buffer and we are not on a switch
1393 * boundary. It's safe to write.
1396 offsets
->end
= offsets
->begin
+ offsets
->size
;
1398 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1400 * The offset_end will fall at the very beginning of the next
1403 offsets
->switch_new_end
= 1; /* For offsets->begin */
1409 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1410 * @ctx: ring buffer context.
1412 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1413 * -EIO for other errors, else returns 0.
1414 * It will take care of sub-buffer switching.
1416 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1418 struct channel
*chan
= ctx
->chan
;
1419 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1420 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1421 struct lttng_ust_lib_ring_buffer
*buf
;
1422 struct switch_offsets offsets
;
1425 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1426 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1428 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1434 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1436 if (caa_unlikely(ret
))
1438 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1443 * Atomically update last_tsc. This update races against concurrent
1444 * atomic updates, but the race will always cause supplementary full TSC
1445 * records, never the opposite (missing a full TSC record when it would
1448 save_last_tsc(config
, buf
, ctx
->tsc
);
1451 * Push the reader if necessary
1453 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1456 * Clear noref flag for this subbuffer.
1458 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1459 subbuf_index(offsets
.end
- 1, chan
),
1463 * Switch old subbuffer if needed.
1465 if (caa_unlikely(offsets
.switch_old_end
)) {
1466 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1467 subbuf_index(offsets
.old
- 1, chan
),
1469 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1473 * Populate new subbuffer.
1475 if (caa_unlikely(offsets
.switch_new_start
))
1476 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1478 if (caa_unlikely(offsets
.switch_new_end
))
1479 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1481 ctx
->slot_size
= offsets
.size
;
1482 ctx
->pre_offset
= offsets
.begin
;
1483 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;