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
)
96 __attribute__((unused
));
99 * lib_ring_buffer_reset - Reset ring buffer to initial values.
102 * Effectively empty the ring buffer. Should be called when the buffer is not
103 * used for writing. The ring buffer can be opened for reading, but the reader
104 * should not be using the iterator concurrently with reset. The previous
105 * current iterator record is reset.
107 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
108 struct lttng_ust_shm_handle
*handle
)
110 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
111 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
115 * Reset iterator first. It will put the subbuffer if it currently holds
118 v_set(config
, &buf
->offset
, 0);
119 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
120 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
121 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
122 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
124 uatomic_set(&buf
->consumed
, 0);
125 uatomic_set(&buf
->record_disabled
, 0);
126 v_set(config
, &buf
->last_tsc
, 0);
127 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
128 /* Don't reset number of active readers */
129 v_set(config
, &buf
->records_lost_full
, 0);
130 v_set(config
, &buf
->records_lost_wrap
, 0);
131 v_set(config
, &buf
->records_lost_big
, 0);
132 v_set(config
, &buf
->records_count
, 0);
133 v_set(config
, &buf
->records_overrun
, 0);
138 * channel_reset - Reset channel to initial values.
141 * Effectively empty the channel. Should be called when the channel is not used
142 * for writing. The channel can be opened for reading, but the reader should not
143 * be using the iterator concurrently with reset. The previous current iterator
146 void channel_reset(struct channel
*chan
)
149 * Reset iterators first. Will put the subbuffer if held for reading.
151 uatomic_set(&chan
->record_disabled
, 0);
152 /* Don't reset commit_count_mask, still valid */
153 channel_backend_reset(&chan
->backend
);
154 /* Don't reset switch/read timer interval */
155 /* Don't reset notifiers and notifier enable bits */
156 /* Don't reset reader reference count */
160 * Must be called under cpu hotplug protection.
162 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
163 struct channel_backend
*chanb
, int cpu
,
164 struct lttng_ust_shm_handle
*handle
,
165 struct shm_object
*shmobj
)
167 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
168 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
169 void *priv
= channel_get_private(chan
);
170 size_t subbuf_header_size
;
174 /* Test for cpu hotplug */
175 if (buf
->backend
.allocated
)
178 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
179 cpu
, handle
, shmobj
);
183 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
184 set_shmp(buf
->commit_hot
,
186 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
187 if (!shmp(handle
, buf
->commit_hot
)) {
192 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
193 set_shmp(buf
->commit_cold
,
195 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
196 if (!shmp(handle
, buf
->commit_cold
)) {
202 * Write the subbuffer header for first subbuffer so we know the total
203 * duration of data gathering.
205 subbuf_header_size
= config
->cb
.subbuffer_header_size();
206 v_set(config
, &buf
->offset
, subbuf_header_size
);
207 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
208 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
209 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
210 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
212 if (config
->cb
.buffer_create
) {
213 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
217 buf
->backend
.allocated
= 1;
222 /* commit_cold will be freed by shm teardown */
224 /* commit_hot will be freed by shm teardown */
230 static void switch_buffer_timer(unsigned long data
)
232 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
233 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
234 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
237 * Only flush buffers periodically if readers are active.
239 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
240 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
243 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
244 // mod_timer_pinned(&buf->switch_timer,
245 // jiffies + chan->switch_timer_interval);
247 // mod_timer(&buf->switch_timer,
248 // jiffies + chan->switch_timer_interval);
252 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
253 struct lttng_ust_shm_handle
*handle
)
255 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
256 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
258 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
261 //init_timer(&buf->switch_timer);
262 //buf->switch_timer.function = switch_buffer_timer;
263 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
264 //buf->switch_timer.data = (unsigned long)buf;
265 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
266 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
268 // add_timer(&buf->switch_timer);
269 buf
->switch_timer_enabled
= 1;
272 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
273 struct lttng_ust_shm_handle
*handle
)
275 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
277 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
281 //del_timer_sync(&buf->switch_timer);
282 buf
->switch_timer_enabled
= 0;
287 * Polling timer to check the channels for data.
289 static void read_buffer_timer(unsigned long data
)
291 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
292 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
293 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
295 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
297 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
298 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
300 //wake_up_interruptible(&buf->read_wait);
301 //wake_up_interruptible(&chan->read_wait);
305 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
306 // mod_timer_pinned(&buf->read_timer,
307 // jiffies + chan->read_timer_interval);
309 // mod_timer(&buf->read_timer,
310 // jiffies + chan->read_timer_interval);
314 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
315 struct lttng_ust_shm_handle
*handle
)
317 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
318 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
320 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
321 || !chan
->read_timer_interval
322 || buf
->read_timer_enabled
)
326 //init_timer(&buf->read_timer);
327 //buf->read_timer.function = read_buffer_timer;
328 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
329 //buf->read_timer.data = (unsigned long)buf;
331 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
332 // add_timer_on(&buf->read_timer, buf->backend.cpu);
334 // add_timer(&buf->read_timer);
335 buf
->read_timer_enabled
= 1;
338 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
339 struct lttng_ust_shm_handle
*handle
)
341 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
342 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
344 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
345 || !chan
->read_timer_interval
346 || !buf
->read_timer_enabled
)
350 //del_timer_sync(&buf->read_timer);
352 * do one more check to catch data that has been written in the last
355 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
357 //wake_up_interruptible(&buf->read_wait);
358 //wake_up_interruptible(&chan->read_wait);
360 buf
->read_timer_enabled
= 0;
363 static void channel_unregister_notifiers(struct channel
*chan
,
364 struct lttng_ust_shm_handle
*handle
)
366 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
369 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
370 for_each_possible_cpu(cpu
) {
371 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
373 lib_ring_buffer_stop_switch_timer(buf
, handle
);
374 lib_ring_buffer_stop_read_timer(buf
, handle
);
377 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
379 lib_ring_buffer_stop_switch_timer(buf
, handle
);
380 lib_ring_buffer_stop_read_timer(buf
, handle
);
382 //channel_backend_unregister_notifiers(&chan->backend);
385 static void channel_free(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
389 channel_backend_free(&chan
->backend
, handle
);
390 /* chan is freed by shm teardown */
391 shm_object_table_destroy(handle
->table
);
396 * channel_create - Create channel.
397 * @config: ring buffer instance configuration
398 * @name: name of the channel
399 * @priv_data: ring buffer client private data area pointer (output)
400 * @priv_data_size: length, in bytes, of the private data area.
401 * @priv_data_init: initialization data for private data.
402 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
403 * address mapping. It is used only by RING_BUFFER_STATIC
404 * configuration. It can be set to NULL for other backends.
405 * @subbuf_size: subbuffer size
406 * @num_subbuf: number of subbuffers
407 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
408 * padding to let readers get those sub-buffers.
409 * Used for live streaming.
410 * @read_timer_interval: Time interval (in us) to wake up pending readers.
413 * Returns NULL on failure.
415 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
418 size_t priv_data_align
,
419 size_t priv_data_size
,
420 void *priv_data_init
,
421 void *buf_addr
, size_t subbuf_size
,
422 size_t num_subbuf
, unsigned int switch_timer_interval
,
423 unsigned int read_timer_interval
,
424 int **shm_fd
, int **wait_fd
, uint64_t **memory_map_size
)
427 size_t shmsize
, chansize
;
428 struct channel
*chan
;
429 struct lttng_ust_shm_handle
*handle
;
430 struct shm_object
*shmobj
;
433 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
434 read_timer_interval
))
437 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
441 /* Allocate table for channel + per-cpu buffers */
442 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
444 goto error_table_alloc
;
446 /* Calculate the shm allocation layout */
447 shmsize
= sizeof(struct channel
);
448 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
449 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
450 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * num_possible_cpus();
452 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
);
454 shmsize
+= offset_align(shmsize
, priv_data_align
);
455 shmsize
+= priv_data_size
;
457 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
460 /* struct channel is at object 0, offset 0 (hardcoded) */
461 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
462 assert(handle
->chan
._ref
.index
== 0);
463 assert(handle
->chan
._ref
.offset
== 0);
464 chan
= shmp(handle
, handle
->chan
);
468 /* space for private data */
469 if (priv_data_size
) {
470 DECLARE_SHMP(void, priv_data_alloc
);
472 align_shm(shmobj
, priv_data_align
);
473 chan
->priv_data_offset
= shmobj
->allocated_len
;
474 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
475 if (!shmp(handle
, priv_data_alloc
))
477 *priv_data
= channel_get_private(chan
);
478 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
480 chan
->priv_data_offset
= -1;
484 ret
= channel_backend_init(&chan
->backend
, name
, config
,
485 subbuf_size
, num_subbuf
, handle
);
487 goto error_backend_init
;
489 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
491 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
492 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
494 //init_waitqueue_head(&chan->read_wait);
495 //init_waitqueue_head(&chan->hp_wait);
497 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
499 * In case of non-hotplug cpu, if the ring-buffer is allocated
500 * in early initcall, it will not be notified of secondary cpus.
501 * In that off case, we need to allocate for all possible cpus.
503 for_each_possible_cpu(cpu
) {
504 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
505 lib_ring_buffer_start_switch_timer(buf
, handle
);
506 lib_ring_buffer_start_read_timer(buf
, handle
);
509 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
511 lib_ring_buffer_start_switch_timer(buf
, handle
);
512 lib_ring_buffer_start_read_timer(buf
, handle
);
514 ref
= &handle
->chan
._ref
;
515 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
, memory_map_size
);
520 shm_object_table_destroy(handle
->table
);
526 struct lttng_ust_shm_handle
*channel_handle_create(int shm_fd
, int wait_fd
,
527 uint64_t memory_map_size
)
529 struct lttng_ust_shm_handle
*handle
;
530 struct shm_object
*object
;
532 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
536 /* Allocate table for channel + per-cpu buffers */
537 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
539 goto error_table_alloc
;
540 /* Add channel object */
541 object
= shm_object_table_append_shadow(handle
->table
,
542 shm_fd
, wait_fd
, memory_map_size
);
544 goto error_table_object
;
545 /* struct channel is at object 0, offset 0 (hardcoded) */
546 handle
->chan
._ref
.index
= 0;
547 handle
->chan
._ref
.offset
= 0;
551 shm_object_table_destroy(handle
->table
);
557 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
558 int shm_fd
, int wait_fd
, uint64_t memory_map_size
)
560 struct shm_object
*object
;
562 /* Add stream object */
563 object
= shm_object_table_append_shadow(handle
->table
,
564 shm_fd
, wait_fd
, memory_map_size
);
571 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
574 channel_free(chan
, handle
, shadow
);
578 * channel_destroy - Finalize, wait for q.s. and destroy channel.
579 * @chan: channel to destroy
582 * Call "destroy" callback, finalize channels, decrement the channel
583 * reference count. Note that when readers have completed data
584 * consumption of finalized channels, get_subbuf() will return -ENODATA.
585 * They should release their handle at that point.
587 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
591 channel_release(chan
, handle
, shadow
);
595 channel_unregister_notifiers(chan
, handle
);
598 * Note: the consumer takes care of finalizing and switching the
603 * sessiond/consumer are keeping a reference on the shm file
604 * descriptor directly. No need to refcount.
606 channel_release(chan
, handle
, shadow
);
610 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
611 const struct lttng_ust_lib_ring_buffer_config
*config
,
612 struct channel
*chan
, int cpu
,
613 struct lttng_ust_shm_handle
*handle
,
614 int **shm_fd
, int **wait_fd
,
615 uint64_t **memory_map_size
)
619 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
620 ref
= &chan
->backend
.buf
[0].shmp
._ref
;
621 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
623 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
625 if (cpu
>= num_possible_cpus())
627 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
628 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
630 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
634 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
635 struct lttng_ust_shm_handle
*handle
,
639 if (uatomic_cmpxchg(&buf
->active_shadow_readers
, 0, 1) != 0)
644 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
650 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
651 struct lttng_ust_shm_handle
*handle
,
654 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
657 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_shadow_readers
) != 1);
659 uatomic_dec(&buf
->active_shadow_readers
);
662 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
664 uatomic_dec(&buf
->active_readers
);
668 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
670 * @consumed: consumed count indicating the position where to read
671 * @produced: produced count, indicates position when to stop reading
673 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
674 * data to read at consumed position, or 0 if the get operation succeeds.
677 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
678 unsigned long *consumed
, unsigned long *produced
,
679 struct lttng_ust_shm_handle
*handle
)
681 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
682 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
683 unsigned long consumed_cur
, write_offset
;
686 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
688 * Read finalized before counters.
691 consumed_cur
= uatomic_read(&buf
->consumed
);
693 * No need to issue a memory barrier between consumed count read and
694 * write offset read, because consumed count can only change
695 * concurrently in overwrite mode, and we keep a sequence counter
696 * identifier derived from the write offset to check we are getting
697 * the same sub-buffer we are expecting (the sub-buffers are atomically
698 * "tagged" upon writes, tags are checked upon read).
700 write_offset
= v_read(config
, &buf
->offset
);
703 * Check that we are not about to read the same subbuffer in
704 * which the writer head is.
706 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
710 *consumed
= consumed_cur
;
711 *produced
= subbuf_trunc(write_offset
, chan
);
717 * The memory barriers __wait_event()/wake_up_interruptible() take care
718 * of "raw_spin_is_locked" memory ordering.
727 * lib_ring_buffer_put_snapshot - move consumed counter forward
729 * @consumed_new: new consumed count value
731 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
732 unsigned long consumed_new
,
733 struct lttng_ust_shm_handle
*handle
)
735 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
736 struct channel
*chan
= shmp(handle
, bufb
->chan
);
737 unsigned long consumed
;
739 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
740 && uatomic_read(&buf
->active_shadow_readers
) != 1);
743 * Only push the consumed value forward.
744 * If the consumed cmpxchg fails, this is because we have been pushed by
745 * the writer in flight recorder mode.
747 consumed
= uatomic_read(&buf
->consumed
);
748 while ((long) consumed
- (long) consumed_new
< 0)
749 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
754 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
756 * @consumed: consumed count indicating the position where to read
758 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
759 * data to read at consumed position, or 0 if the get operation succeeds.
761 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
762 unsigned long consumed
,
763 struct lttng_ust_shm_handle
*handle
)
765 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
766 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
767 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
772 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
774 * Read finalized before counters.
777 consumed_cur
= uatomic_read(&buf
->consumed
);
778 consumed_idx
= subbuf_index(consumed
, chan
);
779 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
781 * Make sure we read the commit count before reading the buffer
782 * data and the write offset. Correct consumed offset ordering
783 * wrt commit count is insured by the use of cmpxchg to update
784 * the consumed offset.
787 * Local rmb to match the remote wmb to read the commit count
788 * before the buffer data and the write offset.
792 write_offset
= v_read(config
, &buf
->offset
);
795 * Check that the buffer we are getting is after or at consumed_cur
798 if ((long) subbuf_trunc(consumed
, chan
)
799 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
803 * Check that the subbuffer we are trying to consume has been
804 * already fully committed.
806 if (((commit_count
- chan
->backend
.subbuf_size
)
807 & chan
->commit_count_mask
)
808 - (buf_trunc(consumed_cur
, chan
)
809 >> chan
->backend
.num_subbuf_order
)
814 * Check that we are not about to read the same subbuffer in
815 * which the writer head is.
817 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
822 * Failure to get the subbuffer causes a busy-loop retry without going
823 * to a wait queue. These are caused by short-lived race windows where
824 * the writer is getting access to a subbuffer we were trying to get
825 * access to. Also checks that the "consumed" buffer count we are
826 * looking for matches the one contained in the subbuffer id.
828 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
829 consumed_idx
, buf_trunc_val(consumed
, chan
),
833 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
835 buf
->get_subbuf_consumed
= consumed
;
842 * The memory barriers __wait_event()/wake_up_interruptible() take care
843 * of "raw_spin_is_locked" memory ordering.
852 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
855 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
856 struct lttng_ust_shm_handle
*handle
)
858 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
859 struct channel
*chan
= shmp(handle
, bufb
->chan
);
860 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
861 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
863 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
864 && uatomic_read(&buf
->active_shadow_readers
) != 1);
866 if (!buf
->get_subbuf
) {
868 * Reader puts a subbuffer it did not get.
870 CHAN_WARN_ON(chan
, 1);
873 consumed
= buf
->get_subbuf_consumed
;
877 * Clear the records_unread counter. (overruns counter)
878 * Can still be non-zero if a file reader simply grabbed the data
879 * without using iterators.
880 * Can be below zero if an iterator is used on a snapshot more than
883 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
884 v_add(config
, v_read(config
,
885 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
886 &bufb
->records_read
);
887 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
888 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
889 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
890 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
893 * Exchange the reader subbuffer with the one we put in its place in the
894 * writer subbuffer table. Expect the original consumed count. If
895 * update_read_sb_index fails, this is because the writer updated the
896 * subbuffer concurrently. We should therefore keep the subbuffer we
897 * currently have: it has become invalid to try reading this sub-buffer
898 * consumed count value anyway.
900 consumed_idx
= subbuf_index(consumed
, chan
);
901 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
902 consumed_idx
, buf_trunc_val(consumed
, chan
),
905 * update_read_sb_index return value ignored. Don't exchange sub-buffer
906 * if the writer concurrently updated it.
911 * cons_offset is an iterator on all subbuffer offsets between the reader
912 * position and the writer position. (inclusive)
915 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
916 struct channel
*chan
,
917 unsigned long cons_offset
,
919 struct lttng_ust_shm_handle
*handle
)
921 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
922 unsigned long cons_idx
, commit_count
, commit_count_sb
;
924 cons_idx
= subbuf_index(cons_offset
, chan
);
925 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
926 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
928 if (subbuf_offset(commit_count
, chan
) != 0)
929 DBG("ring buffer %s, cpu %d: "
930 "commit count in subbuffer %lu,\n"
931 "expecting multiples of %lu bytes\n"
932 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
933 chan
->backend
.name
, cpu
, cons_idx
,
934 chan
->backend
.subbuf_size
,
935 commit_count
, commit_count_sb
);
937 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
938 chan
->backend
.name
, cpu
, commit_count
);
942 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
943 struct channel
*chan
,
945 struct lttng_ust_shm_handle
*handle
)
947 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
948 unsigned long write_offset
, cons_offset
;
951 * No need to order commit_count, write_offset and cons_offset reads
952 * because we execute at teardown when no more writer nor reader
953 * references are left.
955 write_offset
= v_read(config
, &buf
->offset
);
956 cons_offset
= uatomic_read(&buf
->consumed
);
957 if (write_offset
!= cons_offset
)
958 DBG("ring buffer %s, cpu %d: "
959 "non-consumed data\n"
960 " [ %lu bytes written, %lu bytes read ]\n",
961 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
963 for (cons_offset
= uatomic_read(&buf
->consumed
);
964 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
967 cons_offset
= subbuf_align(cons_offset
, chan
))
968 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
973 void lib_ring_buffer_print_errors(struct channel
*chan
,
974 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
975 struct lttng_ust_shm_handle
*handle
)
977 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
978 void *priv
= channel_get_private(chan
);
980 DBG("ring buffer %s, cpu %d: %lu records written, "
981 "%lu records overrun\n",
982 chan
->backend
.name
, cpu
,
983 v_read(config
, &buf
->records_count
),
984 v_read(config
, &buf
->records_overrun
));
986 if (v_read(config
, &buf
->records_lost_full
)
987 || v_read(config
, &buf
->records_lost_wrap
)
988 || v_read(config
, &buf
->records_lost_big
))
989 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
990 " [ %lu buffer full, %lu nest buffer wrap-around, "
991 "%lu event too big ]\n",
992 chan
->backend
.name
, cpu
,
993 v_read(config
, &buf
->records_lost_full
),
994 v_read(config
, &buf
->records_lost_wrap
),
995 v_read(config
, &buf
->records_lost_big
));
997 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1001 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1003 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1006 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1007 struct channel
*chan
,
1008 struct switch_offsets
*offsets
,
1010 struct lttng_ust_shm_handle
*handle
)
1012 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1013 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1014 unsigned long commit_count
;
1016 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1019 * Order all writes to buffer before the commit count update that will
1020 * determine that the subbuffer is full.
1023 v_add(config
, config
->cb
.subbuffer_header_size(),
1024 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1025 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1026 /* Check if the written buffer has to be delivered */
1027 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1028 commit_count
, oldidx
, handle
);
1029 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1030 offsets
->old
, commit_count
,
1031 config
->cb
.subbuffer_header_size(),
1036 * lib_ring_buffer_switch_old_end: switch old subbuffer
1038 * Note : offset_old should never be 0 here. It is ok, because we never perform
1039 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1040 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1044 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1045 struct channel
*chan
,
1046 struct switch_offsets
*offsets
,
1048 struct lttng_ust_shm_handle
*handle
)
1050 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1051 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1052 unsigned long commit_count
, padding_size
, data_size
;
1054 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1055 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1056 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1060 * Order all writes to buffer before the commit count update that will
1061 * determine that the subbuffer is full.
1064 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1065 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1066 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1067 commit_count
, oldidx
, handle
);
1068 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1069 offsets
->old
, commit_count
,
1070 padding_size
, handle
);
1074 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1076 * This code can be executed unordered : writers may already have written to the
1077 * sub-buffer before this code gets executed, caution. The commit makes sure
1078 * that this code is executed before the deliver of this sub-buffer.
1081 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1082 struct channel
*chan
,
1083 struct switch_offsets
*offsets
,
1085 struct lttng_ust_shm_handle
*handle
)
1087 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1088 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1089 unsigned long commit_count
;
1091 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1094 * Order all writes to buffer before the commit count update that will
1095 * determine that the subbuffer is full.
1098 v_add(config
, config
->cb
.subbuffer_header_size(),
1099 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1100 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1101 /* Check if the written buffer has to be delivered */
1102 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1103 commit_count
, beginidx
, handle
);
1104 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1105 offsets
->begin
, commit_count
,
1106 config
->cb
.subbuffer_header_size(),
1111 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1113 * The only remaining threads could be the ones with pending commits. They will
1114 * have to do the deliver themselves.
1117 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1118 struct channel
*chan
,
1119 struct switch_offsets
*offsets
,
1121 struct lttng_ust_shm_handle
*handle
)
1123 const struct lttng_ust_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
,
1133 * Order all writes to buffer before the commit count update that will
1134 * determine that the subbuffer is full.
1137 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1138 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1139 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1140 commit_count
, endidx
, handle
);
1141 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1142 offsets
->end
, commit_count
,
1143 padding_size
, handle
);
1149 * !0 if execution must be aborted.
1152 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1153 struct lttng_ust_lib_ring_buffer
*buf
,
1154 struct channel
*chan
,
1155 struct switch_offsets
*offsets
,
1158 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1161 offsets
->begin
= v_read(config
, &buf
->offset
);
1162 offsets
->old
= offsets
->begin
;
1163 offsets
->switch_old_start
= 0;
1164 off
= subbuf_offset(offsets
->begin
, chan
);
1166 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1169 * Ensure we flush the header of an empty subbuffer when doing the
1170 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1171 * total data gathering duration even if there were no records saved
1172 * after the last buffer switch.
1173 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1174 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1175 * subbuffer header as appropriate.
1176 * The next record that reserves space will be responsible for
1177 * populating the following subbuffer header. We choose not to populate
1178 * the next subbuffer header here because we want to be able to use
1179 * SWITCH_ACTIVE for periodical buffer flush, which must
1180 * guarantee that all the buffer content (records and header
1181 * timestamps) are visible to the reader. This is required for
1182 * quiescence guarantees for the fusion merge.
1184 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1185 if (caa_unlikely(off
== 0)) {
1187 * The client does not save any header information.
1188 * Don't switch empty subbuffer on finalize, because it
1189 * is invalid to deliver a completely empty subbuffer.
1191 if (!config
->cb
.subbuffer_header_size())
1194 * Need to write the subbuffer start header on finalize.
1196 offsets
->switch_old_start
= 1;
1198 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1200 return -1; /* we do not have to switch : buffer is empty */
1201 /* Note: old points to the next subbuf at offset 0 */
1202 offsets
->end
= offsets
->begin
;
1207 * Force a sub-buffer switch. This operation is completely reentrant : can be
1208 * called while tracing is active with absolutely no lock held.
1210 * Note, however, that as a v_cmpxchg is used for some atomic
1211 * operations, this function must be called from the CPU which owns the buffer
1212 * for a ACTIVE flush.
1214 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1215 struct lttng_ust_shm_handle
*handle
)
1217 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1218 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1219 struct switch_offsets offsets
;
1220 unsigned long oldidx
;
1226 * Perform retryable operations.
1229 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1231 return; /* Switch not needed */
1232 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1236 * Atomically update last_tsc. This update races against concurrent
1237 * atomic updates, but the race will always cause supplementary full TSC
1238 * records, never the opposite (missing a full TSC record when it would
1241 save_last_tsc(config
, buf
, tsc
);
1244 * Push the reader if necessary
1246 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1248 oldidx
= subbuf_index(offsets
.old
, chan
);
1249 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1252 * May need to populate header start on SWITCH_FLUSH.
1254 if (offsets
.switch_old_start
) {
1255 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1256 offsets
.old
+= config
->cb
.subbuffer_header_size();
1260 * Switch old subbuffer.
1262 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1268 * -ENOSPC if event size is too large for packet.
1269 * -ENOBUFS if there is currently not enough space in buffer for the event.
1270 * -EIO if data cannot be written into the buffer for any other reason.
1273 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1274 struct channel
*chan
,
1275 struct switch_offsets
*offsets
,
1276 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1278 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1279 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1280 unsigned long reserve_commit_diff
;
1282 offsets
->begin
= v_read(config
, &buf
->offset
);
1283 offsets
->old
= offsets
->begin
;
1284 offsets
->switch_new_start
= 0;
1285 offsets
->switch_new_end
= 0;
1286 offsets
->switch_old_end
= 0;
1287 offsets
->pre_header_padding
= 0;
1289 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1290 if ((int64_t) ctx
->tsc
== -EIO
)
1293 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1294 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1296 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1297 offsets
->switch_new_start
= 1; /* For offsets->begin */
1299 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1301 &offsets
->pre_header_padding
,
1304 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1307 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1308 offsets
->size
> chan
->backend
.subbuf_size
)) {
1309 offsets
->switch_old_end
= 1; /* For offsets->old */
1310 offsets
->switch_new_start
= 1; /* For offsets->begin */
1313 if (caa_unlikely(offsets
->switch_new_start
)) {
1314 unsigned long sb_index
;
1317 * We are typically not filling the previous buffer completely.
1319 if (caa_likely(offsets
->switch_old_end
))
1320 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1321 offsets
->begin
= offsets
->begin
1322 + config
->cb
.subbuffer_header_size();
1323 /* Test new buffer integrity */
1324 sb_index
= subbuf_index(offsets
->begin
, chan
);
1325 reserve_commit_diff
=
1326 (buf_trunc(offsets
->begin
, chan
)
1327 >> chan
->backend
.num_subbuf_order
)
1328 - ((unsigned long) v_read(config
,
1329 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1330 & chan
->commit_count_mask
);
1331 if (caa_likely(reserve_commit_diff
== 0)) {
1332 /* Next subbuffer not being written to. */
1333 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1334 subbuf_trunc(offsets
->begin
, chan
)
1335 - subbuf_trunc((unsigned long)
1336 uatomic_read(&buf
->consumed
), chan
)
1337 >= chan
->backend
.buf_size
)) {
1339 * We do not overwrite non consumed buffers
1340 * and we are full : record is lost.
1342 v_inc(config
, &buf
->records_lost_full
);
1346 * Next subbuffer not being written to, and we
1347 * are either in overwrite mode or the buffer is
1348 * not full. It's safe to write in this new
1354 * Next subbuffer reserve offset does not match the
1355 * commit offset. Drop record in producer-consumer and
1356 * overwrite mode. Caused by either a writer OOPS or too
1357 * many nested writes over a reserve/commit pair.
1359 v_inc(config
, &buf
->records_lost_wrap
);
1363 config
->cb
.record_header_size(config
, chan
,
1365 &offsets
->pre_header_padding
,
1368 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1371 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1372 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1374 * Record too big for subbuffers, report error, don't
1375 * complete the sub-buffer switch.
1377 v_inc(config
, &buf
->records_lost_big
);
1381 * We just made a successful buffer switch and the
1382 * record fits in the new subbuffer. Let's write.
1387 * Record fits in the current buffer and we are not on a switch
1388 * boundary. It's safe to write.
1391 offsets
->end
= offsets
->begin
+ offsets
->size
;
1393 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1395 * The offset_end will fall at the very beginning of the next
1398 offsets
->switch_new_end
= 1; /* For offsets->begin */
1404 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1405 * @ctx: ring buffer context.
1407 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1408 * -EIO for other errors, else returns 0.
1409 * It will take care of sub-buffer switching.
1411 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1413 struct channel
*chan
= ctx
->chan
;
1414 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1415 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1416 struct lttng_ust_lib_ring_buffer
*buf
;
1417 struct switch_offsets offsets
;
1420 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1421 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1423 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1429 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1431 if (caa_unlikely(ret
))
1433 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1438 * Atomically update last_tsc. This update races against concurrent
1439 * atomic updates, but the race will always cause supplementary full TSC
1440 * records, never the opposite (missing a full TSC record when it would
1443 save_last_tsc(config
, buf
, ctx
->tsc
);
1446 * Push the reader if necessary
1448 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1451 * Clear noref flag for this subbuffer.
1453 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1454 subbuf_index(offsets
.end
- 1, chan
),
1458 * Switch old subbuffer if needed.
1460 if (caa_unlikely(offsets
.switch_old_end
)) {
1461 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1462 subbuf_index(offsets
.old
- 1, chan
),
1464 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1468 * Populate new subbuffer.
1470 if (caa_unlikely(offsets
.switch_new_start
))
1471 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1473 if (caa_unlikely(offsets
.switch_new_end
))
1474 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1476 ctx
->slot_size
= offsets
.size
;
1477 ctx
->pre_offset
= offsets
.begin
;
1478 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;