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.
42 #include <sys/types.h>
46 #include <urcu/compiler.h>
51 #include <lttng/ringbuffer-config.h>
57 #include "../liblttng-ust/compat.h" /* For ENODATA */
60 #define max(a, b) ((a) > (b) ? (a) : (b))
63 /* Print DBG() messages about events lost only every 1048576 hits */
64 #define DBG_PRINT_NR_LOST (1UL << 20)
67 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
68 * close(2) to close the fd returned by shm_open.
69 * shm_unlink releases the shared memory object name.
70 * ftruncate(2) sets the size of the memory object.
71 * mmap/munmap maps the shared memory obj to a virtual address in the
72 * calling proceess (should be done both in libust and consumer).
73 * See shm_overview(7) for details.
74 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
77 * Since we don't need to access the object using its name, we can
78 * immediately shm_unlink(3) it, and only keep the handle with its file
83 * Internal structure representing offsets to use at a sub-buffer switch.
85 struct switch_offsets
{
86 unsigned long begin
, end
, old
;
87 size_t pre_header_padding
, size
;
88 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
92 __thread
unsigned int lib_ring_buffer_nesting
;
95 * TODO: this is unused. Errors are saved within the ring buffer.
96 * Eventually, allow consumerd to print these errors.
99 void lib_ring_buffer_print_errors(struct channel
*chan
,
100 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
101 struct lttng_ust_shm_handle
*handle
)
102 __attribute__((unused
));
105 * lib_ring_buffer_reset - Reset ring buffer to initial values.
108 * Effectively empty the ring buffer. Should be called when the buffer is not
109 * used for writing. The ring buffer can be opened for reading, but the reader
110 * should not be using the iterator concurrently with reset. The previous
111 * current iterator record is reset.
113 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
114 struct lttng_ust_shm_handle
*handle
)
116 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
117 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
121 * Reset iterator first. It will put the subbuffer if it currently holds
124 v_set(config
, &buf
->offset
, 0);
125 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
126 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
127 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
128 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
130 uatomic_set(&buf
->consumed
, 0);
131 uatomic_set(&buf
->record_disabled
, 0);
132 v_set(config
, &buf
->last_tsc
, 0);
133 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
134 /* Don't reset number of active readers */
135 v_set(config
, &buf
->records_lost_full
, 0);
136 v_set(config
, &buf
->records_lost_wrap
, 0);
137 v_set(config
, &buf
->records_lost_big
, 0);
138 v_set(config
, &buf
->records_count
, 0);
139 v_set(config
, &buf
->records_overrun
, 0);
144 * channel_reset - Reset channel to initial values.
147 * Effectively empty the channel. Should be called when the channel is not used
148 * for writing. The channel can be opened for reading, but the reader should not
149 * be using the iterator concurrently with reset. The previous current iterator
152 void channel_reset(struct channel
*chan
)
155 * Reset iterators first. Will put the subbuffer if held for reading.
157 uatomic_set(&chan
->record_disabled
, 0);
158 /* Don't reset commit_count_mask, still valid */
159 channel_backend_reset(&chan
->backend
);
160 /* Don't reset switch/read timer interval */
161 /* Don't reset notifiers and notifier enable bits */
162 /* Don't reset reader reference count */
166 * Must be called under cpu hotplug protection.
168 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
169 struct channel_backend
*chanb
, int cpu
,
170 struct lttng_ust_shm_handle
*handle
,
171 struct shm_object
*shmobj
)
173 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
174 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
175 void *priv
= channel_get_private(chan
);
176 size_t subbuf_header_size
;
180 /* Test for cpu hotplug */
181 if (buf
->backend
.allocated
)
184 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
185 cpu
, handle
, shmobj
);
189 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
190 set_shmp(buf
->commit_hot
,
192 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
193 if (!shmp(handle
, buf
->commit_hot
)) {
198 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
199 set_shmp(buf
->commit_cold
,
201 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
202 if (!shmp(handle
, buf
->commit_cold
)) {
208 * Write the subbuffer header for first subbuffer so we know the total
209 * duration of data gathering.
211 subbuf_header_size
= config
->cb
.subbuffer_header_size();
212 v_set(config
, &buf
->offset
, subbuf_header_size
);
213 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
214 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
215 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
216 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
218 if (config
->cb
.buffer_create
) {
219 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
223 buf
->backend
.allocated
= 1;
228 /* commit_cold will be freed by shm teardown */
230 /* commit_hot will be freed by shm teardown */
236 static void switch_buffer_timer(unsigned long data
)
238 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
239 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
240 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
243 * Only flush buffers periodically if readers are active.
245 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
246 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
249 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
250 // mod_timer_pinned(&buf->switch_timer,
251 // jiffies + chan->switch_timer_interval);
253 // mod_timer(&buf->switch_timer,
254 // jiffies + chan->switch_timer_interval);
258 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
259 struct lttng_ust_shm_handle
*handle
)
261 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
262 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
264 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
267 //init_timer(&buf->switch_timer);
268 //buf->switch_timer.function = switch_buffer_timer;
269 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
270 //buf->switch_timer.data = (unsigned long)buf;
271 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
272 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
274 // add_timer(&buf->switch_timer);
275 buf
->switch_timer_enabled
= 1;
278 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
279 struct lttng_ust_shm_handle
*handle
)
281 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
283 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
287 //del_timer_sync(&buf->switch_timer);
288 buf
->switch_timer_enabled
= 0;
293 * Polling timer to check the channels for data.
295 static void read_buffer_timer(unsigned long data
)
297 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
298 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
299 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
301 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
303 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
304 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
306 //wake_up_interruptible(&buf->read_wait);
307 //wake_up_interruptible(&chan->read_wait);
311 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
312 // mod_timer_pinned(&buf->read_timer,
313 // jiffies + chan->read_timer_interval);
315 // mod_timer(&buf->read_timer,
316 // jiffies + chan->read_timer_interval);
320 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
321 struct lttng_ust_shm_handle
*handle
)
323 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
324 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
326 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
327 || !chan
->read_timer_interval
328 || buf
->read_timer_enabled
)
332 //init_timer(&buf->read_timer);
333 //buf->read_timer.function = read_buffer_timer;
334 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
335 //buf->read_timer.data = (unsigned long)buf;
337 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
338 // add_timer_on(&buf->read_timer, buf->backend.cpu);
340 // add_timer(&buf->read_timer);
341 buf
->read_timer_enabled
= 1;
344 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
345 struct lttng_ust_shm_handle
*handle
)
347 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
348 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
350 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
351 || !chan
->read_timer_interval
352 || !buf
->read_timer_enabled
)
356 //del_timer_sync(&buf->read_timer);
358 * do one more check to catch data that has been written in the last
361 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
363 //wake_up_interruptible(&buf->read_wait);
364 //wake_up_interruptible(&chan->read_wait);
366 buf
->read_timer_enabled
= 0;
369 static void channel_unregister_notifiers(struct channel
*chan
,
370 struct lttng_ust_shm_handle
*handle
)
372 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
375 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
376 for_each_possible_cpu(cpu
) {
377 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
379 lib_ring_buffer_stop_switch_timer(buf
, handle
);
380 lib_ring_buffer_stop_read_timer(buf
, handle
);
383 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
385 lib_ring_buffer_stop_switch_timer(buf
, handle
);
386 lib_ring_buffer_stop_read_timer(buf
, handle
);
388 //channel_backend_unregister_notifiers(&chan->backend);
391 static void channel_free(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
395 channel_backend_free(&chan
->backend
, handle
);
396 /* chan is freed by shm teardown */
397 shm_object_table_destroy(handle
->table
);
402 * channel_create - Create channel.
403 * @config: ring buffer instance configuration
404 * @name: name of the channel
405 * @priv_data: ring buffer client private data area pointer (output)
406 * @priv_data_size: length, in bytes, of the private data area.
407 * @priv_data_init: initialization data for private data.
408 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
409 * address mapping. It is used only by RING_BUFFER_STATIC
410 * configuration. It can be set to NULL for other backends.
411 * @subbuf_size: subbuffer size
412 * @num_subbuf: number of subbuffers
413 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
414 * padding to let readers get those sub-buffers.
415 * Used for live streaming.
416 * @read_timer_interval: Time interval (in us) to wake up pending readers.
419 * Returns NULL on failure.
421 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
424 size_t priv_data_align
,
425 size_t priv_data_size
,
426 void *priv_data_init
,
427 void *buf_addr
, size_t subbuf_size
,
428 size_t num_subbuf
, unsigned int switch_timer_interval
,
429 unsigned int read_timer_interval
,
430 int **shm_fd
, int **wait_fd
, uint64_t **memory_map_size
)
433 size_t shmsize
, chansize
;
434 struct channel
*chan
;
435 struct lttng_ust_shm_handle
*handle
;
436 struct shm_object
*shmobj
;
439 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
440 read_timer_interval
))
443 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
447 /* Allocate table for channel + per-cpu buffers */
448 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
450 goto error_table_alloc
;
452 /* Calculate the shm allocation layout */
453 shmsize
= sizeof(struct channel
);
454 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
455 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
456 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * num_possible_cpus();
458 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
);
460 shmsize
+= offset_align(shmsize
, priv_data_align
);
461 shmsize
+= priv_data_size
;
463 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
466 /* struct channel is at object 0, offset 0 (hardcoded) */
467 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
468 assert(handle
->chan
._ref
.index
== 0);
469 assert(handle
->chan
._ref
.offset
== 0);
470 chan
= shmp(handle
, handle
->chan
);
474 /* space for private data */
475 if (priv_data_size
) {
476 DECLARE_SHMP(void, priv_data_alloc
);
478 align_shm(shmobj
, priv_data_align
);
479 chan
->priv_data_offset
= shmobj
->allocated_len
;
480 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
481 if (!shmp(handle
, priv_data_alloc
))
483 *priv_data
= channel_get_private(chan
);
484 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
486 chan
->priv_data_offset
= -1;
490 ret
= channel_backend_init(&chan
->backend
, name
, config
,
491 subbuf_size
, num_subbuf
, handle
);
493 goto error_backend_init
;
495 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
497 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
498 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
500 //init_waitqueue_head(&chan->read_wait);
501 //init_waitqueue_head(&chan->hp_wait);
503 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
505 * In case of non-hotplug cpu, if the ring-buffer is allocated
506 * in early initcall, it will not be notified of secondary cpus.
507 * In that off case, we need to allocate for all possible cpus.
509 for_each_possible_cpu(cpu
) {
510 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
511 lib_ring_buffer_start_switch_timer(buf
, handle
);
512 lib_ring_buffer_start_read_timer(buf
, handle
);
515 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
517 lib_ring_buffer_start_switch_timer(buf
, handle
);
518 lib_ring_buffer_start_read_timer(buf
, handle
);
520 ref
= &handle
->chan
._ref
;
521 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
, memory_map_size
);
526 shm_object_table_destroy(handle
->table
);
532 struct lttng_ust_shm_handle
*channel_handle_create(int shm_fd
, int wait_fd
,
533 uint64_t memory_map_size
)
535 struct lttng_ust_shm_handle
*handle
;
536 struct shm_object
*object
;
538 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
542 /* Allocate table for channel + per-cpu buffers */
543 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
545 goto error_table_alloc
;
546 /* Add channel object */
547 object
= shm_object_table_append_shadow(handle
->table
,
548 shm_fd
, wait_fd
, memory_map_size
);
550 goto error_table_object
;
551 /* struct channel is at object 0, offset 0 (hardcoded) */
552 handle
->chan
._ref
.index
= 0;
553 handle
->chan
._ref
.offset
= 0;
557 shm_object_table_destroy(handle
->table
);
563 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
564 int shm_fd
, int wait_fd
, uint64_t memory_map_size
)
566 struct shm_object
*object
;
568 /* Add stream object */
569 object
= shm_object_table_append_shadow(handle
->table
,
570 shm_fd
, wait_fd
, memory_map_size
);
577 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
580 channel_free(chan
, handle
, shadow
);
584 * channel_destroy - Finalize, wait for q.s. and destroy channel.
585 * @chan: channel to destroy
588 * Call "destroy" callback, finalize channels, decrement the channel
589 * reference count. Note that when readers have completed data
590 * consumption of finalized channels, get_subbuf() will return -ENODATA.
591 * They should release their handle at that point.
593 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
597 channel_release(chan
, handle
, shadow
);
601 channel_unregister_notifiers(chan
, handle
);
604 * Note: the consumer takes care of finalizing and switching the
609 * sessiond/consumer are keeping a reference on the shm file
610 * descriptor directly. No need to refcount.
612 channel_release(chan
, handle
, shadow
);
616 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
617 const struct lttng_ust_lib_ring_buffer_config
*config
,
618 struct channel
*chan
, int cpu
,
619 struct lttng_ust_shm_handle
*handle
,
620 int **shm_fd
, int **wait_fd
,
621 uint64_t **memory_map_size
)
625 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
626 ref
= &chan
->backend
.buf
[0].shmp
._ref
;
627 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
629 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
631 if (cpu
>= num_possible_cpus())
633 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
634 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
636 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
640 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
641 struct lttng_ust_shm_handle
*handle
,
645 if (uatomic_cmpxchg(&buf
->active_shadow_readers
, 0, 1) != 0)
650 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
656 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
657 struct lttng_ust_shm_handle
*handle
,
660 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
663 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_shadow_readers
) != 1);
665 uatomic_dec(&buf
->active_shadow_readers
);
668 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
670 uatomic_dec(&buf
->active_readers
);
674 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
676 * @consumed: consumed count indicating the position where to read
677 * @produced: produced count, indicates position when to stop reading
679 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
680 * data to read at consumed position, or 0 if the get operation succeeds.
683 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
684 unsigned long *consumed
, unsigned long *produced
,
685 struct lttng_ust_shm_handle
*handle
)
687 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
688 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
689 unsigned long consumed_cur
, write_offset
;
692 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
694 * Read finalized before counters.
697 consumed_cur
= uatomic_read(&buf
->consumed
);
699 * No need to issue a memory barrier between consumed count read and
700 * write offset read, because consumed count can only change
701 * concurrently in overwrite mode, and we keep a sequence counter
702 * identifier derived from the write offset to check we are getting
703 * the same sub-buffer we are expecting (the sub-buffers are atomically
704 * "tagged" upon writes, tags are checked upon read).
706 write_offset
= v_read(config
, &buf
->offset
);
709 * Check that we are not about to read the same subbuffer in
710 * which the writer head is.
712 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
716 *consumed
= consumed_cur
;
717 *produced
= subbuf_trunc(write_offset
, chan
);
723 * The memory barriers __wait_event()/wake_up_interruptible() take care
724 * of "raw_spin_is_locked" memory ordering.
733 * lib_ring_buffer_put_snapshot - move consumed counter forward
735 * @consumed_new: new consumed count value
737 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
738 unsigned long consumed_new
,
739 struct lttng_ust_shm_handle
*handle
)
741 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
742 struct channel
*chan
= shmp(handle
, bufb
->chan
);
743 unsigned long consumed
;
745 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
746 && uatomic_read(&buf
->active_shadow_readers
) != 1);
749 * Only push the consumed value forward.
750 * If the consumed cmpxchg fails, this is because we have been pushed by
751 * the writer in flight recorder mode.
753 consumed
= uatomic_read(&buf
->consumed
);
754 while ((long) consumed
- (long) consumed_new
< 0)
755 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
760 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
762 * @consumed: consumed count indicating the position where to read
764 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
765 * data to read at consumed position, or 0 if the get operation succeeds.
767 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
768 unsigned long consumed
,
769 struct lttng_ust_shm_handle
*handle
)
771 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
772 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
773 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
778 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
780 * Read finalized before counters.
783 consumed_cur
= uatomic_read(&buf
->consumed
);
784 consumed_idx
= subbuf_index(consumed
, chan
);
785 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
787 * Make sure we read the commit count before reading the buffer
788 * data and the write offset. Correct consumed offset ordering
789 * wrt commit count is insured by the use of cmpxchg to update
790 * the consumed offset.
793 * Local rmb to match the remote wmb to read the commit count
794 * before the buffer data and the write offset.
798 write_offset
= v_read(config
, &buf
->offset
);
801 * Check that the buffer we are getting is after or at consumed_cur
804 if ((long) subbuf_trunc(consumed
, chan
)
805 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
809 * Check that the subbuffer we are trying to consume has been
810 * already fully committed.
812 if (((commit_count
- chan
->backend
.subbuf_size
)
813 & chan
->commit_count_mask
)
814 - (buf_trunc(consumed_cur
, chan
)
815 >> chan
->backend
.num_subbuf_order
)
820 * Check that we are not about to read the same subbuffer in
821 * which the writer head is.
823 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
828 * Failure to get the subbuffer causes a busy-loop retry without going
829 * to a wait queue. These are caused by short-lived race windows where
830 * the writer is getting access to a subbuffer we were trying to get
831 * access to. Also checks that the "consumed" buffer count we are
832 * looking for matches the one contained in the subbuffer id.
834 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
835 consumed_idx
, buf_trunc_val(consumed
, chan
),
839 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
841 buf
->get_subbuf_consumed
= consumed
;
848 * The memory barriers __wait_event()/wake_up_interruptible() take care
849 * of "raw_spin_is_locked" memory ordering.
858 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
861 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
862 struct lttng_ust_shm_handle
*handle
)
864 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
865 struct channel
*chan
= shmp(handle
, bufb
->chan
);
866 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
867 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
869 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
870 && uatomic_read(&buf
->active_shadow_readers
) != 1);
872 if (!buf
->get_subbuf
) {
874 * Reader puts a subbuffer it did not get.
876 CHAN_WARN_ON(chan
, 1);
879 consumed
= buf
->get_subbuf_consumed
;
883 * Clear the records_unread counter. (overruns counter)
884 * Can still be non-zero if a file reader simply grabbed the data
885 * without using iterators.
886 * Can be below zero if an iterator is used on a snapshot more than
889 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
890 v_add(config
, v_read(config
,
891 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
892 &bufb
->records_read
);
893 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
894 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
895 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
896 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
899 * Exchange the reader subbuffer with the one we put in its place in the
900 * writer subbuffer table. Expect the original consumed count. If
901 * update_read_sb_index fails, this is because the writer updated the
902 * subbuffer concurrently. We should therefore keep the subbuffer we
903 * currently have: it has become invalid to try reading this sub-buffer
904 * consumed count value anyway.
906 consumed_idx
= subbuf_index(consumed
, chan
);
907 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
908 consumed_idx
, buf_trunc_val(consumed
, chan
),
911 * update_read_sb_index return value ignored. Don't exchange sub-buffer
912 * if the writer concurrently updated it.
917 * cons_offset is an iterator on all subbuffer offsets between the reader
918 * position and the writer position. (inclusive)
921 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
922 struct channel
*chan
,
923 unsigned long cons_offset
,
925 struct lttng_ust_shm_handle
*handle
)
927 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
928 unsigned long cons_idx
, commit_count
, commit_count_sb
;
930 cons_idx
= subbuf_index(cons_offset
, chan
);
931 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
932 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
934 if (subbuf_offset(commit_count
, chan
) != 0)
935 DBG("ring buffer %s, cpu %d: "
936 "commit count in subbuffer %lu,\n"
937 "expecting multiples of %lu bytes\n"
938 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
939 chan
->backend
.name
, cpu
, cons_idx
,
940 chan
->backend
.subbuf_size
,
941 commit_count
, commit_count_sb
);
943 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
944 chan
->backend
.name
, cpu
, commit_count
);
948 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
949 struct channel
*chan
,
951 struct lttng_ust_shm_handle
*handle
)
953 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
954 unsigned long write_offset
, cons_offset
;
957 * No need to order commit_count, write_offset and cons_offset reads
958 * because we execute at teardown when no more writer nor reader
959 * references are left.
961 write_offset
= v_read(config
, &buf
->offset
);
962 cons_offset
= uatomic_read(&buf
->consumed
);
963 if (write_offset
!= cons_offset
)
964 DBG("ring buffer %s, cpu %d: "
965 "non-consumed data\n"
966 " [ %lu bytes written, %lu bytes read ]\n",
967 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
969 for (cons_offset
= uatomic_read(&buf
->consumed
);
970 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
973 cons_offset
= subbuf_align(cons_offset
, chan
))
974 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
979 void lib_ring_buffer_print_errors(struct channel
*chan
,
980 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
981 struct lttng_ust_shm_handle
*handle
)
983 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
984 void *priv
= channel_get_private(chan
);
986 DBG("ring buffer %s, cpu %d: %lu records written, "
987 "%lu records overrun\n",
988 chan
->backend
.name
, cpu
,
989 v_read(config
, &buf
->records_count
),
990 v_read(config
, &buf
->records_overrun
));
992 if (v_read(config
, &buf
->records_lost_full
)
993 || v_read(config
, &buf
->records_lost_wrap
)
994 || v_read(config
, &buf
->records_lost_big
))
995 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
996 " [ %lu buffer full, %lu nest buffer wrap-around, "
997 "%lu event too big ]\n",
998 chan
->backend
.name
, cpu
,
999 v_read(config
, &buf
->records_lost_full
),
1000 v_read(config
, &buf
->records_lost_wrap
),
1001 v_read(config
, &buf
->records_lost_big
));
1003 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1007 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1009 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1012 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1013 struct channel
*chan
,
1014 struct switch_offsets
*offsets
,
1016 struct lttng_ust_shm_handle
*handle
)
1018 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1019 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1020 unsigned long commit_count
;
1022 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1025 * Order all writes to buffer before the commit count update that will
1026 * determine that the subbuffer is full.
1029 v_add(config
, config
->cb
.subbuffer_header_size(),
1030 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1031 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1032 /* Check if the written buffer has to be delivered */
1033 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1034 commit_count
, oldidx
, handle
);
1035 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1036 offsets
->old
, commit_count
,
1037 config
->cb
.subbuffer_header_size(),
1042 * lib_ring_buffer_switch_old_end: switch old subbuffer
1044 * Note : offset_old should never be 0 here. It is ok, because we never perform
1045 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1046 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1050 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1051 struct channel
*chan
,
1052 struct switch_offsets
*offsets
,
1054 struct lttng_ust_shm_handle
*handle
)
1056 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1057 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1058 unsigned long commit_count
, padding_size
, data_size
;
1060 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1061 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1062 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1066 * Order all writes to buffer before the commit count update that will
1067 * determine that the subbuffer is full.
1070 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1071 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1072 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1073 commit_count
, oldidx
, handle
);
1074 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1075 offsets
->old
, commit_count
,
1076 padding_size
, handle
);
1080 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1082 * This code can be executed unordered : writers may already have written to the
1083 * sub-buffer before this code gets executed, caution. The commit makes sure
1084 * that this code is executed before the deliver of this sub-buffer.
1087 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1088 struct channel
*chan
,
1089 struct switch_offsets
*offsets
,
1091 struct lttng_ust_shm_handle
*handle
)
1093 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1094 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1095 unsigned long commit_count
;
1097 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1100 * Order all writes to buffer before the commit count update that will
1101 * determine that the subbuffer is full.
1104 v_add(config
, config
->cb
.subbuffer_header_size(),
1105 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1106 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1107 /* Check if the written buffer has to be delivered */
1108 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1109 commit_count
, beginidx
, handle
);
1110 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1111 offsets
->begin
, commit_count
,
1112 config
->cb
.subbuffer_header_size(),
1117 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1119 * The only remaining threads could be the ones with pending commits. They will
1120 * have to do the deliver themselves.
1123 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1124 struct channel
*chan
,
1125 struct switch_offsets
*offsets
,
1127 struct lttng_ust_shm_handle
*handle
)
1129 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1130 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1131 unsigned long commit_count
, padding_size
, data_size
;
1133 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1134 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1135 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1139 * Order all writes to buffer before the commit count update that will
1140 * determine that the subbuffer is full.
1143 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1144 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1145 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1146 commit_count
, endidx
, handle
);
1147 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1148 offsets
->end
, commit_count
,
1149 padding_size
, handle
);
1155 * !0 if execution must be aborted.
1158 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1159 struct lttng_ust_lib_ring_buffer
*buf
,
1160 struct channel
*chan
,
1161 struct switch_offsets
*offsets
,
1164 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1167 offsets
->begin
= v_read(config
, &buf
->offset
);
1168 offsets
->old
= offsets
->begin
;
1169 offsets
->switch_old_start
= 0;
1170 off
= subbuf_offset(offsets
->begin
, chan
);
1172 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1175 * Ensure we flush the header of an empty subbuffer when doing the
1176 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1177 * total data gathering duration even if there were no records saved
1178 * after the last buffer switch.
1179 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1180 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1181 * subbuffer header as appropriate.
1182 * The next record that reserves space will be responsible for
1183 * populating the following subbuffer header. We choose not to populate
1184 * the next subbuffer header here because we want to be able to use
1185 * SWITCH_ACTIVE for periodical buffer flush, which must
1186 * guarantee that all the buffer content (records and header
1187 * timestamps) are visible to the reader. This is required for
1188 * quiescence guarantees for the fusion merge.
1190 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1191 if (caa_unlikely(off
== 0)) {
1193 * The client does not save any header information.
1194 * Don't switch empty subbuffer on finalize, because it
1195 * is invalid to deliver a completely empty subbuffer.
1197 if (!config
->cb
.subbuffer_header_size())
1200 * Need to write the subbuffer start header on finalize.
1202 offsets
->switch_old_start
= 1;
1204 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1206 return -1; /* we do not have to switch : buffer is empty */
1207 /* Note: old points to the next subbuf at offset 0 */
1208 offsets
->end
= offsets
->begin
;
1213 * Force a sub-buffer switch. This operation is completely reentrant : can be
1214 * called while tracing is active with absolutely no lock held.
1216 * Note, however, that as a v_cmpxchg is used for some atomic
1217 * operations, this function must be called from the CPU which owns the buffer
1218 * for a ACTIVE flush.
1220 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1221 struct lttng_ust_shm_handle
*handle
)
1223 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1224 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1225 struct switch_offsets offsets
;
1226 unsigned long oldidx
;
1232 * Perform retryable operations.
1235 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1237 return; /* Switch not needed */
1238 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1242 * Atomically update last_tsc. This update races against concurrent
1243 * atomic updates, but the race will always cause supplementary full TSC
1244 * records, never the opposite (missing a full TSC record when it would
1247 save_last_tsc(config
, buf
, tsc
);
1250 * Push the reader if necessary
1252 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1254 oldidx
= subbuf_index(offsets
.old
, chan
);
1255 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1258 * May need to populate header start on SWITCH_FLUSH.
1260 if (offsets
.switch_old_start
) {
1261 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1262 offsets
.old
+= config
->cb
.subbuffer_header_size();
1266 * Switch old subbuffer.
1268 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1274 * -ENOSPC if event size is too large for packet.
1275 * -ENOBUFS if there is currently not enough space in buffer for the event.
1276 * -EIO if data cannot be written into the buffer for any other reason.
1279 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1280 struct channel
*chan
,
1281 struct switch_offsets
*offsets
,
1282 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1284 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1285 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1286 unsigned long reserve_commit_diff
;
1288 offsets
->begin
= v_read(config
, &buf
->offset
);
1289 offsets
->old
= offsets
->begin
;
1290 offsets
->switch_new_start
= 0;
1291 offsets
->switch_new_end
= 0;
1292 offsets
->switch_old_end
= 0;
1293 offsets
->pre_header_padding
= 0;
1295 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1296 if ((int64_t) ctx
->tsc
== -EIO
)
1299 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1300 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1302 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1303 offsets
->switch_new_start
= 1; /* For offsets->begin */
1305 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1307 &offsets
->pre_header_padding
,
1310 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1313 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1314 offsets
->size
> chan
->backend
.subbuf_size
)) {
1315 offsets
->switch_old_end
= 1; /* For offsets->old */
1316 offsets
->switch_new_start
= 1; /* For offsets->begin */
1319 if (caa_unlikely(offsets
->switch_new_start
)) {
1320 unsigned long sb_index
;
1323 * We are typically not filling the previous buffer completely.
1325 if (caa_likely(offsets
->switch_old_end
))
1326 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1327 offsets
->begin
= offsets
->begin
1328 + config
->cb
.subbuffer_header_size();
1329 /* Test new buffer integrity */
1330 sb_index
= subbuf_index(offsets
->begin
, chan
);
1331 reserve_commit_diff
=
1332 (buf_trunc(offsets
->begin
, chan
)
1333 >> chan
->backend
.num_subbuf_order
)
1334 - ((unsigned long) v_read(config
,
1335 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1336 & chan
->commit_count_mask
);
1337 if (caa_likely(reserve_commit_diff
== 0)) {
1338 /* Next subbuffer not being written to. */
1339 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1340 subbuf_trunc(offsets
->begin
, chan
)
1341 - subbuf_trunc((unsigned long)
1342 uatomic_read(&buf
->consumed
), chan
)
1343 >= chan
->backend
.buf_size
)) {
1344 unsigned long nr_lost
;
1347 * We do not overwrite non consumed buffers
1348 * and we are full : record is lost.
1350 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1351 v_inc(config
, &buf
->records_lost_full
);
1352 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1353 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1354 nr_lost
+ 1, chan
->backend
.name
,
1360 * Next subbuffer not being written to, and we
1361 * are either in overwrite mode or the buffer is
1362 * not full. It's safe to write in this new
1367 unsigned long nr_lost
;
1370 * Next subbuffer reserve offset does not match the
1371 * commit offset. Drop record in producer-consumer and
1372 * overwrite mode. Caused by either a writer OOPS or too
1373 * many nested writes over a reserve/commit pair.
1375 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1376 v_inc(config
, &buf
->records_lost_wrap
);
1377 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1378 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1379 nr_lost
+ 1, chan
->backend
.name
,
1385 config
->cb
.record_header_size(config
, chan
,
1387 &offsets
->pre_header_padding
,
1390 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1393 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1394 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1395 unsigned long nr_lost
;
1398 * Record too big for subbuffers, report error, don't
1399 * complete the sub-buffer switch.
1401 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1402 v_inc(config
, &buf
->records_lost_big
);
1403 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1404 DBG("%lu or more records lost in (%s:%d) record size "
1405 " of %zu bytes is too large for buffer\n",
1406 nr_lost
+ 1, chan
->backend
.name
,
1407 buf
->backend
.cpu
, offsets
->size
);
1412 * We just made a successful buffer switch and the
1413 * record fits in the new subbuffer. Let's write.
1418 * Record fits in the current buffer and we are not on a switch
1419 * boundary. It's safe to write.
1422 offsets
->end
= offsets
->begin
+ offsets
->size
;
1424 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1426 * The offset_end will fall at the very beginning of the next
1429 offsets
->switch_new_end
= 1; /* For offsets->begin */
1435 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1436 * @ctx: ring buffer context.
1438 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1439 * -EIO for other errors, else returns 0.
1440 * It will take care of sub-buffer switching.
1442 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1444 struct channel
*chan
= ctx
->chan
;
1445 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1446 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1447 struct lttng_ust_lib_ring_buffer
*buf
;
1448 struct switch_offsets offsets
;
1451 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1452 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1454 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1460 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1462 if (caa_unlikely(ret
))
1464 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1469 * Atomically update last_tsc. This update races against concurrent
1470 * atomic updates, but the race will always cause supplementary full TSC
1471 * records, never the opposite (missing a full TSC record when it would
1474 save_last_tsc(config
, buf
, ctx
->tsc
);
1477 * Push the reader if necessary
1479 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1482 * Clear noref flag for this subbuffer.
1484 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1485 subbuf_index(offsets
.end
- 1, chan
),
1489 * Switch old subbuffer if needed.
1491 if (caa_unlikely(offsets
.switch_old_end
)) {
1492 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1493 subbuf_index(offsets
.old
- 1, chan
),
1495 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1499 * Populate new subbuffer.
1501 if (caa_unlikely(offsets
.switch_new_start
))
1502 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1504 if (caa_unlikely(offsets
.switch_new_end
))
1505 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1507 ctx
->slot_size
= offsets
.size
;
1508 ctx
->pre_offset
= offsets
.begin
;
1509 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1514 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1516 void lttng_fixup_ringbuffer_tls(void)
1518 asm volatile ("" : : "m" (lib_ring_buffer_nesting
));