2 * ring_buffer_frontend.c
4 * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
7 * recorder (overwrite) modes. See thesis:
9 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
10 * dissertation, Ecole Polytechnique de Montreal.
11 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
13 * - Algorithm presentation in Chapter 5:
14 * "Lockless Multi-Core High-Throughput Buffering".
15 * - Algorithm formal verification in Section 8.6:
16 * "Formal verification of LTTng"
19 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
21 * Inspired from LTT and RelayFS:
22 * Karim Yaghmour <karim@opersys.com>
23 * Tom Zanussi <zanussi@us.ibm.com>
24 * Bob Wisniewski <bob@watson.ibm.com>
26 * Bob Wisniewski <bob@watson.ibm.com>
28 * Buffer reader semantic :
31 * while buffer is not finalized and empty
33 * - if return value != 0, continue
34 * - splice one subbuffer worth of data to a pipe
35 * - splice the data from pipe to disk/network
38 * Dual LGPL v2.1/GPL v2 license.
41 #include <sys/types.h>
45 #include <urcu/compiler.h>
49 #include <lttng/ringbuffer-config.h>
55 #define max(a, b) ((a) > (b) ? (a) : (b))
59 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
60 * close(2) to close the fd returned by shm_open.
61 * shm_unlink releases the shared memory object name.
62 * ftruncate(2) sets the size of the memory object.
63 * mmap/munmap maps the shared memory obj to a virtual address in the
64 * calling proceess (should be done both in libust and consumer).
65 * See shm_overview(7) for details.
66 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
69 * Since we don't need to access the object using its name, we can
70 * immediately shm_unlink(3) it, and only keep the handle with its file
75 * Internal structure representing offsets to use at a sub-buffer switch.
77 struct switch_offsets
{
78 unsigned long begin
, end
, old
;
79 size_t pre_header_padding
, size
;
80 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
84 __thread
unsigned int lib_ring_buffer_nesting
;
87 void lib_ring_buffer_print_errors(struct channel
*chan
,
88 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
89 struct lttng_ust_shm_handle
*handle
);
92 * Must be called under cpu hotplug protection.
94 void lib_ring_buffer_free(struct lttng_ust_lib_ring_buffer
*buf
,
95 struct lttng_ust_shm_handle
*handle
)
97 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
99 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
, handle
);
100 /* buf->commit_hot will be freed by shm teardown */
101 /* buf->commit_cold will be freed by shm teardown */
103 lib_ring_buffer_backend_free(&buf
->backend
);
107 * lib_ring_buffer_reset - Reset ring buffer to initial values.
110 * Effectively empty the ring buffer. Should be called when the buffer is not
111 * used for writing. The ring buffer can be opened for reading, but the reader
112 * should not be using the iterator concurrently with reset. The previous
113 * current iterator record is reset.
115 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
116 struct lttng_ust_shm_handle
*handle
)
118 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
119 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
123 * Reset iterator first. It will put the subbuffer if it currently holds
126 v_set(config
, &buf
->offset
, 0);
127 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
128 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
129 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
130 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
132 uatomic_set(&buf
->consumed
, 0);
133 uatomic_set(&buf
->record_disabled
, 0);
134 v_set(config
, &buf
->last_tsc
, 0);
135 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
136 /* Don't reset number of active readers */
137 v_set(config
, &buf
->records_lost_full
, 0);
138 v_set(config
, &buf
->records_lost_wrap
, 0);
139 v_set(config
, &buf
->records_lost_big
, 0);
140 v_set(config
, &buf
->records_count
, 0);
141 v_set(config
, &buf
->records_overrun
, 0);
146 * channel_reset - Reset channel to initial values.
149 * Effectively empty the channel. Should be called when the channel is not used
150 * for writing. The channel can be opened for reading, but the reader should not
151 * be using the iterator concurrently with reset. The previous current iterator
154 void channel_reset(struct channel
*chan
)
157 * Reset iterators first. Will put the subbuffer if held for reading.
159 uatomic_set(&chan
->record_disabled
, 0);
160 /* Don't reset commit_count_mask, still valid */
161 channel_backend_reset(&chan
->backend
);
162 /* Don't reset switch/read timer interval */
163 /* Don't reset notifiers and notifier enable bits */
164 /* Don't reset reader reference count */
168 * Must be called under cpu hotplug protection.
170 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
171 struct channel_backend
*chanb
, int cpu
,
172 struct lttng_ust_shm_handle
*handle
,
173 struct shm_object
*shmobj
)
175 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
176 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
177 void *priv
= channel_get_private(chan
);
178 size_t subbuf_header_size
;
182 /* Test for cpu hotplug */
183 if (buf
->backend
.allocated
)
186 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
187 cpu
, handle
, shmobj
);
191 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
192 set_shmp(buf
->commit_hot
,
194 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
195 if (!shmp(handle
, buf
->commit_hot
)) {
200 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
201 set_shmp(buf
->commit_cold
,
203 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
204 if (!shmp(handle
, buf
->commit_cold
)) {
210 * Write the subbuffer header for first subbuffer so we know the total
211 * duration of data gathering.
213 subbuf_header_size
= config
->cb
.subbuffer_header_size();
214 v_set(config
, &buf
->offset
, subbuf_header_size
);
215 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
216 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
217 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
218 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
220 if (config
->cb
.buffer_create
) {
221 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
225 buf
->backend
.allocated
= 1;
230 /* commit_cold will be freed by shm teardown */
232 /* commit_hot will be freed by shm teardown */
234 lib_ring_buffer_backend_free(&buf
->backend
);
239 static void switch_buffer_timer(unsigned long data
)
241 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
242 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
243 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
246 * Only flush buffers periodically if readers are active.
248 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
249 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
252 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
253 // mod_timer_pinned(&buf->switch_timer,
254 // jiffies + chan->switch_timer_interval);
256 // mod_timer(&buf->switch_timer,
257 // jiffies + chan->switch_timer_interval);
261 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
262 struct lttng_ust_shm_handle
*handle
)
264 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
265 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
267 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
270 //init_timer(&buf->switch_timer);
271 //buf->switch_timer.function = switch_buffer_timer;
272 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
273 //buf->switch_timer.data = (unsigned long)buf;
274 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
275 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
277 // add_timer(&buf->switch_timer);
278 buf
->switch_timer_enabled
= 1;
281 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
282 struct lttng_ust_shm_handle
*handle
)
284 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
286 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
290 //del_timer_sync(&buf->switch_timer);
291 buf
->switch_timer_enabled
= 0;
296 * Polling timer to check the channels for data.
298 static void read_buffer_timer(unsigned long data
)
300 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
301 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
302 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
304 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
306 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
307 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
309 //wake_up_interruptible(&buf->read_wait);
310 //wake_up_interruptible(&chan->read_wait);
314 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
315 // mod_timer_pinned(&buf->read_timer,
316 // jiffies + chan->read_timer_interval);
318 // mod_timer(&buf->read_timer,
319 // jiffies + chan->read_timer_interval);
323 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
324 struct lttng_ust_shm_handle
*handle
)
326 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
327 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
329 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
330 || !chan
->read_timer_interval
331 || buf
->read_timer_enabled
)
335 //init_timer(&buf->read_timer);
336 //buf->read_timer.function = read_buffer_timer;
337 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
338 //buf->read_timer.data = (unsigned long)buf;
340 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
341 // add_timer_on(&buf->read_timer, buf->backend.cpu);
343 // add_timer(&buf->read_timer);
344 buf
->read_timer_enabled
= 1;
347 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
348 struct lttng_ust_shm_handle
*handle
)
350 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
351 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
353 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
354 || !chan
->read_timer_interval
355 || !buf
->read_timer_enabled
)
359 //del_timer_sync(&buf->read_timer);
361 * do one more check to catch data that has been written in the last
364 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
366 //wake_up_interruptible(&buf->read_wait);
367 //wake_up_interruptible(&chan->read_wait);
369 buf
->read_timer_enabled
= 0;
372 static void channel_unregister_notifiers(struct channel
*chan
,
373 struct lttng_ust_shm_handle
*handle
)
375 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
378 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
379 for_each_possible_cpu(cpu
) {
380 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
382 lib_ring_buffer_stop_switch_timer(buf
, handle
);
383 lib_ring_buffer_stop_read_timer(buf
, handle
);
386 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
388 lib_ring_buffer_stop_switch_timer(buf
, handle
);
389 lib_ring_buffer_stop_read_timer(buf
, handle
);
391 //channel_backend_unregister_notifiers(&chan->backend);
394 static void channel_free(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
398 channel_backend_free(&chan
->backend
, handle
);
399 /* chan is freed by shm teardown */
400 shm_object_table_destroy(handle
->table
);
405 * channel_create - Create channel.
406 * @config: ring buffer instance configuration
407 * @name: name of the channel
408 * @priv_data: ring buffer client private data area pointer (output)
409 * @priv_data_size: length, in bytes, of the private data area.
410 * @priv_data_init: initialization data for private data.
411 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
412 * address mapping. It is used only by RING_BUFFER_STATIC
413 * configuration. It can be set to NULL for other backends.
414 * @subbuf_size: subbuffer size
415 * @num_subbuf: number of subbuffers
416 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
417 * padding to let readers get those sub-buffers.
418 * Used for live streaming.
419 * @read_timer_interval: Time interval (in us) to wake up pending readers.
422 * Returns NULL on failure.
424 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
427 size_t priv_data_align
,
428 size_t priv_data_size
,
429 void *priv_data_init
,
430 void *buf_addr
, size_t subbuf_size
,
431 size_t num_subbuf
, unsigned int switch_timer_interval
,
432 unsigned int read_timer_interval
,
433 int *shm_fd
, int *wait_fd
, uint64_t *memory_map_size
)
436 size_t shmsize
, chansize
;
437 struct channel
*chan
;
438 struct lttng_ust_shm_handle
*handle
;
439 struct shm_object
*shmobj
;
442 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
443 read_timer_interval
))
446 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
450 /* Allocate table for channel + per-cpu buffers */
451 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
453 goto error_table_alloc
;
455 /* Calculate the shm allocation layout */
456 shmsize
= sizeof(struct channel
);
457 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
458 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
459 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * num_possible_cpus();
461 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
);
463 shmsize
+= offset_align(shmsize
, priv_data_align
);
464 shmsize
+= priv_data_size
;
466 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
469 /* struct channel is at object 0, offset 0 (hardcoded) */
470 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
471 assert(handle
->chan
._ref
.index
== 0);
472 assert(handle
->chan
._ref
.offset
== 0);
473 chan
= shmp(handle
, handle
->chan
);
477 /* space for private data */
478 if (priv_data_size
) {
479 DECLARE_SHMP(void, priv_data_alloc
);
481 align_shm(shmobj
, priv_data_align
);
482 chan
->priv_data_offset
= shmobj
->allocated_len
;
483 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
484 if (!shmp(handle
, priv_data_alloc
))
486 *priv_data
= channel_get_private(chan
);
487 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
489 chan
->priv_data_offset
= -1;
493 ret
= channel_backend_init(&chan
->backend
, name
, config
,
494 subbuf_size
, num_subbuf
, handle
);
496 goto error_backend_init
;
498 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
500 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
501 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
503 //init_waitqueue_head(&chan->read_wait);
504 //init_waitqueue_head(&chan->hp_wait);
506 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
508 * In case of non-hotplug cpu, if the ring-buffer is allocated
509 * in early initcall, it will not be notified of secondary cpus.
510 * In that off case, we need to allocate for all possible cpus.
512 for_each_possible_cpu(cpu
) {
513 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
514 lib_ring_buffer_start_switch_timer(buf
, handle
);
515 lib_ring_buffer_start_read_timer(buf
, handle
);
518 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
520 lib_ring_buffer_start_switch_timer(buf
, handle
);
521 lib_ring_buffer_start_read_timer(buf
, handle
);
523 ref
= &handle
->chan
._ref
;
524 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
, memory_map_size
);
529 shm_object_table_destroy(handle
->table
);
535 struct lttng_ust_shm_handle
*channel_handle_create(int shm_fd
, int wait_fd
,
536 uint64_t memory_map_size
)
538 struct lttng_ust_shm_handle
*handle
;
539 struct shm_object
*object
;
541 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
545 /* Allocate table for channel + per-cpu buffers */
546 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
548 goto error_table_alloc
;
549 /* Add channel object */
550 object
= shm_object_table_append_shadow(handle
->table
,
551 shm_fd
, wait_fd
, memory_map_size
);
553 goto error_table_object
;
554 /* struct channel is at object 0, offset 0 (hardcoded) */
555 handle
->chan
._ref
.index
= 0;
556 handle
->chan
._ref
.offset
= 0;
560 shm_object_table_destroy(handle
->table
);
566 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
567 int shm_fd
, int wait_fd
, uint64_t memory_map_size
)
569 struct shm_object
*object
;
571 /* Add stream object */
572 object
= shm_object_table_append_shadow(handle
->table
,
573 shm_fd
, wait_fd
, memory_map_size
);
580 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
583 channel_free(chan
, handle
, shadow
);
587 * channel_destroy - Finalize, wait for q.s. and destroy channel.
588 * @chan: channel to destroy
591 * Call "destroy" callback, finalize channels, decrement the channel
592 * reference count. Note that when readers have completed data
593 * consumption of finalized channels, get_subbuf() will return -ENODATA.
594 * They should release their handle at that point.
596 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
599 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
603 channel_release(chan
, handle
, shadow
);
607 channel_unregister_notifiers(chan
, handle
);
609 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
610 for_each_channel_cpu(cpu
, chan
) {
611 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
613 if (config
->cb
.buffer_finalize
)
614 config
->cb
.buffer_finalize(buf
,
615 channel_get_private(chan
),
617 if (buf
->backend
.allocated
)
618 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
,
621 * Perform flush before writing to finalized.
624 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
625 //wake_up_interruptible(&buf->read_wait);
628 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
630 if (config
->cb
.buffer_finalize
)
631 config
->cb
.buffer_finalize(buf
, channel_get_private(chan
), -1, handle
);
632 if (buf
->backend
.allocated
)
633 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
,
636 * Perform flush before writing to finalized.
639 CMM_ACCESS_ONCE(buf
->finalized
) = 1;
640 //wake_up_interruptible(&buf->read_wait);
642 CMM_ACCESS_ONCE(chan
->finalized
) = 1;
643 //wake_up_interruptible(&chan->hp_wait);
644 //wake_up_interruptible(&chan->read_wait);
646 * sessiond/consumer are keeping a reference on the shm file
647 * descriptor directly. No need to refcount.
649 channel_release(chan
, handle
, shadow
);
653 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
654 const struct lttng_ust_lib_ring_buffer_config
*config
,
655 struct channel
*chan
, int cpu
,
656 struct lttng_ust_shm_handle
*handle
,
657 int *shm_fd
, int *wait_fd
,
658 uint64_t *memory_map_size
)
662 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
663 ref
= &chan
->backend
.buf
[0].shmp
._ref
;
664 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
666 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
668 if (cpu
>= num_possible_cpus())
670 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
671 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
673 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
677 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
678 struct lttng_ust_shm_handle
*handle
,
682 if (uatomic_cmpxchg(&buf
->active_shadow_readers
, 0, 1) != 0)
687 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
693 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
694 struct lttng_ust_shm_handle
*handle
,
697 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
700 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_shadow_readers
) != 1);
702 uatomic_dec(&buf
->active_shadow_readers
);
705 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
707 uatomic_dec(&buf
->active_readers
);
711 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
713 * @consumed: consumed count indicating the position where to read
714 * @produced: produced count, indicates position when to stop reading
716 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
717 * data to read at consumed position, or 0 if the get operation succeeds.
720 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
721 unsigned long *consumed
, unsigned long *produced
,
722 struct lttng_ust_shm_handle
*handle
)
724 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
725 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
726 unsigned long consumed_cur
, write_offset
;
729 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
731 * Read finalized before counters.
734 consumed_cur
= uatomic_read(&buf
->consumed
);
736 * No need to issue a memory barrier between consumed count read and
737 * write offset read, because consumed count can only change
738 * concurrently in overwrite mode, and we keep a sequence counter
739 * identifier derived from the write offset to check we are getting
740 * the same sub-buffer we are expecting (the sub-buffers are atomically
741 * "tagged" upon writes, tags are checked upon read).
743 write_offset
= v_read(config
, &buf
->offset
);
746 * Check that we are not about to read the same subbuffer in
747 * which the writer head is.
749 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
753 *consumed
= consumed_cur
;
754 *produced
= subbuf_trunc(write_offset
, chan
);
760 * The memory barriers __wait_event()/wake_up_interruptible() take care
761 * of "raw_spin_is_locked" memory ordering.
770 * lib_ring_buffer_put_snapshot - move consumed counter forward
772 * @consumed_new: new consumed count value
774 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
775 unsigned long consumed_new
,
776 struct lttng_ust_shm_handle
*handle
)
778 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
779 struct channel
*chan
= shmp(handle
, bufb
->chan
);
780 unsigned long consumed
;
782 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
783 && uatomic_read(&buf
->active_shadow_readers
) != 1);
786 * Only push the consumed value forward.
787 * If the consumed cmpxchg fails, this is because we have been pushed by
788 * the writer in flight recorder mode.
790 consumed
= uatomic_read(&buf
->consumed
);
791 while ((long) consumed
- (long) consumed_new
< 0)
792 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
797 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
799 * @consumed: consumed count indicating the position where to read
801 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
802 * data to read at consumed position, or 0 if the get operation succeeds.
804 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
805 unsigned long consumed
,
806 struct lttng_ust_shm_handle
*handle
)
808 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
809 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
810 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
815 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
817 * Read finalized before counters.
820 consumed_cur
= uatomic_read(&buf
->consumed
);
821 consumed_idx
= subbuf_index(consumed
, chan
);
822 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
824 * Make sure we read the commit count before reading the buffer
825 * data and the write offset. Correct consumed offset ordering
826 * wrt commit count is insured by the use of cmpxchg to update
827 * the consumed offset.
830 * Local rmb to match the remote wmb to read the commit count
831 * before the buffer data and the write offset.
835 write_offset
= v_read(config
, &buf
->offset
);
838 * Check that the buffer we are getting is after or at consumed_cur
841 if ((long) subbuf_trunc(consumed
, chan
)
842 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
846 * Check that the subbuffer we are trying to consume has been
847 * already fully committed.
849 if (((commit_count
- chan
->backend
.subbuf_size
)
850 & chan
->commit_count_mask
)
851 - (buf_trunc(consumed_cur
, chan
)
852 >> chan
->backend
.num_subbuf_order
)
857 * Check that we are not about to read the same subbuffer in
858 * which the writer head is.
860 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
865 * Failure to get the subbuffer causes a busy-loop retry without going
866 * to a wait queue. These are caused by short-lived race windows where
867 * the writer is getting access to a subbuffer we were trying to get
868 * access to. Also checks that the "consumed" buffer count we are
869 * looking for matches the one contained in the subbuffer id.
871 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
872 consumed_idx
, buf_trunc_val(consumed
, chan
),
876 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
878 buf
->get_subbuf_consumed
= consumed
;
885 * The memory barriers __wait_event()/wake_up_interruptible() take care
886 * of "raw_spin_is_locked" memory ordering.
895 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
898 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
899 struct lttng_ust_shm_handle
*handle
)
901 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
902 struct channel
*chan
= shmp(handle
, bufb
->chan
);
903 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
904 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
906 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
907 && uatomic_read(&buf
->active_shadow_readers
) != 1);
909 if (!buf
->get_subbuf
) {
911 * Reader puts a subbuffer it did not get.
913 CHAN_WARN_ON(chan
, 1);
916 consumed
= buf
->get_subbuf_consumed
;
920 * Clear the records_unread counter. (overruns counter)
921 * Can still be non-zero if a file reader simply grabbed the data
922 * without using iterators.
923 * Can be below zero if an iterator is used on a snapshot more than
926 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
927 v_add(config
, v_read(config
,
928 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
929 &bufb
->records_read
);
930 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
931 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
932 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
933 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
936 * Exchange the reader subbuffer with the one we put in its place in the
937 * writer subbuffer table. Expect the original consumed count. If
938 * update_read_sb_index fails, this is because the writer updated the
939 * subbuffer concurrently. We should therefore keep the subbuffer we
940 * currently have: it has become invalid to try reading this sub-buffer
941 * consumed count value anyway.
943 consumed_idx
= subbuf_index(consumed
, chan
);
944 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
945 consumed_idx
, buf_trunc_val(consumed
, chan
),
948 * update_read_sb_index return value ignored. Don't exchange sub-buffer
949 * if the writer concurrently updated it.
954 * cons_offset is an iterator on all subbuffer offsets between the reader
955 * position and the writer position. (inclusive)
958 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
959 struct channel
*chan
,
960 unsigned long cons_offset
,
962 struct lttng_ust_shm_handle
*handle
)
964 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
965 unsigned long cons_idx
, commit_count
, commit_count_sb
;
967 cons_idx
= subbuf_index(cons_offset
, chan
);
968 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
969 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
971 if (subbuf_offset(commit_count
, chan
) != 0)
972 DBG("ring buffer %s, cpu %d: "
973 "commit count in subbuffer %lu,\n"
974 "expecting multiples of %lu bytes\n"
975 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
976 chan
->backend
.name
, cpu
, cons_idx
,
977 chan
->backend
.subbuf_size
,
978 commit_count
, commit_count_sb
);
980 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
981 chan
->backend
.name
, cpu
, commit_count
);
985 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
986 struct channel
*chan
,
988 struct lttng_ust_shm_handle
*handle
)
990 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
991 unsigned long write_offset
, cons_offset
;
994 * Can be called in the error path of allocation when
995 * trans_channel_data is not yet set.
1000 * No need to order commit_count, write_offset and cons_offset reads
1001 * because we execute at teardown when no more writer nor reader
1002 * references are left.
1004 write_offset
= v_read(config
, &buf
->offset
);
1005 cons_offset
= uatomic_read(&buf
->consumed
);
1006 if (write_offset
!= cons_offset
)
1007 DBG("ring buffer %s, cpu %d: "
1008 "non-consumed data\n"
1009 " [ %lu bytes written, %lu bytes read ]\n",
1010 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1012 for (cons_offset
= uatomic_read(&buf
->consumed
);
1013 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1016 cons_offset
= subbuf_align(cons_offset
, chan
))
1017 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1022 void lib_ring_buffer_print_errors(struct channel
*chan
,
1023 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1024 struct lttng_ust_shm_handle
*handle
)
1026 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1027 void *priv
= channel_get_private(chan
);
1029 DBG("ring buffer %s, cpu %d: %lu records written, "
1030 "%lu records overrun\n",
1031 chan
->backend
.name
, cpu
,
1032 v_read(config
, &buf
->records_count
),
1033 v_read(config
, &buf
->records_overrun
));
1035 if (v_read(config
, &buf
->records_lost_full
)
1036 || v_read(config
, &buf
->records_lost_wrap
)
1037 || v_read(config
, &buf
->records_lost_big
))
1038 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1039 " [ %lu buffer full, %lu nest buffer wrap-around, "
1040 "%lu event too big ]\n",
1041 chan
->backend
.name
, cpu
,
1042 v_read(config
, &buf
->records_lost_full
),
1043 v_read(config
, &buf
->records_lost_wrap
),
1044 v_read(config
, &buf
->records_lost_big
));
1046 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1050 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1052 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1055 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1056 struct channel
*chan
,
1057 struct switch_offsets
*offsets
,
1059 struct lttng_ust_shm_handle
*handle
)
1061 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1062 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1063 unsigned long commit_count
;
1065 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1068 * Order all writes to buffer before the commit count update that will
1069 * determine that the subbuffer is full.
1072 v_add(config
, config
->cb
.subbuffer_header_size(),
1073 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1074 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1075 /* Check if the written buffer has to be delivered */
1076 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1077 commit_count
, oldidx
, handle
);
1078 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1079 offsets
->old
, commit_count
,
1080 config
->cb
.subbuffer_header_size(),
1085 * lib_ring_buffer_switch_old_end: switch old subbuffer
1087 * Note : offset_old should never be 0 here. It is ok, because we never perform
1088 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1089 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1093 void lib_ring_buffer_switch_old_end(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 oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1101 unsigned long commit_count
, padding_size
, data_size
;
1103 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1104 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1105 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1109 * Order all writes to buffer before the commit count update that will
1110 * determine that the subbuffer is full.
1113 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1114 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1115 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1116 commit_count
, oldidx
, handle
);
1117 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1118 offsets
->old
, commit_count
,
1119 padding_size
, handle
);
1123 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1125 * This code can be executed unordered : writers may already have written to the
1126 * sub-buffer before this code gets executed, caution. The commit makes sure
1127 * that this code is executed before the deliver of this sub-buffer.
1130 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1131 struct channel
*chan
,
1132 struct switch_offsets
*offsets
,
1134 struct lttng_ust_shm_handle
*handle
)
1136 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1137 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1138 unsigned long commit_count
;
1140 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1143 * Order all writes to buffer before the commit count update that will
1144 * determine that the subbuffer is full.
1147 v_add(config
, config
->cb
.subbuffer_header_size(),
1148 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1149 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1150 /* Check if the written buffer has to be delivered */
1151 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1152 commit_count
, beginidx
, handle
);
1153 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1154 offsets
->begin
, commit_count
,
1155 config
->cb
.subbuffer_header_size(),
1160 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1162 * The only remaining threads could be the ones with pending commits. They will
1163 * have to do the deliver themselves.
1166 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1167 struct channel
*chan
,
1168 struct switch_offsets
*offsets
,
1170 struct lttng_ust_shm_handle
*handle
)
1172 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1173 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1174 unsigned long commit_count
, padding_size
, data_size
;
1176 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1177 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1178 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1182 * Order all writes to buffer before the commit count update that will
1183 * determine that the subbuffer is full.
1186 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1187 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1188 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1189 commit_count
, endidx
, handle
);
1190 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1191 offsets
->end
, commit_count
,
1192 padding_size
, handle
);
1198 * !0 if execution must be aborted.
1201 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1202 struct lttng_ust_lib_ring_buffer
*buf
,
1203 struct channel
*chan
,
1204 struct switch_offsets
*offsets
,
1207 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1210 offsets
->begin
= v_read(config
, &buf
->offset
);
1211 offsets
->old
= offsets
->begin
;
1212 offsets
->switch_old_start
= 0;
1213 off
= subbuf_offset(offsets
->begin
, chan
);
1215 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1218 * Ensure we flush the header of an empty subbuffer when doing the
1219 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1220 * total data gathering duration even if there were no records saved
1221 * after the last buffer switch.
1222 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1223 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1224 * subbuffer header as appropriate.
1225 * The next record that reserves space will be responsible for
1226 * populating the following subbuffer header. We choose not to populate
1227 * the next subbuffer header here because we want to be able to use
1228 * SWITCH_ACTIVE for periodical buffer flush, which must
1229 * guarantee that all the buffer content (records and header
1230 * timestamps) are visible to the reader. This is required for
1231 * quiescence guarantees for the fusion merge.
1233 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1234 if (caa_unlikely(off
== 0)) {
1236 * The client does not save any header information.
1237 * Don't switch empty subbuffer on finalize, because it
1238 * is invalid to deliver a completely empty subbuffer.
1240 if (!config
->cb
.subbuffer_header_size())
1243 * Need to write the subbuffer start header on finalize.
1245 offsets
->switch_old_start
= 1;
1247 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1249 return -1; /* we do not have to switch : buffer is empty */
1250 /* Note: old points to the next subbuf at offset 0 */
1251 offsets
->end
= offsets
->begin
;
1256 * Force a sub-buffer switch. This operation is completely reentrant : can be
1257 * called while tracing is active with absolutely no lock held.
1259 * Note, however, that as a v_cmpxchg is used for some atomic
1260 * operations, this function must be called from the CPU which owns the buffer
1261 * for a ACTIVE flush.
1263 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1264 struct lttng_ust_shm_handle
*handle
)
1266 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1267 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1268 struct switch_offsets offsets
;
1269 unsigned long oldidx
;
1275 * Perform retryable operations.
1278 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1280 return; /* Switch not needed */
1281 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1285 * Atomically update last_tsc. This update races against concurrent
1286 * atomic updates, but the race will always cause supplementary full TSC
1287 * records, never the opposite (missing a full TSC record when it would
1290 save_last_tsc(config
, buf
, tsc
);
1293 * Push the reader if necessary
1295 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1297 oldidx
= subbuf_index(offsets
.old
, chan
);
1298 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1301 * May need to populate header start on SWITCH_FLUSH.
1303 if (offsets
.switch_old_start
) {
1304 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1305 offsets
.old
+= config
->cb
.subbuffer_header_size();
1309 * Switch old subbuffer.
1311 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1317 * -ENOSPC if event size is too large for packet.
1318 * -ENOBUFS if there is currently not enough space in buffer for the event.
1319 * -EIO if data cannot be written into the buffer for any other reason.
1322 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1323 struct channel
*chan
,
1324 struct switch_offsets
*offsets
,
1325 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1327 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1328 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1329 unsigned long reserve_commit_diff
;
1331 offsets
->begin
= v_read(config
, &buf
->offset
);
1332 offsets
->old
= offsets
->begin
;
1333 offsets
->switch_new_start
= 0;
1334 offsets
->switch_new_end
= 0;
1335 offsets
->switch_old_end
= 0;
1336 offsets
->pre_header_padding
= 0;
1338 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1339 if ((int64_t) ctx
->tsc
== -EIO
)
1342 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1343 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1345 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1346 offsets
->switch_new_start
= 1; /* For offsets->begin */
1348 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1350 &offsets
->pre_header_padding
,
1353 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1356 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1357 offsets
->size
> chan
->backend
.subbuf_size
)) {
1358 offsets
->switch_old_end
= 1; /* For offsets->old */
1359 offsets
->switch_new_start
= 1; /* For offsets->begin */
1362 if (caa_unlikely(offsets
->switch_new_start
)) {
1363 unsigned long sb_index
;
1366 * We are typically not filling the previous buffer completely.
1368 if (caa_likely(offsets
->switch_old_end
))
1369 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1370 offsets
->begin
= offsets
->begin
1371 + config
->cb
.subbuffer_header_size();
1372 /* Test new buffer integrity */
1373 sb_index
= subbuf_index(offsets
->begin
, chan
);
1374 reserve_commit_diff
=
1375 (buf_trunc(offsets
->begin
, chan
)
1376 >> chan
->backend
.num_subbuf_order
)
1377 - ((unsigned long) v_read(config
,
1378 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1379 & chan
->commit_count_mask
);
1380 if (caa_likely(reserve_commit_diff
== 0)) {
1381 /* Next subbuffer not being written to. */
1382 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1383 subbuf_trunc(offsets
->begin
, chan
)
1384 - subbuf_trunc((unsigned long)
1385 uatomic_read(&buf
->consumed
), chan
)
1386 >= chan
->backend
.buf_size
)) {
1388 * We do not overwrite non consumed buffers
1389 * and we are full : record is lost.
1391 v_inc(config
, &buf
->records_lost_full
);
1395 * Next subbuffer not being written to, and we
1396 * are either in overwrite mode or the buffer is
1397 * not full. It's safe to write in this new
1403 * Next subbuffer reserve offset does not match the
1404 * commit offset. Drop record in producer-consumer and
1405 * overwrite mode. Caused by either a writer OOPS or too
1406 * many nested writes over a reserve/commit pair.
1408 v_inc(config
, &buf
->records_lost_wrap
);
1412 config
->cb
.record_header_size(config
, chan
,
1414 &offsets
->pre_header_padding
,
1417 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1420 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1421 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1423 * Record too big for subbuffers, report error, don't
1424 * complete the sub-buffer switch.
1426 v_inc(config
, &buf
->records_lost_big
);
1430 * We just made a successful buffer switch and the
1431 * record fits in the new subbuffer. Let's write.
1436 * Record fits in the current buffer and we are not on a switch
1437 * boundary. It's safe to write.
1440 offsets
->end
= offsets
->begin
+ offsets
->size
;
1442 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1444 * The offset_end will fall at the very beginning of the next
1447 offsets
->switch_new_end
= 1; /* For offsets->begin */
1453 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1454 * @ctx: ring buffer context.
1456 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1457 * -EIO for other errors, else returns 0.
1458 * It will take care of sub-buffer switching.
1460 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1462 struct channel
*chan
= ctx
->chan
;
1463 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1464 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1465 struct lttng_ust_lib_ring_buffer
*buf
;
1466 struct switch_offsets offsets
;
1469 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1470 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1472 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1478 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1480 if (caa_unlikely(ret
))
1482 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1487 * Atomically update last_tsc. This update races against concurrent
1488 * atomic updates, but the race will always cause supplementary full TSC
1489 * records, never the opposite (missing a full TSC record when it would
1492 save_last_tsc(config
, buf
, ctx
->tsc
);
1495 * Push the reader if necessary
1497 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1500 * Clear noref flag for this subbuffer.
1502 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1503 subbuf_index(offsets
.end
- 1, chan
),
1507 * Switch old subbuffer if needed.
1509 if (caa_unlikely(offsets
.switch_old_end
)) {
1510 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1511 subbuf_index(offsets
.old
- 1, chan
),
1513 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1517 * Populate new subbuffer.
1519 if (caa_unlikely(offsets
.switch_new_start
))
1520 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1522 if (caa_unlikely(offsets
.switch_new_end
))
1523 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1525 ctx
->slot_size
= offsets
.size
;
1526 ctx
->pre_offset
= offsets
.begin
;
1527 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;