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>
55 #include <lttng/align.h>
57 #include <lttng/ringbuffer-config.h>
63 #include "../liblttng-ust/compat.h" /* For ENODATA */
65 /* Print DBG() messages about events lost only every 1048576 hits */
66 #define DBG_PRINT_NR_LOST (1UL << 20)
68 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
69 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
70 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
71 #define CLOCKID CLOCK_MONOTONIC
72 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
73 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
74 #define RETRY_DELAY_MS 100 /* 100 ms. */
77 * Non-static to ensure the compiler does not optimize away the xor.
79 uint8_t lttng_crash_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
82 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
83 * close(2) to close the fd returned by shm_open.
84 * shm_unlink releases the shared memory object name.
85 * ftruncate(2) sets the size of the memory object.
86 * mmap/munmap maps the shared memory obj to a virtual address in the
87 * calling proceess (should be done both in libust and consumer).
88 * See shm_overview(7) for details.
89 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
92 * Since we don't need to access the object using its name, we can
93 * immediately shm_unlink(3) it, and only keep the handle with its file
98 * Internal structure representing offsets to use at a sub-buffer switch.
100 struct switch_offsets
{
101 unsigned long begin
, end
, old
;
102 size_t pre_header_padding
, size
;
103 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
107 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
110 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
113 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
116 void lib_ring_buffer_print_errors(struct channel
*chan
,
117 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
118 struct lttng_ust_shm_handle
*handle
);
121 * Handle timer teardown race wrt memory free of private data by
122 * ring buffer signals are handled by a single thread, which permits
123 * a synchronization point between handling of each signal.
124 * Protected by the lock within the structure.
126 struct timer_signal_data
{
127 pthread_t tid
; /* thread id managing signals */
130 pthread_mutex_t lock
;
133 static struct timer_signal_data timer_signal
= {
137 .lock
= PTHREAD_MUTEX_INITIALIZER
,
140 static bool lttng_ust_allow_blocking
;
142 void lttng_ust_ringbuffer_set_allow_blocking(void)
144 lttng_ust_allow_blocking
= true;
147 /* Get blocking timeout, in ms */
148 static int lttng_ust_ringbuffer_get_timeout(struct channel
*chan
)
150 if (!lttng_ust_allow_blocking
)
152 return chan
->u
.s
.blocking_timeout_ms
;
156 * lib_ring_buffer_reset - Reset ring buffer to initial values.
159 * Effectively empty the ring buffer. Should be called when the buffer is not
160 * used for writing. The ring buffer can be opened for reading, but the reader
161 * should not be using the iterator concurrently with reset. The previous
162 * current iterator record is reset.
164 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
165 struct lttng_ust_shm_handle
*handle
)
167 struct channel
*chan
;
168 const struct lttng_ust_lib_ring_buffer_config
*config
;
171 chan
= shmp(handle
, buf
->backend
.chan
);
174 config
= &chan
->backend
.config
;
176 * Reset iterator first. It will put the subbuffer if it currently holds
179 v_set(config
, &buf
->offset
, 0);
180 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
181 struct commit_counters_hot
*cc_hot
;
182 struct commit_counters_cold
*cc_cold
;
185 cc_hot
= shmp_index(handle
, buf
->commit_hot
, i
);
188 cc_cold
= shmp_index(handle
, buf
->commit_cold
, i
);
191 ts_end
= shmp_index(handle
, buf
->ts_end
, i
);
194 v_set(config
, &cc_hot
->cc
, 0);
195 v_set(config
, &cc_hot
->seq
, 0);
196 v_set(config
, &cc_cold
->cc_sb
, 0);
199 uatomic_set(&buf
->consumed
, 0);
200 uatomic_set(&buf
->record_disabled
, 0);
201 v_set(config
, &buf
->last_tsc
, 0);
202 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
203 /* Don't reset number of active readers */
204 v_set(config
, &buf
->records_lost_full
, 0);
205 v_set(config
, &buf
->records_lost_wrap
, 0);
206 v_set(config
, &buf
->records_lost_big
, 0);
207 v_set(config
, &buf
->records_count
, 0);
208 v_set(config
, &buf
->records_overrun
, 0);
213 * channel_reset - Reset channel to initial values.
216 * Effectively empty the channel. Should be called when the channel is not used
217 * for writing. The channel can be opened for reading, but the reader should not
218 * be using the iterator concurrently with reset. The previous current iterator
221 void channel_reset(struct channel
*chan
)
224 * Reset iterators first. Will put the subbuffer if held for reading.
226 uatomic_set(&chan
->record_disabled
, 0);
227 /* Don't reset commit_count_mask, still valid */
228 channel_backend_reset(&chan
->backend
);
229 /* Don't reset switch/read timer interval */
230 /* Don't reset notifiers and notifier enable bits */
231 /* Don't reset reader reference count */
235 void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config
*config
,
236 struct lttng_crash_abi
*crash_abi
,
237 struct lttng_ust_lib_ring_buffer
*buf
,
238 struct channel_backend
*chanb
,
239 struct shm_object
*shmobj
,
240 struct lttng_ust_shm_handle
*handle
)
244 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
245 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
246 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
247 crash_abi
->endian
= RB_CRASH_ENDIAN
;
248 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
249 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
250 crash_abi
->word_size
= sizeof(unsigned long);
251 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
253 /* Offset of fields */
254 crash_abi
->offset
.prod_offset
=
255 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
256 crash_abi
->offset
.consumed_offset
=
257 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
258 crash_abi
->offset
.commit_hot_array
=
259 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
260 crash_abi
->offset
.commit_hot_seq
=
261 offsetof(struct commit_counters_hot
, seq
);
262 crash_abi
->offset
.buf_wsb_array
=
263 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
264 crash_abi
->offset
.buf_wsb_id
=
265 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
, id
);
266 crash_abi
->offset
.sb_array
=
267 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
268 crash_abi
->offset
.sb_array_shmp_offset
=
269 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
,
271 crash_abi
->offset
.sb_backend_p_offset
=
272 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages
,
276 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
277 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
278 crash_abi
->length
.commit_hot_seq
=
279 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
280 crash_abi
->length
.buf_wsb_id
=
281 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer
*) NULL
)->id
);
282 crash_abi
->length
.sb_array_shmp_offset
=
283 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
284 crash_abi
->length
.sb_backend_p_offset
=
285 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
288 crash_abi
->stride
.commit_hot_array
=
289 sizeof(struct commit_counters_hot
);
290 crash_abi
->stride
.buf_wsb_array
=
291 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
);
292 crash_abi
->stride
.sb_array
=
293 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
);
295 /* Buffer constants */
296 crash_abi
->buf_size
= chanb
->buf_size
;
297 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
298 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
299 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
301 if (config
->cb
.content_size_field
) {
302 size_t offset
, length
;
304 config
->cb
.content_size_field(config
, &offset
, &length
);
305 crash_abi
->offset
.content_size
= offset
;
306 crash_abi
->length
.content_size
= length
;
308 crash_abi
->offset
.content_size
= 0;
309 crash_abi
->length
.content_size
= 0;
311 if (config
->cb
.packet_size_field
) {
312 size_t offset
, length
;
314 config
->cb
.packet_size_field(config
, &offset
, &length
);
315 crash_abi
->offset
.packet_size
= offset
;
316 crash_abi
->length
.packet_size
= length
;
318 crash_abi
->offset
.packet_size
= 0;
319 crash_abi
->length
.packet_size
= 0;
324 * Must be called under cpu hotplug protection.
326 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
327 struct channel_backend
*chanb
, int cpu
,
328 struct lttng_ust_shm_handle
*handle
,
329 struct shm_object
*shmobj
)
331 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
332 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
333 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
334 struct channel
*shmp_chan
;
335 struct commit_counters_hot
*cc_hot
;
336 void *priv
= channel_get_private(chan
);
337 size_t subbuf_header_size
;
341 /* Test for cpu hotplug */
342 if (buf
->backend
.allocated
)
345 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
346 set_shmp(buf
->commit_hot
,
348 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
349 if (!shmp(handle
, buf
->commit_hot
)) {
353 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
354 set_shmp(buf
->commit_cold
,
356 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
357 if (!shmp(handle
, buf
->commit_cold
)) {
362 align_shm(shmobj
, __alignof__(uint64_t));
363 set_shmp(buf
->ts_end
,
365 sizeof(uint64_t) * chan
->backend
.num_subbuf
));
366 if (!shmp(handle
, buf
->ts_end
)) {
368 goto free_commit_cold
;
372 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
373 cpu
, handle
, shmobj
);
379 * Write the subbuffer header for first subbuffer so we know the total
380 * duration of data gathering.
382 subbuf_header_size
= config
->cb
.subbuffer_header_size();
383 v_set(config
, &buf
->offset
, subbuf_header_size
);
384 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
389 subbuffer_id_clear_noref(config
, &wsb
->id
);
390 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
395 tsc
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
396 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
397 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
402 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
403 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
405 if (config
->cb
.buffer_create
) {
406 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
411 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
413 buf
->backend
.allocated
= 1;
418 /* ts_end will be freed by shm teardown */
420 /* commit_cold will be freed by shm teardown */
422 /* commit_hot will be freed by shm teardown */
428 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
430 const struct lttng_ust_lib_ring_buffer_config
*config
;
431 struct lttng_ust_shm_handle
*handle
;
432 struct channel
*chan
;
435 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
437 chan
= si
->si_value
.sival_ptr
;
438 handle
= chan
->handle
;
439 config
= &chan
->backend
.config
;
441 DBG("Switch timer for channel %p\n", chan
);
444 * Only flush buffers periodically if readers are active.
446 pthread_mutex_lock(&wakeup_fd_mutex
);
447 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
448 for_each_possible_cpu(cpu
) {
449 struct lttng_ust_lib_ring_buffer
*buf
=
450 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
454 if (uatomic_read(&buf
->active_readers
))
455 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
459 struct lttng_ust_lib_ring_buffer
*buf
=
460 shmp(handle
, chan
->backend
.buf
[0].shmp
);
464 if (uatomic_read(&buf
->active_readers
))
465 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
469 pthread_mutex_unlock(&wakeup_fd_mutex
);
474 int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
475 struct lttng_ust_lib_ring_buffer
*buf
,
476 struct channel
*chan
,
477 struct lttng_ust_shm_handle
*handle
)
479 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
480 struct commit_counters_cold
*cc_cold
;
482 consumed_old
= uatomic_read(&buf
->consumed
);
483 consumed_idx
= subbuf_index(consumed_old
, chan
);
484 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
487 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
489 * No memory barrier here, since we are only interested
490 * in a statistically correct polling result. The next poll will
491 * get the data is we are racing. The mb() that ensures correct
492 * memory order is in get_subbuf.
494 write_offset
= v_read(config
, &buf
->offset
);
497 * Check that the subbuffer we are trying to consume has been
498 * already fully committed.
501 if (((commit_count
- chan
->backend
.subbuf_size
)
502 & chan
->commit_count_mask
)
503 - (buf_trunc(consumed_old
, chan
)
504 >> chan
->backend
.num_subbuf_order
)
509 * Check that we are not about to read the same subbuffer in
510 * which the writer head is.
512 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
520 void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer
*buf
,
521 struct lttng_ust_shm_handle
*handle
)
523 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
524 sigset_t sigpipe_set
, pending_set
, old_set
;
525 int ret
, sigpipe_was_pending
= 0;
531 * Wake-up the other end by writing a null byte in the pipe
532 * (non-blocking). Important note: Because writing into the
533 * pipe is non-blocking (and therefore we allow dropping wakeup
534 * data, as long as there is wakeup data present in the pipe
535 * buffer to wake up the consumer), the consumer should perform
536 * the following sequence for waiting:
537 * 1) empty the pipe (reads).
538 * 2) check if there is data in the buffer.
539 * 3) wait on the pipe (poll).
541 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
542 * that might be already pending. If a bogus SIGPIPE is sent to
543 * the entire process concurrently by a malicious user, it may
544 * be simply discarded.
546 ret
= sigemptyset(&pending_set
);
549 * sigpending returns the mask of signals that are _both_
550 * blocked for the thread _and_ pending for either the thread or
551 * the entire process.
553 ret
= sigpending(&pending_set
);
555 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
557 * If sigpipe was pending, it means it was already blocked, so
558 * no need to block it.
560 if (!sigpipe_was_pending
) {
561 ret
= sigemptyset(&sigpipe_set
);
563 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
565 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
569 ret
= write(wakeup_fd
, "", 1);
570 } while (ret
== -1L && errno
== EINTR
);
571 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
572 struct timespec timeout
= { 0, 0 };
574 ret
= sigtimedwait(&sigpipe_set
, NULL
,
576 } while (ret
== -1L && errno
== EINTR
);
578 if (!sigpipe_was_pending
) {
579 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
585 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
587 const struct lttng_ust_lib_ring_buffer_config
*config
;
588 struct lttng_ust_shm_handle
*handle
;
591 handle
= chan
->handle
;
592 config
= &chan
->backend
.config
;
595 * Only flush buffers periodically if readers are active.
597 pthread_mutex_lock(&wakeup_fd_mutex
);
598 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
599 for_each_possible_cpu(cpu
) {
600 struct lttng_ust_lib_ring_buffer
*buf
=
601 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
605 if (uatomic_read(&buf
->active_readers
)
606 && lib_ring_buffer_poll_deliver(config
, buf
,
608 lib_ring_buffer_wakeup(buf
, handle
);
612 struct lttng_ust_lib_ring_buffer
*buf
=
613 shmp(handle
, chan
->backend
.buf
[0].shmp
);
617 if (uatomic_read(&buf
->active_readers
)
618 && lib_ring_buffer_poll_deliver(config
, buf
,
620 lib_ring_buffer_wakeup(buf
, handle
);
624 pthread_mutex_unlock(&wakeup_fd_mutex
);
628 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
630 struct channel
*chan
;
632 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
633 chan
= si
->si_value
.sival_ptr
;
634 DBG("Read timer for channel %p\n", chan
);
635 lib_ring_buffer_channel_do_read(chan
);
640 void rb_setmask(sigset_t
*mask
)
644 ret
= sigemptyset(mask
);
646 PERROR("sigemptyset");
648 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
652 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
656 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
663 void *sig_thread(void *arg
)
669 /* Only self thread will receive signal mask. */
671 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
674 signr
= sigwaitinfo(&mask
, &info
);
677 PERROR("sigwaitinfo");
680 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
681 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
683 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
684 lib_ring_buffer_channel_read_timer(info
.si_signo
,
686 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
688 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
691 ERR("Unexptected signal %d\n", info
.si_signo
);
698 * Ensure only a single thread listens on the timer signal.
701 void lib_ring_buffer_setup_timer_thread(void)
706 pthread_mutex_lock(&timer_signal
.lock
);
707 if (timer_signal
.setup_done
)
710 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
713 PERROR("pthread_create");
715 ret
= pthread_detach(thread
);
718 PERROR("pthread_detach");
720 timer_signal
.setup_done
= 1;
722 pthread_mutex_unlock(&timer_signal
.lock
);
726 * Wait for signal-handling thread quiescent state.
729 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
731 sigset_t pending_set
;
735 * We need to be the only thread interacting with the thread
736 * that manages signals for teardown synchronization.
738 pthread_mutex_lock(&timer_signal
.lock
);
741 * Ensure we don't have any signal queued for this channel.
744 ret
= sigemptyset(&pending_set
);
746 PERROR("sigemptyset");
748 ret
= sigpending(&pending_set
);
750 PERROR("sigpending");
752 if (!sigismember(&pending_set
, signr
))
758 * From this point, no new signal handler will be fired that
759 * would try to access "chan". However, we still need to wait
760 * for any currently executing handler to complete.
763 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
767 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
770 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
772 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
776 pthread_mutex_unlock(&timer_signal
.lock
);
780 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
783 struct itimerspec its
;
786 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
789 chan
->switch_timer_enabled
= 1;
791 lib_ring_buffer_setup_timer_thread();
793 memset(&sev
, 0, sizeof(sev
));
794 sev
.sigev_notify
= SIGEV_SIGNAL
;
795 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
796 sev
.sigev_value
.sival_ptr
= chan
;
797 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
799 PERROR("timer_create");
802 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
803 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
804 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
805 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
807 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
809 PERROR("timer_settime");
814 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
818 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
821 ret
= timer_delete(chan
->switch_timer
);
823 PERROR("timer_delete");
826 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
828 chan
->switch_timer
= 0;
829 chan
->switch_timer_enabled
= 0;
833 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
835 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
837 struct itimerspec its
;
840 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
841 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
844 chan
->read_timer_enabled
= 1;
846 lib_ring_buffer_setup_timer_thread();
848 sev
.sigev_notify
= SIGEV_SIGNAL
;
849 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
850 sev
.sigev_value
.sival_ptr
= chan
;
851 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
853 PERROR("timer_create");
856 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
857 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
858 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
859 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
861 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
863 PERROR("timer_settime");
868 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
870 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
873 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
874 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
877 ret
= timer_delete(chan
->read_timer
);
879 PERROR("timer_delete");
883 * do one more check to catch data that has been written in the last
886 lib_ring_buffer_channel_do_read(chan
);
888 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
890 chan
->read_timer
= 0;
891 chan
->read_timer_enabled
= 0;
894 static void channel_unregister_notifiers(struct channel
*chan
,
895 struct lttng_ust_shm_handle
*handle
)
897 lib_ring_buffer_channel_switch_timer_stop(chan
);
898 lib_ring_buffer_channel_read_timer_stop(chan
);
901 static void channel_print_errors(struct channel
*chan
,
902 struct lttng_ust_shm_handle
*handle
)
904 const struct lttng_ust_lib_ring_buffer_config
*config
=
905 &chan
->backend
.config
;
908 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
909 for_each_possible_cpu(cpu
) {
910 struct lttng_ust_lib_ring_buffer
*buf
=
911 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
913 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
916 struct lttng_ust_lib_ring_buffer
*buf
=
917 shmp(handle
, chan
->backend
.buf
[0].shmp
);
920 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
924 static void channel_free(struct channel
*chan
,
925 struct lttng_ust_shm_handle
*handle
,
928 channel_backend_free(&chan
->backend
, handle
);
929 /* chan is freed by shm teardown */
930 shm_object_table_destroy(handle
->table
, consumer
);
935 * channel_create - Create channel.
936 * @config: ring buffer instance configuration
937 * @name: name of the channel
938 * @priv_data: ring buffer client private data area pointer (output)
939 * @priv_data_size: length, in bytes, of the private data area.
940 * @priv_data_init: initialization data for private data.
941 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
942 * address mapping. It is used only by RING_BUFFER_STATIC
943 * configuration. It can be set to NULL for other backends.
944 * @subbuf_size: subbuffer size
945 * @num_subbuf: number of subbuffers
946 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
947 * padding to let readers get those sub-buffers.
948 * Used for live streaming.
949 * @read_timer_interval: Time interval (in us) to wake up pending readers.
950 * @stream_fds: array of stream file descriptors.
951 * @nr_stream_fds: number of file descriptors in array.
954 * Returns NULL on failure.
956 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
959 size_t priv_data_align
,
960 size_t priv_data_size
,
961 void *priv_data_init
,
962 void *buf_addr
, size_t subbuf_size
,
963 size_t num_subbuf
, unsigned int switch_timer_interval
,
964 unsigned int read_timer_interval
,
965 const int *stream_fds
, int nr_stream_fds
,
966 int64_t blocking_timeout
)
969 size_t shmsize
, chansize
;
970 struct channel
*chan
;
971 struct lttng_ust_shm_handle
*handle
;
972 struct shm_object
*shmobj
;
973 unsigned int nr_streams
;
974 int64_t blocking_timeout_ms
;
976 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
977 nr_streams
= num_possible_cpus();
981 if (nr_stream_fds
!= nr_streams
)
984 if (blocking_timeout
< -1) {
988 if (blocking_timeout
== -1) {
989 blocking_timeout_ms
= -1;
991 blocking_timeout_ms
= blocking_timeout
/ 1000;
992 if (blocking_timeout_ms
!= (int32_t) blocking_timeout_ms
) {
997 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
998 read_timer_interval
))
1001 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1005 /* Allocate table for channel + per-cpu buffers */
1006 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1008 goto error_table_alloc
;
1010 /* Calculate the shm allocation layout */
1011 shmsize
= sizeof(struct channel
);
1012 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
1013 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
1015 if (priv_data_align
)
1016 shmsize
+= lttng_ust_offset_align(shmsize
, priv_data_align
);
1017 shmsize
+= priv_data_size
;
1019 /* Allocate normal memory for channel (not shared) */
1020 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
1024 /* struct channel is at object 0, offset 0 (hardcoded) */
1025 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
1026 assert(handle
->chan
._ref
.index
== 0);
1027 assert(handle
->chan
._ref
.offset
== 0);
1028 chan
= shmp(handle
, handle
->chan
);
1031 chan
->nr_streams
= nr_streams
;
1033 /* space for private data */
1034 if (priv_data_size
) {
1035 DECLARE_SHMP(void, priv_data_alloc
);
1037 align_shm(shmobj
, priv_data_align
);
1038 chan
->priv_data_offset
= shmobj
->allocated_len
;
1039 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1040 if (!shmp(handle
, priv_data_alloc
))
1042 *priv_data
= channel_get_private(chan
);
1043 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
1045 chan
->priv_data_offset
= -1;
1050 chan
->u
.s
.blocking_timeout_ms
= (int32_t) blocking_timeout_ms
;
1052 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1053 subbuf_size
, num_subbuf
, handle
,
1056 goto error_backend_init
;
1058 chan
->handle
= handle
;
1059 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1061 chan
->switch_timer_interval
= switch_timer_interval
;
1062 chan
->read_timer_interval
= read_timer_interval
;
1063 lib_ring_buffer_channel_switch_timer_start(chan
);
1064 lib_ring_buffer_channel_read_timer_start(chan
);
1070 shm_object_table_destroy(handle
->table
, 1);
1076 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1077 uint64_t memory_map_size
,
1080 struct lttng_ust_shm_handle
*handle
;
1081 struct shm_object
*object
;
1083 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1087 /* Allocate table for channel + per-cpu buffers */
1088 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1090 goto error_table_alloc
;
1091 /* Add channel object */
1092 object
= shm_object_table_append_mem(handle
->table
, data
,
1093 memory_map_size
, wakeup_fd
);
1095 goto error_table_object
;
1096 /* struct channel is at object 0, offset 0 (hardcoded) */
1097 handle
->chan
._ref
.index
= 0;
1098 handle
->chan
._ref
.offset
= 0;
1102 shm_object_table_destroy(handle
->table
, 0);
1108 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1109 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1110 uint64_t memory_map_size
)
1112 struct shm_object
*object
;
1114 /* Add stream object */
1115 object
= shm_object_table_append_shm(handle
->table
,
1116 shm_fd
, wakeup_fd
, stream_nr
,
1123 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1125 assert(handle
->table
);
1126 return handle
->table
->allocated_len
- 1;
1130 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1133 channel_free(chan
, handle
, consumer
);
1137 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1138 * @chan: channel to destroy
1140 * Holds cpu hotplug.
1141 * Call "destroy" callback, finalize channels, decrement the channel
1142 * reference count. Note that when readers have completed data
1143 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1144 * They should release their handle at that point.
1146 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1151 * Note: the consumer takes care of finalizing and
1152 * switching the buffers.
1154 channel_unregister_notifiers(chan
, handle
);
1156 * The consumer prints errors.
1158 channel_print_errors(chan
, handle
);
1162 * sessiond/consumer are keeping a reference on the shm file
1163 * descriptor directly. No need to refcount.
1165 channel_release(chan
, handle
, consumer
);
1169 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
1170 const struct lttng_ust_lib_ring_buffer_config
*config
,
1171 struct channel
*chan
, int cpu
,
1172 struct lttng_ust_shm_handle
*handle
,
1173 int *shm_fd
, int *wait_fd
,
1175 uint64_t *memory_map_size
)
1177 struct shm_ref
*ref
;
1179 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1182 if (cpu
>= num_possible_cpus())
1185 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1186 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1187 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1188 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1189 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1191 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1194 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1195 struct channel
*chan
,
1196 struct lttng_ust_shm_handle
*handle
)
1198 struct shm_ref
*ref
;
1200 ref
= &handle
->chan
._ref
;
1201 return shm_close_wait_fd(handle
, ref
);
1204 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1205 struct channel
*chan
,
1206 struct lttng_ust_shm_handle
*handle
)
1208 struct shm_ref
*ref
;
1210 ref
= &handle
->chan
._ref
;
1211 return shm_close_wakeup_fd(handle
, ref
);
1214 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1215 struct channel
*chan
,
1216 struct lttng_ust_shm_handle
*handle
,
1219 struct shm_ref
*ref
;
1221 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1224 if (cpu
>= num_possible_cpus())
1227 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1228 return shm_close_wait_fd(handle
, ref
);
1231 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1232 struct channel
*chan
,
1233 struct lttng_ust_shm_handle
*handle
,
1236 struct shm_ref
*ref
;
1239 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1242 if (cpu
>= num_possible_cpus())
1245 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1246 pthread_mutex_lock(&wakeup_fd_mutex
);
1247 ret
= shm_close_wakeup_fd(handle
, ref
);
1248 pthread_mutex_unlock(&wakeup_fd_mutex
);
1252 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
1253 struct lttng_ust_shm_handle
*handle
)
1255 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1261 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
1262 struct lttng_ust_shm_handle
*handle
)
1264 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1268 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1270 uatomic_dec(&buf
->active_readers
);
1274 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1276 * @consumed: consumed count indicating the position where to read
1277 * @produced: produced count, indicates position when to stop reading
1279 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1280 * data to read at consumed position, or 0 if the get operation succeeds.
1283 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1284 unsigned long *consumed
, unsigned long *produced
,
1285 struct lttng_ust_shm_handle
*handle
)
1287 struct channel
*chan
;
1288 const struct lttng_ust_lib_ring_buffer_config
*config
;
1289 unsigned long consumed_cur
, write_offset
;
1292 chan
= shmp(handle
, buf
->backend
.chan
);
1295 config
= &chan
->backend
.config
;
1296 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1298 * Read finalized before counters.
1301 consumed_cur
= uatomic_read(&buf
->consumed
);
1303 * No need to issue a memory barrier between consumed count read and
1304 * write offset read, because consumed count can only change
1305 * concurrently in overwrite mode, and we keep a sequence counter
1306 * identifier derived from the write offset to check we are getting
1307 * the same sub-buffer we are expecting (the sub-buffers are atomically
1308 * "tagged" upon writes, tags are checked upon read).
1310 write_offset
= v_read(config
, &buf
->offset
);
1313 * Check that we are not about to read the same subbuffer in
1314 * which the writer head is.
1316 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1320 *consumed
= consumed_cur
;
1321 *produced
= subbuf_trunc(write_offset
, chan
);
1327 * The memory barriers __wait_event()/wake_up_interruptible() take care
1328 * of "raw_spin_is_locked" memory ordering.
1337 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1338 * are saved regardless of whether the consumed and produced positions are
1339 * in the same subbuffer.
1341 * @consumed: consumed byte count indicating the last position read
1342 * @produced: produced byte count indicating the last position written
1344 * This function is meant to provide information on the exact producer and
1345 * consumer positions without regard for the "snapshot" feature.
1347 int lib_ring_buffer_snapshot_sample_positions(
1348 struct lttng_ust_lib_ring_buffer
*buf
,
1349 unsigned long *consumed
, unsigned long *produced
,
1350 struct lttng_ust_shm_handle
*handle
)
1352 struct channel
*chan
;
1353 const struct lttng_ust_lib_ring_buffer_config
*config
;
1355 chan
= shmp(handle
, buf
->backend
.chan
);
1358 config
= &chan
->backend
.config
;
1360 *consumed
= uatomic_read(&buf
->consumed
);
1362 * No need to issue a memory barrier between consumed count read and
1363 * write offset read, because consumed count can only change
1364 * concurrently in overwrite mode, and we keep a sequence counter
1365 * identifier derived from the write offset to check we are getting
1366 * the same sub-buffer we are expecting (the sub-buffers are atomically
1367 * "tagged" upon writes, tags are checked upon read).
1369 *produced
= v_read(config
, &buf
->offset
);
1374 * lib_ring_buffer_move_consumer - move consumed counter forward
1376 * @consumed_new: new consumed count value
1378 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1379 unsigned long consumed_new
,
1380 struct lttng_ust_shm_handle
*handle
)
1382 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1383 struct channel
*chan
;
1384 unsigned long consumed
;
1386 chan
= shmp(handle
, bufb
->chan
);
1389 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1392 * Only push the consumed value forward.
1393 * If the consumed cmpxchg fails, this is because we have been pushed by
1394 * the writer in flight recorder mode.
1396 consumed
= uatomic_read(&buf
->consumed
);
1397 while ((long) consumed
- (long) consumed_new
< 0)
1398 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1403 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1405 * @consumed: consumed count indicating the position where to read
1407 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1408 * data to read at consumed position, or 0 if the get operation succeeds.
1410 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1411 unsigned long consumed
,
1412 struct lttng_ust_shm_handle
*handle
)
1414 struct channel
*chan
;
1415 const struct lttng_ust_lib_ring_buffer_config
*config
;
1416 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1417 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1418 struct commit_counters_cold
*cc_cold
;
1420 chan
= shmp(handle
, buf
->backend
.chan
);
1423 config
= &chan
->backend
.config
;
1425 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1427 * Read finalized before counters.
1430 consumed_cur
= uatomic_read(&buf
->consumed
);
1431 consumed_idx
= subbuf_index(consumed
, chan
);
1432 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1435 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1437 * Make sure we read the commit count before reading the buffer
1438 * data and the write offset. Correct consumed offset ordering
1439 * wrt commit count is insured by the use of cmpxchg to update
1440 * the consumed offset.
1443 * Local rmb to match the remote wmb to read the commit count
1444 * before the buffer data and the write offset.
1448 write_offset
= v_read(config
, &buf
->offset
);
1451 * Check that the buffer we are getting is after or at consumed_cur
1454 if ((long) subbuf_trunc(consumed
, chan
)
1455 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1459 * Check that the subbuffer we are trying to consume has been
1460 * already fully committed. There are a few causes that can make
1461 * this unavailability situation occur:
1463 * Temporary (short-term) situation:
1464 * - Application is running on a different CPU, between reserve
1465 * and commit ring buffer operations,
1466 * - Application is preempted between reserve and commit ring
1467 * buffer operations,
1469 * Long-term situation:
1470 * - Application is stopped (SIGSTOP) between reserve and commit
1471 * ring buffer operations. Could eventually be resumed by
1473 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1474 * reserve and commit ring buffer operation.
1476 * From a consumer perspective, handling short-term
1477 * unavailability situations is performed by retrying a few
1478 * times after a delay. Handling long-term unavailability
1479 * situations is handled by failing to get the sub-buffer.
1481 * In all of those situations, if the application is taking a
1482 * long time to perform its commit after ring buffer space
1483 * reservation, we can end up in a situation where the producer
1484 * will fill the ring buffer and try to write into the same
1485 * sub-buffer again (which has a missing commit). This is
1486 * handled by the producer in the sub-buffer switch handling
1487 * code of the reserve routine by detecting unbalanced
1488 * reserve/commit counters and discarding all further events
1489 * until the situation is resolved in those situations. Two
1490 * scenarios can occur:
1492 * 1) The application causing the reserve/commit counters to be
1493 * unbalanced has been terminated. In this situation, all
1494 * further events will be discarded in the buffers, and no
1495 * further buffer data will be readable by the consumer
1496 * daemon. Tearing down the UST tracing session and starting
1497 * anew is a work-around for those situations. Note that this
1498 * only affects per-UID tracing. In per-PID tracing, the
1499 * application vanishes with the termination, and therefore
1500 * no more data needs to be written to the buffers.
1501 * 2) The application causing the unbalance has been delayed for
1502 * a long time, but will eventually try to increment the
1503 * commit counter after eventually writing to the sub-buffer.
1504 * This situation can cause events to be discarded until the
1505 * application resumes its operations.
1507 if (((commit_count
- chan
->backend
.subbuf_size
)
1508 & chan
->commit_count_mask
)
1509 - (buf_trunc(consumed
, chan
)
1510 >> chan
->backend
.num_subbuf_order
)
1512 if (nr_retry
-- > 0) {
1513 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1514 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1522 * Check that we are not about to read the same subbuffer in
1523 * which the writer head is.
1525 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1530 * Failure to get the subbuffer causes a busy-loop retry without going
1531 * to a wait queue. These are caused by short-lived race windows where
1532 * the writer is getting access to a subbuffer we were trying to get
1533 * access to. Also checks that the "consumed" buffer count we are
1534 * looking for matches the one contained in the subbuffer id.
1536 * The short-lived race window described here can be affected by
1537 * application signals and preemption, thus requiring to bound
1538 * the loop to a maximum number of retry.
1540 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1541 consumed_idx
, buf_trunc_val(consumed
, chan
),
1544 if (nr_retry
-- > 0) {
1545 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1546 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1552 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1554 buf
->get_subbuf_consumed
= consumed
;
1555 buf
->get_subbuf
= 1;
1561 * The memory barriers __wait_event()/wake_up_interruptible() take care
1562 * of "raw_spin_is_locked" memory ordering.
1571 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1574 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1575 struct lttng_ust_shm_handle
*handle
)
1577 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1578 struct channel
*chan
;
1579 const struct lttng_ust_lib_ring_buffer_config
*config
;
1580 unsigned long sb_bindex
, consumed_idx
, consumed
;
1581 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
1582 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
1584 chan
= shmp(handle
, bufb
->chan
);
1587 config
= &chan
->backend
.config
;
1588 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1590 if (!buf
->get_subbuf
) {
1592 * Reader puts a subbuffer it did not get.
1594 CHAN_WARN_ON(chan
, 1);
1597 consumed
= buf
->get_subbuf_consumed
;
1598 buf
->get_subbuf
= 0;
1601 * Clear the records_unread counter. (overruns counter)
1602 * Can still be non-zero if a file reader simply grabbed the data
1603 * without using iterators.
1604 * Can be below zero if an iterator is used on a snapshot more than
1607 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1608 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1611 backend_pages
= shmp(handle
, rpages
->shmp
);
1614 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1615 &bufb
->records_read
);
1616 v_set(config
, &backend_pages
->records_unread
, 0);
1617 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1618 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1619 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1622 * Exchange the reader subbuffer with the one we put in its place in the
1623 * writer subbuffer table. Expect the original consumed count. If
1624 * update_read_sb_index fails, this is because the writer updated the
1625 * subbuffer concurrently. We should therefore keep the subbuffer we
1626 * currently have: it has become invalid to try reading this sub-buffer
1627 * consumed count value anyway.
1629 consumed_idx
= subbuf_index(consumed
, chan
);
1630 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1631 consumed_idx
, buf_trunc_val(consumed
, chan
),
1634 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1635 * if the writer concurrently updated it.
1640 * cons_offset is an iterator on all subbuffer offsets between the reader
1641 * position and the writer position. (inclusive)
1644 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1645 struct channel
*chan
,
1646 unsigned long cons_offset
,
1648 struct lttng_ust_shm_handle
*handle
)
1650 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1651 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1652 struct commit_counters_hot
*cc_hot
;
1653 struct commit_counters_cold
*cc_cold
;
1655 cons_idx
= subbuf_index(cons_offset
, chan
);
1656 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1659 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1662 commit_count
= v_read(config
, &cc_hot
->cc
);
1663 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1665 if (subbuf_offset(commit_count
, chan
) != 0)
1666 DBG("ring buffer %s, cpu %d: "
1667 "commit count in subbuffer %lu,\n"
1668 "expecting multiples of %lu bytes\n"
1669 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1670 chan
->backend
.name
, cpu
, cons_idx
,
1671 chan
->backend
.subbuf_size
,
1672 commit_count
, commit_count_sb
);
1674 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1675 chan
->backend
.name
, cpu
, commit_count
);
1679 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1680 struct channel
*chan
,
1681 void *priv
, int cpu
,
1682 struct lttng_ust_shm_handle
*handle
)
1684 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1685 unsigned long write_offset
, cons_offset
;
1688 * No need to order commit_count, write_offset and cons_offset reads
1689 * because we execute at teardown when no more writer nor reader
1690 * references are left.
1692 write_offset
= v_read(config
, &buf
->offset
);
1693 cons_offset
= uatomic_read(&buf
->consumed
);
1694 if (write_offset
!= cons_offset
)
1695 DBG("ring buffer %s, cpu %d: "
1696 "non-consumed data\n"
1697 " [ %lu bytes written, %lu bytes read ]\n",
1698 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1700 for (cons_offset
= uatomic_read(&buf
->consumed
);
1701 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1704 cons_offset
= subbuf_align(cons_offset
, chan
))
1705 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1710 void lib_ring_buffer_print_errors(struct channel
*chan
,
1711 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1712 struct lttng_ust_shm_handle
*handle
)
1714 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1715 void *priv
= channel_get_private(chan
);
1717 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1718 DBG("ring buffer %s: %lu records written, "
1719 "%lu records overrun\n",
1721 v_read(config
, &buf
->records_count
),
1722 v_read(config
, &buf
->records_overrun
));
1724 DBG("ring buffer %s, cpu %d: %lu records written, "
1725 "%lu records overrun\n",
1726 chan
->backend
.name
, cpu
,
1727 v_read(config
, &buf
->records_count
),
1728 v_read(config
, &buf
->records_overrun
));
1730 if (v_read(config
, &buf
->records_lost_full
)
1731 || v_read(config
, &buf
->records_lost_wrap
)
1732 || v_read(config
, &buf
->records_lost_big
))
1733 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1734 " [ %lu buffer full, %lu nest buffer wrap-around, "
1735 "%lu event too big ]\n",
1736 chan
->backend
.name
, cpu
,
1737 v_read(config
, &buf
->records_lost_full
),
1738 v_read(config
, &buf
->records_lost_wrap
),
1739 v_read(config
, &buf
->records_lost_big
));
1741 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1745 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1747 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1748 * active or at buffer finalization (destroy).
1751 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1752 struct channel
*chan
,
1753 struct switch_offsets
*offsets
,
1755 struct lttng_ust_shm_handle
*handle
)
1757 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1758 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1759 unsigned long commit_count
;
1760 struct commit_counters_hot
*cc_hot
;
1762 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1765 * Order all writes to buffer before the commit count update that will
1766 * determine that the subbuffer is full.
1769 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1772 v_add(config
, config
->cb
.subbuffer_header_size(),
1774 commit_count
= v_read(config
, &cc_hot
->cc
);
1775 /* Check if the written buffer has to be delivered */
1776 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1777 commit_count
, oldidx
, handle
, tsc
);
1778 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1779 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1780 commit_count
, handle
, cc_hot
);
1784 * lib_ring_buffer_switch_old_end: switch old subbuffer
1786 * Note : offset_old should never be 0 here. It is ok, because we never perform
1787 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1788 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1792 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1793 struct channel
*chan
,
1794 struct switch_offsets
*offsets
,
1796 struct lttng_ust_shm_handle
*handle
)
1798 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1799 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1800 unsigned long commit_count
, padding_size
, data_size
;
1801 struct commit_counters_hot
*cc_hot
;
1804 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1805 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1806 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1809 ts_end
= shmp_index(handle
, buf
->ts_end
, oldidx
);
1813 * This is the last space reservation in that sub-buffer before
1814 * it gets delivered. This provides exclusive access to write to
1815 * this sub-buffer's ts_end. There are also no concurrent
1816 * readers of that ts_end because delivery of that sub-buffer is
1817 * postponed until the commit counter is incremented for the
1818 * current space reservation.
1823 * Order all writes to buffer and store to ts_end before the commit
1824 * count update that will determine that the subbuffer is full.
1827 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1830 v_add(config
, padding_size
, &cc_hot
->cc
);
1831 commit_count
= v_read(config
, &cc_hot
->cc
);
1832 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1833 commit_count
, oldidx
, handle
, tsc
);
1834 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1835 offsets
->old
+ padding_size
, commit_count
, handle
,
1840 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1842 * This code can be executed unordered : writers may already have written to the
1843 * sub-buffer before this code gets executed, caution. The commit makes sure
1844 * that this code is executed before the deliver of this sub-buffer.
1847 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1848 struct channel
*chan
,
1849 struct switch_offsets
*offsets
,
1851 struct lttng_ust_shm_handle
*handle
)
1853 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1854 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1855 unsigned long commit_count
;
1856 struct commit_counters_hot
*cc_hot
;
1858 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1861 * Order all writes to buffer before the commit count update that will
1862 * determine that the subbuffer is full.
1865 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1868 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1869 commit_count
= v_read(config
, &cc_hot
->cc
);
1870 /* Check if the written buffer has to be delivered */
1871 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1872 commit_count
, beginidx
, handle
, tsc
);
1873 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1874 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1875 commit_count
, handle
, cc_hot
);
1879 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1881 * Calls subbuffer_set_data_size() to set the data size of the current
1882 * sub-buffer. We do not need to perform check_deliver nor commit here,
1883 * since this task will be done by the "commit" of the event for which
1884 * we are currently doing the space reservation.
1887 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1888 struct channel
*chan
,
1889 struct switch_offsets
*offsets
,
1891 struct lttng_ust_shm_handle
*handle
)
1893 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1894 unsigned long endidx
, data_size
;
1897 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1898 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1899 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1901 ts_end
= shmp_index(handle
, buf
->ts_end
, endidx
);
1905 * This is the last space reservation in that sub-buffer before
1906 * it gets delivered. This provides exclusive access to write to
1907 * this sub-buffer's ts_end. There are also no concurrent
1908 * readers of that ts_end because delivery of that sub-buffer is
1909 * postponed until the commit counter is incremented for the
1910 * current space reservation.
1918 * !0 if execution must be aborted.
1921 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1922 struct lttng_ust_lib_ring_buffer
*buf
,
1923 struct channel
*chan
,
1924 struct switch_offsets
*offsets
,
1926 struct lttng_ust_shm_handle
*handle
)
1928 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1929 unsigned long off
, reserve_commit_diff
;
1931 offsets
->begin
= v_read(config
, &buf
->offset
);
1932 offsets
->old
= offsets
->begin
;
1933 offsets
->switch_old_start
= 0;
1934 off
= subbuf_offset(offsets
->begin
, chan
);
1936 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1939 * Ensure we flush the header of an empty subbuffer when doing the
1940 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1941 * total data gathering duration even if there were no records saved
1942 * after the last buffer switch.
1943 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1944 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1945 * subbuffer header as appropriate.
1946 * The next record that reserves space will be responsible for
1947 * populating the following subbuffer header. We choose not to populate
1948 * the next subbuffer header here because we want to be able to use
1949 * SWITCH_ACTIVE for periodical buffer flush, which must
1950 * guarantee that all the buffer content (records and header
1951 * timestamps) are visible to the reader. This is required for
1952 * quiescence guarantees for the fusion merge.
1954 if (mode
!= SWITCH_FLUSH
&& !off
)
1955 return -1; /* we do not have to switch : buffer is empty */
1957 if (caa_unlikely(off
== 0)) {
1958 unsigned long sb_index
, commit_count
;
1959 struct commit_counters_cold
*cc_cold
;
1962 * We are performing a SWITCH_FLUSH. There may be concurrent
1963 * writes into the buffer if e.g. invoked while performing a
1964 * snapshot on an active trace.
1966 * If the client does not save any header information
1967 * (sub-buffer header size == 0), don't switch empty subbuffer
1968 * on finalize, because it is invalid to deliver a completely
1971 if (!config
->cb
.subbuffer_header_size())
1974 /* Test new buffer integrity */
1975 sb_index
= subbuf_index(offsets
->begin
, chan
);
1976 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1979 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1980 reserve_commit_diff
=
1981 (buf_trunc(offsets
->begin
, chan
)
1982 >> chan
->backend
.num_subbuf_order
)
1983 - (commit_count
& chan
->commit_count_mask
);
1984 if (caa_likely(reserve_commit_diff
== 0)) {
1985 /* Next subbuffer not being written to. */
1986 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1987 subbuf_trunc(offsets
->begin
, chan
)
1988 - subbuf_trunc((unsigned long)
1989 uatomic_read(&buf
->consumed
), chan
)
1990 >= chan
->backend
.buf_size
)) {
1992 * We do not overwrite non consumed buffers
1993 * and we are full : don't switch.
1998 * Next subbuffer not being written to, and we
1999 * are either in overwrite mode or the buffer is
2000 * not full. It's safe to write in this new
2006 * Next subbuffer reserve offset does not match the
2007 * commit offset. Don't perform switch in
2008 * producer-consumer and overwrite mode. Caused by
2009 * either a writer OOPS or too many nested writes over a
2010 * reserve/commit pair.
2016 * Need to write the subbuffer start header on finalize.
2018 offsets
->switch_old_start
= 1;
2020 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2021 /* Note: old points to the next subbuf at offset 0 */
2022 offsets
->end
= offsets
->begin
;
2027 * Force a sub-buffer switch. This operation is completely reentrant : can be
2028 * called while tracing is active with absolutely no lock held.
2030 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2031 * some atomic operations, this function must be called from the CPU
2032 * which owns the buffer for a ACTIVE flush. However, for
2033 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2036 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
2037 struct lttng_ust_shm_handle
*handle
)
2039 struct channel
*chan
;
2040 const struct lttng_ust_lib_ring_buffer_config
*config
;
2041 struct switch_offsets offsets
;
2042 unsigned long oldidx
;
2045 chan
= shmp(handle
, buf
->backend
.chan
);
2048 config
= &chan
->backend
.config
;
2053 * Perform retryable operations.
2056 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
2058 return; /* Switch not needed */
2059 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
2063 * Atomically update last_tsc. This update races against concurrent
2064 * atomic updates, but the race will always cause supplementary full TSC
2065 * records, never the opposite (missing a full TSC record when it would
2068 save_last_tsc(config
, buf
, tsc
);
2071 * Push the reader if necessary
2073 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
2075 oldidx
= subbuf_index(offsets
.old
, chan
);
2076 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
2079 * May need to populate header start on SWITCH_FLUSH.
2081 if (offsets
.switch_old_start
) {
2082 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
2083 offsets
.old
+= config
->cb
.subbuffer_header_size();
2087 * Switch old subbuffer.
2089 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
2093 bool handle_blocking_retry(int *timeout_left_ms
)
2095 int timeout
= *timeout_left_ms
, delay
;
2097 if (caa_likely(!timeout
))
2098 return false; /* Do not retry, discard event. */
2099 if (timeout
< 0) /* Wait forever. */
2100 delay
= RETRY_DELAY_MS
;
2102 delay
= min_t(int, timeout
, RETRY_DELAY_MS
);
2103 (void) poll(NULL
, 0, delay
);
2105 *timeout_left_ms
-= delay
;
2106 return true; /* Retry. */
2112 * -ENOSPC if event size is too large for packet.
2113 * -ENOBUFS if there is currently not enough space in buffer for the event.
2114 * -EIO if data cannot be written into the buffer for any other reason.
2117 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
2118 struct channel
*chan
,
2119 struct switch_offsets
*offsets
,
2120 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2123 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2124 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2125 unsigned long reserve_commit_diff
, offset_cmp
;
2126 int timeout_left_ms
= lttng_ust_ringbuffer_get_timeout(chan
);
2129 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2130 offsets
->old
= offsets
->begin
;
2131 offsets
->switch_new_start
= 0;
2132 offsets
->switch_new_end
= 0;
2133 offsets
->switch_old_end
= 0;
2134 offsets
->pre_header_padding
= 0;
2136 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2137 if ((int64_t) ctx
->tsc
== -EIO
)
2140 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
2141 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2143 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
2144 offsets
->switch_new_start
= 1; /* For offsets->begin */
2146 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2148 &offsets
->pre_header_padding
,
2151 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2154 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2155 offsets
->size
> chan
->backend
.subbuf_size
)) {
2156 offsets
->switch_old_end
= 1; /* For offsets->old */
2157 offsets
->switch_new_start
= 1; /* For offsets->begin */
2160 if (caa_unlikely(offsets
->switch_new_start
)) {
2161 unsigned long sb_index
, commit_count
;
2162 struct commit_counters_cold
*cc_cold
;
2165 * We are typically not filling the previous buffer completely.
2167 if (caa_likely(offsets
->switch_old_end
))
2168 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2169 offsets
->begin
= offsets
->begin
2170 + config
->cb
.subbuffer_header_size();
2171 /* Test new buffer integrity */
2172 sb_index
= subbuf_index(offsets
->begin
, chan
);
2174 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2175 * lib_ring_buffer_check_deliver() has the matching
2176 * memory barriers required around commit_cold cc_sb
2177 * updates to ensure reserve and commit counter updates
2178 * are not seen reordered when updated by another CPU.
2181 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2184 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2185 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2187 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2189 * The reserve counter have been concurrently updated
2190 * while we read the commit counter. This means the
2191 * commit counter we read might not match buf->offset
2192 * due to concurrent update. We therefore need to retry.
2196 reserve_commit_diff
=
2197 (buf_trunc(offsets
->begin
, chan
)
2198 >> chan
->backend
.num_subbuf_order
)
2199 - (commit_count
& chan
->commit_count_mask
);
2200 if (caa_likely(reserve_commit_diff
== 0)) {
2201 /* Next subbuffer not being written to. */
2202 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2203 subbuf_trunc(offsets
->begin
, chan
)
2204 - subbuf_trunc((unsigned long)
2205 uatomic_read(&buf
->consumed
), chan
)
2206 >= chan
->backend
.buf_size
)) {
2207 unsigned long nr_lost
;
2209 if (handle_blocking_retry(&timeout_left_ms
))
2213 * We do not overwrite non consumed buffers
2214 * and we are full : record is lost.
2216 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2217 v_inc(config
, &buf
->records_lost_full
);
2218 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2219 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2220 nr_lost
+ 1, chan
->backend
.name
,
2226 * Next subbuffer not being written to, and we
2227 * are either in overwrite mode or the buffer is
2228 * not full. It's safe to write in this new
2233 unsigned long nr_lost
;
2236 * Next subbuffer reserve offset does not match the
2237 * commit offset, and this did not involve update to the
2238 * reserve counter. Drop record in producer-consumer and
2239 * overwrite mode. Caused by either a writer OOPS or too
2240 * many nested writes over a reserve/commit pair.
2242 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2243 v_inc(config
, &buf
->records_lost_wrap
);
2244 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2245 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2246 nr_lost
+ 1, chan
->backend
.name
,
2252 config
->cb
.record_header_size(config
, chan
,
2254 &offsets
->pre_header_padding
,
2257 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2260 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2261 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2262 unsigned long nr_lost
;
2265 * Record too big for subbuffers, report error, don't
2266 * complete the sub-buffer switch.
2268 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2269 v_inc(config
, &buf
->records_lost_big
);
2270 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2271 DBG("%lu or more records lost in (%s:%d) record size "
2272 " of %zu bytes is too large for buffer\n",
2273 nr_lost
+ 1, chan
->backend
.name
,
2274 buf
->backend
.cpu
, offsets
->size
);
2279 * We just made a successful buffer switch and the
2280 * record fits in the new subbuffer. Let's write.
2285 * Record fits in the current buffer and we are not on a switch
2286 * boundary. It's safe to write.
2289 offsets
->end
= offsets
->begin
+ offsets
->size
;
2291 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2293 * The offset_end will fall at the very beginning of the next
2296 offsets
->switch_new_end
= 1; /* For offsets->begin */
2302 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2303 * @ctx: ring buffer context.
2305 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2306 * -EIO for other errors, else returns 0.
2307 * It will take care of sub-buffer switching.
2309 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2312 struct channel
*chan
= ctx
->chan
;
2313 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2314 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2315 struct lttng_ust_lib_ring_buffer
*buf
;
2316 struct switch_offsets offsets
;
2319 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2320 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
2322 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2330 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2332 if (caa_unlikely(ret
))
2334 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2339 * Atomically update last_tsc. This update races against concurrent
2340 * atomic updates, but the race will always cause supplementary full TSC
2341 * records, never the opposite (missing a full TSC record when it would
2344 save_last_tsc(config
, buf
, ctx
->tsc
);
2347 * Push the reader if necessary
2349 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2352 * Clear noref flag for this subbuffer.
2354 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2355 subbuf_index(offsets
.end
- 1, chan
),
2359 * Switch old subbuffer if needed.
2361 if (caa_unlikely(offsets
.switch_old_end
)) {
2362 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2363 subbuf_index(offsets
.old
- 1, chan
),
2365 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2369 * Populate new subbuffer.
2371 if (caa_unlikely(offsets
.switch_new_start
))
2372 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2374 if (caa_unlikely(offsets
.switch_new_end
))
2375 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2377 ctx
->slot_size
= offsets
.size
;
2378 ctx
->pre_offset
= offsets
.begin
;
2379 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2384 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
2385 struct lttng_ust_lib_ring_buffer
*buf
,
2386 unsigned long commit_count
,
2388 struct lttng_ust_shm_handle
*handle
)
2390 struct commit_counters_hot
*cc_hot
;
2392 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2394 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2397 v_set(config
, &cc_hot
->seq
, commit_count
);
2401 * The ring buffer can count events recorded and overwritten per buffer,
2402 * but it is disabled by default due to its performance overhead.
2404 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2406 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2407 struct lttng_ust_lib_ring_buffer
*buf
,
2409 struct lttng_ust_shm_handle
*handle
)
2411 v_add(config
, subbuffer_get_records_count(config
,
2412 &buf
->backend
, idx
, handle
),
2413 &buf
->records_count
);
2414 v_add(config
, subbuffer_count_records_overrun(config
,
2415 &buf
->backend
, idx
, handle
),
2416 &buf
->records_overrun
);
2418 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2420 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2421 struct lttng_ust_lib_ring_buffer
*buf
,
2423 struct lttng_ust_shm_handle
*handle
)
2426 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2428 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
2429 struct lttng_ust_lib_ring_buffer
*buf
,
2430 struct channel
*chan
,
2431 unsigned long offset
,
2432 unsigned long commit_count
,
2434 struct lttng_ust_shm_handle
*handle
,
2437 unsigned long old_commit_count
= commit_count
2438 - chan
->backend
.subbuf_size
;
2439 struct commit_counters_cold
*cc_cold
;
2442 * If we succeeded at updating cc_sb below, we are the subbuffer
2443 * writer delivering the subbuffer. Deals with concurrent
2444 * updates of the "cc" value without adding a add_return atomic
2445 * operation to the fast path.
2447 * We are doing the delivery in two steps:
2448 * - First, we cmpxchg() cc_sb to the new value
2449 * old_commit_count + 1. This ensures that we are the only
2450 * subbuffer user successfully filling the subbuffer, but we
2451 * do _not_ set the cc_sb value to "commit_count" yet.
2452 * Therefore, other writers that would wrap around the ring
2453 * buffer and try to start writing to our subbuffer would
2454 * have to drop records, because it would appear as
2456 * We therefore have exclusive access to the subbuffer control
2457 * structures. This mutual exclusion with other writers is
2458 * crucially important to perform record overruns count in
2459 * flight recorder mode locklessly.
2460 * - When we are ready to release the subbuffer (either for
2461 * reading or for overrun by other writers), we simply set the
2462 * cc_sb value to "commit_count" and perform delivery.
2464 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2465 * This guarantees that old_commit_count + 1 != commit_count.
2469 * Order prior updates to reserve count prior to the
2470 * commit_cold cc_sb update.
2473 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2476 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2477 old_commit_count
, old_commit_count
+ 1)
2478 == old_commit_count
)) {
2482 * Start of exclusive subbuffer access. We are
2483 * guaranteed to be the last writer in this subbuffer
2484 * and any other writer trying to access this subbuffer
2485 * in this state is required to drop records.
2487 * We can read the ts_end for the current sub-buffer
2488 * which has been saved by the very last space
2489 * reservation for the current sub-buffer.
2491 * Order increment of commit counter before reading ts_end.
2494 ts_end
= shmp_index(handle
, buf
->ts_end
, idx
);
2497 deliver_count_events(config
, buf
, idx
, handle
);
2498 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2499 lib_ring_buffer_get_data_size(config
,
2506 * Increment the packet counter while we have exclusive
2509 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2512 * Set noref flag and offset for this subbuffer id.
2513 * Contains a memory barrier that ensures counter stores
2514 * are ordered before set noref and offset.
2516 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2517 buf_trunc_val(offset
, chan
), handle
);
2520 * Order set_noref and record counter updates before the
2521 * end of subbuffer exclusive access. Orders with
2522 * respect to writers coming into the subbuffer after
2523 * wrap around, and also order wrt concurrent readers.
2526 /* End of exclusive subbuffer access */
2527 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2529 * Order later updates to reserve count after
2530 * the commit cold cc_sb update.
2533 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2534 commit_count
, idx
, handle
);
2537 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2539 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2540 && uatomic_read(&buf
->active_readers
)
2541 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2542 lib_ring_buffer_wakeup(buf
, handle
);
2548 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2550 void lttng_fixup_ringbuffer_tls(void)
2552 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2555 void lib_ringbuffer_signal_init(void)
2561 * Block signal for entire process, so only our thread processes
2565 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2568 PERROR("pthread_sigmask");