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 <ust-helper.h>
56 #include <lttng/ringbuffer-config.h>
62 #include "../liblttng-ust/compat.h" /* For ENODATA */
64 /* Print DBG() messages about events lost only every 1048576 hits */
65 #define DBG_PRINT_NR_LOST (1UL << 20)
67 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
68 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
69 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
70 #define CLOCKID CLOCK_MONOTONIC
71 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
72 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
73 #define RETRY_DELAY_MS 100 /* 100 ms. */
76 * Non-static to ensure the compiler does not optimize away the xor.
78 uint8_t lttng_crash_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
81 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
82 * close(2) to close the fd returned by shm_open.
83 * shm_unlink releases the shared memory object name.
84 * ftruncate(2) sets the size of the memory object.
85 * mmap/munmap maps the shared memory obj to a virtual address in the
86 * calling proceess (should be done both in libust and consumer).
87 * See shm_overview(7) for details.
88 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
91 * Since we don't need to access the object using its name, we can
92 * immediately shm_unlink(3) it, and only keep the handle with its file
97 * Internal structure representing offsets to use at a sub-buffer switch.
99 struct switch_offsets
{
100 unsigned long begin
, end
, old
;
101 size_t pre_header_padding
, size
;
102 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
106 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
109 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
112 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
115 void lib_ring_buffer_print_errors(struct channel
*chan
,
116 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
117 struct lttng_ust_shm_handle
*handle
);
120 * Handle timer teardown race wrt memory free of private data by
121 * ring buffer signals are handled by a single thread, which permits
122 * a synchronization point between handling of each signal.
123 * Protected by the lock within the structure.
125 struct timer_signal_data
{
126 pthread_t tid
; /* thread id managing signals */
129 pthread_mutex_t lock
;
132 static struct timer_signal_data timer_signal
= {
136 .lock
= PTHREAD_MUTEX_INITIALIZER
,
139 static bool lttng_ust_allow_blocking
;
141 void lttng_ust_ringbuffer_set_allow_blocking(void)
143 lttng_ust_allow_blocking
= true;
146 /* Get blocking timeout, in ms */
147 static int lttng_ust_ringbuffer_get_timeout(struct channel
*chan
)
149 if (!lttng_ust_allow_blocking
)
151 return chan
->u
.s
.blocking_timeout_ms
;
155 * lib_ring_buffer_reset - Reset ring buffer to initial values.
158 * Effectively empty the ring buffer. Should be called when the buffer is not
159 * used for writing. The ring buffer can be opened for reading, but the reader
160 * should not be using the iterator concurrently with reset. The previous
161 * current iterator record is reset.
163 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
164 struct lttng_ust_shm_handle
*handle
)
166 struct channel
*chan
;
167 const struct lttng_ust_lib_ring_buffer_config
*config
;
170 chan
= shmp(handle
, buf
->backend
.chan
);
173 config
= &chan
->backend
.config
;
175 * Reset iterator first. It will put the subbuffer if it currently holds
178 v_set(config
, &buf
->offset
, 0);
179 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
180 struct commit_counters_hot
*cc_hot
;
181 struct commit_counters_cold
*cc_cold
;
184 cc_hot
= shmp_index(handle
, buf
->commit_hot
, i
);
187 cc_cold
= shmp_index(handle
, buf
->commit_cold
, i
);
190 ts_end
= shmp_index(handle
, buf
->ts_end
, i
);
193 v_set(config
, &cc_hot
->cc
, 0);
194 v_set(config
, &cc_hot
->seq
, 0);
195 v_set(config
, &cc_cold
->cc_sb
, 0);
198 uatomic_set(&buf
->consumed
, 0);
199 uatomic_set(&buf
->record_disabled
, 0);
200 v_set(config
, &buf
->last_tsc
, 0);
201 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
202 /* Don't reset number of active readers */
203 v_set(config
, &buf
->records_lost_full
, 0);
204 v_set(config
, &buf
->records_lost_wrap
, 0);
205 v_set(config
, &buf
->records_lost_big
, 0);
206 v_set(config
, &buf
->records_count
, 0);
207 v_set(config
, &buf
->records_overrun
, 0);
212 * channel_reset - Reset channel to initial values.
215 * Effectively empty the channel. Should be called when the channel is not used
216 * for writing. The channel can be opened for reading, but the reader should not
217 * be using the iterator concurrently with reset. The previous current iterator
220 void channel_reset(struct channel
*chan
)
223 * Reset iterators first. Will put the subbuffer if held for reading.
225 uatomic_set(&chan
->record_disabled
, 0);
226 /* Don't reset commit_count_mask, still valid */
227 channel_backend_reset(&chan
->backend
);
228 /* Don't reset switch/read timer interval */
229 /* Don't reset notifiers and notifier enable bits */
230 /* Don't reset reader reference count */
234 void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config
*config
,
235 struct lttng_crash_abi
*crash_abi
,
236 struct lttng_ust_lib_ring_buffer
*buf
,
237 struct channel_backend
*chanb
,
238 struct shm_object
*shmobj
,
239 struct lttng_ust_shm_handle
*handle
)
243 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
244 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
245 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
246 crash_abi
->endian
= RB_CRASH_ENDIAN
;
247 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
248 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
249 crash_abi
->word_size
= sizeof(unsigned long);
250 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
252 /* Offset of fields */
253 crash_abi
->offset
.prod_offset
=
254 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
255 crash_abi
->offset
.consumed_offset
=
256 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
257 crash_abi
->offset
.commit_hot_array
=
258 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
259 crash_abi
->offset
.commit_hot_seq
=
260 offsetof(struct commit_counters_hot
, seq
);
261 crash_abi
->offset
.buf_wsb_array
=
262 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
263 crash_abi
->offset
.buf_wsb_id
=
264 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
, id
);
265 crash_abi
->offset
.sb_array
=
266 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
267 crash_abi
->offset
.sb_array_shmp_offset
=
268 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
,
270 crash_abi
->offset
.sb_backend_p_offset
=
271 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages
,
275 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
276 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
277 crash_abi
->length
.commit_hot_seq
=
278 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
279 crash_abi
->length
.buf_wsb_id
=
280 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer
*) NULL
)->id
);
281 crash_abi
->length
.sb_array_shmp_offset
=
282 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
283 crash_abi
->length
.sb_backend_p_offset
=
284 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
287 crash_abi
->stride
.commit_hot_array
=
288 sizeof(struct commit_counters_hot
);
289 crash_abi
->stride
.buf_wsb_array
=
290 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
);
291 crash_abi
->stride
.sb_array
=
292 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
);
294 /* Buffer constants */
295 crash_abi
->buf_size
= chanb
->buf_size
;
296 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
297 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
298 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
300 if (config
->cb
.content_size_field
) {
301 size_t offset
, length
;
303 config
->cb
.content_size_field(config
, &offset
, &length
);
304 crash_abi
->offset
.content_size
= offset
;
305 crash_abi
->length
.content_size
= length
;
307 crash_abi
->offset
.content_size
= 0;
308 crash_abi
->length
.content_size
= 0;
310 if (config
->cb
.packet_size_field
) {
311 size_t offset
, length
;
313 config
->cb
.packet_size_field(config
, &offset
, &length
);
314 crash_abi
->offset
.packet_size
= offset
;
315 crash_abi
->length
.packet_size
= length
;
317 crash_abi
->offset
.packet_size
= 0;
318 crash_abi
->length
.packet_size
= 0;
323 * Must be called under cpu hotplug protection.
325 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
326 struct channel_backend
*chanb
, int cpu
,
327 struct lttng_ust_shm_handle
*handle
,
328 struct shm_object
*shmobj
)
330 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
331 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
332 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
333 struct channel
*shmp_chan
;
334 struct commit_counters_hot
*cc_hot
;
335 void *priv
= channel_get_private(chan
);
336 size_t subbuf_header_size
;
340 /* Test for cpu hotplug */
341 if (buf
->backend
.allocated
)
344 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
345 set_shmp(buf
->commit_hot
,
347 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
348 if (!shmp(handle
, buf
->commit_hot
)) {
352 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
353 set_shmp(buf
->commit_cold
,
355 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
356 if (!shmp(handle
, buf
->commit_cold
)) {
361 align_shm(shmobj
, __alignof__(uint64_t));
362 set_shmp(buf
->ts_end
,
364 sizeof(uint64_t) * chan
->backend
.num_subbuf
));
365 if (!shmp(handle
, buf
->ts_end
)) {
367 goto free_commit_cold
;
371 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
372 cpu
, handle
, shmobj
);
378 * Write the subbuffer header for first subbuffer so we know the total
379 * duration of data gathering.
381 subbuf_header_size
= config
->cb
.subbuffer_header_size();
382 v_set(config
, &buf
->offset
, subbuf_header_size
);
383 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
388 subbuffer_id_clear_noref(config
, &wsb
->id
);
389 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
394 tsc
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
395 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
396 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
401 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
402 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
404 if (config
->cb
.buffer_create
) {
405 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
410 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
412 buf
->backend
.allocated
= 1;
417 /* ts_end will be freed by shm teardown */
419 /* commit_cold will be freed by shm teardown */
421 /* commit_hot will be freed by shm teardown */
427 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
429 const struct lttng_ust_lib_ring_buffer_config
*config
;
430 struct lttng_ust_shm_handle
*handle
;
431 struct channel
*chan
;
434 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
436 chan
= si
->si_value
.sival_ptr
;
437 handle
= chan
->handle
;
438 config
= &chan
->backend
.config
;
440 DBG("Switch timer for channel %p\n", chan
);
443 * Only flush buffers periodically if readers are active.
445 pthread_mutex_lock(&wakeup_fd_mutex
);
446 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
447 for_each_possible_cpu(cpu
) {
448 struct lttng_ust_lib_ring_buffer
*buf
=
449 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
453 if (uatomic_read(&buf
->active_readers
))
454 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
458 struct lttng_ust_lib_ring_buffer
*buf
=
459 shmp(handle
, chan
->backend
.buf
[0].shmp
);
463 if (uatomic_read(&buf
->active_readers
))
464 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
468 pthread_mutex_unlock(&wakeup_fd_mutex
);
473 int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
474 struct lttng_ust_lib_ring_buffer
*buf
,
475 struct channel
*chan
,
476 struct lttng_ust_shm_handle
*handle
)
478 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
479 struct commit_counters_cold
*cc_cold
;
481 consumed_old
= uatomic_read(&buf
->consumed
);
482 consumed_idx
= subbuf_index(consumed_old
, chan
);
483 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
486 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
488 * No memory barrier here, since we are only interested
489 * in a statistically correct polling result. The next poll will
490 * get the data is we are racing. The mb() that ensures correct
491 * memory order is in get_subbuf.
493 write_offset
= v_read(config
, &buf
->offset
);
496 * Check that the subbuffer we are trying to consume has been
497 * already fully committed.
500 if (((commit_count
- chan
->backend
.subbuf_size
)
501 & chan
->commit_count_mask
)
502 - (buf_trunc(consumed_old
, chan
)
503 >> chan
->backend
.num_subbuf_order
)
508 * Check that we are not about to read the same subbuffer in
509 * which the writer head is.
511 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
519 void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer
*buf
,
520 struct lttng_ust_shm_handle
*handle
)
522 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
523 sigset_t sigpipe_set
, pending_set
, old_set
;
524 int ret
, sigpipe_was_pending
= 0;
530 * Wake-up the other end by writing a null byte in the pipe
531 * (non-blocking). Important note: Because writing into the
532 * pipe is non-blocking (and therefore we allow dropping wakeup
533 * data, as long as there is wakeup data present in the pipe
534 * buffer to wake up the consumer), the consumer should perform
535 * the following sequence for waiting:
536 * 1) empty the pipe (reads).
537 * 2) check if there is data in the buffer.
538 * 3) wait on the pipe (poll).
540 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
541 * that might be already pending. If a bogus SIGPIPE is sent to
542 * the entire process concurrently by a malicious user, it may
543 * be simply discarded.
545 ret
= sigemptyset(&pending_set
);
548 * sigpending returns the mask of signals that are _both_
549 * blocked for the thread _and_ pending for either the thread or
550 * the entire process.
552 ret
= sigpending(&pending_set
);
554 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
556 * If sigpipe was pending, it means it was already blocked, so
557 * no need to block it.
559 if (!sigpipe_was_pending
) {
560 ret
= sigemptyset(&sigpipe_set
);
562 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
564 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
568 ret
= write(wakeup_fd
, "", 1);
569 } while (ret
== -1L && errno
== EINTR
);
570 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
571 struct timespec timeout
= { 0, 0 };
573 ret
= sigtimedwait(&sigpipe_set
, NULL
,
575 } while (ret
== -1L && errno
== EINTR
);
577 if (!sigpipe_was_pending
) {
578 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
584 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
586 const struct lttng_ust_lib_ring_buffer_config
*config
;
587 struct lttng_ust_shm_handle
*handle
;
590 handle
= chan
->handle
;
591 config
= &chan
->backend
.config
;
594 * Only flush buffers periodically if readers are active.
596 pthread_mutex_lock(&wakeup_fd_mutex
);
597 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
598 for_each_possible_cpu(cpu
) {
599 struct lttng_ust_lib_ring_buffer
*buf
=
600 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
604 if (uatomic_read(&buf
->active_readers
)
605 && lib_ring_buffer_poll_deliver(config
, buf
,
607 lib_ring_buffer_wakeup(buf
, handle
);
611 struct lttng_ust_lib_ring_buffer
*buf
=
612 shmp(handle
, chan
->backend
.buf
[0].shmp
);
616 if (uatomic_read(&buf
->active_readers
)
617 && lib_ring_buffer_poll_deliver(config
, buf
,
619 lib_ring_buffer_wakeup(buf
, handle
);
623 pthread_mutex_unlock(&wakeup_fd_mutex
);
627 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
629 struct channel
*chan
;
631 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
632 chan
= si
->si_value
.sival_ptr
;
633 DBG("Read timer for channel %p\n", chan
);
634 lib_ring_buffer_channel_do_read(chan
);
639 void rb_setmask(sigset_t
*mask
)
643 ret
= sigemptyset(mask
);
645 PERROR("sigemptyset");
647 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
651 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
655 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
662 void *sig_thread(void *arg
)
668 /* Only self thread will receive signal mask. */
670 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
673 signr
= sigwaitinfo(&mask
, &info
);
676 PERROR("sigwaitinfo");
679 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
680 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
682 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
683 lib_ring_buffer_channel_read_timer(info
.si_signo
,
685 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
687 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
690 ERR("Unexptected signal %d\n", info
.si_signo
);
697 * Ensure only a single thread listens on the timer signal.
700 void lib_ring_buffer_setup_timer_thread(void)
705 pthread_mutex_lock(&timer_signal
.lock
);
706 if (timer_signal
.setup_done
)
709 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
712 PERROR("pthread_create");
714 ret
= pthread_detach(thread
);
717 PERROR("pthread_detach");
719 timer_signal
.setup_done
= 1;
721 pthread_mutex_unlock(&timer_signal
.lock
);
725 * Wait for signal-handling thread quiescent state.
728 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
730 sigset_t pending_set
;
734 * We need to be the only thread interacting with the thread
735 * that manages signals for teardown synchronization.
737 pthread_mutex_lock(&timer_signal
.lock
);
740 * Ensure we don't have any signal queued for this channel.
743 ret
= sigemptyset(&pending_set
);
745 PERROR("sigemptyset");
747 ret
= sigpending(&pending_set
);
749 PERROR("sigpending");
751 if (!sigismember(&pending_set
, signr
))
757 * From this point, no new signal handler will be fired that
758 * would try to access "chan". However, we still need to wait
759 * for any currently executing handler to complete.
762 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
766 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
769 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
771 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
775 pthread_mutex_unlock(&timer_signal
.lock
);
779 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
782 struct itimerspec its
;
785 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
788 chan
->switch_timer_enabled
= 1;
790 lib_ring_buffer_setup_timer_thread();
792 memset(&sev
, 0, sizeof(sev
));
793 sev
.sigev_notify
= SIGEV_SIGNAL
;
794 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
795 sev
.sigev_value
.sival_ptr
= chan
;
796 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
798 PERROR("timer_create");
801 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
802 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
803 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
804 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
806 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
808 PERROR("timer_settime");
813 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
817 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
820 ret
= timer_delete(chan
->switch_timer
);
822 PERROR("timer_delete");
825 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
827 chan
->switch_timer
= 0;
828 chan
->switch_timer_enabled
= 0;
832 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
834 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
836 struct itimerspec its
;
839 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
840 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
843 chan
->read_timer_enabled
= 1;
845 lib_ring_buffer_setup_timer_thread();
847 sev
.sigev_notify
= SIGEV_SIGNAL
;
848 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
849 sev
.sigev_value
.sival_ptr
= chan
;
850 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
852 PERROR("timer_create");
855 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
856 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
857 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
858 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
860 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
862 PERROR("timer_settime");
867 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
869 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
872 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
873 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
876 ret
= timer_delete(chan
->read_timer
);
878 PERROR("timer_delete");
882 * do one more check to catch data that has been written in the last
885 lib_ring_buffer_channel_do_read(chan
);
887 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
889 chan
->read_timer
= 0;
890 chan
->read_timer_enabled
= 0;
893 static void channel_unregister_notifiers(struct channel
*chan
,
894 struct lttng_ust_shm_handle
*handle
)
896 lib_ring_buffer_channel_switch_timer_stop(chan
);
897 lib_ring_buffer_channel_read_timer_stop(chan
);
900 static void channel_print_errors(struct channel
*chan
,
901 struct lttng_ust_shm_handle
*handle
)
903 const struct lttng_ust_lib_ring_buffer_config
*config
=
904 &chan
->backend
.config
;
907 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
908 for_each_possible_cpu(cpu
) {
909 struct lttng_ust_lib_ring_buffer
*buf
=
910 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
912 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
915 struct lttng_ust_lib_ring_buffer
*buf
=
916 shmp(handle
, chan
->backend
.buf
[0].shmp
);
919 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
923 static void channel_free(struct channel
*chan
,
924 struct lttng_ust_shm_handle
*handle
,
927 channel_backend_free(&chan
->backend
, handle
);
928 /* chan is freed by shm teardown */
929 shm_object_table_destroy(handle
->table
, consumer
);
934 * channel_create - Create channel.
935 * @config: ring buffer instance configuration
936 * @name: name of the channel
937 * @priv_data: ring buffer client private data area pointer (output)
938 * @priv_data_size: length, in bytes, of the private data area.
939 * @priv_data_init: initialization data for private data.
940 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
941 * address mapping. It is used only by RING_BUFFER_STATIC
942 * configuration. It can be set to NULL for other backends.
943 * @subbuf_size: subbuffer size
944 * @num_subbuf: number of subbuffers
945 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
946 * padding to let readers get those sub-buffers.
947 * Used for live streaming.
948 * @read_timer_interval: Time interval (in us) to wake up pending readers.
949 * @stream_fds: array of stream file descriptors.
950 * @nr_stream_fds: number of file descriptors in array.
953 * Returns NULL on failure.
955 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
958 size_t priv_data_align
,
959 size_t priv_data_size
,
960 void *priv_data_init
,
961 void *buf_addr
, size_t subbuf_size
,
962 size_t num_subbuf
, unsigned int switch_timer_interval
,
963 unsigned int read_timer_interval
,
964 const int *stream_fds
, int nr_stream_fds
,
965 int64_t blocking_timeout
)
968 size_t shmsize
, chansize
;
969 struct channel
*chan
;
970 struct lttng_ust_shm_handle
*handle
;
971 struct shm_object
*shmobj
;
972 unsigned int nr_streams
;
973 int64_t blocking_timeout_ms
;
975 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
976 nr_streams
= num_possible_cpus();
980 if (nr_stream_fds
!= nr_streams
)
983 if (blocking_timeout
< -1) {
987 if (blocking_timeout
== -1) {
988 blocking_timeout_ms
= -1;
990 blocking_timeout_ms
= blocking_timeout
/ 1000;
991 if (blocking_timeout_ms
!= (int32_t) blocking_timeout_ms
) {
996 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
997 read_timer_interval
))
1000 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1004 /* Allocate table for channel + per-cpu buffers */
1005 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1007 goto error_table_alloc
;
1009 /* Calculate the shm allocation layout */
1010 shmsize
= sizeof(struct channel
);
1011 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
1012 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
1014 if (priv_data_align
)
1015 shmsize
+= lttng_ust_offset_align(shmsize
, priv_data_align
);
1016 shmsize
+= priv_data_size
;
1018 /* Allocate normal memory for channel (not shared) */
1019 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
1023 /* struct channel is at object 0, offset 0 (hardcoded) */
1024 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
1025 assert(handle
->chan
._ref
.index
== 0);
1026 assert(handle
->chan
._ref
.offset
== 0);
1027 chan
= shmp(handle
, handle
->chan
);
1030 chan
->nr_streams
= nr_streams
;
1032 /* space for private data */
1033 if (priv_data_size
) {
1034 DECLARE_SHMP(void, priv_data_alloc
);
1036 align_shm(shmobj
, priv_data_align
);
1037 chan
->priv_data_offset
= shmobj
->allocated_len
;
1038 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1039 if (!shmp(handle
, priv_data_alloc
))
1041 *priv_data
= channel_get_private(chan
);
1042 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
1044 chan
->priv_data_offset
= -1;
1049 chan
->u
.s
.blocking_timeout_ms
= (int32_t) blocking_timeout_ms
;
1051 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1052 subbuf_size
, num_subbuf
, handle
,
1055 goto error_backend_init
;
1057 chan
->handle
= handle
;
1058 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1060 chan
->switch_timer_interval
= switch_timer_interval
;
1061 chan
->read_timer_interval
= read_timer_interval
;
1062 lib_ring_buffer_channel_switch_timer_start(chan
);
1063 lib_ring_buffer_channel_read_timer_start(chan
);
1069 shm_object_table_destroy(handle
->table
, 1);
1075 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1076 uint64_t memory_map_size
,
1079 struct lttng_ust_shm_handle
*handle
;
1080 struct shm_object
*object
;
1082 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1086 /* Allocate table for channel + per-cpu buffers */
1087 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1089 goto error_table_alloc
;
1090 /* Add channel object */
1091 object
= shm_object_table_append_mem(handle
->table
, data
,
1092 memory_map_size
, wakeup_fd
);
1094 goto error_table_object
;
1095 /* struct channel is at object 0, offset 0 (hardcoded) */
1096 handle
->chan
._ref
.index
= 0;
1097 handle
->chan
._ref
.offset
= 0;
1101 shm_object_table_destroy(handle
->table
, 0);
1107 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1108 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1109 uint64_t memory_map_size
)
1111 struct shm_object
*object
;
1113 /* Add stream object */
1114 object
= shm_object_table_append_shm(handle
->table
,
1115 shm_fd
, wakeup_fd
, stream_nr
,
1122 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1124 assert(handle
->table
);
1125 return handle
->table
->allocated_len
- 1;
1129 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1132 channel_free(chan
, handle
, consumer
);
1136 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1137 * @chan: channel to destroy
1139 * Holds cpu hotplug.
1140 * Call "destroy" callback, finalize channels, decrement the channel
1141 * reference count. Note that when readers have completed data
1142 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1143 * They should release their handle at that point.
1145 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1150 * Note: the consumer takes care of finalizing and
1151 * switching the buffers.
1153 channel_unregister_notifiers(chan
, handle
);
1155 * The consumer prints errors.
1157 channel_print_errors(chan
, handle
);
1161 * sessiond/consumer are keeping a reference on the shm file
1162 * descriptor directly. No need to refcount.
1164 channel_release(chan
, handle
, consumer
);
1168 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
1169 const struct lttng_ust_lib_ring_buffer_config
*config
,
1170 struct channel
*chan
, int cpu
,
1171 struct lttng_ust_shm_handle
*handle
,
1172 int *shm_fd
, int *wait_fd
,
1174 uint64_t *memory_map_size
)
1176 struct shm_ref
*ref
;
1178 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1181 if (cpu
>= num_possible_cpus())
1184 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1185 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1186 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1187 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1188 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1190 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1193 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1194 struct channel
*chan
,
1195 struct lttng_ust_shm_handle
*handle
)
1197 struct shm_ref
*ref
;
1199 ref
= &handle
->chan
._ref
;
1200 return shm_close_wait_fd(handle
, ref
);
1203 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1204 struct channel
*chan
,
1205 struct lttng_ust_shm_handle
*handle
)
1207 struct shm_ref
*ref
;
1209 ref
= &handle
->chan
._ref
;
1210 return shm_close_wakeup_fd(handle
, ref
);
1213 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1214 struct channel
*chan
,
1215 struct lttng_ust_shm_handle
*handle
,
1218 struct shm_ref
*ref
;
1220 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1223 if (cpu
>= num_possible_cpus())
1226 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1227 return shm_close_wait_fd(handle
, ref
);
1230 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1231 struct channel
*chan
,
1232 struct lttng_ust_shm_handle
*handle
,
1235 struct shm_ref
*ref
;
1238 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1241 if (cpu
>= num_possible_cpus())
1244 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1245 pthread_mutex_lock(&wakeup_fd_mutex
);
1246 ret
= shm_close_wakeup_fd(handle
, ref
);
1247 pthread_mutex_unlock(&wakeup_fd_mutex
);
1251 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
1252 struct lttng_ust_shm_handle
*handle
)
1254 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1260 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
1261 struct lttng_ust_shm_handle
*handle
)
1263 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1267 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1269 uatomic_dec(&buf
->active_readers
);
1273 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1275 * @consumed: consumed count indicating the position where to read
1276 * @produced: produced count, indicates position when to stop reading
1278 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1279 * data to read at consumed position, or 0 if the get operation succeeds.
1282 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1283 unsigned long *consumed
, unsigned long *produced
,
1284 struct lttng_ust_shm_handle
*handle
)
1286 struct channel
*chan
;
1287 const struct lttng_ust_lib_ring_buffer_config
*config
;
1288 unsigned long consumed_cur
, write_offset
;
1291 chan
= shmp(handle
, buf
->backend
.chan
);
1294 config
= &chan
->backend
.config
;
1295 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1297 * Read finalized before counters.
1300 consumed_cur
= uatomic_read(&buf
->consumed
);
1302 * No need to issue a memory barrier between consumed count read and
1303 * write offset read, because consumed count can only change
1304 * concurrently in overwrite mode, and we keep a sequence counter
1305 * identifier derived from the write offset to check we are getting
1306 * the same sub-buffer we are expecting (the sub-buffers are atomically
1307 * "tagged" upon writes, tags are checked upon read).
1309 write_offset
= v_read(config
, &buf
->offset
);
1312 * Check that we are not about to read the same subbuffer in
1313 * which the writer head is.
1315 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1319 *consumed
= consumed_cur
;
1320 *produced
= subbuf_trunc(write_offset
, chan
);
1326 * The memory barriers __wait_event()/wake_up_interruptible() take care
1327 * of "raw_spin_is_locked" memory ordering.
1336 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1337 * are saved regardless of whether the consumed and produced positions are
1338 * in the same subbuffer.
1340 * @consumed: consumed byte count indicating the last position read
1341 * @produced: produced byte count indicating the last position written
1343 * This function is meant to provide information on the exact producer and
1344 * consumer positions without regard for the "snapshot" feature.
1346 int lib_ring_buffer_snapshot_sample_positions(
1347 struct lttng_ust_lib_ring_buffer
*buf
,
1348 unsigned long *consumed
, unsigned long *produced
,
1349 struct lttng_ust_shm_handle
*handle
)
1351 struct channel
*chan
;
1352 const struct lttng_ust_lib_ring_buffer_config
*config
;
1354 chan
= shmp(handle
, buf
->backend
.chan
);
1357 config
= &chan
->backend
.config
;
1359 *consumed
= uatomic_read(&buf
->consumed
);
1361 * No need to issue a memory barrier between consumed count read and
1362 * write offset read, because consumed count can only change
1363 * concurrently in overwrite mode, and we keep a sequence counter
1364 * identifier derived from the write offset to check we are getting
1365 * the same sub-buffer we are expecting (the sub-buffers are atomically
1366 * "tagged" upon writes, tags are checked upon read).
1368 *produced
= v_read(config
, &buf
->offset
);
1373 * lib_ring_buffer_move_consumer - move consumed counter forward
1375 * @consumed_new: new consumed count value
1377 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1378 unsigned long consumed_new
,
1379 struct lttng_ust_shm_handle
*handle
)
1381 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1382 struct channel
*chan
;
1383 unsigned long consumed
;
1385 chan
= shmp(handle
, bufb
->chan
);
1388 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1391 * Only push the consumed value forward.
1392 * If the consumed cmpxchg fails, this is because we have been pushed by
1393 * the writer in flight recorder mode.
1395 consumed
= uatomic_read(&buf
->consumed
);
1396 while ((long) consumed
- (long) consumed_new
< 0)
1397 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1402 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1404 * @consumed: consumed count indicating the position where to read
1406 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1407 * data to read at consumed position, or 0 if the get operation succeeds.
1409 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1410 unsigned long consumed
,
1411 struct lttng_ust_shm_handle
*handle
)
1413 struct channel
*chan
;
1414 const struct lttng_ust_lib_ring_buffer_config
*config
;
1415 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1416 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1417 struct commit_counters_cold
*cc_cold
;
1419 chan
= shmp(handle
, buf
->backend
.chan
);
1422 config
= &chan
->backend
.config
;
1424 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1426 * Read finalized before counters.
1429 consumed_cur
= uatomic_read(&buf
->consumed
);
1430 consumed_idx
= subbuf_index(consumed
, chan
);
1431 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1434 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1436 * Make sure we read the commit count before reading the buffer
1437 * data and the write offset. Correct consumed offset ordering
1438 * wrt commit count is insured by the use of cmpxchg to update
1439 * the consumed offset.
1442 * Local rmb to match the remote wmb to read the commit count
1443 * before the buffer data and the write offset.
1447 write_offset
= v_read(config
, &buf
->offset
);
1450 * Check that the buffer we are getting is after or at consumed_cur
1453 if ((long) subbuf_trunc(consumed
, chan
)
1454 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1458 * Check that the subbuffer we are trying to consume has been
1459 * already fully committed. There are a few causes that can make
1460 * this unavailability situation occur:
1462 * Temporary (short-term) situation:
1463 * - Application is running on a different CPU, between reserve
1464 * and commit ring buffer operations,
1465 * - Application is preempted between reserve and commit ring
1466 * buffer operations,
1468 * Long-term situation:
1469 * - Application is stopped (SIGSTOP) between reserve and commit
1470 * ring buffer operations. Could eventually be resumed by
1472 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1473 * reserve and commit ring buffer operation.
1475 * From a consumer perspective, handling short-term
1476 * unavailability situations is performed by retrying a few
1477 * times after a delay. Handling long-term unavailability
1478 * situations is handled by failing to get the sub-buffer.
1480 * In all of those situations, if the application is taking a
1481 * long time to perform its commit after ring buffer space
1482 * reservation, we can end up in a situation where the producer
1483 * will fill the ring buffer and try to write into the same
1484 * sub-buffer again (which has a missing commit). This is
1485 * handled by the producer in the sub-buffer switch handling
1486 * code of the reserve routine by detecting unbalanced
1487 * reserve/commit counters and discarding all further events
1488 * until the situation is resolved in those situations. Two
1489 * scenarios can occur:
1491 * 1) The application causing the reserve/commit counters to be
1492 * unbalanced has been terminated. In this situation, all
1493 * further events will be discarded in the buffers, and no
1494 * further buffer data will be readable by the consumer
1495 * daemon. Tearing down the UST tracing session and starting
1496 * anew is a work-around for those situations. Note that this
1497 * only affects per-UID tracing. In per-PID tracing, the
1498 * application vanishes with the termination, and therefore
1499 * no more data needs to be written to the buffers.
1500 * 2) The application causing the unbalance has been delayed for
1501 * a long time, but will eventually try to increment the
1502 * commit counter after eventually writing to the sub-buffer.
1503 * This situation can cause events to be discarded until the
1504 * application resumes its operations.
1506 if (((commit_count
- chan
->backend
.subbuf_size
)
1507 & chan
->commit_count_mask
)
1508 - (buf_trunc(consumed
, chan
)
1509 >> chan
->backend
.num_subbuf_order
)
1511 if (nr_retry
-- > 0) {
1512 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1513 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1521 * Check that we are not about to read the same subbuffer in
1522 * which the writer head is.
1524 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1529 * Failure to get the subbuffer causes a busy-loop retry without going
1530 * to a wait queue. These are caused by short-lived race windows where
1531 * the writer is getting access to a subbuffer we were trying to get
1532 * access to. Also checks that the "consumed" buffer count we are
1533 * looking for matches the one contained in the subbuffer id.
1535 * The short-lived race window described here can be affected by
1536 * application signals and preemption, thus requiring to bound
1537 * the loop to a maximum number of retry.
1539 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1540 consumed_idx
, buf_trunc_val(consumed
, chan
),
1543 if (nr_retry
-- > 0) {
1544 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1545 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1551 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1553 buf
->get_subbuf_consumed
= consumed
;
1554 buf
->get_subbuf
= 1;
1560 * The memory barriers __wait_event()/wake_up_interruptible() take care
1561 * of "raw_spin_is_locked" memory ordering.
1570 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1573 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1574 struct lttng_ust_shm_handle
*handle
)
1576 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1577 struct channel
*chan
;
1578 const struct lttng_ust_lib_ring_buffer_config
*config
;
1579 unsigned long sb_bindex
, consumed_idx
, consumed
;
1580 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
1581 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
1583 chan
= shmp(handle
, bufb
->chan
);
1586 config
= &chan
->backend
.config
;
1587 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1589 if (!buf
->get_subbuf
) {
1591 * Reader puts a subbuffer it did not get.
1593 CHAN_WARN_ON(chan
, 1);
1596 consumed
= buf
->get_subbuf_consumed
;
1597 buf
->get_subbuf
= 0;
1600 * Clear the records_unread counter. (overruns counter)
1601 * Can still be non-zero if a file reader simply grabbed the data
1602 * without using iterators.
1603 * Can be below zero if an iterator is used on a snapshot more than
1606 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1607 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1610 backend_pages
= shmp(handle
, rpages
->shmp
);
1613 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1614 &bufb
->records_read
);
1615 v_set(config
, &backend_pages
->records_unread
, 0);
1616 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1617 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1618 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1621 * Exchange the reader subbuffer with the one we put in its place in the
1622 * writer subbuffer table. Expect the original consumed count. If
1623 * update_read_sb_index fails, this is because the writer updated the
1624 * subbuffer concurrently. We should therefore keep the subbuffer we
1625 * currently have: it has become invalid to try reading this sub-buffer
1626 * consumed count value anyway.
1628 consumed_idx
= subbuf_index(consumed
, chan
);
1629 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1630 consumed_idx
, buf_trunc_val(consumed
, chan
),
1633 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1634 * if the writer concurrently updated it.
1639 * cons_offset is an iterator on all subbuffer offsets between the reader
1640 * position and the writer position. (inclusive)
1643 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1644 struct channel
*chan
,
1645 unsigned long cons_offset
,
1647 struct lttng_ust_shm_handle
*handle
)
1649 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1650 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1651 struct commit_counters_hot
*cc_hot
;
1652 struct commit_counters_cold
*cc_cold
;
1654 cons_idx
= subbuf_index(cons_offset
, chan
);
1655 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1658 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1661 commit_count
= v_read(config
, &cc_hot
->cc
);
1662 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1664 if (subbuf_offset(commit_count
, chan
) != 0)
1665 DBG("ring buffer %s, cpu %d: "
1666 "commit count in subbuffer %lu,\n"
1667 "expecting multiples of %lu bytes\n"
1668 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1669 chan
->backend
.name
, cpu
, cons_idx
,
1670 chan
->backend
.subbuf_size
,
1671 commit_count
, commit_count_sb
);
1673 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1674 chan
->backend
.name
, cpu
, commit_count
);
1678 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1679 struct channel
*chan
,
1680 void *priv
, int cpu
,
1681 struct lttng_ust_shm_handle
*handle
)
1683 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1684 unsigned long write_offset
, cons_offset
;
1687 * No need to order commit_count, write_offset and cons_offset reads
1688 * because we execute at teardown when no more writer nor reader
1689 * references are left.
1691 write_offset
= v_read(config
, &buf
->offset
);
1692 cons_offset
= uatomic_read(&buf
->consumed
);
1693 if (write_offset
!= cons_offset
)
1694 DBG("ring buffer %s, cpu %d: "
1695 "non-consumed data\n"
1696 " [ %lu bytes written, %lu bytes read ]\n",
1697 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1699 for (cons_offset
= uatomic_read(&buf
->consumed
);
1700 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1703 cons_offset
= subbuf_align(cons_offset
, chan
))
1704 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1709 void lib_ring_buffer_print_errors(struct channel
*chan
,
1710 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1711 struct lttng_ust_shm_handle
*handle
)
1713 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1714 void *priv
= channel_get_private(chan
);
1716 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1717 DBG("ring buffer %s: %lu records written, "
1718 "%lu records overrun\n",
1720 v_read(config
, &buf
->records_count
),
1721 v_read(config
, &buf
->records_overrun
));
1723 DBG("ring buffer %s, cpu %d: %lu records written, "
1724 "%lu records overrun\n",
1725 chan
->backend
.name
, cpu
,
1726 v_read(config
, &buf
->records_count
),
1727 v_read(config
, &buf
->records_overrun
));
1729 if (v_read(config
, &buf
->records_lost_full
)
1730 || v_read(config
, &buf
->records_lost_wrap
)
1731 || v_read(config
, &buf
->records_lost_big
))
1732 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1733 " [ %lu buffer full, %lu nest buffer wrap-around, "
1734 "%lu event too big ]\n",
1735 chan
->backend
.name
, cpu
,
1736 v_read(config
, &buf
->records_lost_full
),
1737 v_read(config
, &buf
->records_lost_wrap
),
1738 v_read(config
, &buf
->records_lost_big
));
1740 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1744 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1746 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1747 * active or at buffer finalization (destroy).
1750 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1751 struct channel
*chan
,
1752 struct switch_offsets
*offsets
,
1754 struct lttng_ust_shm_handle
*handle
)
1756 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1757 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1758 unsigned long commit_count
;
1759 struct commit_counters_hot
*cc_hot
;
1761 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1764 * Order all writes to buffer before the commit count update that will
1765 * determine that the subbuffer is full.
1768 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1771 v_add(config
, config
->cb
.subbuffer_header_size(),
1773 commit_count
= v_read(config
, &cc_hot
->cc
);
1774 /* Check if the written buffer has to be delivered */
1775 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1776 commit_count
, oldidx
, handle
, tsc
);
1777 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1778 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1779 commit_count
, handle
, cc_hot
);
1783 * lib_ring_buffer_switch_old_end: switch old subbuffer
1785 * Note : offset_old should never be 0 here. It is ok, because we never perform
1786 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1787 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1791 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1792 struct channel
*chan
,
1793 struct switch_offsets
*offsets
,
1795 struct lttng_ust_shm_handle
*handle
)
1797 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1798 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1799 unsigned long commit_count
, padding_size
, data_size
;
1800 struct commit_counters_hot
*cc_hot
;
1803 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1804 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1805 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1808 ts_end
= shmp_index(handle
, buf
->ts_end
, oldidx
);
1812 * This is the last space reservation in that sub-buffer before
1813 * it gets delivered. This provides exclusive access to write to
1814 * this sub-buffer's ts_end. There are also no concurrent
1815 * readers of that ts_end because delivery of that sub-buffer is
1816 * postponed until the commit counter is incremented for the
1817 * current space reservation.
1822 * Order all writes to buffer and store to ts_end before the commit
1823 * count update that will determine that the subbuffer is full.
1826 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1829 v_add(config
, padding_size
, &cc_hot
->cc
);
1830 commit_count
= v_read(config
, &cc_hot
->cc
);
1831 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1832 commit_count
, oldidx
, handle
, tsc
);
1833 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1834 offsets
->old
+ padding_size
, commit_count
, handle
,
1839 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1841 * This code can be executed unordered : writers may already have written to the
1842 * sub-buffer before this code gets executed, caution. The commit makes sure
1843 * that this code is executed before the deliver of this sub-buffer.
1846 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1847 struct channel
*chan
,
1848 struct switch_offsets
*offsets
,
1850 struct lttng_ust_shm_handle
*handle
)
1852 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1853 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1854 unsigned long commit_count
;
1855 struct commit_counters_hot
*cc_hot
;
1857 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1860 * Order all writes to buffer before the commit count update that will
1861 * determine that the subbuffer is full.
1864 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1867 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1868 commit_count
= v_read(config
, &cc_hot
->cc
);
1869 /* Check if the written buffer has to be delivered */
1870 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1871 commit_count
, beginidx
, handle
, tsc
);
1872 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1873 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1874 commit_count
, handle
, cc_hot
);
1878 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1880 * Calls subbuffer_set_data_size() to set the data size of the current
1881 * sub-buffer. We do not need to perform check_deliver nor commit here,
1882 * since this task will be done by the "commit" of the event for which
1883 * we are currently doing the space reservation.
1886 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1887 struct channel
*chan
,
1888 struct switch_offsets
*offsets
,
1890 struct lttng_ust_shm_handle
*handle
)
1892 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1893 unsigned long endidx
, data_size
;
1896 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1897 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1898 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1900 ts_end
= shmp_index(handle
, buf
->ts_end
, endidx
);
1904 * This is the last space reservation in that sub-buffer before
1905 * it gets delivered. This provides exclusive access to write to
1906 * this sub-buffer's ts_end. There are also no concurrent
1907 * readers of that ts_end because delivery of that sub-buffer is
1908 * postponed until the commit counter is incremented for the
1909 * current space reservation.
1917 * !0 if execution must be aborted.
1920 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1921 struct lttng_ust_lib_ring_buffer
*buf
,
1922 struct channel
*chan
,
1923 struct switch_offsets
*offsets
,
1925 struct lttng_ust_shm_handle
*handle
)
1927 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1928 unsigned long off
, reserve_commit_diff
;
1930 offsets
->begin
= v_read(config
, &buf
->offset
);
1931 offsets
->old
= offsets
->begin
;
1932 offsets
->switch_old_start
= 0;
1933 off
= subbuf_offset(offsets
->begin
, chan
);
1935 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1938 * Ensure we flush the header of an empty subbuffer when doing the
1939 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1940 * total data gathering duration even if there were no records saved
1941 * after the last buffer switch.
1942 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1943 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1944 * subbuffer header as appropriate.
1945 * The next record that reserves space will be responsible for
1946 * populating the following subbuffer header. We choose not to populate
1947 * the next subbuffer header here because we want to be able to use
1948 * SWITCH_ACTIVE for periodical buffer flush, which must
1949 * guarantee that all the buffer content (records and header
1950 * timestamps) are visible to the reader. This is required for
1951 * quiescence guarantees for the fusion merge.
1953 if (mode
!= SWITCH_FLUSH
&& !off
)
1954 return -1; /* we do not have to switch : buffer is empty */
1956 if (caa_unlikely(off
== 0)) {
1957 unsigned long sb_index
, commit_count
;
1958 struct commit_counters_cold
*cc_cold
;
1961 * We are performing a SWITCH_FLUSH. There may be concurrent
1962 * writes into the buffer if e.g. invoked while performing a
1963 * snapshot on an active trace.
1965 * If the client does not save any header information
1966 * (sub-buffer header size == 0), don't switch empty subbuffer
1967 * on finalize, because it is invalid to deliver a completely
1970 if (!config
->cb
.subbuffer_header_size())
1973 /* Test new buffer integrity */
1974 sb_index
= subbuf_index(offsets
->begin
, chan
);
1975 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1978 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1979 reserve_commit_diff
=
1980 (buf_trunc(offsets
->begin
, chan
)
1981 >> chan
->backend
.num_subbuf_order
)
1982 - (commit_count
& chan
->commit_count_mask
);
1983 if (caa_likely(reserve_commit_diff
== 0)) {
1984 /* Next subbuffer not being written to. */
1985 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1986 subbuf_trunc(offsets
->begin
, chan
)
1987 - subbuf_trunc((unsigned long)
1988 uatomic_read(&buf
->consumed
), chan
)
1989 >= chan
->backend
.buf_size
)) {
1991 * We do not overwrite non consumed buffers
1992 * and we are full : don't switch.
1997 * Next subbuffer not being written to, and we
1998 * are either in overwrite mode or the buffer is
1999 * not full. It's safe to write in this new
2005 * Next subbuffer reserve offset does not match the
2006 * commit offset. Don't perform switch in
2007 * producer-consumer and overwrite mode. Caused by
2008 * either a writer OOPS or too many nested writes over a
2009 * reserve/commit pair.
2015 * Need to write the subbuffer start header on finalize.
2017 offsets
->switch_old_start
= 1;
2019 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2020 /* Note: old points to the next subbuf at offset 0 */
2021 offsets
->end
= offsets
->begin
;
2026 * Force a sub-buffer switch. This operation is completely reentrant : can be
2027 * called while tracing is active with absolutely no lock held.
2029 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2030 * some atomic operations, this function must be called from the CPU
2031 * which owns the buffer for a ACTIVE flush. However, for
2032 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2035 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
2036 struct lttng_ust_shm_handle
*handle
)
2038 struct channel
*chan
;
2039 const struct lttng_ust_lib_ring_buffer_config
*config
;
2040 struct switch_offsets offsets
;
2041 unsigned long oldidx
;
2044 chan
= shmp(handle
, buf
->backend
.chan
);
2047 config
= &chan
->backend
.config
;
2052 * Perform retryable operations.
2055 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
2057 return; /* Switch not needed */
2058 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
2062 * Atomically update last_tsc. This update races against concurrent
2063 * atomic updates, but the race will always cause supplementary full TSC
2064 * records, never the opposite (missing a full TSC record when it would
2067 save_last_tsc(config
, buf
, tsc
);
2070 * Push the reader if necessary
2072 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
2074 oldidx
= subbuf_index(offsets
.old
, chan
);
2075 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
2078 * May need to populate header start on SWITCH_FLUSH.
2080 if (offsets
.switch_old_start
) {
2081 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
2082 offsets
.old
+= config
->cb
.subbuffer_header_size();
2086 * Switch old subbuffer.
2088 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
2092 bool handle_blocking_retry(int *timeout_left_ms
)
2094 int timeout
= *timeout_left_ms
, delay
;
2096 if (caa_likely(!timeout
))
2097 return false; /* Do not retry, discard event. */
2098 if (timeout
< 0) /* Wait forever. */
2099 delay
= RETRY_DELAY_MS
;
2101 delay
= min_t(int, timeout
, RETRY_DELAY_MS
);
2102 (void) poll(NULL
, 0, delay
);
2104 *timeout_left_ms
-= delay
;
2105 return true; /* Retry. */
2111 * -ENOSPC if event size is too large for packet.
2112 * -ENOBUFS if there is currently not enough space in buffer for the event.
2113 * -EIO if data cannot be written into the buffer for any other reason.
2116 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
2117 struct channel
*chan
,
2118 struct switch_offsets
*offsets
,
2119 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2122 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2123 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2124 unsigned long reserve_commit_diff
, offset_cmp
;
2125 int timeout_left_ms
= lttng_ust_ringbuffer_get_timeout(chan
);
2128 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2129 offsets
->old
= offsets
->begin
;
2130 offsets
->switch_new_start
= 0;
2131 offsets
->switch_new_end
= 0;
2132 offsets
->switch_old_end
= 0;
2133 offsets
->pre_header_padding
= 0;
2135 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2136 if ((int64_t) ctx
->tsc
== -EIO
)
2139 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
2140 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2142 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
2143 offsets
->switch_new_start
= 1; /* For offsets->begin */
2145 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2147 &offsets
->pre_header_padding
,
2150 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2153 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2154 offsets
->size
> chan
->backend
.subbuf_size
)) {
2155 offsets
->switch_old_end
= 1; /* For offsets->old */
2156 offsets
->switch_new_start
= 1; /* For offsets->begin */
2159 if (caa_unlikely(offsets
->switch_new_start
)) {
2160 unsigned long sb_index
, commit_count
;
2161 struct commit_counters_cold
*cc_cold
;
2164 * We are typically not filling the previous buffer completely.
2166 if (caa_likely(offsets
->switch_old_end
))
2167 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2168 offsets
->begin
= offsets
->begin
2169 + config
->cb
.subbuffer_header_size();
2170 /* Test new buffer integrity */
2171 sb_index
= subbuf_index(offsets
->begin
, chan
);
2173 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2174 * lib_ring_buffer_check_deliver() has the matching
2175 * memory barriers required around commit_cold cc_sb
2176 * updates to ensure reserve and commit counter updates
2177 * are not seen reordered when updated by another CPU.
2180 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2183 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2184 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2186 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2188 * The reserve counter have been concurrently updated
2189 * while we read the commit counter. This means the
2190 * commit counter we read might not match buf->offset
2191 * due to concurrent update. We therefore need to retry.
2195 reserve_commit_diff
=
2196 (buf_trunc(offsets
->begin
, chan
)
2197 >> chan
->backend
.num_subbuf_order
)
2198 - (commit_count
& chan
->commit_count_mask
);
2199 if (caa_likely(reserve_commit_diff
== 0)) {
2200 /* Next subbuffer not being written to. */
2201 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2202 subbuf_trunc(offsets
->begin
, chan
)
2203 - subbuf_trunc((unsigned long)
2204 uatomic_read(&buf
->consumed
), chan
)
2205 >= chan
->backend
.buf_size
)) {
2206 unsigned long nr_lost
;
2208 if (handle_blocking_retry(&timeout_left_ms
))
2212 * We do not overwrite non consumed buffers
2213 * and we are full : record is lost.
2215 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2216 v_inc(config
, &buf
->records_lost_full
);
2217 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2218 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2219 nr_lost
+ 1, chan
->backend
.name
,
2225 * Next subbuffer not being written to, and we
2226 * are either in overwrite mode or the buffer is
2227 * not full. It's safe to write in this new
2232 unsigned long nr_lost
;
2235 * Next subbuffer reserve offset does not match the
2236 * commit offset, and this did not involve update to the
2237 * reserve counter. Drop record in producer-consumer and
2238 * overwrite mode. Caused by either a writer OOPS or too
2239 * many nested writes over a reserve/commit pair.
2241 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2242 v_inc(config
, &buf
->records_lost_wrap
);
2243 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2244 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2245 nr_lost
+ 1, chan
->backend
.name
,
2251 config
->cb
.record_header_size(config
, chan
,
2253 &offsets
->pre_header_padding
,
2256 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2259 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2260 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2261 unsigned long nr_lost
;
2264 * Record too big for subbuffers, report error, don't
2265 * complete the sub-buffer switch.
2267 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2268 v_inc(config
, &buf
->records_lost_big
);
2269 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2270 DBG("%lu or more records lost in (%s:%d) record size "
2271 " of %zu bytes is too large for buffer\n",
2272 nr_lost
+ 1, chan
->backend
.name
,
2273 buf
->backend
.cpu
, offsets
->size
);
2278 * We just made a successful buffer switch and the
2279 * record fits in the new subbuffer. Let's write.
2284 * Record fits in the current buffer and we are not on a switch
2285 * boundary. It's safe to write.
2288 offsets
->end
= offsets
->begin
+ offsets
->size
;
2290 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2292 * The offset_end will fall at the very beginning of the next
2295 offsets
->switch_new_end
= 1; /* For offsets->begin */
2301 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2302 * @ctx: ring buffer context.
2304 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2305 * -EIO for other errors, else returns 0.
2306 * It will take care of sub-buffer switching.
2308 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2311 struct channel
*chan
= ctx
->chan
;
2312 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2313 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2314 struct lttng_ust_lib_ring_buffer
*buf
;
2315 struct switch_offsets offsets
;
2318 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2319 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
2321 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2329 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2331 if (caa_unlikely(ret
))
2333 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2338 * Atomically update last_tsc. This update races against concurrent
2339 * atomic updates, but the race will always cause supplementary full TSC
2340 * records, never the opposite (missing a full TSC record when it would
2343 save_last_tsc(config
, buf
, ctx
->tsc
);
2346 * Push the reader if necessary
2348 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2351 * Clear noref flag for this subbuffer.
2353 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2354 subbuf_index(offsets
.end
- 1, chan
),
2358 * Switch old subbuffer if needed.
2360 if (caa_unlikely(offsets
.switch_old_end
)) {
2361 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2362 subbuf_index(offsets
.old
- 1, chan
),
2364 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2368 * Populate new subbuffer.
2370 if (caa_unlikely(offsets
.switch_new_start
))
2371 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2373 if (caa_unlikely(offsets
.switch_new_end
))
2374 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2376 ctx
->slot_size
= offsets
.size
;
2377 ctx
->pre_offset
= offsets
.begin
;
2378 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2383 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
2384 struct lttng_ust_lib_ring_buffer
*buf
,
2385 unsigned long commit_count
,
2387 struct lttng_ust_shm_handle
*handle
)
2389 struct commit_counters_hot
*cc_hot
;
2391 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2393 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2396 v_set(config
, &cc_hot
->seq
, commit_count
);
2400 * The ring buffer can count events recorded and overwritten per buffer,
2401 * but it is disabled by default due to its performance overhead.
2403 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2405 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2406 struct lttng_ust_lib_ring_buffer
*buf
,
2408 struct lttng_ust_shm_handle
*handle
)
2410 v_add(config
, subbuffer_get_records_count(config
,
2411 &buf
->backend
, idx
, handle
),
2412 &buf
->records_count
);
2413 v_add(config
, subbuffer_count_records_overrun(config
,
2414 &buf
->backend
, idx
, handle
),
2415 &buf
->records_overrun
);
2417 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2419 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2420 struct lttng_ust_lib_ring_buffer
*buf
,
2422 struct lttng_ust_shm_handle
*handle
)
2425 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2427 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
2428 struct lttng_ust_lib_ring_buffer
*buf
,
2429 struct channel
*chan
,
2430 unsigned long offset
,
2431 unsigned long commit_count
,
2433 struct lttng_ust_shm_handle
*handle
,
2436 unsigned long old_commit_count
= commit_count
2437 - chan
->backend
.subbuf_size
;
2438 struct commit_counters_cold
*cc_cold
;
2441 * If we succeeded at updating cc_sb below, we are the subbuffer
2442 * writer delivering the subbuffer. Deals with concurrent
2443 * updates of the "cc" value without adding a add_return atomic
2444 * operation to the fast path.
2446 * We are doing the delivery in two steps:
2447 * - First, we cmpxchg() cc_sb to the new value
2448 * old_commit_count + 1. This ensures that we are the only
2449 * subbuffer user successfully filling the subbuffer, but we
2450 * do _not_ set the cc_sb value to "commit_count" yet.
2451 * Therefore, other writers that would wrap around the ring
2452 * buffer and try to start writing to our subbuffer would
2453 * have to drop records, because it would appear as
2455 * We therefore have exclusive access to the subbuffer control
2456 * structures. This mutual exclusion with other writers is
2457 * crucially important to perform record overruns count in
2458 * flight recorder mode locklessly.
2459 * - When we are ready to release the subbuffer (either for
2460 * reading or for overrun by other writers), we simply set the
2461 * cc_sb value to "commit_count" and perform delivery.
2463 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2464 * This guarantees that old_commit_count + 1 != commit_count.
2468 * Order prior updates to reserve count prior to the
2469 * commit_cold cc_sb update.
2472 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2475 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2476 old_commit_count
, old_commit_count
+ 1)
2477 == old_commit_count
)) {
2481 * Start of exclusive subbuffer access. We are
2482 * guaranteed to be the last writer in this subbuffer
2483 * and any other writer trying to access this subbuffer
2484 * in this state is required to drop records.
2486 * We can read the ts_end for the current sub-buffer
2487 * which has been saved by the very last space
2488 * reservation for the current sub-buffer.
2490 * Order increment of commit counter before reading ts_end.
2493 ts_end
= shmp_index(handle
, buf
->ts_end
, idx
);
2496 deliver_count_events(config
, buf
, idx
, handle
);
2497 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2498 lib_ring_buffer_get_data_size(config
,
2505 * Increment the packet counter while we have exclusive
2508 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2511 * Set noref flag and offset for this subbuffer id.
2512 * Contains a memory barrier that ensures counter stores
2513 * are ordered before set noref and offset.
2515 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2516 buf_trunc_val(offset
, chan
), handle
);
2519 * Order set_noref and record counter updates before the
2520 * end of subbuffer exclusive access. Orders with
2521 * respect to writers coming into the subbuffer after
2522 * wrap around, and also order wrt concurrent readers.
2525 /* End of exclusive subbuffer access */
2526 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2528 * Order later updates to reserve count after
2529 * the commit cold cc_sb update.
2532 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2533 commit_count
, idx
, handle
);
2536 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2538 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2539 && uatomic_read(&buf
->active_readers
)
2540 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2541 lib_ring_buffer_wakeup(buf
, handle
);
2547 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2549 void lttng_fixup_ringbuffer_tls(void)
2551 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2554 void lib_ringbuffer_signal_init(void)
2560 * Block signal for entire process, so only our thread processes
2564 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2567 PERROR("pthread_sigmask");