2 * ring_buffer_frontend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
41 * Bob Wisniewski <bob@watson.ibm.com>
43 * Buffer reader semantic :
46 * while buffer is not finalized and empty
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
56 #include <sys/types.h>
63 #include <urcu/compiler.h>
65 #include <urcu/tls-compat.h>
70 #include <lttng/ringbuffer-config.h>
76 #include "../liblttng-ust/compat.h" /* For ENODATA */
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
90 * Non-static to ensure the compiler does not optimize away the xor.
92 uint8_t lttng_crash_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
95 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
96 * close(2) to close the fd returned by shm_open.
97 * shm_unlink releases the shared memory object name.
98 * ftruncate(2) sets the size of the memory object.
99 * mmap/munmap maps the shared memory obj to a virtual address in the
100 * calling proceess (should be done both in libust and consumer).
101 * See shm_overview(7) for details.
102 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
105 * Since we don't need to access the object using its name, we can
106 * immediately shm_unlink(3) it, and only keep the handle with its file
111 * Internal structure representing offsets to use at a sub-buffer switch.
113 struct switch_offsets
{
114 unsigned long begin
, end
, old
;
115 size_t pre_header_padding
, size
;
116 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
120 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
123 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
126 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
129 void lib_ring_buffer_print_errors(struct channel
*chan
,
130 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
131 struct lttng_ust_shm_handle
*handle
);
134 * Handle timer teardown race wrt memory free of private data by
135 * ring buffer signals are handled by a single thread, which permits
136 * a synchronization point between handling of each signal.
137 * Protected by the lock within the structure.
139 struct timer_signal_data
{
140 pthread_t tid
; /* thread id managing signals */
143 pthread_mutex_t lock
;
146 static struct timer_signal_data timer_signal
= {
150 .lock
= PTHREAD_MUTEX_INITIALIZER
,
154 * lib_ring_buffer_reset - Reset ring buffer to initial values.
157 * Effectively empty the ring buffer. Should be called when the buffer is not
158 * used for writing. The ring buffer can be opened for reading, but the reader
159 * should not be using the iterator concurrently with reset. The previous
160 * current iterator record is reset.
162 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
163 struct lttng_ust_shm_handle
*handle
)
165 struct channel
*chan
;
166 const struct lttng_ust_lib_ring_buffer_config
*config
;
169 chan
= shmp(handle
, buf
->backend
.chan
);
172 config
= &chan
->backend
.config
;
174 * Reset iterator first. It will put the subbuffer if it currently holds
177 v_set(config
, &buf
->offset
, 0);
178 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
179 struct commit_counters_hot
*cc_hot
;
180 struct commit_counters_cold
*cc_cold
;
182 cc_hot
= shmp_index(handle
, buf
->commit_hot
, i
);
185 cc_cold
= shmp_index(handle
, buf
->commit_cold
, i
);
188 v_set(config
, &cc_hot
->cc
, 0);
190 v_set(config
, &cc_hot
->seq
, 0);
191 v_set(config
, &cc_cold
->cc_sb
, 0);
193 uatomic_set(&buf
->consumed
, 0);
194 uatomic_set(&buf
->record_disabled
, 0);
195 v_set(config
, &buf
->last_tsc
, 0);
196 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
197 /* Don't reset number of active readers */
198 v_set(config
, &buf
->records_lost_full
, 0);
199 v_set(config
, &buf
->records_lost_wrap
, 0);
200 v_set(config
, &buf
->records_lost_big
, 0);
201 v_set(config
, &buf
->records_count
, 0);
202 v_set(config
, &buf
->records_overrun
, 0);
207 * channel_reset - Reset channel to initial values.
210 * Effectively empty the channel. Should be called when the channel is not used
211 * for writing. The channel can be opened for reading, but the reader should not
212 * be using the iterator concurrently with reset. The previous current iterator
215 void channel_reset(struct channel
*chan
)
218 * Reset iterators first. Will put the subbuffer if held for reading.
220 uatomic_set(&chan
->record_disabled
, 0);
221 /* Don't reset commit_count_mask, still valid */
222 channel_backend_reset(&chan
->backend
);
223 /* Don't reset switch/read timer interval */
224 /* Don't reset notifiers and notifier enable bits */
225 /* Don't reset reader reference count */
229 void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config
*config
,
230 struct lttng_crash_abi
*crash_abi
,
231 struct lttng_ust_lib_ring_buffer
*buf
,
232 struct channel_backend
*chanb
,
233 struct shm_object
*shmobj
,
234 struct lttng_ust_shm_handle
*handle
)
238 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
239 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
240 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
241 crash_abi
->endian
= RB_CRASH_ENDIAN
;
242 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
243 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
244 crash_abi
->word_size
= sizeof(unsigned long);
245 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
247 /* Offset of fields */
248 crash_abi
->offset
.prod_offset
=
249 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
250 crash_abi
->offset
.consumed_offset
=
251 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
252 crash_abi
->offset
.commit_hot_array
=
253 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
254 crash_abi
->offset
.commit_hot_seq
=
255 offsetof(struct commit_counters_hot
, seq
);
256 crash_abi
->offset
.buf_wsb_array
=
257 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
258 crash_abi
->offset
.buf_wsb_id
=
259 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
, id
);
260 crash_abi
->offset
.sb_array
=
261 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
262 crash_abi
->offset
.sb_array_shmp_offset
=
263 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
,
265 crash_abi
->offset
.sb_backend_p_offset
=
266 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages
,
270 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
271 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
272 crash_abi
->length
.commit_hot_seq
=
273 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
274 crash_abi
->length
.buf_wsb_id
=
275 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer
*) NULL
)->id
);
276 crash_abi
->length
.sb_array_shmp_offset
=
277 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
278 crash_abi
->length
.sb_backend_p_offset
=
279 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
282 crash_abi
->stride
.commit_hot_array
=
283 sizeof(struct commit_counters_hot
);
284 crash_abi
->stride
.buf_wsb_array
=
285 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
);
286 crash_abi
->stride
.sb_array
=
287 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
);
289 /* Buffer constants */
290 crash_abi
->buf_size
= chanb
->buf_size
;
291 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
292 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
293 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
295 if (config
->cb
.content_size_field
) {
296 size_t offset
, length
;
298 config
->cb
.content_size_field(config
, &offset
, &length
);
299 crash_abi
->offset
.content_size
= offset
;
300 crash_abi
->length
.content_size
= length
;
302 crash_abi
->offset
.content_size
= 0;
303 crash_abi
->length
.content_size
= 0;
305 if (config
->cb
.packet_size_field
) {
306 size_t offset
, length
;
308 config
->cb
.packet_size_field(config
, &offset
, &length
);
309 crash_abi
->offset
.packet_size
= offset
;
310 crash_abi
->length
.packet_size
= length
;
312 crash_abi
->offset
.packet_size
= 0;
313 crash_abi
->length
.packet_size
= 0;
318 * Must be called under cpu hotplug protection.
320 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
321 struct channel_backend
*chanb
, int cpu
,
322 struct lttng_ust_shm_handle
*handle
,
323 struct shm_object
*shmobj
)
325 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
326 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
327 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
328 struct channel
*shmp_chan
;
329 struct commit_counters_hot
*cc_hot
;
330 void *priv
= channel_get_private(chan
);
331 size_t subbuf_header_size
;
335 /* Test for cpu hotplug */
336 if (buf
->backend
.allocated
)
339 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
340 set_shmp(buf
->commit_hot
,
342 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
343 if (!shmp(handle
, buf
->commit_hot
)) {
347 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
348 set_shmp(buf
->commit_cold
,
350 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
351 if (!shmp(handle
, buf
->commit_cold
)) {
356 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
357 cpu
, handle
, shmobj
);
363 * Write the subbuffer header for first subbuffer so we know the total
364 * duration of data gathering.
366 subbuf_header_size
= config
->cb
.subbuffer_header_size();
367 v_set(config
, &buf
->offset
, subbuf_header_size
);
368 wsb
= shmp_index(handle
, buf
->backend
.buf_wsb
, 0);
373 subbuffer_id_clear_noref(config
, &wsb
->id
);
374 shmp_chan
= shmp(handle
, buf
->backend
.chan
);
379 tsc
= config
->cb
.ring_buffer_clock_read(shmp_chan
);
380 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
381 cc_hot
= shmp_index(handle
, buf
->commit_hot
, 0);
386 v_add(config
, subbuf_header_size
, &cc_hot
->cc
);
387 v_add(config
, subbuf_header_size
, &cc_hot
->seq
);
389 if (config
->cb
.buffer_create
) {
390 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
395 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
397 buf
->backend
.allocated
= 1;
402 /* commit_cold will be freed by shm teardown */
404 /* commit_hot will be freed by shm teardown */
410 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
412 const struct lttng_ust_lib_ring_buffer_config
*config
;
413 struct lttng_ust_shm_handle
*handle
;
414 struct channel
*chan
;
417 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
419 chan
= si
->si_value
.sival_ptr
;
420 handle
= chan
->handle
;
421 config
= &chan
->backend
.config
;
423 DBG("Switch timer for channel %p\n", chan
);
426 * Only flush buffers periodically if readers are active.
428 pthread_mutex_lock(&wakeup_fd_mutex
);
429 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
430 for_each_possible_cpu(cpu
) {
431 struct lttng_ust_lib_ring_buffer
*buf
=
432 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
436 if (uatomic_read(&buf
->active_readers
))
437 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
441 struct lttng_ust_lib_ring_buffer
*buf
=
442 shmp(handle
, chan
->backend
.buf
[0].shmp
);
446 if (uatomic_read(&buf
->active_readers
))
447 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
451 pthread_mutex_unlock(&wakeup_fd_mutex
);
456 int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
457 struct lttng_ust_lib_ring_buffer
*buf
,
458 struct channel
*chan
,
459 struct lttng_ust_shm_handle
*handle
)
461 unsigned long consumed_old
, consumed_idx
, commit_count
, write_offset
;
462 struct commit_counters_cold
*cc_cold
;
464 consumed_old
= uatomic_read(&buf
->consumed
);
465 consumed_idx
= subbuf_index(consumed_old
, chan
);
466 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
469 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
471 * No memory barrier here, since we are only interested
472 * in a statistically correct polling result. The next poll will
473 * get the data is we are racing. The mb() that ensures correct
474 * memory order is in get_subbuf.
476 write_offset
= v_read(config
, &buf
->offset
);
479 * Check that the subbuffer we are trying to consume has been
480 * already fully committed.
483 if (((commit_count
- chan
->backend
.subbuf_size
)
484 & chan
->commit_count_mask
)
485 - (buf_trunc(consumed_old
, chan
)
486 >> chan
->backend
.num_subbuf_order
)
491 * Check that we are not about to read the same subbuffer in
492 * which the writer head is.
494 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_old
, chan
)
502 void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer
*buf
,
503 struct lttng_ust_shm_handle
*handle
)
505 int wakeup_fd
= shm_get_wakeup_fd(handle
, &buf
->self
._ref
);
506 sigset_t sigpipe_set
, pending_set
, old_set
;
507 int ret
, sigpipe_was_pending
= 0;
513 * Wake-up the other end by writing a null byte in the pipe
514 * (non-blocking). Important note: Because writing into the
515 * pipe is non-blocking (and therefore we allow dropping wakeup
516 * data, as long as there is wakeup data present in the pipe
517 * buffer to wake up the consumer), the consumer should perform
518 * the following sequence for waiting:
519 * 1) empty the pipe (reads).
520 * 2) check if there is data in the buffer.
521 * 3) wait on the pipe (poll).
523 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
524 * that might be already pending. If a bogus SIGPIPE is sent to
525 * the entire process concurrently by a malicious user, it may
526 * be simply discarded.
528 ret
= sigemptyset(&pending_set
);
531 * sigpending returns the mask of signals that are _both_
532 * blocked for the thread _and_ pending for either the thread or
533 * the entire process.
535 ret
= sigpending(&pending_set
);
537 sigpipe_was_pending
= sigismember(&pending_set
, SIGPIPE
);
539 * If sigpipe was pending, it means it was already blocked, so
540 * no need to block it.
542 if (!sigpipe_was_pending
) {
543 ret
= sigemptyset(&sigpipe_set
);
545 ret
= sigaddset(&sigpipe_set
, SIGPIPE
);
547 ret
= pthread_sigmask(SIG_BLOCK
, &sigpipe_set
, &old_set
);
551 ret
= write(wakeup_fd
, "", 1);
552 } while (ret
== -1L && errno
== EINTR
);
553 if (ret
== -1L && errno
== EPIPE
&& !sigpipe_was_pending
) {
554 struct timespec timeout
= { 0, 0 };
556 ret
= sigtimedwait(&sigpipe_set
, NULL
,
558 } while (ret
== -1L && errno
== EINTR
);
560 if (!sigpipe_was_pending
) {
561 ret
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
567 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
569 const struct lttng_ust_lib_ring_buffer_config
*config
;
570 struct lttng_ust_shm_handle
*handle
;
573 handle
= chan
->handle
;
574 config
= &chan
->backend
.config
;
577 * Only flush buffers periodically if readers are active.
579 pthread_mutex_lock(&wakeup_fd_mutex
);
580 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
581 for_each_possible_cpu(cpu
) {
582 struct lttng_ust_lib_ring_buffer
*buf
=
583 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
587 if (uatomic_read(&buf
->active_readers
)
588 && lib_ring_buffer_poll_deliver(config
, buf
,
590 lib_ring_buffer_wakeup(buf
, handle
);
594 struct lttng_ust_lib_ring_buffer
*buf
=
595 shmp(handle
, chan
->backend
.buf
[0].shmp
);
599 if (uatomic_read(&buf
->active_readers
)
600 && lib_ring_buffer_poll_deliver(config
, buf
,
602 lib_ring_buffer_wakeup(buf
, handle
);
606 pthread_mutex_unlock(&wakeup_fd_mutex
);
610 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
612 struct channel
*chan
;
614 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
615 chan
= si
->si_value
.sival_ptr
;
616 DBG("Read timer for channel %p\n", chan
);
617 lib_ring_buffer_channel_do_read(chan
);
622 void rb_setmask(sigset_t
*mask
)
626 ret
= sigemptyset(mask
);
628 PERROR("sigemptyset");
630 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
634 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
638 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
645 void *sig_thread(void *arg
)
651 /* Only self thread will receive signal mask. */
653 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
656 signr
= sigwaitinfo(&mask
, &info
);
659 PERROR("sigwaitinfo");
662 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
663 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
665 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
666 lib_ring_buffer_channel_read_timer(info
.si_signo
,
668 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
670 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
673 ERR("Unexptected signal %d\n", info
.si_signo
);
680 * Ensure only a single thread listens on the timer signal.
683 void lib_ring_buffer_setup_timer_thread(void)
688 pthread_mutex_lock(&timer_signal
.lock
);
689 if (timer_signal
.setup_done
)
692 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
695 PERROR("pthread_create");
697 ret
= pthread_detach(thread
);
700 PERROR("pthread_detach");
702 timer_signal
.setup_done
= 1;
704 pthread_mutex_unlock(&timer_signal
.lock
);
708 * Wait for signal-handling thread quiescent state.
711 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
713 sigset_t pending_set
;
717 * We need to be the only thread interacting with the thread
718 * that manages signals for teardown synchronization.
720 pthread_mutex_lock(&timer_signal
.lock
);
723 * Ensure we don't have any signal queued for this channel.
726 ret
= sigemptyset(&pending_set
);
728 PERROR("sigemptyset");
730 ret
= sigpending(&pending_set
);
732 PERROR("sigpending");
734 if (!sigismember(&pending_set
, signr
))
740 * From this point, no new signal handler will be fired that
741 * would try to access "chan". However, we still need to wait
742 * for any currently executing handler to complete.
745 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
749 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
752 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
754 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
758 pthread_mutex_unlock(&timer_signal
.lock
);
762 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
765 struct itimerspec its
;
768 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
771 chan
->switch_timer_enabled
= 1;
773 lib_ring_buffer_setup_timer_thread();
775 sev
.sigev_notify
= SIGEV_SIGNAL
;
776 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
777 sev
.sigev_value
.sival_ptr
= chan
;
778 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
780 PERROR("timer_create");
783 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
784 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
785 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
786 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
788 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
790 PERROR("timer_settime");
795 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
799 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
802 ret
= timer_delete(chan
->switch_timer
);
804 PERROR("timer_delete");
807 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
809 chan
->switch_timer
= 0;
810 chan
->switch_timer_enabled
= 0;
814 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
816 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
818 struct itimerspec its
;
821 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
822 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
825 chan
->read_timer_enabled
= 1;
827 lib_ring_buffer_setup_timer_thread();
829 sev
.sigev_notify
= SIGEV_SIGNAL
;
830 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
831 sev
.sigev_value
.sival_ptr
= chan
;
832 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
834 PERROR("timer_create");
837 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
838 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
839 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
840 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
842 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
844 PERROR("timer_settime");
849 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
851 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
854 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
855 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
858 ret
= timer_delete(chan
->read_timer
);
860 PERROR("timer_delete");
864 * do one more check to catch data that has been written in the last
867 lib_ring_buffer_channel_do_read(chan
);
869 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
871 chan
->read_timer
= 0;
872 chan
->read_timer_enabled
= 0;
875 static void channel_unregister_notifiers(struct channel
*chan
,
876 struct lttng_ust_shm_handle
*handle
)
878 lib_ring_buffer_channel_switch_timer_stop(chan
);
879 lib_ring_buffer_channel_read_timer_stop(chan
);
882 static void channel_print_errors(struct channel
*chan
,
883 struct lttng_ust_shm_handle
*handle
)
885 const struct lttng_ust_lib_ring_buffer_config
*config
=
886 &chan
->backend
.config
;
889 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
890 for_each_possible_cpu(cpu
) {
891 struct lttng_ust_lib_ring_buffer
*buf
=
892 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
894 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
897 struct lttng_ust_lib_ring_buffer
*buf
=
898 shmp(handle
, chan
->backend
.buf
[0].shmp
);
901 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
905 static void channel_free(struct channel
*chan
,
906 struct lttng_ust_shm_handle
*handle
,
909 channel_backend_free(&chan
->backend
, handle
);
910 /* chan is freed by shm teardown */
911 shm_object_table_destroy(handle
->table
, consumer
);
916 * channel_create - Create channel.
917 * @config: ring buffer instance configuration
918 * @name: name of the channel
919 * @priv_data: ring buffer client private data area pointer (output)
920 * @priv_data_size: length, in bytes, of the private data area.
921 * @priv_data_init: initialization data for private data.
922 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
923 * address mapping. It is used only by RING_BUFFER_STATIC
924 * configuration. It can be set to NULL for other backends.
925 * @subbuf_size: subbuffer size
926 * @num_subbuf: number of subbuffers
927 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
928 * padding to let readers get those sub-buffers.
929 * Used for live streaming.
930 * @read_timer_interval: Time interval (in us) to wake up pending readers.
931 * @stream_fds: array of stream file descriptors.
932 * @nr_stream_fds: number of file descriptors in array.
935 * Returns NULL on failure.
937 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
940 size_t priv_data_align
,
941 size_t priv_data_size
,
942 void *priv_data_init
,
943 void *buf_addr
, size_t subbuf_size
,
944 size_t num_subbuf
, unsigned int switch_timer_interval
,
945 unsigned int read_timer_interval
,
946 const int *stream_fds
, int nr_stream_fds
)
949 size_t shmsize
, chansize
;
950 struct channel
*chan
;
951 struct lttng_ust_shm_handle
*handle
;
952 struct shm_object
*shmobj
;
953 unsigned int nr_streams
;
955 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
956 nr_streams
= num_possible_cpus();
960 if (nr_stream_fds
!= nr_streams
)
963 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
964 read_timer_interval
))
967 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
971 /* Allocate table for channel + per-cpu buffers */
972 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
974 goto error_table_alloc
;
976 /* Calculate the shm allocation layout */
977 shmsize
= sizeof(struct channel
);
978 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
979 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
982 shmsize
+= offset_align(shmsize
, priv_data_align
);
983 shmsize
+= priv_data_size
;
985 /* Allocate normal memory for channel (not shared) */
986 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
990 /* struct channel is at object 0, offset 0 (hardcoded) */
991 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
992 assert(handle
->chan
._ref
.index
== 0);
993 assert(handle
->chan
._ref
.offset
== 0);
994 chan
= shmp(handle
, handle
->chan
);
997 chan
->nr_streams
= nr_streams
;
999 /* space for private data */
1000 if (priv_data_size
) {
1001 DECLARE_SHMP(void, priv_data_alloc
);
1003 align_shm(shmobj
, priv_data_align
);
1004 chan
->priv_data_offset
= shmobj
->allocated_len
;
1005 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
1006 if (!shmp(handle
, priv_data_alloc
))
1008 *priv_data
= channel_get_private(chan
);
1009 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
1011 chan
->priv_data_offset
= -1;
1016 ret
= channel_backend_init(&chan
->backend
, name
, config
,
1017 subbuf_size
, num_subbuf
, handle
,
1020 goto error_backend_init
;
1022 chan
->handle
= handle
;
1023 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
1025 chan
->switch_timer_interval
= switch_timer_interval
;
1026 chan
->read_timer_interval
= read_timer_interval
;
1027 lib_ring_buffer_channel_switch_timer_start(chan
);
1028 lib_ring_buffer_channel_read_timer_start(chan
);
1034 shm_object_table_destroy(handle
->table
, 1);
1040 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
1041 uint64_t memory_map_size
,
1044 struct lttng_ust_shm_handle
*handle
;
1045 struct shm_object
*object
;
1047 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
1051 /* Allocate table for channel + per-cpu buffers */
1052 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
1054 goto error_table_alloc
;
1055 /* Add channel object */
1056 object
= shm_object_table_append_mem(handle
->table
, data
,
1057 memory_map_size
, wakeup_fd
);
1059 goto error_table_object
;
1060 /* struct channel is at object 0, offset 0 (hardcoded) */
1061 handle
->chan
._ref
.index
= 0;
1062 handle
->chan
._ref
.offset
= 0;
1066 shm_object_table_destroy(handle
->table
, 0);
1072 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
1073 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
1074 uint64_t memory_map_size
)
1076 struct shm_object
*object
;
1078 /* Add stream object */
1079 object
= shm_object_table_append_shm(handle
->table
,
1080 shm_fd
, wakeup_fd
, stream_nr
,
1087 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
1089 assert(handle
->table
);
1090 return handle
->table
->allocated_len
- 1;
1094 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1097 channel_free(chan
, handle
, consumer
);
1101 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1102 * @chan: channel to destroy
1104 * Holds cpu hotplug.
1105 * Call "destroy" callback, finalize channels, decrement the channel
1106 * reference count. Note that when readers have completed data
1107 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1108 * They should release their handle at that point.
1110 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
1115 * Note: the consumer takes care of finalizing and
1116 * switching the buffers.
1118 channel_unregister_notifiers(chan
, handle
);
1120 * The consumer prints errors.
1122 channel_print_errors(chan
, handle
);
1126 * sessiond/consumer are keeping a reference on the shm file
1127 * descriptor directly. No need to refcount.
1129 channel_release(chan
, handle
, consumer
);
1133 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
1134 const struct lttng_ust_lib_ring_buffer_config
*config
,
1135 struct channel
*chan
, int cpu
,
1136 struct lttng_ust_shm_handle
*handle
,
1137 int *shm_fd
, int *wait_fd
,
1139 uint64_t *memory_map_size
)
1141 struct shm_ref
*ref
;
1143 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1146 if (cpu
>= num_possible_cpus())
1149 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1150 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1151 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1152 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1153 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1155 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1158 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1159 struct channel
*chan
,
1160 struct lttng_ust_shm_handle
*handle
)
1162 struct shm_ref
*ref
;
1164 ref
= &handle
->chan
._ref
;
1165 return shm_close_wait_fd(handle
, ref
);
1168 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1169 struct channel
*chan
,
1170 struct lttng_ust_shm_handle
*handle
)
1172 struct shm_ref
*ref
;
1174 ref
= &handle
->chan
._ref
;
1175 return shm_close_wakeup_fd(handle
, ref
);
1178 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1179 struct channel
*chan
,
1180 struct lttng_ust_shm_handle
*handle
,
1183 struct shm_ref
*ref
;
1185 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1188 if (cpu
>= num_possible_cpus())
1191 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1192 return shm_close_wait_fd(handle
, ref
);
1195 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1196 struct channel
*chan
,
1197 struct lttng_ust_shm_handle
*handle
,
1200 struct shm_ref
*ref
;
1203 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1206 if (cpu
>= num_possible_cpus())
1209 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1210 pthread_mutex_lock(&wakeup_fd_mutex
);
1211 ret
= shm_close_wakeup_fd(handle
, ref
);
1212 pthread_mutex_unlock(&wakeup_fd_mutex
);
1216 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
1217 struct lttng_ust_shm_handle
*handle
)
1219 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1225 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
1226 struct lttng_ust_shm_handle
*handle
)
1228 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1232 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1234 uatomic_dec(&buf
->active_readers
);
1238 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1240 * @consumed: consumed count indicating the position where to read
1241 * @produced: produced count, indicates position when to stop reading
1243 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1244 * data to read at consumed position, or 0 if the get operation succeeds.
1247 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1248 unsigned long *consumed
, unsigned long *produced
,
1249 struct lttng_ust_shm_handle
*handle
)
1251 struct channel
*chan
;
1252 const struct lttng_ust_lib_ring_buffer_config
*config
;
1253 unsigned long consumed_cur
, write_offset
;
1256 chan
= shmp(handle
, buf
->backend
.chan
);
1259 config
= &chan
->backend
.config
;
1260 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1262 * Read finalized before counters.
1265 consumed_cur
= uatomic_read(&buf
->consumed
);
1267 * No need to issue a memory barrier between consumed count read and
1268 * write offset read, because consumed count can only change
1269 * concurrently in overwrite mode, and we keep a sequence counter
1270 * identifier derived from the write offset to check we are getting
1271 * the same sub-buffer we are expecting (the sub-buffers are atomically
1272 * "tagged" upon writes, tags are checked upon read).
1274 write_offset
= v_read(config
, &buf
->offset
);
1277 * Check that we are not about to read the same subbuffer in
1278 * which the writer head is.
1280 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1284 *consumed
= consumed_cur
;
1285 *produced
= subbuf_trunc(write_offset
, chan
);
1291 * The memory barriers __wait_event()/wake_up_interruptible() take care
1292 * of "raw_spin_is_locked" memory ordering.
1301 * lib_ring_buffer_move_consumer - move consumed counter forward
1303 * @consumed_new: new consumed count value
1305 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1306 unsigned long consumed_new
,
1307 struct lttng_ust_shm_handle
*handle
)
1309 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1310 struct channel
*chan
;
1311 unsigned long consumed
;
1313 chan
= shmp(handle
, bufb
->chan
);
1316 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1319 * Only push the consumed value forward.
1320 * If the consumed cmpxchg fails, this is because we have been pushed by
1321 * the writer in flight recorder mode.
1323 consumed
= uatomic_read(&buf
->consumed
);
1324 while ((long) consumed
- (long) consumed_new
< 0)
1325 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1330 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1332 * @consumed: consumed count indicating the position where to read
1334 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1335 * data to read at consumed position, or 0 if the get operation succeeds.
1337 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1338 unsigned long consumed
,
1339 struct lttng_ust_shm_handle
*handle
)
1341 struct channel
*chan
;
1342 const struct lttng_ust_lib_ring_buffer_config
*config
;
1343 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1344 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1345 struct commit_counters_cold
*cc_cold
;
1347 chan
= shmp(handle
, buf
->backend
.chan
);
1350 config
= &chan
->backend
.config
;
1352 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1354 * Read finalized before counters.
1357 consumed_cur
= uatomic_read(&buf
->consumed
);
1358 consumed_idx
= subbuf_index(consumed
, chan
);
1359 cc_cold
= shmp_index(handle
, buf
->commit_cold
, consumed_idx
);
1362 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1364 * Make sure we read the commit count before reading the buffer
1365 * data and the write offset. Correct consumed offset ordering
1366 * wrt commit count is insured by the use of cmpxchg to update
1367 * the consumed offset.
1370 * Local rmb to match the remote wmb to read the commit count
1371 * before the buffer data and the write offset.
1375 write_offset
= v_read(config
, &buf
->offset
);
1378 * Check that the buffer we are getting is after or at consumed_cur
1381 if ((long) subbuf_trunc(consumed
, chan
)
1382 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1386 * Check that the subbuffer we are trying to consume has been
1387 * already fully committed. There are a few causes that can make
1388 * this unavailability situation occur:
1390 * Temporary (short-term) situation:
1391 * - Application is running on a different CPU, between reserve
1392 * and commit ring buffer operations,
1393 * - Application is preempted between reserve and commit ring
1394 * buffer operations,
1396 * Long-term situation:
1397 * - Application is stopped (SIGSTOP) between reserve and commit
1398 * ring buffer operations. Could eventually be resumed by
1400 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1401 * reserve and commit ring buffer operation.
1403 * From a consumer perspective, handling short-term
1404 * unavailability situations is performed by retrying a few
1405 * times after a delay. Handling long-term unavailability
1406 * situations is handled by failing to get the sub-buffer.
1408 * In all of those situations, if the application is taking a
1409 * long time to perform its commit after ring buffer space
1410 * reservation, we can end up in a situation where the producer
1411 * will fill the ring buffer and try to write into the same
1412 * sub-buffer again (which has a missing commit). This is
1413 * handled by the producer in the sub-buffer switch handling
1414 * code of the reserve routine by detecting unbalanced
1415 * reserve/commit counters and discarding all further events
1416 * until the situation is resolved in those situations. Two
1417 * scenarios can occur:
1419 * 1) The application causing the reserve/commit counters to be
1420 * unbalanced has been terminated. In this situation, all
1421 * further events will be discarded in the buffers, and no
1422 * further buffer data will be readable by the consumer
1423 * daemon. Tearing down the UST tracing session and starting
1424 * anew is a work-around for those situations. Note that this
1425 * only affects per-UID tracing. In per-PID tracing, the
1426 * application vanishes with the termination, and therefore
1427 * no more data needs to be written to the buffers.
1428 * 2) The application causing the unbalance has been delayed for
1429 * a long time, but will eventually try to increment the
1430 * commit counter after eventually writing to the sub-buffer.
1431 * This situation can cause events to be discarded until the
1432 * application resumes its operations.
1434 if (((commit_count
- chan
->backend
.subbuf_size
)
1435 & chan
->commit_count_mask
)
1436 - (buf_trunc(consumed
, chan
)
1437 >> chan
->backend
.num_subbuf_order
)
1439 if (nr_retry
-- > 0) {
1440 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1441 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1449 * Check that we are not about to read the same subbuffer in
1450 * which the writer head is.
1452 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1457 * Failure to get the subbuffer causes a busy-loop retry without going
1458 * to a wait queue. These are caused by short-lived race windows where
1459 * the writer is getting access to a subbuffer we were trying to get
1460 * access to. Also checks that the "consumed" buffer count we are
1461 * looking for matches the one contained in the subbuffer id.
1463 * The short-lived race window described here can be affected by
1464 * application signals and preemption, thus requiring to bound
1465 * the loop to a maximum number of retry.
1467 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1468 consumed_idx
, buf_trunc_val(consumed
, chan
),
1471 if (nr_retry
-- > 0) {
1472 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1473 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1479 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1481 buf
->get_subbuf_consumed
= consumed
;
1482 buf
->get_subbuf
= 1;
1488 * The memory barriers __wait_event()/wake_up_interruptible() take care
1489 * of "raw_spin_is_locked" memory ordering.
1498 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1501 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1502 struct lttng_ust_shm_handle
*handle
)
1504 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1505 struct channel
*chan
;
1506 const struct lttng_ust_lib_ring_buffer_config
*config
;
1507 unsigned long sb_bindex
, consumed_idx
, consumed
;
1508 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
1509 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
1511 chan
= shmp(handle
, bufb
->chan
);
1514 config
= &chan
->backend
.config
;
1515 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1517 if (!buf
->get_subbuf
) {
1519 * Reader puts a subbuffer it did not get.
1521 CHAN_WARN_ON(chan
, 1);
1524 consumed
= buf
->get_subbuf_consumed
;
1525 buf
->get_subbuf
= 0;
1528 * Clear the records_unread counter. (overruns counter)
1529 * Can still be non-zero if a file reader simply grabbed the data
1530 * without using iterators.
1531 * Can be below zero if an iterator is used on a snapshot more than
1534 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1535 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
1538 backend_pages
= shmp(handle
, rpages
->shmp
);
1541 v_add(config
, v_read(config
, &backend_pages
->records_unread
),
1542 &bufb
->records_read
);
1543 v_set(config
, &backend_pages
->records_unread
, 0);
1544 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1545 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1546 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1549 * Exchange the reader subbuffer with the one we put in its place in the
1550 * writer subbuffer table. Expect the original consumed count. If
1551 * update_read_sb_index fails, this is because the writer updated the
1552 * subbuffer concurrently. We should therefore keep the subbuffer we
1553 * currently have: it has become invalid to try reading this sub-buffer
1554 * consumed count value anyway.
1556 consumed_idx
= subbuf_index(consumed
, chan
);
1557 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1558 consumed_idx
, buf_trunc_val(consumed
, chan
),
1561 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1562 * if the writer concurrently updated it.
1567 * cons_offset is an iterator on all subbuffer offsets between the reader
1568 * position and the writer position. (inclusive)
1571 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1572 struct channel
*chan
,
1573 unsigned long cons_offset
,
1575 struct lttng_ust_shm_handle
*handle
)
1577 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1578 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1579 struct commit_counters_hot
*cc_hot
;
1580 struct commit_counters_cold
*cc_cold
;
1582 cons_idx
= subbuf_index(cons_offset
, chan
);
1583 cc_hot
= shmp_index(handle
, buf
->commit_hot
, cons_idx
);
1586 cc_cold
= shmp_index(handle
, buf
->commit_cold
, cons_idx
);
1589 commit_count
= v_read(config
, &cc_hot
->cc
);
1590 commit_count
+= cc_hot
->cc_rseq
;
1591 commit_count_sb
= v_read(config
, &cc_cold
->cc_sb
);
1593 if (subbuf_offset(commit_count
, chan
) != 0)
1594 DBG("ring buffer %s, cpu %d: "
1595 "commit count in subbuffer %lu,\n"
1596 "expecting multiples of %lu bytes\n"
1597 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1598 chan
->backend
.name
, cpu
, cons_idx
,
1599 chan
->backend
.subbuf_size
,
1600 commit_count
, commit_count_sb
);
1602 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1603 chan
->backend
.name
, cpu
, commit_count
);
1607 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1608 struct channel
*chan
,
1609 void *priv
, int cpu
,
1610 struct lttng_ust_shm_handle
*handle
)
1612 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1613 unsigned long write_offset
, cons_offset
;
1616 * No need to order commit_count, write_offset and cons_offset reads
1617 * because we execute at teardown when no more writer nor reader
1618 * references are left.
1620 write_offset
= v_read(config
, &buf
->offset
);
1621 cons_offset
= uatomic_read(&buf
->consumed
);
1622 if (write_offset
!= cons_offset
)
1623 DBG("ring buffer %s, cpu %d: "
1624 "non-consumed data\n"
1625 " [ %lu bytes written, %lu bytes read ]\n",
1626 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1628 for (cons_offset
= uatomic_read(&buf
->consumed
);
1629 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1632 cons_offset
= subbuf_align(cons_offset
, chan
))
1633 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1638 void lib_ring_buffer_print_errors(struct channel
*chan
,
1639 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1640 struct lttng_ust_shm_handle
*handle
)
1642 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1643 void *priv
= channel_get_private(chan
);
1645 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1646 DBG("ring buffer %s: %lu records written, "
1647 "%lu records overrun\n",
1649 v_read(config
, &buf
->records_count
),
1650 v_read(config
, &buf
->records_overrun
));
1652 DBG("ring buffer %s, cpu %d: %lu records written, "
1653 "%lu records overrun\n",
1654 chan
->backend
.name
, cpu
,
1655 v_read(config
, &buf
->records_count
),
1656 v_read(config
, &buf
->records_overrun
));
1658 if (v_read(config
, &buf
->records_lost_full
)
1659 || v_read(config
, &buf
->records_lost_wrap
)
1660 || v_read(config
, &buf
->records_lost_big
))
1661 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1662 " [ %lu buffer full, %lu nest buffer wrap-around, "
1663 "%lu event too big ]\n",
1664 chan
->backend
.name
, cpu
,
1665 v_read(config
, &buf
->records_lost_full
),
1666 v_read(config
, &buf
->records_lost_wrap
),
1667 v_read(config
, &buf
->records_lost_big
));
1669 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1673 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1675 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1676 * active or at buffer finalization (destroy).
1679 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1680 struct channel
*chan
,
1681 struct switch_offsets
*offsets
,
1683 struct lttng_ust_shm_handle
*handle
)
1685 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1686 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1687 unsigned long commit_count
;
1688 struct commit_counters_hot
*cc_hot
;
1690 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1693 * Order all writes to buffer before the commit count update that will
1694 * determine that the subbuffer is full.
1697 cc_hot
= shmp_index(handle
, buf
->commit_hot
, oldidx
);
1700 v_add(config
, config
->cb
.subbuffer_header_size(),
1702 commit_count
= v_read(config
, &cc_hot
->cc
);
1703 commit_count
+= cc_hot
->cc_rseq
;
1704 /* Check if the written buffer has to be delivered */
1705 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1706 commit_count
, oldidx
, handle
, tsc
);
1707 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1708 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1709 commit_count
, handle
, cc_hot
);
1713 * lib_ring_buffer_switch_old_end: switch old subbuffer
1715 * Note : offset_old should never be 0 here. It is ok, because we never perform
1716 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1717 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1721 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1722 struct channel
*chan
,
1723 struct switch_offsets
*offsets
,
1725 struct lttng_ust_shm_handle
*handle
)
1727 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1728 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1729 unsigned long commit_count
, padding_size
, data_size
;
1730 struct commit_counters_hot
*cc_hot
;
1732 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1733 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1734 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
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
, padding_size
, &cc_hot
->cc
);
1746 commit_count
= v_read(config
, &cc_hot
->cc
);
1747 commit_count
+= cc_hot
->cc_rseq
;
1748 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1749 commit_count
, oldidx
, handle
, tsc
);
1750 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1751 offsets
->old
+ padding_size
, commit_count
, handle
,
1756 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1758 * This code can be executed unordered : writers may already have written to the
1759 * sub-buffer before this code gets executed, caution. The commit makes sure
1760 * that this code is executed before the deliver of this sub-buffer.
1763 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1764 struct channel
*chan
,
1765 struct switch_offsets
*offsets
,
1767 struct lttng_ust_shm_handle
*handle
)
1769 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1770 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1771 unsigned long commit_count
;
1772 struct commit_counters_hot
*cc_hot
;
1774 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1777 * Order all writes to buffer before the commit count update that will
1778 * determine that the subbuffer is full.
1781 cc_hot
= shmp_index(handle
, buf
->commit_hot
, beginidx
);
1784 v_add(config
, config
->cb
.subbuffer_header_size(), &cc_hot
->cc
);
1785 commit_count
= v_read(config
, &cc_hot
->cc
);
1786 commit_count
+= cc_hot
->cc_rseq
;
1787 /* Check if the written buffer has to be delivered */
1788 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1789 commit_count
, beginidx
, handle
, tsc
);
1790 lib_ring_buffer_write_commit_counter(config
, buf
, chan
,
1791 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1792 commit_count
, handle
, cc_hot
);
1796 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1798 * Calls subbuffer_set_data_size() to set the data size of the current
1799 * sub-buffer. We do not need to perform check_deliver nor commit here,
1800 * since this task will be done by the "commit" of the event for which
1801 * we are currently doing the space reservation.
1804 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1805 struct channel
*chan
,
1806 struct switch_offsets
*offsets
,
1808 struct lttng_ust_shm_handle
*handle
)
1810 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1811 unsigned long endidx
, data_size
;
1813 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1814 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1815 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1822 * !0 if execution must be aborted.
1825 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1826 struct lttng_ust_lib_ring_buffer
*buf
,
1827 struct channel
*chan
,
1828 struct switch_offsets
*offsets
,
1830 struct lttng_ust_shm_handle
*handle
)
1832 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1833 unsigned long off
, reserve_commit_diff
;
1835 offsets
->begin
= v_read(config
, &buf
->offset
);
1836 offsets
->old
= offsets
->begin
;
1837 offsets
->switch_old_start
= 0;
1838 off
= subbuf_offset(offsets
->begin
, chan
);
1840 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1843 * Ensure we flush the header of an empty subbuffer when doing the
1844 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1845 * total data gathering duration even if there were no records saved
1846 * after the last buffer switch.
1847 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1848 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1849 * subbuffer header as appropriate.
1850 * The next record that reserves space will be responsible for
1851 * populating the following subbuffer header. We choose not to populate
1852 * the next subbuffer header here because we want to be able to use
1853 * SWITCH_ACTIVE for periodical buffer flush, which must
1854 * guarantee that all the buffer content (records and header
1855 * timestamps) are visible to the reader. This is required for
1856 * quiescence guarantees for the fusion merge.
1858 if (mode
!= SWITCH_FLUSH
&& !off
)
1859 return -1; /* we do not have to switch : buffer is empty */
1861 if (caa_unlikely(off
== 0)) {
1862 unsigned long sb_index
, commit_count
;
1863 struct commit_counters_cold
*cc_cold
;
1866 * We are performing a SWITCH_FLUSH. There may be concurrent
1867 * writes into the buffer if e.g. invoked while performing a
1868 * snapshot on an active trace.
1870 * If the client does not save any header information
1871 * (sub-buffer header size == 0), don't switch empty subbuffer
1872 * on finalize, because it is invalid to deliver a completely
1875 if (!config
->cb
.subbuffer_header_size())
1878 /* Test new buffer integrity */
1879 sb_index
= subbuf_index(offsets
->begin
, chan
);
1880 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
1883 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
1884 reserve_commit_diff
=
1885 (buf_trunc(offsets
->begin
, chan
)
1886 >> chan
->backend
.num_subbuf_order
)
1887 - (commit_count
& chan
->commit_count_mask
);
1888 if (caa_likely(reserve_commit_diff
== 0)) {
1889 /* Next subbuffer not being written to. */
1890 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1891 subbuf_trunc(offsets
->begin
, chan
)
1892 - subbuf_trunc((unsigned long)
1893 uatomic_read(&buf
->consumed
), chan
)
1894 >= chan
->backend
.buf_size
)) {
1896 * We do not overwrite non consumed buffers
1897 * and we are full : don't switch.
1902 * Next subbuffer not being written to, and we
1903 * are either in overwrite mode or the buffer is
1904 * not full. It's safe to write in this new
1910 * Next subbuffer reserve offset does not match the
1911 * commit offset. Don't perform switch in
1912 * producer-consumer and overwrite mode. Caused by
1913 * either a writer OOPS or too many nested writes over a
1914 * reserve/commit pair.
1920 * Need to write the subbuffer start header on finalize.
1922 offsets
->switch_old_start
= 1;
1924 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1925 /* Note: old points to the next subbuf at offset 0 */
1926 offsets
->end
= offsets
->begin
;
1931 * Force a sub-buffer switch. This operation is completely reentrant : can be
1932 * called while tracing is active with absolutely no lock held.
1934 * Note, however, that as a v_cmpxchg is used for some atomic
1935 * operations, this function must be called from the CPU which owns the buffer
1936 * for a ACTIVE flush.
1938 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1939 struct lttng_ust_shm_handle
*handle
)
1941 struct channel
*chan
;
1942 const struct lttng_ust_lib_ring_buffer_config
*config
;
1943 struct switch_offsets offsets
;
1944 unsigned long oldidx
;
1947 chan
= shmp(handle
, buf
->backend
.chan
);
1950 config
= &chan
->backend
.config
;
1955 * Perform retryable operations.
1958 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1960 return; /* Switch not needed */
1961 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1965 * Atomically update last_tsc. This update races against concurrent
1966 * atomic updates, but the race will always cause supplementary full TSC
1967 * records, never the opposite (missing a full TSC record when it would
1970 save_last_tsc(config
, buf
, tsc
);
1973 * Push the reader if necessary
1975 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1977 oldidx
= subbuf_index(offsets
.old
, chan
);
1978 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1981 * May need to populate header start on SWITCH_FLUSH.
1983 if (offsets
.switch_old_start
) {
1984 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1985 offsets
.old
+= config
->cb
.subbuffer_header_size();
1989 * Switch old subbuffer.
1991 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1997 * -ENOSPC if event size is too large for packet.
1998 * -ENOBUFS if there is currently not enough space in buffer for the event.
1999 * -EIO if data cannot be written into the buffer for any other reason.
2002 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
2003 struct channel
*chan
,
2004 struct switch_offsets
*offsets
,
2005 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
2007 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2008 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2009 unsigned long reserve_commit_diff
, offset_cmp
;
2012 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
2013 offsets
->old
= offsets
->begin
;
2014 offsets
->switch_new_start
= 0;
2015 offsets
->switch_new_end
= 0;
2016 offsets
->switch_old_end
= 0;
2017 offsets
->pre_header_padding
= 0;
2019 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
2020 if ((int64_t) ctx
->tsc
== -EIO
)
2023 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
2024 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
2026 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
2027 offsets
->switch_new_start
= 1; /* For offsets->begin */
2029 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
2031 &offsets
->pre_header_padding
,
2034 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2037 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
2038 offsets
->size
> chan
->backend
.subbuf_size
)) {
2039 offsets
->switch_old_end
= 1; /* For offsets->old */
2040 offsets
->switch_new_start
= 1; /* For offsets->begin */
2043 if (caa_unlikely(offsets
->switch_new_start
)) {
2044 unsigned long sb_index
, commit_count
;
2045 struct commit_counters_cold
*cc_cold
;
2048 * We are typically not filling the previous buffer completely.
2050 if (caa_likely(offsets
->switch_old_end
))
2051 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
2052 offsets
->begin
= offsets
->begin
2053 + config
->cb
.subbuffer_header_size();
2054 /* Test new buffer integrity */
2055 sb_index
= subbuf_index(offsets
->begin
, chan
);
2057 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2058 * lib_ring_buffer_check_deliver() has the matching
2059 * memory barriers required around commit_cold cc_sb
2060 * updates to ensure reserve and commit counter updates
2061 * are not seen reordered when updated by another CPU.
2064 cc_cold
= shmp_index(handle
, buf
->commit_cold
, sb_index
);
2067 commit_count
= v_read(config
, &cc_cold
->cc_sb
);
2068 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2070 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
2072 * The reserve counter have been concurrently updated
2073 * while we read the commit counter. This means the
2074 * commit counter we read might not match buf->offset
2075 * due to concurrent update. We therefore need to retry.
2079 reserve_commit_diff
=
2080 (buf_trunc(offsets
->begin
, chan
)
2081 >> chan
->backend
.num_subbuf_order
)
2082 - (commit_count
& chan
->commit_count_mask
);
2083 if (caa_likely(reserve_commit_diff
== 0)) {
2084 /* Next subbuffer not being written to. */
2085 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
2086 subbuf_trunc(offsets
->begin
, chan
)
2087 - subbuf_trunc((unsigned long)
2088 uatomic_read(&buf
->consumed
), chan
)
2089 >= chan
->backend
.buf_size
)) {
2090 unsigned long nr_lost
;
2093 * We do not overwrite non consumed buffers
2094 * and we are full : record is lost.
2096 nr_lost
= v_read(config
, &buf
->records_lost_full
);
2097 v_inc(config
, &buf
->records_lost_full
);
2098 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2099 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2100 nr_lost
+ 1, chan
->backend
.name
,
2106 * Next subbuffer not being written to, and we
2107 * are either in overwrite mode or the buffer is
2108 * not full. It's safe to write in this new
2113 unsigned long nr_lost
;
2116 * Next subbuffer reserve offset does not match the
2117 * commit offset, and this did not involve update to the
2118 * reserve counter. Drop record in producer-consumer and
2119 * overwrite mode. Caused by either a writer OOPS or too
2120 * many nested writes over a reserve/commit pair.
2122 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
2123 v_inc(config
, &buf
->records_lost_wrap
);
2124 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2125 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2126 nr_lost
+ 1, chan
->backend
.name
,
2132 config
->cb
.record_header_size(config
, chan
,
2134 &offsets
->pre_header_padding
,
2137 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
2140 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
2141 + offsets
->size
> chan
->backend
.subbuf_size
)) {
2142 unsigned long nr_lost
;
2145 * Record too big for subbuffers, report error, don't
2146 * complete the sub-buffer switch.
2148 nr_lost
= v_read(config
, &buf
->records_lost_big
);
2149 v_inc(config
, &buf
->records_lost_big
);
2150 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
2151 DBG("%lu or more records lost in (%s:%d) record size "
2152 " of %zu bytes is too large for buffer\n",
2153 nr_lost
+ 1, chan
->backend
.name
,
2154 buf
->backend
.cpu
, offsets
->size
);
2159 * We just made a successful buffer switch and the
2160 * record fits in the new subbuffer. Let's write.
2165 * Record fits in the current buffer and we are not on a switch
2166 * boundary. It's safe to write.
2169 offsets
->end
= offsets
->begin
+ offsets
->size
;
2171 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
2173 * The offset_end will fall at the very beginning of the next
2176 offsets
->switch_new_end
= 1; /* For offsets->begin */
2182 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2183 * @ctx: ring buffer context.
2185 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2186 * -EIO for other errors, else returns 0.
2187 * It will take care of sub-buffer switching.
2189 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
2191 struct channel
*chan
= ctx
->chan
;
2192 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
2193 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
2194 struct lttng_ust_lib_ring_buffer
*buf
;
2195 struct switch_offsets offsets
;
2197 struct lttng_rseq_state rseq_state
;
2199 if (caa_likely(ctx
->ctx_len
2200 >= sizeof(struct lttng_ust_lib_ring_buffer_ctx
))) {
2201 rseq_state
= ctx
->rseq_state
;
2203 rseq_state
.cpu_id
= -2;
2204 rseq_state
.event_counter
= 0;
2205 rseq_state
.rseqp
= NULL
;
2208 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
2209 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
2211 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2218 if (caa_unlikely(config
->sync
== RING_BUFFER_SYNC_GLOBAL
2219 || rseq_state
.cpu_id
< 0
2220 || uatomic_read(&chan
->u
.reserve_fallback_ref
))) {
2222 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
,
2224 if (caa_unlikely(ret
))
2226 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
,
2227 offsets
.old
, offsets
.end
)
2230 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
,
2232 if (caa_unlikely(ret
))
2234 if (caa_unlikely(buf
->offset
.a
!= offsets
.old
))
2236 if (caa_unlikely(!__rseq_finish(NULL
, 0, NULL
, NULL
, 0,
2237 (intptr_t *) &buf
->offset
.a
,
2238 (intptr_t) offsets
.end
,
2239 rseq_state
, RSEQ_FINISH_SINGLE
, false)))
2244 * Atomically update last_tsc. This update races against concurrent
2245 * atomic updates, but the race will always cause supplementary full TSC
2246 * records, never the opposite (missing a full TSC record when it would
2249 save_last_tsc(config
, buf
, ctx
->tsc
);
2252 * Push the reader if necessary
2254 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2257 * Clear noref flag for this subbuffer.
2259 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2260 subbuf_index(offsets
.end
- 1, chan
),
2264 * Switch old subbuffer if needed.
2266 if (caa_unlikely(offsets
.switch_old_end
)) {
2267 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2268 subbuf_index(offsets
.old
- 1, chan
),
2270 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2274 * Populate new subbuffer.
2276 if (caa_unlikely(offsets
.switch_new_start
))
2277 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2279 if (caa_unlikely(offsets
.switch_new_end
))
2280 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2282 ctx
->slot_size
= offsets
.size
;
2283 ctx
->pre_offset
= offsets
.begin
;
2284 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2289 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config
*config
,
2290 struct lttng_ust_lib_ring_buffer
*buf
,
2291 unsigned long commit_count
,
2293 struct lttng_ust_shm_handle
*handle
)
2295 struct commit_counters_hot
*cc_hot
;
2297 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
2299 cc_hot
= shmp_index(handle
, buf
->commit_hot
, idx
);
2302 v_set(config
, &cc_hot
->seq
, commit_count
);
2306 * The ring buffer can count events recorded and overwritten per buffer,
2307 * but it is disabled by default due to its performance overhead.
2309 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2311 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2312 struct lttng_ust_lib_ring_buffer
*buf
,
2314 struct lttng_ust_shm_handle
*handle
)
2316 v_add(config
, subbuffer_get_records_count(config
,
2317 &buf
->backend
, idx
, handle
),
2318 &buf
->records_count
);
2319 v_add(config
, subbuffer_count_records_overrun(config
,
2320 &buf
->backend
, idx
, handle
),
2321 &buf
->records_overrun
);
2323 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2325 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config
*config
,
2326 struct lttng_ust_lib_ring_buffer
*buf
,
2328 struct lttng_ust_shm_handle
*handle
)
2331 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2333 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config
*config
,
2334 struct lttng_ust_lib_ring_buffer
*buf
,
2335 struct channel
*chan
,
2336 unsigned long offset
,
2337 unsigned long commit_count
,
2339 struct lttng_ust_shm_handle
*handle
,
2342 unsigned long old_commit_count
= commit_count
2343 - chan
->backend
.subbuf_size
;
2344 struct commit_counters_cold
*cc_cold
;
2347 * If we succeeded at updating cc_sb below, we are the subbuffer
2348 * writer delivering the subbuffer. Deals with concurrent
2349 * updates of the "cc" value without adding a add_return atomic
2350 * operation to the fast path.
2352 * We are doing the delivery in two steps:
2353 * - First, we cmpxchg() cc_sb to the new value
2354 * old_commit_count + 1. This ensures that we are the only
2355 * subbuffer user successfully filling the subbuffer, but we
2356 * do _not_ set the cc_sb value to "commit_count" yet.
2357 * Therefore, other writers that would wrap around the ring
2358 * buffer and try to start writing to our subbuffer would
2359 * have to drop records, because it would appear as
2361 * We therefore have exclusive access to the subbuffer control
2362 * structures. This mutual exclusion with other writers is
2363 * crucially important to perform record overruns count in
2364 * flight recorder mode locklessly.
2365 * - When we are ready to release the subbuffer (either for
2366 * reading or for overrun by other writers), we simply set the
2367 * cc_sb value to "commit_count" and perform delivery.
2369 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2370 * This guarantees that old_commit_count + 1 != commit_count.
2374 * Order prior updates to reserve count prior to the
2375 * commit_cold cc_sb update.
2378 cc_cold
= shmp_index(handle
, buf
->commit_cold
, idx
);
2381 if (caa_likely(v_cmpxchg(config
, &cc_cold
->cc_sb
,
2382 old_commit_count
, old_commit_count
+ 1)
2383 == old_commit_count
)) {
2385 * Start of exclusive subbuffer access. We are
2386 * guaranteed to be the last writer in this subbuffer
2387 * and any other writer trying to access this subbuffer
2388 * in this state is required to drop records.
2390 deliver_count_events(config
, buf
, idx
, handle
);
2391 config
->cb
.buffer_end(buf
, tsc
, idx
,
2392 lib_ring_buffer_get_data_size(config
,
2399 * Increment the packet counter while we have exclusive
2402 subbuffer_inc_packet_count(config
, &buf
->backend
, idx
, handle
);
2405 * Set noref flag and offset for this subbuffer id.
2406 * Contains a memory barrier that ensures counter stores
2407 * are ordered before set noref and offset.
2409 lib_ring_buffer_set_noref_offset(config
, &buf
->backend
, idx
,
2410 buf_trunc_val(offset
, chan
), handle
);
2413 * Order set_noref and record counter updates before the
2414 * end of subbuffer exclusive access. Orders with
2415 * respect to writers coming into the subbuffer after
2416 * wrap around, and also order wrt concurrent readers.
2419 /* End of exclusive subbuffer access */
2420 v_set(config
, &cc_cold
->cc_sb
, commit_count
);
2422 * Order later updates to reserve count after
2423 * the commit cold cc_sb update.
2426 lib_ring_buffer_vmcore_check_deliver(config
, buf
,
2427 commit_count
, idx
, handle
);
2430 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2432 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_WRITER
2433 && uatomic_read(&buf
->active_readers
)
2434 && lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
2435 lib_ring_buffer_wakeup(buf
, handle
);
2441 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2443 void lttng_fixup_ringbuffer_tls(void)
2445 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2448 void lib_ringbuffer_signal_init(void)
2454 * Block signal for entire process, so only our thread processes
2458 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2461 PERROR("pthread_sigmask");