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>
59 #define max(a, b) ((a) > (b) ? (a) : (b))
62 /* Print DBG() messages about events lost only every 1048576 hits */
63 #define DBG_PRINT_NR_LOST (1UL << 20)
66 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
67 * close(2) to close the fd returned by shm_open.
68 * shm_unlink releases the shared memory object name.
69 * ftruncate(2) sets the size of the memory object.
70 * mmap/munmap maps the shared memory obj to a virtual address in the
71 * calling proceess (should be done both in libust and consumer).
72 * See shm_overview(7) for details.
73 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
76 * Since we don't need to access the object using its name, we can
77 * immediately shm_unlink(3) it, and only keep the handle with its file
82 * Internal structure representing offsets to use at a sub-buffer switch.
84 struct switch_offsets
{
85 unsigned long begin
, end
, old
;
86 size_t pre_header_padding
, size
;
87 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
91 __thread
unsigned int lib_ring_buffer_nesting
;
94 * TODO: this is unused. Errors are saved within the ring buffer.
95 * Eventually, allow consumerd to print these errors.
98 void lib_ring_buffer_print_errors(struct channel
*chan
,
99 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
100 struct lttng_ust_shm_handle
*handle
)
101 __attribute__((unused
));
104 * lib_ring_buffer_reset - Reset ring buffer to initial values.
107 * Effectively empty the ring buffer. Should be called when the buffer is not
108 * used for writing. The ring buffer can be opened for reading, but the reader
109 * should not be using the iterator concurrently with reset. The previous
110 * current iterator record is reset.
112 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
113 struct lttng_ust_shm_handle
*handle
)
115 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
116 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
120 * Reset iterator first. It will put the subbuffer if it currently holds
123 v_set(config
, &buf
->offset
, 0);
124 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
125 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
126 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
127 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
129 uatomic_set(&buf
->consumed
, 0);
130 uatomic_set(&buf
->record_disabled
, 0);
131 v_set(config
, &buf
->last_tsc
, 0);
132 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
133 /* Don't reset number of active readers */
134 v_set(config
, &buf
->records_lost_full
, 0);
135 v_set(config
, &buf
->records_lost_wrap
, 0);
136 v_set(config
, &buf
->records_lost_big
, 0);
137 v_set(config
, &buf
->records_count
, 0);
138 v_set(config
, &buf
->records_overrun
, 0);
143 * channel_reset - Reset channel to initial values.
146 * Effectively empty the channel. Should be called when the channel is not used
147 * for writing. The channel can be opened for reading, but the reader should not
148 * be using the iterator concurrently with reset. The previous current iterator
151 void channel_reset(struct channel
*chan
)
154 * Reset iterators first. Will put the subbuffer if held for reading.
156 uatomic_set(&chan
->record_disabled
, 0);
157 /* Don't reset commit_count_mask, still valid */
158 channel_backend_reset(&chan
->backend
);
159 /* Don't reset switch/read timer interval */
160 /* Don't reset notifiers and notifier enable bits */
161 /* Don't reset reader reference count */
165 * Must be called under cpu hotplug protection.
167 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
168 struct channel_backend
*chanb
, int cpu
,
169 struct lttng_ust_shm_handle
*handle
,
170 struct shm_object
*shmobj
)
172 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
173 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
174 void *priv
= channel_get_private(chan
);
175 size_t subbuf_header_size
;
179 /* Test for cpu hotplug */
180 if (buf
->backend
.allocated
)
183 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
184 cpu
, handle
, shmobj
);
188 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
189 set_shmp(buf
->commit_hot
,
191 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
192 if (!shmp(handle
, buf
->commit_hot
)) {
197 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
198 set_shmp(buf
->commit_cold
,
200 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
201 if (!shmp(handle
, buf
->commit_cold
)) {
207 * Write the subbuffer header for first subbuffer so we know the total
208 * duration of data gathering.
210 subbuf_header_size
= config
->cb
.subbuffer_header_size();
211 v_set(config
, &buf
->offset
, subbuf_header_size
);
212 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
213 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
214 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
215 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
217 if (config
->cb
.buffer_create
) {
218 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
222 buf
->backend
.allocated
= 1;
227 /* commit_cold will be freed by shm teardown */
229 /* commit_hot will be freed by shm teardown */
235 static void switch_buffer_timer(unsigned long data
)
237 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
238 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
239 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
242 * Only flush buffers periodically if readers are active.
244 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
245 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
248 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
249 // mod_timer_pinned(&buf->switch_timer,
250 // jiffies + chan->switch_timer_interval);
252 // mod_timer(&buf->switch_timer,
253 // jiffies + chan->switch_timer_interval);
257 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
258 struct lttng_ust_shm_handle
*handle
)
260 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
261 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
263 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
266 //init_timer(&buf->switch_timer);
267 //buf->switch_timer.function = switch_buffer_timer;
268 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
269 //buf->switch_timer.data = (unsigned long)buf;
270 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
271 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
273 // add_timer(&buf->switch_timer);
274 buf
->switch_timer_enabled
= 1;
277 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
278 struct lttng_ust_shm_handle
*handle
)
280 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
282 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
286 //del_timer_sync(&buf->switch_timer);
287 buf
->switch_timer_enabled
= 0;
292 * Polling timer to check the channels for data.
294 static void read_buffer_timer(unsigned long data
)
296 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
297 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
298 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
300 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
302 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
303 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
305 //wake_up_interruptible(&buf->read_wait);
306 //wake_up_interruptible(&chan->read_wait);
310 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
311 // mod_timer_pinned(&buf->read_timer,
312 // jiffies + chan->read_timer_interval);
314 // mod_timer(&buf->read_timer,
315 // jiffies + chan->read_timer_interval);
319 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
320 struct lttng_ust_shm_handle
*handle
)
322 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
323 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
325 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
326 || !chan
->read_timer_interval
327 || buf
->read_timer_enabled
)
331 //init_timer(&buf->read_timer);
332 //buf->read_timer.function = read_buffer_timer;
333 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
334 //buf->read_timer.data = (unsigned long)buf;
336 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
337 // add_timer_on(&buf->read_timer, buf->backend.cpu);
339 // add_timer(&buf->read_timer);
340 buf
->read_timer_enabled
= 1;
343 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
344 struct lttng_ust_shm_handle
*handle
)
346 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
347 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
349 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
350 || !chan
->read_timer_interval
351 || !buf
->read_timer_enabled
)
355 //del_timer_sync(&buf->read_timer);
357 * do one more check to catch data that has been written in the last
360 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
362 //wake_up_interruptible(&buf->read_wait);
363 //wake_up_interruptible(&chan->read_wait);
365 buf
->read_timer_enabled
= 0;
368 static void channel_unregister_notifiers(struct channel
*chan
,
369 struct lttng_ust_shm_handle
*handle
)
371 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
374 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
375 for_each_possible_cpu(cpu
) {
376 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
378 lib_ring_buffer_stop_switch_timer(buf
, handle
);
379 lib_ring_buffer_stop_read_timer(buf
, handle
);
382 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
384 lib_ring_buffer_stop_switch_timer(buf
, handle
);
385 lib_ring_buffer_stop_read_timer(buf
, handle
);
387 //channel_backend_unregister_notifiers(&chan->backend);
390 static void channel_free(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
394 channel_backend_free(&chan
->backend
, handle
);
395 /* chan is freed by shm teardown */
396 shm_object_table_destroy(handle
->table
);
401 * channel_create - Create channel.
402 * @config: ring buffer instance configuration
403 * @name: name of the channel
404 * @priv_data: ring buffer client private data area pointer (output)
405 * @priv_data_size: length, in bytes, of the private data area.
406 * @priv_data_init: initialization data for private data.
407 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
408 * address mapping. It is used only by RING_BUFFER_STATIC
409 * configuration. It can be set to NULL for other backends.
410 * @subbuf_size: subbuffer size
411 * @num_subbuf: number of subbuffers
412 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
413 * padding to let readers get those sub-buffers.
414 * Used for live streaming.
415 * @read_timer_interval: Time interval (in us) to wake up pending readers.
418 * Returns NULL on failure.
420 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
423 size_t priv_data_align
,
424 size_t priv_data_size
,
425 void *priv_data_init
,
426 void *buf_addr
, size_t subbuf_size
,
427 size_t num_subbuf
, unsigned int switch_timer_interval
,
428 unsigned int read_timer_interval
,
429 int **shm_fd
, int **wait_fd
, uint64_t **memory_map_size
)
432 size_t shmsize
, chansize
;
433 struct channel
*chan
;
434 struct lttng_ust_shm_handle
*handle
;
435 struct shm_object
*shmobj
;
438 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
439 read_timer_interval
))
442 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
446 /* Allocate table for channel + per-cpu buffers */
447 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
449 goto error_table_alloc
;
451 /* Calculate the shm allocation layout */
452 shmsize
= sizeof(struct channel
);
453 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
454 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
455 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * num_possible_cpus();
457 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
);
459 shmsize
+= offset_align(shmsize
, priv_data_align
);
460 shmsize
+= priv_data_size
;
462 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
465 /* struct channel is at object 0, offset 0 (hardcoded) */
466 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
467 assert(handle
->chan
._ref
.index
== 0);
468 assert(handle
->chan
._ref
.offset
== 0);
469 chan
= shmp(handle
, handle
->chan
);
473 /* space for private data */
474 if (priv_data_size
) {
475 DECLARE_SHMP(void, priv_data_alloc
);
477 align_shm(shmobj
, priv_data_align
);
478 chan
->priv_data_offset
= shmobj
->allocated_len
;
479 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
480 if (!shmp(handle
, priv_data_alloc
))
482 *priv_data
= channel_get_private(chan
);
483 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
485 chan
->priv_data_offset
= -1;
489 ret
= channel_backend_init(&chan
->backend
, name
, config
,
490 subbuf_size
, num_subbuf
, handle
);
492 goto error_backend_init
;
494 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
496 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
497 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
499 //init_waitqueue_head(&chan->read_wait);
500 //init_waitqueue_head(&chan->hp_wait);
502 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
504 * In case of non-hotplug cpu, if the ring-buffer is allocated
505 * in early initcall, it will not be notified of secondary cpus.
506 * In that off case, we need to allocate for all possible cpus.
508 for_each_possible_cpu(cpu
) {
509 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
510 lib_ring_buffer_start_switch_timer(buf
, handle
);
511 lib_ring_buffer_start_read_timer(buf
, handle
);
514 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
516 lib_ring_buffer_start_switch_timer(buf
, handle
);
517 lib_ring_buffer_start_read_timer(buf
, handle
);
519 ref
= &handle
->chan
._ref
;
520 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
, memory_map_size
);
525 shm_object_table_destroy(handle
->table
);
531 struct lttng_ust_shm_handle
*channel_handle_create(int shm_fd
, int wait_fd
,
532 uint64_t memory_map_size
)
534 struct lttng_ust_shm_handle
*handle
;
535 struct shm_object
*object
;
537 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
541 /* Allocate table for channel + per-cpu buffers */
542 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
544 goto error_table_alloc
;
545 /* Add channel object */
546 object
= shm_object_table_append_shadow(handle
->table
,
547 shm_fd
, wait_fd
, memory_map_size
);
549 goto error_table_object
;
550 /* struct channel is at object 0, offset 0 (hardcoded) */
551 handle
->chan
._ref
.index
= 0;
552 handle
->chan
._ref
.offset
= 0;
556 shm_object_table_destroy(handle
->table
);
562 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
563 int shm_fd
, int wait_fd
, uint64_t memory_map_size
)
565 struct shm_object
*object
;
567 /* Add stream object */
568 object
= shm_object_table_append_shadow(handle
->table
,
569 shm_fd
, wait_fd
, memory_map_size
);
576 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
579 channel_free(chan
, handle
, shadow
);
583 * channel_destroy - Finalize, wait for q.s. and destroy channel.
584 * @chan: channel to destroy
587 * Call "destroy" callback, finalize channels, decrement the channel
588 * reference count. Note that when readers have completed data
589 * consumption of finalized channels, get_subbuf() will return -ENODATA.
590 * They should release their handle at that point.
592 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
596 channel_release(chan
, handle
, shadow
);
600 channel_unregister_notifiers(chan
, handle
);
603 * Note: the consumer takes care of finalizing and switching the
608 * sessiond/consumer are keeping a reference on the shm file
609 * descriptor directly. No need to refcount.
611 channel_release(chan
, handle
, shadow
);
615 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
616 const struct lttng_ust_lib_ring_buffer_config
*config
,
617 struct channel
*chan
, int cpu
,
618 struct lttng_ust_shm_handle
*handle
,
619 int **shm_fd
, int **wait_fd
,
620 uint64_t **memory_map_size
)
624 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
625 ref
= &chan
->backend
.buf
[0].shmp
._ref
;
626 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
628 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
630 if (cpu
>= num_possible_cpus())
632 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
633 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
635 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
639 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
640 struct lttng_ust_shm_handle
*handle
,
644 if (uatomic_cmpxchg(&buf
->active_shadow_readers
, 0, 1) != 0)
649 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
655 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
656 struct lttng_ust_shm_handle
*handle
,
659 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
662 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_shadow_readers
) != 1);
664 uatomic_dec(&buf
->active_shadow_readers
);
667 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
669 uatomic_dec(&buf
->active_readers
);
673 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
675 * @consumed: consumed count indicating the position where to read
676 * @produced: produced count, indicates position when to stop reading
678 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
679 * data to read at consumed position, or 0 if the get operation succeeds.
682 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
683 unsigned long *consumed
, unsigned long *produced
,
684 struct lttng_ust_shm_handle
*handle
)
686 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
687 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
688 unsigned long consumed_cur
, write_offset
;
691 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
693 * Read finalized before counters.
696 consumed_cur
= uatomic_read(&buf
->consumed
);
698 * No need to issue a memory barrier between consumed count read and
699 * write offset read, because consumed count can only change
700 * concurrently in overwrite mode, and we keep a sequence counter
701 * identifier derived from the write offset to check we are getting
702 * the same sub-buffer we are expecting (the sub-buffers are atomically
703 * "tagged" upon writes, tags are checked upon read).
705 write_offset
= v_read(config
, &buf
->offset
);
708 * Check that we are not about to read the same subbuffer in
709 * which the writer head is.
711 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
715 *consumed
= consumed_cur
;
716 *produced
= subbuf_trunc(write_offset
, chan
);
722 * The memory barriers __wait_event()/wake_up_interruptible() take care
723 * of "raw_spin_is_locked" memory ordering.
732 * lib_ring_buffer_put_snapshot - move consumed counter forward
734 * @consumed_new: new consumed count value
736 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
737 unsigned long consumed_new
,
738 struct lttng_ust_shm_handle
*handle
)
740 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
741 struct channel
*chan
= shmp(handle
, bufb
->chan
);
742 unsigned long consumed
;
744 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
745 && uatomic_read(&buf
->active_shadow_readers
) != 1);
748 * Only push the consumed value forward.
749 * If the consumed cmpxchg fails, this is because we have been pushed by
750 * the writer in flight recorder mode.
752 consumed
= uatomic_read(&buf
->consumed
);
753 while ((long) consumed
- (long) consumed_new
< 0)
754 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
759 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
761 * @consumed: consumed count indicating the position where to read
763 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
764 * data to read at consumed position, or 0 if the get operation succeeds.
766 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
767 unsigned long consumed
,
768 struct lttng_ust_shm_handle
*handle
)
770 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
771 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
772 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
777 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
779 * Read finalized before counters.
782 consumed_cur
= uatomic_read(&buf
->consumed
);
783 consumed_idx
= subbuf_index(consumed
, chan
);
784 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
786 * Make sure we read the commit count before reading the buffer
787 * data and the write offset. Correct consumed offset ordering
788 * wrt commit count is insured by the use of cmpxchg to update
789 * the consumed offset.
792 * Local rmb to match the remote wmb to read the commit count
793 * before the buffer data and the write offset.
797 write_offset
= v_read(config
, &buf
->offset
);
800 * Check that the buffer we are getting is after or at consumed_cur
803 if ((long) subbuf_trunc(consumed
, chan
)
804 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
808 * Check that the subbuffer we are trying to consume has been
809 * already fully committed.
811 if (((commit_count
- chan
->backend
.subbuf_size
)
812 & chan
->commit_count_mask
)
813 - (buf_trunc(consumed_cur
, chan
)
814 >> chan
->backend
.num_subbuf_order
)
819 * Check that we are not about to read the same subbuffer in
820 * which the writer head is.
822 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
827 * Failure to get the subbuffer causes a busy-loop retry without going
828 * to a wait queue. These are caused by short-lived race windows where
829 * the writer is getting access to a subbuffer we were trying to get
830 * access to. Also checks that the "consumed" buffer count we are
831 * looking for matches the one contained in the subbuffer id.
833 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
834 consumed_idx
, buf_trunc_val(consumed
, chan
),
838 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
840 buf
->get_subbuf_consumed
= consumed
;
847 * The memory barriers __wait_event()/wake_up_interruptible() take care
848 * of "raw_spin_is_locked" memory ordering.
857 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
860 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
861 struct lttng_ust_shm_handle
*handle
)
863 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
864 struct channel
*chan
= shmp(handle
, bufb
->chan
);
865 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
866 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
868 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
869 && uatomic_read(&buf
->active_shadow_readers
) != 1);
871 if (!buf
->get_subbuf
) {
873 * Reader puts a subbuffer it did not get.
875 CHAN_WARN_ON(chan
, 1);
878 consumed
= buf
->get_subbuf_consumed
;
882 * Clear the records_unread counter. (overruns counter)
883 * Can still be non-zero if a file reader simply grabbed the data
884 * without using iterators.
885 * Can be below zero if an iterator is used on a snapshot more than
888 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
889 v_add(config
, v_read(config
,
890 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
891 &bufb
->records_read
);
892 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
893 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
894 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
895 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
898 * Exchange the reader subbuffer with the one we put in its place in the
899 * writer subbuffer table. Expect the original consumed count. If
900 * update_read_sb_index fails, this is because the writer updated the
901 * subbuffer concurrently. We should therefore keep the subbuffer we
902 * currently have: it has become invalid to try reading this sub-buffer
903 * consumed count value anyway.
905 consumed_idx
= subbuf_index(consumed
, chan
);
906 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
907 consumed_idx
, buf_trunc_val(consumed
, chan
),
910 * update_read_sb_index return value ignored. Don't exchange sub-buffer
911 * if the writer concurrently updated it.
916 * cons_offset is an iterator on all subbuffer offsets between the reader
917 * position and the writer position. (inclusive)
920 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
921 struct channel
*chan
,
922 unsigned long cons_offset
,
924 struct lttng_ust_shm_handle
*handle
)
926 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
927 unsigned long cons_idx
, commit_count
, commit_count_sb
;
929 cons_idx
= subbuf_index(cons_offset
, chan
);
930 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
931 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
933 if (subbuf_offset(commit_count
, chan
) != 0)
934 DBG("ring buffer %s, cpu %d: "
935 "commit count in subbuffer %lu,\n"
936 "expecting multiples of %lu bytes\n"
937 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
938 chan
->backend
.name
, cpu
, cons_idx
,
939 chan
->backend
.subbuf_size
,
940 commit_count
, commit_count_sb
);
942 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
943 chan
->backend
.name
, cpu
, commit_count
);
947 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
948 struct channel
*chan
,
950 struct lttng_ust_shm_handle
*handle
)
952 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
953 unsigned long write_offset
, cons_offset
;
956 * No need to order commit_count, write_offset and cons_offset reads
957 * because we execute at teardown when no more writer nor reader
958 * references are left.
960 write_offset
= v_read(config
, &buf
->offset
);
961 cons_offset
= uatomic_read(&buf
->consumed
);
962 if (write_offset
!= cons_offset
)
963 DBG("ring buffer %s, cpu %d: "
964 "non-consumed data\n"
965 " [ %lu bytes written, %lu bytes read ]\n",
966 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
968 for (cons_offset
= uatomic_read(&buf
->consumed
);
969 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
972 cons_offset
= subbuf_align(cons_offset
, chan
))
973 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
978 void lib_ring_buffer_print_errors(struct channel
*chan
,
979 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
980 struct lttng_ust_shm_handle
*handle
)
982 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
983 void *priv
= channel_get_private(chan
);
985 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
986 DBG("ring buffer %s: %lu records written, "
987 "%lu records overrun\n",
989 v_read(config
, &buf
->records_count
),
990 v_read(config
, &buf
->records_overrun
));
992 DBG("ring buffer %s, cpu %d: %lu records written, "
993 "%lu records overrun\n",
994 chan
->backend
.name
, cpu
,
995 v_read(config
, &buf
->records_count
),
996 v_read(config
, &buf
->records_overrun
));
998 if (v_read(config
, &buf
->records_lost_full
)
999 || v_read(config
, &buf
->records_lost_wrap
)
1000 || v_read(config
, &buf
->records_lost_big
))
1001 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1002 " [ %lu buffer full, %lu nest buffer wrap-around, "
1003 "%lu event too big ]\n",
1004 chan
->backend
.name
, cpu
,
1005 v_read(config
, &buf
->records_lost_full
),
1006 v_read(config
, &buf
->records_lost_wrap
),
1007 v_read(config
, &buf
->records_lost_big
));
1009 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1013 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1015 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1018 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1019 struct channel
*chan
,
1020 struct switch_offsets
*offsets
,
1022 struct lttng_ust_shm_handle
*handle
)
1024 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1025 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1026 unsigned long commit_count
;
1028 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1031 * Order all writes to buffer before the commit count update that will
1032 * determine that the subbuffer is full.
1035 v_add(config
, config
->cb
.subbuffer_header_size(),
1036 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1037 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1038 /* Check if the written buffer has to be delivered */
1039 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1040 commit_count
, oldidx
, handle
);
1041 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1042 offsets
->old
, commit_count
,
1043 config
->cb
.subbuffer_header_size(),
1048 * lib_ring_buffer_switch_old_end: switch old subbuffer
1050 * Note : offset_old should never be 0 here. It is ok, because we never perform
1051 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1052 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1056 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1057 struct channel
*chan
,
1058 struct switch_offsets
*offsets
,
1060 struct lttng_ust_shm_handle
*handle
)
1062 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1063 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1064 unsigned long commit_count
, padding_size
, data_size
;
1066 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1067 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1068 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1072 * Order all writes to buffer before the commit count update that will
1073 * determine that the subbuffer is full.
1076 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1077 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1078 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1079 commit_count
, oldidx
, handle
);
1080 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1081 offsets
->old
, commit_count
,
1082 padding_size
, handle
);
1086 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1088 * This code can be executed unordered : writers may already have written to the
1089 * sub-buffer before this code gets executed, caution. The commit makes sure
1090 * that this code is executed before the deliver of this sub-buffer.
1093 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1094 struct channel
*chan
,
1095 struct switch_offsets
*offsets
,
1097 struct lttng_ust_shm_handle
*handle
)
1099 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1100 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1101 unsigned long commit_count
;
1103 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1106 * Order all writes to buffer before the commit count update that will
1107 * determine that the subbuffer is full.
1110 v_add(config
, config
->cb
.subbuffer_header_size(),
1111 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1112 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1113 /* Check if the written buffer has to be delivered */
1114 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1115 commit_count
, beginidx
, handle
);
1116 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1117 offsets
->begin
, commit_count
,
1118 config
->cb
.subbuffer_header_size(),
1123 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1125 * The only remaining threads could be the ones with pending commits. They will
1126 * have to do the deliver themselves.
1129 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1130 struct channel
*chan
,
1131 struct switch_offsets
*offsets
,
1133 struct lttng_ust_shm_handle
*handle
)
1135 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1136 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1137 unsigned long commit_count
, padding_size
, data_size
;
1139 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1140 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1141 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1145 * Order all writes to buffer before the commit count update that will
1146 * determine that the subbuffer is full.
1149 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1150 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1151 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1152 commit_count
, endidx
, handle
);
1153 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1154 offsets
->end
, commit_count
,
1155 padding_size
, handle
);
1161 * !0 if execution must be aborted.
1164 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1165 struct lttng_ust_lib_ring_buffer
*buf
,
1166 struct channel
*chan
,
1167 struct switch_offsets
*offsets
,
1170 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1173 offsets
->begin
= v_read(config
, &buf
->offset
);
1174 offsets
->old
= offsets
->begin
;
1175 offsets
->switch_old_start
= 0;
1176 off
= subbuf_offset(offsets
->begin
, chan
);
1178 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1181 * Ensure we flush the header of an empty subbuffer when doing the
1182 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1183 * total data gathering duration even if there were no records saved
1184 * after the last buffer switch.
1185 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1186 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1187 * subbuffer header as appropriate.
1188 * The next record that reserves space will be responsible for
1189 * populating the following subbuffer header. We choose not to populate
1190 * the next subbuffer header here because we want to be able to use
1191 * SWITCH_ACTIVE for periodical buffer flush, which must
1192 * guarantee that all the buffer content (records and header
1193 * timestamps) are visible to the reader. This is required for
1194 * quiescence guarantees for the fusion merge.
1196 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1197 if (caa_unlikely(off
== 0)) {
1199 * The client does not save any header information.
1200 * Don't switch empty subbuffer on finalize, because it
1201 * is invalid to deliver a completely empty subbuffer.
1203 if (!config
->cb
.subbuffer_header_size())
1206 * Need to write the subbuffer start header on finalize.
1208 offsets
->switch_old_start
= 1;
1210 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1212 return -1; /* we do not have to switch : buffer is empty */
1213 /* Note: old points to the next subbuf at offset 0 */
1214 offsets
->end
= offsets
->begin
;
1219 * Force a sub-buffer switch. This operation is completely reentrant : can be
1220 * called while tracing is active with absolutely no lock held.
1222 * Note, however, that as a v_cmpxchg is used for some atomic
1223 * operations, this function must be called from the CPU which owns the buffer
1224 * for a ACTIVE flush.
1226 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1227 struct lttng_ust_shm_handle
*handle
)
1229 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1230 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1231 struct switch_offsets offsets
;
1232 unsigned long oldidx
;
1238 * Perform retryable operations.
1241 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1243 return; /* Switch not needed */
1244 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1248 * Atomically update last_tsc. This update races against concurrent
1249 * atomic updates, but the race will always cause supplementary full TSC
1250 * records, never the opposite (missing a full TSC record when it would
1253 save_last_tsc(config
, buf
, tsc
);
1256 * Push the reader if necessary
1258 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1260 oldidx
= subbuf_index(offsets
.old
, chan
);
1261 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1264 * May need to populate header start on SWITCH_FLUSH.
1266 if (offsets
.switch_old_start
) {
1267 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1268 offsets
.old
+= config
->cb
.subbuffer_header_size();
1272 * Switch old subbuffer.
1274 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1280 * -ENOSPC if event size is too large for packet.
1281 * -ENOBUFS if there is currently not enough space in buffer for the event.
1282 * -EIO if data cannot be written into the buffer for any other reason.
1285 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1286 struct channel
*chan
,
1287 struct switch_offsets
*offsets
,
1288 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1290 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1291 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1292 unsigned long reserve_commit_diff
;
1294 offsets
->begin
= v_read(config
, &buf
->offset
);
1295 offsets
->old
= offsets
->begin
;
1296 offsets
->switch_new_start
= 0;
1297 offsets
->switch_new_end
= 0;
1298 offsets
->switch_old_end
= 0;
1299 offsets
->pre_header_padding
= 0;
1301 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1302 if ((int64_t) ctx
->tsc
== -EIO
)
1305 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1306 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1308 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1309 offsets
->switch_new_start
= 1; /* For offsets->begin */
1311 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1313 &offsets
->pre_header_padding
,
1316 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1319 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1320 offsets
->size
> chan
->backend
.subbuf_size
)) {
1321 offsets
->switch_old_end
= 1; /* For offsets->old */
1322 offsets
->switch_new_start
= 1; /* For offsets->begin */
1325 if (caa_unlikely(offsets
->switch_new_start
)) {
1326 unsigned long sb_index
;
1329 * We are typically not filling the previous buffer completely.
1331 if (caa_likely(offsets
->switch_old_end
))
1332 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1333 offsets
->begin
= offsets
->begin
1334 + config
->cb
.subbuffer_header_size();
1335 /* Test new buffer integrity */
1336 sb_index
= subbuf_index(offsets
->begin
, chan
);
1337 reserve_commit_diff
=
1338 (buf_trunc(offsets
->begin
, chan
)
1339 >> chan
->backend
.num_subbuf_order
)
1340 - ((unsigned long) v_read(config
,
1341 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1342 & chan
->commit_count_mask
);
1343 if (caa_likely(reserve_commit_diff
== 0)) {
1344 /* Next subbuffer not being written to. */
1345 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1346 subbuf_trunc(offsets
->begin
, chan
)
1347 - subbuf_trunc((unsigned long)
1348 uatomic_read(&buf
->consumed
), chan
)
1349 >= chan
->backend
.buf_size
)) {
1350 unsigned long nr_lost
;
1353 * We do not overwrite non consumed buffers
1354 * and we are full : record is lost.
1356 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1357 v_inc(config
, &buf
->records_lost_full
);
1358 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1359 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1360 nr_lost
+ 1, chan
->backend
.name
,
1366 * Next subbuffer not being written to, and we
1367 * are either in overwrite mode or the buffer is
1368 * not full. It's safe to write in this new
1373 unsigned long nr_lost
;
1376 * Next subbuffer reserve offset does not match the
1377 * commit offset. Drop record in producer-consumer and
1378 * overwrite mode. Caused by either a writer OOPS or too
1379 * many nested writes over a reserve/commit pair.
1381 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1382 v_inc(config
, &buf
->records_lost_wrap
);
1383 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1384 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1385 nr_lost
+ 1, chan
->backend
.name
,
1391 config
->cb
.record_header_size(config
, chan
,
1393 &offsets
->pre_header_padding
,
1396 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1399 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1400 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1401 unsigned long nr_lost
;
1404 * Record too big for subbuffers, report error, don't
1405 * complete the sub-buffer switch.
1407 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1408 v_inc(config
, &buf
->records_lost_big
);
1409 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1410 DBG("%lu or more records lost in (%s:%d) record size "
1411 " of %zu bytes is too large for buffer\n",
1412 nr_lost
+ 1, chan
->backend
.name
,
1413 buf
->backend
.cpu
, offsets
->size
);
1418 * We just made a successful buffer switch and the
1419 * record fits in the new subbuffer. Let's write.
1424 * Record fits in the current buffer and we are not on a switch
1425 * boundary. It's safe to write.
1428 offsets
->end
= offsets
->begin
+ offsets
->size
;
1430 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1432 * The offset_end will fall at the very beginning of the next
1435 offsets
->switch_new_end
= 1; /* For offsets->begin */
1441 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1442 * @ctx: ring buffer context.
1444 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1445 * -EIO for other errors, else returns 0.
1446 * It will take care of sub-buffer switching.
1448 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1450 struct channel
*chan
= ctx
->chan
;
1451 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1452 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1453 struct lttng_ust_lib_ring_buffer
*buf
;
1454 struct switch_offsets offsets
;
1457 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1458 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1460 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1466 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1468 if (caa_unlikely(ret
))
1470 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1475 * Atomically update last_tsc. This update races against concurrent
1476 * atomic updates, but the race will always cause supplementary full TSC
1477 * records, never the opposite (missing a full TSC record when it would
1480 save_last_tsc(config
, buf
, ctx
->tsc
);
1483 * Push the reader if necessary
1485 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1488 * Clear noref flag for this subbuffer.
1490 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1491 subbuf_index(offsets
.end
- 1, chan
),
1495 * Switch old subbuffer if needed.
1497 if (caa_unlikely(offsets
.switch_old_end
)) {
1498 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1499 subbuf_index(offsets
.old
- 1, chan
),
1501 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1505 * Populate new subbuffer.
1507 if (caa_unlikely(offsets
.switch_new_start
))
1508 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1510 if (caa_unlikely(offsets
.switch_new_end
))
1511 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1513 ctx
->slot_size
= offsets
.size
;
1514 ctx
->pre_offset
= offsets
.begin
;
1515 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1520 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1522 void lttng_fixup_ringbuffer_tls(void)
1524 asm volatile ("" : : "m" (lib_ring_buffer_nesting
));