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/ust-ringbuffer-context.h>
58 #include "common/smp.h"
59 #include "ringbuffer-config.h"
65 #include "common/compat/errno.h" /* For ENODATA */
66 #include "common/populate.h"
68 /* Print DBG() messages about events lost only every 1048576 hits */
69 #define DBG_PRINT_NR_LOST (1UL << 20)
71 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
72 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
73 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
74 #define CLOCKID CLOCK_MONOTONIC
75 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
76 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
77 #define RETRY_DELAY_MS 100 /* 100 ms. */
80 * Non-static to ensure the compiler does not optimize away the xor.
82 uint8_t lttng_crash_magic_xor
[]
83 __attribute__((visibility("hidden")));
84 uint8_t lttng_crash_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
87 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
88 * close(2) to close the fd returned by shm_open.
89 * shm_unlink releases the shared memory object name.
90 * ftruncate(2) sets the size of the memory object.
91 * mmap/munmap maps the shared memory obj to a virtual address in the
92 * calling proceess (should be done both in libust and consumer).
93 * See shm_overview(7) for details.
94 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
97 * Since we don't need to access the object using its name, we can
98 * immediately shm_unlink(3) it, and only keep the handle with its file
103 * Internal structure representing offsets to use at a sub-buffer switch.
105 struct switch_offsets
{
106 unsigned long begin
, end
, old
;
107 size_t pre_header_padding
, size
;
108 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
112 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
115 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
118 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
121 void lib_ring_buffer_print_errors(struct lttng_ust_ring_buffer_channel
*chan
,
122 struct lttng_ust_ring_buffer
*buf
, int cpu
,
123 struct lttng_ust_shm_handle
*handle
);
126 * Handle timer teardown race wrt memory free of private data by
127 * ring buffer signals are handled by a single thread, which permits
128 * a synchronization point between handling of each signal.
129 * Protected by the lock within the structure.
131 struct timer_signal_data
{
132 pthread_t tid
; /* thread id managing signals */
135 pthread_mutex_t lock
;
138 static struct timer_signal_data timer_signal
= {
142 .lock
= PTHREAD_MUTEX_INITIALIZER
,
145 static bool lttng_ust_allow_blocking
;
147 void lttng_ust_ringbuffer_set_allow_blocking(void)
149 lttng_ust_allow_blocking
= true;
152 /* Get blocking timeout, in ms */
153 static int lttng_ust_ringbuffer_get_timeout(struct lttng_ust_ring_buffer_channel
*chan
)
155 if (!lttng_ust_allow_blocking
)
157 return chan
->u
.s
.blocking_timeout_ms
;
161 * lib_ring_buffer_reset - Reset ring buffer to initial values.
164 * Effectively empty the ring buffer. Should be called when the buffer is not
165 * used for writing. The ring buffer can be opened for reading, but the reader
166 * should not be using the iterator concurrently with reset. The previous
167 * current iterator record is reset.
169 void lib_ring_buffer_reset(struct lttng_ust_ring_buffer
*buf
,
170 struct lttng_ust_shm_handle
*handle
)
172 struct lttng_ust_ring_buffer_channel
*chan
;
173 const struct lttng_ust_ring_buffer_config
*config
;
176 chan
= shmp(handle
, buf
->backend
.chan
);
179 config
= &chan
->backend
.config
;
181 * Reset iterator first. It will put the subbuffer if it currently holds
184 v_set(config
, &buf
->offset
, 0);
185 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
186 struct commit_counters_hot
*cc_hot
;
187 struct commit_counters_cold
*cc_cold
;
190 cc_hot
= shmp_index(handle
, buf
->commit_hot
, i
);
193 cc_cold
= shmp_index(handle
, buf
->commit_cold
, i
);
196 ts_end
= shmp_index(handle
, buf
->ts_end
, i
);
199 v_set(config
, &cc_hot
->cc
, 0);
200 v_set(config
, &cc_hot
->seq
, 0);
201 v_set(config
, &cc_cold
->cc_sb
, 0);
204 uatomic_set(&buf
->consumed
, 0);
205 uatomic_set(&buf
->record_disabled
, 0);
206 v_set(config
, &buf
->last_timestamp
, 0);
207 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
208 /* Don't reset number of active readers */
209 v_set(config
, &buf
->records_lost_full
, 0);
210 v_set(config
, &buf
->records_lost_wrap
, 0);
211 v_set(config
, &buf
->records_lost_big
, 0);
212 v_set(config
, &buf
->records_count
, 0);
213 v_set(config
, &buf
->records_overrun
, 0);
218 * channel_reset - Reset channel to initial values.
221 * Effectively empty the channel. Should be called when the channel is not used
222 * for writing. The channel can be opened for reading, but the reader should not
223 * be using the iterator concurrently with reset. The previous current iterator
226 void channel_reset(struct lttng_ust_ring_buffer_channel
*chan
)
229 * Reset iterators first. Will put the subbuffer if held for reading.
231 uatomic_set(&chan
->record_disabled
, 0);
232 /* Don't reset commit_count_mask, still valid */
233 channel_backend_reset(&chan
->backend
);
234 /* Don't reset switch/read timer interval */
235 /* Don't reset notifiers and notifier enable bits */
236 /* Don't reset reader reference count */
240 void init_crash_abi(const struct lttng_ust_ring_buffer_config
*config
,
241 struct lttng_crash_abi
*crash_abi
,
242 struct lttng_ust_ring_buffer
*buf
,
243 struct channel_backend
*chanb
,
244 struct shm_object
*shmobj
,
245 struct lttng_ust_shm_handle
*handle
)
249 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
250 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
251 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
252 crash_abi
->endian
= RB_CRASH_ENDIAN
;
253 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
254 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
255 crash_abi
->word_size
= sizeof(unsigned long);
256 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
258 /* Offset of fields */
259 crash_abi
->offset
.prod_offset
=
260 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
261 crash_abi
->offset
.consumed_offset
=
262 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
263 crash_abi
->offset
.commit_hot_array
=
264 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
265 crash_abi
->offset
.commit_hot_seq
=
266 offsetof(struct commit_counters_hot
, seq
);
267 crash_abi
->offset
.buf_wsb_array
=
268 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
269 crash_abi
->offset
.buf_wsb_id
=
270 offsetof(struct lttng_ust_ring_buffer_backend_subbuffer
, id
);
271 crash_abi
->offset
.sb_array
=
272 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
273 crash_abi
->offset
.sb_array_shmp_offset
=
274 offsetof(struct lttng_ust_ring_buffer_backend_pages_shmp
,
276 crash_abi
->offset
.sb_backend_p_offset
=
277 offsetof(struct lttng_ust_ring_buffer_backend_pages
,
281 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
282 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
283 crash_abi
->length
.commit_hot_seq
=
284 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
285 crash_abi
->length
.buf_wsb_id
=
286 sizeof(((struct lttng_ust_ring_buffer_backend_subbuffer
*) NULL
)->id
);
287 crash_abi
->length
.sb_array_shmp_offset
=
288 sizeof(((struct lttng_ust_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
289 crash_abi
->length
.sb_backend_p_offset
=
290 sizeof(((struct lttng_ust_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
293 crash_abi
->stride
.commit_hot_array
=
294 sizeof(struct commit_counters_hot
);
295 crash_abi
->stride
.buf_wsb_array
=
296 sizeof(struct lttng_ust_ring_buffer_backend_subbuffer
);
297 crash_abi
->stride
.sb_array
=
298 sizeof(struct lttng_ust_ring_buffer_backend_pages_shmp
);
300 /* Buffer constants */
301 crash_abi
->buf_size
= chanb
->buf_size
;
302 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
303 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
304 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
306 if (config
->cb
.content_size_field
) {
307 size_t offset
, length
;
309 config
->cb
.content_size_field(config
, &offset
, &length
);
310 crash_abi
->offset
.content_size
= offset
;
311 crash_abi
->length
.content_size
= length
;
313 crash_abi
->offset
.content_size
= 0;
314 crash_abi
->length
.content_size
= 0;
316 if (config
->cb
.packet_size_field
) {
317 size_t offset
, length
;
319 config
->cb
.packet_size_field(config
, &offset
, &length
);
320 crash_abi
->offset
.packet_size
= offset
;
321 crash_abi
->length
.packet_size
= length
;
323 crash_abi
->offset
.packet_size
= 0;
324 crash_abi
->length
.packet_size
= 0;
329 * Must be called under cpu hotplug protection.
331 int lib_ring_buffer_create(struct lttng_ust_ring_buffer
*buf
,
332 struct channel_backend
*chanb
, int cpu
,
333 struct lttng_ust_shm_handle
*handle
,
334 struct shm_object
*shmobj
)
336 const struct lttng_ust_ring_buffer_config
*config
= &chanb
->config
;
337 struct lttng_ust_ring_buffer_channel
*chan
= caa_container_of(chanb
,
338 struct lttng_ust_ring_buffer_channel
, backend
);
339 struct lttng_ust_ring_buffer_backend_subbuffer
*wsb
;
340 struct lttng_ust_ring_buffer_channel
*shmp_chan
;
341 struct commit_counters_hot
*cc_hot
;
342 void *priv
= channel_get_private_config(chan
);
343 size_t subbuf_header_size
;
347 /* Test for cpu hotplug */
348 if (buf
->backend
.allocated
)
351 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
352 set_shmp(buf
->commit_hot
,
354 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
355 if (!shmp(handle
, buf
->commit_hot
)) {
359 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
360 set_shmp(buf
->commit_cold
,
362 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
363 if (!shmp(handle
, buf
->commit_cold
)) {
368 align_shm(shmobj
, __alignof__(uint64_t));
369 set_shmp(buf
->ts_end
,
371 sizeof(uint64_t) * chan
->backend
.num_subbuf
));
372 if (!shmp(handle
, buf
->ts_end
)) {
374 goto free_commit_cold
;
378 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
379 cpu
, handle
, shmobj
);
385 * Write the subbuffer header for first subbuffer so we know the total
386 * duration of data gathering.
388 subbuf_header_size
= config
->cb
.subbuffer_header_size();
389 v_set(config
, &buf
->offset
, subbuf_header_size
);
390 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
395 subbuffer_id_clear_noref(config
, &wsb
->id
);
396 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
401 timestamp
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
402 config
->cb
.buffer_begin(buf
, timestamp
, 0, handle
);
403 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
408 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
409 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
411 if (config
->cb
.buffer_create
) {
412 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
417 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
419 buf
->backend
.allocated
= 1;
424 /* ts_end will be freed by shm teardown */
426 /* commit_cold will be freed by shm teardown */
428 /* commit_hot will be freed by shm teardown */
434 void lib_ring_buffer_channel_switch_timer(int sig
__attribute__((unused
)),
435 siginfo_t
*si
, void *uc
__attribute__((unused
)))
437 const struct lttng_ust_ring_buffer_config
*config
;
438 struct lttng_ust_shm_handle
*handle
;
439 struct lttng_ust_ring_buffer_channel
*chan
;
442 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
444 chan
= si
->si_value
.sival_ptr
;
445 handle
= chan
->handle
;
446 config
= &chan
->backend
.config
;
448 DBG("Switch timer for channel %p\n", chan
);
451 * Only flush buffers periodically if readers are active.
453 pthread_mutex_lock(&wakeup_fd_mutex
);
454 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
455 for_each_possible_cpu(cpu
) {
456 struct lttng_ust_ring_buffer
*buf
=
457 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
461 if (uatomic_read(&buf
->active_readers
))
462 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
466 struct lttng_ust_ring_buffer
*buf
=
467 shmp(handle
, chan
->backend
.buf
[0].shmp
);
471 if (uatomic_read(&buf
->active_readers
))
472 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
476 pthread_mutex_unlock(&wakeup_fd_mutex
);
481 int lib_ring_buffer_poll_deliver(const struct lttng_ust_ring_buffer_config
*config
,
482 struct lttng_ust_ring_buffer
*buf
,
483 struct lttng_ust_ring_buffer_channel
*chan
,
484 struct lttng_ust_shm_handle
*handle
)
486 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
487 struct commit_counters_cold
*cc_cold
;
489 consumed_old
= uatomic_read(&buf
->consumed
);
490 consumed_idx
= subbuf_index(consumed_old
, chan
);
491 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
494 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
496 * No memory barrier here, since we are only interested
497 * in a statistically correct polling result. The next poll will
498 * get the data is we are racing. The mb() that ensures correct
499 * memory order is in get_subbuf.
501 write_offset
= v_read(config
, &buf
->offset
);
504 * Check that the subbuffer we are trying to consume has been
505 * already fully committed.
508 if (((commit_count
- chan
->backend
.subbuf_size
)
509 & chan
->commit_count_mask
)
510 - (buf_trunc(consumed_old
, chan
)
511 >> chan
->backend
.num_subbuf_order
)
516 * Check that we are not about to read the same subbuffer in
517 * which the writer head is.
519 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
527 void lib_ring_buffer_wakeup(struct lttng_ust_ring_buffer
*buf
,
528 struct lttng_ust_shm_handle
*handle
)
530 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
531 sigset_t sigpipe_set
, pending_set
, old_set
;
532 int ret
, sigpipe_was_pending
= 0;
538 * Wake-up the other end by writing a null byte in the pipe
539 * (non-blocking). Important note: Because writing into the
540 * pipe is non-blocking (and therefore we allow dropping wakeup
541 * data, as long as there is wakeup data present in the pipe
542 * buffer to wake up the consumer), the consumer should perform
543 * the following sequence for waiting:
544 * 1) empty the pipe (reads).
545 * 2) check if there is data in the buffer.
546 * 3) wait on the pipe (poll).
548 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
549 * that might be already pending. If a bogus SIGPIPE is sent to
550 * the entire process concurrently by a malicious user, it may
551 * be simply discarded.
553 ret
= sigemptyset(&pending_set
);
556 * sigpending returns the mask of signals that are _both_
557 * blocked for the thread _and_ pending for either the thread or
558 * the entire process.
560 ret
= sigpending(&pending_set
);
562 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
564 * If sigpipe was pending, it means it was already blocked, so
565 * no need to block it.
567 if (!sigpipe_was_pending
) {
568 ret
= sigemptyset(&sigpipe_set
);
570 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
572 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
576 ret
= write(wakeup_fd
, "", 1);
577 } while (ret
== -1L && errno
== EINTR
);
578 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
579 struct timespec timeout
= { 0, 0 };
581 ret
= sigtimedwait(&sigpipe_set
, NULL
,
583 } while (ret
== -1L && errno
== EINTR
);
585 if (!sigpipe_was_pending
) {
586 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
592 void lib_ring_buffer_channel_do_read(struct lttng_ust_ring_buffer_channel
*chan
)
594 const struct lttng_ust_ring_buffer_config
*config
;
595 struct lttng_ust_shm_handle
*handle
;
598 handle
= chan
->handle
;
599 config
= &chan
->backend
.config
;
602 * Only flush buffers periodically if readers are active.
604 pthread_mutex_lock(&wakeup_fd_mutex
);
605 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
606 for_each_possible_cpu(cpu
) {
607 struct lttng_ust_ring_buffer
*buf
=
608 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
612 if (uatomic_read(&buf
->active_readers
)
613 && lib_ring_buffer_poll_deliver(config
, buf
,
615 lib_ring_buffer_wakeup(buf
, handle
);
619 struct lttng_ust_ring_buffer
*buf
=
620 shmp(handle
, chan
->backend
.buf
[0].shmp
);
624 if (uatomic_read(&buf
->active_readers
)
625 && lib_ring_buffer_poll_deliver(config
, buf
,
627 lib_ring_buffer_wakeup(buf
, handle
);
631 pthread_mutex_unlock(&wakeup_fd_mutex
);
635 void lib_ring_buffer_channel_read_timer(int sig
__attribute__((unused
)),
636 siginfo_t
*si
, void *uc
__attribute__((unused
)))
638 struct lttng_ust_ring_buffer_channel
*chan
;
640 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
641 chan
= si
->si_value
.sival_ptr
;
642 DBG("Read timer for channel %p\n", chan
);
643 lib_ring_buffer_channel_do_read(chan
);
648 void rb_setmask(sigset_t
*mask
)
652 ret
= sigemptyset(mask
);
654 PERROR("sigemptyset");
656 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
660 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
664 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
671 void *sig_thread(void *arg
__attribute__((unused
)))
677 /* Only self thread will receive signal mask. */
679 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
682 signr
= sigwaitinfo(&mask
, &info
);
685 PERROR("sigwaitinfo");
688 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
689 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
691 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
692 lib_ring_buffer_channel_read_timer(info
.si_signo
,
694 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
696 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
699 ERR("Unexptected signal %d\n", info
.si_signo
);
706 * Ensure only a single thread listens on the timer signal.
709 void lib_ring_buffer_setup_timer_thread(void)
714 pthread_mutex_lock(&timer_signal
.lock
);
715 if (timer_signal
.setup_done
)
718 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
721 PERROR("pthread_create");
723 ret
= pthread_detach(thread
);
726 PERROR("pthread_detach");
728 timer_signal
.setup_done
= 1;
730 pthread_mutex_unlock(&timer_signal
.lock
);
734 * Wait for signal-handling thread quiescent state.
737 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
739 sigset_t pending_set
;
743 * We need to be the only thread interacting with the thread
744 * that manages signals for teardown synchronization.
746 pthread_mutex_lock(&timer_signal
.lock
);
749 * Ensure we don't have any signal queued for this channel.
752 ret
= sigemptyset(&pending_set
);
754 PERROR("sigemptyset");
756 ret
= sigpending(&pending_set
);
758 PERROR("sigpending");
760 if (!sigismember(&pending_set
, signr
))
766 * From this point, no new signal handler will be fired that
767 * would try to access "chan". However, we still need to wait
768 * for any currently executing handler to complete.
771 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
775 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
778 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
780 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
784 pthread_mutex_unlock(&timer_signal
.lock
);
788 void lib_ring_buffer_channel_switch_timer_start(struct lttng_ust_ring_buffer_channel
*chan
)
791 struct itimerspec its
;
794 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
797 chan
->switch_timer_enabled
= 1;
799 lib_ring_buffer_setup_timer_thread();
801 memset(&sev
, 0, sizeof(sev
));
802 sev
.sigev_notify
= SIGEV_SIGNAL
;
803 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
804 sev
.sigev_value
.sival_ptr
= chan
;
805 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
807 PERROR("timer_create");
810 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
811 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
812 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
813 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
815 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
817 PERROR("timer_settime");
822 void lib_ring_buffer_channel_switch_timer_stop(struct lttng_ust_ring_buffer_channel
*chan
)
826 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
829 ret
= timer_delete(chan
->switch_timer
);
831 PERROR("timer_delete");
834 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
836 chan
->switch_timer
= 0;
837 chan
->switch_timer_enabled
= 0;
841 void lib_ring_buffer_channel_read_timer_start(struct lttng_ust_ring_buffer_channel
*chan
)
843 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
845 struct itimerspec its
;
848 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
849 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
852 chan
->read_timer_enabled
= 1;
854 lib_ring_buffer_setup_timer_thread();
856 sev
.sigev_notify
= SIGEV_SIGNAL
;
857 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
858 sev
.sigev_value
.sival_ptr
= chan
;
859 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
861 PERROR("timer_create");
864 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
865 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
866 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
867 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
869 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
871 PERROR("timer_settime");
876 void lib_ring_buffer_channel_read_timer_stop(struct lttng_ust_ring_buffer_channel
*chan
)
878 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
881 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
882 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
885 ret
= timer_delete(chan
->read_timer
);
887 PERROR("timer_delete");
891 * do one more check to catch data that has been written in the last
894 lib_ring_buffer_channel_do_read(chan
);
896 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
898 chan
->read_timer
= 0;
899 chan
->read_timer_enabled
= 0;
902 static void channel_unregister_notifiers(struct lttng_ust_ring_buffer_channel
*chan
,
903 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
905 lib_ring_buffer_channel_switch_timer_stop(chan
);
906 lib_ring_buffer_channel_read_timer_stop(chan
);
909 static void channel_print_errors(struct lttng_ust_ring_buffer_channel
*chan
,
910 struct lttng_ust_shm_handle
*handle
)
912 const struct lttng_ust_ring_buffer_config
*config
=
913 &chan
->backend
.config
;
916 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
917 for_each_possible_cpu(cpu
) {
918 struct lttng_ust_ring_buffer
*buf
=
919 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
921 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
924 struct lttng_ust_ring_buffer
*buf
=
925 shmp(handle
, chan
->backend
.buf
[0].shmp
);
928 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
932 static void channel_free(struct lttng_ust_ring_buffer_channel
*chan
,
933 struct lttng_ust_shm_handle
*handle
,
936 channel_backend_free(&chan
->backend
, handle
);
937 /* chan is freed by shm teardown */
938 shm_object_table_destroy(handle
->table
, consumer
);
943 * channel_create - Create channel.
944 * @config: ring buffer instance configuration
945 * @name: name of the channel
946 * @priv_data_align: alignment, in bytes, of the private data area. (config)
947 * @priv_data_size: length, in bytes, of the private data area. (config)
948 * @priv_data_init: initialization data for private data. (config)
949 * @priv: local private data (memory owner by caller)
950 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
951 * address mapping. It is used only by RING_BUFFER_STATIC
952 * configuration. It can be set to NULL for other backends.
953 * @subbuf_size: subbuffer size
954 * @num_subbuf: number of subbuffers
955 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
956 * padding to let readers get those sub-buffers.
957 * Used for live streaming.
958 * @read_timer_interval: Time interval (in us) to wake up pending readers.
959 * @stream_fds: array of stream file descriptors.
960 * @nr_stream_fds: number of file descriptors in array.
963 * Returns NULL on failure.
965 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_ring_buffer_config
*config
,
967 size_t priv_data_align
,
968 size_t priv_data_size
,
969 void *priv_data_init
,
971 void *buf_addr
__attribute__((unused
)), size_t subbuf_size
,
972 size_t num_subbuf
, unsigned int switch_timer_interval
,
973 unsigned int read_timer_interval
,
974 const int *stream_fds
, int nr_stream_fds
,
975 int64_t blocking_timeout
)
978 size_t shmsize
, chansize
;
979 struct lttng_ust_ring_buffer_channel
*chan
;
980 struct lttng_ust_shm_handle
*handle
;
981 struct shm_object
*shmobj
;
982 unsigned int nr_streams
;
983 int64_t blocking_timeout_ms
;
984 bool populate
= lttng_ust_map_populate_is_enabled();
986 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
987 nr_streams
= get_possible_cpus_array_len();
991 if (nr_stream_fds
!= nr_streams
)
994 if (blocking_timeout
< -1) {
998 if (blocking_timeout
== -1) {
999 blocking_timeout_ms
= -1;
1001 blocking_timeout_ms
= blocking_timeout
/ 1000;
1002 if (blocking_timeout_ms
!= (int32_t) blocking_timeout_ms
) {
1007 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
1008 read_timer_interval
))
1011 handle
= zmalloc_populate(sizeof(struct lttng_ust_shm_handle
), populate
);
1015 /* Allocate table for channel + per-cpu buffers */
1016 handle
->table
= shm_object_table_create(1 + get_possible_cpus_array_len(), populate
);
1018 goto error_table_alloc
;
1020 /* Calculate the shm allocation layout */
1021 shmsize
= sizeof(struct lttng_ust_ring_buffer_channel
);
1022 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_ring_buffer_shmp
));
1023 shmsize
+= sizeof(struct lttng_ust_ring_buffer_shmp
) * nr_streams
;
1025 if (priv_data_align
)
1026 shmsize
+= lttng_ust_offset_align(shmsize
, priv_data_align
);
1027 shmsize
+= priv_data_size
;
1029 /* Allocate normal memory for channel (not shared) */
1030 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
1034 /* struct lttng_ust_ring_buffer_channel is at object 0, offset 0 (hardcoded) */
1035 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
1036 assert(handle
->chan
._ref
.index
== 0);
1037 assert(handle
->chan
._ref
.offset
== 0);
1038 chan
= shmp(handle
, handle
->chan
);
1041 chan
->nr_streams
= nr_streams
;
1043 /* space for private data */
1044 if (priv_data_size
) {
1047 DECLARE_SHMP(void, priv_data_alloc
);
1049 align_shm(shmobj
, priv_data_align
);
1050 chan
->priv_data_offset
= shmobj
->allocated_len
;
1051 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1052 if (!shmp(handle
, priv_data_alloc
))
1054 priv_config
= channel_get_private_config(chan
);
1055 memcpy(priv_config
, priv_data_init
, priv_data_size
);
1057 chan
->priv_data_offset
= -1;
1060 chan
->u
.s
.blocking_timeout_ms
= (int32_t) blocking_timeout_ms
;
1062 channel_set_private(chan
, priv
);
1064 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1065 subbuf_size
, num_subbuf
, handle
,
1068 goto error_backend_init
;
1070 chan
->handle
= handle
;
1071 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1073 chan
->switch_timer_interval
= switch_timer_interval
;
1074 chan
->read_timer_interval
= read_timer_interval
;
1075 lib_ring_buffer_channel_switch_timer_start(chan
);
1076 lib_ring_buffer_channel_read_timer_start(chan
);
1082 shm_object_table_destroy(handle
->table
, 1);
1088 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1089 uint64_t memory_map_size
,
1092 struct lttng_ust_shm_handle
*handle
;
1093 struct shm_object
*object
;
1094 bool populate
= lttng_ust_map_populate_is_enabled();
1096 handle
= zmalloc_populate(sizeof(struct lttng_ust_shm_handle
), populate
);
1100 /* Allocate table for channel + per-cpu buffers */
1101 handle
->table
= shm_object_table_create(1 + get_possible_cpus_array_len(), populate
);
1103 goto error_table_alloc
;
1104 /* Add channel object */
1105 object
= shm_object_table_append_mem(handle
->table
, data
,
1106 memory_map_size
, wakeup_fd
);
1108 goto error_table_object
;
1109 /* struct lttng_ust_ring_buffer_channel is at object 0, offset 0 (hardcoded) */
1110 handle
->chan
._ref
.index
= 0;
1111 handle
->chan
._ref
.offset
= 0;
1115 shm_object_table_destroy(handle
->table
, 0);
1121 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1122 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1123 uint64_t memory_map_size
)
1125 struct shm_object
*object
;
1127 /* Add stream object */
1128 object
= shm_object_table_append_shm(handle
->table
,
1129 shm_fd
, wakeup_fd
, stream_nr
,
1130 memory_map_size
, lttng_ust_map_populate_cpu_is_enabled(stream_nr
));
1136 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1138 assert(handle
->table
);
1139 return handle
->table
->allocated_len
- 1;
1143 void channel_release(struct lttng_ust_ring_buffer_channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1146 channel_free(chan
, handle
, consumer
);
1150 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1151 * @chan: channel to destroy
1153 * Holds cpu hotplug.
1154 * Call "destroy" callback, finalize channels, decrement the channel
1155 * reference count. Note that when readers have completed data
1156 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1157 * They should release their handle at that point.
1159 void channel_destroy(struct lttng_ust_ring_buffer_channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1164 * Note: the consumer takes care of finalizing and
1165 * switching the buffers.
1167 channel_unregister_notifiers(chan
, handle
);
1169 * The consumer prints errors.
1171 channel_print_errors(chan
, handle
);
1175 * sessiond/consumer are keeping a reference on the shm file
1176 * descriptor directly. No need to refcount.
1178 channel_release(chan
, handle
, consumer
);
1182 struct lttng_ust_ring_buffer
*channel_get_ring_buffer(
1183 const struct lttng_ust_ring_buffer_config
*config
,
1184 struct lttng_ust_ring_buffer_channel
*chan
, int cpu
,
1185 struct lttng_ust_shm_handle
*handle
,
1186 int *shm_fd
, int *wait_fd
,
1188 uint64_t *memory_map_size
,
1189 void **memory_map_addr
)
1191 struct shm_ref
*ref
;
1193 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1196 if (cpu
>= get_possible_cpus_array_len())
1199 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1200 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1201 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1202 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1203 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1205 *memory_map_addr
= handle
->table
->objects
[ref
->index
].memory_map
;
1206 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1209 int ring_buffer_channel_close_wait_fd(
1210 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
1211 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
1212 struct lttng_ust_shm_handle
*handle
)
1214 struct shm_ref
*ref
;
1216 ref
= &handle
->chan
._ref
;
1217 return shm_close_wait_fd(handle
, ref
);
1220 int ring_buffer_channel_close_wakeup_fd(
1221 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
1222 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
1223 struct lttng_ust_shm_handle
*handle
)
1225 struct shm_ref
*ref
;
1227 ref
= &handle
->chan
._ref
;
1228 return shm_close_wakeup_fd(handle
, ref
);
1231 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_ring_buffer_config
*config
,
1232 struct lttng_ust_ring_buffer_channel
*chan
,
1233 struct lttng_ust_shm_handle
*handle
,
1236 struct shm_ref
*ref
;
1238 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1241 if (cpu
>= get_possible_cpus_array_len())
1244 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1245 return shm_close_wait_fd(handle
, ref
);
1248 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_ring_buffer_config
*config
,
1249 struct lttng_ust_ring_buffer_channel
*chan
,
1250 struct lttng_ust_shm_handle
*handle
,
1253 struct shm_ref
*ref
;
1256 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1259 if (cpu
>= get_possible_cpus_array_len())
1262 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1263 pthread_mutex_lock(&wakeup_fd_mutex
);
1264 ret
= shm_close_wakeup_fd(handle
, ref
);
1265 pthread_mutex_unlock(&wakeup_fd_mutex
);
1269 int lib_ring_buffer_open_read(struct lttng_ust_ring_buffer
*buf
,
1270 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
1272 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1278 void lib_ring_buffer_release_read(struct lttng_ust_ring_buffer
*buf
,
1279 struct lttng_ust_shm_handle
*handle
)
1281 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1285 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1287 uatomic_dec(&buf
->active_readers
);
1291 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1293 * @consumed: consumed count indicating the position where to read
1294 * @produced: produced count, indicates position when to stop reading
1296 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1297 * data to read at consumed position, or 0 if the get operation succeeds.
1300 int lib_ring_buffer_snapshot(struct lttng_ust_ring_buffer
*buf
,
1301 unsigned long *consumed
, unsigned long *produced
,
1302 struct lttng_ust_shm_handle
*handle
)
1304 struct lttng_ust_ring_buffer_channel
*chan
;
1305 const struct lttng_ust_ring_buffer_config
*config
;
1306 unsigned long consumed_cur
, write_offset
;
1309 chan
= shmp(handle
, buf
->backend
.chan
);
1312 config
= &chan
->backend
.config
;
1313 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1315 * Read finalized before counters.
1318 consumed_cur
= uatomic_read(&buf
->consumed
);
1320 * No need to issue a memory barrier between consumed count read and
1321 * write offset read, because consumed count can only change
1322 * concurrently in overwrite mode, and we keep a sequence counter
1323 * identifier derived from the write offset to check we are getting
1324 * the same sub-buffer we are expecting (the sub-buffers are atomically
1325 * "tagged" upon writes, tags are checked upon read).
1327 write_offset
= v_read(config
, &buf
->offset
);
1330 * Check that we are not about to read the same subbuffer in
1331 * which the writer head is.
1333 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1337 *consumed
= consumed_cur
;
1338 *produced
= subbuf_trunc(write_offset
, chan
);
1344 * The memory barriers __wait_event()/wake_up_interruptible() take care
1345 * of "raw_spin_is_locked" memory ordering.
1354 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1355 * are saved regardless of whether the consumed and produced positions are
1356 * in the same subbuffer.
1358 * @consumed: consumed byte count indicating the last position read
1359 * @produced: produced byte count indicating the last position written
1361 * This function is meant to provide information on the exact producer and
1362 * consumer positions without regard for the "snapshot" feature.
1364 int lib_ring_buffer_snapshot_sample_positions(
1365 struct lttng_ust_ring_buffer
*buf
,
1366 unsigned long *consumed
, unsigned long *produced
,
1367 struct lttng_ust_shm_handle
*handle
)
1369 struct lttng_ust_ring_buffer_channel
*chan
;
1370 const struct lttng_ust_ring_buffer_config
*config
;
1372 chan
= shmp(handle
, buf
->backend
.chan
);
1375 config
= &chan
->backend
.config
;
1377 *consumed
= uatomic_read(&buf
->consumed
);
1379 * No need to issue a memory barrier between consumed count read and
1380 * write offset read, because consumed count can only change
1381 * concurrently in overwrite mode, and we keep a sequence counter
1382 * identifier derived from the write offset to check we are getting
1383 * the same sub-buffer we are expecting (the sub-buffers are atomically
1384 * "tagged" upon writes, tags are checked upon read).
1386 *produced
= v_read(config
, &buf
->offset
);
1391 * lib_ring_buffer_move_consumer - move consumed counter forward
1393 * @consumed_new: new consumed count value
1395 void lib_ring_buffer_move_consumer(struct lttng_ust_ring_buffer
*buf
,
1396 unsigned long consumed_new
,
1397 struct lttng_ust_shm_handle
*handle
)
1399 struct lttng_ust_ring_buffer_backend
*bufb
= &buf
->backend
;
1400 struct lttng_ust_ring_buffer_channel
*chan
;
1401 unsigned long consumed
;
1403 chan
= shmp(handle
, bufb
->chan
);
1406 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1409 * Only push the consumed value forward.
1410 * If the consumed cmpxchg fails, this is because we have been pushed by
1411 * the writer in flight recorder mode.
1413 consumed
= uatomic_read(&buf
->consumed
);
1414 while ((long) consumed
- (long) consumed_new
< 0)
1415 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1420 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1422 * @consumed: consumed count indicating the position where to read
1424 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1425 * data to read at consumed position, or 0 if the get operation succeeds.
1427 int lib_ring_buffer_get_subbuf(struct lttng_ust_ring_buffer
*buf
,
1428 unsigned long consumed
,
1429 struct lttng_ust_shm_handle
*handle
)
1431 struct lttng_ust_ring_buffer_channel
*chan
;
1432 const struct lttng_ust_ring_buffer_config
*config
;
1433 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1434 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1435 struct commit_counters_cold
*cc_cold
;
1437 chan
= shmp(handle
, buf
->backend
.chan
);
1440 config
= &chan
->backend
.config
;
1442 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1444 * Read finalized before counters.
1447 consumed_cur
= uatomic_read(&buf
->consumed
);
1448 consumed_idx
= subbuf_index(consumed
, chan
);
1449 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1452 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1454 * Make sure we read the commit count before reading the buffer
1455 * data and the write offset. Correct consumed offset ordering
1456 * wrt commit count is insured by the use of cmpxchg to update
1457 * the consumed offset.
1460 * Local rmb to match the remote wmb to read the commit count
1461 * before the buffer data and the write offset.
1465 write_offset
= v_read(config
, &buf
->offset
);
1468 * Check that the buffer we are getting is after or at consumed_cur
1471 if ((long) subbuf_trunc(consumed
, chan
)
1472 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1476 * Check that the subbuffer we are trying to consume has been
1477 * already fully committed. There are a few causes that can make
1478 * this unavailability situation occur:
1480 * Temporary (short-term) situation:
1481 * - Application is running on a different CPU, between reserve
1482 * and commit ring buffer operations,
1483 * - Application is preempted between reserve and commit ring
1484 * buffer operations,
1486 * Long-term situation:
1487 * - Application is stopped (SIGSTOP) between reserve and commit
1488 * ring buffer operations. Could eventually be resumed by
1490 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1491 * reserve and commit ring buffer operation.
1493 * From a consumer perspective, handling short-term
1494 * unavailability situations is performed by retrying a few
1495 * times after a delay. Handling long-term unavailability
1496 * situations is handled by failing to get the sub-buffer.
1498 * In all of those situations, if the application is taking a
1499 * long time to perform its commit after ring buffer space
1500 * reservation, we can end up in a situation where the producer
1501 * will fill the ring buffer and try to write into the same
1502 * sub-buffer again (which has a missing commit). This is
1503 * handled by the producer in the sub-buffer switch handling
1504 * code of the reserve routine by detecting unbalanced
1505 * reserve/commit counters and discarding all further events
1506 * until the situation is resolved in those situations. Two
1507 * scenarios can occur:
1509 * 1) The application causing the reserve/commit counters to be
1510 * unbalanced has been terminated. In this situation, all
1511 * further events will be discarded in the buffers, and no
1512 * further buffer data will be readable by the consumer
1513 * daemon. Tearing down the UST tracing session and starting
1514 * anew is a work-around for those situations. Note that this
1515 * only affects per-UID tracing. In per-PID tracing, the
1516 * application vanishes with the termination, and therefore
1517 * no more data needs to be written to the buffers.
1518 * 2) The application causing the unbalance has been delayed for
1519 * a long time, but will eventually try to increment the
1520 * commit counter after eventually writing to the sub-buffer.
1521 * This situation can cause events to be discarded until the
1522 * application resumes its operations.
1524 if (((commit_count
- chan
->backend
.subbuf_size
)
1525 & chan
->commit_count_mask
)
1526 - (buf_trunc(consumed
, chan
)
1527 >> chan
->backend
.num_subbuf_order
)
1529 if (nr_retry
-- > 0) {
1530 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1531 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1539 * Check that we are not about to read the same subbuffer in
1540 * which the writer head is.
1542 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1547 * Failure to get the subbuffer causes a busy-loop retry without going
1548 * to a wait queue. These are caused by short-lived race windows where
1549 * the writer is getting access to a subbuffer we were trying to get
1550 * access to. Also checks that the "consumed" buffer count we are
1551 * looking for matches the one contained in the subbuffer id.
1553 * The short-lived race window described here can be affected by
1554 * application signals and preemption, thus requiring to bound
1555 * the loop to a maximum number of retry.
1557 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1558 consumed_idx
, buf_trunc_val(consumed
, chan
),
1561 if (nr_retry
-- > 0) {
1562 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1563 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1569 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1571 buf
->get_subbuf_consumed
= consumed
;
1572 buf
->get_subbuf
= 1;
1578 * The memory barriers __wait_event()/wake_up_interruptible() take care
1579 * of "raw_spin_is_locked" memory ordering.
1588 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1591 void lib_ring_buffer_put_subbuf(struct lttng_ust_ring_buffer
*buf
,
1592 struct lttng_ust_shm_handle
*handle
)
1594 struct lttng_ust_ring_buffer_backend
*bufb
= &buf
->backend
;
1595 struct lttng_ust_ring_buffer_channel
*chan
;
1596 const struct lttng_ust_ring_buffer_config
*config
;
1597 unsigned long sb_bindex
, consumed_idx
, consumed
;
1598 struct lttng_ust_ring_buffer_backend_pages_shmp
*rpages
;
1599 struct lttng_ust_ring_buffer_backend_pages
*backend_pages
;
1601 chan
= shmp(handle
, bufb
->chan
);
1604 config
= &chan
->backend
.config
;
1605 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1607 if (!buf
->get_subbuf
) {
1609 * Reader puts a subbuffer it did not get.
1611 CHAN_WARN_ON(chan
, 1);
1614 consumed
= buf
->get_subbuf_consumed
;
1615 buf
->get_subbuf
= 0;
1618 * Clear the records_unread counter. (overruns counter)
1619 * Can still be non-zero if a file reader simply grabbed the data
1620 * without using iterators.
1621 * Can be below zero if an iterator is used on a snapshot more than
1624 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1625 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1628 backend_pages
= shmp(handle
, rpages
->shmp
);
1631 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1632 &bufb
->records_read
);
1633 v_set(config
, &backend_pages
->records_unread
, 0);
1634 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1635 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1636 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1639 * Exchange the reader subbuffer with the one we put in its place in the
1640 * writer subbuffer table. Expect the original consumed count. If
1641 * update_read_sb_index fails, this is because the writer updated the
1642 * subbuffer concurrently. We should therefore keep the subbuffer we
1643 * currently have: it has become invalid to try reading this sub-buffer
1644 * consumed count value anyway.
1646 consumed_idx
= subbuf_index(consumed
, chan
);
1647 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1648 consumed_idx
, buf_trunc_val(consumed
, chan
),
1651 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1652 * if the writer concurrently updated it.
1657 * cons_offset is an iterator on all subbuffer offsets between the reader
1658 * position and the writer position. (inclusive)
1661 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_ring_buffer
*buf
,
1662 struct lttng_ust_ring_buffer_channel
*chan
,
1663 unsigned long cons_offset
,
1665 struct lttng_ust_shm_handle
*handle
)
1667 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1668 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1669 struct commit_counters_hot
*cc_hot
;
1670 struct commit_counters_cold
*cc_cold
;
1672 cons_idx
= subbuf_index(cons_offset
, chan
);
1673 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1676 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1679 commit_count
= v_read(config
, &cc_hot
->cc
);
1680 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1682 if (subbuf_offset(commit_count
, chan
) != 0)
1683 DBG("ring buffer %s, cpu %d: "
1684 "commit count in subbuffer %lu,\n"
1685 "expecting multiples of %lu bytes\n"
1686 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1687 chan
->backend
.name
, cpu
, cons_idx
,
1688 chan
->backend
.subbuf_size
,
1689 commit_count
, commit_count_sb
);
1691 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1692 chan
->backend
.name
, cpu
, commit_count
);
1696 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_ring_buffer
*buf
,
1697 struct lttng_ust_ring_buffer_channel
*chan
,
1698 int cpu
, struct lttng_ust_shm_handle
*handle
)
1700 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1701 unsigned long write_offset
, cons_offset
;
1704 * No need to order commit_count, write_offset and cons_offset reads
1705 * because we execute at teardown when no more writer nor reader
1706 * references are left.
1708 write_offset
= v_read(config
, &buf
->offset
);
1709 cons_offset
= uatomic_read(&buf
->consumed
);
1710 if (write_offset
!= cons_offset
)
1711 DBG("ring buffer %s, cpu %d: "
1712 "non-consumed data\n"
1713 " [ %lu bytes written, %lu bytes read ]\n",
1714 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1716 for (cons_offset
= uatomic_read(&buf
->consumed
);
1717 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1720 cons_offset
= subbuf_align(cons_offset
, chan
))
1721 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1726 void lib_ring_buffer_print_errors(struct lttng_ust_ring_buffer_channel
*chan
,
1727 struct lttng_ust_ring_buffer
*buf
, int cpu
,
1728 struct lttng_ust_shm_handle
*handle
)
1730 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1732 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1733 DBG("ring buffer %s: %lu records written, "
1734 "%lu records overrun\n",
1736 v_read(config
, &buf
->records_count
),
1737 v_read(config
, &buf
->records_overrun
));
1739 DBG("ring buffer %s, cpu %d: %lu records written, "
1740 "%lu records overrun\n",
1741 chan
->backend
.name
, cpu
,
1742 v_read(config
, &buf
->records_count
),
1743 v_read(config
, &buf
->records_overrun
));
1745 if (v_read(config
, &buf
->records_lost_full
)
1746 || v_read(config
, &buf
->records_lost_wrap
)
1747 || v_read(config
, &buf
->records_lost_big
))
1748 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1749 " [ %lu buffer full, %lu nest buffer wrap-around, "
1750 "%lu event too big ]\n",
1751 chan
->backend
.name
, cpu
,
1752 v_read(config
, &buf
->records_lost_full
),
1753 v_read(config
, &buf
->records_lost_wrap
),
1754 v_read(config
, &buf
->records_lost_big
));
1756 lib_ring_buffer_print_buffer_errors(buf
, chan
, cpu
, handle
);
1760 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1762 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1763 * active or at buffer finalization (destroy).
1766 void lib_ring_buffer_switch_old_start(struct lttng_ust_ring_buffer
*buf
,
1767 struct lttng_ust_ring_buffer_channel
*chan
,
1768 struct switch_offsets
*offsets
,
1769 const struct lttng_ust_ring_buffer_ctx
*ctx
,
1770 struct lttng_ust_shm_handle
*handle
)
1772 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1773 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1774 unsigned long commit_count
;
1775 struct commit_counters_hot
*cc_hot
;
1777 config
->cb
.buffer_begin(buf
, ctx
->priv
->timestamp
, oldidx
, handle
);
1780 * Order all writes to buffer before the commit count update that will
1781 * determine that the subbuffer is full.
1784 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1787 v_add(config
, config
->cb
.subbuffer_header_size(),
1789 commit_count
= v_read(config
, &cc_hot
->cc
);
1790 /* Check if the written buffer has to be delivered */
1791 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1792 commit_count
, oldidx
, handle
, ctx
);
1793 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1794 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1795 commit_count
, handle
, cc_hot
);
1799 * lib_ring_buffer_switch_old_end: switch old subbuffer
1801 * Note : offset_old should never be 0 here. It is ok, because we never perform
1802 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1803 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1807 void lib_ring_buffer_switch_old_end(struct lttng_ust_ring_buffer
*buf
,
1808 struct lttng_ust_ring_buffer_channel
*chan
,
1809 struct switch_offsets
*offsets
,
1810 const struct lttng_ust_ring_buffer_ctx
*ctx
,
1811 struct lttng_ust_shm_handle
*handle
)
1813 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1814 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1815 unsigned long commit_count
, padding_size
, data_size
;
1816 struct commit_counters_hot
*cc_hot
;
1819 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1820 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1821 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1824 ts_end
= shmp_index(handle
, buf
->ts_end
, oldidx
);
1828 * This is the last space reservation in that sub-buffer before
1829 * it gets delivered. This provides exclusive access to write to
1830 * this sub-buffer's ts_end. There are also no concurrent
1831 * readers of that ts_end because delivery of that sub-buffer is
1832 * postponed until the commit counter is incremented for the
1833 * current space reservation.
1835 *ts_end
= ctx
->priv
->timestamp
;
1838 * Order all writes to buffer and store to ts_end before the commit
1839 * count update that will determine that the subbuffer is full.
1842 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1845 v_add(config
, padding_size
, &cc_hot
->cc
);
1846 commit_count
= v_read(config
, &cc_hot
->cc
);
1847 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1848 commit_count
, oldidx
, handle
, ctx
);
1849 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1850 offsets
->old
+ padding_size
, commit_count
, handle
,
1855 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1857 * This code can be executed unordered : writers may already have written to the
1858 * sub-buffer before this code gets executed, caution. The commit makes sure
1859 * that this code is executed before the deliver of this sub-buffer.
1862 void lib_ring_buffer_switch_new_start(struct lttng_ust_ring_buffer
*buf
,
1863 struct lttng_ust_ring_buffer_channel
*chan
,
1864 struct switch_offsets
*offsets
,
1865 const struct lttng_ust_ring_buffer_ctx
*ctx
,
1866 struct lttng_ust_shm_handle
*handle
)
1868 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1869 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1870 unsigned long commit_count
;
1871 struct commit_counters_hot
*cc_hot
;
1873 config
->cb
.buffer_begin(buf
, ctx
->priv
->timestamp
, beginidx
, handle
);
1876 * Order all writes to buffer before the commit count update that will
1877 * determine that the subbuffer is full.
1880 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1883 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1884 commit_count
= v_read(config
, &cc_hot
->cc
);
1885 /* Check if the written buffer has to be delivered */
1886 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1887 commit_count
, beginidx
, handle
, ctx
);
1888 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1889 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1890 commit_count
, handle
, cc_hot
);
1894 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1896 * Calls subbuffer_set_data_size() to set the data size of the current
1897 * sub-buffer. We do not need to perform check_deliver nor commit here,
1898 * since this task will be done by the "commit" of the event for which
1899 * we are currently doing the space reservation.
1902 void lib_ring_buffer_switch_new_end(struct lttng_ust_ring_buffer
*buf
,
1903 struct lttng_ust_ring_buffer_channel
*chan
,
1904 struct switch_offsets
*offsets
,
1905 const struct lttng_ust_ring_buffer_ctx
*ctx
,
1906 struct lttng_ust_shm_handle
*handle
)
1908 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1909 unsigned long endidx
, data_size
;
1912 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1913 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1914 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1916 ts_end
= shmp_index(handle
, buf
->ts_end
, endidx
);
1920 * This is the last space reservation in that sub-buffer before
1921 * it gets delivered. This provides exclusive access to write to
1922 * this sub-buffer's ts_end. There are also no concurrent
1923 * readers of that ts_end because delivery of that sub-buffer is
1924 * postponed until the commit counter is incremented for the
1925 * current space reservation.
1927 *ts_end
= ctx
->priv
->timestamp
;
1933 * !0 if execution must be aborted.
1936 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1937 struct lttng_ust_ring_buffer
*buf
,
1938 struct lttng_ust_ring_buffer_channel
*chan
,
1939 struct switch_offsets
*offsets
,
1940 struct lttng_ust_ring_buffer_ctx
*ctx
,
1941 struct lttng_ust_shm_handle
*handle
)
1943 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1944 unsigned long off
, reserve_commit_diff
;
1946 offsets
->begin
= v_read(config
, &buf
->offset
);
1947 offsets
->old
= offsets
->begin
;
1948 offsets
->switch_old_start
= 0;
1949 off
= subbuf_offset(offsets
->begin
, chan
);
1951 ctx
->priv
->timestamp
= config
->cb
.ring_buffer_clock_read(chan
);
1954 * Ensure we flush the header of an empty subbuffer when doing the
1955 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1956 * total data gathering duration even if there were no records saved
1957 * after the last buffer switch.
1958 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1959 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1960 * subbuffer header as appropriate.
1961 * The next record that reserves space will be responsible for
1962 * populating the following subbuffer header. We choose not to populate
1963 * the next subbuffer header here because we want to be able to use
1964 * SWITCH_ACTIVE for periodical buffer flush, which must
1965 * guarantee that all the buffer content (records and header
1966 * timestamps) are visible to the reader. This is required for
1967 * quiescence guarantees for the fusion merge.
1969 if (mode
!= SWITCH_FLUSH
&& !off
)
1970 return -1; /* we do not have to switch : buffer is empty */
1972 if (caa_unlikely(off
== 0)) {
1973 unsigned long sb_index
, commit_count
;
1974 struct commit_counters_cold
*cc_cold
;
1977 * We are performing a SWITCH_FLUSH. There may be concurrent
1978 * writes into the buffer if e.g. invoked while performing a
1979 * snapshot on an active trace.
1981 * If the client does not save any header information
1982 * (sub-buffer header size == 0), don't switch empty subbuffer
1983 * on finalize, because it is invalid to deliver a completely
1986 if (!config
->cb
.subbuffer_header_size())
1989 /* Test new buffer integrity */
1990 sb_index
= subbuf_index(offsets
->begin
, chan
);
1991 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1994 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1995 reserve_commit_diff
=
1996 (buf_trunc(offsets
->begin
, chan
)
1997 >> chan
->backend
.num_subbuf_order
)
1998 - (commit_count
& chan
->commit_count_mask
);
1999 if (caa_likely(reserve_commit_diff
== 0)) {
2000 /* Next subbuffer not being written to. */
2001 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2002 subbuf_trunc(offsets
->begin
, chan
)
2003 - subbuf_trunc((unsigned long)
2004 uatomic_read(&buf
->consumed
), chan
)
2005 >= chan
->backend
.buf_size
)) {
2007 * We do not overwrite non consumed buffers
2008 * and we are full : don't switch.
2013 * Next subbuffer not being written to, and we
2014 * are either in overwrite mode or the buffer is
2015 * not full. It's safe to write in this new
2021 * Next subbuffer reserve offset does not match the
2022 * commit offset. Don't perform switch in
2023 * producer-consumer and overwrite mode. Caused by
2024 * either a writer OOPS or too many nested writes over a
2025 * reserve/commit pair.
2031 * Need to write the subbuffer start header on finalize.
2033 offsets
->switch_old_start
= 1;
2035 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2036 /* Note: old points to the next subbuf at offset 0 */
2037 offsets
->end
= offsets
->begin
;
2039 * Populate the records lost counters prior to performing a
2040 * sub-buffer switch.
2042 ctx
->priv
->records_lost_full
= v_read(config
, &buf
->records_lost_full
);
2043 ctx
->priv
->records_lost_wrap
= v_read(config
, &buf
->records_lost_wrap
);
2044 ctx
->priv
->records_lost_big
= v_read(config
, &buf
->records_lost_big
);
2049 * Force a sub-buffer switch. This operation is completely reentrant : can be
2050 * called while tracing is active with absolutely no lock held.
2052 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2053 * some atomic operations, this function must be called from the CPU
2054 * which owns the buffer for a ACTIVE flush. However, for
2055 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2058 void lib_ring_buffer_switch_slow(struct lttng_ust_ring_buffer
*buf
, enum switch_mode mode
,
2059 struct lttng_ust_shm_handle
*handle
)
2061 struct lttng_ust_ring_buffer_channel
*chan
;
2062 const struct lttng_ust_ring_buffer_config
*config
;
2063 struct lttng_ust_ring_buffer_ctx_private ctx_priv
;
2064 struct lttng_ust_ring_buffer_ctx ctx
;
2065 struct switch_offsets offsets
;
2066 unsigned long oldidx
;
2068 ctx
.priv
= &ctx_priv
;
2069 chan
= shmp(handle
, buf
->backend
.chan
);
2072 config
= &chan
->backend
.config
;
2077 * Perform retryable operations.
2080 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
2082 return; /* Switch not needed */
2083 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
2087 * Atomically update last_timestamp. This update races against concurrent
2088 * atomic updates, but the race will always cause supplementary full
2089 * timestamp records, never the opposite (missing a full timestamp
2090 * record when it would be needed).
2092 save_last_timestamp(config
, buf
, ctx
.priv
->timestamp
);
2095 * Push the reader if necessary
2097 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
2099 oldidx
= subbuf_index(offsets
.old
, chan
);
2100 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
2103 * May need to populate header start on SWITCH_FLUSH.
2105 if (offsets
.switch_old_start
) {
2106 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, &ctx
, handle
);
2107 offsets
.old
+= config
->cb
.subbuffer_header_size();
2111 * Switch old subbuffer.
2113 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, &ctx
, handle
);
2117 bool handle_blocking_retry(int *timeout_left_ms
)
2119 int timeout
= *timeout_left_ms
, delay
;
2121 if (caa_likely(!timeout
))
2122 return false; /* Do not retry, discard event. */
2123 if (timeout
< 0) /* Wait forever. */
2124 delay
= RETRY_DELAY_MS
;
2126 delay
= min_t(int, timeout
, RETRY_DELAY_MS
);
2127 (void) poll(NULL
, 0, delay
);
2129 *timeout_left_ms
-= delay
;
2130 return true; /* Retry. */
2136 * -ENOSPC if event size is too large for packet.
2137 * -ENOBUFS if there is currently not enough space in buffer for the event.
2138 * -EIO if data cannot be written into the buffer for any other reason.
2141 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_ring_buffer
*buf
,
2142 struct lttng_ust_ring_buffer_channel
*chan
,
2143 struct switch_offsets
*offsets
,
2144 struct lttng_ust_ring_buffer_ctx
*ctx
,
2147 struct lttng_ust_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
2148 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
2149 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
2150 unsigned long reserve_commit_diff
, offset_cmp
;
2151 int timeout_left_ms
= lttng_ust_ringbuffer_get_timeout(chan
);
2154 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2155 offsets
->old
= offsets
->begin
;
2156 offsets
->switch_new_start
= 0;
2157 offsets
->switch_new_end
= 0;
2158 offsets
->switch_old_end
= 0;
2159 offsets
->pre_header_padding
= 0;
2161 ctx_private
->timestamp
= config
->cb
.ring_buffer_clock_read(chan
);
2162 if ((int64_t) ctx_private
->timestamp
== -EIO
)
2165 if (last_timestamp_overflow(config
, buf
, ctx_private
->timestamp
))
2166 ctx_private
->rflags
|= RING_BUFFER_RFLAG_FULL_TIMESTAMP
;
2168 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) == 0)) {
2169 offsets
->switch_new_start
= 1; /* For offsets->begin */
2171 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2173 &offsets
->pre_header_padding
,
2176 lttng_ust_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2179 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2180 offsets
->size
> chan
->backend
.subbuf_size
)) {
2181 offsets
->switch_old_end
= 1; /* For offsets->old */
2182 offsets
->switch_new_start
= 1; /* For offsets->begin */
2185 if (caa_unlikely(offsets
->switch_new_start
)) {
2186 unsigned long sb_index
, commit_count
;
2187 struct commit_counters_cold
*cc_cold
;
2190 * We are typically not filling the previous buffer completely.
2192 if (caa_likely(offsets
->switch_old_end
))
2193 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2194 offsets
->begin
= offsets
->begin
2195 + config
->cb
.subbuffer_header_size();
2196 /* Test new buffer integrity */
2197 sb_index
= subbuf_index(offsets
->begin
, chan
);
2199 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2200 * lib_ring_buffer_check_deliver() has the matching
2201 * memory barriers required around commit_cold cc_sb
2202 * updates to ensure reserve and commit counter updates
2203 * are not seen reordered when updated by another CPU.
2206 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2209 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2210 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2212 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2214 * The reserve counter have been concurrently updated
2215 * while we read the commit counter. This means the
2216 * commit counter we read might not match buf->offset
2217 * due to concurrent update. We therefore need to retry.
2221 reserve_commit_diff
=
2222 (buf_trunc(offsets
->begin
, chan
)
2223 >> chan
->backend
.num_subbuf_order
)
2224 - (commit_count
& chan
->commit_count_mask
);
2225 if (caa_likely(reserve_commit_diff
== 0)) {
2226 /* Next subbuffer not being written to. */
2227 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2228 subbuf_trunc(offsets
->begin
, chan
)
2229 - subbuf_trunc((unsigned long)
2230 uatomic_read(&buf
->consumed
), chan
)
2231 >= chan
->backend
.buf_size
)) {
2232 unsigned long nr_lost
;
2234 if (handle_blocking_retry(&timeout_left_ms
))
2238 * We do not overwrite non consumed buffers
2239 * and we are full : record is lost.
2241 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2242 v_inc(config
, &buf
->records_lost_full
);
2243 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2244 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2245 nr_lost
+ 1, chan
->backend
.name
,
2251 * Next subbuffer not being written to, and we
2252 * are either in overwrite mode or the buffer is
2253 * not full. It's safe to write in this new
2258 unsigned long nr_lost
;
2261 * Next subbuffer reserve offset does not match the
2262 * commit offset, and this did not involve update to the
2263 * reserve counter. Drop record in producer-consumer and
2264 * overwrite mode. Caused by either a writer OOPS or too
2265 * many nested writes over a reserve/commit pair.
2267 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2268 v_inc(config
, &buf
->records_lost_wrap
);
2269 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2270 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2271 nr_lost
+ 1, chan
->backend
.name
,
2277 config
->cb
.record_header_size(config
, chan
,
2279 &offsets
->pre_header_padding
,
2282 lttng_ust_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2285 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2286 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2287 unsigned long nr_lost
;
2290 * Record too big for subbuffers, report error, don't
2291 * complete the sub-buffer switch.
2293 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2294 v_inc(config
, &buf
->records_lost_big
);
2295 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2296 DBG("%lu or more records lost in (%s:%d) record size "
2297 " of %zu bytes is too large for buffer\n",
2298 nr_lost
+ 1, chan
->backend
.name
,
2299 buf
->backend
.cpu
, offsets
->size
);
2304 * We just made a successful buffer switch and the
2305 * record fits in the new subbuffer. Let's write.
2310 * Record fits in the current buffer and we are not on a switch
2311 * boundary. It's safe to write.
2314 offsets
->end
= offsets
->begin
+ offsets
->size
;
2316 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2318 * The offset_end will fall at the very beginning of the next
2321 offsets
->switch_new_end
= 1; /* For offsets->begin */
2324 * Populate the records lost counters when the space reservation
2325 * may cause a sub-buffer switch.
2327 if (offsets
->switch_new_end
|| offsets
->switch_old_end
) {
2328 ctx_private
->records_lost_full
= v_read(config
, &buf
->records_lost_full
);
2329 ctx_private
->records_lost_wrap
= v_read(config
, &buf
->records_lost_wrap
);
2330 ctx_private
->records_lost_big
= v_read(config
, &buf
->records_lost_big
);
2336 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2337 * @ctx: ring buffer context.
2339 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2340 * -EIO for other errors, else returns 0.
2341 * It will take care of sub-buffer switching.
2343 int lib_ring_buffer_reserve_slow(struct lttng_ust_ring_buffer_ctx
*ctx
,
2346 struct lttng_ust_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
2347 struct lttng_ust_ring_buffer_channel
*chan
= ctx_private
->chan
;
2348 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
2349 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
2350 struct lttng_ust_ring_buffer
*buf
;
2351 struct switch_offsets offsets
;
2354 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2355 buf
= shmp(handle
, chan
->backend
.buf
[ctx_private
->reserve_cpu
].shmp
);
2357 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2360 ctx_private
->buf
= buf
;
2365 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2367 if (caa_unlikely(ret
))
2369 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2374 * Atomically update last_timestamp. This update races against concurrent
2375 * atomic updates, but the race will always cause supplementary full
2376 * timestamp records, never the opposite (missing a full timestamp
2377 * record when it would be needed).
2379 save_last_timestamp(config
, buf
, ctx_private
->timestamp
);
2382 * Push the reader if necessary
2384 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2387 * Clear noref flag for this subbuffer.
2389 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2390 subbuf_index(offsets
.end
- 1, chan
),
2394 * Switch old subbuffer if needed.
2396 if (caa_unlikely(offsets
.switch_old_end
)) {
2397 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2398 subbuf_index(offsets
.old
- 1, chan
),
2400 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
, handle
);
2404 * Populate new subbuffer.
2406 if (caa_unlikely(offsets
.switch_new_start
))
2407 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
, handle
);
2409 if (caa_unlikely(offsets
.switch_new_end
))
2410 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
, handle
);
2412 ctx_private
->slot_size
= offsets
.size
;
2413 ctx_private
->pre_offset
= offsets
.begin
;
2414 ctx_private
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2419 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_ring_buffer_config
*config
,
2420 struct lttng_ust_ring_buffer
*buf
,
2421 unsigned long commit_count
,
2423 struct lttng_ust_shm_handle
*handle
)
2425 struct commit_counters_hot
*cc_hot
;
2427 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2429 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2432 v_set(config
, &cc_hot
->seq
, commit_count
);
2436 * The ring buffer can count events recorded and overwritten per buffer,
2437 * but it is disabled by default due to its performance overhead.
2439 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2441 void deliver_count_events(const struct lttng_ust_ring_buffer_config
*config
,
2442 struct lttng_ust_ring_buffer
*buf
,
2444 struct lttng_ust_shm_handle
*handle
)
2446 v_add(config
, subbuffer_get_records_count(config
,
2447 &buf
->backend
, idx
, handle
),
2448 &buf
->records_count
);
2449 v_add(config
, subbuffer_count_records_overrun(config
,
2450 &buf
->backend
, idx
, handle
),
2451 &buf
->records_overrun
);
2453 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2455 void deliver_count_events(
2456 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
2457 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
2458 unsigned long idx
__attribute__((unused
)),
2459 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
2462 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2464 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_ring_buffer_config
*config
,
2465 struct lttng_ust_ring_buffer
*buf
,
2466 struct lttng_ust_ring_buffer_channel
*chan
,
2467 unsigned long offset
,
2468 unsigned long commit_count
,
2470 struct lttng_ust_shm_handle
*handle
,
2471 const struct lttng_ust_ring_buffer_ctx
*ctx
)
2473 unsigned long old_commit_count
= commit_count
2474 - chan
->backend
.subbuf_size
;
2475 struct commit_counters_cold
*cc_cold
;
2478 * If we succeeded at updating cc_sb below, we are the subbuffer
2479 * writer delivering the subbuffer. Deals with concurrent
2480 * updates of the "cc" value without adding a add_return atomic
2481 * operation to the fast path.
2483 * We are doing the delivery in two steps:
2484 * - First, we cmpxchg() cc_sb to the new value
2485 * old_commit_count + 1. This ensures that we are the only
2486 * subbuffer user successfully filling the subbuffer, but we
2487 * do _not_ set the cc_sb value to "commit_count" yet.
2488 * Therefore, other writers that would wrap around the ring
2489 * buffer and try to start writing to our subbuffer would
2490 * have to drop records, because it would appear as
2492 * We therefore have exclusive access to the subbuffer control
2493 * structures. This mutual exclusion with other writers is
2494 * crucially important to perform record overruns count in
2495 * flight recorder mode locklessly.
2496 * - When we are ready to release the subbuffer (either for
2497 * reading or for overrun by other writers), we simply set the
2498 * cc_sb value to "commit_count" and perform delivery.
2500 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2501 * This guarantees that old_commit_count + 1 != commit_count.
2505 * Order prior updates to reserve count prior to the
2506 * commit_cold cc_sb update.
2509 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2512 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2513 old_commit_count
, old_commit_count
+ 1)
2514 == old_commit_count
)) {
2518 * Start of exclusive subbuffer access. We are
2519 * guaranteed to be the last writer in this subbuffer
2520 * and any other writer trying to access this subbuffer
2521 * in this state is required to drop records.
2523 * We can read the ts_end for the current sub-buffer
2524 * which has been saved by the very last space
2525 * reservation for the current sub-buffer.
2527 * Order increment of commit counter before reading ts_end.
2530 ts_end
= shmp_index(handle
, buf
->ts_end
, idx
);
2533 deliver_count_events(config
, buf
, idx
, handle
);
2534 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2535 lib_ring_buffer_get_data_size(config
,
2542 * Increment the packet counter while we have exclusive
2545 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2548 * Set noref flag and offset for this subbuffer id.
2549 * Contains a memory barrier that ensures counter stores
2550 * are ordered before set noref and offset.
2552 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2553 buf_trunc_val(offset
, chan
), handle
);
2556 * Order set_noref and record counter updates before the
2557 * end of subbuffer exclusive access. Orders with
2558 * respect to writers coming into the subbuffer after
2559 * wrap around, and also order wrt concurrent readers.
2562 /* End of exclusive subbuffer access */
2563 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2565 * Order later updates to reserve count after
2566 * the commit cold cc_sb update.
2569 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2570 commit_count
, idx
, handle
);
2573 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2575 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2576 && uatomic_read(&buf
->active_readers
)
2577 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2578 lib_ring_buffer_wakeup(buf
, handle
);
2584 * Force a read (imply TLS allocation for dlopen) of TLS variables.
2586 void lttng_ringbuffer_alloc_tls(void)
2588 __asm__
__volatile__ ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2591 void lib_ringbuffer_signal_init(void)
2597 * Block signal for entire process, so only our thread processes
2601 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2604 PERROR("pthread_sigmask");