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 lttng_ust_lib_ring_buffer_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 lttng_ust_lib_ring_buffer_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 lttng_ust_lib_ring_buffer_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 lttng_ust_lib_ring_buffer_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 lttng_ust_lib_ring_buffer_channel
*chan
= caa_container_of(chanb
,
333 struct lttng_ust_lib_ring_buffer_channel
, backend
);
334 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
335 struct lttng_ust_lib_ring_buffer_channel
*shmp_chan
;
336 struct commit_counters_hot
*cc_hot
;
337 void *priv
= channel_get_private(chan
);
338 size_t subbuf_header_size
;
342 /* Test for cpu hotplug */
343 if (buf
->backend
.allocated
)
346 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
347 set_shmp(buf
->commit_hot
,
349 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
350 if (!shmp(handle
, buf
->commit_hot
)) {
354 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
355 set_shmp(buf
->commit_cold
,
357 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
358 if (!shmp(handle
, buf
->commit_cold
)) {
363 align_shm(shmobj
, __alignof__(uint64_t));
364 set_shmp(buf
->ts_end
,
366 sizeof(uint64_t) * chan
->backend
.num_subbuf
));
367 if (!shmp(handle
, buf
->ts_end
)) {
369 goto free_commit_cold
;
373 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
374 cpu
, handle
, shmobj
);
380 * Write the subbuffer header for first subbuffer so we know the total
381 * duration of data gathering.
383 subbuf_header_size
= config
->cb
.subbuffer_header_size();
384 v_set(config
, &buf
->offset
, subbuf_header_size
);
385 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
390 subbuffer_id_clear_noref(config
, &wsb
->id
);
391 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
396 tsc
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
397 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
398 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
403 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
404 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
406 if (config
->cb
.buffer_create
) {
407 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
412 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
414 buf
->backend
.allocated
= 1;
419 /* ts_end will be freed by shm teardown */
421 /* commit_cold will be freed by shm teardown */
423 /* commit_hot will be freed by shm teardown */
429 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
431 const struct lttng_ust_lib_ring_buffer_config
*config
;
432 struct lttng_ust_shm_handle
*handle
;
433 struct lttng_ust_lib_ring_buffer_channel
*chan
;
436 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
438 chan
= si
->si_value
.sival_ptr
;
439 handle
= chan
->handle
;
440 config
= &chan
->backend
.config
;
442 DBG("Switch timer for channel %p\n", chan
);
445 * Only flush buffers periodically if readers are active.
447 pthread_mutex_lock(&wakeup_fd_mutex
);
448 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
449 for_each_possible_cpu(cpu
) {
450 struct lttng_ust_lib_ring_buffer
*buf
=
451 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
455 if (uatomic_read(&buf
->active_readers
))
456 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
460 struct lttng_ust_lib_ring_buffer
*buf
=
461 shmp(handle
, chan
->backend
.buf
[0].shmp
);
465 if (uatomic_read(&buf
->active_readers
))
466 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
470 pthread_mutex_unlock(&wakeup_fd_mutex
);
475 int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
476 struct lttng_ust_lib_ring_buffer
*buf
,
477 struct lttng_ust_lib_ring_buffer_channel
*chan
,
478 struct lttng_ust_shm_handle
*handle
)
480 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
481 struct commit_counters_cold
*cc_cold
;
483 consumed_old
= uatomic_read(&buf
->consumed
);
484 consumed_idx
= subbuf_index(consumed_old
, chan
);
485 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
488 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
490 * No memory barrier here, since we are only interested
491 * in a statistically correct polling result. The next poll will
492 * get the data is we are racing. The mb() that ensures correct
493 * memory order is in get_subbuf.
495 write_offset
= v_read(config
, &buf
->offset
);
498 * Check that the subbuffer we are trying to consume has been
499 * already fully committed.
502 if (((commit_count
- chan
->backend
.subbuf_size
)
503 & chan
->commit_count_mask
)
504 - (buf_trunc(consumed_old
, chan
)
505 >> chan
->backend
.num_subbuf_order
)
510 * Check that we are not about to read the same subbuffer in
511 * which the writer head is.
513 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
521 void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer
*buf
,
522 struct lttng_ust_shm_handle
*handle
)
524 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
525 sigset_t sigpipe_set
, pending_set
, old_set
;
526 int ret
, sigpipe_was_pending
= 0;
532 * Wake-up the other end by writing a null byte in the pipe
533 * (non-blocking). Important note: Because writing into the
534 * pipe is non-blocking (and therefore we allow dropping wakeup
535 * data, as long as there is wakeup data present in the pipe
536 * buffer to wake up the consumer), the consumer should perform
537 * the following sequence for waiting:
538 * 1) empty the pipe (reads).
539 * 2) check if there is data in the buffer.
540 * 3) wait on the pipe (poll).
542 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
543 * that might be already pending. If a bogus SIGPIPE is sent to
544 * the entire process concurrently by a malicious user, it may
545 * be simply discarded.
547 ret
= sigemptyset(&pending_set
);
550 * sigpending returns the mask of signals that are _both_
551 * blocked for the thread _and_ pending for either the thread or
552 * the entire process.
554 ret
= sigpending(&pending_set
);
556 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
558 * If sigpipe was pending, it means it was already blocked, so
559 * no need to block it.
561 if (!sigpipe_was_pending
) {
562 ret
= sigemptyset(&sigpipe_set
);
564 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
566 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
570 ret
= write(wakeup_fd
, "", 1);
571 } while (ret
== -1L && errno
== EINTR
);
572 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
573 struct timespec timeout
= { 0, 0 };
575 ret
= sigtimedwait(&sigpipe_set
, NULL
,
577 } while (ret
== -1L && errno
== EINTR
);
579 if (!sigpipe_was_pending
) {
580 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
586 void lib_ring_buffer_channel_do_read(struct lttng_ust_lib_ring_buffer_channel
*chan
)
588 const struct lttng_ust_lib_ring_buffer_config
*config
;
589 struct lttng_ust_shm_handle
*handle
;
592 handle
= chan
->handle
;
593 config
= &chan
->backend
.config
;
596 * Only flush buffers periodically if readers are active.
598 pthread_mutex_lock(&wakeup_fd_mutex
);
599 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
600 for_each_possible_cpu(cpu
) {
601 struct lttng_ust_lib_ring_buffer
*buf
=
602 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
606 if (uatomic_read(&buf
->active_readers
)
607 && lib_ring_buffer_poll_deliver(config
, buf
,
609 lib_ring_buffer_wakeup(buf
, handle
);
613 struct lttng_ust_lib_ring_buffer
*buf
=
614 shmp(handle
, chan
->backend
.buf
[0].shmp
);
618 if (uatomic_read(&buf
->active_readers
)
619 && lib_ring_buffer_poll_deliver(config
, buf
,
621 lib_ring_buffer_wakeup(buf
, handle
);
625 pthread_mutex_unlock(&wakeup_fd_mutex
);
629 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
631 struct lttng_ust_lib_ring_buffer_channel
*chan
;
633 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
634 chan
= si
->si_value
.sival_ptr
;
635 DBG("Read timer for channel %p\n", chan
);
636 lib_ring_buffer_channel_do_read(chan
);
641 void rb_setmask(sigset_t
*mask
)
645 ret
= sigemptyset(mask
);
647 PERROR("sigemptyset");
649 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
653 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
657 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
664 void *sig_thread(void *arg
)
670 /* Only self thread will receive signal mask. */
672 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
675 signr
= sigwaitinfo(&mask
, &info
);
678 PERROR("sigwaitinfo");
681 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
682 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
684 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
685 lib_ring_buffer_channel_read_timer(info
.si_signo
,
687 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
689 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
692 ERR("Unexptected signal %d\n", info
.si_signo
);
699 * Ensure only a single thread listens on the timer signal.
702 void lib_ring_buffer_setup_timer_thread(void)
707 pthread_mutex_lock(&timer_signal
.lock
);
708 if (timer_signal
.setup_done
)
711 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
714 PERROR("pthread_create");
716 ret
= pthread_detach(thread
);
719 PERROR("pthread_detach");
721 timer_signal
.setup_done
= 1;
723 pthread_mutex_unlock(&timer_signal
.lock
);
727 * Wait for signal-handling thread quiescent state.
730 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
732 sigset_t pending_set
;
736 * We need to be the only thread interacting with the thread
737 * that manages signals for teardown synchronization.
739 pthread_mutex_lock(&timer_signal
.lock
);
742 * Ensure we don't have any signal queued for this channel.
745 ret
= sigemptyset(&pending_set
);
747 PERROR("sigemptyset");
749 ret
= sigpending(&pending_set
);
751 PERROR("sigpending");
753 if (!sigismember(&pending_set
, signr
))
759 * From this point, no new signal handler will be fired that
760 * would try to access "chan". However, we still need to wait
761 * for any currently executing handler to complete.
764 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
768 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
771 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
773 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
777 pthread_mutex_unlock(&timer_signal
.lock
);
781 void lib_ring_buffer_channel_switch_timer_start(struct lttng_ust_lib_ring_buffer_channel
*chan
)
784 struct itimerspec its
;
787 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
790 chan
->switch_timer_enabled
= 1;
792 lib_ring_buffer_setup_timer_thread();
794 memset(&sev
, 0, sizeof(sev
));
795 sev
.sigev_notify
= SIGEV_SIGNAL
;
796 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
797 sev
.sigev_value
.sival_ptr
= chan
;
798 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
800 PERROR("timer_create");
803 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
804 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
805 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
806 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
808 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
810 PERROR("timer_settime");
815 void lib_ring_buffer_channel_switch_timer_stop(struct lttng_ust_lib_ring_buffer_channel
*chan
)
819 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
822 ret
= timer_delete(chan
->switch_timer
);
824 PERROR("timer_delete");
827 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
829 chan
->switch_timer
= 0;
830 chan
->switch_timer_enabled
= 0;
834 void lib_ring_buffer_channel_read_timer_start(struct lttng_ust_lib_ring_buffer_channel
*chan
)
836 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
838 struct itimerspec its
;
841 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
842 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
845 chan
->read_timer_enabled
= 1;
847 lib_ring_buffer_setup_timer_thread();
849 sev
.sigev_notify
= SIGEV_SIGNAL
;
850 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
851 sev
.sigev_value
.sival_ptr
= chan
;
852 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
854 PERROR("timer_create");
857 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
858 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
859 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
860 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
862 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
864 PERROR("timer_settime");
869 void lib_ring_buffer_channel_read_timer_stop(struct lttng_ust_lib_ring_buffer_channel
*chan
)
871 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
874 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
875 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
878 ret
= timer_delete(chan
->read_timer
);
880 PERROR("timer_delete");
884 * do one more check to catch data that has been written in the last
887 lib_ring_buffer_channel_do_read(chan
);
889 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
891 chan
->read_timer
= 0;
892 chan
->read_timer_enabled
= 0;
895 static void channel_unregister_notifiers(struct lttng_ust_lib_ring_buffer_channel
*chan
,
896 struct lttng_ust_shm_handle
*handle
)
898 lib_ring_buffer_channel_switch_timer_stop(chan
);
899 lib_ring_buffer_channel_read_timer_stop(chan
);
902 static void channel_print_errors(struct lttng_ust_lib_ring_buffer_channel
*chan
,
903 struct lttng_ust_shm_handle
*handle
)
905 const struct lttng_ust_lib_ring_buffer_config
*config
=
906 &chan
->backend
.config
;
909 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
910 for_each_possible_cpu(cpu
) {
911 struct lttng_ust_lib_ring_buffer
*buf
=
912 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
914 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
917 struct lttng_ust_lib_ring_buffer
*buf
=
918 shmp(handle
, chan
->backend
.buf
[0].shmp
);
921 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
925 static void channel_free(struct lttng_ust_lib_ring_buffer_channel
*chan
,
926 struct lttng_ust_shm_handle
*handle
,
929 channel_backend_free(&chan
->backend
, handle
);
930 /* chan is freed by shm teardown */
931 shm_object_table_destroy(handle
->table
, consumer
);
936 * channel_create - Create channel.
937 * @config: ring buffer instance configuration
938 * @name: name of the channel
939 * @priv_data: ring buffer client private data area pointer (output)
940 * @priv_data_size: length, in bytes, of the private data area.
941 * @priv_data_init: initialization data for private data.
942 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
943 * address mapping. It is used only by RING_BUFFER_STATIC
944 * configuration. It can be set to NULL for other backends.
945 * @subbuf_size: subbuffer size
946 * @num_subbuf: number of subbuffers
947 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
948 * padding to let readers get those sub-buffers.
949 * Used for live streaming.
950 * @read_timer_interval: Time interval (in us) to wake up pending readers.
951 * @stream_fds: array of stream file descriptors.
952 * @nr_stream_fds: number of file descriptors in array.
955 * Returns NULL on failure.
957 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
960 size_t priv_data_align
,
961 size_t priv_data_size
,
962 void *priv_data_init
,
963 void *buf_addr
, size_t subbuf_size
,
964 size_t num_subbuf
, unsigned int switch_timer_interval
,
965 unsigned int read_timer_interval
,
966 const int *stream_fds
, int nr_stream_fds
,
967 int64_t blocking_timeout
)
970 size_t shmsize
, chansize
;
971 struct lttng_ust_lib_ring_buffer_channel
*chan
;
972 struct lttng_ust_shm_handle
*handle
;
973 struct shm_object
*shmobj
;
974 unsigned int nr_streams
;
975 int64_t blocking_timeout_ms
;
977 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
978 nr_streams
= num_possible_cpus();
982 if (nr_stream_fds
!= nr_streams
)
985 if (blocking_timeout
< -1) {
989 if (blocking_timeout
== -1) {
990 blocking_timeout_ms
= -1;
992 blocking_timeout_ms
= blocking_timeout
/ 1000;
993 if (blocking_timeout_ms
!= (int32_t) blocking_timeout_ms
) {
998 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
999 read_timer_interval
))
1002 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1006 /* Allocate table for channel + per-cpu buffers */
1007 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1009 goto error_table_alloc
;
1011 /* Calculate the shm allocation layout */
1012 shmsize
= sizeof(struct lttng_ust_lib_ring_buffer_channel
);
1013 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
1014 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
1016 if (priv_data_align
)
1017 shmsize
+= lttng_ust_offset_align(shmsize
, priv_data_align
);
1018 shmsize
+= priv_data_size
;
1020 /* Allocate normal memory for channel (not shared) */
1021 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
1025 /* struct lttng_ust_lib_ring_buffer_channel is at object 0, offset 0 (hardcoded) */
1026 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
1027 assert(handle
->chan
._ref
.index
== 0);
1028 assert(handle
->chan
._ref
.offset
== 0);
1029 chan
= shmp(handle
, handle
->chan
);
1032 chan
->nr_streams
= nr_streams
;
1034 /* space for private data */
1035 if (priv_data_size
) {
1036 DECLARE_SHMP(void, priv_data_alloc
);
1038 align_shm(shmobj
, priv_data_align
);
1039 chan
->priv_data_offset
= shmobj
->allocated_len
;
1040 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1041 if (!shmp(handle
, priv_data_alloc
))
1043 *priv_data
= channel_get_private(chan
);
1044 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
1046 chan
->priv_data_offset
= -1;
1051 chan
->u
.s
.blocking_timeout_ms
= (int32_t) blocking_timeout_ms
;
1053 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1054 subbuf_size
, num_subbuf
, handle
,
1057 goto error_backend_init
;
1059 chan
->handle
= handle
;
1060 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1062 chan
->switch_timer_interval
= switch_timer_interval
;
1063 chan
->read_timer_interval
= read_timer_interval
;
1064 lib_ring_buffer_channel_switch_timer_start(chan
);
1065 lib_ring_buffer_channel_read_timer_start(chan
);
1071 shm_object_table_destroy(handle
->table
, 1);
1077 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1078 uint64_t memory_map_size
,
1081 struct lttng_ust_shm_handle
*handle
;
1082 struct shm_object
*object
;
1084 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1088 /* Allocate table for channel + per-cpu buffers */
1089 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1091 goto error_table_alloc
;
1092 /* Add channel object */
1093 object
= shm_object_table_append_mem(handle
->table
, data
,
1094 memory_map_size
, wakeup_fd
);
1096 goto error_table_object
;
1097 /* struct lttng_ust_lib_ring_buffer_channel is at object 0, offset 0 (hardcoded) */
1098 handle
->chan
._ref
.index
= 0;
1099 handle
->chan
._ref
.offset
= 0;
1103 shm_object_table_destroy(handle
->table
, 0);
1109 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1110 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1111 uint64_t memory_map_size
)
1113 struct shm_object
*object
;
1115 /* Add stream object */
1116 object
= shm_object_table_append_shm(handle
->table
,
1117 shm_fd
, wakeup_fd
, stream_nr
,
1124 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1126 assert(handle
->table
);
1127 return handle
->table
->allocated_len
- 1;
1131 void channel_release(struct lttng_ust_lib_ring_buffer_channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1134 channel_free(chan
, handle
, consumer
);
1138 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1139 * @chan: channel to destroy
1141 * Holds cpu hotplug.
1142 * Call "destroy" callback, finalize channels, decrement the channel
1143 * reference count. Note that when readers have completed data
1144 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1145 * They should release their handle at that point.
1147 void channel_destroy(struct lttng_ust_lib_ring_buffer_channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1152 * Note: the consumer takes care of finalizing and
1153 * switching the buffers.
1155 channel_unregister_notifiers(chan
, handle
);
1157 * The consumer prints errors.
1159 channel_print_errors(chan
, handle
);
1163 * sessiond/consumer are keeping a reference on the shm file
1164 * descriptor directly. No need to refcount.
1166 channel_release(chan
, handle
, consumer
);
1170 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
1171 const struct lttng_ust_lib_ring_buffer_config
*config
,
1172 struct lttng_ust_lib_ring_buffer_channel
*chan
, int cpu
,
1173 struct lttng_ust_shm_handle
*handle
,
1174 int *shm_fd
, int *wait_fd
,
1176 uint64_t *memory_map_size
)
1178 struct shm_ref
*ref
;
1180 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1183 if (cpu
>= num_possible_cpus())
1186 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1187 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1188 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1189 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1190 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1192 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1195 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1196 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1197 struct lttng_ust_shm_handle
*handle
)
1199 struct shm_ref
*ref
;
1201 ref
= &handle
->chan
._ref
;
1202 return shm_close_wait_fd(handle
, ref
);
1205 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1206 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1207 struct lttng_ust_shm_handle
*handle
)
1209 struct shm_ref
*ref
;
1211 ref
= &handle
->chan
._ref
;
1212 return shm_close_wakeup_fd(handle
, ref
);
1215 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1216 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1217 struct lttng_ust_shm_handle
*handle
,
1220 struct shm_ref
*ref
;
1222 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1225 if (cpu
>= num_possible_cpus())
1228 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1229 return shm_close_wait_fd(handle
, ref
);
1232 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1233 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1234 struct lttng_ust_shm_handle
*handle
,
1237 struct shm_ref
*ref
;
1240 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1243 if (cpu
>= num_possible_cpus())
1246 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1247 pthread_mutex_lock(&wakeup_fd_mutex
);
1248 ret
= shm_close_wakeup_fd(handle
, ref
);
1249 pthread_mutex_unlock(&wakeup_fd_mutex
);
1253 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
1254 struct lttng_ust_shm_handle
*handle
)
1256 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1262 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
1263 struct lttng_ust_shm_handle
*handle
)
1265 struct lttng_ust_lib_ring_buffer_channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1269 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1271 uatomic_dec(&buf
->active_readers
);
1275 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1277 * @consumed: consumed count indicating the position where to read
1278 * @produced: produced count, indicates position when to stop reading
1280 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1281 * data to read at consumed position, or 0 if the get operation succeeds.
1284 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1285 unsigned long *consumed
, unsigned long *produced
,
1286 struct lttng_ust_shm_handle
*handle
)
1288 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1289 const struct lttng_ust_lib_ring_buffer_config
*config
;
1290 unsigned long consumed_cur
, write_offset
;
1293 chan
= shmp(handle
, buf
->backend
.chan
);
1296 config
= &chan
->backend
.config
;
1297 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1299 * Read finalized before counters.
1302 consumed_cur
= uatomic_read(&buf
->consumed
);
1304 * No need to issue a memory barrier between consumed count read and
1305 * write offset read, because consumed count can only change
1306 * concurrently in overwrite mode, and we keep a sequence counter
1307 * identifier derived from the write offset to check we are getting
1308 * the same sub-buffer we are expecting (the sub-buffers are atomically
1309 * "tagged" upon writes, tags are checked upon read).
1311 write_offset
= v_read(config
, &buf
->offset
);
1314 * Check that we are not about to read the same subbuffer in
1315 * which the writer head is.
1317 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1321 *consumed
= consumed_cur
;
1322 *produced
= subbuf_trunc(write_offset
, chan
);
1328 * The memory barriers __wait_event()/wake_up_interruptible() take care
1329 * of "raw_spin_is_locked" memory ordering.
1338 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1339 * are saved regardless of whether the consumed and produced positions are
1340 * in the same subbuffer.
1342 * @consumed: consumed byte count indicating the last position read
1343 * @produced: produced byte count indicating the last position written
1345 * This function is meant to provide information on the exact producer and
1346 * consumer positions without regard for the "snapshot" feature.
1348 int lib_ring_buffer_snapshot_sample_positions(
1349 struct lttng_ust_lib_ring_buffer
*buf
,
1350 unsigned long *consumed
, unsigned long *produced
,
1351 struct lttng_ust_shm_handle
*handle
)
1353 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1354 const struct lttng_ust_lib_ring_buffer_config
*config
;
1356 chan
= shmp(handle
, buf
->backend
.chan
);
1359 config
= &chan
->backend
.config
;
1361 *consumed
= uatomic_read(&buf
->consumed
);
1363 * No need to issue a memory barrier between consumed count read and
1364 * write offset read, because consumed count can only change
1365 * concurrently in overwrite mode, and we keep a sequence counter
1366 * identifier derived from the write offset to check we are getting
1367 * the same sub-buffer we are expecting (the sub-buffers are atomically
1368 * "tagged" upon writes, tags are checked upon read).
1370 *produced
= v_read(config
, &buf
->offset
);
1375 * lib_ring_buffer_move_consumer - move consumed counter forward
1377 * @consumed_new: new consumed count value
1379 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1380 unsigned long consumed_new
,
1381 struct lttng_ust_shm_handle
*handle
)
1383 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1384 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1385 unsigned long consumed
;
1387 chan
= shmp(handle
, bufb
->chan
);
1390 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1393 * Only push the consumed value forward.
1394 * If the consumed cmpxchg fails, this is because we have been pushed by
1395 * the writer in flight recorder mode.
1397 consumed
= uatomic_read(&buf
->consumed
);
1398 while ((long) consumed
- (long) consumed_new
< 0)
1399 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1404 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1406 * @consumed: consumed count indicating the position where to read
1408 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1409 * data to read at consumed position, or 0 if the get operation succeeds.
1411 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1412 unsigned long consumed
,
1413 struct lttng_ust_shm_handle
*handle
)
1415 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1416 const struct lttng_ust_lib_ring_buffer_config
*config
;
1417 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1418 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1419 struct commit_counters_cold
*cc_cold
;
1421 chan
= shmp(handle
, buf
->backend
.chan
);
1424 config
= &chan
->backend
.config
;
1426 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1428 * Read finalized before counters.
1431 consumed_cur
= uatomic_read(&buf
->consumed
);
1432 consumed_idx
= subbuf_index(consumed
, chan
);
1433 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1436 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1438 * Make sure we read the commit count before reading the buffer
1439 * data and the write offset. Correct consumed offset ordering
1440 * wrt commit count is insured by the use of cmpxchg to update
1441 * the consumed offset.
1444 * Local rmb to match the remote wmb to read the commit count
1445 * before the buffer data and the write offset.
1449 write_offset
= v_read(config
, &buf
->offset
);
1452 * Check that the buffer we are getting is after or at consumed_cur
1455 if ((long) subbuf_trunc(consumed
, chan
)
1456 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1460 * Check that the subbuffer we are trying to consume has been
1461 * already fully committed. There are a few causes that can make
1462 * this unavailability situation occur:
1464 * Temporary (short-term) situation:
1465 * - Application is running on a different CPU, between reserve
1466 * and commit ring buffer operations,
1467 * - Application is preempted between reserve and commit ring
1468 * buffer operations,
1470 * Long-term situation:
1471 * - Application is stopped (SIGSTOP) between reserve and commit
1472 * ring buffer operations. Could eventually be resumed by
1474 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1475 * reserve and commit ring buffer operation.
1477 * From a consumer perspective, handling short-term
1478 * unavailability situations is performed by retrying a few
1479 * times after a delay. Handling long-term unavailability
1480 * situations is handled by failing to get the sub-buffer.
1482 * In all of those situations, if the application is taking a
1483 * long time to perform its commit after ring buffer space
1484 * reservation, we can end up in a situation where the producer
1485 * will fill the ring buffer and try to write into the same
1486 * sub-buffer again (which has a missing commit). This is
1487 * handled by the producer in the sub-buffer switch handling
1488 * code of the reserve routine by detecting unbalanced
1489 * reserve/commit counters and discarding all further events
1490 * until the situation is resolved in those situations. Two
1491 * scenarios can occur:
1493 * 1) The application causing the reserve/commit counters to be
1494 * unbalanced has been terminated. In this situation, all
1495 * further events will be discarded in the buffers, and no
1496 * further buffer data will be readable by the consumer
1497 * daemon. Tearing down the UST tracing session and starting
1498 * anew is a work-around for those situations. Note that this
1499 * only affects per-UID tracing. In per-PID tracing, the
1500 * application vanishes with the termination, and therefore
1501 * no more data needs to be written to the buffers.
1502 * 2) The application causing the unbalance has been delayed for
1503 * a long time, but will eventually try to increment the
1504 * commit counter after eventually writing to the sub-buffer.
1505 * This situation can cause events to be discarded until the
1506 * application resumes its operations.
1508 if (((commit_count
- chan
->backend
.subbuf_size
)
1509 & chan
->commit_count_mask
)
1510 - (buf_trunc(consumed
, chan
)
1511 >> chan
->backend
.num_subbuf_order
)
1513 if (nr_retry
-- > 0) {
1514 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1515 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1523 * Check that we are not about to read the same subbuffer in
1524 * which the writer head is.
1526 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1531 * Failure to get the subbuffer causes a busy-loop retry without going
1532 * to a wait queue. These are caused by short-lived race windows where
1533 * the writer is getting access to a subbuffer we were trying to get
1534 * access to. Also checks that the "consumed" buffer count we are
1535 * looking for matches the one contained in the subbuffer id.
1537 * The short-lived race window described here can be affected by
1538 * application signals and preemption, thus requiring to bound
1539 * the loop to a maximum number of retry.
1541 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1542 consumed_idx
, buf_trunc_val(consumed
, chan
),
1545 if (nr_retry
-- > 0) {
1546 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1547 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1553 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1555 buf
->get_subbuf_consumed
= consumed
;
1556 buf
->get_subbuf
= 1;
1562 * The memory barriers __wait_event()/wake_up_interruptible() take care
1563 * of "raw_spin_is_locked" memory ordering.
1572 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1575 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1576 struct lttng_ust_shm_handle
*handle
)
1578 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1579 struct lttng_ust_lib_ring_buffer_channel
*chan
;
1580 const struct lttng_ust_lib_ring_buffer_config
*config
;
1581 unsigned long sb_bindex
, consumed_idx
, consumed
;
1582 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
1583 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
1585 chan
= shmp(handle
, bufb
->chan
);
1588 config
= &chan
->backend
.config
;
1589 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1591 if (!buf
->get_subbuf
) {
1593 * Reader puts a subbuffer it did not get.
1595 CHAN_WARN_ON(chan
, 1);
1598 consumed
= buf
->get_subbuf_consumed
;
1599 buf
->get_subbuf
= 0;
1602 * Clear the records_unread counter. (overruns counter)
1603 * Can still be non-zero if a file reader simply grabbed the data
1604 * without using iterators.
1605 * Can be below zero if an iterator is used on a snapshot more than
1608 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1609 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1612 backend_pages
= shmp(handle
, rpages
->shmp
);
1615 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1616 &bufb
->records_read
);
1617 v_set(config
, &backend_pages
->records_unread
, 0);
1618 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1619 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1620 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1623 * Exchange the reader subbuffer with the one we put in its place in the
1624 * writer subbuffer table. Expect the original consumed count. If
1625 * update_read_sb_index fails, this is because the writer updated the
1626 * subbuffer concurrently. We should therefore keep the subbuffer we
1627 * currently have: it has become invalid to try reading this sub-buffer
1628 * consumed count value anyway.
1630 consumed_idx
= subbuf_index(consumed
, chan
);
1631 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1632 consumed_idx
, buf_trunc_val(consumed
, chan
),
1635 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1636 * if the writer concurrently updated it.
1641 * cons_offset is an iterator on all subbuffer offsets between the reader
1642 * position and the writer position. (inclusive)
1645 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1646 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1647 unsigned long cons_offset
,
1649 struct lttng_ust_shm_handle
*handle
)
1651 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1652 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1653 struct commit_counters_hot
*cc_hot
;
1654 struct commit_counters_cold
*cc_cold
;
1656 cons_idx
= subbuf_index(cons_offset
, chan
);
1657 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1660 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1663 commit_count
= v_read(config
, &cc_hot
->cc
);
1664 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1666 if (subbuf_offset(commit_count
, chan
) != 0)
1667 DBG("ring buffer %s, cpu %d: "
1668 "commit count in subbuffer %lu,\n"
1669 "expecting multiples of %lu bytes\n"
1670 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1671 chan
->backend
.name
, cpu
, cons_idx
,
1672 chan
->backend
.subbuf_size
,
1673 commit_count
, commit_count_sb
);
1675 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1676 chan
->backend
.name
, cpu
, commit_count
);
1680 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1681 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1682 void *priv
, int cpu
,
1683 struct lttng_ust_shm_handle
*handle
)
1685 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1686 unsigned long write_offset
, cons_offset
;
1689 * No need to order commit_count, write_offset and cons_offset reads
1690 * because we execute at teardown when no more writer nor reader
1691 * references are left.
1693 write_offset
= v_read(config
, &buf
->offset
);
1694 cons_offset
= uatomic_read(&buf
->consumed
);
1695 if (write_offset
!= cons_offset
)
1696 DBG("ring buffer %s, cpu %d: "
1697 "non-consumed data\n"
1698 " [ %lu bytes written, %lu bytes read ]\n",
1699 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1701 for (cons_offset
= uatomic_read(&buf
->consumed
);
1702 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1705 cons_offset
= subbuf_align(cons_offset
, chan
))
1706 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1711 void lib_ring_buffer_print_errors(struct lttng_ust_lib_ring_buffer_channel
*chan
,
1712 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1713 struct lttng_ust_shm_handle
*handle
)
1715 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1716 void *priv
= channel_get_private(chan
);
1718 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1719 DBG("ring buffer %s: %lu records written, "
1720 "%lu records overrun\n",
1722 v_read(config
, &buf
->records_count
),
1723 v_read(config
, &buf
->records_overrun
));
1725 DBG("ring buffer %s, cpu %d: %lu records written, "
1726 "%lu records overrun\n",
1727 chan
->backend
.name
, cpu
,
1728 v_read(config
, &buf
->records_count
),
1729 v_read(config
, &buf
->records_overrun
));
1731 if (v_read(config
, &buf
->records_lost_full
)
1732 || v_read(config
, &buf
->records_lost_wrap
)
1733 || v_read(config
, &buf
->records_lost_big
))
1734 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1735 " [ %lu buffer full, %lu nest buffer wrap-around, "
1736 "%lu event too big ]\n",
1737 chan
->backend
.name
, cpu
,
1738 v_read(config
, &buf
->records_lost_full
),
1739 v_read(config
, &buf
->records_lost_wrap
),
1740 v_read(config
, &buf
->records_lost_big
));
1742 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1746 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1748 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1749 * active or at buffer finalization (destroy).
1752 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1753 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1754 struct switch_offsets
*offsets
,
1756 struct lttng_ust_shm_handle
*handle
)
1758 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1759 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1760 unsigned long commit_count
;
1761 struct commit_counters_hot
*cc_hot
;
1763 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1766 * Order all writes to buffer before the commit count update that will
1767 * determine that the subbuffer is full.
1770 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1773 v_add(config
, config
->cb
.subbuffer_header_size(),
1775 commit_count
= v_read(config
, &cc_hot
->cc
);
1776 /* Check if the written buffer has to be delivered */
1777 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1778 commit_count
, oldidx
, handle
, tsc
);
1779 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1780 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1781 commit_count
, handle
, cc_hot
);
1785 * lib_ring_buffer_switch_old_end: switch old subbuffer
1787 * Note : offset_old should never be 0 here. It is ok, because we never perform
1788 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1789 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1793 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1794 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1795 struct switch_offsets
*offsets
,
1797 struct lttng_ust_shm_handle
*handle
)
1799 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1800 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1801 unsigned long commit_count
, padding_size
, data_size
;
1802 struct commit_counters_hot
*cc_hot
;
1805 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1806 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1807 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1810 ts_end
= shmp_index(handle
, buf
->ts_end
, oldidx
);
1814 * This is the last space reservation in that sub-buffer before
1815 * it gets delivered. This provides exclusive access to write to
1816 * this sub-buffer's ts_end. There are also no concurrent
1817 * readers of that ts_end because delivery of that sub-buffer is
1818 * postponed until the commit counter is incremented for the
1819 * current space reservation.
1824 * Order all writes to buffer and store to ts_end before the commit
1825 * count update that will determine that the subbuffer is full.
1828 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1831 v_add(config
, padding_size
, &cc_hot
->cc
);
1832 commit_count
= v_read(config
, &cc_hot
->cc
);
1833 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1834 commit_count
, oldidx
, handle
, tsc
);
1835 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1836 offsets
->old
+ padding_size
, commit_count
, handle
,
1841 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1843 * This code can be executed unordered : writers may already have written to the
1844 * sub-buffer before this code gets executed, caution. The commit makes sure
1845 * that this code is executed before the deliver of this sub-buffer.
1848 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1849 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1850 struct switch_offsets
*offsets
,
1852 struct lttng_ust_shm_handle
*handle
)
1854 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1855 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1856 unsigned long commit_count
;
1857 struct commit_counters_hot
*cc_hot
;
1859 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1862 * Order all writes to buffer before the commit count update that will
1863 * determine that the subbuffer is full.
1866 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1869 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1870 commit_count
= v_read(config
, &cc_hot
->cc
);
1871 /* Check if the written buffer has to be delivered */
1872 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1873 commit_count
, beginidx
, handle
, tsc
);
1874 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1875 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1876 commit_count
, handle
, cc_hot
);
1880 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1882 * Calls subbuffer_set_data_size() to set the data size of the current
1883 * sub-buffer. We do not need to perform check_deliver nor commit here,
1884 * since this task will be done by the "commit" of the event for which
1885 * we are currently doing the space reservation.
1888 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1889 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1890 struct switch_offsets
*offsets
,
1892 struct lttng_ust_shm_handle
*handle
)
1894 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1895 unsigned long endidx
, data_size
;
1898 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1899 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1900 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1902 ts_end
= shmp_index(handle
, buf
->ts_end
, endidx
);
1906 * This is the last space reservation in that sub-buffer before
1907 * it gets delivered. This provides exclusive access to write to
1908 * this sub-buffer's ts_end. There are also no concurrent
1909 * readers of that ts_end because delivery of that sub-buffer is
1910 * postponed until the commit counter is incremented for the
1911 * current space reservation.
1919 * !0 if execution must be aborted.
1922 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1923 struct lttng_ust_lib_ring_buffer
*buf
,
1924 struct lttng_ust_lib_ring_buffer_channel
*chan
,
1925 struct switch_offsets
*offsets
,
1927 struct lttng_ust_shm_handle
*handle
)
1929 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1930 unsigned long off
, reserve_commit_diff
;
1932 offsets
->begin
= v_read(config
, &buf
->offset
);
1933 offsets
->old
= offsets
->begin
;
1934 offsets
->switch_old_start
= 0;
1935 off
= subbuf_offset(offsets
->begin
, chan
);
1937 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1940 * Ensure we flush the header of an empty subbuffer when doing the
1941 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1942 * total data gathering duration even if there were no records saved
1943 * after the last buffer switch.
1944 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1945 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1946 * subbuffer header as appropriate.
1947 * The next record that reserves space will be responsible for
1948 * populating the following subbuffer header. We choose not to populate
1949 * the next subbuffer header here because we want to be able to use
1950 * SWITCH_ACTIVE for periodical buffer flush, which must
1951 * guarantee that all the buffer content (records and header
1952 * timestamps) are visible to the reader. This is required for
1953 * quiescence guarantees for the fusion merge.
1955 if (mode
!= SWITCH_FLUSH
&& !off
)
1956 return -1; /* we do not have to switch : buffer is empty */
1958 if (caa_unlikely(off
== 0)) {
1959 unsigned long sb_index
, commit_count
;
1960 struct commit_counters_cold
*cc_cold
;
1963 * We are performing a SWITCH_FLUSH. There may be concurrent
1964 * writes into the buffer if e.g. invoked while performing a
1965 * snapshot on an active trace.
1967 * If the client does not save any header information
1968 * (sub-buffer header size == 0), don't switch empty subbuffer
1969 * on finalize, because it is invalid to deliver a completely
1972 if (!config
->cb
.subbuffer_header_size())
1975 /* Test new buffer integrity */
1976 sb_index
= subbuf_index(offsets
->begin
, chan
);
1977 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1980 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1981 reserve_commit_diff
=
1982 (buf_trunc(offsets
->begin
, chan
)
1983 >> chan
->backend
.num_subbuf_order
)
1984 - (commit_count
& chan
->commit_count_mask
);
1985 if (caa_likely(reserve_commit_diff
== 0)) {
1986 /* Next subbuffer not being written to. */
1987 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1988 subbuf_trunc(offsets
->begin
, chan
)
1989 - subbuf_trunc((unsigned long)
1990 uatomic_read(&buf
->consumed
), chan
)
1991 >= chan
->backend
.buf_size
)) {
1993 * We do not overwrite non consumed buffers
1994 * and we are full : don't switch.
1999 * Next subbuffer not being written to, and we
2000 * are either in overwrite mode or the buffer is
2001 * not full. It's safe to write in this new
2007 * Next subbuffer reserve offset does not match the
2008 * commit offset. Don't perform switch in
2009 * producer-consumer and overwrite mode. Caused by
2010 * either a writer OOPS or too many nested writes over a
2011 * reserve/commit pair.
2017 * Need to write the subbuffer start header on finalize.
2019 offsets
->switch_old_start
= 1;
2021 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2022 /* Note: old points to the next subbuf at offset 0 */
2023 offsets
->end
= offsets
->begin
;
2028 * Force a sub-buffer switch. This operation is completely reentrant : can be
2029 * called while tracing is active with absolutely no lock held.
2031 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2032 * some atomic operations, this function must be called from the CPU
2033 * which owns the buffer for a ACTIVE flush. However, for
2034 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2037 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
2038 struct lttng_ust_shm_handle
*handle
)
2040 struct lttng_ust_lib_ring_buffer_channel
*chan
;
2041 const struct lttng_ust_lib_ring_buffer_config
*config
;
2042 struct switch_offsets offsets
;
2043 unsigned long oldidx
;
2046 chan
= shmp(handle
, buf
->backend
.chan
);
2049 config
= &chan
->backend
.config
;
2054 * Perform retryable operations.
2057 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
2059 return; /* Switch not needed */
2060 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
2064 * Atomically update last_tsc. This update races against concurrent
2065 * atomic updates, but the race will always cause supplementary full TSC
2066 * records, never the opposite (missing a full TSC record when it would
2069 save_last_tsc(config
, buf
, tsc
);
2072 * Push the reader if necessary
2074 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
2076 oldidx
= subbuf_index(offsets
.old
, chan
);
2077 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
2080 * May need to populate header start on SWITCH_FLUSH.
2082 if (offsets
.switch_old_start
) {
2083 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
2084 offsets
.old
+= config
->cb
.subbuffer_header_size();
2088 * Switch old subbuffer.
2090 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
2094 bool handle_blocking_retry(int *timeout_left_ms
)
2096 int timeout
= *timeout_left_ms
, delay
;
2098 if (caa_likely(!timeout
))
2099 return false; /* Do not retry, discard event. */
2100 if (timeout
< 0) /* Wait forever. */
2101 delay
= RETRY_DELAY_MS
;
2103 delay
= min_t(int, timeout
, RETRY_DELAY_MS
);
2104 (void) poll(NULL
, 0, delay
);
2106 *timeout_left_ms
-= delay
;
2107 return true; /* Retry. */
2113 * -ENOSPC if event size is too large for packet.
2114 * -ENOBUFS if there is currently not enough space in buffer for the event.
2115 * -EIO if data cannot be written into the buffer for any other reason.
2118 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
2119 struct lttng_ust_lib_ring_buffer_channel
*chan
,
2120 struct switch_offsets
*offsets
,
2121 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2124 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2125 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2126 unsigned long reserve_commit_diff
, offset_cmp
;
2127 int timeout_left_ms
= lttng_ust_ringbuffer_get_timeout(chan
);
2130 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2131 offsets
->old
= offsets
->begin
;
2132 offsets
->switch_new_start
= 0;
2133 offsets
->switch_new_end
= 0;
2134 offsets
->switch_old_end
= 0;
2135 offsets
->pre_header_padding
= 0;
2137 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2138 if ((int64_t) ctx
->tsc
== -EIO
)
2141 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
2142 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2144 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
2145 offsets
->switch_new_start
= 1; /* For offsets->begin */
2147 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2149 &offsets
->pre_header_padding
,
2152 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2155 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2156 offsets
->size
> chan
->backend
.subbuf_size
)) {
2157 offsets
->switch_old_end
= 1; /* For offsets->old */
2158 offsets
->switch_new_start
= 1; /* For offsets->begin */
2161 if (caa_unlikely(offsets
->switch_new_start
)) {
2162 unsigned long sb_index
, commit_count
;
2163 struct commit_counters_cold
*cc_cold
;
2166 * We are typically not filling the previous buffer completely.
2168 if (caa_likely(offsets
->switch_old_end
))
2169 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2170 offsets
->begin
= offsets
->begin
2171 + config
->cb
.subbuffer_header_size();
2172 /* Test new buffer integrity */
2173 sb_index
= subbuf_index(offsets
->begin
, chan
);
2175 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2176 * lib_ring_buffer_check_deliver() has the matching
2177 * memory barriers required around commit_cold cc_sb
2178 * updates to ensure reserve and commit counter updates
2179 * are not seen reordered when updated by another CPU.
2182 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2185 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2186 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2188 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2190 * The reserve counter have been concurrently updated
2191 * while we read the commit counter. This means the
2192 * commit counter we read might not match buf->offset
2193 * due to concurrent update. We therefore need to retry.
2197 reserve_commit_diff
=
2198 (buf_trunc(offsets
->begin
, chan
)
2199 >> chan
->backend
.num_subbuf_order
)
2200 - (commit_count
& chan
->commit_count_mask
);
2201 if (caa_likely(reserve_commit_diff
== 0)) {
2202 /* Next subbuffer not being written to. */
2203 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2204 subbuf_trunc(offsets
->begin
, chan
)
2205 - subbuf_trunc((unsigned long)
2206 uatomic_read(&buf
->consumed
), chan
)
2207 >= chan
->backend
.buf_size
)) {
2208 unsigned long nr_lost
;
2210 if (handle_blocking_retry(&timeout_left_ms
))
2214 * We do not overwrite non consumed buffers
2215 * and we are full : record is lost.
2217 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2218 v_inc(config
, &buf
->records_lost_full
);
2219 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2220 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2221 nr_lost
+ 1, chan
->backend
.name
,
2227 * Next subbuffer not being written to, and we
2228 * are either in overwrite mode or the buffer is
2229 * not full. It's safe to write in this new
2234 unsigned long nr_lost
;
2237 * Next subbuffer reserve offset does not match the
2238 * commit offset, and this did not involve update to the
2239 * reserve counter. Drop record in producer-consumer and
2240 * overwrite mode. Caused by either a writer OOPS or too
2241 * many nested writes over a reserve/commit pair.
2243 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2244 v_inc(config
, &buf
->records_lost_wrap
);
2245 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2246 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2247 nr_lost
+ 1, chan
->backend
.name
,
2253 config
->cb
.record_header_size(config
, chan
,
2255 &offsets
->pre_header_padding
,
2258 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2261 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2262 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2263 unsigned long nr_lost
;
2266 * Record too big for subbuffers, report error, don't
2267 * complete the sub-buffer switch.
2269 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2270 v_inc(config
, &buf
->records_lost_big
);
2271 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2272 DBG("%lu or more records lost in (%s:%d) record size "
2273 " of %zu bytes is too large for buffer\n",
2274 nr_lost
+ 1, chan
->backend
.name
,
2275 buf
->backend
.cpu
, offsets
->size
);
2280 * We just made a successful buffer switch and the
2281 * record fits in the new subbuffer. Let's write.
2286 * Record fits in the current buffer and we are not on a switch
2287 * boundary. It's safe to write.
2290 offsets
->end
= offsets
->begin
+ offsets
->size
;
2292 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2294 * The offset_end will fall at the very beginning of the next
2297 offsets
->switch_new_end
= 1; /* For offsets->begin */
2303 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2304 * @ctx: ring buffer context.
2306 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2307 * -EIO for other errors, else returns 0.
2308 * It will take care of sub-buffer switching.
2310 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2313 struct lttng_ust_lib_ring_buffer_channel
*chan
= ctx
->chan
;
2314 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2315 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2316 struct lttng_ust_lib_ring_buffer
*buf
;
2317 struct switch_offsets offsets
;
2320 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2321 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
2323 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2331 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2333 if (caa_unlikely(ret
))
2335 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2340 * Atomically update last_tsc. This update races against concurrent
2341 * atomic updates, but the race will always cause supplementary full TSC
2342 * records, never the opposite (missing a full TSC record when it would
2345 save_last_tsc(config
, buf
, ctx
->tsc
);
2348 * Push the reader if necessary
2350 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2353 * Clear noref flag for this subbuffer.
2355 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2356 subbuf_index(offsets
.end
- 1, chan
),
2360 * Switch old subbuffer if needed.
2362 if (caa_unlikely(offsets
.switch_old_end
)) {
2363 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2364 subbuf_index(offsets
.old
- 1, chan
),
2366 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2370 * Populate new subbuffer.
2372 if (caa_unlikely(offsets
.switch_new_start
))
2373 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2375 if (caa_unlikely(offsets
.switch_new_end
))
2376 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2378 ctx
->slot_size
= offsets
.size
;
2379 ctx
->pre_offset
= offsets
.begin
;
2380 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2385 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
2386 struct lttng_ust_lib_ring_buffer
*buf
,
2387 unsigned long commit_count
,
2389 struct lttng_ust_shm_handle
*handle
)
2391 struct commit_counters_hot
*cc_hot
;
2393 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2395 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2398 v_set(config
, &cc_hot
->seq
, commit_count
);
2402 * The ring buffer can count events recorded and overwritten per buffer,
2403 * but it is disabled by default due to its performance overhead.
2405 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2407 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2408 struct lttng_ust_lib_ring_buffer
*buf
,
2410 struct lttng_ust_shm_handle
*handle
)
2412 v_add(config
, subbuffer_get_records_count(config
,
2413 &buf
->backend
, idx
, handle
),
2414 &buf
->records_count
);
2415 v_add(config
, subbuffer_count_records_overrun(config
,
2416 &buf
->backend
, idx
, handle
),
2417 &buf
->records_overrun
);
2419 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2421 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2422 struct lttng_ust_lib_ring_buffer
*buf
,
2424 struct lttng_ust_shm_handle
*handle
)
2427 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2429 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
2430 struct lttng_ust_lib_ring_buffer
*buf
,
2431 struct lttng_ust_lib_ring_buffer_channel
*chan
,
2432 unsigned long offset
,
2433 unsigned long commit_count
,
2435 struct lttng_ust_shm_handle
*handle
,
2438 unsigned long old_commit_count
= commit_count
2439 - chan
->backend
.subbuf_size
;
2440 struct commit_counters_cold
*cc_cold
;
2443 * If we succeeded at updating cc_sb below, we are the subbuffer
2444 * writer delivering the subbuffer. Deals with concurrent
2445 * updates of the "cc" value without adding a add_return atomic
2446 * operation to the fast path.
2448 * We are doing the delivery in two steps:
2449 * - First, we cmpxchg() cc_sb to the new value
2450 * old_commit_count + 1. This ensures that we are the only
2451 * subbuffer user successfully filling the subbuffer, but we
2452 * do _not_ set the cc_sb value to "commit_count" yet.
2453 * Therefore, other writers that would wrap around the ring
2454 * buffer and try to start writing to our subbuffer would
2455 * have to drop records, because it would appear as
2457 * We therefore have exclusive access to the subbuffer control
2458 * structures. This mutual exclusion with other writers is
2459 * crucially important to perform record overruns count in
2460 * flight recorder mode locklessly.
2461 * - When we are ready to release the subbuffer (either for
2462 * reading or for overrun by other writers), we simply set the
2463 * cc_sb value to "commit_count" and perform delivery.
2465 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2466 * This guarantees that old_commit_count + 1 != commit_count.
2470 * Order prior updates to reserve count prior to the
2471 * commit_cold cc_sb update.
2474 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2477 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2478 old_commit_count
, old_commit_count
+ 1)
2479 == old_commit_count
)) {
2483 * Start of exclusive subbuffer access. We are
2484 * guaranteed to be the last writer in this subbuffer
2485 * and any other writer trying to access this subbuffer
2486 * in this state is required to drop records.
2488 * We can read the ts_end for the current sub-buffer
2489 * which has been saved by the very last space
2490 * reservation for the current sub-buffer.
2492 * Order increment of commit counter before reading ts_end.
2495 ts_end
= shmp_index(handle
, buf
->ts_end
, idx
);
2498 deliver_count_events(config
, buf
, idx
, handle
);
2499 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2500 lib_ring_buffer_get_data_size(config
,
2507 * Increment the packet counter while we have exclusive
2510 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2513 * Set noref flag and offset for this subbuffer id.
2514 * Contains a memory barrier that ensures counter stores
2515 * are ordered before set noref and offset.
2517 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2518 buf_trunc_val(offset
, chan
), handle
);
2521 * Order set_noref and record counter updates before the
2522 * end of subbuffer exclusive access. Orders with
2523 * respect to writers coming into the subbuffer after
2524 * wrap around, and also order wrt concurrent readers.
2527 /* End of exclusive subbuffer access */
2528 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2530 * Order later updates to reserve count after
2531 * the commit cold cc_sb update.
2534 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2535 commit_count
, idx
, handle
);
2538 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2540 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2541 && uatomic_read(&buf
->active_readers
)
2542 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2543 lib_ring_buffer_wakeup(buf
, handle
);
2549 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2551 void lttng_fixup_ringbuffer_tls(void)
2553 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2556 void lib_ringbuffer_signal_init(void)
2562 * Block signal for entire process, so only our thread processes
2566 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2569 PERROR("pthread_sigmask");