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>
49 #include <lttng/ringbuffer-config.h>
55 #define max(a, b) ((a) > (b) ? (a) : (b))
59 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
60 * close(2) to close the fd returned by shm_open.
61 * shm_unlink releases the shared memory object name.
62 * ftruncate(2) sets the size of the memory object.
63 * mmap/munmap maps the shared memory obj to a virtual address in the
64 * calling proceess (should be done both in libust and consumer).
65 * See shm_overview(7) for details.
66 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
69 * Since we don't need to access the object using its name, we can
70 * immediately shm_unlink(3) it, and only keep the handle with its file
75 * Internal structure representing offsets to use at a sub-buffer switch.
77 struct switch_offsets
{
78 unsigned long begin
, end
, old
;
79 size_t pre_header_padding
, size
;
80 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
84 __thread
unsigned int lib_ring_buffer_nesting
;
87 * TODO: this is unused. Errors are saved within the ring buffer.
88 * Eventually, allow consumerd to print these errors.
91 void lib_ring_buffer_print_errors(struct channel
*chan
,
92 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
93 struct lttng_ust_shm_handle
*handle
);
96 * lib_ring_buffer_reset - Reset ring buffer to initial values.
99 * Effectively empty the ring buffer. Should be called when the buffer is not
100 * used for writing. The ring buffer can be opened for reading, but the reader
101 * should not be using the iterator concurrently with reset. The previous
102 * current iterator record is reset.
104 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
105 struct lttng_ust_shm_handle
*handle
)
107 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
108 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
112 * Reset iterator first. It will put the subbuffer if it currently holds
115 v_set(config
, &buf
->offset
, 0);
116 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
117 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
118 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
119 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
121 uatomic_set(&buf
->consumed
, 0);
122 uatomic_set(&buf
->record_disabled
, 0);
123 v_set(config
, &buf
->last_tsc
, 0);
124 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
125 /* Don't reset number of active readers */
126 v_set(config
, &buf
->records_lost_full
, 0);
127 v_set(config
, &buf
->records_lost_wrap
, 0);
128 v_set(config
, &buf
->records_lost_big
, 0);
129 v_set(config
, &buf
->records_count
, 0);
130 v_set(config
, &buf
->records_overrun
, 0);
135 * channel_reset - Reset channel to initial values.
138 * Effectively empty the channel. Should be called when the channel is not used
139 * for writing. The channel can be opened for reading, but the reader should not
140 * be using the iterator concurrently with reset. The previous current iterator
143 void channel_reset(struct channel
*chan
)
146 * Reset iterators first. Will put the subbuffer if held for reading.
148 uatomic_set(&chan
->record_disabled
, 0);
149 /* Don't reset commit_count_mask, still valid */
150 channel_backend_reset(&chan
->backend
);
151 /* Don't reset switch/read timer interval */
152 /* Don't reset notifiers and notifier enable bits */
153 /* Don't reset reader reference count */
157 * Must be called under cpu hotplug protection.
159 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
160 struct channel_backend
*chanb
, int cpu
,
161 struct lttng_ust_shm_handle
*handle
,
162 struct shm_object
*shmobj
)
164 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
165 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
166 void *priv
= channel_get_private(chan
);
167 size_t subbuf_header_size
;
171 /* Test for cpu hotplug */
172 if (buf
->backend
.allocated
)
175 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
176 cpu
, handle
, shmobj
);
180 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
181 set_shmp(buf
->commit_hot
,
183 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
184 if (!shmp(handle
, buf
->commit_hot
)) {
189 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
190 set_shmp(buf
->commit_cold
,
192 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
193 if (!shmp(handle
, buf
->commit_cold
)) {
199 * Write the subbuffer header for first subbuffer so we know the total
200 * duration of data gathering.
202 subbuf_header_size
= config
->cb
.subbuffer_header_size();
203 v_set(config
, &buf
->offset
, subbuf_header_size
);
204 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
205 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
206 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
207 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
209 if (config
->cb
.buffer_create
) {
210 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
214 buf
->backend
.allocated
= 1;
219 /* commit_cold will be freed by shm teardown */
221 /* commit_hot will be freed by shm teardown */
227 static void switch_buffer_timer(unsigned long data
)
229 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
230 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
231 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
234 * Only flush buffers periodically if readers are active.
236 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
237 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
240 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
241 // mod_timer_pinned(&buf->switch_timer,
242 // jiffies + chan->switch_timer_interval);
244 // mod_timer(&buf->switch_timer,
245 // jiffies + chan->switch_timer_interval);
249 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
250 struct lttng_ust_shm_handle
*handle
)
252 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
253 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
255 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
258 //init_timer(&buf->switch_timer);
259 //buf->switch_timer.function = switch_buffer_timer;
260 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
261 //buf->switch_timer.data = (unsigned long)buf;
262 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
263 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
265 // add_timer(&buf->switch_timer);
266 buf
->switch_timer_enabled
= 1;
269 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
270 struct lttng_ust_shm_handle
*handle
)
272 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
274 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
278 //del_timer_sync(&buf->switch_timer);
279 buf
->switch_timer_enabled
= 0;
284 * Polling timer to check the channels for data.
286 static void read_buffer_timer(unsigned long data
)
288 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
289 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
290 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
292 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
294 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
295 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
297 //wake_up_interruptible(&buf->read_wait);
298 //wake_up_interruptible(&chan->read_wait);
302 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
303 // mod_timer_pinned(&buf->read_timer,
304 // jiffies + chan->read_timer_interval);
306 // mod_timer(&buf->read_timer,
307 // jiffies + chan->read_timer_interval);
311 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
312 struct lttng_ust_shm_handle
*handle
)
314 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
315 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
317 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
318 || !chan
->read_timer_interval
319 || buf
->read_timer_enabled
)
323 //init_timer(&buf->read_timer);
324 //buf->read_timer.function = read_buffer_timer;
325 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
326 //buf->read_timer.data = (unsigned long)buf;
328 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
329 // add_timer_on(&buf->read_timer, buf->backend.cpu);
331 // add_timer(&buf->read_timer);
332 buf
->read_timer_enabled
= 1;
335 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
336 struct lttng_ust_shm_handle
*handle
)
338 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
339 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
341 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
342 || !chan
->read_timer_interval
343 || !buf
->read_timer_enabled
)
347 //del_timer_sync(&buf->read_timer);
349 * do one more check to catch data that has been written in the last
352 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
354 //wake_up_interruptible(&buf->read_wait);
355 //wake_up_interruptible(&chan->read_wait);
357 buf
->read_timer_enabled
= 0;
360 static void channel_unregister_notifiers(struct channel
*chan
,
361 struct lttng_ust_shm_handle
*handle
)
363 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
366 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
367 for_each_possible_cpu(cpu
) {
368 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
370 lib_ring_buffer_stop_switch_timer(buf
, handle
);
371 lib_ring_buffer_stop_read_timer(buf
, handle
);
374 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
376 lib_ring_buffer_stop_switch_timer(buf
, handle
);
377 lib_ring_buffer_stop_read_timer(buf
, handle
);
379 //channel_backend_unregister_notifiers(&chan->backend);
382 static void channel_free(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
386 channel_backend_free(&chan
->backend
, handle
);
387 /* chan is freed by shm teardown */
388 shm_object_table_destroy(handle
->table
);
393 * channel_create - Create channel.
394 * @config: ring buffer instance configuration
395 * @name: name of the channel
396 * @priv_data: ring buffer client private data area pointer (output)
397 * @priv_data_size: length, in bytes, of the private data area.
398 * @priv_data_init: initialization data for private data.
399 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
400 * address mapping. It is used only by RING_BUFFER_STATIC
401 * configuration. It can be set to NULL for other backends.
402 * @subbuf_size: subbuffer size
403 * @num_subbuf: number of subbuffers
404 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
405 * padding to let readers get those sub-buffers.
406 * Used for live streaming.
407 * @read_timer_interval: Time interval (in us) to wake up pending readers.
410 * Returns NULL on failure.
412 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
415 size_t priv_data_align
,
416 size_t priv_data_size
,
417 void *priv_data_init
,
418 void *buf_addr
, size_t subbuf_size
,
419 size_t num_subbuf
, unsigned int switch_timer_interval
,
420 unsigned int read_timer_interval
,
421 int *shm_fd
, int *wait_fd
, uint64_t *memory_map_size
)
424 size_t shmsize
, chansize
;
425 struct channel
*chan
;
426 struct lttng_ust_shm_handle
*handle
;
427 struct shm_object
*shmobj
;
430 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
431 read_timer_interval
))
434 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
438 /* Allocate table for channel + per-cpu buffers */
439 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
441 goto error_table_alloc
;
443 /* Calculate the shm allocation layout */
444 shmsize
= sizeof(struct channel
);
445 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
446 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
447 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * num_possible_cpus();
449 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
);
451 shmsize
+= offset_align(shmsize
, priv_data_align
);
452 shmsize
+= priv_data_size
;
454 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
457 /* struct channel is at object 0, offset 0 (hardcoded) */
458 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
459 assert(handle
->chan
._ref
.index
== 0);
460 assert(handle
->chan
._ref
.offset
== 0);
461 chan
= shmp(handle
, handle
->chan
);
465 /* space for private data */
466 if (priv_data_size
) {
467 DECLARE_SHMP(void, priv_data_alloc
);
469 align_shm(shmobj
, priv_data_align
);
470 chan
->priv_data_offset
= shmobj
->allocated_len
;
471 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
472 if (!shmp(handle
, priv_data_alloc
))
474 *priv_data
= channel_get_private(chan
);
475 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
477 chan
->priv_data_offset
= -1;
481 ret
= channel_backend_init(&chan
->backend
, name
, config
,
482 subbuf_size
, num_subbuf
, handle
);
484 goto error_backend_init
;
486 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
488 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
489 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
491 //init_waitqueue_head(&chan->read_wait);
492 //init_waitqueue_head(&chan->hp_wait);
494 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
496 * In case of non-hotplug cpu, if the ring-buffer is allocated
497 * in early initcall, it will not be notified of secondary cpus.
498 * In that off case, we need to allocate for all possible cpus.
500 for_each_possible_cpu(cpu
) {
501 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
502 lib_ring_buffer_start_switch_timer(buf
, handle
);
503 lib_ring_buffer_start_read_timer(buf
, handle
);
506 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
508 lib_ring_buffer_start_switch_timer(buf
, handle
);
509 lib_ring_buffer_start_read_timer(buf
, handle
);
511 ref
= &handle
->chan
._ref
;
512 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
, memory_map_size
);
517 shm_object_table_destroy(handle
->table
);
523 struct lttng_ust_shm_handle
*channel_handle_create(int shm_fd
, int wait_fd
,
524 uint64_t memory_map_size
)
526 struct lttng_ust_shm_handle
*handle
;
527 struct shm_object
*object
;
529 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
533 /* Allocate table for channel + per-cpu buffers */
534 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
536 goto error_table_alloc
;
537 /* Add channel object */
538 object
= shm_object_table_append_shadow(handle
->table
,
539 shm_fd
, wait_fd
, memory_map_size
);
541 goto error_table_object
;
542 /* struct channel is at object 0, offset 0 (hardcoded) */
543 handle
->chan
._ref
.index
= 0;
544 handle
->chan
._ref
.offset
= 0;
548 shm_object_table_destroy(handle
->table
);
554 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
555 int shm_fd
, int wait_fd
, uint64_t memory_map_size
)
557 struct shm_object
*object
;
559 /* Add stream object */
560 object
= shm_object_table_append_shadow(handle
->table
,
561 shm_fd
, wait_fd
, memory_map_size
);
568 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
571 channel_free(chan
, handle
, shadow
);
575 * channel_destroy - Finalize, wait for q.s. and destroy channel.
576 * @chan: channel to destroy
579 * Call "destroy" callback, finalize channels, decrement the channel
580 * reference count. Note that when readers have completed data
581 * consumption of finalized channels, get_subbuf() will return -ENODATA.
582 * They should release their handle at that point.
584 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
588 channel_release(chan
, handle
, shadow
);
592 channel_unregister_notifiers(chan
, handle
);
595 * Note: the consumer takes care of finalizing and switching the
600 * sessiond/consumer are keeping a reference on the shm file
601 * descriptor directly. No need to refcount.
603 channel_release(chan
, handle
, shadow
);
607 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
608 const struct lttng_ust_lib_ring_buffer_config
*config
,
609 struct channel
*chan
, int cpu
,
610 struct lttng_ust_shm_handle
*handle
,
611 int *shm_fd
, int *wait_fd
,
612 uint64_t *memory_map_size
)
616 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
617 ref
= &chan
->backend
.buf
[0].shmp
._ref
;
618 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
620 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
622 if (cpu
>= num_possible_cpus())
624 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
625 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
627 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
631 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
632 struct lttng_ust_shm_handle
*handle
,
636 if (uatomic_cmpxchg(&buf
->active_shadow_readers
, 0, 1) != 0)
641 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
647 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
648 struct lttng_ust_shm_handle
*handle
,
651 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
654 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_shadow_readers
) != 1);
656 uatomic_dec(&buf
->active_shadow_readers
);
659 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
661 uatomic_dec(&buf
->active_readers
);
665 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
667 * @consumed: consumed count indicating the position where to read
668 * @produced: produced count, indicates position when to stop reading
670 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
671 * data to read at consumed position, or 0 if the get operation succeeds.
674 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
675 unsigned long *consumed
, unsigned long *produced
,
676 struct lttng_ust_shm_handle
*handle
)
678 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
679 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
680 unsigned long consumed_cur
, write_offset
;
683 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
685 * Read finalized before counters.
688 consumed_cur
= uatomic_read(&buf
->consumed
);
690 * No need to issue a memory barrier between consumed count read and
691 * write offset read, because consumed count can only change
692 * concurrently in overwrite mode, and we keep a sequence counter
693 * identifier derived from the write offset to check we are getting
694 * the same sub-buffer we are expecting (the sub-buffers are atomically
695 * "tagged" upon writes, tags are checked upon read).
697 write_offset
= v_read(config
, &buf
->offset
);
700 * Check that we are not about to read the same subbuffer in
701 * which the writer head is.
703 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
707 *consumed
= consumed_cur
;
708 *produced
= subbuf_trunc(write_offset
, chan
);
714 * The memory barriers __wait_event()/wake_up_interruptible() take care
715 * of "raw_spin_is_locked" memory ordering.
724 * lib_ring_buffer_put_snapshot - move consumed counter forward
726 * @consumed_new: new consumed count value
728 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
729 unsigned long consumed_new
,
730 struct lttng_ust_shm_handle
*handle
)
732 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
733 struct channel
*chan
= shmp(handle
, bufb
->chan
);
734 unsigned long consumed
;
736 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
737 && uatomic_read(&buf
->active_shadow_readers
) != 1);
740 * Only push the consumed value forward.
741 * If the consumed cmpxchg fails, this is because we have been pushed by
742 * the writer in flight recorder mode.
744 consumed
= uatomic_read(&buf
->consumed
);
745 while ((long) consumed
- (long) consumed_new
< 0)
746 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
751 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
753 * @consumed: consumed count indicating the position where to read
755 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
756 * data to read at consumed position, or 0 if the get operation succeeds.
758 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
759 unsigned long consumed
,
760 struct lttng_ust_shm_handle
*handle
)
762 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
763 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
764 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
769 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
771 * Read finalized before counters.
774 consumed_cur
= uatomic_read(&buf
->consumed
);
775 consumed_idx
= subbuf_index(consumed
, chan
);
776 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
778 * Make sure we read the commit count before reading the buffer
779 * data and the write offset. Correct consumed offset ordering
780 * wrt commit count is insured by the use of cmpxchg to update
781 * the consumed offset.
784 * Local rmb to match the remote wmb to read the commit count
785 * before the buffer data and the write offset.
789 write_offset
= v_read(config
, &buf
->offset
);
792 * Check that the buffer we are getting is after or at consumed_cur
795 if ((long) subbuf_trunc(consumed
, chan
)
796 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
800 * Check that the subbuffer we are trying to consume has been
801 * already fully committed.
803 if (((commit_count
- chan
->backend
.subbuf_size
)
804 & chan
->commit_count_mask
)
805 - (buf_trunc(consumed_cur
, chan
)
806 >> chan
->backend
.num_subbuf_order
)
811 * Check that we are not about to read the same subbuffer in
812 * which the writer head is.
814 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
819 * Failure to get the subbuffer causes a busy-loop retry without going
820 * to a wait queue. These are caused by short-lived race windows where
821 * the writer is getting access to a subbuffer we were trying to get
822 * access to. Also checks that the "consumed" buffer count we are
823 * looking for matches the one contained in the subbuffer id.
825 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
826 consumed_idx
, buf_trunc_val(consumed
, chan
),
830 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
832 buf
->get_subbuf_consumed
= consumed
;
839 * The memory barriers __wait_event()/wake_up_interruptible() take care
840 * of "raw_spin_is_locked" memory ordering.
849 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
852 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
853 struct lttng_ust_shm_handle
*handle
)
855 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
856 struct channel
*chan
= shmp(handle
, bufb
->chan
);
857 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
858 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
860 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
861 && uatomic_read(&buf
->active_shadow_readers
) != 1);
863 if (!buf
->get_subbuf
) {
865 * Reader puts a subbuffer it did not get.
867 CHAN_WARN_ON(chan
, 1);
870 consumed
= buf
->get_subbuf_consumed
;
874 * Clear the records_unread counter. (overruns counter)
875 * Can still be non-zero if a file reader simply grabbed the data
876 * without using iterators.
877 * Can be below zero if an iterator is used on a snapshot more than
880 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
881 v_add(config
, v_read(config
,
882 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
883 &bufb
->records_read
);
884 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
885 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
886 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
887 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
890 * Exchange the reader subbuffer with the one we put in its place in the
891 * writer subbuffer table. Expect the original consumed count. If
892 * update_read_sb_index fails, this is because the writer updated the
893 * subbuffer concurrently. We should therefore keep the subbuffer we
894 * currently have: it has become invalid to try reading this sub-buffer
895 * consumed count value anyway.
897 consumed_idx
= subbuf_index(consumed
, chan
);
898 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
899 consumed_idx
, buf_trunc_val(consumed
, chan
),
902 * update_read_sb_index return value ignored. Don't exchange sub-buffer
903 * if the writer concurrently updated it.
908 * cons_offset is an iterator on all subbuffer offsets between the reader
909 * position and the writer position. (inclusive)
912 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
913 struct channel
*chan
,
914 unsigned long cons_offset
,
916 struct lttng_ust_shm_handle
*handle
)
918 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
919 unsigned long cons_idx
, commit_count
, commit_count_sb
;
921 cons_idx
= subbuf_index(cons_offset
, chan
);
922 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
923 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
925 if (subbuf_offset(commit_count
, chan
) != 0)
926 DBG("ring buffer %s, cpu %d: "
927 "commit count in subbuffer %lu,\n"
928 "expecting multiples of %lu bytes\n"
929 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
930 chan
->backend
.name
, cpu
, cons_idx
,
931 chan
->backend
.subbuf_size
,
932 commit_count
, commit_count_sb
);
934 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
935 chan
->backend
.name
, cpu
, commit_count
);
939 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
940 struct channel
*chan
,
942 struct lttng_ust_shm_handle
*handle
)
944 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
945 unsigned long write_offset
, cons_offset
;
948 * Can be called in the error path of allocation when
949 * trans_channel_data is not yet set.
954 * No need to order commit_count, write_offset and cons_offset reads
955 * because we execute at teardown when no more writer nor reader
956 * references are left.
958 write_offset
= v_read(config
, &buf
->offset
);
959 cons_offset
= uatomic_read(&buf
->consumed
);
960 if (write_offset
!= cons_offset
)
961 DBG("ring buffer %s, cpu %d: "
962 "non-consumed data\n"
963 " [ %lu bytes written, %lu bytes read ]\n",
964 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
966 for (cons_offset
= uatomic_read(&buf
->consumed
);
967 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
970 cons_offset
= subbuf_align(cons_offset
, chan
))
971 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
976 void lib_ring_buffer_print_errors(struct channel
*chan
,
977 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
978 struct lttng_ust_shm_handle
*handle
)
980 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
981 void *priv
= channel_get_private(chan
);
983 DBG("ring buffer %s, cpu %d: %lu records written, "
984 "%lu records overrun\n",
985 chan
->backend
.name
, cpu
,
986 v_read(config
, &buf
->records_count
),
987 v_read(config
, &buf
->records_overrun
));
989 if (v_read(config
, &buf
->records_lost_full
)
990 || v_read(config
, &buf
->records_lost_wrap
)
991 || v_read(config
, &buf
->records_lost_big
))
992 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
993 " [ %lu buffer full, %lu nest buffer wrap-around, "
994 "%lu event too big ]\n",
995 chan
->backend
.name
, cpu
,
996 v_read(config
, &buf
->records_lost_full
),
997 v_read(config
, &buf
->records_lost_wrap
),
998 v_read(config
, &buf
->records_lost_big
));
1000 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1004 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1006 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1009 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1010 struct channel
*chan
,
1011 struct switch_offsets
*offsets
,
1013 struct lttng_ust_shm_handle
*handle
)
1015 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1016 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1017 unsigned long commit_count
;
1019 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1022 * Order all writes to buffer before the commit count update that will
1023 * determine that the subbuffer is full.
1026 v_add(config
, config
->cb
.subbuffer_header_size(),
1027 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1028 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1029 /* Check if the written buffer has to be delivered */
1030 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1031 commit_count
, oldidx
, handle
);
1032 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1033 offsets
->old
, commit_count
,
1034 config
->cb
.subbuffer_header_size(),
1039 * lib_ring_buffer_switch_old_end: switch old subbuffer
1041 * Note : offset_old should never be 0 here. It is ok, because we never perform
1042 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1043 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1047 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1048 struct channel
*chan
,
1049 struct switch_offsets
*offsets
,
1051 struct lttng_ust_shm_handle
*handle
)
1053 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1054 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1055 unsigned long commit_count
, padding_size
, data_size
;
1057 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1058 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1059 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1063 * Order all writes to buffer before the commit count update that will
1064 * determine that the subbuffer is full.
1067 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1068 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1069 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1070 commit_count
, oldidx
, handle
);
1071 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1072 offsets
->old
, commit_count
,
1073 padding_size
, handle
);
1077 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1079 * This code can be executed unordered : writers may already have written to the
1080 * sub-buffer before this code gets executed, caution. The commit makes sure
1081 * that this code is executed before the deliver of this sub-buffer.
1084 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1085 struct channel
*chan
,
1086 struct switch_offsets
*offsets
,
1088 struct lttng_ust_shm_handle
*handle
)
1090 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1091 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1092 unsigned long commit_count
;
1094 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1097 * Order all writes to buffer before the commit count update that will
1098 * determine that the subbuffer is full.
1101 v_add(config
, config
->cb
.subbuffer_header_size(),
1102 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1103 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1104 /* Check if the written buffer has to be delivered */
1105 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1106 commit_count
, beginidx
, handle
);
1107 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1108 offsets
->begin
, commit_count
,
1109 config
->cb
.subbuffer_header_size(),
1114 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1116 * The only remaining threads could be the ones with pending commits. They will
1117 * have to do the deliver themselves.
1120 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1121 struct channel
*chan
,
1122 struct switch_offsets
*offsets
,
1124 struct lttng_ust_shm_handle
*handle
)
1126 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1127 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1128 unsigned long commit_count
, padding_size
, data_size
;
1130 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1131 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1132 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1136 * Order all writes to buffer before the commit count update that will
1137 * determine that the subbuffer is full.
1140 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1141 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1142 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1143 commit_count
, endidx
, handle
);
1144 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1145 offsets
->end
, commit_count
,
1146 padding_size
, handle
);
1152 * !0 if execution must be aborted.
1155 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1156 struct lttng_ust_lib_ring_buffer
*buf
,
1157 struct channel
*chan
,
1158 struct switch_offsets
*offsets
,
1161 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1164 offsets
->begin
= v_read(config
, &buf
->offset
);
1165 offsets
->old
= offsets
->begin
;
1166 offsets
->switch_old_start
= 0;
1167 off
= subbuf_offset(offsets
->begin
, chan
);
1169 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1172 * Ensure we flush the header of an empty subbuffer when doing the
1173 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1174 * total data gathering duration even if there were no records saved
1175 * after the last buffer switch.
1176 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1177 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1178 * subbuffer header as appropriate.
1179 * The next record that reserves space will be responsible for
1180 * populating the following subbuffer header. We choose not to populate
1181 * the next subbuffer header here because we want to be able to use
1182 * SWITCH_ACTIVE for periodical buffer flush, which must
1183 * guarantee that all the buffer content (records and header
1184 * timestamps) are visible to the reader. This is required for
1185 * quiescence guarantees for the fusion merge.
1187 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1188 if (caa_unlikely(off
== 0)) {
1190 * The client does not save any header information.
1191 * Don't switch empty subbuffer on finalize, because it
1192 * is invalid to deliver a completely empty subbuffer.
1194 if (!config
->cb
.subbuffer_header_size())
1197 * Need to write the subbuffer start header on finalize.
1199 offsets
->switch_old_start
= 1;
1201 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1203 return -1; /* we do not have to switch : buffer is empty */
1204 /* Note: old points to the next subbuf at offset 0 */
1205 offsets
->end
= offsets
->begin
;
1210 * Force a sub-buffer switch. This operation is completely reentrant : can be
1211 * called while tracing is active with absolutely no lock held.
1213 * Note, however, that as a v_cmpxchg is used for some atomic
1214 * operations, this function must be called from the CPU which owns the buffer
1215 * for a ACTIVE flush.
1217 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1218 struct lttng_ust_shm_handle
*handle
)
1220 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1221 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1222 struct switch_offsets offsets
;
1223 unsigned long oldidx
;
1229 * Perform retryable operations.
1232 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1234 return; /* Switch not needed */
1235 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1239 * Atomically update last_tsc. This update races against concurrent
1240 * atomic updates, but the race will always cause supplementary full TSC
1241 * records, never the opposite (missing a full TSC record when it would
1244 save_last_tsc(config
, buf
, tsc
);
1247 * Push the reader if necessary
1249 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1251 oldidx
= subbuf_index(offsets
.old
, chan
);
1252 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1255 * May need to populate header start on SWITCH_FLUSH.
1257 if (offsets
.switch_old_start
) {
1258 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1259 offsets
.old
+= config
->cb
.subbuffer_header_size();
1263 * Switch old subbuffer.
1265 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1271 * -ENOSPC if event size is too large for packet.
1272 * -ENOBUFS if there is currently not enough space in buffer for the event.
1273 * -EIO if data cannot be written into the buffer for any other reason.
1276 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1277 struct channel
*chan
,
1278 struct switch_offsets
*offsets
,
1279 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1281 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1282 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1283 unsigned long reserve_commit_diff
;
1285 offsets
->begin
= v_read(config
, &buf
->offset
);
1286 offsets
->old
= offsets
->begin
;
1287 offsets
->switch_new_start
= 0;
1288 offsets
->switch_new_end
= 0;
1289 offsets
->switch_old_end
= 0;
1290 offsets
->pre_header_padding
= 0;
1292 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1293 if ((int64_t) ctx
->tsc
== -EIO
)
1296 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1297 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1299 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1300 offsets
->switch_new_start
= 1; /* For offsets->begin */
1302 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1304 &offsets
->pre_header_padding
,
1307 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1310 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1311 offsets
->size
> chan
->backend
.subbuf_size
)) {
1312 offsets
->switch_old_end
= 1; /* For offsets->old */
1313 offsets
->switch_new_start
= 1; /* For offsets->begin */
1316 if (caa_unlikely(offsets
->switch_new_start
)) {
1317 unsigned long sb_index
;
1320 * We are typically not filling the previous buffer completely.
1322 if (caa_likely(offsets
->switch_old_end
))
1323 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1324 offsets
->begin
= offsets
->begin
1325 + config
->cb
.subbuffer_header_size();
1326 /* Test new buffer integrity */
1327 sb_index
= subbuf_index(offsets
->begin
, chan
);
1328 reserve_commit_diff
=
1329 (buf_trunc(offsets
->begin
, chan
)
1330 >> chan
->backend
.num_subbuf_order
)
1331 - ((unsigned long) v_read(config
,
1332 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1333 & chan
->commit_count_mask
);
1334 if (caa_likely(reserve_commit_diff
== 0)) {
1335 /* Next subbuffer not being written to. */
1336 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1337 subbuf_trunc(offsets
->begin
, chan
)
1338 - subbuf_trunc((unsigned long)
1339 uatomic_read(&buf
->consumed
), chan
)
1340 >= chan
->backend
.buf_size
)) {
1342 * We do not overwrite non consumed buffers
1343 * and we are full : record is lost.
1345 v_inc(config
, &buf
->records_lost_full
);
1349 * Next subbuffer not being written to, and we
1350 * are either in overwrite mode or the buffer is
1351 * not full. It's safe to write in this new
1357 * Next subbuffer reserve offset does not match the
1358 * commit offset. Drop record in producer-consumer and
1359 * overwrite mode. Caused by either a writer OOPS or too
1360 * many nested writes over a reserve/commit pair.
1362 v_inc(config
, &buf
->records_lost_wrap
);
1366 config
->cb
.record_header_size(config
, chan
,
1368 &offsets
->pre_header_padding
,
1371 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1374 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1375 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1377 * Record too big for subbuffers, report error, don't
1378 * complete the sub-buffer switch.
1380 v_inc(config
, &buf
->records_lost_big
);
1384 * We just made a successful buffer switch and the
1385 * record fits in the new subbuffer. Let's write.
1390 * Record fits in the current buffer and we are not on a switch
1391 * boundary. It's safe to write.
1394 offsets
->end
= offsets
->begin
+ offsets
->size
;
1396 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1398 * The offset_end will fall at the very beginning of the next
1401 offsets
->switch_new_end
= 1; /* For offsets->begin */
1407 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1408 * @ctx: ring buffer context.
1410 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1411 * -EIO for other errors, else returns 0.
1412 * It will take care of sub-buffer switching.
1414 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1416 struct channel
*chan
= ctx
->chan
;
1417 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1418 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1419 struct lttng_ust_lib_ring_buffer
*buf
;
1420 struct switch_offsets offsets
;
1423 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1424 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1426 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1432 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1434 if (caa_unlikely(ret
))
1436 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1441 * Atomically update last_tsc. This update races against concurrent
1442 * atomic updates, but the race will always cause supplementary full TSC
1443 * records, never the opposite (missing a full TSC record when it would
1446 save_last_tsc(config
, buf
, ctx
->tsc
);
1449 * Push the reader if necessary
1451 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1454 * Clear noref flag for this subbuffer.
1456 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1457 subbuf_index(offsets
.end
- 1, chan
),
1461 * Switch old subbuffer if needed.
1463 if (caa_unlikely(offsets
.switch_old_end
)) {
1464 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1465 subbuf_index(offsets
.old
- 1, chan
),
1467 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1471 * Populate new subbuffer.
1473 if (caa_unlikely(offsets
.switch_new_start
))
1474 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1476 if (caa_unlikely(offsets
.switch_new_end
))
1477 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1479 ctx
->slot_size
= offsets
.size
;
1480 ctx
->pre_offset
= offsets
.begin
;
1481 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;