2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2005-2012 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
40 #include <sys/types.h>
49 #include <urcu/compiler.h>
51 #include <urcu/tls-compat.h>
53 #include "common/macros.h"
55 #include <lttng/ust-utils.h>
56 #include <lttng/ringbuffer-context.h>
59 #include "ringbuffer-config.h"
65 #include "common/compat/errno.h" /* For ENODATA */
67 /* Print DBG() messages about events lost only every 1048576 hits */
68 #define DBG_PRINT_NR_LOST (1UL << 20)
70 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
71 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
72 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
73 #define CLOCKID CLOCK_MONOTONIC
74 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
75 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
76 #define RETRY_DELAY_MS 100 /* 100 ms. */
79 * Non-static to ensure the compiler does not optimize away the xor.
81 uint8_t lttng_crash_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
84 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
85 * close(2) to close the fd returned by shm_open.
86 * shm_unlink releases the shared memory object name.
87 * ftruncate(2) sets the size of the memory object.
88 * mmap/munmap maps the shared memory obj to a virtual address in the
89 * calling proceess (should be done both in libust and consumer).
90 * See shm_overview(7) for details.
91 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
94 * Since we don't need to access the object using its name, we can
95 * immediately shm_unlink(3) it, and only keep the handle with its file
100 * Internal structure representing offsets to use at a sub-buffer switch.
102 struct switch_offsets
{
103 unsigned long begin
, end
, old
;
104 size_t pre_header_padding
, size
;
105 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
109 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
112 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
115 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
118 void lib_ring_buffer_print_errors(struct lttng_ust_lib_ring_buffer_channel
*chan
,
119 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
120 struct lttng_ust_shm_handle
*handle
);
123 * Handle timer teardown race wrt memory free of private data by
124 * ring buffer signals are handled by a single thread, which permits
125 * a synchronization point between handling of each signal.
126 * Protected by the lock within the structure.
128 struct timer_signal_data
{
129 pthread_t tid
; /* thread id managing signals */
132 pthread_mutex_t lock
;
135 static struct timer_signal_data timer_signal
= {
139 .lock
= PTHREAD_MUTEX_INITIALIZER
,
142 static bool lttng_ust_allow_blocking
;
144 void lttng_ust_ringbuffer_set_allow_blocking(void)
146 lttng_ust_allow_blocking
= true;
149 /* Get blocking timeout, in ms */
150 static int lttng_ust_ringbuffer_get_timeout(struct lttng_ust_lib_ring_buffer_channel
*chan
)
152 if (!lttng_ust_allow_blocking
)
154 return chan
->u
.s
.blocking_timeout_ms
;
158 * lib_ring_buffer_reset - Reset ring buffer to initial values.
161 * Effectively empty the ring buffer. Should be called when the buffer is not
162 * used for writing. The ring buffer can be opened for reading, but the reader
163 * should not be using the iterator concurrently with reset. The previous
164 * current iterator record is reset.
166 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
167 struct lttng_ust_shm_handle
*handle
)
169 struct lttng_ust_lib_ring_buffer_channel
*chan
;
170 const struct lttng_ust_lib_ring_buffer_config
*config
;
173 chan
= shmp(handle
, buf
->backend
.chan
);
176 config
= &chan
->backend
.config
;
178 * Reset iterator first. It will put the subbuffer if it currently holds
181 v_set(config
, &buf
->offset
, 0);
182 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
183 struct commit_counters_hot
*cc_hot
;
184 struct commit_counters_cold
*cc_cold
;
187 cc_hot
= shmp_index(handle
, buf
->commit_hot
, i
);
190 cc_cold
= shmp_index(handle
, buf
->commit_cold
, i
);
193 ts_end
= shmp_index(handle
, buf
->ts_end
, i
);
196 v_set(config
, &cc_hot
->cc
, 0);
197 v_set(config
, &cc_hot
->seq
, 0);
198 v_set(config
, &cc_cold
->cc_sb
, 0);
201 uatomic_set(&buf
->consumed
, 0);
202 uatomic_set(&buf
->record_disabled
, 0);
203 v_set(config
, &buf
->last_tsc
, 0);
204 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
205 /* Don't reset number of active readers */
206 v_set(config
, &buf
->records_lost_full
, 0);
207 v_set(config
, &buf
->records_lost_wrap
, 0);
208 v_set(config
, &buf
->records_lost_big
, 0);
209 v_set(config
, &buf
->records_count
, 0);
210 v_set(config
, &buf
->records_overrun
, 0);
215 * channel_reset - Reset channel to initial values.
218 * Effectively empty the channel. Should be called when the channel is not used
219 * for writing. The channel can be opened for reading, but the reader should not
220 * be using the iterator concurrently with reset. The previous current iterator
223 void channel_reset(struct lttng_ust_lib_ring_buffer_channel
*chan
)
226 * Reset iterators first. Will put the subbuffer if held for reading.
228 uatomic_set(&chan
->record_disabled
, 0);
229 /* Don't reset commit_count_mask, still valid */
230 channel_backend_reset(&chan
->backend
);
231 /* Don't reset switch/read timer interval */
232 /* Don't reset notifiers and notifier enable bits */
233 /* Don't reset reader reference count */
237 void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config
*config
,
238 struct lttng_crash_abi
*crash_abi
,
239 struct lttng_ust_lib_ring_buffer
*buf
,
240 struct channel_backend
*chanb
,
241 struct shm_object
*shmobj
,
242 struct lttng_ust_shm_handle
*handle
)
246 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
247 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
248 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
249 crash_abi
->endian
= RB_CRASH_ENDIAN
;
250 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
251 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
252 crash_abi
->word_size
= sizeof(unsigned long);
253 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
255 /* Offset of fields */
256 crash_abi
->offset
.prod_offset
=
257 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
258 crash_abi
->offset
.consumed_offset
=
259 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
260 crash_abi
->offset
.commit_hot_array
=
261 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
262 crash_abi
->offset
.commit_hot_seq
=
263 offsetof(struct commit_counters_hot
, seq
);
264 crash_abi
->offset
.buf_wsb_array
=
265 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
266 crash_abi
->offset
.buf_wsb_id
=
267 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
, id
);
268 crash_abi
->offset
.sb_array
=
269 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
270 crash_abi
->offset
.sb_array_shmp_offset
=
271 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
,
273 crash_abi
->offset
.sb_backend_p_offset
=
274 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages
,
278 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
279 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
280 crash_abi
->length
.commit_hot_seq
=
281 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
282 crash_abi
->length
.buf_wsb_id
=
283 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer
*) NULL
)->id
);
284 crash_abi
->length
.sb_array_shmp_offset
=
285 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
286 crash_abi
->length
.sb_backend_p_offset
=
287 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
290 crash_abi
->stride
.commit_hot_array
=
291 sizeof(struct commit_counters_hot
);
292 crash_abi
->stride
.buf_wsb_array
=
293 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
);
294 crash_abi
->stride
.sb_array
=
295 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
);
297 /* Buffer constants */
298 crash_abi
->buf_size
= chanb
->buf_size
;
299 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
300 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
301 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
303 if (config
->cb
.content_size_field
) {
304 size_t offset
, length
;
306 config
->cb
.content_size_field(config
, &offset
, &length
);
307 crash_abi
->offset
.content_size
= offset
;
308 crash_abi
->length
.content_size
= length
;
310 crash_abi
->offset
.content_size
= 0;
311 crash_abi
->length
.content_size
= 0;
313 if (config
->cb
.packet_size_field
) {
314 size_t offset
, length
;
316 config
->cb
.packet_size_field(config
, &offset
, &length
);
317 crash_abi
->offset
.packet_size
= offset
;
318 crash_abi
->length
.packet_size
= length
;
320 crash_abi
->offset
.packet_size
= 0;
321 crash_abi
->length
.packet_size
= 0;
326 * Must be called under cpu hotplug protection.
328 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
329 struct channel_backend
*chanb
, int cpu
,
330 struct lttng_ust_shm_handle
*handle
,
331 struct shm_object
*shmobj
)
333 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
334 struct lttng_ust_lib_ring_buffer_channel
*chan
= caa_container_of(chanb
,
335 struct lttng_ust_lib_ring_buffer_channel
, backend
);
336 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
337 struct lttng_ust_lib_ring_buffer_channel
*shmp_chan
;
338 struct commit_counters_hot
*cc_hot
;
339 void *priv
= channel_get_private_config(chan
);
340 size_t subbuf_header_size
;
344 /* Test for cpu hotplug */
345 if (buf
->backend
.allocated
)
348 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
349 set_shmp(buf
->commit_hot
,
351 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
352 if (!shmp(handle
, buf
->commit_hot
)) {
356 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
357 set_shmp(buf
->commit_cold
,
359 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
360 if (!shmp(handle
, buf
->commit_cold
)) {
365 align_shm(shmobj
, __alignof__(uint64_t));
366 set_shmp(buf
->ts_end
,
368 sizeof(uint64_t) * chan
->backend
.num_subbuf
));
369 if (!shmp(handle
, buf
->ts_end
)) {
371 goto free_commit_cold
;
375 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
376 cpu
, handle
, shmobj
);
382 * Write the subbuffer header for first subbuffer so we know the total
383 * duration of data gathering.
385 subbuf_header_size
= config
->cb
.subbuffer_header_size();
386 v_set(config
, &buf
->offset
, subbuf_header_size
);
387 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
392 subbuffer_id_clear_noref(config
, &wsb
->id
);
393 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
398 tsc
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
399 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
400 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
405 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
406 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
408 if (config
->cb
.buffer_create
) {
409 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
414 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
416 buf
->backend
.allocated
= 1;
421 /* ts_end will be freed by shm teardown */
423 /* commit_cold will be freed by shm teardown */
425 /* commit_hot will be freed by shm teardown */
431 void lib_ring_buffer_channel_switch_timer(int sig
__attribute__((unused
)),
432 siginfo_t
*si
, void *uc
__attribute__((unused
)))
434 const struct lttng_ust_lib_ring_buffer_config
*config
;
435 struct lttng_ust_shm_handle
*handle
;
436 struct lttng_ust_lib_ring_buffer_channel
*chan
;
439 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
441 chan
= si
->si_value
.sival_ptr
;
442 handle
= chan
->handle
;
443 config
= &chan
->backend
.config
;
445 DBG("Switch timer for channel %p\n", chan
);
448 * Only flush buffers periodically if readers are active.
450 pthread_mutex_lock(&wakeup_fd_mutex
);
451 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
452 for_each_possible_cpu(cpu
) {
453 struct lttng_ust_lib_ring_buffer
*buf
=
454 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
458 if (uatomic_read(&buf
->active_readers
))
459 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
463 struct lttng_ust_lib_ring_buffer
*buf
=
464 shmp(handle
, chan
->backend
.buf
[0].shmp
);
468 if (uatomic_read(&buf
->active_readers
))
469 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
473 pthread_mutex_unlock(&wakeup_fd_mutex
);
478 int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
479 struct lttng_ust_lib_ring_buffer
*buf
,
480 struct lttng_ust_lib_ring_buffer_channel
*chan
,
481 struct lttng_ust_shm_handle
*handle
)
483 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
484 struct commit_counters_cold
*cc_cold
;
486 consumed_old
= uatomic_read(&buf
->consumed
);
487 consumed_idx
= subbuf_index(consumed_old
, chan
);
488 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
491 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
493 * No memory barrier here, since we are only interested
494 * in a statistically correct polling result. The next poll will
495 * get the data is we are racing. The mb() that ensures correct
496 * memory order is in get_subbuf.
498 write_offset
= v_read(config
, &buf
->offset
);
501 * Check that the subbuffer we are trying to consume has been
502 * already fully committed.
505 if (((commit_count
- chan
->backend
.subbuf_size
)
506 & chan
->commit_count_mask
)
507 - (buf_trunc(consumed_old
, chan
)
508 >> chan
->backend
.num_subbuf_order
)
513 * Check that we are not about to read the same subbuffer in
514 * which the writer head is.
516 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
524 void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer
*buf
,
525 struct lttng_ust_shm_handle
*handle
)
527 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
528 sigset_t sigpipe_set
, pending_set
, old_set
;
529 int ret
, sigpipe_was_pending
= 0;
535 * Wake-up the other end by writing a null byte in the pipe
536 * (non-blocking). Important note: Because writing into the
537 * pipe is non-blocking (and therefore we allow dropping wakeup
538 * data, as long as there is wakeup data present in the pipe
539 * buffer to wake up the consumer), the consumer should perform
540 * the following sequence for waiting:
541 * 1) empty the pipe (reads).
542 * 2) check if there is data in the buffer.
543 * 3) wait on the pipe (poll).
545 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
546 * that might be already pending. If a bogus SIGPIPE is sent to
547 * the entire process concurrently by a malicious user, it may
548 * be simply discarded.
550 ret
= sigemptyset(&pending_set
);
553 * sigpending returns the mask of signals that are _both_
554 * blocked for the thread _and_ pending for either the thread or
555 * the entire process.
557 ret
= sigpending(&pending_set
);
559 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
561 * If sigpipe was pending, it means it was already blocked, so
562 * no need to block it.
564 if (!sigpipe_was_pending
) {
565 ret
= sigemptyset(&sigpipe_set
);
567 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
569 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
573 ret
= write(wakeup_fd
, "", 1);
574 } while (ret
== -1L && errno
== EINTR
);
575 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
576 struct timespec timeout
= { 0, 0 };
578 ret
= sigtimedwait(&sigpipe_set
, NULL
,
580 } while (ret
== -1L && errno
== EINTR
);
582 if (!sigpipe_was_pending
) {
583 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
589 void lib_ring_buffer_channel_do_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
591 const struct lttng_ust_lib_ring_buffer_config
*config
;
592 struct lttng_ust_shm_handle
*handle
;
595 handle
= chan
->handle
;
596 config
= &chan
->backend
.config
;
599 * Only flush buffers periodically if readers are active.
601 pthread_mutex_lock(&wakeup_fd_mutex
);
602 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
603 for_each_possible_cpu(cpu
) {
604 struct lttng_ust_lib_ring_buffer
*buf
=
605 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
609 if (uatomic_read(&buf
->active_readers
)
610 && lib_ring_buffer_poll_deliver(config
, buf
,
612 lib_ring_buffer_wakeup(buf
, handle
);
616 struct lttng_ust_lib_ring_buffer
*buf
=
617 shmp(handle
, chan
->backend
.buf
[0].shmp
);
621 if (uatomic_read(&buf
->active_readers
)
622 && lib_ring_buffer_poll_deliver(config
, buf
,
624 lib_ring_buffer_wakeup(buf
, handle
);
628 pthread_mutex_unlock(&wakeup_fd_mutex
);
632 void lib_ring_buffer_channel_read_timer(int sig
__attribute__((unused
)),
633 siginfo_t
*si
, void *uc
__attribute__((unused
)))
635 struct lttng_ust_lib_ring_buffer_channel
*chan
;
637 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
638 chan
= si
->si_value
.sival_ptr
;
639 DBG("Read timer for channel %p\n", chan
);
640 lib_ring_buffer_channel_do_read(chan
);
645 void rb_setmask(sigset_t
*mask
)
649 ret
= sigemptyset(mask
);
651 PERROR("sigemptyset");
653 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
657 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
661 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
668 void *sig_thread(void *arg
__attribute__((unused
)))
674 /* Only self thread will receive signal mask. */
676 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
679 signr
= sigwaitinfo(&mask
, &info
);
682 PERROR("sigwaitinfo");
685 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
686 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
688 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
689 lib_ring_buffer_channel_read_timer(info
.si_signo
,
691 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
693 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
696 ERR("Unexptected signal %d\n", info
.si_signo
);
703 * Ensure only a single thread listens on the timer signal.
706 void lib_ring_buffer_setup_timer_thread(void)
711 pthread_mutex_lock(&timer_signal
.lock
);
712 if (timer_signal
.setup_done
)
715 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
718 PERROR("pthread_create");
720 ret
= pthread_detach(thread
);
723 PERROR("pthread_detach");
725 timer_signal
.setup_done
= 1;
727 pthread_mutex_unlock(&timer_signal
.lock
);
731 * Wait for signal-handling thread quiescent state.
734 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
736 sigset_t pending_set
;
740 * We need to be the only thread interacting with the thread
741 * that manages signals for teardown synchronization.
743 pthread_mutex_lock(&timer_signal
.lock
);
746 * Ensure we don't have any signal queued for this channel.
749 ret
= sigemptyset(&pending_set
);
751 PERROR("sigemptyset");
753 ret
= sigpending(&pending_set
);
755 PERROR("sigpending");
757 if (!sigismember(&pending_set
, signr
))
763 * From this point, no new signal handler will be fired that
764 * would try to access "chan". However, we still need to wait
765 * for any currently executing handler to complete.
768 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
772 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
775 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
777 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
781 pthread_mutex_unlock(&timer_signal
.lock
);
785 void lib_ring_buffer_channel_switch_timer_start(struct lttng_ust_lib_ring_buffer_channel
*chan
)
788 struct itimerspec its
;
791 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
794 chan
->switch_timer_enabled
= 1;
796 lib_ring_buffer_setup_timer_thread();
798 memset(&sev
, 0, sizeof(sev
));
799 sev
.sigev_notify
= SIGEV_SIGNAL
;
800 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
801 sev
.sigev_value
.sival_ptr
= chan
;
802 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
804 PERROR("timer_create");
807 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
808 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
809 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
810 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
812 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
814 PERROR("timer_settime");
819 void lib_ring_buffer_channel_switch_timer_stop(struct lttng_ust_lib_ring_buffer_channel
*chan
)
823 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
826 ret
= timer_delete(chan
->switch_timer
);
828 PERROR("timer_delete");
831 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
833 chan
->switch_timer
= 0;
834 chan
->switch_timer_enabled
= 0;
838 void lib_ring_buffer_channel_read_timer_start(struct lttng_ust_lib_ring_buffer_channel
*chan
)
840 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
842 struct itimerspec its
;
845 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
846 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
849 chan
->read_timer_enabled
= 1;
851 lib_ring_buffer_setup_timer_thread();
853 sev
.sigev_notify
= SIGEV_SIGNAL
;
854 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
855 sev
.sigev_value
.sival_ptr
= chan
;
856 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
858 PERROR("timer_create");
861 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
862 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
863 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
864 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
866 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
868 PERROR("timer_settime");
873 void lib_ring_buffer_channel_read_timer_stop(struct lttng_ust_lib_ring_buffer_channel
*chan
)
875 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
878 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
879 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
882 ret
= timer_delete(chan
->read_timer
);
884 PERROR("timer_delete");
888 * do one more check to catch data that has been written in the last
891 lib_ring_buffer_channel_do_read(chan
);
893 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
895 chan
->read_timer
= 0;
896 chan
->read_timer_enabled
= 0;
899 static void channel_unregister_notifiers(struct lttng_ust_lib_ring_buffer_channel
*chan
,
900 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
902 lib_ring_buffer_channel_switch_timer_stop(chan
);
903 lib_ring_buffer_channel_read_timer_stop(chan
);
906 static void channel_print_errors(struct lttng_ust_lib_ring_buffer_channel
*chan
,
907 struct lttng_ust_shm_handle
*handle
)
909 const struct lttng_ust_lib_ring_buffer_config
*config
=
910 &chan
->backend
.config
;
913 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
914 for_each_possible_cpu(cpu
) {
915 struct lttng_ust_lib_ring_buffer
*buf
=
916 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
918 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
921 struct lttng_ust_lib_ring_buffer
*buf
=
922 shmp(handle
, chan
->backend
.buf
[0].shmp
);
925 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
929 static void channel_free(struct lttng_ust_lib_ring_buffer_channel
*chan
,
930 struct lttng_ust_shm_handle
*handle
,
933 channel_backend_free(&chan
->backend
, handle
);
934 /* chan is freed by shm teardown */
935 shm_object_table_destroy(handle
->table
, consumer
);
940 * channel_create - Create channel.
941 * @config: ring buffer instance configuration
942 * @name: name of the channel
943 * @priv_data_align: alignment, in bytes, of the private data area. (config)
944 * @priv_data_size: length, in bytes, of the private data area. (config)
945 * @priv_data_init: initialization data for private data. (config)
946 * @priv: local private data (memory owner by caller)
947 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
948 * address mapping. It is used only by RING_BUFFER_STATIC
949 * configuration. It can be set to NULL for other backends.
950 * @subbuf_size: subbuffer size
951 * @num_subbuf: number of subbuffers
952 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
953 * padding to let readers get those sub-buffers.
954 * Used for live streaming.
955 * @read_timer_interval: Time interval (in us) to wake up pending readers.
956 * @stream_fds: array of stream file descriptors.
957 * @nr_stream_fds: number of file descriptors in array.
960 * Returns NULL on failure.
962 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
964 size_t priv_data_align
,
965 size_t priv_data_size
,
966 void *priv_data_init
,
968 void *buf_addr
__attribute__((unused
)), size_t subbuf_size
,
969 size_t num_subbuf
, unsigned int switch_timer_interval
,
970 unsigned int read_timer_interval
,
971 const int *stream_fds
, int nr_stream_fds
,
972 int64_t blocking_timeout
)
975 size_t shmsize
, chansize
;
976 struct lttng_ust_lib_ring_buffer_channel
*chan
;
977 struct lttng_ust_shm_handle
*handle
;
978 struct shm_object
*shmobj
;
979 unsigned int nr_streams
;
980 int64_t blocking_timeout_ms
;
982 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
983 nr_streams
= num_possible_cpus();
987 if (nr_stream_fds
!= nr_streams
)
990 if (blocking_timeout
< -1) {
994 if (blocking_timeout
== -1) {
995 blocking_timeout_ms
= -1;
997 blocking_timeout_ms
= blocking_timeout
/ 1000;
998 if (blocking_timeout_ms
!= (int32_t) blocking_timeout_ms
) {
1003 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
1004 read_timer_interval
))
1007 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1011 /* Allocate table for channel + per-cpu buffers */
1012 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1014 goto error_table_alloc
;
1016 /* Calculate the shm allocation layout */
1017 shmsize
= sizeof(struct lttng_ust_lib_ring_buffer_channel
);
1018 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
1019 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
1021 if (priv_data_align
)
1022 shmsize
+= lttng_ust_offset_align(shmsize
, priv_data_align
);
1023 shmsize
+= priv_data_size
;
1025 /* Allocate normal memory for channel (not shared) */
1026 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
1030 /* struct lttng_ust_lib_ring_buffer_channel is at object 0, offset 0 (hardcoded) */
1031 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
1032 assert(handle
->chan
._ref
.index
== 0);
1033 assert(handle
->chan
._ref
.offset
== 0);
1034 chan
= shmp(handle
, handle
->chan
);
1037 chan
->nr_streams
= nr_streams
;
1039 /* space for private data */
1040 if (priv_data_size
) {
1043 DECLARE_SHMP(void, priv_data_alloc
);
1045 align_shm(shmobj
, priv_data_align
);
1046 chan
->priv_data_offset
= shmobj
->allocated_len
;
1047 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1048 if (!shmp(handle
, priv_data_alloc
))
1050 priv_config
= channel_get_private_config(chan
);
1051 memcpy(priv_config
, priv_data_init
, priv_data_size
);
1053 chan
->priv_data_offset
= -1;
1056 chan
->u
.s
.blocking_timeout_ms
= (int32_t) blocking_timeout_ms
;
1058 channel_set_private(chan
, priv
);
1060 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1061 subbuf_size
, num_subbuf
, handle
,
1064 goto error_backend_init
;
1066 chan
->handle
= handle
;
1067 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1069 chan
->switch_timer_interval
= switch_timer_interval
;
1070 chan
->read_timer_interval
= read_timer_interval
;
1071 lib_ring_buffer_channel_switch_timer_start(chan
);
1072 lib_ring_buffer_channel_read_timer_start(chan
);
1078 shm_object_table_destroy(handle
->table
, 1);
1084 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1085 uint64_t memory_map_size
,
1088 struct lttng_ust_shm_handle
*handle
;
1089 struct shm_object
*object
;
1091 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1095 /* Allocate table for channel + per-cpu buffers */
1096 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1098 goto error_table_alloc
;
1099 /* Add channel object */
1100 object
= shm_object_table_append_mem(handle
->table
, data
,
1101 memory_map_size
, wakeup_fd
);
1103 goto error_table_object
;
1104 /* struct lttng_ust_lib_ring_buffer_channel is at object 0, offset 0 (hardcoded) */
1105 handle
->chan
._ref
.index
= 0;
1106 handle
->chan
._ref
.offset
= 0;
1110 shm_object_table_destroy(handle
->table
, 0);
1116 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1117 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1118 uint64_t memory_map_size
)
1120 struct shm_object
*object
;
1122 /* Add stream object */
1123 object
= shm_object_table_append_shm(handle
->table
,
1124 shm_fd
, wakeup_fd
, stream_nr
,
1131 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1133 assert(handle
->table
);
1134 return handle
->table
->allocated_len
- 1;
1138 void channel_release(struct lttng_ust_lib_ring_buffer_channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1141 channel_free(chan
, handle
, consumer
);
1145 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1146 * @chan: channel to destroy
1148 * Holds cpu hotplug.
1149 * Call "destroy" callback, finalize channels, decrement the channel
1150 * reference count. Note that when readers have completed data
1151 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1152 * They should release their handle at that point.
1154 void channel_destroy(struct lttng_ust_lib_ring_buffer_channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1159 * Note: the consumer takes care of finalizing and
1160 * switching the buffers.
1162 channel_unregister_notifiers(chan
, handle
);
1164 * The consumer prints errors.
1166 channel_print_errors(chan
, handle
);
1170 * sessiond/consumer are keeping a reference on the shm file
1171 * descriptor directly. No need to refcount.
1173 channel_release(chan
, handle
, consumer
);
1177 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
1178 const struct lttng_ust_lib_ring_buffer_config
*config
,
1179 struct lttng_ust_lib_ring_buffer_channel
*chan
, int cpu
,
1180 struct lttng_ust_shm_handle
*handle
,
1181 int *shm_fd
, int *wait_fd
,
1183 uint64_t *memory_map_size
)
1185 struct shm_ref
*ref
;
1187 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1190 if (cpu
>= num_possible_cpus())
1193 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1194 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1195 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1196 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1197 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1199 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1202 int ring_buffer_channel_close_wait_fd(
1203 const struct lttng_ust_lib_ring_buffer_config
*config
__attribute__((unused
)),
1204 struct lttng_ust_lib_ring_buffer_channel
*chan
__attribute__((unused
)),
1205 struct lttng_ust_shm_handle
*handle
)
1207 struct shm_ref
*ref
;
1209 ref
= &handle
->chan
._ref
;
1210 return shm_close_wait_fd(handle
, ref
);
1213 int ring_buffer_channel_close_wakeup_fd(
1214 const struct lttng_ust_lib_ring_buffer_config
*config
__attribute__((unused
)),
1215 struct lttng_ust_lib_ring_buffer_channel
*chan
__attribute__((unused
)),
1216 struct lttng_ust_shm_handle
*handle
)
1218 struct shm_ref
*ref
;
1220 ref
= &handle
->chan
._ref
;
1221 return shm_close_wakeup_fd(handle
, ref
);
1224 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1225 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1226 struct lttng_ust_shm_handle
*handle
,
1229 struct shm_ref
*ref
;
1231 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1234 if (cpu
>= num_possible_cpus())
1237 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1238 return shm_close_wait_fd(handle
, ref
);
1241 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1242 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1243 struct lttng_ust_shm_handle
*handle
,
1246 struct shm_ref
*ref
;
1249 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1252 if (cpu
>= num_possible_cpus())
1255 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1256 pthread_mutex_lock(&wakeup_fd_mutex
);
1257 ret
= shm_close_wakeup_fd(handle
, ref
);
1258 pthread_mutex_unlock(&wakeup_fd_mutex
);
1262 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
1263 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
1265 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1271 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
1272 struct lttng_ust_shm_handle
*handle
)
1274 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1278 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1280 uatomic_dec(&buf
->active_readers
);
1284 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1286 * @consumed: consumed count indicating the position where to read
1287 * @produced: produced count, indicates position when to stop reading
1289 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1290 * data to read at consumed position, or 0 if the get operation succeeds.
1293 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1294 unsigned long *consumed
, unsigned long *produced
,
1295 struct lttng_ust_shm_handle
*handle
)
1297 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1298 const struct lttng_ust_lib_ring_buffer_config
*config
;
1299 unsigned long consumed_cur
, write_offset
;
1302 chan
= shmp(handle
, buf
->backend
.chan
);
1305 config
= &chan
->backend
.config
;
1306 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1308 * Read finalized before counters.
1311 consumed_cur
= uatomic_read(&buf
->consumed
);
1313 * No need to issue a memory barrier between consumed count read and
1314 * write offset read, because consumed count can only change
1315 * concurrently in overwrite mode, and we keep a sequence counter
1316 * identifier derived from the write offset to check we are getting
1317 * the same sub-buffer we are expecting (the sub-buffers are atomically
1318 * "tagged" upon writes, tags are checked upon read).
1320 write_offset
= v_read(config
, &buf
->offset
);
1323 * Check that we are not about to read the same subbuffer in
1324 * which the writer head is.
1326 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1330 *consumed
= consumed_cur
;
1331 *produced
= subbuf_trunc(write_offset
, chan
);
1337 * The memory barriers __wait_event()/wake_up_interruptible() take care
1338 * of "raw_spin_is_locked" memory ordering.
1347 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1348 * are saved regardless of whether the consumed and produced positions are
1349 * in the same subbuffer.
1351 * @consumed: consumed byte count indicating the last position read
1352 * @produced: produced byte count indicating the last position written
1354 * This function is meant to provide information on the exact producer and
1355 * consumer positions without regard for the "snapshot" feature.
1357 int lib_ring_buffer_snapshot_sample_positions(
1358 struct lttng_ust_lib_ring_buffer
*buf
,
1359 unsigned long *consumed
, unsigned long *produced
,
1360 struct lttng_ust_shm_handle
*handle
)
1362 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1363 const struct lttng_ust_lib_ring_buffer_config
*config
;
1365 chan
= shmp(handle
, buf
->backend
.chan
);
1368 config
= &chan
->backend
.config
;
1370 *consumed
= uatomic_read(&buf
->consumed
);
1372 * No need to issue a memory barrier between consumed count read and
1373 * write offset read, because consumed count can only change
1374 * concurrently in overwrite mode, and we keep a sequence counter
1375 * identifier derived from the write offset to check we are getting
1376 * the same sub-buffer we are expecting (the sub-buffers are atomically
1377 * "tagged" upon writes, tags are checked upon read).
1379 *produced
= v_read(config
, &buf
->offset
);
1384 * lib_ring_buffer_move_consumer - move consumed counter forward
1386 * @consumed_new: new consumed count value
1388 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1389 unsigned long consumed_new
,
1390 struct lttng_ust_shm_handle
*handle
)
1392 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1393 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1394 unsigned long consumed
;
1396 chan
= shmp(handle
, bufb
->chan
);
1399 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1402 * Only push the consumed value forward.
1403 * If the consumed cmpxchg fails, this is because we have been pushed by
1404 * the writer in flight recorder mode.
1406 consumed
= uatomic_read(&buf
->consumed
);
1407 while ((long) consumed
- (long) consumed_new
< 0)
1408 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1413 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1415 * @consumed: consumed count indicating the position where to read
1417 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1418 * data to read at consumed position, or 0 if the get operation succeeds.
1420 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1421 unsigned long consumed
,
1422 struct lttng_ust_shm_handle
*handle
)
1424 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1425 const struct lttng_ust_lib_ring_buffer_config
*config
;
1426 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1427 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1428 struct commit_counters_cold
*cc_cold
;
1430 chan
= shmp(handle
, buf
->backend
.chan
);
1433 config
= &chan
->backend
.config
;
1435 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1437 * Read finalized before counters.
1440 consumed_cur
= uatomic_read(&buf
->consumed
);
1441 consumed_idx
= subbuf_index(consumed
, chan
);
1442 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1445 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1447 * Make sure we read the commit count before reading the buffer
1448 * data and the write offset. Correct consumed offset ordering
1449 * wrt commit count is insured by the use of cmpxchg to update
1450 * the consumed offset.
1453 * Local rmb to match the remote wmb to read the commit count
1454 * before the buffer data and the write offset.
1458 write_offset
= v_read(config
, &buf
->offset
);
1461 * Check that the buffer we are getting is after or at consumed_cur
1464 if ((long) subbuf_trunc(consumed
, chan
)
1465 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1469 * Check that the subbuffer we are trying to consume has been
1470 * already fully committed. There are a few causes that can make
1471 * this unavailability situation occur:
1473 * Temporary (short-term) situation:
1474 * - Application is running on a different CPU, between reserve
1475 * and commit ring buffer operations,
1476 * - Application is preempted between reserve and commit ring
1477 * buffer operations,
1479 * Long-term situation:
1480 * - Application is stopped (SIGSTOP) between reserve and commit
1481 * ring buffer operations. Could eventually be resumed by
1483 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1484 * reserve and commit ring buffer operation.
1486 * From a consumer perspective, handling short-term
1487 * unavailability situations is performed by retrying a few
1488 * times after a delay. Handling long-term unavailability
1489 * situations is handled by failing to get the sub-buffer.
1491 * In all of those situations, if the application is taking a
1492 * long time to perform its commit after ring buffer space
1493 * reservation, we can end up in a situation where the producer
1494 * will fill the ring buffer and try to write into the same
1495 * sub-buffer again (which has a missing commit). This is
1496 * handled by the producer in the sub-buffer switch handling
1497 * code of the reserve routine by detecting unbalanced
1498 * reserve/commit counters and discarding all further events
1499 * until the situation is resolved in those situations. Two
1500 * scenarios can occur:
1502 * 1) The application causing the reserve/commit counters to be
1503 * unbalanced has been terminated. In this situation, all
1504 * further events will be discarded in the buffers, and no
1505 * further buffer data will be readable by the consumer
1506 * daemon. Tearing down the UST tracing session and starting
1507 * anew is a work-around for those situations. Note that this
1508 * only affects per-UID tracing. In per-PID tracing, the
1509 * application vanishes with the termination, and therefore
1510 * no more data needs to be written to the buffers.
1511 * 2) The application causing the unbalance has been delayed for
1512 * a long time, but will eventually try to increment the
1513 * commit counter after eventually writing to the sub-buffer.
1514 * This situation can cause events to be discarded until the
1515 * application resumes its operations.
1517 if (((commit_count
- chan
->backend
.subbuf_size
)
1518 & chan
->commit_count_mask
)
1519 - (buf_trunc(consumed
, chan
)
1520 >> chan
->backend
.num_subbuf_order
)
1522 if (nr_retry
-- > 0) {
1523 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1524 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1532 * Check that we are not about to read the same subbuffer in
1533 * which the writer head is.
1535 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1540 * Failure to get the subbuffer causes a busy-loop retry without going
1541 * to a wait queue. These are caused by short-lived race windows where
1542 * the writer is getting access to a subbuffer we were trying to get
1543 * access to. Also checks that the "consumed" buffer count we are
1544 * looking for matches the one contained in the subbuffer id.
1546 * The short-lived race window described here can be affected by
1547 * application signals and preemption, thus requiring to bound
1548 * the loop to a maximum number of retry.
1550 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1551 consumed_idx
, buf_trunc_val(consumed
, chan
),
1554 if (nr_retry
-- > 0) {
1555 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1556 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1562 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1564 buf
->get_subbuf_consumed
= consumed
;
1565 buf
->get_subbuf
= 1;
1571 * The memory barriers __wait_event()/wake_up_interruptible() take care
1572 * of "raw_spin_is_locked" memory ordering.
1581 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1584 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1585 struct lttng_ust_shm_handle
*handle
)
1587 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1588 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1589 const struct lttng_ust_lib_ring_buffer_config
*config
;
1590 unsigned long sb_bindex
, consumed_idx
, consumed
;
1591 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
1592 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
1594 chan
= shmp(handle
, bufb
->chan
);
1597 config
= &chan
->backend
.config
;
1598 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1600 if (!buf
->get_subbuf
) {
1602 * Reader puts a subbuffer it did not get.
1604 CHAN_WARN_ON(chan
, 1);
1607 consumed
= buf
->get_subbuf_consumed
;
1608 buf
->get_subbuf
= 0;
1611 * Clear the records_unread counter. (overruns counter)
1612 * Can still be non-zero if a file reader simply grabbed the data
1613 * without using iterators.
1614 * Can be below zero if an iterator is used on a snapshot more than
1617 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1618 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1621 backend_pages
= shmp(handle
, rpages
->shmp
);
1624 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1625 &bufb
->records_read
);
1626 v_set(config
, &backend_pages
->records_unread
, 0);
1627 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1628 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1629 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1632 * Exchange the reader subbuffer with the one we put in its place in the
1633 * writer subbuffer table. Expect the original consumed count. If
1634 * update_read_sb_index fails, this is because the writer updated the
1635 * subbuffer concurrently. We should therefore keep the subbuffer we
1636 * currently have: it has become invalid to try reading this sub-buffer
1637 * consumed count value anyway.
1639 consumed_idx
= subbuf_index(consumed
, chan
);
1640 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1641 consumed_idx
, buf_trunc_val(consumed
, chan
),
1644 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1645 * if the writer concurrently updated it.
1650 * cons_offset is an iterator on all subbuffer offsets between the reader
1651 * position and the writer position. (inclusive)
1654 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1655 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1656 unsigned long cons_offset
,
1658 struct lttng_ust_shm_handle
*handle
)
1660 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1661 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1662 struct commit_counters_hot
*cc_hot
;
1663 struct commit_counters_cold
*cc_cold
;
1665 cons_idx
= subbuf_index(cons_offset
, chan
);
1666 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1669 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1672 commit_count
= v_read(config
, &cc_hot
->cc
);
1673 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1675 if (subbuf_offset(commit_count
, chan
) != 0)
1676 DBG("ring buffer %s, cpu %d: "
1677 "commit count in subbuffer %lu,\n"
1678 "expecting multiples of %lu bytes\n"
1679 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1680 chan
->backend
.name
, cpu
, cons_idx
,
1681 chan
->backend
.subbuf_size
,
1682 commit_count
, commit_count_sb
);
1684 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1685 chan
->backend
.name
, cpu
, commit_count
);
1689 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1690 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1691 int cpu
, struct lttng_ust_shm_handle
*handle
)
1693 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1694 unsigned long write_offset
, cons_offset
;
1697 * No need to order commit_count, write_offset and cons_offset reads
1698 * because we execute at teardown when no more writer nor reader
1699 * references are left.
1701 write_offset
= v_read(config
, &buf
->offset
);
1702 cons_offset
= uatomic_read(&buf
->consumed
);
1703 if (write_offset
!= cons_offset
)
1704 DBG("ring buffer %s, cpu %d: "
1705 "non-consumed data\n"
1706 " [ %lu bytes written, %lu bytes read ]\n",
1707 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1709 for (cons_offset
= uatomic_read(&buf
->consumed
);
1710 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1713 cons_offset
= subbuf_align(cons_offset
, chan
))
1714 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1719 void lib_ring_buffer_print_errors(struct lttng_ust_lib_ring_buffer_channel
*chan
,
1720 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1721 struct lttng_ust_shm_handle
*handle
)
1723 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1725 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1726 DBG("ring buffer %s: %lu records written, "
1727 "%lu records overrun\n",
1729 v_read(config
, &buf
->records_count
),
1730 v_read(config
, &buf
->records_overrun
));
1732 DBG("ring buffer %s, cpu %d: %lu records written, "
1733 "%lu records overrun\n",
1734 chan
->backend
.name
, cpu
,
1735 v_read(config
, &buf
->records_count
),
1736 v_read(config
, &buf
->records_overrun
));
1738 if (v_read(config
, &buf
->records_lost_full
)
1739 || v_read(config
, &buf
->records_lost_wrap
)
1740 || v_read(config
, &buf
->records_lost_big
))
1741 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1742 " [ %lu buffer full, %lu nest buffer wrap-around, "
1743 "%lu event too big ]\n",
1744 chan
->backend
.name
, cpu
,
1745 v_read(config
, &buf
->records_lost_full
),
1746 v_read(config
, &buf
->records_lost_wrap
),
1747 v_read(config
, &buf
->records_lost_big
));
1749 lib_ring_buffer_print_buffer_errors(buf
, chan
, cpu
, handle
);
1753 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1755 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1756 * active or at buffer finalization (destroy).
1759 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1760 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1761 struct switch_offsets
*offsets
,
1763 struct lttng_ust_shm_handle
*handle
)
1765 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1766 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1767 unsigned long commit_count
;
1768 struct commit_counters_hot
*cc_hot
;
1770 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1773 * Order all writes to buffer before the commit count update that will
1774 * determine that the subbuffer is full.
1777 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1780 v_add(config
, config
->cb
.subbuffer_header_size(),
1782 commit_count
= v_read(config
, &cc_hot
->cc
);
1783 /* Check if the written buffer has to be delivered */
1784 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1785 commit_count
, oldidx
, handle
, tsc
);
1786 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1787 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1788 commit_count
, handle
, cc_hot
);
1792 * lib_ring_buffer_switch_old_end: switch old subbuffer
1794 * Note : offset_old should never be 0 here. It is ok, because we never perform
1795 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1796 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1800 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1801 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1802 struct switch_offsets
*offsets
,
1804 struct lttng_ust_shm_handle
*handle
)
1806 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1807 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1808 unsigned long commit_count
, padding_size
, data_size
;
1809 struct commit_counters_hot
*cc_hot
;
1812 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1813 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1814 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1817 ts_end
= shmp_index(handle
, buf
->ts_end
, oldidx
);
1821 * This is the last space reservation in that sub-buffer before
1822 * it gets delivered. This provides exclusive access to write to
1823 * this sub-buffer's ts_end. There are also no concurrent
1824 * readers of that ts_end because delivery of that sub-buffer is
1825 * postponed until the commit counter is incremented for the
1826 * current space reservation.
1831 * Order all writes to buffer and store to ts_end before the commit
1832 * count update that will determine that the subbuffer is full.
1835 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1838 v_add(config
, padding_size
, &cc_hot
->cc
);
1839 commit_count
= v_read(config
, &cc_hot
->cc
);
1840 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1841 commit_count
, oldidx
, handle
, tsc
);
1842 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1843 offsets
->old
+ padding_size
, commit_count
, handle
,
1848 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1850 * This code can be executed unordered : writers may already have written to the
1851 * sub-buffer before this code gets executed, caution. The commit makes sure
1852 * that this code is executed before the deliver of this sub-buffer.
1855 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1856 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1857 struct switch_offsets
*offsets
,
1859 struct lttng_ust_shm_handle
*handle
)
1861 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1862 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1863 unsigned long commit_count
;
1864 struct commit_counters_hot
*cc_hot
;
1866 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1869 * Order all writes to buffer before the commit count update that will
1870 * determine that the subbuffer is full.
1873 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1876 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1877 commit_count
= v_read(config
, &cc_hot
->cc
);
1878 /* Check if the written buffer has to be delivered */
1879 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1880 commit_count
, beginidx
, handle
, tsc
);
1881 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1882 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1883 commit_count
, handle
, cc_hot
);
1887 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1889 * Calls subbuffer_set_data_size() to set the data size of the current
1890 * sub-buffer. We do not need to perform check_deliver nor commit here,
1891 * since this task will be done by the "commit" of the event for which
1892 * we are currently doing the space reservation.
1895 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1896 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1897 struct switch_offsets
*offsets
,
1899 struct lttng_ust_shm_handle
*handle
)
1901 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1902 unsigned long endidx
, data_size
;
1905 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1906 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1907 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1909 ts_end
= shmp_index(handle
, buf
->ts_end
, endidx
);
1913 * This is the last space reservation in that sub-buffer before
1914 * it gets delivered. This provides exclusive access to write to
1915 * this sub-buffer's ts_end. There are also no concurrent
1916 * readers of that ts_end because delivery of that sub-buffer is
1917 * postponed until the commit counter is incremented for the
1918 * current space reservation.
1926 * !0 if execution must be aborted.
1929 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1930 struct lttng_ust_lib_ring_buffer
*buf
,
1931 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1932 struct switch_offsets
*offsets
,
1934 struct lttng_ust_shm_handle
*handle
)
1936 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1937 unsigned long off
, reserve_commit_diff
;
1939 offsets
->begin
= v_read(config
, &buf
->offset
);
1940 offsets
->old
= offsets
->begin
;
1941 offsets
->switch_old_start
= 0;
1942 off
= subbuf_offset(offsets
->begin
, chan
);
1944 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1947 * Ensure we flush the header of an empty subbuffer when doing the
1948 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1949 * total data gathering duration even if there were no records saved
1950 * after the last buffer switch.
1951 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1952 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1953 * subbuffer header as appropriate.
1954 * The next record that reserves space will be responsible for
1955 * populating the following subbuffer header. We choose not to populate
1956 * the next subbuffer header here because we want to be able to use
1957 * SWITCH_ACTIVE for periodical buffer flush, which must
1958 * guarantee that all the buffer content (records and header
1959 * timestamps) are visible to the reader. This is required for
1960 * quiescence guarantees for the fusion merge.
1962 if (mode
!= SWITCH_FLUSH
&& !off
)
1963 return -1; /* we do not have to switch : buffer is empty */
1965 if (caa_unlikely(off
== 0)) {
1966 unsigned long sb_index
, commit_count
;
1967 struct commit_counters_cold
*cc_cold
;
1970 * We are performing a SWITCH_FLUSH. There may be concurrent
1971 * writes into the buffer if e.g. invoked while performing a
1972 * snapshot on an active trace.
1974 * If the client does not save any header information
1975 * (sub-buffer header size == 0), don't switch empty subbuffer
1976 * on finalize, because it is invalid to deliver a completely
1979 if (!config
->cb
.subbuffer_header_size())
1982 /* Test new buffer integrity */
1983 sb_index
= subbuf_index(offsets
->begin
, chan
);
1984 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1987 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1988 reserve_commit_diff
=
1989 (buf_trunc(offsets
->begin
, chan
)
1990 >> chan
->backend
.num_subbuf_order
)
1991 - (commit_count
& chan
->commit_count_mask
);
1992 if (caa_likely(reserve_commit_diff
== 0)) {
1993 /* Next subbuffer not being written to. */
1994 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1995 subbuf_trunc(offsets
->begin
, chan
)
1996 - subbuf_trunc((unsigned long)
1997 uatomic_read(&buf
->consumed
), chan
)
1998 >= chan
->backend
.buf_size
)) {
2000 * We do not overwrite non consumed buffers
2001 * and we are full : don't switch.
2006 * Next subbuffer not being written to, and we
2007 * are either in overwrite mode or the buffer is
2008 * not full. It's safe to write in this new
2014 * Next subbuffer reserve offset does not match the
2015 * commit offset. Don't perform switch in
2016 * producer-consumer and overwrite mode. Caused by
2017 * either a writer OOPS or too many nested writes over a
2018 * reserve/commit pair.
2024 * Need to write the subbuffer start header on finalize.
2026 offsets
->switch_old_start
= 1;
2028 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2029 /* Note: old points to the next subbuf at offset 0 */
2030 offsets
->end
= offsets
->begin
;
2035 * Force a sub-buffer switch. This operation is completely reentrant : can be
2036 * called while tracing is active with absolutely no lock held.
2038 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2039 * some atomic operations, this function must be called from the CPU
2040 * which owns the buffer for a ACTIVE flush. However, for
2041 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2044 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
2045 struct lttng_ust_shm_handle
*handle
)
2047 struct lttng_ust_lib_ring_buffer_channel
*chan
;
2048 const struct lttng_ust_lib_ring_buffer_config
*config
;
2049 struct switch_offsets offsets
;
2050 unsigned long oldidx
;
2053 chan
= shmp(handle
, buf
->backend
.chan
);
2056 config
= &chan
->backend
.config
;
2061 * Perform retryable operations.
2064 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
2066 return; /* Switch not needed */
2067 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
2071 * Atomically update last_tsc. This update races against concurrent
2072 * atomic updates, but the race will always cause supplementary full TSC
2073 * records, never the opposite (missing a full TSC record when it would
2076 save_last_tsc(config
, buf
, tsc
);
2079 * Push the reader if necessary
2081 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
2083 oldidx
= subbuf_index(offsets
.old
, chan
);
2084 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
2087 * May need to populate header start on SWITCH_FLUSH.
2089 if (offsets
.switch_old_start
) {
2090 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
2091 offsets
.old
+= config
->cb
.subbuffer_header_size();
2095 * Switch old subbuffer.
2097 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
2101 bool handle_blocking_retry(int *timeout_left_ms
)
2103 int timeout
= *timeout_left_ms
, delay
;
2105 if (caa_likely(!timeout
))
2106 return false; /* Do not retry, discard event. */
2107 if (timeout
< 0) /* Wait forever. */
2108 delay
= RETRY_DELAY_MS
;
2110 delay
= min_t(int, timeout
, RETRY_DELAY_MS
);
2111 (void) poll(NULL
, 0, delay
);
2113 *timeout_left_ms
-= delay
;
2114 return true; /* Retry. */
2120 * -ENOSPC if event size is too large for packet.
2121 * -ENOBUFS if there is currently not enough space in buffer for the event.
2122 * -EIO if data cannot be written into the buffer for any other reason.
2125 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
2126 struct lttng_ust_lib_ring_buffer_channel
*chan
,
2127 struct switch_offsets
*offsets
,
2128 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2131 struct lttng_ust_lib_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
2132 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2133 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
2134 unsigned long reserve_commit_diff
, offset_cmp
;
2135 int timeout_left_ms
= lttng_ust_ringbuffer_get_timeout(chan
);
2138 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2139 offsets
->old
= offsets
->begin
;
2140 offsets
->switch_new_start
= 0;
2141 offsets
->switch_new_end
= 0;
2142 offsets
->switch_old_end
= 0;
2143 offsets
->pre_header_padding
= 0;
2145 ctx_private
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2146 if ((int64_t) ctx_private
->tsc
== -EIO
)
2149 if (last_tsc_overflow(config
, buf
, ctx_private
->tsc
))
2150 ctx_private
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2152 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) == 0)) {
2153 offsets
->switch_new_start
= 1; /* For offsets->begin */
2155 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2157 &offsets
->pre_header_padding
,
2160 lttng_ust_lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2163 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2164 offsets
->size
> chan
->backend
.subbuf_size
)) {
2165 offsets
->switch_old_end
= 1; /* For offsets->old */
2166 offsets
->switch_new_start
= 1; /* For offsets->begin */
2169 if (caa_unlikely(offsets
->switch_new_start
)) {
2170 unsigned long sb_index
, commit_count
;
2171 struct commit_counters_cold
*cc_cold
;
2174 * We are typically not filling the previous buffer completely.
2176 if (caa_likely(offsets
->switch_old_end
))
2177 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2178 offsets
->begin
= offsets
->begin
2179 + config
->cb
.subbuffer_header_size();
2180 /* Test new buffer integrity */
2181 sb_index
= subbuf_index(offsets
->begin
, chan
);
2183 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2184 * lib_ring_buffer_check_deliver() has the matching
2185 * memory barriers required around commit_cold cc_sb
2186 * updates to ensure reserve and commit counter updates
2187 * are not seen reordered when updated by another CPU.
2190 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2193 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2194 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2196 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2198 * The reserve counter have been concurrently updated
2199 * while we read the commit counter. This means the
2200 * commit counter we read might not match buf->offset
2201 * due to concurrent update. We therefore need to retry.
2205 reserve_commit_diff
=
2206 (buf_trunc(offsets
->begin
, chan
)
2207 >> chan
->backend
.num_subbuf_order
)
2208 - (commit_count
& chan
->commit_count_mask
);
2209 if (caa_likely(reserve_commit_diff
== 0)) {
2210 /* Next subbuffer not being written to. */
2211 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2212 subbuf_trunc(offsets
->begin
, chan
)
2213 - subbuf_trunc((unsigned long)
2214 uatomic_read(&buf
->consumed
), chan
)
2215 >= chan
->backend
.buf_size
)) {
2216 unsigned long nr_lost
;
2218 if (handle_blocking_retry(&timeout_left_ms
))
2222 * We do not overwrite non consumed buffers
2223 * and we are full : record is lost.
2225 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2226 v_inc(config
, &buf
->records_lost_full
);
2227 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2228 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2229 nr_lost
+ 1, chan
->backend
.name
,
2235 * Next subbuffer not being written to, and we
2236 * are either in overwrite mode or the buffer is
2237 * not full. It's safe to write in this new
2242 unsigned long nr_lost
;
2245 * Next subbuffer reserve offset does not match the
2246 * commit offset, and this did not involve update to the
2247 * reserve counter. Drop record in producer-consumer and
2248 * overwrite mode. Caused by either a writer OOPS or too
2249 * many nested writes over a reserve/commit pair.
2251 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2252 v_inc(config
, &buf
->records_lost_wrap
);
2253 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2254 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2255 nr_lost
+ 1, chan
->backend
.name
,
2261 config
->cb
.record_header_size(config
, chan
,
2263 &offsets
->pre_header_padding
,
2266 lttng_ust_lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2269 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2270 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2271 unsigned long nr_lost
;
2274 * Record too big for subbuffers, report error, don't
2275 * complete the sub-buffer switch.
2277 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2278 v_inc(config
, &buf
->records_lost_big
);
2279 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2280 DBG("%lu or more records lost in (%s:%d) record size "
2281 " of %zu bytes is too large for buffer\n",
2282 nr_lost
+ 1, chan
->backend
.name
,
2283 buf
->backend
.cpu
, offsets
->size
);
2288 * We just made a successful buffer switch and the
2289 * record fits in the new subbuffer. Let's write.
2294 * Record fits in the current buffer and we are not on a switch
2295 * boundary. It's safe to write.
2298 offsets
->end
= offsets
->begin
+ offsets
->size
;
2300 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2302 * The offset_end will fall at the very beginning of the next
2305 offsets
->switch_new_end
= 1; /* For offsets->begin */
2311 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2312 * @ctx: ring buffer context.
2314 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2315 * -EIO for other errors, else returns 0.
2316 * It will take care of sub-buffer switching.
2318 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2321 struct lttng_ust_lib_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
2322 struct lttng_ust_lib_ring_buffer_channel
*chan
= ctx_private
->chan
;
2323 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
2324 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2325 struct lttng_ust_lib_ring_buffer
*buf
;
2326 struct switch_offsets offsets
;
2329 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2330 buf
= shmp(handle
, chan
->backend
.buf
[ctx_private
->reserve_cpu
].shmp
);
2332 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2335 ctx_private
->buf
= buf
;
2340 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2342 if (caa_unlikely(ret
))
2344 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2349 * Atomically update last_tsc. This update races against concurrent
2350 * atomic updates, but the race will always cause supplementary full TSC
2351 * records, never the opposite (missing a full TSC record when it would
2354 save_last_tsc(config
, buf
, ctx_private
->tsc
);
2357 * Push the reader if necessary
2359 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2362 * Clear noref flag for this subbuffer.
2364 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2365 subbuf_index(offsets
.end
- 1, chan
),
2369 * Switch old subbuffer if needed.
2371 if (caa_unlikely(offsets
.switch_old_end
)) {
2372 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2373 subbuf_index(offsets
.old
- 1, chan
),
2375 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx_private
->tsc
, handle
);
2379 * Populate new subbuffer.
2381 if (caa_unlikely(offsets
.switch_new_start
))
2382 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx_private
->tsc
, handle
);
2384 if (caa_unlikely(offsets
.switch_new_end
))
2385 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx_private
->tsc
, handle
);
2387 ctx_private
->slot_size
= offsets
.size
;
2388 ctx_private
->pre_offset
= offsets
.begin
;
2389 ctx_private
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2394 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
2395 struct lttng_ust_lib_ring_buffer
*buf
,
2396 unsigned long commit_count
,
2398 struct lttng_ust_shm_handle
*handle
)
2400 struct commit_counters_hot
*cc_hot
;
2402 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2404 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2407 v_set(config
, &cc_hot
->seq
, commit_count
);
2411 * The ring buffer can count events recorded and overwritten per buffer,
2412 * but it is disabled by default due to its performance overhead.
2414 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2416 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2417 struct lttng_ust_lib_ring_buffer
*buf
,
2419 struct lttng_ust_shm_handle
*handle
)
2421 v_add(config
, subbuffer_get_records_count(config
,
2422 &buf
->backend
, idx
, handle
),
2423 &buf
->records_count
);
2424 v_add(config
, subbuffer_count_records_overrun(config
,
2425 &buf
->backend
, idx
, handle
),
2426 &buf
->records_overrun
);
2428 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2430 void deliver_count_events(
2431 const struct lttng_ust_lib_ring_buffer_config
*config
__attribute__((unused
)),
2432 struct lttng_ust_lib_ring_buffer
*buf
__attribute__((unused
)),
2433 unsigned long idx
__attribute__((unused
)),
2434 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
2437 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2439 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
2440 struct lttng_ust_lib_ring_buffer
*buf
,
2441 struct lttng_ust_lib_ring_buffer_channel
*chan
,
2442 unsigned long offset
,
2443 unsigned long commit_count
,
2445 struct lttng_ust_shm_handle
*handle
,
2446 uint64_t tsc
__attribute__((unused
)))
2448 unsigned long old_commit_count
= commit_count
2449 - chan
->backend
.subbuf_size
;
2450 struct commit_counters_cold
*cc_cold
;
2453 * If we succeeded at updating cc_sb below, we are the subbuffer
2454 * writer delivering the subbuffer. Deals with concurrent
2455 * updates of the "cc" value without adding a add_return atomic
2456 * operation to the fast path.
2458 * We are doing the delivery in two steps:
2459 * - First, we cmpxchg() cc_sb to the new value
2460 * old_commit_count + 1. This ensures that we are the only
2461 * subbuffer user successfully filling the subbuffer, but we
2462 * do _not_ set the cc_sb value to "commit_count" yet.
2463 * Therefore, other writers that would wrap around the ring
2464 * buffer and try to start writing to our subbuffer would
2465 * have to drop records, because it would appear as
2467 * We therefore have exclusive access to the subbuffer control
2468 * structures. This mutual exclusion with other writers is
2469 * crucially important to perform record overruns count in
2470 * flight recorder mode locklessly.
2471 * - When we are ready to release the subbuffer (either for
2472 * reading or for overrun by other writers), we simply set the
2473 * cc_sb value to "commit_count" and perform delivery.
2475 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2476 * This guarantees that old_commit_count + 1 != commit_count.
2480 * Order prior updates to reserve count prior to the
2481 * commit_cold cc_sb update.
2484 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2487 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2488 old_commit_count
, old_commit_count
+ 1)
2489 == old_commit_count
)) {
2493 * Start of exclusive subbuffer access. We are
2494 * guaranteed to be the last writer in this subbuffer
2495 * and any other writer trying to access this subbuffer
2496 * in this state is required to drop records.
2498 * We can read the ts_end for the current sub-buffer
2499 * which has been saved by the very last space
2500 * reservation for the current sub-buffer.
2502 * Order increment of commit counter before reading ts_end.
2505 ts_end
= shmp_index(handle
, buf
->ts_end
, idx
);
2508 deliver_count_events(config
, buf
, idx
, handle
);
2509 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2510 lib_ring_buffer_get_data_size(config
,
2517 * Increment the packet counter while we have exclusive
2520 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2523 * Set noref flag and offset for this subbuffer id.
2524 * Contains a memory barrier that ensures counter stores
2525 * are ordered before set noref and offset.
2527 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2528 buf_trunc_val(offset
, chan
), handle
);
2531 * Order set_noref and record counter updates before the
2532 * end of subbuffer exclusive access. Orders with
2533 * respect to writers coming into the subbuffer after
2534 * wrap around, and also order wrt concurrent readers.
2537 /* End of exclusive subbuffer access */
2538 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2540 * Order later updates to reserve count after
2541 * the commit cold cc_sb update.
2544 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2545 commit_count
, idx
, handle
);
2548 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2550 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2551 && uatomic_read(&buf
->active_readers
)
2552 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2553 lib_ring_buffer_wakeup(buf
, handle
);
2559 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2561 void lttng_fixup_ringbuffer_tls(void)
2563 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2566 void lib_ringbuffer_signal_init(void)
2572 * Block signal for entire process, so only our thread processes
2576 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2579 PERROR("pthread_sigmask");