2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
7 * recorder (overwrite) modes. See thesis:
9 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
10 * dissertation, Ecole Polytechnique de Montreal.
11 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
13 * - Algorithm presentation in Chapter 5:
14 * "Lockless Multi-Core High-Throughput Buffering".
15 * - Algorithm formal verification in Section 8.6:
16 * "Formal verification of LTTng"
19 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
21 * Inspired from LTT and RelayFS:
22 * Karim Yaghmour <karim@opersys.com>
23 * Tom Zanussi <zanussi@us.ibm.com>
24 * Bob Wisniewski <bob@watson.ibm.com>
26 * Bob Wisniewski <bob@watson.ibm.com>
28 * Buffer reader semantic :
31 * while buffer is not finalized and empty
33 * - if return value != 0, continue
34 * - splice one subbuffer worth of data to a pipe
35 * - splice the data from pipe to disk/network
40 #include <sys/types.h>
49 #include <urcu/compiler.h>
51 #include <urcu/tls-compat.h>
53 #include "common/macros.h"
55 #include <lttng/ust-utils.h>
56 #include <lttng/ust-ringbuffer-context.h>
58 #include "common/smp.h"
59 #include "ringbuffer-config.h"
65 #include "common/compat/errno.h" /* For ENODATA */
67 /* Print DBG() messages about events lost only every 1048576 hits */
68 #define DBG_PRINT_NR_LOST (1UL << 20)
70 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
71 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
72 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
73 #define CLOCKID CLOCK_MONOTONIC
74 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
75 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
76 #define RETRY_DELAY_MS 100 /* 100 ms. */
79 * Non-static to ensure the compiler does not optimize away the xor.
81 uint8_t lttng_crash_magic_xor
[]
82 __attribute__((visibility("hidden")));
83 uint8_t lttng_crash_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
86 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
87 * close(2) to close the fd returned by shm_open.
88 * shm_unlink releases the shared memory object name.
89 * ftruncate(2) sets the size of the memory object.
90 * mmap/munmap maps the shared memory obj to a virtual address in the
91 * calling proceess (should be done both in libust and consumer).
92 * See shm_overview(7) for details.
93 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
96 * Since we don't need to access the object using its name, we can
97 * immediately shm_unlink(3) it, and only keep the handle with its file
102 * Internal structure representing offsets to use at a sub-buffer switch.
104 struct switch_offsets
{
105 unsigned long begin
, end
, old
;
106 size_t pre_header_padding
, size
;
107 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
111 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
114 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
117 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
120 void lib_ring_buffer_print_errors(struct lttng_ust_ring_buffer_channel
*chan
,
121 struct lttng_ust_ring_buffer
*buf
, int cpu
,
122 struct lttng_ust_shm_handle
*handle
);
125 * Handle timer teardown race wrt memory free of private data by
126 * ring buffer signals are handled by a single thread, which permits
127 * a synchronization point between handling of each signal.
128 * Protected by the lock within the structure.
130 struct timer_signal_data
{
131 pthread_t tid
; /* thread id managing signals */
134 pthread_mutex_t lock
;
137 static struct timer_signal_data timer_signal
= {
141 .lock
= PTHREAD_MUTEX_INITIALIZER
,
144 static bool lttng_ust_allow_blocking
;
146 void lttng_ust_ringbuffer_set_allow_blocking(void)
148 lttng_ust_allow_blocking
= true;
151 /* Get blocking timeout, in ms */
152 static int lttng_ust_ringbuffer_get_timeout(struct lttng_ust_ring_buffer_channel
*chan
)
154 if (!lttng_ust_allow_blocking
)
156 return chan
->u
.s
.blocking_timeout_ms
;
160 * lib_ring_buffer_reset - Reset ring buffer to initial values.
163 * Effectively empty the ring buffer. Should be called when the buffer is not
164 * used for writing. The ring buffer can be opened for reading, but the reader
165 * should not be using the iterator concurrently with reset. The previous
166 * current iterator record is reset.
168 void lib_ring_buffer_reset(struct lttng_ust_ring_buffer
*buf
,
169 struct lttng_ust_shm_handle
*handle
)
171 struct lttng_ust_ring_buffer_channel
*chan
;
172 const struct lttng_ust_ring_buffer_config
*config
;
175 chan
= shmp(handle
, buf
->backend
.chan
);
178 config
= &chan
->backend
.config
;
180 * Reset iterator first. It will put the subbuffer if it currently holds
183 v_set(config
, &buf
->offset
, 0);
184 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
185 struct commit_counters_hot
*cc_hot
;
186 struct commit_counters_cold
*cc_cold
;
189 cc_hot
= shmp_index(handle
, buf
->commit_hot
, i
);
192 cc_cold
= shmp_index(handle
, buf
->commit_cold
, i
);
195 ts_end
= shmp_index(handle
, buf
->ts_end
, i
);
198 v_set(config
, &cc_hot
->cc
, 0);
199 v_set(config
, &cc_hot
->seq
, 0);
200 v_set(config
, &cc_cold
->cc_sb
, 0);
203 uatomic_set(&buf
->consumed
, 0);
204 uatomic_set(&buf
->record_disabled
, 0);
205 v_set(config
, &buf
->last_tsc
, 0);
206 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
207 /* Don't reset number of active readers */
208 v_set(config
, &buf
->records_lost_full
, 0);
209 v_set(config
, &buf
->records_lost_wrap
, 0);
210 v_set(config
, &buf
->records_lost_big
, 0);
211 v_set(config
, &buf
->records_count
, 0);
212 v_set(config
, &buf
->records_overrun
, 0);
217 * channel_reset - Reset channel to initial values.
220 * Effectively empty the channel. Should be called when the channel is not used
221 * for writing. The channel can be opened for reading, but the reader should not
222 * be using the iterator concurrently with reset. The previous current iterator
225 void channel_reset(struct lttng_ust_ring_buffer_channel
*chan
)
228 * Reset iterators first. Will put the subbuffer if held for reading.
230 uatomic_set(&chan
->record_disabled
, 0);
231 /* Don't reset commit_count_mask, still valid */
232 channel_backend_reset(&chan
->backend
);
233 /* Don't reset switch/read timer interval */
234 /* Don't reset notifiers and notifier enable bits */
235 /* Don't reset reader reference count */
239 void init_crash_abi(const struct lttng_ust_ring_buffer_config
*config
,
240 struct lttng_crash_abi
*crash_abi
,
241 struct lttng_ust_ring_buffer
*buf
,
242 struct channel_backend
*chanb
,
243 struct shm_object
*shmobj
,
244 struct lttng_ust_shm_handle
*handle
)
248 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
249 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
250 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
251 crash_abi
->endian
= RB_CRASH_ENDIAN
;
252 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
253 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
254 crash_abi
->word_size
= sizeof(unsigned long);
255 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
257 /* Offset of fields */
258 crash_abi
->offset
.prod_offset
=
259 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
260 crash_abi
->offset
.consumed_offset
=
261 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
262 crash_abi
->offset
.commit_hot_array
=
263 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
264 crash_abi
->offset
.commit_hot_seq
=
265 offsetof(struct commit_counters_hot
, seq
);
266 crash_abi
->offset
.buf_wsb_array
=
267 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
268 crash_abi
->offset
.buf_wsb_id
=
269 offsetof(struct lttng_ust_ring_buffer_backend_subbuffer
, id
);
270 crash_abi
->offset
.sb_array
=
271 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
272 crash_abi
->offset
.sb_array_shmp_offset
=
273 offsetof(struct lttng_ust_ring_buffer_backend_pages_shmp
,
275 crash_abi
->offset
.sb_backend_p_offset
=
276 offsetof(struct lttng_ust_ring_buffer_backend_pages
,
280 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
281 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
282 crash_abi
->length
.commit_hot_seq
=
283 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
284 crash_abi
->length
.buf_wsb_id
=
285 sizeof(((struct lttng_ust_ring_buffer_backend_subbuffer
*) NULL
)->id
);
286 crash_abi
->length
.sb_array_shmp_offset
=
287 sizeof(((struct lttng_ust_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
288 crash_abi
->length
.sb_backend_p_offset
=
289 sizeof(((struct lttng_ust_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
292 crash_abi
->stride
.commit_hot_array
=
293 sizeof(struct commit_counters_hot
);
294 crash_abi
->stride
.buf_wsb_array
=
295 sizeof(struct lttng_ust_ring_buffer_backend_subbuffer
);
296 crash_abi
->stride
.sb_array
=
297 sizeof(struct lttng_ust_ring_buffer_backend_pages_shmp
);
299 /* Buffer constants */
300 crash_abi
->buf_size
= chanb
->buf_size
;
301 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
302 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
303 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
305 if (config
->cb
.content_size_field
) {
306 size_t offset
, length
;
308 config
->cb
.content_size_field(config
, &offset
, &length
);
309 crash_abi
->offset
.content_size
= offset
;
310 crash_abi
->length
.content_size
= length
;
312 crash_abi
->offset
.content_size
= 0;
313 crash_abi
->length
.content_size
= 0;
315 if (config
->cb
.packet_size_field
) {
316 size_t offset
, length
;
318 config
->cb
.packet_size_field(config
, &offset
, &length
);
319 crash_abi
->offset
.packet_size
= offset
;
320 crash_abi
->length
.packet_size
= length
;
322 crash_abi
->offset
.packet_size
= 0;
323 crash_abi
->length
.packet_size
= 0;
328 * Must be called under cpu hotplug protection.
330 int lib_ring_buffer_create(struct lttng_ust_ring_buffer
*buf
,
331 struct channel_backend
*chanb
, int cpu
,
332 struct lttng_ust_shm_handle
*handle
,
333 struct shm_object
*shmobj
)
335 const struct lttng_ust_ring_buffer_config
*config
= &chanb
->config
;
336 struct lttng_ust_ring_buffer_channel
*chan
= caa_container_of(chanb
,
337 struct lttng_ust_ring_buffer_channel
, backend
);
338 struct lttng_ust_ring_buffer_backend_subbuffer
*wsb
;
339 struct lttng_ust_ring_buffer_channel
*shmp_chan
;
340 struct commit_counters_hot
*cc_hot
;
341 void *priv
= channel_get_private_config(chan
);
342 size_t subbuf_header_size
;
346 /* Test for cpu hotplug */
347 if (buf
->backend
.allocated
)
350 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
351 set_shmp(buf
->commit_hot
,
353 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
354 if (!shmp(handle
, buf
->commit_hot
)) {
358 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
359 set_shmp(buf
->commit_cold
,
361 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
362 if (!shmp(handle
, buf
->commit_cold
)) {
367 align_shm(shmobj
, __alignof__(uint64_t));
368 set_shmp(buf
->ts_end
,
370 sizeof(uint64_t) * chan
->backend
.num_subbuf
));
371 if (!shmp(handle
, buf
->ts_end
)) {
373 goto free_commit_cold
;
377 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
378 cpu
, handle
, shmobj
);
384 * Write the subbuffer header for first subbuffer so we know the total
385 * duration of data gathering.
387 subbuf_header_size
= config
->cb
.subbuffer_header_size();
388 v_set(config
, &buf
->offset
, subbuf_header_size
);
389 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
394 subbuffer_id_clear_noref(config
, &wsb
->id
);
395 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
400 tsc
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
401 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
402 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
407 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
408 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
410 if (config
->cb
.buffer_create
) {
411 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
416 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
418 buf
->backend
.allocated
= 1;
423 /* ts_end will be freed by shm teardown */
425 /* commit_cold will be freed by shm teardown */
427 /* commit_hot will be freed by shm teardown */
433 void lib_ring_buffer_channel_switch_timer(int sig
__attribute__((unused
)),
434 siginfo_t
*si
, void *uc
__attribute__((unused
)))
436 const struct lttng_ust_ring_buffer_config
*config
;
437 struct lttng_ust_shm_handle
*handle
;
438 struct lttng_ust_ring_buffer_channel
*chan
;
441 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
443 chan
= si
->si_value
.sival_ptr
;
444 handle
= chan
->handle
;
445 config
= &chan
->backend
.config
;
447 DBG("Switch timer for channel %p\n", chan
);
450 * Only flush buffers periodically if readers are active.
452 pthread_mutex_lock(&wakeup_fd_mutex
);
453 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
454 for_each_possible_cpu(cpu
) {
455 struct lttng_ust_ring_buffer
*buf
=
456 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
460 if (uatomic_read(&buf
->active_readers
))
461 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
465 struct lttng_ust_ring_buffer
*buf
=
466 shmp(handle
, chan
->backend
.buf
[0].shmp
);
470 if (uatomic_read(&buf
->active_readers
))
471 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
475 pthread_mutex_unlock(&wakeup_fd_mutex
);
480 int lib_ring_buffer_poll_deliver(const struct lttng_ust_ring_buffer_config
*config
,
481 struct lttng_ust_ring_buffer
*buf
,
482 struct lttng_ust_ring_buffer_channel
*chan
,
483 struct lttng_ust_shm_handle
*handle
)
485 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
486 struct commit_counters_cold
*cc_cold
;
488 consumed_old
= uatomic_read(&buf
->consumed
);
489 consumed_idx
= subbuf_index(consumed_old
, chan
);
490 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
493 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
495 * No memory barrier here, since we are only interested
496 * in a statistically correct polling result. The next poll will
497 * get the data is we are racing. The mb() that ensures correct
498 * memory order is in get_subbuf.
500 write_offset
= v_read(config
, &buf
->offset
);
503 * Check that the subbuffer we are trying to consume has been
504 * already fully committed.
507 if (((commit_count
- chan
->backend
.subbuf_size
)
508 & chan
->commit_count_mask
)
509 - (buf_trunc(consumed_old
, chan
)
510 >> chan
->backend
.num_subbuf_order
)
515 * Check that we are not about to read the same subbuffer in
516 * which the writer head is.
518 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
526 void lib_ring_buffer_wakeup(struct lttng_ust_ring_buffer
*buf
,
527 struct lttng_ust_shm_handle
*handle
)
529 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
530 sigset_t sigpipe_set
, pending_set
, old_set
;
531 int ret
, sigpipe_was_pending
= 0;
537 * Wake-up the other end by writing a null byte in the pipe
538 * (non-blocking). Important note: Because writing into the
539 * pipe is non-blocking (and therefore we allow dropping wakeup
540 * data, as long as there is wakeup data present in the pipe
541 * buffer to wake up the consumer), the consumer should perform
542 * the following sequence for waiting:
543 * 1) empty the pipe (reads).
544 * 2) check if there is data in the buffer.
545 * 3) wait on the pipe (poll).
547 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
548 * that might be already pending. If a bogus SIGPIPE is sent to
549 * the entire process concurrently by a malicious user, it may
550 * be simply discarded.
552 ret
= sigemptyset(&pending_set
);
555 * sigpending returns the mask of signals that are _both_
556 * blocked for the thread _and_ pending for either the thread or
557 * the entire process.
559 ret
= sigpending(&pending_set
);
561 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
563 * If sigpipe was pending, it means it was already blocked, so
564 * no need to block it.
566 if (!sigpipe_was_pending
) {
567 ret
= sigemptyset(&sigpipe_set
);
569 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
571 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
575 ret
= write(wakeup_fd
, "", 1);
576 } while (ret
== -1L && errno
== EINTR
);
577 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
578 struct timespec timeout
= { 0, 0 };
580 ret
= sigtimedwait(&sigpipe_set
, NULL
,
582 } while (ret
== -1L && errno
== EINTR
);
584 if (!sigpipe_was_pending
) {
585 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
591 void lib_ring_buffer_channel_do_read(struct lttng_ust_ring_buffer_channel
*chan
)
593 const struct lttng_ust_ring_buffer_config
*config
;
594 struct lttng_ust_shm_handle
*handle
;
597 handle
= chan
->handle
;
598 config
= &chan
->backend
.config
;
601 * Only flush buffers periodically if readers are active.
603 pthread_mutex_lock(&wakeup_fd_mutex
);
604 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
605 for_each_possible_cpu(cpu
) {
606 struct lttng_ust_ring_buffer
*buf
=
607 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
611 if (uatomic_read(&buf
->active_readers
)
612 && lib_ring_buffer_poll_deliver(config
, buf
,
614 lib_ring_buffer_wakeup(buf
, handle
);
618 struct lttng_ust_ring_buffer
*buf
=
619 shmp(handle
, chan
->backend
.buf
[0].shmp
);
623 if (uatomic_read(&buf
->active_readers
)
624 && lib_ring_buffer_poll_deliver(config
, buf
,
626 lib_ring_buffer_wakeup(buf
, handle
);
630 pthread_mutex_unlock(&wakeup_fd_mutex
);
634 void lib_ring_buffer_channel_read_timer(int sig
__attribute__((unused
)),
635 siginfo_t
*si
, void *uc
__attribute__((unused
)))
637 struct lttng_ust_ring_buffer_channel
*chan
;
639 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
640 chan
= si
->si_value
.sival_ptr
;
641 DBG("Read timer for channel %p\n", chan
);
642 lib_ring_buffer_channel_do_read(chan
);
647 void rb_setmask(sigset_t
*mask
)
651 ret
= sigemptyset(mask
);
653 PERROR("sigemptyset");
655 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
659 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
663 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
670 void *sig_thread(void *arg
__attribute__((unused
)))
676 /* Only self thread will receive signal mask. */
678 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
681 signr
= sigwaitinfo(&mask
, &info
);
684 PERROR("sigwaitinfo");
687 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
688 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
690 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
691 lib_ring_buffer_channel_read_timer(info
.si_signo
,
693 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
695 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
698 ERR("Unexptected signal %d\n", info
.si_signo
);
705 * Ensure only a single thread listens on the timer signal.
708 void lib_ring_buffer_setup_timer_thread(void)
713 pthread_mutex_lock(&timer_signal
.lock
);
714 if (timer_signal
.setup_done
)
717 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
720 PERROR("pthread_create");
722 ret
= pthread_detach(thread
);
725 PERROR("pthread_detach");
727 timer_signal
.setup_done
= 1;
729 pthread_mutex_unlock(&timer_signal
.lock
);
733 * Wait for signal-handling thread quiescent state.
736 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
738 sigset_t pending_set
;
742 * We need to be the only thread interacting with the thread
743 * that manages signals for teardown synchronization.
745 pthread_mutex_lock(&timer_signal
.lock
);
748 * Ensure we don't have any signal queued for this channel.
751 ret
= sigemptyset(&pending_set
);
753 PERROR("sigemptyset");
755 ret
= sigpending(&pending_set
);
757 PERROR("sigpending");
759 if (!sigismember(&pending_set
, signr
))
765 * From this point, no new signal handler will be fired that
766 * would try to access "chan". However, we still need to wait
767 * for any currently executing handler to complete.
770 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
774 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
777 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
779 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
783 pthread_mutex_unlock(&timer_signal
.lock
);
787 void lib_ring_buffer_channel_switch_timer_start(struct lttng_ust_ring_buffer_channel
*chan
)
790 struct itimerspec its
;
793 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
796 chan
->switch_timer_enabled
= 1;
798 lib_ring_buffer_setup_timer_thread();
800 memset(&sev
, 0, sizeof(sev
));
801 sev
.sigev_notify
= SIGEV_SIGNAL
;
802 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
803 sev
.sigev_value
.sival_ptr
= chan
;
804 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
806 PERROR("timer_create");
809 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
810 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
811 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
812 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
814 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
816 PERROR("timer_settime");
821 void lib_ring_buffer_channel_switch_timer_stop(struct lttng_ust_ring_buffer_channel
*chan
)
825 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
828 ret
= timer_delete(chan
->switch_timer
);
830 PERROR("timer_delete");
833 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
835 chan
->switch_timer
= 0;
836 chan
->switch_timer_enabled
= 0;
840 void lib_ring_buffer_channel_read_timer_start(struct lttng_ust_ring_buffer_channel
*chan
)
842 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
844 struct itimerspec its
;
847 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
848 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
851 chan
->read_timer_enabled
= 1;
853 lib_ring_buffer_setup_timer_thread();
855 sev
.sigev_notify
= SIGEV_SIGNAL
;
856 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
857 sev
.sigev_value
.sival_ptr
= chan
;
858 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
860 PERROR("timer_create");
863 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
864 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
865 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
866 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
868 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
870 PERROR("timer_settime");
875 void lib_ring_buffer_channel_read_timer_stop(struct lttng_ust_ring_buffer_channel
*chan
)
877 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
880 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
881 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
884 ret
= timer_delete(chan
->read_timer
);
886 PERROR("timer_delete");
890 * do one more check to catch data that has been written in the last
893 lib_ring_buffer_channel_do_read(chan
);
895 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
897 chan
->read_timer
= 0;
898 chan
->read_timer_enabled
= 0;
901 static void channel_unregister_notifiers(struct lttng_ust_ring_buffer_channel
*chan
,
902 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
904 lib_ring_buffer_channel_switch_timer_stop(chan
);
905 lib_ring_buffer_channel_read_timer_stop(chan
);
908 static void channel_print_errors(struct lttng_ust_ring_buffer_channel
*chan
,
909 struct lttng_ust_shm_handle
*handle
)
911 const struct lttng_ust_ring_buffer_config
*config
=
912 &chan
->backend
.config
;
915 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
916 for_each_possible_cpu(cpu
) {
917 struct lttng_ust_ring_buffer
*buf
=
918 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
920 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
923 struct lttng_ust_ring_buffer
*buf
=
924 shmp(handle
, chan
->backend
.buf
[0].shmp
);
927 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
931 static void channel_free(struct lttng_ust_ring_buffer_channel
*chan
,
932 struct lttng_ust_shm_handle
*handle
,
935 channel_backend_free(&chan
->backend
, handle
);
936 /* chan is freed by shm teardown */
937 shm_object_table_destroy(handle
->table
, consumer
);
942 * channel_create - Create channel.
943 * @config: ring buffer instance configuration
944 * @name: name of the channel
945 * @priv_data_align: alignment, in bytes, of the private data area. (config)
946 * @priv_data_size: length, in bytes, of the private data area. (config)
947 * @priv_data_init: initialization data for private data. (config)
948 * @priv: local private data (memory owner by caller)
949 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
950 * address mapping. It is used only by RING_BUFFER_STATIC
951 * configuration. It can be set to NULL for other backends.
952 * @subbuf_size: subbuffer size
953 * @num_subbuf: number of subbuffers
954 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
955 * padding to let readers get those sub-buffers.
956 * Used for live streaming.
957 * @read_timer_interval: Time interval (in us) to wake up pending readers.
958 * @stream_fds: array of stream file descriptors.
959 * @nr_stream_fds: number of file descriptors in array.
962 * Returns NULL on failure.
964 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_ring_buffer_config
*config
,
966 size_t priv_data_align
,
967 size_t priv_data_size
,
968 void *priv_data_init
,
970 void *buf_addr
__attribute__((unused
)), size_t subbuf_size
,
971 size_t num_subbuf
, unsigned int switch_timer_interval
,
972 unsigned int read_timer_interval
,
973 const int *stream_fds
, int nr_stream_fds
,
974 int64_t blocking_timeout
)
977 size_t shmsize
, chansize
;
978 struct lttng_ust_ring_buffer_channel
*chan
;
979 struct lttng_ust_shm_handle
*handle
;
980 struct shm_object
*shmobj
;
981 unsigned int nr_streams
;
982 int64_t blocking_timeout_ms
;
984 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
985 nr_streams
= num_possible_cpus();
989 if (nr_stream_fds
!= nr_streams
)
992 if (blocking_timeout
< -1) {
996 if (blocking_timeout
== -1) {
997 blocking_timeout_ms
= -1;
999 blocking_timeout_ms
= blocking_timeout
/ 1000;
1000 if (blocking_timeout_ms
!= (int32_t) blocking_timeout_ms
) {
1005 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
1006 read_timer_interval
))
1009 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1013 /* Allocate table for channel + per-cpu buffers */
1014 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1016 goto error_table_alloc
;
1018 /* Calculate the shm allocation layout */
1019 shmsize
= sizeof(struct lttng_ust_ring_buffer_channel
);
1020 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_ring_buffer_shmp
));
1021 shmsize
+= sizeof(struct lttng_ust_ring_buffer_shmp
) * nr_streams
;
1023 if (priv_data_align
)
1024 shmsize
+= lttng_ust_offset_align(shmsize
, priv_data_align
);
1025 shmsize
+= priv_data_size
;
1027 /* Allocate normal memory for channel (not shared) */
1028 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
1032 /* struct lttng_ust_ring_buffer_channel is at object 0, offset 0 (hardcoded) */
1033 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
1034 assert(handle
->chan
._ref
.index
== 0);
1035 assert(handle
->chan
._ref
.offset
== 0);
1036 chan
= shmp(handle
, handle
->chan
);
1039 chan
->nr_streams
= nr_streams
;
1041 /* space for private data */
1042 if (priv_data_size
) {
1045 DECLARE_SHMP(void, priv_data_alloc
);
1047 align_shm(shmobj
, priv_data_align
);
1048 chan
->priv_data_offset
= shmobj
->allocated_len
;
1049 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1050 if (!shmp(handle
, priv_data_alloc
))
1052 priv_config
= channel_get_private_config(chan
);
1053 memcpy(priv_config
, priv_data_init
, priv_data_size
);
1055 chan
->priv_data_offset
= -1;
1058 chan
->u
.s
.blocking_timeout_ms
= (int32_t) blocking_timeout_ms
;
1060 channel_set_private(chan
, priv
);
1062 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1063 subbuf_size
, num_subbuf
, handle
,
1066 goto error_backend_init
;
1068 chan
->handle
= handle
;
1069 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1071 chan
->switch_timer_interval
= switch_timer_interval
;
1072 chan
->read_timer_interval
= read_timer_interval
;
1073 lib_ring_buffer_channel_switch_timer_start(chan
);
1074 lib_ring_buffer_channel_read_timer_start(chan
);
1080 shm_object_table_destroy(handle
->table
, 1);
1086 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1087 uint64_t memory_map_size
,
1090 struct lttng_ust_shm_handle
*handle
;
1091 struct shm_object
*object
;
1093 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1097 /* Allocate table for channel + per-cpu buffers */
1098 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1100 goto error_table_alloc
;
1101 /* Add channel object */
1102 object
= shm_object_table_append_mem(handle
->table
, data
,
1103 memory_map_size
, wakeup_fd
);
1105 goto error_table_object
;
1106 /* struct lttng_ust_ring_buffer_channel is at object 0, offset 0 (hardcoded) */
1107 handle
->chan
._ref
.index
= 0;
1108 handle
->chan
._ref
.offset
= 0;
1112 shm_object_table_destroy(handle
->table
, 0);
1118 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1119 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1120 uint64_t memory_map_size
)
1122 struct shm_object
*object
;
1124 /* Add stream object */
1125 object
= shm_object_table_append_shm(handle
->table
,
1126 shm_fd
, wakeup_fd
, stream_nr
,
1133 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1135 assert(handle
->table
);
1136 return handle
->table
->allocated_len
- 1;
1140 void channel_release(struct lttng_ust_ring_buffer_channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1143 channel_free(chan
, handle
, consumer
);
1147 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1148 * @chan: channel to destroy
1150 * Holds cpu hotplug.
1151 * Call "destroy" callback, finalize channels, decrement the channel
1152 * reference count. Note that when readers have completed data
1153 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1154 * They should release their handle at that point.
1156 void channel_destroy(struct lttng_ust_ring_buffer_channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1161 * Note: the consumer takes care of finalizing and
1162 * switching the buffers.
1164 channel_unregister_notifiers(chan
, handle
);
1166 * The consumer prints errors.
1168 channel_print_errors(chan
, handle
);
1172 * sessiond/consumer are keeping a reference on the shm file
1173 * descriptor directly. No need to refcount.
1175 channel_release(chan
, handle
, consumer
);
1179 struct lttng_ust_ring_buffer
*channel_get_ring_buffer(
1180 const struct lttng_ust_ring_buffer_config
*config
,
1181 struct lttng_ust_ring_buffer_channel
*chan
, int cpu
,
1182 struct lttng_ust_shm_handle
*handle
,
1183 int *shm_fd
, int *wait_fd
,
1185 uint64_t *memory_map_size
)
1187 struct shm_ref
*ref
;
1189 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1192 if (cpu
>= num_possible_cpus())
1195 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1196 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1197 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1198 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1199 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1201 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1204 int ring_buffer_channel_close_wait_fd(
1205 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
1206 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
1207 struct lttng_ust_shm_handle
*handle
)
1209 struct shm_ref
*ref
;
1211 ref
= &handle
->chan
._ref
;
1212 return shm_close_wait_fd(handle
, ref
);
1215 int ring_buffer_channel_close_wakeup_fd(
1216 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
1217 struct lttng_ust_ring_buffer_channel
*chan
__attribute__((unused
)),
1218 struct lttng_ust_shm_handle
*handle
)
1220 struct shm_ref
*ref
;
1222 ref
= &handle
->chan
._ref
;
1223 return shm_close_wakeup_fd(handle
, ref
);
1226 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_ring_buffer_config
*config
,
1227 struct lttng_ust_ring_buffer_channel
*chan
,
1228 struct lttng_ust_shm_handle
*handle
,
1231 struct shm_ref
*ref
;
1233 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1236 if (cpu
>= num_possible_cpus())
1239 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1240 return shm_close_wait_fd(handle
, ref
);
1243 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_ring_buffer_config
*config
,
1244 struct lttng_ust_ring_buffer_channel
*chan
,
1245 struct lttng_ust_shm_handle
*handle
,
1248 struct shm_ref
*ref
;
1251 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1254 if (cpu
>= num_possible_cpus())
1257 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1258 pthread_mutex_lock(&wakeup_fd_mutex
);
1259 ret
= shm_close_wakeup_fd(handle
, ref
);
1260 pthread_mutex_unlock(&wakeup_fd_mutex
);
1264 int lib_ring_buffer_open_read(struct lttng_ust_ring_buffer
*buf
,
1265 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
1267 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1273 void lib_ring_buffer_release_read(struct lttng_ust_ring_buffer
*buf
,
1274 struct lttng_ust_shm_handle
*handle
)
1276 struct lttng_ust_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1280 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1282 uatomic_dec(&buf
->active_readers
);
1286 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1288 * @consumed: consumed count indicating the position where to read
1289 * @produced: produced count, indicates position when to stop reading
1291 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1292 * data to read at consumed position, or 0 if the get operation succeeds.
1295 int lib_ring_buffer_snapshot(struct lttng_ust_ring_buffer
*buf
,
1296 unsigned long *consumed
, unsigned long *produced
,
1297 struct lttng_ust_shm_handle
*handle
)
1299 struct lttng_ust_ring_buffer_channel
*chan
;
1300 const struct lttng_ust_ring_buffer_config
*config
;
1301 unsigned long consumed_cur
, write_offset
;
1304 chan
= shmp(handle
, buf
->backend
.chan
);
1307 config
= &chan
->backend
.config
;
1308 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1310 * Read finalized before counters.
1313 consumed_cur
= uatomic_read(&buf
->consumed
);
1315 * No need to issue a memory barrier between consumed count read and
1316 * write offset read, because consumed count can only change
1317 * concurrently in overwrite mode, and we keep a sequence counter
1318 * identifier derived from the write offset to check we are getting
1319 * the same sub-buffer we are expecting (the sub-buffers are atomically
1320 * "tagged" upon writes, tags are checked upon read).
1322 write_offset
= v_read(config
, &buf
->offset
);
1325 * Check that we are not about to read the same subbuffer in
1326 * which the writer head is.
1328 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1332 *consumed
= consumed_cur
;
1333 *produced
= subbuf_trunc(write_offset
, chan
);
1339 * The memory barriers __wait_event()/wake_up_interruptible() take care
1340 * of "raw_spin_is_locked" memory ordering.
1349 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1350 * are saved regardless of whether the consumed and produced positions are
1351 * in the same subbuffer.
1353 * @consumed: consumed byte count indicating the last position read
1354 * @produced: produced byte count indicating the last position written
1356 * This function is meant to provide information on the exact producer and
1357 * consumer positions without regard for the "snapshot" feature.
1359 int lib_ring_buffer_snapshot_sample_positions(
1360 struct lttng_ust_ring_buffer
*buf
,
1361 unsigned long *consumed
, unsigned long *produced
,
1362 struct lttng_ust_shm_handle
*handle
)
1364 struct lttng_ust_ring_buffer_channel
*chan
;
1365 const struct lttng_ust_ring_buffer_config
*config
;
1367 chan
= shmp(handle
, buf
->backend
.chan
);
1370 config
= &chan
->backend
.config
;
1372 *consumed
= uatomic_read(&buf
->consumed
);
1374 * No need to issue a memory barrier between consumed count read and
1375 * write offset read, because consumed count can only change
1376 * concurrently in overwrite mode, and we keep a sequence counter
1377 * identifier derived from the write offset to check we are getting
1378 * the same sub-buffer we are expecting (the sub-buffers are atomically
1379 * "tagged" upon writes, tags are checked upon read).
1381 *produced
= v_read(config
, &buf
->offset
);
1386 * lib_ring_buffer_move_consumer - move consumed counter forward
1388 * @consumed_new: new consumed count value
1390 void lib_ring_buffer_move_consumer(struct lttng_ust_ring_buffer
*buf
,
1391 unsigned long consumed_new
,
1392 struct lttng_ust_shm_handle
*handle
)
1394 struct lttng_ust_ring_buffer_backend
*bufb
= &buf
->backend
;
1395 struct lttng_ust_ring_buffer_channel
*chan
;
1396 unsigned long consumed
;
1398 chan
= shmp(handle
, bufb
->chan
);
1401 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1404 * Only push the consumed value forward.
1405 * If the consumed cmpxchg fails, this is because we have been pushed by
1406 * the writer in flight recorder mode.
1408 consumed
= uatomic_read(&buf
->consumed
);
1409 while ((long) consumed
- (long) consumed_new
< 0)
1410 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1415 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1417 * @consumed: consumed count indicating the position where to read
1419 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1420 * data to read at consumed position, or 0 if the get operation succeeds.
1422 int lib_ring_buffer_get_subbuf(struct lttng_ust_ring_buffer
*buf
,
1423 unsigned long consumed
,
1424 struct lttng_ust_shm_handle
*handle
)
1426 struct lttng_ust_ring_buffer_channel
*chan
;
1427 const struct lttng_ust_ring_buffer_config
*config
;
1428 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1429 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1430 struct commit_counters_cold
*cc_cold
;
1432 chan
= shmp(handle
, buf
->backend
.chan
);
1435 config
= &chan
->backend
.config
;
1437 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1439 * Read finalized before counters.
1442 consumed_cur
= uatomic_read(&buf
->consumed
);
1443 consumed_idx
= subbuf_index(consumed
, chan
);
1444 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1447 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1449 * Make sure we read the commit count before reading the buffer
1450 * data and the write offset. Correct consumed offset ordering
1451 * wrt commit count is insured by the use of cmpxchg to update
1452 * the consumed offset.
1455 * Local rmb to match the remote wmb to read the commit count
1456 * before the buffer data and the write offset.
1460 write_offset
= v_read(config
, &buf
->offset
);
1463 * Check that the buffer we are getting is after or at consumed_cur
1466 if ((long) subbuf_trunc(consumed
, chan
)
1467 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1471 * Check that the subbuffer we are trying to consume has been
1472 * already fully committed. There are a few causes that can make
1473 * this unavailability situation occur:
1475 * Temporary (short-term) situation:
1476 * - Application is running on a different CPU, between reserve
1477 * and commit ring buffer operations,
1478 * - Application is preempted between reserve and commit ring
1479 * buffer operations,
1481 * Long-term situation:
1482 * - Application is stopped (SIGSTOP) between reserve and commit
1483 * ring buffer operations. Could eventually be resumed by
1485 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1486 * reserve and commit ring buffer operation.
1488 * From a consumer perspective, handling short-term
1489 * unavailability situations is performed by retrying a few
1490 * times after a delay. Handling long-term unavailability
1491 * situations is handled by failing to get the sub-buffer.
1493 * In all of those situations, if the application is taking a
1494 * long time to perform its commit after ring buffer space
1495 * reservation, we can end up in a situation where the producer
1496 * will fill the ring buffer and try to write into the same
1497 * sub-buffer again (which has a missing commit). This is
1498 * handled by the producer in the sub-buffer switch handling
1499 * code of the reserve routine by detecting unbalanced
1500 * reserve/commit counters and discarding all further events
1501 * until the situation is resolved in those situations. Two
1502 * scenarios can occur:
1504 * 1) The application causing the reserve/commit counters to be
1505 * unbalanced has been terminated. In this situation, all
1506 * further events will be discarded in the buffers, and no
1507 * further buffer data will be readable by the consumer
1508 * daemon. Tearing down the UST tracing session and starting
1509 * anew is a work-around for those situations. Note that this
1510 * only affects per-UID tracing. In per-PID tracing, the
1511 * application vanishes with the termination, and therefore
1512 * no more data needs to be written to the buffers.
1513 * 2) The application causing the unbalance has been delayed for
1514 * a long time, but will eventually try to increment the
1515 * commit counter after eventually writing to the sub-buffer.
1516 * This situation can cause events to be discarded until the
1517 * application resumes its operations.
1519 if (((commit_count
- chan
->backend
.subbuf_size
)
1520 & chan
->commit_count_mask
)
1521 - (buf_trunc(consumed
, chan
)
1522 >> chan
->backend
.num_subbuf_order
)
1524 if (nr_retry
-- > 0) {
1525 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1526 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1534 * Check that we are not about to read the same subbuffer in
1535 * which the writer head is.
1537 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1542 * Failure to get the subbuffer causes a busy-loop retry without going
1543 * to a wait queue. These are caused by short-lived race windows where
1544 * the writer is getting access to a subbuffer we were trying to get
1545 * access to. Also checks that the "consumed" buffer count we are
1546 * looking for matches the one contained in the subbuffer id.
1548 * The short-lived race window described here can be affected by
1549 * application signals and preemption, thus requiring to bound
1550 * the loop to a maximum number of retry.
1552 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1553 consumed_idx
, buf_trunc_val(consumed
, chan
),
1556 if (nr_retry
-- > 0) {
1557 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1558 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1564 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1566 buf
->get_subbuf_consumed
= consumed
;
1567 buf
->get_subbuf
= 1;
1573 * The memory barriers __wait_event()/wake_up_interruptible() take care
1574 * of "raw_spin_is_locked" memory ordering.
1583 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1586 void lib_ring_buffer_put_subbuf(struct lttng_ust_ring_buffer
*buf
,
1587 struct lttng_ust_shm_handle
*handle
)
1589 struct lttng_ust_ring_buffer_backend
*bufb
= &buf
->backend
;
1590 struct lttng_ust_ring_buffer_channel
*chan
;
1591 const struct lttng_ust_ring_buffer_config
*config
;
1592 unsigned long sb_bindex
, consumed_idx
, consumed
;
1593 struct lttng_ust_ring_buffer_backend_pages_shmp
*rpages
;
1594 struct lttng_ust_ring_buffer_backend_pages
*backend_pages
;
1596 chan
= shmp(handle
, bufb
->chan
);
1599 config
= &chan
->backend
.config
;
1600 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1602 if (!buf
->get_subbuf
) {
1604 * Reader puts a subbuffer it did not get.
1606 CHAN_WARN_ON(chan
, 1);
1609 consumed
= buf
->get_subbuf_consumed
;
1610 buf
->get_subbuf
= 0;
1613 * Clear the records_unread counter. (overruns counter)
1614 * Can still be non-zero if a file reader simply grabbed the data
1615 * without using iterators.
1616 * Can be below zero if an iterator is used on a snapshot more than
1619 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1620 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1623 backend_pages
= shmp(handle
, rpages
->shmp
);
1626 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1627 &bufb
->records_read
);
1628 v_set(config
, &backend_pages
->records_unread
, 0);
1629 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1630 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1631 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1634 * Exchange the reader subbuffer with the one we put in its place in the
1635 * writer subbuffer table. Expect the original consumed count. If
1636 * update_read_sb_index fails, this is because the writer updated the
1637 * subbuffer concurrently. We should therefore keep the subbuffer we
1638 * currently have: it has become invalid to try reading this sub-buffer
1639 * consumed count value anyway.
1641 consumed_idx
= subbuf_index(consumed
, chan
);
1642 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1643 consumed_idx
, buf_trunc_val(consumed
, chan
),
1646 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1647 * if the writer concurrently updated it.
1652 * cons_offset is an iterator on all subbuffer offsets between the reader
1653 * position and the writer position. (inclusive)
1656 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_ring_buffer
*buf
,
1657 struct lttng_ust_ring_buffer_channel
*chan
,
1658 unsigned long cons_offset
,
1660 struct lttng_ust_shm_handle
*handle
)
1662 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1663 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1664 struct commit_counters_hot
*cc_hot
;
1665 struct commit_counters_cold
*cc_cold
;
1667 cons_idx
= subbuf_index(cons_offset
, chan
);
1668 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1671 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1674 commit_count
= v_read(config
, &cc_hot
->cc
);
1675 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1677 if (subbuf_offset(commit_count
, chan
) != 0)
1678 DBG("ring buffer %s, cpu %d: "
1679 "commit count in subbuffer %lu,\n"
1680 "expecting multiples of %lu bytes\n"
1681 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1682 chan
->backend
.name
, cpu
, cons_idx
,
1683 chan
->backend
.subbuf_size
,
1684 commit_count
, commit_count_sb
);
1686 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1687 chan
->backend
.name
, cpu
, commit_count
);
1691 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_ring_buffer
*buf
,
1692 struct lttng_ust_ring_buffer_channel
*chan
,
1693 int cpu
, struct lttng_ust_shm_handle
*handle
)
1695 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1696 unsigned long write_offset
, cons_offset
;
1699 * No need to order commit_count, write_offset and cons_offset reads
1700 * because we execute at teardown when no more writer nor reader
1701 * references are left.
1703 write_offset
= v_read(config
, &buf
->offset
);
1704 cons_offset
= uatomic_read(&buf
->consumed
);
1705 if (write_offset
!= cons_offset
)
1706 DBG("ring buffer %s, cpu %d: "
1707 "non-consumed data\n"
1708 " [ %lu bytes written, %lu bytes read ]\n",
1709 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1711 for (cons_offset
= uatomic_read(&buf
->consumed
);
1712 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1715 cons_offset
= subbuf_align(cons_offset
, chan
))
1716 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1721 void lib_ring_buffer_print_errors(struct lttng_ust_ring_buffer_channel
*chan
,
1722 struct lttng_ust_ring_buffer
*buf
, int cpu
,
1723 struct lttng_ust_shm_handle
*handle
)
1725 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1727 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1728 DBG("ring buffer %s: %lu records written, "
1729 "%lu records overrun\n",
1731 v_read(config
, &buf
->records_count
),
1732 v_read(config
, &buf
->records_overrun
));
1734 DBG("ring buffer %s, cpu %d: %lu records written, "
1735 "%lu records overrun\n",
1736 chan
->backend
.name
, cpu
,
1737 v_read(config
, &buf
->records_count
),
1738 v_read(config
, &buf
->records_overrun
));
1740 if (v_read(config
, &buf
->records_lost_full
)
1741 || v_read(config
, &buf
->records_lost_wrap
)
1742 || v_read(config
, &buf
->records_lost_big
))
1743 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1744 " [ %lu buffer full, %lu nest buffer wrap-around, "
1745 "%lu event too big ]\n",
1746 chan
->backend
.name
, cpu
,
1747 v_read(config
, &buf
->records_lost_full
),
1748 v_read(config
, &buf
->records_lost_wrap
),
1749 v_read(config
, &buf
->records_lost_big
));
1751 lib_ring_buffer_print_buffer_errors(buf
, chan
, cpu
, handle
);
1755 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1757 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1758 * active or at buffer finalization (destroy).
1761 void lib_ring_buffer_switch_old_start(struct lttng_ust_ring_buffer
*buf
,
1762 struct lttng_ust_ring_buffer_channel
*chan
,
1763 struct switch_offsets
*offsets
,
1765 struct lttng_ust_shm_handle
*handle
)
1767 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1768 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1769 unsigned long commit_count
;
1770 struct commit_counters_hot
*cc_hot
;
1772 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1775 * Order all writes to buffer before the commit count update that will
1776 * determine that the subbuffer is full.
1779 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1782 v_add(config
, config
->cb
.subbuffer_header_size(),
1784 commit_count
= v_read(config
, &cc_hot
->cc
);
1785 /* Check if the written buffer has to be delivered */
1786 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1787 commit_count
, oldidx
, handle
, tsc
);
1788 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1789 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1790 commit_count
, handle
, cc_hot
);
1794 * lib_ring_buffer_switch_old_end: switch old subbuffer
1796 * Note : offset_old should never be 0 here. It is ok, because we never perform
1797 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1798 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1802 void lib_ring_buffer_switch_old_end(struct lttng_ust_ring_buffer
*buf
,
1803 struct lttng_ust_ring_buffer_channel
*chan
,
1804 struct switch_offsets
*offsets
,
1806 struct lttng_ust_shm_handle
*handle
)
1808 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1809 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1810 unsigned long commit_count
, padding_size
, data_size
;
1811 struct commit_counters_hot
*cc_hot
;
1814 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1815 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1816 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1819 ts_end
= shmp_index(handle
, buf
->ts_end
, oldidx
);
1823 * This is the last space reservation in that sub-buffer before
1824 * it gets delivered. This provides exclusive access to write to
1825 * this sub-buffer's ts_end. There are also no concurrent
1826 * readers of that ts_end because delivery of that sub-buffer is
1827 * postponed until the commit counter is incremented for the
1828 * current space reservation.
1833 * Order all writes to buffer and store to ts_end before the commit
1834 * count update that will determine that the subbuffer is full.
1837 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1840 v_add(config
, padding_size
, &cc_hot
->cc
);
1841 commit_count
= v_read(config
, &cc_hot
->cc
);
1842 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1843 commit_count
, oldidx
, handle
, tsc
);
1844 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1845 offsets
->old
+ padding_size
, commit_count
, handle
,
1850 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1852 * This code can be executed unordered : writers may already have written to the
1853 * sub-buffer before this code gets executed, caution. The commit makes sure
1854 * that this code is executed before the deliver of this sub-buffer.
1857 void lib_ring_buffer_switch_new_start(struct lttng_ust_ring_buffer
*buf
,
1858 struct lttng_ust_ring_buffer_channel
*chan
,
1859 struct switch_offsets
*offsets
,
1861 struct lttng_ust_shm_handle
*handle
)
1863 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1864 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1865 unsigned long commit_count
;
1866 struct commit_counters_hot
*cc_hot
;
1868 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1871 * Order all writes to buffer before the commit count update that will
1872 * determine that the subbuffer is full.
1875 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1878 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1879 commit_count
= v_read(config
, &cc_hot
->cc
);
1880 /* Check if the written buffer has to be delivered */
1881 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1882 commit_count
, beginidx
, handle
, tsc
);
1883 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1884 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1885 commit_count
, handle
, cc_hot
);
1889 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1891 * Calls subbuffer_set_data_size() to set the data size of the current
1892 * sub-buffer. We do not need to perform check_deliver nor commit here,
1893 * since this task will be done by the "commit" of the event for which
1894 * we are currently doing the space reservation.
1897 void lib_ring_buffer_switch_new_end(struct lttng_ust_ring_buffer
*buf
,
1898 struct lttng_ust_ring_buffer_channel
*chan
,
1899 struct switch_offsets
*offsets
,
1901 struct lttng_ust_shm_handle
*handle
)
1903 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1904 unsigned long endidx
, data_size
;
1907 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1908 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1909 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1911 ts_end
= shmp_index(handle
, buf
->ts_end
, endidx
);
1915 * This is the last space reservation in that sub-buffer before
1916 * it gets delivered. This provides exclusive access to write to
1917 * this sub-buffer's ts_end. There are also no concurrent
1918 * readers of that ts_end because delivery of that sub-buffer is
1919 * postponed until the commit counter is incremented for the
1920 * current space reservation.
1928 * !0 if execution must be aborted.
1931 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1932 struct lttng_ust_ring_buffer
*buf
,
1933 struct lttng_ust_ring_buffer_channel
*chan
,
1934 struct switch_offsets
*offsets
,
1936 struct lttng_ust_shm_handle
*handle
)
1938 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
1939 unsigned long off
, reserve_commit_diff
;
1941 offsets
->begin
= v_read(config
, &buf
->offset
);
1942 offsets
->old
= offsets
->begin
;
1943 offsets
->switch_old_start
= 0;
1944 off
= subbuf_offset(offsets
->begin
, chan
);
1946 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1949 * Ensure we flush the header of an empty subbuffer when doing the
1950 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1951 * total data gathering duration even if there were no records saved
1952 * after the last buffer switch.
1953 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1954 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1955 * subbuffer header as appropriate.
1956 * The next record that reserves space will be responsible for
1957 * populating the following subbuffer header. We choose not to populate
1958 * the next subbuffer header here because we want to be able to use
1959 * SWITCH_ACTIVE for periodical buffer flush, which must
1960 * guarantee that all the buffer content (records and header
1961 * timestamps) are visible to the reader. This is required for
1962 * quiescence guarantees for the fusion merge.
1964 if (mode
!= SWITCH_FLUSH
&& !off
)
1965 return -1; /* we do not have to switch : buffer is empty */
1967 if (caa_unlikely(off
== 0)) {
1968 unsigned long sb_index
, commit_count
;
1969 struct commit_counters_cold
*cc_cold
;
1972 * We are performing a SWITCH_FLUSH. There may be concurrent
1973 * writes into the buffer if e.g. invoked while performing a
1974 * snapshot on an active trace.
1976 * If the client does not save any header information
1977 * (sub-buffer header size == 0), don't switch empty subbuffer
1978 * on finalize, because it is invalid to deliver a completely
1981 if (!config
->cb
.subbuffer_header_size())
1984 /* Test new buffer integrity */
1985 sb_index
= subbuf_index(offsets
->begin
, chan
);
1986 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1989 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1990 reserve_commit_diff
=
1991 (buf_trunc(offsets
->begin
, chan
)
1992 >> chan
->backend
.num_subbuf_order
)
1993 - (commit_count
& chan
->commit_count_mask
);
1994 if (caa_likely(reserve_commit_diff
== 0)) {
1995 /* Next subbuffer not being written to. */
1996 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1997 subbuf_trunc(offsets
->begin
, chan
)
1998 - subbuf_trunc((unsigned long)
1999 uatomic_read(&buf
->consumed
), chan
)
2000 >= chan
->backend
.buf_size
)) {
2002 * We do not overwrite non consumed buffers
2003 * and we are full : don't switch.
2008 * Next subbuffer not being written to, and we
2009 * are either in overwrite mode or the buffer is
2010 * not full. It's safe to write in this new
2016 * Next subbuffer reserve offset does not match the
2017 * commit offset. Don't perform switch in
2018 * producer-consumer and overwrite mode. Caused by
2019 * either a writer OOPS or too many nested writes over a
2020 * reserve/commit pair.
2026 * Need to write the subbuffer start header on finalize.
2028 offsets
->switch_old_start
= 1;
2030 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2031 /* Note: old points to the next subbuf at offset 0 */
2032 offsets
->end
= offsets
->begin
;
2037 * Force a sub-buffer switch. This operation is completely reentrant : can be
2038 * called while tracing is active with absolutely no lock held.
2040 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2041 * some atomic operations, this function must be called from the CPU
2042 * which owns the buffer for a ACTIVE flush. However, for
2043 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2046 void lib_ring_buffer_switch_slow(struct lttng_ust_ring_buffer
*buf
, enum switch_mode mode
,
2047 struct lttng_ust_shm_handle
*handle
)
2049 struct lttng_ust_ring_buffer_channel
*chan
;
2050 const struct lttng_ust_ring_buffer_config
*config
;
2051 struct switch_offsets offsets
;
2052 unsigned long oldidx
;
2055 chan
= shmp(handle
, buf
->backend
.chan
);
2058 config
= &chan
->backend
.config
;
2063 * Perform retryable operations.
2066 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
2068 return; /* Switch not needed */
2069 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
2073 * Atomically update last_tsc. This update races against concurrent
2074 * atomic updates, but the race will always cause supplementary full TSC
2075 * records, never the opposite (missing a full TSC record when it would
2078 save_last_tsc(config
, buf
, tsc
);
2081 * Push the reader if necessary
2083 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
2085 oldidx
= subbuf_index(offsets
.old
, chan
);
2086 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
2089 * May need to populate header start on SWITCH_FLUSH.
2091 if (offsets
.switch_old_start
) {
2092 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
2093 offsets
.old
+= config
->cb
.subbuffer_header_size();
2097 * Switch old subbuffer.
2099 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
2103 bool handle_blocking_retry(int *timeout_left_ms
)
2105 int timeout
= *timeout_left_ms
, delay
;
2107 if (caa_likely(!timeout
))
2108 return false; /* Do not retry, discard event. */
2109 if (timeout
< 0) /* Wait forever. */
2110 delay
= RETRY_DELAY_MS
;
2112 delay
= min_t(int, timeout
, RETRY_DELAY_MS
);
2113 (void) poll(NULL
, 0, delay
);
2115 *timeout_left_ms
-= delay
;
2116 return true; /* Retry. */
2122 * -ENOSPC if event size is too large for packet.
2123 * -ENOBUFS if there is currently not enough space in buffer for the event.
2124 * -EIO if data cannot be written into the buffer for any other reason.
2127 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_ring_buffer
*buf
,
2128 struct lttng_ust_ring_buffer_channel
*chan
,
2129 struct switch_offsets
*offsets
,
2130 struct lttng_ust_ring_buffer_ctx
*ctx
,
2133 struct lttng_ust_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
2134 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
2135 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
2136 unsigned long reserve_commit_diff
, offset_cmp
;
2137 int timeout_left_ms
= lttng_ust_ringbuffer_get_timeout(chan
);
2140 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2141 offsets
->old
= offsets
->begin
;
2142 offsets
->switch_new_start
= 0;
2143 offsets
->switch_new_end
= 0;
2144 offsets
->switch_old_end
= 0;
2145 offsets
->pre_header_padding
= 0;
2147 ctx_private
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2148 if ((int64_t) ctx_private
->tsc
== -EIO
)
2151 if (last_tsc_overflow(config
, buf
, ctx_private
->tsc
))
2152 ctx_private
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2154 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) == 0)) {
2155 offsets
->switch_new_start
= 1; /* For offsets->begin */
2157 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2159 &offsets
->pre_header_padding
,
2162 lttng_ust_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2165 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2166 offsets
->size
> chan
->backend
.subbuf_size
)) {
2167 offsets
->switch_old_end
= 1; /* For offsets->old */
2168 offsets
->switch_new_start
= 1; /* For offsets->begin */
2171 if (caa_unlikely(offsets
->switch_new_start
)) {
2172 unsigned long sb_index
, commit_count
;
2173 struct commit_counters_cold
*cc_cold
;
2176 * We are typically not filling the previous buffer completely.
2178 if (caa_likely(offsets
->switch_old_end
))
2179 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2180 offsets
->begin
= offsets
->begin
2181 + config
->cb
.subbuffer_header_size();
2182 /* Test new buffer integrity */
2183 sb_index
= subbuf_index(offsets
->begin
, chan
);
2185 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2186 * lib_ring_buffer_check_deliver() has the matching
2187 * memory barriers required around commit_cold cc_sb
2188 * updates to ensure reserve and commit counter updates
2189 * are not seen reordered when updated by another CPU.
2192 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2195 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2196 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2198 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2200 * The reserve counter have been concurrently updated
2201 * while we read the commit counter. This means the
2202 * commit counter we read might not match buf->offset
2203 * due to concurrent update. We therefore need to retry.
2207 reserve_commit_diff
=
2208 (buf_trunc(offsets
->begin
, chan
)
2209 >> chan
->backend
.num_subbuf_order
)
2210 - (commit_count
& chan
->commit_count_mask
);
2211 if (caa_likely(reserve_commit_diff
== 0)) {
2212 /* Next subbuffer not being written to. */
2213 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2214 subbuf_trunc(offsets
->begin
, chan
)
2215 - subbuf_trunc((unsigned long)
2216 uatomic_read(&buf
->consumed
), chan
)
2217 >= chan
->backend
.buf_size
)) {
2218 unsigned long nr_lost
;
2220 if (handle_blocking_retry(&timeout_left_ms
))
2224 * We do not overwrite non consumed buffers
2225 * and we are full : record is lost.
2227 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2228 v_inc(config
, &buf
->records_lost_full
);
2229 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2230 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2231 nr_lost
+ 1, chan
->backend
.name
,
2237 * Next subbuffer not being written to, and we
2238 * are either in overwrite mode or the buffer is
2239 * not full. It's safe to write in this new
2244 unsigned long nr_lost
;
2247 * Next subbuffer reserve offset does not match the
2248 * commit offset, and this did not involve update to the
2249 * reserve counter. Drop record in producer-consumer and
2250 * overwrite mode. Caused by either a writer OOPS or too
2251 * many nested writes over a reserve/commit pair.
2253 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2254 v_inc(config
, &buf
->records_lost_wrap
);
2255 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2256 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2257 nr_lost
+ 1, chan
->backend
.name
,
2263 config
->cb
.record_header_size(config
, chan
,
2265 &offsets
->pre_header_padding
,
2268 lttng_ust_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2271 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2272 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2273 unsigned long nr_lost
;
2276 * Record too big for subbuffers, report error, don't
2277 * complete the sub-buffer switch.
2279 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2280 v_inc(config
, &buf
->records_lost_big
);
2281 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2282 DBG("%lu or more records lost in (%s:%d) record size "
2283 " of %zu bytes is too large for buffer\n",
2284 nr_lost
+ 1, chan
->backend
.name
,
2285 buf
->backend
.cpu
, offsets
->size
);
2290 * We just made a successful buffer switch and the
2291 * record fits in the new subbuffer. Let's write.
2296 * Record fits in the current buffer and we are not on a switch
2297 * boundary. It's safe to write.
2300 offsets
->end
= offsets
->begin
+ offsets
->size
;
2302 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2304 * The offset_end will fall at the very beginning of the next
2307 offsets
->switch_new_end
= 1; /* For offsets->begin */
2313 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2314 * @ctx: ring buffer context.
2316 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2317 * -EIO for other errors, else returns 0.
2318 * It will take care of sub-buffer switching.
2320 int lib_ring_buffer_reserve_slow(struct lttng_ust_ring_buffer_ctx
*ctx
,
2323 struct lttng_ust_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
2324 struct lttng_ust_ring_buffer_channel
*chan
= ctx_private
->chan
;
2325 struct lttng_ust_shm_handle
*handle
= chan
->handle
;
2326 const struct lttng_ust_ring_buffer_config
*config
= &chan
->backend
.config
;
2327 struct lttng_ust_ring_buffer
*buf
;
2328 struct switch_offsets offsets
;
2331 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2332 buf
= shmp(handle
, chan
->backend
.buf
[ctx_private
->reserve_cpu
].shmp
);
2334 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2337 ctx_private
->buf
= buf
;
2342 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2344 if (caa_unlikely(ret
))
2346 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2351 * Atomically update last_tsc. This update races against concurrent
2352 * atomic updates, but the race will always cause supplementary full TSC
2353 * records, never the opposite (missing a full TSC record when it would
2356 save_last_tsc(config
, buf
, ctx_private
->tsc
);
2359 * Push the reader if necessary
2361 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2364 * Clear noref flag for this subbuffer.
2366 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2367 subbuf_index(offsets
.end
- 1, chan
),
2371 * Switch old subbuffer if needed.
2373 if (caa_unlikely(offsets
.switch_old_end
)) {
2374 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2375 subbuf_index(offsets
.old
- 1, chan
),
2377 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx_private
->tsc
, handle
);
2381 * Populate new subbuffer.
2383 if (caa_unlikely(offsets
.switch_new_start
))
2384 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx_private
->tsc
, handle
);
2386 if (caa_unlikely(offsets
.switch_new_end
))
2387 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx_private
->tsc
, handle
);
2389 ctx_private
->slot_size
= offsets
.size
;
2390 ctx_private
->pre_offset
= offsets
.begin
;
2391 ctx_private
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2396 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_ring_buffer_config
*config
,
2397 struct lttng_ust_ring_buffer
*buf
,
2398 unsigned long commit_count
,
2400 struct lttng_ust_shm_handle
*handle
)
2402 struct commit_counters_hot
*cc_hot
;
2404 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2406 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2409 v_set(config
, &cc_hot
->seq
, commit_count
);
2413 * The ring buffer can count events recorded and overwritten per buffer,
2414 * but it is disabled by default due to its performance overhead.
2416 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2418 void deliver_count_events(const struct lttng_ust_ring_buffer_config
*config
,
2419 struct lttng_ust_ring_buffer
*buf
,
2421 struct lttng_ust_shm_handle
*handle
)
2423 v_add(config
, subbuffer_get_records_count(config
,
2424 &buf
->backend
, idx
, handle
),
2425 &buf
->records_count
);
2426 v_add(config
, subbuffer_count_records_overrun(config
,
2427 &buf
->backend
, idx
, handle
),
2428 &buf
->records_overrun
);
2430 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2432 void deliver_count_events(
2433 const struct lttng_ust_ring_buffer_config
*config
__attribute__((unused
)),
2434 struct lttng_ust_ring_buffer
*buf
__attribute__((unused
)),
2435 unsigned long idx
__attribute__((unused
)),
2436 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
2439 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2441 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_ring_buffer_config
*config
,
2442 struct lttng_ust_ring_buffer
*buf
,
2443 struct lttng_ust_ring_buffer_channel
*chan
,
2444 unsigned long offset
,
2445 unsigned long commit_count
,
2447 struct lttng_ust_shm_handle
*handle
,
2448 uint64_t tsc
__attribute__((unused
)))
2450 unsigned long old_commit_count
= commit_count
2451 - chan
->backend
.subbuf_size
;
2452 struct commit_counters_cold
*cc_cold
;
2455 * If we succeeded at updating cc_sb below, we are the subbuffer
2456 * writer delivering the subbuffer. Deals with concurrent
2457 * updates of the "cc" value without adding a add_return atomic
2458 * operation to the fast path.
2460 * We are doing the delivery in two steps:
2461 * - First, we cmpxchg() cc_sb to the new value
2462 * old_commit_count + 1. This ensures that we are the only
2463 * subbuffer user successfully filling the subbuffer, but we
2464 * do _not_ set the cc_sb value to "commit_count" yet.
2465 * Therefore, other writers that would wrap around the ring
2466 * buffer and try to start writing to our subbuffer would
2467 * have to drop records, because it would appear as
2469 * We therefore have exclusive access to the subbuffer control
2470 * structures. This mutual exclusion with other writers is
2471 * crucially important to perform record overruns count in
2472 * flight recorder mode locklessly.
2473 * - When we are ready to release the subbuffer (either for
2474 * reading or for overrun by other writers), we simply set the
2475 * cc_sb value to "commit_count" and perform delivery.
2477 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2478 * This guarantees that old_commit_count + 1 != commit_count.
2482 * Order prior updates to reserve count prior to the
2483 * commit_cold cc_sb update.
2486 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2489 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2490 old_commit_count
, old_commit_count
+ 1)
2491 == old_commit_count
)) {
2495 * Start of exclusive subbuffer access. We are
2496 * guaranteed to be the last writer in this subbuffer
2497 * and any other writer trying to access this subbuffer
2498 * in this state is required to drop records.
2500 * We can read the ts_end for the current sub-buffer
2501 * which has been saved by the very last space
2502 * reservation for the current sub-buffer.
2504 * Order increment of commit counter before reading ts_end.
2507 ts_end
= shmp_index(handle
, buf
->ts_end
, idx
);
2510 deliver_count_events(config
, buf
, idx
, handle
);
2511 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2512 lib_ring_buffer_get_data_size(config
,
2519 * Increment the packet counter while we have exclusive
2522 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2525 * Set noref flag and offset for this subbuffer id.
2526 * Contains a memory barrier that ensures counter stores
2527 * are ordered before set noref and offset.
2529 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2530 buf_trunc_val(offset
, chan
), handle
);
2533 * Order set_noref and record counter updates before the
2534 * end of subbuffer exclusive access. Orders with
2535 * respect to writers coming into the subbuffer after
2536 * wrap around, and also order wrt concurrent readers.
2539 /* End of exclusive subbuffer access */
2540 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2542 * Order later updates to reserve count after
2543 * the commit cold cc_sb update.
2546 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2547 commit_count
, idx
, handle
);
2550 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2552 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2553 && uatomic_read(&buf
->active_readers
)
2554 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2555 lib_ring_buffer_wakeup(buf
, handle
);
2561 * Force a read (imply TLS allocation for dlopen) of TLS variables.
2563 void lttng_ringbuffer_alloc_tls(void)
2565 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2568 void lib_ringbuffer_signal_init(void)
2574 * Block signal for entire process, so only our thread processes
2578 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2581 PERROR("pthread_sigmask");