2 * ring_buffer_frontend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
41 * Bob Wisniewski <bob@watson.ibm.com>
43 * Buffer reader semantic :
46 * while buffer is not finalized and empty
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
55 #include <sys/types.h>
59 #include <urcu/compiler.h>
61 #include <urcu/tls-compat.h>
65 #include <lttng/ringbuffer-config.h>
71 #include "../liblttng-ust/compat.h" /* For ENODATA */
74 #define max(a, b) ((a) > (b) ? (a) : (b))
77 /* Print DBG() messages about events lost only every 1048576 hits */
78 #define DBG_PRINT_NR_LOST (1UL << 20)
81 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
82 * close(2) to close the fd returned by shm_open.
83 * shm_unlink releases the shared memory object name.
84 * ftruncate(2) sets the size of the memory object.
85 * mmap/munmap maps the shared memory obj to a virtual address in the
86 * calling proceess (should be done both in libust and consumer).
87 * See shm_overview(7) for details.
88 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
91 * Since we don't need to access the object using its name, we can
92 * immediately shm_unlink(3) it, and only keep the handle with its file
97 * Internal structure representing offsets to use at a sub-buffer switch.
99 struct switch_offsets
{
100 unsigned long begin
, end
, old
;
101 size_t pre_header_padding
, size
;
102 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
106 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
109 * TODO: this is unused. Errors are saved within the ring buffer.
110 * Eventually, allow consumerd to print these errors.
113 void lib_ring_buffer_print_errors(struct channel
*chan
,
114 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
115 struct lttng_ust_shm_handle
*handle
)
116 __attribute__((unused
));
119 * lib_ring_buffer_reset - Reset ring buffer to initial values.
122 * Effectively empty the ring buffer. Should be called when the buffer is not
123 * used for writing. The ring buffer can be opened for reading, but the reader
124 * should not be using the iterator concurrently with reset. The previous
125 * current iterator record is reset.
127 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
128 struct lttng_ust_shm_handle
*handle
)
130 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
131 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
135 * Reset iterator first. It will put the subbuffer if it currently holds
138 v_set(config
, &buf
->offset
, 0);
139 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
140 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
141 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
142 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
144 uatomic_set(&buf
->consumed
, 0);
145 uatomic_set(&buf
->record_disabled
, 0);
146 v_set(config
, &buf
->last_tsc
, 0);
147 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
148 /* Don't reset number of active readers */
149 v_set(config
, &buf
->records_lost_full
, 0);
150 v_set(config
, &buf
->records_lost_wrap
, 0);
151 v_set(config
, &buf
->records_lost_big
, 0);
152 v_set(config
, &buf
->records_count
, 0);
153 v_set(config
, &buf
->records_overrun
, 0);
158 * channel_reset - Reset channel to initial values.
161 * Effectively empty the channel. Should be called when the channel is not used
162 * for writing. The channel can be opened for reading, but the reader should not
163 * be using the iterator concurrently with reset. The previous current iterator
166 void channel_reset(struct channel
*chan
)
169 * Reset iterators first. Will put the subbuffer if held for reading.
171 uatomic_set(&chan
->record_disabled
, 0);
172 /* Don't reset commit_count_mask, still valid */
173 channel_backend_reset(&chan
->backend
);
174 /* Don't reset switch/read timer interval */
175 /* Don't reset notifiers and notifier enable bits */
176 /* Don't reset reader reference count */
180 * Must be called under cpu hotplug protection.
182 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
183 struct channel_backend
*chanb
, int cpu
,
184 struct lttng_ust_shm_handle
*handle
,
185 struct shm_object
*shmobj
)
187 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
188 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
189 void *priv
= channel_get_private(chan
);
190 size_t subbuf_header_size
;
194 /* Test for cpu hotplug */
195 if (buf
->backend
.allocated
)
198 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
199 cpu
, handle
, shmobj
);
203 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
204 set_shmp(buf
->commit_hot
,
206 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
207 if (!shmp(handle
, buf
->commit_hot
)) {
212 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
213 set_shmp(buf
->commit_cold
,
215 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
216 if (!shmp(handle
, buf
->commit_cold
)) {
222 * Write the subbuffer header for first subbuffer so we know the total
223 * duration of data gathering.
225 subbuf_header_size
= config
->cb
.subbuffer_header_size();
226 v_set(config
, &buf
->offset
, subbuf_header_size
);
227 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
228 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
229 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
230 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
232 if (config
->cb
.buffer_create
) {
233 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
237 buf
->backend
.allocated
= 1;
242 /* commit_cold will be freed by shm teardown */
244 /* commit_hot will be freed by shm teardown */
250 static void switch_buffer_timer(unsigned long data
)
252 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
253 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
254 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
257 * Only flush buffers periodically if readers are active.
259 if (uatomic_read(&buf
->active_readers
))
260 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
263 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
264 // mod_timer_pinned(&buf->switch_timer,
265 // jiffies + chan->switch_timer_interval);
267 // mod_timer(&buf->switch_timer,
268 // jiffies + chan->switch_timer_interval);
272 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
273 struct lttng_ust_shm_handle
*handle
)
275 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
276 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
278 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
281 //init_timer(&buf->switch_timer);
282 //buf->switch_timer.function = switch_buffer_timer;
283 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
284 //buf->switch_timer.data = (unsigned long)buf;
285 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
286 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
288 // add_timer(&buf->switch_timer);
289 buf
->switch_timer_enabled
= 1;
292 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
293 struct lttng_ust_shm_handle
*handle
)
295 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
297 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
301 //del_timer_sync(&buf->switch_timer);
302 buf
->switch_timer_enabled
= 0;
307 * Polling timer to check the channels for data.
309 static void read_buffer_timer(unsigned long data
)
311 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
312 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
313 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
315 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
317 if (uatomic_read(&buf
->active_readers
))
318 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
320 //wake_up_interruptible(&buf->read_wait);
321 //wake_up_interruptible(&chan->read_wait);
325 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
326 // mod_timer_pinned(&buf->read_timer,
327 // jiffies + chan->read_timer_interval);
329 // mod_timer(&buf->read_timer,
330 // jiffies + chan->read_timer_interval);
334 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
335 struct lttng_ust_shm_handle
*handle
)
337 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
338 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
340 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
341 || !chan
->read_timer_interval
342 || buf
->read_timer_enabled
)
346 //init_timer(&buf->read_timer);
347 //buf->read_timer.function = read_buffer_timer;
348 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
349 //buf->read_timer.data = (unsigned long)buf;
351 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
352 // add_timer_on(&buf->read_timer, buf->backend.cpu);
354 // add_timer(&buf->read_timer);
355 buf
->read_timer_enabled
= 1;
358 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
359 struct lttng_ust_shm_handle
*handle
)
361 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
362 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
364 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
365 || !chan
->read_timer_interval
366 || !buf
->read_timer_enabled
)
370 //del_timer_sync(&buf->read_timer);
372 * do one more check to catch data that has been written in the last
375 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
377 //wake_up_interruptible(&buf->read_wait);
378 //wake_up_interruptible(&chan->read_wait);
380 buf
->read_timer_enabled
= 0;
383 static void channel_unregister_notifiers(struct channel
*chan
,
384 struct lttng_ust_shm_handle
*handle
)
386 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
389 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
390 for_each_possible_cpu(cpu
) {
391 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
393 lib_ring_buffer_stop_switch_timer(buf
, handle
);
394 lib_ring_buffer_stop_read_timer(buf
, handle
);
397 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
399 lib_ring_buffer_stop_switch_timer(buf
, handle
);
400 lib_ring_buffer_stop_read_timer(buf
, handle
);
402 //channel_backend_unregister_notifiers(&chan->backend);
405 static void channel_free(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
407 channel_backend_free(&chan
->backend
, handle
);
408 /* chan is freed by shm teardown */
409 shm_object_table_destroy(handle
->table
);
414 * channel_create - Create channel.
415 * @config: ring buffer instance configuration
416 * @name: name of the channel
417 * @priv_data: ring buffer client private data area pointer (output)
418 * @priv_data_size: length, in bytes, of the private data area.
419 * @priv_data_init: initialization data for private data.
420 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
421 * address mapping. It is used only by RING_BUFFER_STATIC
422 * configuration. It can be set to NULL for other backends.
423 * @subbuf_size: subbuffer size
424 * @num_subbuf: number of subbuffers
425 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
426 * padding to let readers get those sub-buffers.
427 * Used for live streaming.
428 * @read_timer_interval: Time interval (in us) to wake up pending readers.
431 * Returns NULL on failure.
433 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
436 size_t priv_data_align
,
437 size_t priv_data_size
,
438 void *priv_data_init
,
439 void *buf_addr
, size_t subbuf_size
,
440 size_t num_subbuf
, unsigned int switch_timer_interval
,
441 unsigned int read_timer_interval
)
444 size_t shmsize
, chansize
;
445 struct channel
*chan
;
446 struct lttng_ust_shm_handle
*handle
;
447 struct shm_object
*shmobj
;
448 unsigned int nr_streams
;
450 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
451 nr_streams
= num_possible_cpus();
455 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
456 read_timer_interval
))
459 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
463 /* Allocate table for channel + per-cpu buffers */
464 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
466 goto error_table_alloc
;
468 /* Calculate the shm allocation layout */
469 shmsize
= sizeof(struct channel
);
470 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
471 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
474 shmsize
+= offset_align(shmsize
, priv_data_align
);
475 shmsize
+= priv_data_size
;
477 /* Allocate normal memory for channel (not shared) */
478 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
);
481 /* struct channel is at object 0, offset 0 (hardcoded) */
482 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
483 assert(handle
->chan
._ref
.index
== 0);
484 assert(handle
->chan
._ref
.offset
== 0);
485 chan
= shmp(handle
, handle
->chan
);
488 chan
->nr_streams
= nr_streams
;
490 /* space for private data */
491 if (priv_data_size
) {
492 DECLARE_SHMP(void, priv_data_alloc
);
494 align_shm(shmobj
, priv_data_align
);
495 chan
->priv_data_offset
= shmobj
->allocated_len
;
496 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
497 if (!shmp(handle
, priv_data_alloc
))
499 *priv_data
= channel_get_private(chan
);
500 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
502 chan
->priv_data_offset
= -1;
507 ret
= channel_backend_init(&chan
->backend
, name
, config
,
508 subbuf_size
, num_subbuf
, handle
);
510 goto error_backend_init
;
512 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
514 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
515 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
517 //init_waitqueue_head(&chan->read_wait);
518 //init_waitqueue_head(&chan->hp_wait);
520 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
522 * In case of non-hotplug cpu, if the ring-buffer is allocated
523 * in early initcall, it will not be notified of secondary cpus.
524 * In that off case, we need to allocate for all possible cpus.
526 for_each_possible_cpu(cpu
) {
527 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
528 lib_ring_buffer_start_switch_timer(buf
, handle
);
529 lib_ring_buffer_start_read_timer(buf
, handle
);
532 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
534 lib_ring_buffer_start_switch_timer(buf
, handle
);
535 lib_ring_buffer_start_read_timer(buf
, handle
);
541 shm_object_table_destroy(handle
->table
);
547 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
548 uint64_t memory_map_size
)
550 struct lttng_ust_shm_handle
*handle
;
551 struct shm_object
*object
;
553 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
557 /* Allocate table for channel + per-cpu buffers */
558 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
560 goto error_table_alloc
;
561 /* Add channel object */
562 object
= shm_object_table_append_mem(handle
->table
, data
,
565 goto error_table_object
;
566 /* struct channel is at object 0, offset 0 (hardcoded) */
567 handle
->chan
._ref
.index
= 0;
568 handle
->chan
._ref
.offset
= 0;
572 shm_object_table_destroy(handle
->table
);
578 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
579 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
580 uint64_t memory_map_size
)
582 struct shm_object
*object
;
584 /* Add stream object */
585 object
= shm_object_table_append_shm(handle
->table
,
586 shm_fd
, wakeup_fd
, stream_nr
,
593 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
595 assert(handle
->table
);
596 return handle
->table
->allocated_len
- 1;
600 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
602 channel_free(chan
, handle
);
606 * channel_destroy - Finalize, wait for q.s. and destroy channel.
607 * @chan: channel to destroy
610 * Call "destroy" callback, finalize channels, decrement the channel
611 * reference count. Note that when readers have completed data
612 * consumption of finalized channels, get_subbuf() will return -ENODATA.
613 * They should release their handle at that point.
615 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
620 * Note: the consumer takes care of finalizing and
621 * switching the buffers.
623 channel_unregister_notifiers(chan
, handle
);
627 * sessiond/consumer are keeping a reference on the shm file
628 * descriptor directly. No need to refcount.
630 channel_release(chan
, handle
);
634 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
635 const struct lttng_ust_lib_ring_buffer_config
*config
,
636 struct channel
*chan
, int cpu
,
637 struct lttng_ust_shm_handle
*handle
,
638 int *shm_fd
, int *wait_fd
,
640 uint64_t *memory_map_size
)
644 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
647 if (cpu
>= num_possible_cpus())
650 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
651 *shm_fd
= shm_get_shm_fd(handle
, ref
);
652 *wait_fd
= shm_get_wait_fd(handle
, ref
);
653 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
654 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
656 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
659 int ring_buffer_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
660 struct channel
*chan
,
661 struct lttng_ust_shm_handle
*handle
,
666 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
669 if (cpu
>= num_possible_cpus())
672 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
673 return shm_close_wait_fd(handle
, ref
);
676 int ring_buffer_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
677 struct channel
*chan
,
678 struct lttng_ust_shm_handle
*handle
,
683 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
686 if (cpu
>= num_possible_cpus())
689 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
690 return shm_close_wakeup_fd(handle
, ref
);
693 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
694 struct lttng_ust_shm_handle
*handle
)
696 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
702 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
703 struct lttng_ust_shm_handle
*handle
)
705 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
707 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
709 uatomic_dec(&buf
->active_readers
);
713 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
715 * @consumed: consumed count indicating the position where to read
716 * @produced: produced count, indicates position when to stop reading
718 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
719 * data to read at consumed position, or 0 if the get operation succeeds.
722 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
723 unsigned long *consumed
, unsigned long *produced
,
724 struct lttng_ust_shm_handle
*handle
)
726 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
727 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
728 unsigned long consumed_cur
, write_offset
;
731 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
733 * Read finalized before counters.
736 consumed_cur
= uatomic_read(&buf
->consumed
);
738 * No need to issue a memory barrier between consumed count read and
739 * write offset read, because consumed count can only change
740 * concurrently in overwrite mode, and we keep a sequence counter
741 * identifier derived from the write offset to check we are getting
742 * the same sub-buffer we are expecting (the sub-buffers are atomically
743 * "tagged" upon writes, tags are checked upon read).
745 write_offset
= v_read(config
, &buf
->offset
);
748 * Check that we are not about to read the same subbuffer in
749 * which the writer head is.
751 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
755 *consumed
= consumed_cur
;
756 *produced
= subbuf_trunc(write_offset
, chan
);
762 * The memory barriers __wait_event()/wake_up_interruptible() take care
763 * of "raw_spin_is_locked" memory ordering.
772 * lib_ring_buffer_put_snapshot - move consumed counter forward
774 * @consumed_new: new consumed count value
776 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
777 unsigned long consumed_new
,
778 struct lttng_ust_shm_handle
*handle
)
780 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
781 struct channel
*chan
= shmp(handle
, bufb
->chan
);
782 unsigned long consumed
;
784 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
787 * Only push the consumed value forward.
788 * If the consumed cmpxchg fails, this is because we have been pushed by
789 * the writer in flight recorder mode.
791 consumed
= uatomic_read(&buf
->consumed
);
792 while ((long) consumed
- (long) consumed_new
< 0)
793 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
798 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
800 * @consumed: consumed count indicating the position where to read
802 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
803 * data to read at consumed position, or 0 if the get operation succeeds.
805 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
806 unsigned long consumed
,
807 struct lttng_ust_shm_handle
*handle
)
809 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
810 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
811 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
816 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
818 * Read finalized before counters.
821 consumed_cur
= uatomic_read(&buf
->consumed
);
822 consumed_idx
= subbuf_index(consumed
, chan
);
823 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
825 * Make sure we read the commit count before reading the buffer
826 * data and the write offset. Correct consumed offset ordering
827 * wrt commit count is insured by the use of cmpxchg to update
828 * the consumed offset.
831 * Local rmb to match the remote wmb to read the commit count
832 * before the buffer data and the write offset.
836 write_offset
= v_read(config
, &buf
->offset
);
839 * Check that the buffer we are getting is after or at consumed_cur
842 if ((long) subbuf_trunc(consumed
, chan
)
843 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
847 * Check that the subbuffer we are trying to consume has been
848 * already fully committed.
850 if (((commit_count
- chan
->backend
.subbuf_size
)
851 & chan
->commit_count_mask
)
852 - (buf_trunc(consumed_cur
, chan
)
853 >> chan
->backend
.num_subbuf_order
)
858 * Check that we are not about to read the same subbuffer in
859 * which the writer head is.
861 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
866 * Failure to get the subbuffer causes a busy-loop retry without going
867 * to a wait queue. These are caused by short-lived race windows where
868 * the writer is getting access to a subbuffer we were trying to get
869 * access to. Also checks that the "consumed" buffer count we are
870 * looking for matches the one contained in the subbuffer id.
872 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
873 consumed_idx
, buf_trunc_val(consumed
, chan
),
877 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
879 buf
->get_subbuf_consumed
= consumed
;
886 * The memory barriers __wait_event()/wake_up_interruptible() take care
887 * of "raw_spin_is_locked" memory ordering.
896 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
899 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
900 struct lttng_ust_shm_handle
*handle
)
902 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
903 struct channel
*chan
= shmp(handle
, bufb
->chan
);
904 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
905 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
907 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_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 * No need to order commit_count, write_offset and cons_offset reads
995 * because we execute at teardown when no more writer nor reader
996 * references are left.
998 write_offset
= v_read(config
, &buf
->offset
);
999 cons_offset
= uatomic_read(&buf
->consumed
);
1000 if (write_offset
!= cons_offset
)
1001 DBG("ring buffer %s, cpu %d: "
1002 "non-consumed data\n"
1003 " [ %lu bytes written, %lu bytes read ]\n",
1004 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1006 for (cons_offset
= uatomic_read(&buf
->consumed
);
1007 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1010 cons_offset
= subbuf_align(cons_offset
, chan
))
1011 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1016 void lib_ring_buffer_print_errors(struct channel
*chan
,
1017 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1018 struct lttng_ust_shm_handle
*handle
)
1020 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1021 void *priv
= channel_get_private(chan
);
1023 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1024 DBG("ring buffer %s: %lu records written, "
1025 "%lu records overrun\n",
1027 v_read(config
, &buf
->records_count
),
1028 v_read(config
, &buf
->records_overrun
));
1030 DBG("ring buffer %s, cpu %d: %lu records written, "
1031 "%lu records overrun\n",
1032 chan
->backend
.name
, cpu
,
1033 v_read(config
, &buf
->records_count
),
1034 v_read(config
, &buf
->records_overrun
));
1036 if (v_read(config
, &buf
->records_lost_full
)
1037 || v_read(config
, &buf
->records_lost_wrap
)
1038 || v_read(config
, &buf
->records_lost_big
))
1039 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1040 " [ %lu buffer full, %lu nest buffer wrap-around, "
1041 "%lu event too big ]\n",
1042 chan
->backend
.name
, cpu
,
1043 v_read(config
, &buf
->records_lost_full
),
1044 v_read(config
, &buf
->records_lost_wrap
),
1045 v_read(config
, &buf
->records_lost_big
));
1047 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1051 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1053 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1056 void lib_ring_buffer_switch_old_start(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
, chan
);
1064 unsigned long commit_count
;
1066 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1069 * Order all writes to buffer before the commit count update that will
1070 * determine that the subbuffer is full.
1073 v_add(config
, config
->cb
.subbuffer_header_size(),
1074 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1075 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1076 /* Check if the written buffer has to be delivered */
1077 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1078 commit_count
, oldidx
, handle
);
1079 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1080 offsets
->old
, commit_count
,
1081 config
->cb
.subbuffer_header_size(),
1086 * lib_ring_buffer_switch_old_end: switch old subbuffer
1088 * Note : offset_old should never be 0 here. It is ok, because we never perform
1089 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1090 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1094 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1095 struct channel
*chan
,
1096 struct switch_offsets
*offsets
,
1098 struct lttng_ust_shm_handle
*handle
)
1100 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1101 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1102 unsigned long commit_count
, padding_size
, data_size
;
1104 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1105 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1106 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1110 * Order all writes to buffer before the commit count update that will
1111 * determine that the subbuffer is full.
1114 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1115 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1116 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1117 commit_count
, oldidx
, handle
);
1118 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1119 offsets
->old
, commit_count
,
1120 padding_size
, handle
);
1124 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1126 * This code can be executed unordered : writers may already have written to the
1127 * sub-buffer before this code gets executed, caution. The commit makes sure
1128 * that this code is executed before the deliver of this sub-buffer.
1131 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1132 struct channel
*chan
,
1133 struct switch_offsets
*offsets
,
1135 struct lttng_ust_shm_handle
*handle
)
1137 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1138 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1139 unsigned long commit_count
;
1141 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1144 * Order all writes to buffer before the commit count update that will
1145 * determine that the subbuffer is full.
1148 v_add(config
, config
->cb
.subbuffer_header_size(),
1149 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1150 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1151 /* Check if the written buffer has to be delivered */
1152 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1153 commit_count
, beginidx
, handle
);
1154 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1155 offsets
->begin
, commit_count
,
1156 config
->cb
.subbuffer_header_size(),
1161 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1163 * The only remaining threads could be the ones with pending commits. They will
1164 * have to do the deliver themselves.
1167 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1168 struct channel
*chan
,
1169 struct switch_offsets
*offsets
,
1171 struct lttng_ust_shm_handle
*handle
)
1173 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1174 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1175 unsigned long commit_count
, padding_size
, data_size
;
1177 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1178 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1179 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1183 * Order all writes to buffer before the commit count update that will
1184 * determine that the subbuffer is full.
1187 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1188 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1189 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1190 commit_count
, endidx
, handle
);
1191 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1192 offsets
->end
, commit_count
,
1193 padding_size
, handle
);
1199 * !0 if execution must be aborted.
1202 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1203 struct lttng_ust_lib_ring_buffer
*buf
,
1204 struct channel
*chan
,
1205 struct switch_offsets
*offsets
,
1208 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1211 offsets
->begin
= v_read(config
, &buf
->offset
);
1212 offsets
->old
= offsets
->begin
;
1213 offsets
->switch_old_start
= 0;
1214 off
= subbuf_offset(offsets
->begin
, chan
);
1216 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1219 * Ensure we flush the header of an empty subbuffer when doing the
1220 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1221 * total data gathering duration even if there were no records saved
1222 * after the last buffer switch.
1223 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1224 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1225 * subbuffer header as appropriate.
1226 * The next record that reserves space will be responsible for
1227 * populating the following subbuffer header. We choose not to populate
1228 * the next subbuffer header here because we want to be able to use
1229 * SWITCH_ACTIVE for periodical buffer flush, which must
1230 * guarantee that all the buffer content (records and header
1231 * timestamps) are visible to the reader. This is required for
1232 * quiescence guarantees for the fusion merge.
1234 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1235 if (caa_unlikely(off
== 0)) {
1237 * A final flush that encounters an empty
1238 * sub-buffer cannot switch buffer if a
1239 * reader is located within this sub-buffer.
1240 * Anyway, the purpose of final flushing of a
1241 * sub-buffer at offset 0 is to handle the case
1242 * of entirely empty stream.
1244 if (caa_unlikely(subbuf_trunc(offsets
->begin
, chan
)
1245 - subbuf_trunc((unsigned long)
1246 uatomic_read(&buf
->consumed
), chan
)
1247 >= chan
->backend
.buf_size
))
1250 * The client does not save any header information.
1251 * Don't switch empty subbuffer on finalize, because it
1252 * is invalid to deliver a completely empty subbuffer.
1254 if (!config
->cb
.subbuffer_header_size())
1257 * Need to write the subbuffer start header on finalize.
1259 offsets
->switch_old_start
= 1;
1261 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1263 return -1; /* we do not have to switch : buffer is empty */
1264 /* Note: old points to the next subbuf at offset 0 */
1265 offsets
->end
= offsets
->begin
;
1270 * Force a sub-buffer switch. This operation is completely reentrant : can be
1271 * called while tracing is active with absolutely no lock held.
1273 * Note, however, that as a v_cmpxchg is used for some atomic
1274 * operations, this function must be called from the CPU which owns the buffer
1275 * for a ACTIVE flush.
1277 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1278 struct lttng_ust_shm_handle
*handle
)
1280 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1281 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1282 struct switch_offsets offsets
;
1283 unsigned long oldidx
;
1289 * Perform retryable operations.
1292 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1294 return; /* Switch not needed */
1295 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1299 * Atomically update last_tsc. This update races against concurrent
1300 * atomic updates, but the race will always cause supplementary full TSC
1301 * records, never the opposite (missing a full TSC record when it would
1304 save_last_tsc(config
, buf
, tsc
);
1307 * Push the reader if necessary
1309 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1311 oldidx
= subbuf_index(offsets
.old
, chan
);
1312 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1315 * May need to populate header start on SWITCH_FLUSH.
1317 if (offsets
.switch_old_start
) {
1318 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1319 offsets
.old
+= config
->cb
.subbuffer_header_size();
1323 * Switch old subbuffer.
1325 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1331 * -ENOSPC if event size is too large for packet.
1332 * -ENOBUFS if there is currently not enough space in buffer for the event.
1333 * -EIO if data cannot be written into the buffer for any other reason.
1336 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1337 struct channel
*chan
,
1338 struct switch_offsets
*offsets
,
1339 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1341 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1342 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1343 unsigned long reserve_commit_diff
;
1345 offsets
->begin
= v_read(config
, &buf
->offset
);
1346 offsets
->old
= offsets
->begin
;
1347 offsets
->switch_new_start
= 0;
1348 offsets
->switch_new_end
= 0;
1349 offsets
->switch_old_end
= 0;
1350 offsets
->pre_header_padding
= 0;
1352 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1353 if ((int64_t) ctx
->tsc
== -EIO
)
1356 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1357 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1359 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1360 offsets
->switch_new_start
= 1; /* For offsets->begin */
1362 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1364 &offsets
->pre_header_padding
,
1367 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1370 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1371 offsets
->size
> chan
->backend
.subbuf_size
)) {
1372 offsets
->switch_old_end
= 1; /* For offsets->old */
1373 offsets
->switch_new_start
= 1; /* For offsets->begin */
1376 if (caa_unlikely(offsets
->switch_new_start
)) {
1377 unsigned long sb_index
;
1380 * We are typically not filling the previous buffer completely.
1382 if (caa_likely(offsets
->switch_old_end
))
1383 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1384 offsets
->begin
= offsets
->begin
1385 + config
->cb
.subbuffer_header_size();
1386 /* Test new buffer integrity */
1387 sb_index
= subbuf_index(offsets
->begin
, chan
);
1388 reserve_commit_diff
=
1389 (buf_trunc(offsets
->begin
, chan
)
1390 >> chan
->backend
.num_subbuf_order
)
1391 - ((unsigned long) v_read(config
,
1392 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1393 & chan
->commit_count_mask
);
1394 if (caa_likely(reserve_commit_diff
== 0)) {
1395 /* Next subbuffer not being written to. */
1396 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1397 subbuf_trunc(offsets
->begin
, chan
)
1398 - subbuf_trunc((unsigned long)
1399 uatomic_read(&buf
->consumed
), chan
)
1400 >= chan
->backend
.buf_size
)) {
1401 unsigned long nr_lost
;
1404 * We do not overwrite non consumed buffers
1405 * and we are full : record is lost.
1407 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1408 v_inc(config
, &buf
->records_lost_full
);
1409 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1410 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1411 nr_lost
+ 1, chan
->backend
.name
,
1417 * Next subbuffer not being written to, and we
1418 * are either in overwrite mode or the buffer is
1419 * not full. It's safe to write in this new
1424 unsigned long nr_lost
;
1427 * Next subbuffer reserve offset does not match the
1428 * commit offset. Drop record in producer-consumer and
1429 * overwrite mode. Caused by either a writer OOPS or too
1430 * many nested writes over a reserve/commit pair.
1432 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1433 v_inc(config
, &buf
->records_lost_wrap
);
1434 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1435 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1436 nr_lost
+ 1, chan
->backend
.name
,
1442 config
->cb
.record_header_size(config
, chan
,
1444 &offsets
->pre_header_padding
,
1447 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1450 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1451 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1452 unsigned long nr_lost
;
1455 * Record too big for subbuffers, report error, don't
1456 * complete the sub-buffer switch.
1458 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1459 v_inc(config
, &buf
->records_lost_big
);
1460 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1461 DBG("%lu or more records lost in (%s:%d) record size "
1462 " of %zu bytes is too large for buffer\n",
1463 nr_lost
+ 1, chan
->backend
.name
,
1464 buf
->backend
.cpu
, offsets
->size
);
1469 * We just made a successful buffer switch and the
1470 * record fits in the new subbuffer. Let's write.
1475 * Record fits in the current buffer and we are not on a switch
1476 * boundary. It's safe to write.
1479 offsets
->end
= offsets
->begin
+ offsets
->size
;
1481 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1483 * The offset_end will fall at the very beginning of the next
1486 offsets
->switch_new_end
= 1; /* For offsets->begin */
1492 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1493 * @ctx: ring buffer context.
1495 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1496 * -EIO for other errors, else returns 0.
1497 * It will take care of sub-buffer switching.
1499 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1501 struct channel
*chan
= ctx
->chan
;
1502 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1503 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1504 struct lttng_ust_lib_ring_buffer
*buf
;
1505 struct switch_offsets offsets
;
1508 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1509 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1511 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1517 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1519 if (caa_unlikely(ret
))
1521 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1526 * Atomically update last_tsc. This update races against concurrent
1527 * atomic updates, but the race will always cause supplementary full TSC
1528 * records, never the opposite (missing a full TSC record when it would
1531 save_last_tsc(config
, buf
, ctx
->tsc
);
1534 * Push the reader if necessary
1536 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1539 * Clear noref flag for this subbuffer.
1541 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1542 subbuf_index(offsets
.end
- 1, chan
),
1546 * Switch old subbuffer if needed.
1548 if (caa_unlikely(offsets
.switch_old_end
)) {
1549 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1550 subbuf_index(offsets
.old
- 1, chan
),
1552 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1556 * Populate new subbuffer.
1558 if (caa_unlikely(offsets
.switch_new_start
))
1559 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1561 if (caa_unlikely(offsets
.switch_new_end
))
1562 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1564 ctx
->slot_size
= offsets
.size
;
1565 ctx
->pre_offset
= offsets
.begin
;
1566 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1571 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1573 void lttng_fixup_ringbuffer_tls(void)
1575 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));