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>
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 int lttng_ust_blocking_retry_timeout
=
155 CONFIG_LTTNG_UST_DEFAULT_BLOCKING_RETRY_TIMEOUT_MS
;
157 void lttng_ust_ringbuffer_set_retry_timeout(int timeout
)
159 lttng_ust_blocking_retry_timeout
= timeout
;
163 * lib_ring_buffer_reset - Reset ring buffer to initial values.
166 * Effectively empty the ring buffer. Should be called when the buffer is not
167 * used for writing. The ring buffer can be opened for reading, but the reader
168 * should not be using the iterator concurrently with reset. The previous
169 * current iterator record is reset.
171 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
172 struct lttng_ust_shm_handle
*handle
)
174 struct channel
*chan
;
175 const struct lttng_ust_lib_ring_buffer_config
*config
;
178 chan
= shmp(handle
, buf
->backend
.chan
);
181 config
= &chan
->backend
.config
;
183 * Reset iterator first. It will put the subbuffer if it currently holds
186 v_set(config
, &buf
->offset
, 0);
187 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
188 struct commit_counters_hot
*cc_hot
;
189 struct commit_counters_cold
*cc_cold
;
191 cc_hot
= shmp_index(handle
, buf
->commit_hot
, i
);
194 cc_cold
= shmp_index(handle
, buf
->commit_cold
, i
);
197 v_set(config
, &cc_hot
->cc
, 0);
198 v_set(config
, &cc_hot
->seq
, 0);
199 v_set(config
, &cc_cold
->cc_sb
, 0);
201 uatomic_set(&buf
->consumed
, 0);
202 uatomic_set(&buf
->record_disabled
, 0);
203 v_set(config
, &buf
->last_tsc
, 0);
204 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
205 /* Don't reset number of active readers */
206 v_set(config
, &buf
->records_lost_full
, 0);
207 v_set(config
, &buf
->records_lost_wrap
, 0);
208 v_set(config
, &buf
->records_lost_big
, 0);
209 v_set(config
, &buf
->records_count
, 0);
210 v_set(config
, &buf
->records_overrun
, 0);
215 * channel_reset - Reset channel to initial values.
218 * Effectively empty the channel. Should be called when the channel is not used
219 * for writing. The channel can be opened for reading, but the reader should not
220 * be using the iterator concurrently with reset. The previous current iterator
223 void channel_reset(struct channel
*chan
)
226 * Reset iterators first. Will put the subbuffer if held for reading.
228 uatomic_set(&chan
->record_disabled
, 0);
229 /* Don't reset commit_count_mask, still valid */
230 channel_backend_reset(&chan
->backend
);
231 /* Don't reset switch/read timer interval */
232 /* Don't reset notifiers and notifier enable bits */
233 /* Don't reset reader reference count */
237 void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config
*config
,
238 struct lttng_crash_abi
*crash_abi
,
239 struct lttng_ust_lib_ring_buffer
*buf
,
240 struct channel_backend
*chanb
,
241 struct shm_object
*shmobj
,
242 struct lttng_ust_shm_handle
*handle
)
246 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
247 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
248 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
249 crash_abi
->endian
= RB_CRASH_ENDIAN
;
250 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
251 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
252 crash_abi
->word_size
= sizeof(unsigned long);
253 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
255 /* Offset of fields */
256 crash_abi
->offset
.prod_offset
=
257 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
258 crash_abi
->offset
.consumed_offset
=
259 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
260 crash_abi
->offset
.commit_hot_array
=
261 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
262 crash_abi
->offset
.commit_hot_seq
=
263 offsetof(struct commit_counters_hot
, seq
);
264 crash_abi
->offset
.buf_wsb_array
=
265 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
266 crash_abi
->offset
.buf_wsb_id
=
267 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
, id
);
268 crash_abi
->offset
.sb_array
=
269 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
270 crash_abi
->offset
.sb_array_shmp_offset
=
271 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
,
273 crash_abi
->offset
.sb_backend_p_offset
=
274 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages
,
278 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
279 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
280 crash_abi
->length
.commit_hot_seq
=
281 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
282 crash_abi
->length
.buf_wsb_id
=
283 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer
*) NULL
)->id
);
284 crash_abi
->length
.sb_array_shmp_offset
=
285 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
286 crash_abi
->length
.sb_backend_p_offset
=
287 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
290 crash_abi
->stride
.commit_hot_array
=
291 sizeof(struct commit_counters_hot
);
292 crash_abi
->stride
.buf_wsb_array
=
293 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
);
294 crash_abi
->stride
.sb_array
=
295 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
);
297 /* Buffer constants */
298 crash_abi
->buf_size
= chanb
->buf_size
;
299 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
300 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
301 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
303 if (config
->cb
.content_size_field
) {
304 size_t offset
, length
;
306 config
->cb
.content_size_field(config
, &offset
, &length
);
307 crash_abi
->offset
.content_size
= offset
;
308 crash_abi
->length
.content_size
= length
;
310 crash_abi
->offset
.content_size
= 0;
311 crash_abi
->length
.content_size
= 0;
313 if (config
->cb
.packet_size_field
) {
314 size_t offset
, length
;
316 config
->cb
.packet_size_field(config
, &offset
, &length
);
317 crash_abi
->offset
.packet_size
= offset
;
318 crash_abi
->length
.packet_size
= length
;
320 crash_abi
->offset
.packet_size
= 0;
321 crash_abi
->length
.packet_size
= 0;
326 * Must be called under cpu hotplug protection.
328 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
329 struct channel_backend
*chanb
, int cpu
,
330 struct lttng_ust_shm_handle
*handle
,
331 struct shm_object
*shmobj
)
333 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
334 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
335 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
336 struct channel
*shmp_chan
;
337 struct commit_counters_hot
*cc_hot
;
338 void *priv
= channel_get_private(chan
);
339 size_t subbuf_header_size
;
343 /* Test for cpu hotplug */
344 if (buf
->backend
.allocated
)
347 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
348 set_shmp(buf
->commit_hot
,
350 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
351 if (!shmp(handle
, buf
->commit_hot
)) {
355 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
356 set_shmp(buf
->commit_cold
,
358 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
359 if (!shmp(handle
, buf
->commit_cold
)) {
364 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
365 cpu
, handle
, shmobj
);
371 * Write the subbuffer header for first subbuffer so we know the total
372 * duration of data gathering.
374 subbuf_header_size
= config
->cb
.subbuffer_header_size();
375 v_set(config
, &buf
->offset
, subbuf_header_size
);
376 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
381 subbuffer_id_clear_noref(config
, &wsb
->id
);
382 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
387 tsc
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
388 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
389 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
394 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
395 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
397 if (config
->cb
.buffer_create
) {
398 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
403 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
405 buf
->backend
.allocated
= 1;
410 /* commit_cold will be freed by shm teardown */
412 /* commit_hot will be freed by shm teardown */
418 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
420 const struct lttng_ust_lib_ring_buffer_config
*config
;
421 struct lttng_ust_shm_handle
*handle
;
422 struct channel
*chan
;
425 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
427 chan
= si
->si_value
.sival_ptr
;
428 handle
= chan
->handle
;
429 config
= &chan
->backend
.config
;
431 DBG("Switch timer for channel %p\n", chan
);
434 * Only flush buffers periodically if readers are active.
436 pthread_mutex_lock(&wakeup_fd_mutex
);
437 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
438 for_each_possible_cpu(cpu
) {
439 struct lttng_ust_lib_ring_buffer
*buf
=
440 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
444 if (uatomic_read(&buf
->active_readers
))
445 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
449 struct lttng_ust_lib_ring_buffer
*buf
=
450 shmp(handle
, chan
->backend
.buf
[0].shmp
);
454 if (uatomic_read(&buf
->active_readers
))
455 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
459 pthread_mutex_unlock(&wakeup_fd_mutex
);
464 int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
465 struct lttng_ust_lib_ring_buffer
*buf
,
466 struct channel
*chan
,
467 struct lttng_ust_shm_handle
*handle
)
469 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
470 struct commit_counters_cold
*cc_cold
;
472 consumed_old
= uatomic_read(&buf
->consumed
);
473 consumed_idx
= subbuf_index(consumed_old
, chan
);
474 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
477 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
479 * No memory barrier here, since we are only interested
480 * in a statistically correct polling result. The next poll will
481 * get the data is we are racing. The mb() that ensures correct
482 * memory order is in get_subbuf.
484 write_offset
= v_read(config
, &buf
->offset
);
487 * Check that the subbuffer we are trying to consume has been
488 * already fully committed.
491 if (((commit_count
- chan
->backend
.subbuf_size
)
492 & chan
->commit_count_mask
)
493 - (buf_trunc(consumed_old
, chan
)
494 >> chan
->backend
.num_subbuf_order
)
499 * Check that we are not about to read the same subbuffer in
500 * which the writer head is.
502 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
510 void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer
*buf
,
511 struct lttng_ust_shm_handle
*handle
)
513 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
514 sigset_t sigpipe_set
, pending_set
, old_set
;
515 int ret
, sigpipe_was_pending
= 0;
521 * Wake-up the other end by writing a null byte in the pipe
522 * (non-blocking). Important note: Because writing into the
523 * pipe is non-blocking (and therefore we allow dropping wakeup
524 * data, as long as there is wakeup data present in the pipe
525 * buffer to wake up the consumer), the consumer should perform
526 * the following sequence for waiting:
527 * 1) empty the pipe (reads).
528 * 2) check if there is data in the buffer.
529 * 3) wait on the pipe (poll).
531 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
532 * that might be already pending. If a bogus SIGPIPE is sent to
533 * the entire process concurrently by a malicious user, it may
534 * be simply discarded.
536 ret
= sigemptyset(&pending_set
);
539 * sigpending returns the mask of signals that are _both_
540 * blocked for the thread _and_ pending for either the thread or
541 * the entire process.
543 ret
= sigpending(&pending_set
);
545 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
547 * If sigpipe was pending, it means it was already blocked, so
548 * no need to block it.
550 if (!sigpipe_was_pending
) {
551 ret
= sigemptyset(&sigpipe_set
);
553 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
555 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
559 ret
= write(wakeup_fd
, "", 1);
560 } while (ret
== -1L && errno
== EINTR
);
561 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
562 struct timespec timeout
= { 0, 0 };
564 ret
= sigtimedwait(&sigpipe_set
, NULL
,
566 } while (ret
== -1L && errno
== EINTR
);
568 if (!sigpipe_was_pending
) {
569 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
575 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
577 const struct lttng_ust_lib_ring_buffer_config
*config
;
578 struct lttng_ust_shm_handle
*handle
;
581 handle
= chan
->handle
;
582 config
= &chan
->backend
.config
;
585 * Only flush buffers periodically if readers are active.
587 pthread_mutex_lock(&wakeup_fd_mutex
);
588 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
589 for_each_possible_cpu(cpu
) {
590 struct lttng_ust_lib_ring_buffer
*buf
=
591 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
595 if (uatomic_read(&buf
->active_readers
)
596 && lib_ring_buffer_poll_deliver(config
, buf
,
598 lib_ring_buffer_wakeup(buf
, handle
);
602 struct lttng_ust_lib_ring_buffer
*buf
=
603 shmp(handle
, chan
->backend
.buf
[0].shmp
);
607 if (uatomic_read(&buf
->active_readers
)
608 && lib_ring_buffer_poll_deliver(config
, buf
,
610 lib_ring_buffer_wakeup(buf
, handle
);
614 pthread_mutex_unlock(&wakeup_fd_mutex
);
618 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
620 struct channel
*chan
;
622 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
623 chan
= si
->si_value
.sival_ptr
;
624 DBG("Read timer for channel %p\n", chan
);
625 lib_ring_buffer_channel_do_read(chan
);
630 void rb_setmask(sigset_t
*mask
)
634 ret
= sigemptyset(mask
);
636 PERROR("sigemptyset");
638 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
642 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
646 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
653 void *sig_thread(void *arg
)
659 /* Only self thread will receive signal mask. */
661 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
664 signr
= sigwaitinfo(&mask
, &info
);
667 PERROR("sigwaitinfo");
670 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
671 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
673 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
674 lib_ring_buffer_channel_read_timer(info
.si_signo
,
676 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
678 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
681 ERR("Unexptected signal %d\n", info
.si_signo
);
688 * Ensure only a single thread listens on the timer signal.
691 void lib_ring_buffer_setup_timer_thread(void)
696 pthread_mutex_lock(&timer_signal
.lock
);
697 if (timer_signal
.setup_done
)
700 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
703 PERROR("pthread_create");
705 ret
= pthread_detach(thread
);
708 PERROR("pthread_detach");
710 timer_signal
.setup_done
= 1;
712 pthread_mutex_unlock(&timer_signal
.lock
);
716 * Wait for signal-handling thread quiescent state.
719 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
721 sigset_t pending_set
;
725 * We need to be the only thread interacting with the thread
726 * that manages signals for teardown synchronization.
728 pthread_mutex_lock(&timer_signal
.lock
);
731 * Ensure we don't have any signal queued for this channel.
734 ret
= sigemptyset(&pending_set
);
736 PERROR("sigemptyset");
738 ret
= sigpending(&pending_set
);
740 PERROR("sigpending");
742 if (!sigismember(&pending_set
, signr
))
748 * From this point, no new signal handler will be fired that
749 * would try to access "chan". However, we still need to wait
750 * for any currently executing handler to complete.
753 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
757 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
760 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
762 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
766 pthread_mutex_unlock(&timer_signal
.lock
);
770 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
773 struct itimerspec its
;
776 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
779 chan
->switch_timer_enabled
= 1;
781 lib_ring_buffer_setup_timer_thread();
783 sev
.sigev_notify
= SIGEV_SIGNAL
;
784 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
785 sev
.sigev_value
.sival_ptr
= chan
;
786 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
788 PERROR("timer_create");
791 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
792 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
793 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
794 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
796 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
798 PERROR("timer_settime");
803 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
807 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
810 ret
= timer_delete(chan
->switch_timer
);
812 PERROR("timer_delete");
815 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
817 chan
->switch_timer
= 0;
818 chan
->switch_timer_enabled
= 0;
822 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
824 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
826 struct itimerspec its
;
829 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
830 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
833 chan
->read_timer_enabled
= 1;
835 lib_ring_buffer_setup_timer_thread();
837 sev
.sigev_notify
= SIGEV_SIGNAL
;
838 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
839 sev
.sigev_value
.sival_ptr
= chan
;
840 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
842 PERROR("timer_create");
845 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
846 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
847 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
848 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
850 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
852 PERROR("timer_settime");
857 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
859 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
862 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
863 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
866 ret
= timer_delete(chan
->read_timer
);
868 PERROR("timer_delete");
872 * do one more check to catch data that has been written in the last
875 lib_ring_buffer_channel_do_read(chan
);
877 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
879 chan
->read_timer
= 0;
880 chan
->read_timer_enabled
= 0;
883 static void channel_unregister_notifiers(struct channel
*chan
,
884 struct lttng_ust_shm_handle
*handle
)
886 lib_ring_buffer_channel_switch_timer_stop(chan
);
887 lib_ring_buffer_channel_read_timer_stop(chan
);
890 static void channel_print_errors(struct channel
*chan
,
891 struct lttng_ust_shm_handle
*handle
)
893 const struct lttng_ust_lib_ring_buffer_config
*config
=
894 &chan
->backend
.config
;
897 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
898 for_each_possible_cpu(cpu
) {
899 struct lttng_ust_lib_ring_buffer
*buf
=
900 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
902 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
905 struct lttng_ust_lib_ring_buffer
*buf
=
906 shmp(handle
, chan
->backend
.buf
[0].shmp
);
909 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
913 static void channel_free(struct channel
*chan
,
914 struct lttng_ust_shm_handle
*handle
,
917 channel_backend_free(&chan
->backend
, handle
);
918 /* chan is freed by shm teardown */
919 shm_object_table_destroy(handle
->table
, consumer
);
924 * channel_create - Create channel.
925 * @config: ring buffer instance configuration
926 * @name: name of the channel
927 * @priv_data: ring buffer client private data area pointer (output)
928 * @priv_data_size: length, in bytes, of the private data area.
929 * @priv_data_init: initialization data for private data.
930 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
931 * address mapping. It is used only by RING_BUFFER_STATIC
932 * configuration. It can be set to NULL for other backends.
933 * @subbuf_size: subbuffer size
934 * @num_subbuf: number of subbuffers
935 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
936 * padding to let readers get those sub-buffers.
937 * Used for live streaming.
938 * @read_timer_interval: Time interval (in us) to wake up pending readers.
939 * @stream_fds: array of stream file descriptors.
940 * @nr_stream_fds: number of file descriptors in array.
943 * Returns NULL on failure.
945 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
948 size_t priv_data_align
,
949 size_t priv_data_size
,
950 void *priv_data_init
,
951 void *buf_addr
, size_t subbuf_size
,
952 size_t num_subbuf
, unsigned int switch_timer_interval
,
953 unsigned int read_timer_interval
,
954 const int *stream_fds
, int nr_stream_fds
)
957 size_t shmsize
, chansize
;
958 struct channel
*chan
;
959 struct lttng_ust_shm_handle
*handle
;
960 struct shm_object
*shmobj
;
961 unsigned int nr_streams
;
963 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
964 nr_streams
= num_possible_cpus();
968 if (nr_stream_fds
!= nr_streams
)
971 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
972 read_timer_interval
))
975 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
979 /* Allocate table for channel + per-cpu buffers */
980 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
982 goto error_table_alloc
;
984 /* Calculate the shm allocation layout */
985 shmsize
= sizeof(struct channel
);
986 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
987 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
990 shmsize
+= offset_align(shmsize
, priv_data_align
);
991 shmsize
+= priv_data_size
;
993 /* Allocate normal memory for channel (not shared) */
994 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
998 /* struct channel is at object 0, offset 0 (hardcoded) */
999 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
1000 assert(handle
->chan
._ref
.index
== 0);
1001 assert(handle
->chan
._ref
.offset
== 0);
1002 chan
= shmp(handle
, handle
->chan
);
1005 chan
->nr_streams
= nr_streams
;
1007 /* space for private data */
1008 if (priv_data_size
) {
1009 DECLARE_SHMP(void, priv_data_alloc
);
1011 align_shm(shmobj
, priv_data_align
);
1012 chan
->priv_data_offset
= shmobj
->allocated_len
;
1013 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1014 if (!shmp(handle
, priv_data_alloc
))
1016 *priv_data
= channel_get_private(chan
);
1017 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
1019 chan
->priv_data_offset
= -1;
1024 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1025 subbuf_size
, num_subbuf
, handle
,
1028 goto error_backend_init
;
1030 chan
->handle
= handle
;
1031 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1033 chan
->switch_timer_interval
= switch_timer_interval
;
1034 chan
->read_timer_interval
= read_timer_interval
;
1035 lib_ring_buffer_channel_switch_timer_start(chan
);
1036 lib_ring_buffer_channel_read_timer_start(chan
);
1042 shm_object_table_destroy(handle
->table
, 1);
1048 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1049 uint64_t memory_map_size
,
1052 struct lttng_ust_shm_handle
*handle
;
1053 struct shm_object
*object
;
1055 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1059 /* Allocate table for channel + per-cpu buffers */
1060 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1062 goto error_table_alloc
;
1063 /* Add channel object */
1064 object
= shm_object_table_append_mem(handle
->table
, data
,
1065 memory_map_size
, wakeup_fd
);
1067 goto error_table_object
;
1068 /* struct channel is at object 0, offset 0 (hardcoded) */
1069 handle
->chan
._ref
.index
= 0;
1070 handle
->chan
._ref
.offset
= 0;
1074 shm_object_table_destroy(handle
->table
, 0);
1080 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1081 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1082 uint64_t memory_map_size
)
1084 struct shm_object
*object
;
1086 /* Add stream object */
1087 object
= shm_object_table_append_shm(handle
->table
,
1088 shm_fd
, wakeup_fd
, stream_nr
,
1095 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1097 assert(handle
->table
);
1098 return handle
->table
->allocated_len
- 1;
1102 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1105 channel_free(chan
, handle
, consumer
);
1109 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1110 * @chan: channel to destroy
1112 * Holds cpu hotplug.
1113 * Call "destroy" callback, finalize channels, decrement the channel
1114 * reference count. Note that when readers have completed data
1115 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1116 * They should release their handle at that point.
1118 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1123 * Note: the consumer takes care of finalizing and
1124 * switching the buffers.
1126 channel_unregister_notifiers(chan
, handle
);
1128 * The consumer prints errors.
1130 channel_print_errors(chan
, handle
);
1134 * sessiond/consumer are keeping a reference on the shm file
1135 * descriptor directly. No need to refcount.
1137 channel_release(chan
, handle
, consumer
);
1141 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
1142 const struct lttng_ust_lib_ring_buffer_config
*config
,
1143 struct channel
*chan
, int cpu
,
1144 struct lttng_ust_shm_handle
*handle
,
1145 int *shm_fd
, int *wait_fd
,
1147 uint64_t *memory_map_size
)
1149 struct shm_ref
*ref
;
1151 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1154 if (cpu
>= num_possible_cpus())
1157 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1158 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1159 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1160 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1161 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1163 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1166 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1167 struct channel
*chan
,
1168 struct lttng_ust_shm_handle
*handle
)
1170 struct shm_ref
*ref
;
1172 ref
= &handle
->chan
._ref
;
1173 return shm_close_wait_fd(handle
, ref
);
1176 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1177 struct channel
*chan
,
1178 struct lttng_ust_shm_handle
*handle
)
1180 struct shm_ref
*ref
;
1182 ref
= &handle
->chan
._ref
;
1183 return shm_close_wakeup_fd(handle
, ref
);
1186 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1187 struct channel
*chan
,
1188 struct lttng_ust_shm_handle
*handle
,
1191 struct shm_ref
*ref
;
1193 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1196 if (cpu
>= num_possible_cpus())
1199 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1200 return shm_close_wait_fd(handle
, ref
);
1203 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1204 struct channel
*chan
,
1205 struct lttng_ust_shm_handle
*handle
,
1208 struct shm_ref
*ref
;
1211 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1214 if (cpu
>= num_possible_cpus())
1217 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1218 pthread_mutex_lock(&wakeup_fd_mutex
);
1219 ret
= shm_close_wakeup_fd(handle
, ref
);
1220 pthread_mutex_unlock(&wakeup_fd_mutex
);
1224 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
1225 struct lttng_ust_shm_handle
*handle
)
1227 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1233 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
1234 struct lttng_ust_shm_handle
*handle
)
1236 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1240 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1242 uatomic_dec(&buf
->active_readers
);
1246 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1248 * @consumed: consumed count indicating the position where to read
1249 * @produced: produced count, indicates position when to stop reading
1251 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1252 * data to read at consumed position, or 0 if the get operation succeeds.
1255 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1256 unsigned long *consumed
, unsigned long *produced
,
1257 struct lttng_ust_shm_handle
*handle
)
1259 struct channel
*chan
;
1260 const struct lttng_ust_lib_ring_buffer_config
*config
;
1261 unsigned long consumed_cur
, write_offset
;
1264 chan
= shmp(handle
, buf
->backend
.chan
);
1267 config
= &chan
->backend
.config
;
1268 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1270 * Read finalized before counters.
1273 consumed_cur
= uatomic_read(&buf
->consumed
);
1275 * No need to issue a memory barrier between consumed count read and
1276 * write offset read, because consumed count can only change
1277 * concurrently in overwrite mode, and we keep a sequence counter
1278 * identifier derived from the write offset to check we are getting
1279 * the same sub-buffer we are expecting (the sub-buffers are atomically
1280 * "tagged" upon writes, tags are checked upon read).
1282 write_offset
= v_read(config
, &buf
->offset
);
1285 * Check that we are not about to read the same subbuffer in
1286 * which the writer head is.
1288 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1292 *consumed
= consumed_cur
;
1293 *produced
= subbuf_trunc(write_offset
, chan
);
1299 * The memory barriers __wait_event()/wake_up_interruptible() take care
1300 * of "raw_spin_is_locked" memory ordering.
1309 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1310 * are saved regardless of whether the consumed and produced positions are
1311 * in the same subbuffer.
1313 * @consumed: consumed byte count indicating the last position read
1314 * @produced: produced byte count indicating the last position written
1316 * This function is meant to provide information on the exact producer and
1317 * consumer positions without regard for the "snapshot" feature.
1319 int lib_ring_buffer_snapshot_sample_positions(
1320 struct lttng_ust_lib_ring_buffer
*buf
,
1321 unsigned long *consumed
, unsigned long *produced
,
1322 struct lttng_ust_shm_handle
*handle
)
1324 struct channel
*chan
;
1325 const struct lttng_ust_lib_ring_buffer_config
*config
;
1326 unsigned long consumed_cur
, write_offset
;
1328 chan
= shmp(handle
, buf
->backend
.chan
);
1331 config
= &chan
->backend
.config
;
1333 *consumed
= uatomic_read(&buf
->consumed
);
1335 * No need to issue a memory barrier between consumed count read and
1336 * write offset read, because consumed count can only change
1337 * concurrently in overwrite mode, and we keep a sequence counter
1338 * identifier derived from the write offset to check we are getting
1339 * the same sub-buffer we are expecting (the sub-buffers are atomically
1340 * "tagged" upon writes, tags are checked upon read).
1342 *produced
= v_read(config
, &buf
->offset
);
1347 * lib_ring_buffer_move_consumer - move consumed counter forward
1349 * @consumed_new: new consumed count value
1351 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1352 unsigned long consumed_new
,
1353 struct lttng_ust_shm_handle
*handle
)
1355 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1356 struct channel
*chan
;
1357 unsigned long consumed
;
1359 chan
= shmp(handle
, bufb
->chan
);
1362 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1365 * Only push the consumed value forward.
1366 * If the consumed cmpxchg fails, this is because we have been pushed by
1367 * the writer in flight recorder mode.
1369 consumed
= uatomic_read(&buf
->consumed
);
1370 while ((long) consumed
- (long) consumed_new
< 0)
1371 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1376 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1378 * @consumed: consumed count indicating the position where to read
1380 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1381 * data to read at consumed position, or 0 if the get operation succeeds.
1383 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1384 unsigned long consumed
,
1385 struct lttng_ust_shm_handle
*handle
)
1387 struct channel
*chan
;
1388 const struct lttng_ust_lib_ring_buffer_config
*config
;
1389 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1390 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1391 struct commit_counters_cold
*cc_cold
;
1393 chan
= shmp(handle
, buf
->backend
.chan
);
1396 config
= &chan
->backend
.config
;
1398 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1400 * Read finalized before counters.
1403 consumed_cur
= uatomic_read(&buf
->consumed
);
1404 consumed_idx
= subbuf_index(consumed
, chan
);
1405 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1408 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1410 * Make sure we read the commit count before reading the buffer
1411 * data and the write offset. Correct consumed offset ordering
1412 * wrt commit count is insured by the use of cmpxchg to update
1413 * the consumed offset.
1416 * Local rmb to match the remote wmb to read the commit count
1417 * before the buffer data and the write offset.
1421 write_offset
= v_read(config
, &buf
->offset
);
1424 * Check that the buffer we are getting is after or at consumed_cur
1427 if ((long) subbuf_trunc(consumed
, chan
)
1428 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1432 * Check that the subbuffer we are trying to consume has been
1433 * already fully committed. There are a few causes that can make
1434 * this unavailability situation occur:
1436 * Temporary (short-term) situation:
1437 * - Application is running on a different CPU, between reserve
1438 * and commit ring buffer operations,
1439 * - Application is preempted between reserve and commit ring
1440 * buffer operations,
1442 * Long-term situation:
1443 * - Application is stopped (SIGSTOP) between reserve and commit
1444 * ring buffer operations. Could eventually be resumed by
1446 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1447 * reserve and commit ring buffer operation.
1449 * From a consumer perspective, handling short-term
1450 * unavailability situations is performed by retrying a few
1451 * times after a delay. Handling long-term unavailability
1452 * situations is handled by failing to get the sub-buffer.
1454 * In all of those situations, if the application is taking a
1455 * long time to perform its commit after ring buffer space
1456 * reservation, we can end up in a situation where the producer
1457 * will fill the ring buffer and try to write into the same
1458 * sub-buffer again (which has a missing commit). This is
1459 * handled by the producer in the sub-buffer switch handling
1460 * code of the reserve routine by detecting unbalanced
1461 * reserve/commit counters and discarding all further events
1462 * until the situation is resolved in those situations. Two
1463 * scenarios can occur:
1465 * 1) The application causing the reserve/commit counters to be
1466 * unbalanced has been terminated. In this situation, all
1467 * further events will be discarded in the buffers, and no
1468 * further buffer data will be readable by the consumer
1469 * daemon. Tearing down the UST tracing session and starting
1470 * anew is a work-around for those situations. Note that this
1471 * only affects per-UID tracing. In per-PID tracing, the
1472 * application vanishes with the termination, and therefore
1473 * no more data needs to be written to the buffers.
1474 * 2) The application causing the unbalance has been delayed for
1475 * a long time, but will eventually try to increment the
1476 * commit counter after eventually writing to the sub-buffer.
1477 * This situation can cause events to be discarded until the
1478 * application resumes its operations.
1480 if (((commit_count
- chan
->backend
.subbuf_size
)
1481 & chan
->commit_count_mask
)
1482 - (buf_trunc(consumed
, chan
)
1483 >> chan
->backend
.num_subbuf_order
)
1485 if (nr_retry
-- > 0) {
1486 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1487 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1495 * Check that we are not about to read the same subbuffer in
1496 * which the writer head is.
1498 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1503 * Failure to get the subbuffer causes a busy-loop retry without going
1504 * to a wait queue. These are caused by short-lived race windows where
1505 * the writer is getting access to a subbuffer we were trying to get
1506 * access to. Also checks that the "consumed" buffer count we are
1507 * looking for matches the one contained in the subbuffer id.
1509 * The short-lived race window described here can be affected by
1510 * application signals and preemption, thus requiring to bound
1511 * the loop to a maximum number of retry.
1513 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1514 consumed_idx
, buf_trunc_val(consumed
, chan
),
1517 if (nr_retry
-- > 0) {
1518 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1519 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1525 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1527 buf
->get_subbuf_consumed
= consumed
;
1528 buf
->get_subbuf
= 1;
1534 * The memory barriers __wait_event()/wake_up_interruptible() take care
1535 * of "raw_spin_is_locked" memory ordering.
1544 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1547 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1548 struct lttng_ust_shm_handle
*handle
)
1550 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1551 struct channel
*chan
;
1552 const struct lttng_ust_lib_ring_buffer_config
*config
;
1553 unsigned long sb_bindex
, consumed_idx
, consumed
;
1554 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
1555 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
1557 chan
= shmp(handle
, bufb
->chan
);
1560 config
= &chan
->backend
.config
;
1561 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1563 if (!buf
->get_subbuf
) {
1565 * Reader puts a subbuffer it did not get.
1567 CHAN_WARN_ON(chan
, 1);
1570 consumed
= buf
->get_subbuf_consumed
;
1571 buf
->get_subbuf
= 0;
1574 * Clear the records_unread counter. (overruns counter)
1575 * Can still be non-zero if a file reader simply grabbed the data
1576 * without using iterators.
1577 * Can be below zero if an iterator is used on a snapshot more than
1580 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1581 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1584 backend_pages
= shmp(handle
, rpages
->shmp
);
1587 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1588 &bufb
->records_read
);
1589 v_set(config
, &backend_pages
->records_unread
, 0);
1590 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1591 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1592 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1595 * Exchange the reader subbuffer with the one we put in its place in the
1596 * writer subbuffer table. Expect the original consumed count. If
1597 * update_read_sb_index fails, this is because the writer updated the
1598 * subbuffer concurrently. We should therefore keep the subbuffer we
1599 * currently have: it has become invalid to try reading this sub-buffer
1600 * consumed count value anyway.
1602 consumed_idx
= subbuf_index(consumed
, chan
);
1603 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1604 consumed_idx
, buf_trunc_val(consumed
, chan
),
1607 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1608 * if the writer concurrently updated it.
1613 * cons_offset is an iterator on all subbuffer offsets between the reader
1614 * position and the writer position. (inclusive)
1617 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1618 struct channel
*chan
,
1619 unsigned long cons_offset
,
1621 struct lttng_ust_shm_handle
*handle
)
1623 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1624 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1625 struct commit_counters_hot
*cc_hot
;
1626 struct commit_counters_cold
*cc_cold
;
1628 cons_idx
= subbuf_index(cons_offset
, chan
);
1629 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1632 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1635 commit_count
= v_read(config
, &cc_hot
->cc
);
1636 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1638 if (subbuf_offset(commit_count
, chan
) != 0)
1639 DBG("ring buffer %s, cpu %d: "
1640 "commit count in subbuffer %lu,\n"
1641 "expecting multiples of %lu bytes\n"
1642 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1643 chan
->backend
.name
, cpu
, cons_idx
,
1644 chan
->backend
.subbuf_size
,
1645 commit_count
, commit_count_sb
);
1647 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1648 chan
->backend
.name
, cpu
, commit_count
);
1652 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1653 struct channel
*chan
,
1654 void *priv
, int cpu
,
1655 struct lttng_ust_shm_handle
*handle
)
1657 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1658 unsigned long write_offset
, cons_offset
;
1661 * No need to order commit_count, write_offset and cons_offset reads
1662 * because we execute at teardown when no more writer nor reader
1663 * references are left.
1665 write_offset
= v_read(config
, &buf
->offset
);
1666 cons_offset
= uatomic_read(&buf
->consumed
);
1667 if (write_offset
!= cons_offset
)
1668 DBG("ring buffer %s, cpu %d: "
1669 "non-consumed data\n"
1670 " [ %lu bytes written, %lu bytes read ]\n",
1671 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1673 for (cons_offset
= uatomic_read(&buf
->consumed
);
1674 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1677 cons_offset
= subbuf_align(cons_offset
, chan
))
1678 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1683 void lib_ring_buffer_print_errors(struct channel
*chan
,
1684 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1685 struct lttng_ust_shm_handle
*handle
)
1687 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1688 void *priv
= channel_get_private(chan
);
1690 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1691 DBG("ring buffer %s: %lu records written, "
1692 "%lu records overrun\n",
1694 v_read(config
, &buf
->records_count
),
1695 v_read(config
, &buf
->records_overrun
));
1697 DBG("ring buffer %s, cpu %d: %lu records written, "
1698 "%lu records overrun\n",
1699 chan
->backend
.name
, cpu
,
1700 v_read(config
, &buf
->records_count
),
1701 v_read(config
, &buf
->records_overrun
));
1703 if (v_read(config
, &buf
->records_lost_full
)
1704 || v_read(config
, &buf
->records_lost_wrap
)
1705 || v_read(config
, &buf
->records_lost_big
))
1706 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1707 " [ %lu buffer full, %lu nest buffer wrap-around, "
1708 "%lu event too big ]\n",
1709 chan
->backend
.name
, cpu
,
1710 v_read(config
, &buf
->records_lost_full
),
1711 v_read(config
, &buf
->records_lost_wrap
),
1712 v_read(config
, &buf
->records_lost_big
));
1714 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1718 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1720 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1721 * active or at buffer finalization (destroy).
1724 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1725 struct channel
*chan
,
1726 struct switch_offsets
*offsets
,
1728 struct lttng_ust_shm_handle
*handle
)
1730 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1731 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1732 unsigned long commit_count
;
1733 struct commit_counters_hot
*cc_hot
;
1735 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1738 * Order all writes to buffer before the commit count update that will
1739 * determine that the subbuffer is full.
1742 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1745 v_add(config
, config
->cb
.subbuffer_header_size(),
1747 commit_count
= v_read(config
, &cc_hot
->cc
);
1748 /* Check if the written buffer has to be delivered */
1749 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1750 commit_count
, oldidx
, handle
, tsc
);
1751 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1752 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1753 commit_count
, handle
, cc_hot
);
1757 * lib_ring_buffer_switch_old_end: switch old subbuffer
1759 * Note : offset_old should never be 0 here. It is ok, because we never perform
1760 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1761 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1765 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1766 struct channel
*chan
,
1767 struct switch_offsets
*offsets
,
1769 struct lttng_ust_shm_handle
*handle
)
1771 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1772 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1773 unsigned long commit_count
, padding_size
, data_size
;
1774 struct commit_counters_hot
*cc_hot
;
1776 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1777 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1778 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1782 * Order all writes to buffer before the commit count update that will
1783 * determine that the subbuffer is full.
1786 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1789 v_add(config
, padding_size
, &cc_hot
->cc
);
1790 commit_count
= v_read(config
, &cc_hot
->cc
);
1791 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1792 commit_count
, oldidx
, handle
, tsc
);
1793 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1794 offsets
->old
+ padding_size
, commit_count
, handle
,
1799 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1801 * This code can be executed unordered : writers may already have written to the
1802 * sub-buffer before this code gets executed, caution. The commit makes sure
1803 * that this code is executed before the deliver of this sub-buffer.
1806 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1807 struct channel
*chan
,
1808 struct switch_offsets
*offsets
,
1810 struct lttng_ust_shm_handle
*handle
)
1812 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1813 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1814 unsigned long commit_count
;
1815 struct commit_counters_hot
*cc_hot
;
1817 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1820 * Order all writes to buffer before the commit count update that will
1821 * determine that the subbuffer is full.
1824 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1827 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1828 commit_count
= v_read(config
, &cc_hot
->cc
);
1829 /* Check if the written buffer has to be delivered */
1830 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1831 commit_count
, beginidx
, handle
, tsc
);
1832 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1833 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1834 commit_count
, handle
, cc_hot
);
1838 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1840 * Calls subbuffer_set_data_size() to set the data size of the current
1841 * sub-buffer. We do not need to perform check_deliver nor commit here,
1842 * since this task will be done by the "commit" of the event for which
1843 * we are currently doing the space reservation.
1846 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1847 struct channel
*chan
,
1848 struct switch_offsets
*offsets
,
1850 struct lttng_ust_shm_handle
*handle
)
1852 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1853 unsigned long endidx
, data_size
;
1855 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1856 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1857 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1864 * !0 if execution must be aborted.
1867 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1868 struct lttng_ust_lib_ring_buffer
*buf
,
1869 struct channel
*chan
,
1870 struct switch_offsets
*offsets
,
1872 struct lttng_ust_shm_handle
*handle
)
1874 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1875 unsigned long off
, reserve_commit_diff
;
1877 offsets
->begin
= v_read(config
, &buf
->offset
);
1878 offsets
->old
= offsets
->begin
;
1879 offsets
->switch_old_start
= 0;
1880 off
= subbuf_offset(offsets
->begin
, chan
);
1882 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1885 * Ensure we flush the header of an empty subbuffer when doing the
1886 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1887 * total data gathering duration even if there were no records saved
1888 * after the last buffer switch.
1889 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1890 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1891 * subbuffer header as appropriate.
1892 * The next record that reserves space will be responsible for
1893 * populating the following subbuffer header. We choose not to populate
1894 * the next subbuffer header here because we want to be able to use
1895 * SWITCH_ACTIVE for periodical buffer flush, which must
1896 * guarantee that all the buffer content (records and header
1897 * timestamps) are visible to the reader. This is required for
1898 * quiescence guarantees for the fusion merge.
1900 if (mode
!= SWITCH_FLUSH
&& !off
)
1901 return -1; /* we do not have to switch : buffer is empty */
1903 if (caa_unlikely(off
== 0)) {
1904 unsigned long sb_index
, commit_count
;
1905 struct commit_counters_cold
*cc_cold
;
1908 * We are performing a SWITCH_FLUSH. There may be concurrent
1909 * writes into the buffer if e.g. invoked while performing a
1910 * snapshot on an active trace.
1912 * If the client does not save any header information
1913 * (sub-buffer header size == 0), don't switch empty subbuffer
1914 * on finalize, because it is invalid to deliver a completely
1917 if (!config
->cb
.subbuffer_header_size())
1920 /* Test new buffer integrity */
1921 sb_index
= subbuf_index(offsets
->begin
, chan
);
1922 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1925 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1926 reserve_commit_diff
=
1927 (buf_trunc(offsets
->begin
, chan
)
1928 >> chan
->backend
.num_subbuf_order
)
1929 - (commit_count
& chan
->commit_count_mask
);
1930 if (caa_likely(reserve_commit_diff
== 0)) {
1931 /* Next subbuffer not being written to. */
1932 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1933 subbuf_trunc(offsets
->begin
, chan
)
1934 - subbuf_trunc((unsigned long)
1935 uatomic_read(&buf
->consumed
), chan
)
1936 >= chan
->backend
.buf_size
)) {
1938 * We do not overwrite non consumed buffers
1939 * and we are full : don't switch.
1944 * Next subbuffer not being written to, and we
1945 * are either in overwrite mode or the buffer is
1946 * not full. It's safe to write in this new
1952 * Next subbuffer reserve offset does not match the
1953 * commit offset. Don't perform switch in
1954 * producer-consumer and overwrite mode. Caused by
1955 * either a writer OOPS or too many nested writes over a
1956 * reserve/commit pair.
1962 * Need to write the subbuffer start header on finalize.
1964 offsets
->switch_old_start
= 1;
1966 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1967 /* Note: old points to the next subbuf at offset 0 */
1968 offsets
->end
= offsets
->begin
;
1973 * Force a sub-buffer switch. This operation is completely reentrant : can be
1974 * called while tracing is active with absolutely no lock held.
1976 * Note, however, that as a v_cmpxchg is used for some atomic
1977 * operations, this function must be called from the CPU which owns the buffer
1978 * for a ACTIVE flush.
1980 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1981 struct lttng_ust_shm_handle
*handle
)
1983 struct channel
*chan
;
1984 const struct lttng_ust_lib_ring_buffer_config
*config
;
1985 struct switch_offsets offsets
;
1986 unsigned long oldidx
;
1989 chan
= shmp(handle
, buf
->backend
.chan
);
1992 config
= &chan
->backend
.config
;
1997 * Perform retryable operations.
2000 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
2002 return; /* Switch not needed */
2003 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
2007 * Atomically update last_tsc. This update races against concurrent
2008 * atomic updates, but the race will always cause supplementary full TSC
2009 * records, never the opposite (missing a full TSC record when it would
2012 save_last_tsc(config
, buf
, tsc
);
2015 * Push the reader if necessary
2017 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
2019 oldidx
= subbuf_index(offsets
.old
, chan
);
2020 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
2023 * May need to populate header start on SWITCH_FLUSH.
2025 if (offsets
.switch_old_start
) {
2026 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
2027 offsets
.old
+= config
->cb
.subbuffer_header_size();
2031 * Switch old subbuffer.
2033 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
2037 bool handle_blocking_retry(int *timeout_left_ms
)
2039 int timeout
= *timeout_left_ms
, delay
;
2041 if (caa_likely(!timeout
))
2042 return false; /* Do not retry, discard event. */
2043 if (timeout
< 0) /* Wait forever. */
2044 delay
= RETRY_DELAY_MS
;
2046 delay
= min_t(int, timeout
, RETRY_DELAY_MS
);
2047 (void) poll(NULL
, 0, delay
);
2049 *timeout_left_ms
-= delay
;
2050 return true; /* Retry. */
2056 * -ENOSPC if event size is too large for packet.
2057 * -ENOBUFS if there is currently not enough space in buffer for the event.
2058 * -EIO if data cannot be written into the buffer for any other reason.
2061 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
2062 struct channel
*chan
,
2063 struct switch_offsets
*offsets
,
2064 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
2066 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2067 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2068 unsigned long reserve_commit_diff
, offset_cmp
;
2069 int timeout_left_ms
= lttng_ust_blocking_retry_timeout
;
2072 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2073 offsets
->old
= offsets
->begin
;
2074 offsets
->switch_new_start
= 0;
2075 offsets
->switch_new_end
= 0;
2076 offsets
->switch_old_end
= 0;
2077 offsets
->pre_header_padding
= 0;
2079 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2080 if ((int64_t) ctx
->tsc
== -EIO
)
2083 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
2084 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2086 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
2087 offsets
->switch_new_start
= 1; /* For offsets->begin */
2089 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2091 &offsets
->pre_header_padding
,
2094 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2097 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2098 offsets
->size
> chan
->backend
.subbuf_size
)) {
2099 offsets
->switch_old_end
= 1; /* For offsets->old */
2100 offsets
->switch_new_start
= 1; /* For offsets->begin */
2103 if (caa_unlikely(offsets
->switch_new_start
)) {
2104 unsigned long sb_index
, commit_count
;
2105 struct commit_counters_cold
*cc_cold
;
2108 * We are typically not filling the previous buffer completely.
2110 if (caa_likely(offsets
->switch_old_end
))
2111 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2112 offsets
->begin
= offsets
->begin
2113 + config
->cb
.subbuffer_header_size();
2114 /* Test new buffer integrity */
2115 sb_index
= subbuf_index(offsets
->begin
, chan
);
2117 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2118 * lib_ring_buffer_check_deliver() has the matching
2119 * memory barriers required around commit_cold cc_sb
2120 * updates to ensure reserve and commit counter updates
2121 * are not seen reordered when updated by another CPU.
2124 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2127 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2128 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2130 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2132 * The reserve counter have been concurrently updated
2133 * while we read the commit counter. This means the
2134 * commit counter we read might not match buf->offset
2135 * due to concurrent update. We therefore need to retry.
2139 reserve_commit_diff
=
2140 (buf_trunc(offsets
->begin
, chan
)
2141 >> chan
->backend
.num_subbuf_order
)
2142 - (commit_count
& chan
->commit_count_mask
);
2143 if (caa_likely(reserve_commit_diff
== 0)) {
2144 /* Next subbuffer not being written to. */
2145 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2146 subbuf_trunc(offsets
->begin
, chan
)
2147 - subbuf_trunc((unsigned long)
2148 uatomic_read(&buf
->consumed
), chan
)
2149 >= chan
->backend
.buf_size
)) {
2150 unsigned long nr_lost
;
2152 if (handle_blocking_retry(&timeout_left_ms
))
2156 * We do not overwrite non consumed buffers
2157 * and we are full : record is lost.
2159 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2160 v_inc(config
, &buf
->records_lost_full
);
2161 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2162 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2163 nr_lost
+ 1, chan
->backend
.name
,
2169 * Next subbuffer not being written to, and we
2170 * are either in overwrite mode or the buffer is
2171 * not full. It's safe to write in this new
2176 unsigned long nr_lost
;
2179 * Next subbuffer reserve offset does not match the
2180 * commit offset, and this did not involve update to the
2181 * reserve counter. Drop record in producer-consumer and
2182 * overwrite mode. Caused by either a writer OOPS or too
2183 * many nested writes over a reserve/commit pair.
2185 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2186 v_inc(config
, &buf
->records_lost_wrap
);
2187 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2188 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2189 nr_lost
+ 1, chan
->backend
.name
,
2195 config
->cb
.record_header_size(config
, chan
,
2197 &offsets
->pre_header_padding
,
2200 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2203 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2204 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2205 unsigned long nr_lost
;
2208 * Record too big for subbuffers, report error, don't
2209 * complete the sub-buffer switch.
2211 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2212 v_inc(config
, &buf
->records_lost_big
);
2213 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2214 DBG("%lu or more records lost in (%s:%d) record size "
2215 " of %zu bytes is too large for buffer\n",
2216 nr_lost
+ 1, chan
->backend
.name
,
2217 buf
->backend
.cpu
, offsets
->size
);
2222 * We just made a successful buffer switch and the
2223 * record fits in the new subbuffer. Let's write.
2228 * Record fits in the current buffer and we are not on a switch
2229 * boundary. It's safe to write.
2232 offsets
->end
= offsets
->begin
+ offsets
->size
;
2234 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2236 * The offset_end will fall at the very beginning of the next
2239 offsets
->switch_new_end
= 1; /* For offsets->begin */
2245 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2246 * @ctx: ring buffer context.
2248 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2249 * -EIO for other errors, else returns 0.
2250 * It will take care of sub-buffer switching.
2252 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
2254 struct channel
*chan
= ctx
->chan
;
2255 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2256 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2257 struct lttng_ust_lib_ring_buffer
*buf
;
2258 struct switch_offsets offsets
;
2261 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2262 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
2264 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2272 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2274 if (caa_unlikely(ret
))
2276 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2281 * Atomically update last_tsc. This update races against concurrent
2282 * atomic updates, but the race will always cause supplementary full TSC
2283 * records, never the opposite (missing a full TSC record when it would
2286 save_last_tsc(config
, buf
, ctx
->tsc
);
2289 * Push the reader if necessary
2291 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2294 * Clear noref flag for this subbuffer.
2296 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2297 subbuf_index(offsets
.end
- 1, chan
),
2301 * Switch old subbuffer if needed.
2303 if (caa_unlikely(offsets
.switch_old_end
)) {
2304 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2305 subbuf_index(offsets
.old
- 1, chan
),
2307 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2311 * Populate new subbuffer.
2313 if (caa_unlikely(offsets
.switch_new_start
))
2314 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2316 if (caa_unlikely(offsets
.switch_new_end
))
2317 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2319 ctx
->slot_size
= offsets
.size
;
2320 ctx
->pre_offset
= offsets
.begin
;
2321 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2326 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
2327 struct lttng_ust_lib_ring_buffer
*buf
,
2328 unsigned long commit_count
,
2330 struct lttng_ust_shm_handle
*handle
)
2332 struct commit_counters_hot
*cc_hot
;
2334 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2336 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2339 v_set(config
, &cc_hot
->seq
, commit_count
);
2343 * The ring buffer can count events recorded and overwritten per buffer,
2344 * but it is disabled by default due to its performance overhead.
2346 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2348 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2349 struct lttng_ust_lib_ring_buffer
*buf
,
2351 struct lttng_ust_shm_handle
*handle
)
2353 v_add(config
, subbuffer_get_records_count(config
,
2354 &buf
->backend
, idx
, handle
),
2355 &buf
->records_count
);
2356 v_add(config
, subbuffer_count_records_overrun(config
,
2357 &buf
->backend
, idx
, handle
),
2358 &buf
->records_overrun
);
2360 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2362 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2363 struct lttng_ust_lib_ring_buffer
*buf
,
2365 struct lttng_ust_shm_handle
*handle
)
2368 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2370 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
2371 struct lttng_ust_lib_ring_buffer
*buf
,
2372 struct channel
*chan
,
2373 unsigned long offset
,
2374 unsigned long commit_count
,
2376 struct lttng_ust_shm_handle
*handle
,
2379 unsigned long old_commit_count
= commit_count
2380 - chan
->backend
.subbuf_size
;
2381 struct commit_counters_cold
*cc_cold
;
2384 * If we succeeded at updating cc_sb below, we are the subbuffer
2385 * writer delivering the subbuffer. Deals with concurrent
2386 * updates of the "cc" value without adding a add_return atomic
2387 * operation to the fast path.
2389 * We are doing the delivery in two steps:
2390 * - First, we cmpxchg() cc_sb to the new value
2391 * old_commit_count + 1. This ensures that we are the only
2392 * subbuffer user successfully filling the subbuffer, but we
2393 * do _not_ set the cc_sb value to "commit_count" yet.
2394 * Therefore, other writers that would wrap around the ring
2395 * buffer and try to start writing to our subbuffer would
2396 * have to drop records, because it would appear as
2398 * We therefore have exclusive access to the subbuffer control
2399 * structures. This mutual exclusion with other writers is
2400 * crucially important to perform record overruns count in
2401 * flight recorder mode locklessly.
2402 * - When we are ready to release the subbuffer (either for
2403 * reading or for overrun by other writers), we simply set the
2404 * cc_sb value to "commit_count" and perform delivery.
2406 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2407 * This guarantees that old_commit_count + 1 != commit_count.
2411 * Order prior updates to reserve count prior to the
2412 * commit_cold cc_sb update.
2415 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2418 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2419 old_commit_count
, old_commit_count
+ 1)
2420 == old_commit_count
)) {
2422 * Start of exclusive subbuffer access. We are
2423 * guaranteed to be the last writer in this subbuffer
2424 * and any other writer trying to access this subbuffer
2425 * in this state is required to drop records.
2427 deliver_count_events(config
, buf
, idx
, handle
);
2428 config
->cb
.buffer_end(buf
, tsc
, idx
,
2429 lib_ring_buffer_get_data_size(config
,
2436 * Increment the packet counter while we have exclusive
2439 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2442 * Set noref flag and offset for this subbuffer id.
2443 * Contains a memory barrier that ensures counter stores
2444 * are ordered before set noref and offset.
2446 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2447 buf_trunc_val(offset
, chan
), handle
);
2450 * Order set_noref and record counter updates before the
2451 * end of subbuffer exclusive access. Orders with
2452 * respect to writers coming into the subbuffer after
2453 * wrap around, and also order wrt concurrent readers.
2456 /* End of exclusive subbuffer access */
2457 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2459 * Order later updates to reserve count after
2460 * the commit cold cc_sb update.
2463 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2464 commit_count
, idx
, handle
);
2467 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2469 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2470 && uatomic_read(&buf
->active_readers
)
2471 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2472 lib_ring_buffer_wakeup(buf
, handle
);
2478 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2480 void lttng_fixup_ringbuffer_tls(void)
2482 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2485 void lib_ringbuffer_signal_init(void)
2491 * Block signal for entire process, so only our thread processes
2495 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2498 PERROR("pthread_sigmask");