2 * ring_buffer_frontend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
41 * Bob Wisniewski <bob@watson.ibm.com>
43 * Buffer reader semantic :
46 * while buffer is not finalized and empty
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
56 #include <sys/types.h>
63 #include <urcu/compiler.h>
65 #include <urcu/tls-compat.h>
70 #include <lttng/ringbuffer-config.h>
76 #include "../liblttng-ust/compat.h" /* For ENODATA */
78 /* Print DBG() messages about events lost only every 1048576 hits */
79 #define DBG_PRINT_NR_LOST (1UL << 20)
81 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
82 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
83 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
84 #define CLOCKID CLOCK_MONOTONIC
85 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
86 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
87 #define RETRY_DELAY_MS 100 /* 100 ms. */
90 * Non-static to ensure the compiler does not optimize away the xor.
92 uint8_t lttng_crash_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
95 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
96 * close(2) to close the fd returned by shm_open.
97 * shm_unlink releases the shared memory object name.
98 * ftruncate(2) sets the size of the memory object.
99 * mmap/munmap maps the shared memory obj to a virtual address in the
100 * calling proceess (should be done both in libust and consumer).
101 * See shm_overview(7) for details.
102 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
105 * Since we don't need to access the object using its name, we can
106 * immediately shm_unlink(3) it, and only keep the handle with its file
111 * Internal structure representing offsets to use at a sub-buffer switch.
113 struct switch_offsets
{
114 unsigned long begin
, end
, old
;
115 size_t pre_header_padding
, size
;
116 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
120 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
123 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
126 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
129 void lib_ring_buffer_print_errors(struct channel
*chan
,
130 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
131 struct lttng_ust_shm_handle
*handle
);
134 * Handle timer teardown race wrt memory free of private data by
135 * ring buffer signals are handled by a single thread, which permits
136 * a synchronization point between handling of each signal.
137 * Protected by the lock within the structure.
139 struct timer_signal_data
{
140 pthread_t tid
; /* thread id managing signals */
143 pthread_mutex_t lock
;
146 static struct timer_signal_data timer_signal
= {
150 .lock
= PTHREAD_MUTEX_INITIALIZER
,
153 int lttng_ust_blocking_retry_timeout
=
154 CONFIG_LTTNG_UST_DEFAULT_BLOCKING_RETRY_TIMEOUT_MS
;
156 void lttng_ust_ringbuffer_set_retry_timeout(int timeout
)
158 lttng_ust_blocking_retry_timeout
= timeout
;
162 * lib_ring_buffer_reset - Reset ring buffer to initial values.
165 * Effectively empty the ring buffer. Should be called when the buffer is not
166 * used for writing. The ring buffer can be opened for reading, but the reader
167 * should not be using the iterator concurrently with reset. The previous
168 * current iterator record is reset.
170 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
171 struct lttng_ust_shm_handle
*handle
)
173 struct channel
*chan
;
174 const struct lttng_ust_lib_ring_buffer_config
*config
;
177 chan
= shmp(handle
, buf
->backend
.chan
);
180 config
= &chan
->backend
.config
;
182 * Reset iterator first. It will put the subbuffer if it currently holds
185 v_set(config
, &buf
->offset
, 0);
186 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
187 struct commit_counters_hot
*cc_hot
;
188 struct commit_counters_cold
*cc_cold
;
190 cc_hot
= shmp_index(handle
, buf
->commit_hot
, i
);
193 cc_cold
= shmp_index(handle
, buf
->commit_cold
, i
);
196 v_set(config
, &cc_hot
->cc
, 0);
197 v_set(config
, &cc_hot
->seq
, 0);
198 v_set(config
, &cc_cold
->cc_sb
, 0);
200 uatomic_set(&buf
->consumed
, 0);
201 uatomic_set(&buf
->record_disabled
, 0);
202 v_set(config
, &buf
->last_tsc
, 0);
203 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
204 /* Don't reset number of active readers */
205 v_set(config
, &buf
->records_lost_full
, 0);
206 v_set(config
, &buf
->records_lost_wrap
, 0);
207 v_set(config
, &buf
->records_lost_big
, 0);
208 v_set(config
, &buf
->records_count
, 0);
209 v_set(config
, &buf
->records_overrun
, 0);
214 * channel_reset - Reset channel to initial values.
217 * Effectively empty the channel. Should be called when the channel is not used
218 * for writing. The channel can be opened for reading, but the reader should not
219 * be using the iterator concurrently with reset. The previous current iterator
222 void channel_reset(struct channel
*chan
)
225 * Reset iterators first. Will put the subbuffer if held for reading.
227 uatomic_set(&chan
->record_disabled
, 0);
228 /* Don't reset commit_count_mask, still valid */
229 channel_backend_reset(&chan
->backend
);
230 /* Don't reset switch/read timer interval */
231 /* Don't reset notifiers and notifier enable bits */
232 /* Don't reset reader reference count */
236 void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config
*config
,
237 struct lttng_crash_abi
*crash_abi
,
238 struct lttng_ust_lib_ring_buffer
*buf
,
239 struct channel_backend
*chanb
,
240 struct shm_object
*shmobj
,
241 struct lttng_ust_shm_handle
*handle
)
245 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
246 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
247 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
248 crash_abi
->endian
= RB_CRASH_ENDIAN
;
249 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
250 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
251 crash_abi
->word_size
= sizeof(unsigned long);
252 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
254 /* Offset of fields */
255 crash_abi
->offset
.prod_offset
=
256 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
257 crash_abi
->offset
.consumed_offset
=
258 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
259 crash_abi
->offset
.commit_hot_array
=
260 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
261 crash_abi
->offset
.commit_hot_seq
=
262 offsetof(struct commit_counters_hot
, seq
);
263 crash_abi
->offset
.buf_wsb_array
=
264 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
265 crash_abi
->offset
.buf_wsb_id
=
266 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
, id
);
267 crash_abi
->offset
.sb_array
=
268 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
269 crash_abi
->offset
.sb_array_shmp_offset
=
270 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
,
272 crash_abi
->offset
.sb_backend_p_offset
=
273 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages
,
277 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
278 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
279 crash_abi
->length
.commit_hot_seq
=
280 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
281 crash_abi
->length
.buf_wsb_id
=
282 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer
*) NULL
)->id
);
283 crash_abi
->length
.sb_array_shmp_offset
=
284 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
285 crash_abi
->length
.sb_backend_p_offset
=
286 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
289 crash_abi
->stride
.commit_hot_array
=
290 sizeof(struct commit_counters_hot
);
291 crash_abi
->stride
.buf_wsb_array
=
292 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
);
293 crash_abi
->stride
.sb_array
=
294 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
);
296 /* Buffer constants */
297 crash_abi
->buf_size
= chanb
->buf_size
;
298 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
299 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
300 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
302 if (config
->cb
.content_size_field
) {
303 size_t offset
, length
;
305 config
->cb
.content_size_field(config
, &offset
, &length
);
306 crash_abi
->offset
.content_size
= offset
;
307 crash_abi
->length
.content_size
= length
;
309 crash_abi
->offset
.content_size
= 0;
310 crash_abi
->length
.content_size
= 0;
312 if (config
->cb
.packet_size_field
) {
313 size_t offset
, length
;
315 config
->cb
.packet_size_field(config
, &offset
, &length
);
316 crash_abi
->offset
.packet_size
= offset
;
317 crash_abi
->length
.packet_size
= length
;
319 crash_abi
->offset
.packet_size
= 0;
320 crash_abi
->length
.packet_size
= 0;
325 * Must be called under cpu hotplug protection.
327 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
328 struct channel_backend
*chanb
, int cpu
,
329 struct lttng_ust_shm_handle
*handle
,
330 struct shm_object
*shmobj
)
332 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
333 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
334 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
335 struct 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 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
364 cpu
, handle
, shmobj
);
370 * Write the subbuffer header for first subbuffer so we know the total
371 * duration of data gathering.
373 subbuf_header_size
= config
->cb
.subbuffer_header_size();
374 v_set(config
, &buf
->offset
, subbuf_header_size
);
375 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
380 subbuffer_id_clear_noref(config
, &wsb
->id
);
381 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
386 tsc
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
387 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
388 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
393 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
394 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
396 if (config
->cb
.buffer_create
) {
397 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
402 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
404 buf
->backend
.allocated
= 1;
409 /* commit_cold will be freed by shm teardown */
411 /* commit_hot will be freed by shm teardown */
417 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
419 const struct lttng_ust_lib_ring_buffer_config
*config
;
420 struct lttng_ust_shm_handle
*handle
;
421 struct channel
*chan
;
424 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
426 chan
= si
->si_value
.sival_ptr
;
427 handle
= chan
->handle
;
428 config
= &chan
->backend
.config
;
430 DBG("Switch timer for channel %p\n", chan
);
433 * Only flush buffers periodically if readers are active.
435 pthread_mutex_lock(&wakeup_fd_mutex
);
436 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
437 for_each_possible_cpu(cpu
) {
438 struct lttng_ust_lib_ring_buffer
*buf
=
439 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
443 if (uatomic_read(&buf
->active_readers
))
444 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
448 struct lttng_ust_lib_ring_buffer
*buf
=
449 shmp(handle
, chan
->backend
.buf
[0].shmp
);
453 if (uatomic_read(&buf
->active_readers
))
454 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
458 pthread_mutex_unlock(&wakeup_fd_mutex
);
463 int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
464 struct lttng_ust_lib_ring_buffer
*buf
,
465 struct channel
*chan
,
466 struct lttng_ust_shm_handle
*handle
)
468 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
469 struct commit_counters_cold
*cc_cold
;
471 consumed_old
= uatomic_read(&buf
->consumed
);
472 consumed_idx
= subbuf_index(consumed_old
, chan
);
473 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
476 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
478 * No memory barrier here, since we are only interested
479 * in a statistically correct polling result. The next poll will
480 * get the data is we are racing. The mb() that ensures correct
481 * memory order is in get_subbuf.
483 write_offset
= v_read(config
, &buf
->offset
);
486 * Check that the subbuffer we are trying to consume has been
487 * already fully committed.
490 if (((commit_count
- chan
->backend
.subbuf_size
)
491 & chan
->commit_count_mask
)
492 - (buf_trunc(consumed_old
, chan
)
493 >> chan
->backend
.num_subbuf_order
)
498 * Check that we are not about to read the same subbuffer in
499 * which the writer head is.
501 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
509 void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer
*buf
,
510 struct lttng_ust_shm_handle
*handle
)
512 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
513 sigset_t sigpipe_set
, pending_set
, old_set
;
514 int ret
, sigpipe_was_pending
= 0;
520 * Wake-up the other end by writing a null byte in the pipe
521 * (non-blocking). Important note: Because writing into the
522 * pipe is non-blocking (and therefore we allow dropping wakeup
523 * data, as long as there is wakeup data present in the pipe
524 * buffer to wake up the consumer), the consumer should perform
525 * the following sequence for waiting:
526 * 1) empty the pipe (reads).
527 * 2) check if there is data in the buffer.
528 * 3) wait on the pipe (poll).
530 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
531 * that might be already pending. If a bogus SIGPIPE is sent to
532 * the entire process concurrently by a malicious user, it may
533 * be simply discarded.
535 ret
= sigemptyset(&pending_set
);
538 * sigpending returns the mask of signals that are _both_
539 * blocked for the thread _and_ pending for either the thread or
540 * the entire process.
542 ret
= sigpending(&pending_set
);
544 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
546 * If sigpipe was pending, it means it was already blocked, so
547 * no need to block it.
549 if (!sigpipe_was_pending
) {
550 ret
= sigemptyset(&sigpipe_set
);
552 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
554 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
558 ret
= write(wakeup_fd
, "", 1);
559 } while (ret
== -1L && errno
== EINTR
);
560 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
561 struct timespec timeout
= { 0, 0 };
563 ret
= sigtimedwait(&sigpipe_set
, NULL
,
565 } while (ret
== -1L && errno
== EINTR
);
567 if (!sigpipe_was_pending
) {
568 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
574 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
576 const struct lttng_ust_lib_ring_buffer_config
*config
;
577 struct lttng_ust_shm_handle
*handle
;
580 handle
= chan
->handle
;
581 config
= &chan
->backend
.config
;
584 * Only flush buffers periodically if readers are active.
586 pthread_mutex_lock(&wakeup_fd_mutex
);
587 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
588 for_each_possible_cpu(cpu
) {
589 struct lttng_ust_lib_ring_buffer
*buf
=
590 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
594 if (uatomic_read(&buf
->active_readers
)
595 && lib_ring_buffer_poll_deliver(config
, buf
,
597 lib_ring_buffer_wakeup(buf
, handle
);
601 struct lttng_ust_lib_ring_buffer
*buf
=
602 shmp(handle
, chan
->backend
.buf
[0].shmp
);
606 if (uatomic_read(&buf
->active_readers
)
607 && lib_ring_buffer_poll_deliver(config
, buf
,
609 lib_ring_buffer_wakeup(buf
, handle
);
613 pthread_mutex_unlock(&wakeup_fd_mutex
);
617 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
619 struct channel
*chan
;
621 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
622 chan
= si
->si_value
.sival_ptr
;
623 DBG("Read timer for channel %p\n", chan
);
624 lib_ring_buffer_channel_do_read(chan
);
629 void rb_setmask(sigset_t
*mask
)
633 ret
= sigemptyset(mask
);
635 PERROR("sigemptyset");
637 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
641 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
645 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
652 void *sig_thread(void *arg
)
658 /* Only self thread will receive signal mask. */
660 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
663 signr
= sigwaitinfo(&mask
, &info
);
666 PERROR("sigwaitinfo");
669 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
670 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
672 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
673 lib_ring_buffer_channel_read_timer(info
.si_signo
,
675 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
677 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
680 ERR("Unexptected signal %d\n", info
.si_signo
);
687 * Ensure only a single thread listens on the timer signal.
690 void lib_ring_buffer_setup_timer_thread(void)
695 pthread_mutex_lock(&timer_signal
.lock
);
696 if (timer_signal
.setup_done
)
699 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
702 PERROR("pthread_create");
704 ret
= pthread_detach(thread
);
707 PERROR("pthread_detach");
709 timer_signal
.setup_done
= 1;
711 pthread_mutex_unlock(&timer_signal
.lock
);
715 * Wait for signal-handling thread quiescent state.
718 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
720 sigset_t pending_set
;
724 * We need to be the only thread interacting with the thread
725 * that manages signals for teardown synchronization.
727 pthread_mutex_lock(&timer_signal
.lock
);
730 * Ensure we don't have any signal queued for this channel.
733 ret
= sigemptyset(&pending_set
);
735 PERROR("sigemptyset");
737 ret
= sigpending(&pending_set
);
739 PERROR("sigpending");
741 if (!sigismember(&pending_set
, signr
))
747 * From this point, no new signal handler will be fired that
748 * would try to access "chan". However, we still need to wait
749 * for any currently executing handler to complete.
752 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
756 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
759 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
761 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
765 pthread_mutex_unlock(&timer_signal
.lock
);
769 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
772 struct itimerspec its
;
775 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
778 chan
->switch_timer_enabled
= 1;
780 lib_ring_buffer_setup_timer_thread();
782 sev
.sigev_notify
= SIGEV_SIGNAL
;
783 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
784 sev
.sigev_value
.sival_ptr
= chan
;
785 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
787 PERROR("timer_create");
790 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
791 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
792 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
793 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
795 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
797 PERROR("timer_settime");
802 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
806 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
809 ret
= timer_delete(chan
->switch_timer
);
811 PERROR("timer_delete");
814 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
816 chan
->switch_timer
= 0;
817 chan
->switch_timer_enabled
= 0;
821 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
823 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
825 struct itimerspec its
;
828 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
829 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
832 chan
->read_timer_enabled
= 1;
834 lib_ring_buffer_setup_timer_thread();
836 sev
.sigev_notify
= SIGEV_SIGNAL
;
837 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
838 sev
.sigev_value
.sival_ptr
= chan
;
839 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
841 PERROR("timer_create");
844 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
845 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
846 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
847 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
849 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
851 PERROR("timer_settime");
856 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
858 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
861 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
862 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
865 ret
= timer_delete(chan
->read_timer
);
867 PERROR("timer_delete");
871 * do one more check to catch data that has been written in the last
874 lib_ring_buffer_channel_do_read(chan
);
876 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
878 chan
->read_timer
= 0;
879 chan
->read_timer_enabled
= 0;
882 static void channel_unregister_notifiers(struct channel
*chan
,
883 struct lttng_ust_shm_handle
*handle
)
885 lib_ring_buffer_channel_switch_timer_stop(chan
);
886 lib_ring_buffer_channel_read_timer_stop(chan
);
889 static void channel_print_errors(struct channel
*chan
,
890 struct lttng_ust_shm_handle
*handle
)
892 const struct lttng_ust_lib_ring_buffer_config
*config
=
893 &chan
->backend
.config
;
896 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
897 for_each_possible_cpu(cpu
) {
898 struct lttng_ust_lib_ring_buffer
*buf
=
899 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
901 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
904 struct lttng_ust_lib_ring_buffer
*buf
=
905 shmp(handle
, chan
->backend
.buf
[0].shmp
);
908 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
912 static void channel_free(struct channel
*chan
,
913 struct lttng_ust_shm_handle
*handle
,
916 channel_backend_free(&chan
->backend
, handle
);
917 /* chan is freed by shm teardown */
918 shm_object_table_destroy(handle
->table
, consumer
);
923 * channel_create - Create channel.
924 * @config: ring buffer instance configuration
925 * @name: name of the channel
926 * @priv_data: ring buffer client private data area pointer (output)
927 * @priv_data_size: length, in bytes, of the private data area.
928 * @priv_data_init: initialization data for private data.
929 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
930 * address mapping. It is used only by RING_BUFFER_STATIC
931 * configuration. It can be set to NULL for other backends.
932 * @subbuf_size: subbuffer size
933 * @num_subbuf: number of subbuffers
934 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
935 * padding to let readers get those sub-buffers.
936 * Used for live streaming.
937 * @read_timer_interval: Time interval (in us) to wake up pending readers.
938 * @stream_fds: array of stream file descriptors.
939 * @nr_stream_fds: number of file descriptors in array.
942 * Returns NULL on failure.
944 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
947 size_t priv_data_align
,
948 size_t priv_data_size
,
949 void *priv_data_init
,
950 void *buf_addr
, size_t subbuf_size
,
951 size_t num_subbuf
, unsigned int switch_timer_interval
,
952 unsigned int read_timer_interval
,
953 const int *stream_fds
, int nr_stream_fds
)
956 size_t shmsize
, chansize
;
957 struct channel
*chan
;
958 struct lttng_ust_shm_handle
*handle
;
959 struct shm_object
*shmobj
;
960 unsigned int nr_streams
;
962 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
963 nr_streams
= num_possible_cpus();
967 if (nr_stream_fds
!= nr_streams
)
970 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
971 read_timer_interval
))
974 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
978 /* Allocate table for channel + per-cpu buffers */
979 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
981 goto error_table_alloc
;
983 /* Calculate the shm allocation layout */
984 shmsize
= sizeof(struct channel
);
985 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
986 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
989 shmsize
+= offset_align(shmsize
, priv_data_align
);
990 shmsize
+= priv_data_size
;
992 /* Allocate normal memory for channel (not shared) */
993 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
997 /* struct channel is at object 0, offset 0 (hardcoded) */
998 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
999 assert(handle
->chan
._ref
.index
== 0);
1000 assert(handle
->chan
._ref
.offset
== 0);
1001 chan
= shmp(handle
, handle
->chan
);
1004 chan
->nr_streams
= nr_streams
;
1006 /* space for private data */
1007 if (priv_data_size
) {
1008 DECLARE_SHMP(void, priv_data_alloc
);
1010 align_shm(shmobj
, priv_data_align
);
1011 chan
->priv_data_offset
= shmobj
->allocated_len
;
1012 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1013 if (!shmp(handle
, priv_data_alloc
))
1015 *priv_data
= channel_get_private(chan
);
1016 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
1018 chan
->priv_data_offset
= -1;
1023 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1024 subbuf_size
, num_subbuf
, handle
,
1027 goto error_backend_init
;
1029 chan
->handle
= handle
;
1030 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1032 chan
->switch_timer_interval
= switch_timer_interval
;
1033 chan
->read_timer_interval
= read_timer_interval
;
1034 lib_ring_buffer_channel_switch_timer_start(chan
);
1035 lib_ring_buffer_channel_read_timer_start(chan
);
1041 shm_object_table_destroy(handle
->table
, 1);
1047 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1048 uint64_t memory_map_size
,
1051 struct lttng_ust_shm_handle
*handle
;
1052 struct shm_object
*object
;
1054 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1058 /* Allocate table for channel + per-cpu buffers */
1059 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1061 goto error_table_alloc
;
1062 /* Add channel object */
1063 object
= shm_object_table_append_mem(handle
->table
, data
,
1064 memory_map_size
, wakeup_fd
);
1066 goto error_table_object
;
1067 /* struct channel is at object 0, offset 0 (hardcoded) */
1068 handle
->chan
._ref
.index
= 0;
1069 handle
->chan
._ref
.offset
= 0;
1073 shm_object_table_destroy(handle
->table
, 0);
1079 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1080 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1081 uint64_t memory_map_size
)
1083 struct shm_object
*object
;
1085 /* Add stream object */
1086 object
= shm_object_table_append_shm(handle
->table
,
1087 shm_fd
, wakeup_fd
, stream_nr
,
1094 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1096 assert(handle
->table
);
1097 return handle
->table
->allocated_len
- 1;
1101 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1104 channel_free(chan
, handle
, consumer
);
1108 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1109 * @chan: channel to destroy
1111 * Holds cpu hotplug.
1112 * Call "destroy" callback, finalize channels, decrement the channel
1113 * reference count. Note that when readers have completed data
1114 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1115 * They should release their handle at that point.
1117 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1122 * Note: the consumer takes care of finalizing and
1123 * switching the buffers.
1125 channel_unregister_notifiers(chan
, handle
);
1127 * The consumer prints errors.
1129 channel_print_errors(chan
, handle
);
1133 * sessiond/consumer are keeping a reference on the shm file
1134 * descriptor directly. No need to refcount.
1136 channel_release(chan
, handle
, consumer
);
1140 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
1141 const struct lttng_ust_lib_ring_buffer_config
*config
,
1142 struct channel
*chan
, int cpu
,
1143 struct lttng_ust_shm_handle
*handle
,
1144 int *shm_fd
, int *wait_fd
,
1146 uint64_t *memory_map_size
)
1148 struct shm_ref
*ref
;
1150 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1153 if (cpu
>= num_possible_cpus())
1156 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1157 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1158 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1159 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1160 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1162 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1165 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1166 struct channel
*chan
,
1167 struct lttng_ust_shm_handle
*handle
)
1169 struct shm_ref
*ref
;
1171 ref
= &handle
->chan
._ref
;
1172 return shm_close_wait_fd(handle
, ref
);
1175 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1176 struct channel
*chan
,
1177 struct lttng_ust_shm_handle
*handle
)
1179 struct shm_ref
*ref
;
1181 ref
= &handle
->chan
._ref
;
1182 return shm_close_wakeup_fd(handle
, ref
);
1185 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1186 struct channel
*chan
,
1187 struct lttng_ust_shm_handle
*handle
,
1190 struct shm_ref
*ref
;
1192 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1195 if (cpu
>= num_possible_cpus())
1198 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1199 return shm_close_wait_fd(handle
, ref
);
1202 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1203 struct channel
*chan
,
1204 struct lttng_ust_shm_handle
*handle
,
1207 struct shm_ref
*ref
;
1210 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1213 if (cpu
>= num_possible_cpus())
1216 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1217 pthread_mutex_lock(&wakeup_fd_mutex
);
1218 ret
= shm_close_wakeup_fd(handle
, ref
);
1219 pthread_mutex_unlock(&wakeup_fd_mutex
);
1223 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
1224 struct lttng_ust_shm_handle
*handle
)
1226 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1232 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
1233 struct lttng_ust_shm_handle
*handle
)
1235 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1239 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1241 uatomic_dec(&buf
->active_readers
);
1245 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1247 * @consumed: consumed count indicating the position where to read
1248 * @produced: produced count, indicates position when to stop reading
1250 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1251 * data to read at consumed position, or 0 if the get operation succeeds.
1254 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1255 unsigned long *consumed
, unsigned long *produced
,
1256 struct lttng_ust_shm_handle
*handle
)
1258 struct channel
*chan
;
1259 const struct lttng_ust_lib_ring_buffer_config
*config
;
1260 unsigned long consumed_cur
, write_offset
;
1263 chan
= shmp(handle
, buf
->backend
.chan
);
1266 config
= &chan
->backend
.config
;
1267 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1269 * Read finalized before counters.
1272 consumed_cur
= uatomic_read(&buf
->consumed
);
1274 * No need to issue a memory barrier between consumed count read and
1275 * write offset read, because consumed count can only change
1276 * concurrently in overwrite mode, and we keep a sequence counter
1277 * identifier derived from the write offset to check we are getting
1278 * the same sub-buffer we are expecting (the sub-buffers are atomically
1279 * "tagged" upon writes, tags are checked upon read).
1281 write_offset
= v_read(config
, &buf
->offset
);
1284 * Check that we are not about to read the same subbuffer in
1285 * which the writer head is.
1287 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1291 *consumed
= consumed_cur
;
1292 *produced
= subbuf_trunc(write_offset
, chan
);
1298 * The memory barriers __wait_event()/wake_up_interruptible() take care
1299 * of "raw_spin_is_locked" memory ordering.
1308 * lib_ring_buffer_move_consumer - move consumed counter forward
1310 * @consumed_new: new consumed count value
1312 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1313 unsigned long consumed_new
,
1314 struct lttng_ust_shm_handle
*handle
)
1316 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1317 struct channel
*chan
;
1318 unsigned long consumed
;
1320 chan
= shmp(handle
, bufb
->chan
);
1323 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1326 * Only push the consumed value forward.
1327 * If the consumed cmpxchg fails, this is because we have been pushed by
1328 * the writer in flight recorder mode.
1330 consumed
= uatomic_read(&buf
->consumed
);
1331 while ((long) consumed
- (long) consumed_new
< 0)
1332 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1337 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1339 * @consumed: consumed count indicating the position where to read
1341 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1342 * data to read at consumed position, or 0 if the get operation succeeds.
1344 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1345 unsigned long consumed
,
1346 struct lttng_ust_shm_handle
*handle
)
1348 struct channel
*chan
;
1349 const struct lttng_ust_lib_ring_buffer_config
*config
;
1350 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1351 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1352 struct commit_counters_cold
*cc_cold
;
1354 chan
= shmp(handle
, buf
->backend
.chan
);
1357 config
= &chan
->backend
.config
;
1359 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1361 * Read finalized before counters.
1364 consumed_cur
= uatomic_read(&buf
->consumed
);
1365 consumed_idx
= subbuf_index(consumed
, chan
);
1366 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1369 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1371 * Make sure we read the commit count before reading the buffer
1372 * data and the write offset. Correct consumed offset ordering
1373 * wrt commit count is insured by the use of cmpxchg to update
1374 * the consumed offset.
1377 * Local rmb to match the remote wmb to read the commit count
1378 * before the buffer data and the write offset.
1382 write_offset
= v_read(config
, &buf
->offset
);
1385 * Check that the buffer we are getting is after or at consumed_cur
1388 if ((long) subbuf_trunc(consumed
, chan
)
1389 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1393 * Check that the subbuffer we are trying to consume has been
1394 * already fully committed. There are a few causes that can make
1395 * this unavailability situation occur:
1397 * Temporary (short-term) situation:
1398 * - Application is running on a different CPU, between reserve
1399 * and commit ring buffer operations,
1400 * - Application is preempted between reserve and commit ring
1401 * buffer operations,
1403 * Long-term situation:
1404 * - Application is stopped (SIGSTOP) between reserve and commit
1405 * ring buffer operations. Could eventually be resumed by
1407 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1408 * reserve and commit ring buffer operation.
1410 * From a consumer perspective, handling short-term
1411 * unavailability situations is performed by retrying a few
1412 * times after a delay. Handling long-term unavailability
1413 * situations is handled by failing to get the sub-buffer.
1415 * In all of those situations, if the application is taking a
1416 * long time to perform its commit after ring buffer space
1417 * reservation, we can end up in a situation where the producer
1418 * will fill the ring buffer and try to write into the same
1419 * sub-buffer again (which has a missing commit). This is
1420 * handled by the producer in the sub-buffer switch handling
1421 * code of the reserve routine by detecting unbalanced
1422 * reserve/commit counters and discarding all further events
1423 * until the situation is resolved in those situations. Two
1424 * scenarios can occur:
1426 * 1) The application causing the reserve/commit counters to be
1427 * unbalanced has been terminated. In this situation, all
1428 * further events will be discarded in the buffers, and no
1429 * further buffer data will be readable by the consumer
1430 * daemon. Tearing down the UST tracing session and starting
1431 * anew is a work-around for those situations. Note that this
1432 * only affects per-UID tracing. In per-PID tracing, the
1433 * application vanishes with the termination, and therefore
1434 * no more data needs to be written to the buffers.
1435 * 2) The application causing the unbalance has been delayed for
1436 * a long time, but will eventually try to increment the
1437 * commit counter after eventually writing to the sub-buffer.
1438 * This situation can cause events to be discarded until the
1439 * application resumes its operations.
1441 if (((commit_count
- chan
->backend
.subbuf_size
)
1442 & chan
->commit_count_mask
)
1443 - (buf_trunc(consumed
, chan
)
1444 >> chan
->backend
.num_subbuf_order
)
1446 if (nr_retry
-- > 0) {
1447 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1448 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1456 * Check that we are not about to read the same subbuffer in
1457 * which the writer head is.
1459 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1464 * Failure to get the subbuffer causes a busy-loop retry without going
1465 * to a wait queue. These are caused by short-lived race windows where
1466 * the writer is getting access to a subbuffer we were trying to get
1467 * access to. Also checks that the "consumed" buffer count we are
1468 * looking for matches the one contained in the subbuffer id.
1470 * The short-lived race window described here can be affected by
1471 * application signals and preemption, thus requiring to bound
1472 * the loop to a maximum number of retry.
1474 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1475 consumed_idx
, buf_trunc_val(consumed
, chan
),
1478 if (nr_retry
-- > 0) {
1479 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1480 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1486 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1488 buf
->get_subbuf_consumed
= consumed
;
1489 buf
->get_subbuf
= 1;
1495 * The memory barriers __wait_event()/wake_up_interruptible() take care
1496 * of "raw_spin_is_locked" memory ordering.
1505 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1508 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1509 struct lttng_ust_shm_handle
*handle
)
1511 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1512 struct channel
*chan
;
1513 const struct lttng_ust_lib_ring_buffer_config
*config
;
1514 unsigned long sb_bindex
, consumed_idx
, consumed
;
1515 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
1516 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
1518 chan
= shmp(handle
, bufb
->chan
);
1521 config
= &chan
->backend
.config
;
1522 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1524 if (!buf
->get_subbuf
) {
1526 * Reader puts a subbuffer it did not get.
1528 CHAN_WARN_ON(chan
, 1);
1531 consumed
= buf
->get_subbuf_consumed
;
1532 buf
->get_subbuf
= 0;
1535 * Clear the records_unread counter. (overruns counter)
1536 * Can still be non-zero if a file reader simply grabbed the data
1537 * without using iterators.
1538 * Can be below zero if an iterator is used on a snapshot more than
1541 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1542 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1545 backend_pages
= shmp(handle
, rpages
->shmp
);
1548 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1549 &bufb
->records_read
);
1550 v_set(config
, &backend_pages
->records_unread
, 0);
1551 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1552 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1553 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1556 * Exchange the reader subbuffer with the one we put in its place in the
1557 * writer subbuffer table. Expect the original consumed count. If
1558 * update_read_sb_index fails, this is because the writer updated the
1559 * subbuffer concurrently. We should therefore keep the subbuffer we
1560 * currently have: it has become invalid to try reading this sub-buffer
1561 * consumed count value anyway.
1563 consumed_idx
= subbuf_index(consumed
, chan
);
1564 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1565 consumed_idx
, buf_trunc_val(consumed
, chan
),
1568 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1569 * if the writer concurrently updated it.
1574 * cons_offset is an iterator on all subbuffer offsets between the reader
1575 * position and the writer position. (inclusive)
1578 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1579 struct channel
*chan
,
1580 unsigned long cons_offset
,
1582 struct lttng_ust_shm_handle
*handle
)
1584 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1585 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1586 struct commit_counters_hot
*cc_hot
;
1587 struct commit_counters_cold
*cc_cold
;
1589 cons_idx
= subbuf_index(cons_offset
, chan
);
1590 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1593 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1596 commit_count
= v_read(config
, &cc_hot
->cc
);
1597 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1599 if (subbuf_offset(commit_count
, chan
) != 0)
1600 DBG("ring buffer %s, cpu %d: "
1601 "commit count in subbuffer %lu,\n"
1602 "expecting multiples of %lu bytes\n"
1603 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1604 chan
->backend
.name
, cpu
, cons_idx
,
1605 chan
->backend
.subbuf_size
,
1606 commit_count
, commit_count_sb
);
1608 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1609 chan
->backend
.name
, cpu
, commit_count
);
1613 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1614 struct channel
*chan
,
1615 void *priv
, int cpu
,
1616 struct lttng_ust_shm_handle
*handle
)
1618 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1619 unsigned long write_offset
, cons_offset
;
1622 * No need to order commit_count, write_offset and cons_offset reads
1623 * because we execute at teardown when no more writer nor reader
1624 * references are left.
1626 write_offset
= v_read(config
, &buf
->offset
);
1627 cons_offset
= uatomic_read(&buf
->consumed
);
1628 if (write_offset
!= cons_offset
)
1629 DBG("ring buffer %s, cpu %d: "
1630 "non-consumed data\n"
1631 " [ %lu bytes written, %lu bytes read ]\n",
1632 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1634 for (cons_offset
= uatomic_read(&buf
->consumed
);
1635 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1638 cons_offset
= subbuf_align(cons_offset
, chan
))
1639 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1644 void lib_ring_buffer_print_errors(struct channel
*chan
,
1645 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1646 struct lttng_ust_shm_handle
*handle
)
1648 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1649 void *priv
= channel_get_private(chan
);
1651 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1652 DBG("ring buffer %s: %lu records written, "
1653 "%lu records overrun\n",
1655 v_read(config
, &buf
->records_count
),
1656 v_read(config
, &buf
->records_overrun
));
1658 DBG("ring buffer %s, cpu %d: %lu records written, "
1659 "%lu records overrun\n",
1660 chan
->backend
.name
, cpu
,
1661 v_read(config
, &buf
->records_count
),
1662 v_read(config
, &buf
->records_overrun
));
1664 if (v_read(config
, &buf
->records_lost_full
)
1665 || v_read(config
, &buf
->records_lost_wrap
)
1666 || v_read(config
, &buf
->records_lost_big
))
1667 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1668 " [ %lu buffer full, %lu nest buffer wrap-around, "
1669 "%lu event too big ]\n",
1670 chan
->backend
.name
, cpu
,
1671 v_read(config
, &buf
->records_lost_full
),
1672 v_read(config
, &buf
->records_lost_wrap
),
1673 v_read(config
, &buf
->records_lost_big
));
1675 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1679 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1681 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1682 * active or at buffer finalization (destroy).
1685 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1686 struct channel
*chan
,
1687 struct switch_offsets
*offsets
,
1689 struct lttng_ust_shm_handle
*handle
)
1691 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1692 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1693 unsigned long commit_count
;
1694 struct commit_counters_hot
*cc_hot
;
1696 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1699 * Order all writes to buffer before the commit count update that will
1700 * determine that the subbuffer is full.
1703 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1706 v_add(config
, config
->cb
.subbuffer_header_size(),
1708 commit_count
= v_read(config
, &cc_hot
->cc
);
1709 /* Check if the written buffer has to be delivered */
1710 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1711 commit_count
, oldidx
, handle
, tsc
);
1712 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1713 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1714 commit_count
, handle
, cc_hot
);
1718 * lib_ring_buffer_switch_old_end: switch old subbuffer
1720 * Note : offset_old should never be 0 here. It is ok, because we never perform
1721 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1722 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1726 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1727 struct channel
*chan
,
1728 struct switch_offsets
*offsets
,
1730 struct lttng_ust_shm_handle
*handle
)
1732 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1733 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1734 unsigned long commit_count
, padding_size
, data_size
;
1735 struct commit_counters_hot
*cc_hot
;
1737 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1738 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1739 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1743 * Order all writes to buffer before the commit count update that will
1744 * determine that the subbuffer is full.
1747 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1750 v_add(config
, padding_size
, &cc_hot
->cc
);
1751 commit_count
= v_read(config
, &cc_hot
->cc
);
1752 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1753 commit_count
, oldidx
, handle
, tsc
);
1754 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1755 offsets
->old
+ padding_size
, commit_count
, handle
,
1760 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1762 * This code can be executed unordered : writers may already have written to the
1763 * sub-buffer before this code gets executed, caution. The commit makes sure
1764 * that this code is executed before the deliver of this sub-buffer.
1767 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1768 struct channel
*chan
,
1769 struct switch_offsets
*offsets
,
1771 struct lttng_ust_shm_handle
*handle
)
1773 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1774 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1775 unsigned long commit_count
;
1776 struct commit_counters_hot
*cc_hot
;
1778 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1781 * Order all writes to buffer before the commit count update that will
1782 * determine that the subbuffer is full.
1785 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1788 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1789 commit_count
= v_read(config
, &cc_hot
->cc
);
1790 /* Check if the written buffer has to be delivered */
1791 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1792 commit_count
, beginidx
, handle
, tsc
);
1793 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1794 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1795 commit_count
, handle
, cc_hot
);
1799 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1801 * Calls subbuffer_set_data_size() to set the data size of the current
1802 * sub-buffer. We do not need to perform check_deliver nor commit here,
1803 * since this task will be done by the "commit" of the event for which
1804 * we are currently doing the space reservation.
1807 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1808 struct channel
*chan
,
1809 struct switch_offsets
*offsets
,
1811 struct lttng_ust_shm_handle
*handle
)
1813 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1814 unsigned long endidx
, data_size
;
1816 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1817 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1818 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1825 * !0 if execution must be aborted.
1828 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1829 struct lttng_ust_lib_ring_buffer
*buf
,
1830 struct channel
*chan
,
1831 struct switch_offsets
*offsets
,
1833 struct lttng_ust_shm_handle
*handle
)
1835 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1836 unsigned long off
, reserve_commit_diff
;
1838 offsets
->begin
= v_read(config
, &buf
->offset
);
1839 offsets
->old
= offsets
->begin
;
1840 offsets
->switch_old_start
= 0;
1841 off
= subbuf_offset(offsets
->begin
, chan
);
1843 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1846 * Ensure we flush the header of an empty subbuffer when doing the
1847 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1848 * total data gathering duration even if there were no records saved
1849 * after the last buffer switch.
1850 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1851 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1852 * subbuffer header as appropriate.
1853 * The next record that reserves space will be responsible for
1854 * populating the following subbuffer header. We choose not to populate
1855 * the next subbuffer header here because we want to be able to use
1856 * SWITCH_ACTIVE for periodical buffer flush, which must
1857 * guarantee that all the buffer content (records and header
1858 * timestamps) are visible to the reader. This is required for
1859 * quiescence guarantees for the fusion merge.
1861 if (mode
!= SWITCH_FLUSH
&& !off
)
1862 return -1; /* we do not have to switch : buffer is empty */
1864 if (caa_unlikely(off
== 0)) {
1865 unsigned long sb_index
, commit_count
;
1866 struct commit_counters_cold
*cc_cold
;
1869 * We are performing a SWITCH_FLUSH. There may be concurrent
1870 * writes into the buffer if e.g. invoked while performing a
1871 * snapshot on an active trace.
1873 * If the client does not save any header information
1874 * (sub-buffer header size == 0), don't switch empty subbuffer
1875 * on finalize, because it is invalid to deliver a completely
1878 if (!config
->cb
.subbuffer_header_size())
1881 /* Test new buffer integrity */
1882 sb_index
= subbuf_index(offsets
->begin
, chan
);
1883 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1886 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1887 reserve_commit_diff
=
1888 (buf_trunc(offsets
->begin
, chan
)
1889 >> chan
->backend
.num_subbuf_order
)
1890 - (commit_count
& chan
->commit_count_mask
);
1891 if (caa_likely(reserve_commit_diff
== 0)) {
1892 /* Next subbuffer not being written to. */
1893 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1894 subbuf_trunc(offsets
->begin
, chan
)
1895 - subbuf_trunc((unsigned long)
1896 uatomic_read(&buf
->consumed
), chan
)
1897 >= chan
->backend
.buf_size
)) {
1899 * We do not overwrite non consumed buffers
1900 * and we are full : don't switch.
1905 * Next subbuffer not being written to, and we
1906 * are either in overwrite mode or the buffer is
1907 * not full. It's safe to write in this new
1913 * Next subbuffer reserve offset does not match the
1914 * commit offset. Don't perform switch in
1915 * producer-consumer and overwrite mode. Caused by
1916 * either a writer OOPS or too many nested writes over a
1917 * reserve/commit pair.
1923 * Need to write the subbuffer start header on finalize.
1925 offsets
->switch_old_start
= 1;
1927 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1928 /* Note: old points to the next subbuf at offset 0 */
1929 offsets
->end
= offsets
->begin
;
1934 * Force a sub-buffer switch. This operation is completely reentrant : can be
1935 * called while tracing is active with absolutely no lock held.
1937 * Note, however, that as a v_cmpxchg is used for some atomic
1938 * operations, this function must be called from the CPU which owns the buffer
1939 * for a ACTIVE flush.
1941 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1942 struct lttng_ust_shm_handle
*handle
)
1944 struct channel
*chan
;
1945 const struct lttng_ust_lib_ring_buffer_config
*config
;
1946 struct switch_offsets offsets
;
1947 unsigned long oldidx
;
1950 chan
= shmp(handle
, buf
->backend
.chan
);
1953 config
= &chan
->backend
.config
;
1958 * Perform retryable operations.
1961 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1963 return; /* Switch not needed */
1964 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1968 * Atomically update last_tsc. This update races against concurrent
1969 * atomic updates, but the race will always cause supplementary full TSC
1970 * records, never the opposite (missing a full TSC record when it would
1973 save_last_tsc(config
, buf
, tsc
);
1976 * Push the reader if necessary
1978 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1980 oldidx
= subbuf_index(offsets
.old
, chan
);
1981 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1984 * May need to populate header start on SWITCH_FLUSH.
1986 if (offsets
.switch_old_start
) {
1987 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1988 offsets
.old
+= config
->cb
.subbuffer_header_size();
1992 * Switch old subbuffer.
1994 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1998 bool handle_blocking_retry(int *timeout_left_ms
)
2000 int timeout
= *timeout_left_ms
, delay
;
2002 if (caa_likely(!timeout
))
2003 return false; /* Do not retry, discard event. */
2004 if (timeout
< 0) /* Wait forever. */
2005 delay
= RETRY_DELAY_MS
;
2007 delay
= min_t(int, timeout
, RETRY_DELAY_MS
);
2008 (void) poll(NULL
, 0, delay
);
2010 *timeout_left_ms
-= delay
;
2011 return true; /* Retry. */
2017 * -ENOSPC if event size is too large for packet.
2018 * -ENOBUFS if there is currently not enough space in buffer for the event.
2019 * -EIO if data cannot be written into the buffer for any other reason.
2022 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
2023 struct channel
*chan
,
2024 struct switch_offsets
*offsets
,
2025 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
2027 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2028 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2029 unsigned long reserve_commit_diff
, offset_cmp
;
2030 int timeout_left_ms
= lttng_ust_blocking_retry_timeout
;
2033 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2034 offsets
->old
= offsets
->begin
;
2035 offsets
->switch_new_start
= 0;
2036 offsets
->switch_new_end
= 0;
2037 offsets
->switch_old_end
= 0;
2038 offsets
->pre_header_padding
= 0;
2040 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2041 if ((int64_t) ctx
->tsc
== -EIO
)
2044 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
2045 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2047 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
2048 offsets
->switch_new_start
= 1; /* For offsets->begin */
2050 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2052 &offsets
->pre_header_padding
,
2055 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2058 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2059 offsets
->size
> chan
->backend
.subbuf_size
)) {
2060 offsets
->switch_old_end
= 1; /* For offsets->old */
2061 offsets
->switch_new_start
= 1; /* For offsets->begin */
2064 if (caa_unlikely(offsets
->switch_new_start
)) {
2065 unsigned long sb_index
, commit_count
;
2066 struct commit_counters_cold
*cc_cold
;
2069 * We are typically not filling the previous buffer completely.
2071 if (caa_likely(offsets
->switch_old_end
))
2072 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2073 offsets
->begin
= offsets
->begin
2074 + config
->cb
.subbuffer_header_size();
2075 /* Test new buffer integrity */
2076 sb_index
= subbuf_index(offsets
->begin
, chan
);
2078 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2079 * lib_ring_buffer_check_deliver() has the matching
2080 * memory barriers required around commit_cold cc_sb
2081 * updates to ensure reserve and commit counter updates
2082 * are not seen reordered when updated by another CPU.
2085 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2088 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2089 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2091 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2093 * The reserve counter have been concurrently updated
2094 * while we read the commit counter. This means the
2095 * commit counter we read might not match buf->offset
2096 * due to concurrent update. We therefore need to retry.
2100 reserve_commit_diff
=
2101 (buf_trunc(offsets
->begin
, chan
)
2102 >> chan
->backend
.num_subbuf_order
)
2103 - (commit_count
& chan
->commit_count_mask
);
2104 if (caa_likely(reserve_commit_diff
== 0)) {
2105 /* Next subbuffer not being written to. */
2106 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2107 subbuf_trunc(offsets
->begin
, chan
)
2108 - subbuf_trunc((unsigned long)
2109 uatomic_read(&buf
->consumed
), chan
)
2110 >= chan
->backend
.buf_size
)) {
2111 unsigned long nr_lost
;
2113 if (handle_blocking_retry(&timeout_left_ms
))
2117 * We do not overwrite non consumed buffers
2118 * and we are full : record is lost.
2120 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2121 v_inc(config
, &buf
->records_lost_full
);
2122 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2123 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2124 nr_lost
+ 1, chan
->backend
.name
,
2130 * Next subbuffer not being written to, and we
2131 * are either in overwrite mode or the buffer is
2132 * not full. It's safe to write in this new
2137 unsigned long nr_lost
;
2140 * Next subbuffer reserve offset does not match the
2141 * commit offset, and this did not involve update to the
2142 * reserve counter. Drop record in producer-consumer and
2143 * overwrite mode. Caused by either a writer OOPS or too
2144 * many nested writes over a reserve/commit pair.
2146 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2147 v_inc(config
, &buf
->records_lost_wrap
);
2148 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2149 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2150 nr_lost
+ 1, chan
->backend
.name
,
2156 config
->cb
.record_header_size(config
, chan
,
2158 &offsets
->pre_header_padding
,
2161 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2164 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2165 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2166 unsigned long nr_lost
;
2169 * Record too big for subbuffers, report error, don't
2170 * complete the sub-buffer switch.
2172 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2173 v_inc(config
, &buf
->records_lost_big
);
2174 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2175 DBG("%lu or more records lost in (%s:%d) record size "
2176 " of %zu bytes is too large for buffer\n",
2177 nr_lost
+ 1, chan
->backend
.name
,
2178 buf
->backend
.cpu
, offsets
->size
);
2183 * We just made a successful buffer switch and the
2184 * record fits in the new subbuffer. Let's write.
2189 * Record fits in the current buffer and we are not on a switch
2190 * boundary. It's safe to write.
2193 offsets
->end
= offsets
->begin
+ offsets
->size
;
2195 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2197 * The offset_end will fall at the very beginning of the next
2200 offsets
->switch_new_end
= 1; /* For offsets->begin */
2206 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2207 * @ctx: ring buffer context.
2209 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2210 * -EIO for other errors, else returns 0.
2211 * It will take care of sub-buffer switching.
2213 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
2215 struct channel
*chan
= ctx
->chan
;
2216 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2217 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2218 struct lttng_ust_lib_ring_buffer
*buf
;
2219 struct switch_offsets offsets
;
2222 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2223 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
2225 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2233 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2235 if (caa_unlikely(ret
))
2237 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2242 * Atomically update last_tsc. This update races against concurrent
2243 * atomic updates, but the race will always cause supplementary full TSC
2244 * records, never the opposite (missing a full TSC record when it would
2247 save_last_tsc(config
, buf
, ctx
->tsc
);
2250 * Push the reader if necessary
2252 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2255 * Clear noref flag for this subbuffer.
2257 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2258 subbuf_index(offsets
.end
- 1, chan
),
2262 * Switch old subbuffer if needed.
2264 if (caa_unlikely(offsets
.switch_old_end
)) {
2265 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2266 subbuf_index(offsets
.old
- 1, chan
),
2268 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2272 * Populate new subbuffer.
2274 if (caa_unlikely(offsets
.switch_new_start
))
2275 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2277 if (caa_unlikely(offsets
.switch_new_end
))
2278 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2280 ctx
->slot_size
= offsets
.size
;
2281 ctx
->pre_offset
= offsets
.begin
;
2282 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2287 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
2288 struct lttng_ust_lib_ring_buffer
*buf
,
2289 unsigned long commit_count
,
2291 struct lttng_ust_shm_handle
*handle
)
2293 struct commit_counters_hot
*cc_hot
;
2295 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2297 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2300 v_set(config
, &cc_hot
->seq
, commit_count
);
2304 * The ring buffer can count events recorded and overwritten per buffer,
2305 * but it is disabled by default due to its performance overhead.
2307 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2309 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2310 struct lttng_ust_lib_ring_buffer
*buf
,
2312 struct lttng_ust_shm_handle
*handle
)
2314 v_add(config
, subbuffer_get_records_count(config
,
2315 &buf
->backend
, idx
, handle
),
2316 &buf
->records_count
);
2317 v_add(config
, subbuffer_count_records_overrun(config
,
2318 &buf
->backend
, idx
, handle
),
2319 &buf
->records_overrun
);
2321 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2323 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2324 struct lttng_ust_lib_ring_buffer
*buf
,
2326 struct lttng_ust_shm_handle
*handle
)
2329 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2331 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
2332 struct lttng_ust_lib_ring_buffer
*buf
,
2333 struct channel
*chan
,
2334 unsigned long offset
,
2335 unsigned long commit_count
,
2337 struct lttng_ust_shm_handle
*handle
,
2340 unsigned long old_commit_count
= commit_count
2341 - chan
->backend
.subbuf_size
;
2342 struct commit_counters_cold
*cc_cold
;
2345 * If we succeeded at updating cc_sb below, we are the subbuffer
2346 * writer delivering the subbuffer. Deals with concurrent
2347 * updates of the "cc" value without adding a add_return atomic
2348 * operation to the fast path.
2350 * We are doing the delivery in two steps:
2351 * - First, we cmpxchg() cc_sb to the new value
2352 * old_commit_count + 1. This ensures that we are the only
2353 * subbuffer user successfully filling the subbuffer, but we
2354 * do _not_ set the cc_sb value to "commit_count" yet.
2355 * Therefore, other writers that would wrap around the ring
2356 * buffer and try to start writing to our subbuffer would
2357 * have to drop records, because it would appear as
2359 * We therefore have exclusive access to the subbuffer control
2360 * structures. This mutual exclusion with other writers is
2361 * crucially important to perform record overruns count in
2362 * flight recorder mode locklessly.
2363 * - When we are ready to release the subbuffer (either for
2364 * reading or for overrun by other writers), we simply set the
2365 * cc_sb value to "commit_count" and perform delivery.
2367 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2368 * This guarantees that old_commit_count + 1 != commit_count.
2372 * Order prior updates to reserve count prior to the
2373 * commit_cold cc_sb update.
2376 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2379 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2380 old_commit_count
, old_commit_count
+ 1)
2381 == old_commit_count
)) {
2383 * Start of exclusive subbuffer access. We are
2384 * guaranteed to be the last writer in this subbuffer
2385 * and any other writer trying to access this subbuffer
2386 * in this state is required to drop records.
2388 deliver_count_events(config
, buf
, idx
, handle
);
2389 config
->cb
.buffer_end(buf
, tsc
, idx
,
2390 lib_ring_buffer_get_data_size(config
,
2397 * Increment the packet counter while we have exclusive
2400 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2403 * Set noref flag and offset for this subbuffer id.
2404 * Contains a memory barrier that ensures counter stores
2405 * are ordered before set noref and offset.
2407 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2408 buf_trunc_val(offset
, chan
), handle
);
2411 * Order set_noref and record counter updates before the
2412 * end of subbuffer exclusive access. Orders with
2413 * respect to writers coming into the subbuffer after
2414 * wrap around, and also order wrt concurrent readers.
2417 /* End of exclusive subbuffer access */
2418 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2420 * Order later updates to reserve count after
2421 * the commit cold cc_sb update.
2424 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2425 commit_count
, idx
, handle
);
2428 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2430 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2431 && uatomic_read(&buf
->active_readers
)
2432 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2433 lib_ring_buffer_wakeup(buf
, handle
);
2439 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2441 void lttng_fixup_ringbuffer_tls(void)
2443 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2446 void lib_ringbuffer_signal_init(void)
2452 * Block signal for entire process, so only our thread processes
2456 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2459 PERROR("pthread_sigmask");