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
55 #include <sys/types.h>
64 #include <urcu/compiler.h>
66 #include <urcu/tls-compat.h>
71 #include <lttng/ringbuffer-config.h>
77 #include "../liblttng-ust/compat.h" /* For ENODATA */
79 /* Print DBG() messages about events lost only every 1048576 hits */
80 #define DBG_PRINT_NR_LOST (1UL << 20)
82 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
83 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
84 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
85 #define CLOCKID CLOCK_MONOTONIC
86 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
87 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
88 #define RETRY_DELAY_MS 100 /* 100 ms. */
91 * Non-static to ensure the compiler does not optimize away the xor.
93 uint8_t lttng_crash_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
96 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
97 * close(2) to close the fd returned by shm_open.
98 * shm_unlink releases the shared memory object name.
99 * ftruncate(2) sets the size of the memory object.
100 * mmap/munmap maps the shared memory obj to a virtual address in the
101 * calling proceess (should be done both in libust and consumer).
102 * See shm_overview(7) for details.
103 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
106 * Since we don't need to access the object using its name, we can
107 * immediately shm_unlink(3) it, and only keep the handle with its file
112 * Internal structure representing offsets to use at a sub-buffer switch.
114 struct switch_offsets
{
115 unsigned long begin
, end
, old
;
116 size_t pre_header_padding
, size
;
117 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
121 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
124 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
127 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
130 void lib_ring_buffer_print_errors(struct channel
*chan
,
131 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
132 struct lttng_ust_shm_handle
*handle
);
135 * Handle timer teardown race wrt memory free of private data by
136 * ring buffer signals are handled by a single thread, which permits
137 * a synchronization point between handling of each signal.
138 * Protected by the lock within the structure.
140 struct timer_signal_data
{
141 pthread_t tid
; /* thread id managing signals */
144 pthread_mutex_t lock
;
147 static struct timer_signal_data timer_signal
= {
151 .lock
= PTHREAD_MUTEX_INITIALIZER
,
154 static bool lttng_ust_allow_blocking
;
156 void lttng_ust_ringbuffer_set_allow_blocking(void)
158 lttng_ust_allow_blocking
= true;
161 /* Get blocking timeout, in ms */
162 static int lttng_ust_ringbuffer_get_timeout(struct channel
*chan
)
164 if (!lttng_ust_allow_blocking
)
166 return chan
->u
.s
.blocking_timeout_ms
;
170 * lib_ring_buffer_reset - Reset ring buffer to initial values.
173 * Effectively empty the ring buffer. Should be called when the buffer is not
174 * used for writing. The ring buffer can be opened for reading, but the reader
175 * should not be using the iterator concurrently with reset. The previous
176 * current iterator record is reset.
178 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
179 struct lttng_ust_shm_handle
*handle
)
181 struct channel
*chan
;
182 const struct lttng_ust_lib_ring_buffer_config
*config
;
185 chan
= shmp(handle
, buf
->backend
.chan
);
188 config
= &chan
->backend
.config
;
190 * Reset iterator first. It will put the subbuffer if it currently holds
193 v_set(config
, &buf
->offset
, 0);
194 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
195 struct commit_counters_hot
*cc_hot
;
196 struct commit_counters_cold
*cc_cold
;
199 cc_hot
= shmp_index(handle
, buf
->commit_hot
, i
);
202 cc_cold
= shmp_index(handle
, buf
->commit_cold
, i
);
205 ts_end
= shmp_index(handle
, buf
->ts_end
, i
);
208 v_set(config
, &cc_hot
->cc
, 0);
209 v_set(config
, &cc_hot
->seq
, 0);
210 v_set(config
, &cc_cold
->cc_sb
, 0);
213 uatomic_set(&buf
->consumed
, 0);
214 uatomic_set(&buf
->record_disabled
, 0);
215 v_set(config
, &buf
->last_tsc
, 0);
216 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
217 /* Don't reset number of active readers */
218 v_set(config
, &buf
->records_lost_full
, 0);
219 v_set(config
, &buf
->records_lost_wrap
, 0);
220 v_set(config
, &buf
->records_lost_big
, 0);
221 v_set(config
, &buf
->records_count
, 0);
222 v_set(config
, &buf
->records_overrun
, 0);
227 * channel_reset - Reset channel to initial values.
230 * Effectively empty the channel. Should be called when the channel is not used
231 * for writing. The channel can be opened for reading, but the reader should not
232 * be using the iterator concurrently with reset. The previous current iterator
235 void channel_reset(struct channel
*chan
)
238 * Reset iterators first. Will put the subbuffer if held for reading.
240 uatomic_set(&chan
->record_disabled
, 0);
241 /* Don't reset commit_count_mask, still valid */
242 channel_backend_reset(&chan
->backend
);
243 /* Don't reset switch/read timer interval */
244 /* Don't reset notifiers and notifier enable bits */
245 /* Don't reset reader reference count */
249 void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config
*config
,
250 struct lttng_crash_abi
*crash_abi
,
251 struct lttng_ust_lib_ring_buffer
*buf
,
252 struct channel_backend
*chanb
,
253 struct shm_object
*shmobj
,
254 struct lttng_ust_shm_handle
*handle
)
258 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
259 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
260 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
261 crash_abi
->endian
= RB_CRASH_ENDIAN
;
262 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
263 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
264 crash_abi
->word_size
= sizeof(unsigned long);
265 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
267 /* Offset of fields */
268 crash_abi
->offset
.prod_offset
=
269 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
270 crash_abi
->offset
.consumed_offset
=
271 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
272 crash_abi
->offset
.commit_hot_array
=
273 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
274 crash_abi
->offset
.commit_hot_seq
=
275 offsetof(struct commit_counters_hot
, seq
);
276 crash_abi
->offset
.buf_wsb_array
=
277 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
278 crash_abi
->offset
.buf_wsb_id
=
279 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
, id
);
280 crash_abi
->offset
.sb_array
=
281 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
282 crash_abi
->offset
.sb_array_shmp_offset
=
283 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
,
285 crash_abi
->offset
.sb_backend_p_offset
=
286 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages
,
290 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
291 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
292 crash_abi
->length
.commit_hot_seq
=
293 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
294 crash_abi
->length
.buf_wsb_id
=
295 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer
*) NULL
)->id
);
296 crash_abi
->length
.sb_array_shmp_offset
=
297 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
298 crash_abi
->length
.sb_backend_p_offset
=
299 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
302 crash_abi
->stride
.commit_hot_array
=
303 sizeof(struct commit_counters_hot
);
304 crash_abi
->stride
.buf_wsb_array
=
305 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
);
306 crash_abi
->stride
.sb_array
=
307 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
);
309 /* Buffer constants */
310 crash_abi
->buf_size
= chanb
->buf_size
;
311 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
312 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
313 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
315 if (config
->cb
.content_size_field
) {
316 size_t offset
, length
;
318 config
->cb
.content_size_field(config
, &offset
, &length
);
319 crash_abi
->offset
.content_size
= offset
;
320 crash_abi
->length
.content_size
= length
;
322 crash_abi
->offset
.content_size
= 0;
323 crash_abi
->length
.content_size
= 0;
325 if (config
->cb
.packet_size_field
) {
326 size_t offset
, length
;
328 config
->cb
.packet_size_field(config
, &offset
, &length
);
329 crash_abi
->offset
.packet_size
= offset
;
330 crash_abi
->length
.packet_size
= length
;
332 crash_abi
->offset
.packet_size
= 0;
333 crash_abi
->length
.packet_size
= 0;
338 * Must be called under cpu hotplug protection.
340 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
341 struct channel_backend
*chanb
, int cpu
,
342 struct lttng_ust_shm_handle
*handle
,
343 struct shm_object
*shmobj
)
345 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
346 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
347 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
348 struct channel
*shmp_chan
;
349 struct commit_counters_hot
*cc_hot
;
350 void *priv
= channel_get_private(chan
);
351 size_t subbuf_header_size
;
355 /* Test for cpu hotplug */
356 if (buf
->backend
.allocated
)
359 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
360 set_shmp(buf
->commit_hot
,
362 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
363 if (!shmp(handle
, buf
->commit_hot
)) {
367 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
368 set_shmp(buf
->commit_cold
,
370 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
371 if (!shmp(handle
, buf
->commit_cold
)) {
376 align_shm(shmobj
, __alignof__(uint64_t));
377 set_shmp(buf
->ts_end
,
379 sizeof(uint64_t) * chan
->backend
.num_subbuf
));
380 if (!shmp(handle
, buf
->ts_end
)) {
382 goto free_commit_cold
;
386 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
387 cpu
, handle
, shmobj
);
393 * Write the subbuffer header for first subbuffer so we know the total
394 * duration of data gathering.
396 subbuf_header_size
= config
->cb
.subbuffer_header_size();
397 v_set(config
, &buf
->offset
, subbuf_header_size
);
398 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
403 subbuffer_id_clear_noref(config
, &wsb
->id
);
404 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
409 tsc
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
410 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
411 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
416 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
417 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
419 if (config
->cb
.buffer_create
) {
420 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
425 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
427 buf
->backend
.allocated
= 1;
432 /* ts_end will be freed by shm teardown */
434 /* commit_cold will be freed by shm teardown */
436 /* commit_hot will be freed by shm teardown */
442 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
444 const struct lttng_ust_lib_ring_buffer_config
*config
;
445 struct lttng_ust_shm_handle
*handle
;
446 struct channel
*chan
;
449 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
451 chan
= si
->si_value
.sival_ptr
;
452 handle
= chan
->handle
;
453 config
= &chan
->backend
.config
;
455 DBG("Switch timer for channel %p\n", chan
);
458 * Only flush buffers periodically if readers are active.
460 pthread_mutex_lock(&wakeup_fd_mutex
);
461 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
462 for_each_possible_cpu(cpu
) {
463 struct lttng_ust_lib_ring_buffer
*buf
=
464 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
468 if (uatomic_read(&buf
->active_readers
))
469 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
473 struct lttng_ust_lib_ring_buffer
*buf
=
474 shmp(handle
, chan
->backend
.buf
[0].shmp
);
478 if (uatomic_read(&buf
->active_readers
))
479 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
483 pthread_mutex_unlock(&wakeup_fd_mutex
);
488 int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
489 struct lttng_ust_lib_ring_buffer
*buf
,
490 struct channel
*chan
,
491 struct lttng_ust_shm_handle
*handle
)
493 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
494 struct commit_counters_cold
*cc_cold
;
496 consumed_old
= uatomic_read(&buf
->consumed
);
497 consumed_idx
= subbuf_index(consumed_old
, chan
);
498 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
501 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
503 * No memory barrier here, since we are only interested
504 * in a statistically correct polling result. The next poll will
505 * get the data is we are racing. The mb() that ensures correct
506 * memory order is in get_subbuf.
508 write_offset
= v_read(config
, &buf
->offset
);
511 * Check that the subbuffer we are trying to consume has been
512 * already fully committed.
515 if (((commit_count
- chan
->backend
.subbuf_size
)
516 & chan
->commit_count_mask
)
517 - (buf_trunc(consumed_old
, chan
)
518 >> chan
->backend
.num_subbuf_order
)
523 * Check that we are not about to read the same subbuffer in
524 * which the writer head is.
526 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
534 void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer
*buf
,
535 struct lttng_ust_shm_handle
*handle
)
537 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
538 sigset_t sigpipe_set
, pending_set
, old_set
;
539 int ret
, sigpipe_was_pending
= 0;
545 * Wake-up the other end by writing a null byte in the pipe
546 * (non-blocking). Important note: Because writing into the
547 * pipe is non-blocking (and therefore we allow dropping wakeup
548 * data, as long as there is wakeup data present in the pipe
549 * buffer to wake up the consumer), the consumer should perform
550 * the following sequence for waiting:
551 * 1) empty the pipe (reads).
552 * 2) check if there is data in the buffer.
553 * 3) wait on the pipe (poll).
555 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
556 * that might be already pending. If a bogus SIGPIPE is sent to
557 * the entire process concurrently by a malicious user, it may
558 * be simply discarded.
560 ret
= sigemptyset(&pending_set
);
563 * sigpending returns the mask of signals that are _both_
564 * blocked for the thread _and_ pending for either the thread or
565 * the entire process.
567 ret
= sigpending(&pending_set
);
569 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
571 * If sigpipe was pending, it means it was already blocked, so
572 * no need to block it.
574 if (!sigpipe_was_pending
) {
575 ret
= sigemptyset(&sigpipe_set
);
577 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
579 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
583 ret
= write(wakeup_fd
, "", 1);
584 } while (ret
== -1L && errno
== EINTR
);
585 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
586 struct timespec timeout
= { 0, 0 };
588 ret
= sigtimedwait(&sigpipe_set
, NULL
,
590 } while (ret
== -1L && errno
== EINTR
);
592 if (!sigpipe_was_pending
) {
593 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
599 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
601 const struct lttng_ust_lib_ring_buffer_config
*config
;
602 struct lttng_ust_shm_handle
*handle
;
605 handle
= chan
->handle
;
606 config
= &chan
->backend
.config
;
609 * Only flush buffers periodically if readers are active.
611 pthread_mutex_lock(&wakeup_fd_mutex
);
612 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
613 for_each_possible_cpu(cpu
) {
614 struct lttng_ust_lib_ring_buffer
*buf
=
615 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
619 if (uatomic_read(&buf
->active_readers
)
620 && lib_ring_buffer_poll_deliver(config
, buf
,
622 lib_ring_buffer_wakeup(buf
, handle
);
626 struct lttng_ust_lib_ring_buffer
*buf
=
627 shmp(handle
, chan
->backend
.buf
[0].shmp
);
631 if (uatomic_read(&buf
->active_readers
)
632 && lib_ring_buffer_poll_deliver(config
, buf
,
634 lib_ring_buffer_wakeup(buf
, handle
);
638 pthread_mutex_unlock(&wakeup_fd_mutex
);
642 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
644 struct channel
*chan
;
646 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
647 chan
= si
->si_value
.sival_ptr
;
648 DBG("Read timer for channel %p\n", chan
);
649 lib_ring_buffer_channel_do_read(chan
);
654 void rb_setmask(sigset_t
*mask
)
658 ret
= sigemptyset(mask
);
660 PERROR("sigemptyset");
662 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
666 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
670 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
677 void *sig_thread(void *arg
)
683 /* Only self thread will receive signal mask. */
685 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
688 signr
= sigwaitinfo(&mask
, &info
);
691 PERROR("sigwaitinfo");
694 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
695 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
697 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
698 lib_ring_buffer_channel_read_timer(info
.si_signo
,
700 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
702 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
705 ERR("Unexptected signal %d\n", info
.si_signo
);
712 * Ensure only a single thread listens on the timer signal.
715 void lib_ring_buffer_setup_timer_thread(void)
720 pthread_mutex_lock(&timer_signal
.lock
);
721 if (timer_signal
.setup_done
)
724 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
727 PERROR("pthread_create");
729 ret
= pthread_detach(thread
);
732 PERROR("pthread_detach");
734 timer_signal
.setup_done
= 1;
736 pthread_mutex_unlock(&timer_signal
.lock
);
740 * Wait for signal-handling thread quiescent state.
743 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
745 sigset_t pending_set
;
749 * We need to be the only thread interacting with the thread
750 * that manages signals for teardown synchronization.
752 pthread_mutex_lock(&timer_signal
.lock
);
755 * Ensure we don't have any signal queued for this channel.
758 ret
= sigemptyset(&pending_set
);
760 PERROR("sigemptyset");
762 ret
= sigpending(&pending_set
);
764 PERROR("sigpending");
766 if (!sigismember(&pending_set
, signr
))
772 * From this point, no new signal handler will be fired that
773 * would try to access "chan". However, we still need to wait
774 * for any currently executing handler to complete.
777 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
781 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
784 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
786 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
790 pthread_mutex_unlock(&timer_signal
.lock
);
794 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
797 struct itimerspec its
;
800 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
803 chan
->switch_timer_enabled
= 1;
805 lib_ring_buffer_setup_timer_thread();
807 sev
.sigev_notify
= SIGEV_SIGNAL
;
808 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
809 sev
.sigev_value
.sival_ptr
= chan
;
810 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
812 PERROR("timer_create");
815 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
816 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
817 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
818 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
820 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
822 PERROR("timer_settime");
827 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
831 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
834 ret
= timer_delete(chan
->switch_timer
);
836 PERROR("timer_delete");
839 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
841 chan
->switch_timer
= 0;
842 chan
->switch_timer_enabled
= 0;
846 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
848 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
850 struct itimerspec its
;
853 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
854 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
857 chan
->read_timer_enabled
= 1;
859 lib_ring_buffer_setup_timer_thread();
861 sev
.sigev_notify
= SIGEV_SIGNAL
;
862 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
863 sev
.sigev_value
.sival_ptr
= chan
;
864 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
866 PERROR("timer_create");
869 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
870 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
871 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
872 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
874 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
876 PERROR("timer_settime");
881 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
883 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
886 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
887 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
890 ret
= timer_delete(chan
->read_timer
);
892 PERROR("timer_delete");
896 * do one more check to catch data that has been written in the last
899 lib_ring_buffer_channel_do_read(chan
);
901 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
903 chan
->read_timer
= 0;
904 chan
->read_timer_enabled
= 0;
907 static void channel_unregister_notifiers(struct channel
*chan
,
908 struct lttng_ust_shm_handle
*handle
)
910 lib_ring_buffer_channel_switch_timer_stop(chan
);
911 lib_ring_buffer_channel_read_timer_stop(chan
);
914 static void channel_print_errors(struct channel
*chan
,
915 struct lttng_ust_shm_handle
*handle
)
917 const struct lttng_ust_lib_ring_buffer_config
*config
=
918 &chan
->backend
.config
;
921 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
922 for_each_possible_cpu(cpu
) {
923 struct lttng_ust_lib_ring_buffer
*buf
=
924 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
926 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
929 struct lttng_ust_lib_ring_buffer
*buf
=
930 shmp(handle
, chan
->backend
.buf
[0].shmp
);
933 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
937 static void channel_free(struct channel
*chan
,
938 struct lttng_ust_shm_handle
*handle
,
941 channel_backend_free(&chan
->backend
, handle
);
942 /* chan is freed by shm teardown */
943 shm_object_table_destroy(handle
->table
, consumer
);
948 * channel_create - Create channel.
949 * @config: ring buffer instance configuration
950 * @name: name of the channel
951 * @priv_data: ring buffer client private data area pointer (output)
952 * @priv_data_size: length, in bytes, of the private data area.
953 * @priv_data_init: initialization data for private data.
954 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
955 * address mapping. It is used only by RING_BUFFER_STATIC
956 * configuration. It can be set to NULL for other backends.
957 * @subbuf_size: subbuffer size
958 * @num_subbuf: number of subbuffers
959 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
960 * padding to let readers get those sub-buffers.
961 * Used for live streaming.
962 * @read_timer_interval: Time interval (in us) to wake up pending readers.
963 * @stream_fds: array of stream file descriptors.
964 * @nr_stream_fds: number of file descriptors in array.
967 * Returns NULL on failure.
969 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
972 size_t priv_data_align
,
973 size_t priv_data_size
,
974 void *priv_data_init
,
975 void *buf_addr
, size_t subbuf_size
,
976 size_t num_subbuf
, unsigned int switch_timer_interval
,
977 unsigned int read_timer_interval
,
978 const int *stream_fds
, int nr_stream_fds
,
979 int64_t blocking_timeout
)
982 size_t shmsize
, chansize
;
983 struct channel
*chan
;
984 struct lttng_ust_shm_handle
*handle
;
985 struct shm_object
*shmobj
;
986 unsigned int nr_streams
;
987 int64_t blocking_timeout_ms
;
989 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
990 nr_streams
= num_possible_cpus();
994 if (nr_stream_fds
!= nr_streams
)
997 if (blocking_timeout
< -1) {
1001 if (blocking_timeout
== -1) {
1002 blocking_timeout_ms
= -1;
1004 blocking_timeout_ms
= blocking_timeout
/ 1000;
1005 if (blocking_timeout_ms
!= (int32_t) blocking_timeout_ms
) {
1010 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
1011 read_timer_interval
))
1014 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1018 /* Allocate table for channel + per-cpu buffers */
1019 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1021 goto error_table_alloc
;
1023 /* Calculate the shm allocation layout */
1024 shmsize
= sizeof(struct channel
);
1025 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
1026 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
1028 if (priv_data_align
)
1029 shmsize
+= lttng_ust_offset_align(shmsize
, priv_data_align
);
1030 shmsize
+= priv_data_size
;
1032 /* Allocate normal memory for channel (not shared) */
1033 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
1037 /* struct channel is at object 0, offset 0 (hardcoded) */
1038 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
1039 assert(handle
->chan
._ref
.index
== 0);
1040 assert(handle
->chan
._ref
.offset
== 0);
1041 chan
= shmp(handle
, handle
->chan
);
1044 chan
->nr_streams
= nr_streams
;
1046 /* space for private data */
1047 if (priv_data_size
) {
1048 DECLARE_SHMP(void, priv_data_alloc
);
1050 align_shm(shmobj
, priv_data_align
);
1051 chan
->priv_data_offset
= shmobj
->allocated_len
;
1052 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1053 if (!shmp(handle
, priv_data_alloc
))
1055 *priv_data
= channel_get_private(chan
);
1056 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
1058 chan
->priv_data_offset
= -1;
1063 chan
->u
.s
.blocking_timeout_ms
= (int32_t) blocking_timeout_ms
;
1065 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1066 subbuf_size
, num_subbuf
, handle
,
1069 goto error_backend_init
;
1071 chan
->handle
= handle
;
1072 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1074 chan
->switch_timer_interval
= switch_timer_interval
;
1075 chan
->read_timer_interval
= read_timer_interval
;
1076 lib_ring_buffer_channel_switch_timer_start(chan
);
1077 lib_ring_buffer_channel_read_timer_start(chan
);
1083 shm_object_table_destroy(handle
->table
, 1);
1089 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1090 uint64_t memory_map_size
,
1093 struct lttng_ust_shm_handle
*handle
;
1094 struct shm_object
*object
;
1096 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1100 /* Allocate table for channel + per-cpu buffers */
1101 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1103 goto error_table_alloc
;
1104 /* Add channel object */
1105 object
= shm_object_table_append_mem(handle
->table
, data
,
1106 memory_map_size
, wakeup_fd
);
1108 goto error_table_object
;
1109 /* struct channel is at object 0, offset 0 (hardcoded) */
1110 handle
->chan
._ref
.index
= 0;
1111 handle
->chan
._ref
.offset
= 0;
1115 shm_object_table_destroy(handle
->table
, 0);
1121 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1122 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1123 uint64_t memory_map_size
)
1125 struct shm_object
*object
;
1127 /* Add stream object */
1128 object
= shm_object_table_append_shm(handle
->table
,
1129 shm_fd
, wakeup_fd
, stream_nr
,
1136 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1138 assert(handle
->table
);
1139 return handle
->table
->allocated_len
- 1;
1143 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1146 channel_free(chan
, handle
, consumer
);
1150 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1151 * @chan: channel to destroy
1153 * Holds cpu hotplug.
1154 * Call "destroy" callback, finalize channels, decrement the channel
1155 * reference count. Note that when readers have completed data
1156 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1157 * They should release their handle at that point.
1159 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1164 * Note: the consumer takes care of finalizing and
1165 * switching the buffers.
1167 channel_unregister_notifiers(chan
, handle
);
1169 * The consumer prints errors.
1171 channel_print_errors(chan
, handle
);
1175 * sessiond/consumer are keeping a reference on the shm file
1176 * descriptor directly. No need to refcount.
1178 channel_release(chan
, handle
, consumer
);
1182 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
1183 const struct lttng_ust_lib_ring_buffer_config
*config
,
1184 struct channel
*chan
, int cpu
,
1185 struct lttng_ust_shm_handle
*handle
,
1186 int *shm_fd
, int *wait_fd
,
1188 uint64_t *memory_map_size
)
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 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1200 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1201 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1202 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1204 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1207 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1208 struct channel
*chan
,
1209 struct lttng_ust_shm_handle
*handle
)
1211 struct shm_ref
*ref
;
1213 ref
= &handle
->chan
._ref
;
1214 return shm_close_wait_fd(handle
, ref
);
1217 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1218 struct channel
*chan
,
1219 struct lttng_ust_shm_handle
*handle
)
1221 struct shm_ref
*ref
;
1223 ref
= &handle
->chan
._ref
;
1224 return shm_close_wakeup_fd(handle
, ref
);
1227 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1228 struct channel
*chan
,
1229 struct lttng_ust_shm_handle
*handle
,
1232 struct shm_ref
*ref
;
1234 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1237 if (cpu
>= num_possible_cpus())
1240 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1241 return shm_close_wait_fd(handle
, ref
);
1244 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1245 struct channel
*chan
,
1246 struct lttng_ust_shm_handle
*handle
,
1249 struct shm_ref
*ref
;
1252 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1255 if (cpu
>= num_possible_cpus())
1258 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1259 pthread_mutex_lock(&wakeup_fd_mutex
);
1260 ret
= shm_close_wakeup_fd(handle
, ref
);
1261 pthread_mutex_unlock(&wakeup_fd_mutex
);
1265 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
1266 struct lttng_ust_shm_handle
*handle
)
1268 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1274 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
1275 struct lttng_ust_shm_handle
*handle
)
1277 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1281 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1283 uatomic_dec(&buf
->active_readers
);
1287 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1289 * @consumed: consumed count indicating the position where to read
1290 * @produced: produced count, indicates position when to stop reading
1292 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1293 * data to read at consumed position, or 0 if the get operation succeeds.
1296 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1297 unsigned long *consumed
, unsigned long *produced
,
1298 struct lttng_ust_shm_handle
*handle
)
1300 struct channel
*chan
;
1301 const struct lttng_ust_lib_ring_buffer_config
*config
;
1302 unsigned long consumed_cur
, write_offset
;
1305 chan
= shmp(handle
, buf
->backend
.chan
);
1308 config
= &chan
->backend
.config
;
1309 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1311 * Read finalized before counters.
1314 consumed_cur
= uatomic_read(&buf
->consumed
);
1316 * No need to issue a memory barrier between consumed count read and
1317 * write offset read, because consumed count can only change
1318 * concurrently in overwrite mode, and we keep a sequence counter
1319 * identifier derived from the write offset to check we are getting
1320 * the same sub-buffer we are expecting (the sub-buffers are atomically
1321 * "tagged" upon writes, tags are checked upon read).
1323 write_offset
= v_read(config
, &buf
->offset
);
1326 * Check that we are not about to read the same subbuffer in
1327 * which the writer head is.
1329 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1333 *consumed
= consumed_cur
;
1334 *produced
= subbuf_trunc(write_offset
, chan
);
1340 * The memory barriers __wait_event()/wake_up_interruptible() take care
1341 * of "raw_spin_is_locked" memory ordering.
1350 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1351 * are saved regardless of whether the consumed and produced positions are
1352 * in the same subbuffer.
1354 * @consumed: consumed byte count indicating the last position read
1355 * @produced: produced byte count indicating the last position written
1357 * This function is meant to provide information on the exact producer and
1358 * consumer positions without regard for the "snapshot" feature.
1360 int lib_ring_buffer_snapshot_sample_positions(
1361 struct lttng_ust_lib_ring_buffer
*buf
,
1362 unsigned long *consumed
, unsigned long *produced
,
1363 struct lttng_ust_shm_handle
*handle
)
1365 struct channel
*chan
;
1366 const struct lttng_ust_lib_ring_buffer_config
*config
;
1368 chan
= shmp(handle
, buf
->backend
.chan
);
1371 config
= &chan
->backend
.config
;
1373 *consumed
= uatomic_read(&buf
->consumed
);
1375 * No need to issue a memory barrier between consumed count read and
1376 * write offset read, because consumed count can only change
1377 * concurrently in overwrite mode, and we keep a sequence counter
1378 * identifier derived from the write offset to check we are getting
1379 * the same sub-buffer we are expecting (the sub-buffers are atomically
1380 * "tagged" upon writes, tags are checked upon read).
1382 *produced
= v_read(config
, &buf
->offset
);
1387 * lib_ring_buffer_move_consumer - move consumed counter forward
1389 * @consumed_new: new consumed count value
1391 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1392 unsigned long consumed_new
,
1393 struct lttng_ust_shm_handle
*handle
)
1395 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1396 struct channel
*chan
;
1397 unsigned long consumed
;
1399 chan
= shmp(handle
, bufb
->chan
);
1402 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1405 * Only push the consumed value forward.
1406 * If the consumed cmpxchg fails, this is because we have been pushed by
1407 * the writer in flight recorder mode.
1409 consumed
= uatomic_read(&buf
->consumed
);
1410 while ((long) consumed
- (long) consumed_new
< 0)
1411 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1416 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1418 * @consumed: consumed count indicating the position where to read
1420 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1421 * data to read at consumed position, or 0 if the get operation succeeds.
1423 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1424 unsigned long consumed
,
1425 struct lttng_ust_shm_handle
*handle
)
1427 struct channel
*chan
;
1428 const struct lttng_ust_lib_ring_buffer_config
*config
;
1429 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1430 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1431 struct commit_counters_cold
*cc_cold
;
1433 chan
= shmp(handle
, buf
->backend
.chan
);
1436 config
= &chan
->backend
.config
;
1438 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1440 * Read finalized before counters.
1443 consumed_cur
= uatomic_read(&buf
->consumed
);
1444 consumed_idx
= subbuf_index(consumed
, chan
);
1445 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1448 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1450 * Make sure we read the commit count before reading the buffer
1451 * data and the write offset. Correct consumed offset ordering
1452 * wrt commit count is insured by the use of cmpxchg to update
1453 * the consumed offset.
1456 * Local rmb to match the remote wmb to read the commit count
1457 * before the buffer data and the write offset.
1461 write_offset
= v_read(config
, &buf
->offset
);
1464 * Check that the buffer we are getting is after or at consumed_cur
1467 if ((long) subbuf_trunc(consumed
, chan
)
1468 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1472 * Check that the subbuffer we are trying to consume has been
1473 * already fully committed. There are a few causes that can make
1474 * this unavailability situation occur:
1476 * Temporary (short-term) situation:
1477 * - Application is running on a different CPU, between reserve
1478 * and commit ring buffer operations,
1479 * - Application is preempted between reserve and commit ring
1480 * buffer operations,
1482 * Long-term situation:
1483 * - Application is stopped (SIGSTOP) between reserve and commit
1484 * ring buffer operations. Could eventually be resumed by
1486 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1487 * reserve and commit ring buffer operation.
1489 * From a consumer perspective, handling short-term
1490 * unavailability situations is performed by retrying a few
1491 * times after a delay. Handling long-term unavailability
1492 * situations is handled by failing to get the sub-buffer.
1494 * In all of those situations, if the application is taking a
1495 * long time to perform its commit after ring buffer space
1496 * reservation, we can end up in a situation where the producer
1497 * will fill the ring buffer and try to write into the same
1498 * sub-buffer again (which has a missing commit). This is
1499 * handled by the producer in the sub-buffer switch handling
1500 * code of the reserve routine by detecting unbalanced
1501 * reserve/commit counters and discarding all further events
1502 * until the situation is resolved in those situations. Two
1503 * scenarios can occur:
1505 * 1) The application causing the reserve/commit counters to be
1506 * unbalanced has been terminated. In this situation, all
1507 * further events will be discarded in the buffers, and no
1508 * further buffer data will be readable by the consumer
1509 * daemon. Tearing down the UST tracing session and starting
1510 * anew is a work-around for those situations. Note that this
1511 * only affects per-UID tracing. In per-PID tracing, the
1512 * application vanishes with the termination, and therefore
1513 * no more data needs to be written to the buffers.
1514 * 2) The application causing the unbalance has been delayed for
1515 * a long time, but will eventually try to increment the
1516 * commit counter after eventually writing to the sub-buffer.
1517 * This situation can cause events to be discarded until the
1518 * application resumes its operations.
1520 if (((commit_count
- chan
->backend
.subbuf_size
)
1521 & chan
->commit_count_mask
)
1522 - (buf_trunc(consumed
, chan
)
1523 >> chan
->backend
.num_subbuf_order
)
1525 if (nr_retry
-- > 0) {
1526 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1527 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1535 * Check that we are not about to read the same subbuffer in
1536 * which the writer head is.
1538 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1543 * Failure to get the subbuffer causes a busy-loop retry without going
1544 * to a wait queue. These are caused by short-lived race windows where
1545 * the writer is getting access to a subbuffer we were trying to get
1546 * access to. Also checks that the "consumed" buffer count we are
1547 * looking for matches the one contained in the subbuffer id.
1549 * The short-lived race window described here can be affected by
1550 * application signals and preemption, thus requiring to bound
1551 * the loop to a maximum number of retry.
1553 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1554 consumed_idx
, buf_trunc_val(consumed
, chan
),
1557 if (nr_retry
-- > 0) {
1558 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1559 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1565 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1567 buf
->get_subbuf_consumed
= consumed
;
1568 buf
->get_subbuf
= 1;
1574 * The memory barriers __wait_event()/wake_up_interruptible() take care
1575 * of "raw_spin_is_locked" memory ordering.
1584 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1587 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1588 struct lttng_ust_shm_handle
*handle
)
1590 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1591 struct channel
*chan
;
1592 const struct lttng_ust_lib_ring_buffer_config
*config
;
1593 unsigned long sb_bindex
, consumed_idx
, consumed
;
1594 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
1595 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
1597 chan
= shmp(handle
, bufb
->chan
);
1600 config
= &chan
->backend
.config
;
1601 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1603 if (!buf
->get_subbuf
) {
1605 * Reader puts a subbuffer it did not get.
1607 CHAN_WARN_ON(chan
, 1);
1610 consumed
= buf
->get_subbuf_consumed
;
1611 buf
->get_subbuf
= 0;
1614 * Clear the records_unread counter. (overruns counter)
1615 * Can still be non-zero if a file reader simply grabbed the data
1616 * without using iterators.
1617 * Can be below zero if an iterator is used on a snapshot more than
1620 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1621 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1624 backend_pages
= shmp(handle
, rpages
->shmp
);
1627 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1628 &bufb
->records_read
);
1629 v_set(config
, &backend_pages
->records_unread
, 0);
1630 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1631 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1632 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1635 * Exchange the reader subbuffer with the one we put in its place in the
1636 * writer subbuffer table. Expect the original consumed count. If
1637 * update_read_sb_index fails, this is because the writer updated the
1638 * subbuffer concurrently. We should therefore keep the subbuffer we
1639 * currently have: it has become invalid to try reading this sub-buffer
1640 * consumed count value anyway.
1642 consumed_idx
= subbuf_index(consumed
, chan
);
1643 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1644 consumed_idx
, buf_trunc_val(consumed
, chan
),
1647 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1648 * if the writer concurrently updated it.
1653 * cons_offset is an iterator on all subbuffer offsets between the reader
1654 * position and the writer position. (inclusive)
1657 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1658 struct channel
*chan
,
1659 unsigned long cons_offset
,
1661 struct lttng_ust_shm_handle
*handle
)
1663 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1664 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1665 struct commit_counters_hot
*cc_hot
;
1666 struct commit_counters_cold
*cc_cold
;
1668 cons_idx
= subbuf_index(cons_offset
, chan
);
1669 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1672 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1675 commit_count
= v_read(config
, &cc_hot
->cc
);
1676 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1678 if (subbuf_offset(commit_count
, chan
) != 0)
1679 DBG("ring buffer %s, cpu %d: "
1680 "commit count in subbuffer %lu,\n"
1681 "expecting multiples of %lu bytes\n"
1682 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1683 chan
->backend
.name
, cpu
, cons_idx
,
1684 chan
->backend
.subbuf_size
,
1685 commit_count
, commit_count_sb
);
1687 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1688 chan
->backend
.name
, cpu
, commit_count
);
1692 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1693 struct channel
*chan
,
1694 void *priv
, int cpu
,
1695 struct lttng_ust_shm_handle
*handle
)
1697 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1698 unsigned long write_offset
, cons_offset
;
1701 * No need to order commit_count, write_offset and cons_offset reads
1702 * because we execute at teardown when no more writer nor reader
1703 * references are left.
1705 write_offset
= v_read(config
, &buf
->offset
);
1706 cons_offset
= uatomic_read(&buf
->consumed
);
1707 if (write_offset
!= cons_offset
)
1708 DBG("ring buffer %s, cpu %d: "
1709 "non-consumed data\n"
1710 " [ %lu bytes written, %lu bytes read ]\n",
1711 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1713 for (cons_offset
= uatomic_read(&buf
->consumed
);
1714 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1717 cons_offset
= subbuf_align(cons_offset
, chan
))
1718 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1723 void lib_ring_buffer_print_errors(struct channel
*chan
,
1724 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1725 struct lttng_ust_shm_handle
*handle
)
1727 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1728 void *priv
= channel_get_private(chan
);
1730 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1731 DBG("ring buffer %s: %lu records written, "
1732 "%lu records overrun\n",
1734 v_read(config
, &buf
->records_count
),
1735 v_read(config
, &buf
->records_overrun
));
1737 DBG("ring buffer %s, cpu %d: %lu records written, "
1738 "%lu records overrun\n",
1739 chan
->backend
.name
, cpu
,
1740 v_read(config
, &buf
->records_count
),
1741 v_read(config
, &buf
->records_overrun
));
1743 if (v_read(config
, &buf
->records_lost_full
)
1744 || v_read(config
, &buf
->records_lost_wrap
)
1745 || v_read(config
, &buf
->records_lost_big
))
1746 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1747 " [ %lu buffer full, %lu nest buffer wrap-around, "
1748 "%lu event too big ]\n",
1749 chan
->backend
.name
, cpu
,
1750 v_read(config
, &buf
->records_lost_full
),
1751 v_read(config
, &buf
->records_lost_wrap
),
1752 v_read(config
, &buf
->records_lost_big
));
1754 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1758 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1760 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1761 * active or at buffer finalization (destroy).
1764 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1765 struct channel
*chan
,
1766 struct switch_offsets
*offsets
,
1768 struct lttng_ust_shm_handle
*handle
)
1770 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1771 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1772 unsigned long commit_count
;
1773 struct commit_counters_hot
*cc_hot
;
1775 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1778 * Order all writes to buffer before the commit count update that will
1779 * determine that the subbuffer is full.
1782 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1785 v_add(config
, config
->cb
.subbuffer_header_size(),
1787 commit_count
= v_read(config
, &cc_hot
->cc
);
1788 /* Check if the written buffer has to be delivered */
1789 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1790 commit_count
, oldidx
, handle
, tsc
);
1791 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1792 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1793 commit_count
, handle
, cc_hot
);
1797 * lib_ring_buffer_switch_old_end: switch old subbuffer
1799 * Note : offset_old should never be 0 here. It is ok, because we never perform
1800 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1801 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1805 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1806 struct channel
*chan
,
1807 struct switch_offsets
*offsets
,
1809 struct lttng_ust_shm_handle
*handle
)
1811 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1812 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1813 unsigned long commit_count
, padding_size
, data_size
;
1814 struct commit_counters_hot
*cc_hot
;
1817 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1818 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1819 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1822 ts_end
= shmp_index(handle
, buf
->ts_end
, oldidx
);
1826 * This is the last space reservation in that sub-buffer before
1827 * it gets delivered. This provides exclusive access to write to
1828 * this sub-buffer's ts_end. There are also no concurrent
1829 * readers of that ts_end because delivery of that sub-buffer is
1830 * postponed until the commit counter is incremented for the
1831 * current space reservation.
1836 * Order all writes to buffer and store to ts_end before the commit
1837 * count update that will determine that the subbuffer is full.
1840 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1843 v_add(config
, padding_size
, &cc_hot
->cc
);
1844 commit_count
= v_read(config
, &cc_hot
->cc
);
1845 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1846 commit_count
, oldidx
, handle
, tsc
);
1847 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1848 offsets
->old
+ padding_size
, commit_count
, handle
,
1853 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1855 * This code can be executed unordered : writers may already have written to the
1856 * sub-buffer before this code gets executed, caution. The commit makes sure
1857 * that this code is executed before the deliver of this sub-buffer.
1860 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1861 struct channel
*chan
,
1862 struct switch_offsets
*offsets
,
1864 struct lttng_ust_shm_handle
*handle
)
1866 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1867 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1868 unsigned long commit_count
;
1869 struct commit_counters_hot
*cc_hot
;
1871 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1874 * Order all writes to buffer before the commit count update that will
1875 * determine that the subbuffer is full.
1878 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1881 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1882 commit_count
= v_read(config
, &cc_hot
->cc
);
1883 /* Check if the written buffer has to be delivered */
1884 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1885 commit_count
, beginidx
, handle
, tsc
);
1886 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1887 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1888 commit_count
, handle
, cc_hot
);
1892 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1894 * Calls subbuffer_set_data_size() to set the data size of the current
1895 * sub-buffer. We do not need to perform check_deliver nor commit here,
1896 * since this task will be done by the "commit" of the event for which
1897 * we are currently doing the space reservation.
1900 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1901 struct channel
*chan
,
1902 struct switch_offsets
*offsets
,
1904 struct lttng_ust_shm_handle
*handle
)
1906 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1907 unsigned long endidx
, data_size
;
1910 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1911 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1912 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1914 ts_end
= shmp_index(handle
, buf
->ts_end
, endidx
);
1918 * This is the last space reservation in that sub-buffer before
1919 * it gets delivered. This provides exclusive access to write to
1920 * this sub-buffer's ts_end. There are also no concurrent
1921 * readers of that ts_end because delivery of that sub-buffer is
1922 * postponed until the commit counter is incremented for the
1923 * current space reservation.
1931 * !0 if execution must be aborted.
1934 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1935 struct lttng_ust_lib_ring_buffer
*buf
,
1936 struct channel
*chan
,
1937 struct switch_offsets
*offsets
,
1939 struct lttng_ust_shm_handle
*handle
)
1941 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1942 unsigned long off
, reserve_commit_diff
;
1944 offsets
->begin
= v_read(config
, &buf
->offset
);
1945 offsets
->old
= offsets
->begin
;
1946 offsets
->switch_old_start
= 0;
1947 off
= subbuf_offset(offsets
->begin
, chan
);
1949 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1952 * Ensure we flush the header of an empty subbuffer when doing the
1953 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1954 * total data gathering duration even if there were no records saved
1955 * after the last buffer switch.
1956 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1957 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1958 * subbuffer header as appropriate.
1959 * The next record that reserves space will be responsible for
1960 * populating the following subbuffer header. We choose not to populate
1961 * the next subbuffer header here because we want to be able to use
1962 * SWITCH_ACTIVE for periodical buffer flush, which must
1963 * guarantee that all the buffer content (records and header
1964 * timestamps) are visible to the reader. This is required for
1965 * quiescence guarantees for the fusion merge.
1967 if (mode
!= SWITCH_FLUSH
&& !off
)
1968 return -1; /* we do not have to switch : buffer is empty */
1970 if (caa_unlikely(off
== 0)) {
1971 unsigned long sb_index
, commit_count
;
1972 struct commit_counters_cold
*cc_cold
;
1975 * We are performing a SWITCH_FLUSH. There may be concurrent
1976 * writes into the buffer if e.g. invoked while performing a
1977 * snapshot on an active trace.
1979 * If the client does not save any header information
1980 * (sub-buffer header size == 0), don't switch empty subbuffer
1981 * on finalize, because it is invalid to deliver a completely
1984 if (!config
->cb
.subbuffer_header_size())
1987 /* Test new buffer integrity */
1988 sb_index
= subbuf_index(offsets
->begin
, chan
);
1989 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1992 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1993 reserve_commit_diff
=
1994 (buf_trunc(offsets
->begin
, chan
)
1995 >> chan
->backend
.num_subbuf_order
)
1996 - (commit_count
& chan
->commit_count_mask
);
1997 if (caa_likely(reserve_commit_diff
== 0)) {
1998 /* Next subbuffer not being written to. */
1999 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2000 subbuf_trunc(offsets
->begin
, chan
)
2001 - subbuf_trunc((unsigned long)
2002 uatomic_read(&buf
->consumed
), chan
)
2003 >= chan
->backend
.buf_size
)) {
2005 * We do not overwrite non consumed buffers
2006 * and we are full : don't switch.
2011 * Next subbuffer not being written to, and we
2012 * are either in overwrite mode or the buffer is
2013 * not full. It's safe to write in this new
2019 * Next subbuffer reserve offset does not match the
2020 * commit offset. Don't perform switch in
2021 * producer-consumer and overwrite mode. Caused by
2022 * either a writer OOPS or too many nested writes over a
2023 * reserve/commit pair.
2029 * Need to write the subbuffer start header on finalize.
2031 offsets
->switch_old_start
= 1;
2033 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2034 /* Note: old points to the next subbuf at offset 0 */
2035 offsets
->end
= offsets
->begin
;
2040 * Force a sub-buffer switch. This operation is completely reentrant : can be
2041 * called while tracing is active with absolutely no lock held.
2043 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2044 * some atomic operations, this function must be called from the CPU
2045 * which owns the buffer for a ACTIVE flush. However, for
2046 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2049 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
2050 struct lttng_ust_shm_handle
*handle
)
2052 struct channel
*chan
;
2053 const struct lttng_ust_lib_ring_buffer_config
*config
;
2054 struct switch_offsets offsets
;
2055 unsigned long oldidx
;
2058 chan
= shmp(handle
, buf
->backend
.chan
);
2061 config
= &chan
->backend
.config
;
2066 * Perform retryable operations.
2069 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
2071 return; /* Switch not needed */
2072 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
2076 * Atomically update last_tsc. This update races against concurrent
2077 * atomic updates, but the race will always cause supplementary full TSC
2078 * records, never the opposite (missing a full TSC record when it would
2081 save_last_tsc(config
, buf
, tsc
);
2084 * Push the reader if necessary
2086 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
2088 oldidx
= subbuf_index(offsets
.old
, chan
);
2089 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
2092 * May need to populate header start on SWITCH_FLUSH.
2094 if (offsets
.switch_old_start
) {
2095 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
2096 offsets
.old
+= config
->cb
.subbuffer_header_size();
2100 * Switch old subbuffer.
2102 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
2106 bool handle_blocking_retry(int *timeout_left_ms
)
2108 int timeout
= *timeout_left_ms
, delay
;
2110 if (caa_likely(!timeout
))
2111 return false; /* Do not retry, discard event. */
2112 if (timeout
< 0) /* Wait forever. */
2113 delay
= RETRY_DELAY_MS
;
2115 delay
= min_t(int, timeout
, RETRY_DELAY_MS
);
2116 (void) poll(NULL
, 0, delay
);
2118 *timeout_left_ms
-= delay
;
2119 return true; /* Retry. */
2125 * -ENOSPC if event size is too large for packet.
2126 * -ENOBUFS if there is currently not enough space in buffer for the event.
2127 * -EIO if data cannot be written into the buffer for any other reason.
2130 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
2131 struct channel
*chan
,
2132 struct switch_offsets
*offsets
,
2133 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2136 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2137 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2138 unsigned long reserve_commit_diff
, offset_cmp
;
2139 int timeout_left_ms
= lttng_ust_ringbuffer_get_timeout(chan
);
2142 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2143 offsets
->old
= offsets
->begin
;
2144 offsets
->switch_new_start
= 0;
2145 offsets
->switch_new_end
= 0;
2146 offsets
->switch_old_end
= 0;
2147 offsets
->pre_header_padding
= 0;
2149 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2150 if ((int64_t) ctx
->tsc
== -EIO
)
2153 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
2154 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2156 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
2157 offsets
->switch_new_start
= 1; /* For offsets->begin */
2159 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2161 &offsets
->pre_header_padding
,
2164 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2167 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2168 offsets
->size
> chan
->backend
.subbuf_size
)) {
2169 offsets
->switch_old_end
= 1; /* For offsets->old */
2170 offsets
->switch_new_start
= 1; /* For offsets->begin */
2173 if (caa_unlikely(offsets
->switch_new_start
)) {
2174 unsigned long sb_index
, commit_count
;
2175 struct commit_counters_cold
*cc_cold
;
2178 * We are typically not filling the previous buffer completely.
2180 if (caa_likely(offsets
->switch_old_end
))
2181 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2182 offsets
->begin
= offsets
->begin
2183 + config
->cb
.subbuffer_header_size();
2184 /* Test new buffer integrity */
2185 sb_index
= subbuf_index(offsets
->begin
, chan
);
2187 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2188 * lib_ring_buffer_check_deliver() has the matching
2189 * memory barriers required around commit_cold cc_sb
2190 * updates to ensure reserve and commit counter updates
2191 * are not seen reordered when updated by another CPU.
2194 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2197 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2198 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2200 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2202 * The reserve counter have been concurrently updated
2203 * while we read the commit counter. This means the
2204 * commit counter we read might not match buf->offset
2205 * due to concurrent update. We therefore need to retry.
2209 reserve_commit_diff
=
2210 (buf_trunc(offsets
->begin
, chan
)
2211 >> chan
->backend
.num_subbuf_order
)
2212 - (commit_count
& chan
->commit_count_mask
);
2213 if (caa_likely(reserve_commit_diff
== 0)) {
2214 /* Next subbuffer not being written to. */
2215 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2216 subbuf_trunc(offsets
->begin
, chan
)
2217 - subbuf_trunc((unsigned long)
2218 uatomic_read(&buf
->consumed
), chan
)
2219 >= chan
->backend
.buf_size
)) {
2220 unsigned long nr_lost
;
2222 if (handle_blocking_retry(&timeout_left_ms
))
2226 * We do not overwrite non consumed buffers
2227 * and we are full : record is lost.
2229 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2230 v_inc(config
, &buf
->records_lost_full
);
2231 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2232 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2233 nr_lost
+ 1, chan
->backend
.name
,
2239 * Next subbuffer not being written to, and we
2240 * are either in overwrite mode or the buffer is
2241 * not full. It's safe to write in this new
2246 unsigned long nr_lost
;
2249 * Next subbuffer reserve offset does not match the
2250 * commit offset, and this did not involve update to the
2251 * reserve counter. Drop record in producer-consumer and
2252 * overwrite mode. Caused by either a writer OOPS or too
2253 * many nested writes over a reserve/commit pair.
2255 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2256 v_inc(config
, &buf
->records_lost_wrap
);
2257 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2258 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2259 nr_lost
+ 1, chan
->backend
.name
,
2265 config
->cb
.record_header_size(config
, chan
,
2267 &offsets
->pre_header_padding
,
2270 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2273 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2274 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2275 unsigned long nr_lost
;
2278 * Record too big for subbuffers, report error, don't
2279 * complete the sub-buffer switch.
2281 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2282 v_inc(config
, &buf
->records_lost_big
);
2283 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2284 DBG("%lu or more records lost in (%s:%d) record size "
2285 " of %zu bytes is too large for buffer\n",
2286 nr_lost
+ 1, chan
->backend
.name
,
2287 buf
->backend
.cpu
, offsets
->size
);
2292 * We just made a successful buffer switch and the
2293 * record fits in the new subbuffer. Let's write.
2298 * Record fits in the current buffer and we are not on a switch
2299 * boundary. It's safe to write.
2302 offsets
->end
= offsets
->begin
+ offsets
->size
;
2304 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2306 * The offset_end will fall at the very beginning of the next
2309 offsets
->switch_new_end
= 1; /* For offsets->begin */
2315 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2316 * @ctx: ring buffer context.
2318 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2319 * -EIO for other errors, else returns 0.
2320 * It will take care of sub-buffer switching.
2322 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
2325 struct channel
*chan
= ctx
->chan
;
2326 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2327 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2328 struct lttng_ust_lib_ring_buffer
*buf
;
2329 struct switch_offsets offsets
;
2332 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2333 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
2335 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2343 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2345 if (caa_unlikely(ret
))
2347 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2352 * Atomically update last_tsc. This update races against concurrent
2353 * atomic updates, but the race will always cause supplementary full TSC
2354 * records, never the opposite (missing a full TSC record when it would
2357 save_last_tsc(config
, buf
, ctx
->tsc
);
2360 * Push the reader if necessary
2362 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2365 * Clear noref flag for this subbuffer.
2367 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2368 subbuf_index(offsets
.end
- 1, chan
),
2372 * Switch old subbuffer if needed.
2374 if (caa_unlikely(offsets
.switch_old_end
)) {
2375 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2376 subbuf_index(offsets
.old
- 1, chan
),
2378 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2382 * Populate new subbuffer.
2384 if (caa_unlikely(offsets
.switch_new_start
))
2385 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2387 if (caa_unlikely(offsets
.switch_new_end
))
2388 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2390 ctx
->slot_size
= offsets
.size
;
2391 ctx
->pre_offset
= offsets
.begin
;
2392 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2397 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
2398 struct lttng_ust_lib_ring_buffer
*buf
,
2399 unsigned long commit_count
,
2401 struct lttng_ust_shm_handle
*handle
)
2403 struct commit_counters_hot
*cc_hot
;
2405 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2407 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2410 v_set(config
, &cc_hot
->seq
, commit_count
);
2414 * The ring buffer can count events recorded and overwritten per buffer,
2415 * but it is disabled by default due to its performance overhead.
2417 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2419 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2420 struct lttng_ust_lib_ring_buffer
*buf
,
2422 struct lttng_ust_shm_handle
*handle
)
2424 v_add(config
, subbuffer_get_records_count(config
,
2425 &buf
->backend
, idx
, handle
),
2426 &buf
->records_count
);
2427 v_add(config
, subbuffer_count_records_overrun(config
,
2428 &buf
->backend
, idx
, handle
),
2429 &buf
->records_overrun
);
2431 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2433 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2434 struct lttng_ust_lib_ring_buffer
*buf
,
2436 struct lttng_ust_shm_handle
*handle
)
2439 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2441 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
2442 struct lttng_ust_lib_ring_buffer
*buf
,
2443 struct channel
*chan
,
2444 unsigned long offset
,
2445 unsigned long commit_count
,
2447 struct lttng_ust_shm_handle
*handle
,
2450 unsigned long old_commit_count
= commit_count
2451 - chan
->backend
.subbuf_size
;
2452 struct commit_counters_cold
*cc_cold
;
2455 * If we succeeded at updating cc_sb below, we are the subbuffer
2456 * writer delivering the subbuffer. Deals with concurrent
2457 * updates of the "cc" value without adding a add_return atomic
2458 * operation to the fast path.
2460 * We are doing the delivery in two steps:
2461 * - First, we cmpxchg() cc_sb to the new value
2462 * old_commit_count + 1. This ensures that we are the only
2463 * subbuffer user successfully filling the subbuffer, but we
2464 * do _not_ set the cc_sb value to "commit_count" yet.
2465 * Therefore, other writers that would wrap around the ring
2466 * buffer and try to start writing to our subbuffer would
2467 * have to drop records, because it would appear as
2469 * We therefore have exclusive access to the subbuffer control
2470 * structures. This mutual exclusion with other writers is
2471 * crucially important to perform record overruns count in
2472 * flight recorder mode locklessly.
2473 * - When we are ready to release the subbuffer (either for
2474 * reading or for overrun by other writers), we simply set the
2475 * cc_sb value to "commit_count" and perform delivery.
2477 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2478 * This guarantees that old_commit_count + 1 != commit_count.
2482 * Order prior updates to reserve count prior to the
2483 * commit_cold cc_sb update.
2486 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2489 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2490 old_commit_count
, old_commit_count
+ 1)
2491 == old_commit_count
)) {
2495 * Start of exclusive subbuffer access. We are
2496 * guaranteed to be the last writer in this subbuffer
2497 * and any other writer trying to access this subbuffer
2498 * in this state is required to drop records.
2500 * We can read the ts_end for the current sub-buffer
2501 * which has been saved by the very last space
2502 * reservation for the current sub-buffer.
2504 * Order increment of commit counter before reading ts_end.
2507 ts_end
= shmp_index(handle
, buf
->ts_end
, idx
);
2510 deliver_count_events(config
, buf
, idx
, handle
);
2511 config
->cb
.buffer_end(buf
, *ts_end
, idx
,
2512 lib_ring_buffer_get_data_size(config
,
2519 * Increment the packet counter while we have exclusive
2522 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2525 * Set noref flag and offset for this subbuffer id.
2526 * Contains a memory barrier that ensures counter stores
2527 * are ordered before set noref and offset.
2529 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2530 buf_trunc_val(offset
, chan
), handle
);
2533 * Order set_noref and record counter updates before the
2534 * end of subbuffer exclusive access. Orders with
2535 * respect to writers coming into the subbuffer after
2536 * wrap around, and also order wrt concurrent readers.
2539 /* End of exclusive subbuffer access */
2540 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2542 * Order later updates to reserve count after
2543 * the commit cold cc_sb update.
2546 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2547 commit_count
, idx
, handle
);
2550 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2552 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2553 && uatomic_read(&buf
->active_readers
)
2554 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2555 lib_ring_buffer_wakeup(buf
, handle
);
2561 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2563 void lttng_fixup_ringbuffer_tls(void)
2565 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2568 void lib_ringbuffer_signal_init(void)
2574 * Block signal for entire process, so only our thread processes
2578 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2581 PERROR("pthread_sigmask");