2 * ring_buffer_frontend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
41 * Bob Wisniewski <bob@watson.ibm.com>
43 * Buffer reader semantic :
46 * while buffer is not finalized and empty
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
55 #include <sys/types.h>
62 #include <urcu/compiler.h>
64 #include <urcu/tls-compat.h>
68 #include <lttng/ringbuffer-config.h>
74 #include "../liblttng-ust/compat.h" /* For ENODATA */
77 #define max(a, b) ((a) > (b) ? (a) : (b))
80 /* Print DBG() messages about events lost only every 1048576 hits */
81 #define DBG_PRINT_NR_LOST (1UL << 20)
83 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
84 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
85 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
86 #define CLOCKID CLOCK_MONOTONIC
87 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
88 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
91 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
92 * close(2) to close the fd returned by shm_open.
93 * shm_unlink releases the shared memory object name.
94 * ftruncate(2) sets the size of the memory object.
95 * mmap/munmap maps the shared memory obj to a virtual address in the
96 * calling proceess (should be done both in libust and consumer).
97 * See shm_overview(7) for details.
98 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
101 * Since we don't need to access the object using its name, we can
102 * immediately shm_unlink(3) it, and only keep the handle with its file
107 * Internal structure representing offsets to use at a sub-buffer switch.
109 struct switch_offsets
{
110 unsigned long begin
, end
, old
;
111 size_t pre_header_padding
, size
;
112 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
116 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
119 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
122 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
125 void lib_ring_buffer_print_errors(struct channel
*chan
,
126 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
127 struct lttng_ust_shm_handle
*handle
);
130 * Handle timer teardown race wrt memory free of private data by
131 * ring buffer signals are handled by a single thread, which permits
132 * a synchronization point between handling of each signal.
133 * Protected by the lock within the structure.
135 struct timer_signal_data
{
136 pthread_t tid
; /* thread id managing signals */
139 pthread_mutex_t lock
;
142 static struct timer_signal_data timer_signal
= {
146 .lock
= PTHREAD_MUTEX_INITIALIZER
,
150 * lib_ring_buffer_reset - Reset ring buffer to initial values.
153 * Effectively empty the ring buffer. Should be called when the buffer is not
154 * used for writing. The ring buffer can be opened for reading, but the reader
155 * should not be using the iterator concurrently with reset. The previous
156 * current iterator record is reset.
158 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
159 struct lttng_ust_shm_handle
*handle
)
161 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
162 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
166 * Reset iterator first. It will put the subbuffer if it currently holds
169 v_set(config
, &buf
->offset
, 0);
170 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
171 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
172 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
173 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
175 uatomic_set(&buf
->consumed
, 0);
176 uatomic_set(&buf
->record_disabled
, 0);
177 v_set(config
, &buf
->last_tsc
, 0);
178 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
179 /* Don't reset number of active readers */
180 v_set(config
, &buf
->records_lost_full
, 0);
181 v_set(config
, &buf
->records_lost_wrap
, 0);
182 v_set(config
, &buf
->records_lost_big
, 0);
183 v_set(config
, &buf
->records_count
, 0);
184 v_set(config
, &buf
->records_overrun
, 0);
189 * channel_reset - Reset channel to initial values.
192 * Effectively empty the channel. Should be called when the channel is not used
193 * for writing. The channel can be opened for reading, but the reader should not
194 * be using the iterator concurrently with reset. The previous current iterator
197 void channel_reset(struct channel
*chan
)
200 * Reset iterators first. Will put the subbuffer if held for reading.
202 uatomic_set(&chan
->record_disabled
, 0);
203 /* Don't reset commit_count_mask, still valid */
204 channel_backend_reset(&chan
->backend
);
205 /* Don't reset switch/read timer interval */
206 /* Don't reset notifiers and notifier enable bits */
207 /* Don't reset reader reference count */
211 * Must be called under cpu hotplug protection.
213 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
214 struct channel_backend
*chanb
, int cpu
,
215 struct lttng_ust_shm_handle
*handle
,
216 struct shm_object
*shmobj
)
218 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
219 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
220 void *priv
= channel_get_private(chan
);
221 size_t subbuf_header_size
;
225 /* Test for cpu hotplug */
226 if (buf
->backend
.allocated
)
229 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
230 cpu
, handle
, shmobj
);
234 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
235 set_shmp(buf
->commit_hot
,
237 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
238 if (!shmp(handle
, buf
->commit_hot
)) {
243 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
244 set_shmp(buf
->commit_cold
,
246 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
247 if (!shmp(handle
, buf
->commit_cold
)) {
253 * Write the subbuffer header for first subbuffer so we know the total
254 * duration of data gathering.
256 subbuf_header_size
= config
->cb
.subbuffer_header_size();
257 v_set(config
, &buf
->offset
, subbuf_header_size
);
258 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
259 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
260 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
261 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
263 if (config
->cb
.buffer_create
) {
264 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
268 buf
->backend
.allocated
= 1;
273 /* commit_cold will be freed by shm teardown */
275 /* commit_hot will be freed by shm teardown */
281 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
283 const struct lttng_ust_lib_ring_buffer_config
*config
;
284 struct lttng_ust_shm_handle
*handle
;
285 struct channel
*chan
;
288 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
290 chan
= si
->si_value
.sival_ptr
;
291 handle
= chan
->handle
;
292 config
= &chan
->backend
.config
;
294 DBG("Switch timer for channel %p\n", chan
);
297 * Only flush buffers periodically if readers are active.
299 pthread_mutex_lock(&wakeup_fd_mutex
);
300 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
301 for_each_possible_cpu(cpu
) {
302 struct lttng_ust_lib_ring_buffer
*buf
=
303 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
304 if (uatomic_read(&buf
->active_readers
))
305 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
309 struct lttng_ust_lib_ring_buffer
*buf
=
310 shmp(handle
, chan
->backend
.buf
[0].shmp
);
312 if (uatomic_read(&buf
->active_readers
))
313 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
316 pthread_mutex_unlock(&wakeup_fd_mutex
);
321 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
323 const struct lttng_ust_lib_ring_buffer_config
*config
;
324 struct lttng_ust_shm_handle
*handle
;
327 handle
= chan
->handle
;
328 config
= &chan
->backend
.config
;
331 * Only flush buffers periodically if readers are active.
333 pthread_mutex_lock(&wakeup_fd_mutex
);
334 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
335 for_each_possible_cpu(cpu
) {
336 struct lttng_ust_lib_ring_buffer
*buf
=
337 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
339 if (uatomic_read(&buf
->active_readers
)
340 && lib_ring_buffer_poll_deliver(config
, buf
,
342 lib_ring_buffer_wakeup(buf
, handle
);
346 struct lttng_ust_lib_ring_buffer
*buf
=
347 shmp(handle
, chan
->backend
.buf
[0].shmp
);
349 if (uatomic_read(&buf
->active_readers
)
350 && lib_ring_buffer_poll_deliver(config
, buf
,
352 lib_ring_buffer_wakeup(buf
, handle
);
355 pthread_mutex_unlock(&wakeup_fd_mutex
);
359 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
361 struct channel
*chan
;
363 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
364 chan
= si
->si_value
.sival_ptr
;
365 DBG("Read timer for channel %p\n", chan
);
366 lib_ring_buffer_channel_do_read(chan
);
371 void rb_setmask(sigset_t
*mask
)
375 ret
= sigemptyset(mask
);
377 PERROR("sigemptyset");
379 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
383 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
387 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
394 void *sig_thread(void *arg
)
400 /* Only self thread will receive signal mask. */
402 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
405 signr
= sigwaitinfo(&mask
, &info
);
408 PERROR("sigwaitinfo");
411 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
412 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
414 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
415 lib_ring_buffer_channel_read_timer(info
.si_signo
,
417 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
419 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
422 ERR("Unexptected signal %d\n", info
.si_signo
);
429 * Ensure only a single thread listens on the timer signal.
432 void lib_ring_buffer_setup_timer_thread(void)
437 pthread_mutex_lock(&timer_signal
.lock
);
438 if (timer_signal
.setup_done
)
441 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
444 PERROR("pthread_create");
446 ret
= pthread_detach(thread
);
449 PERROR("pthread_detach");
451 timer_signal
.setup_done
= 1;
453 pthread_mutex_unlock(&timer_signal
.lock
);
457 * Wait for signal-handling thread quiescent state.
460 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
462 sigset_t pending_set
;
466 * We need to be the only thread interacting with the thread
467 * that manages signals for teardown synchronization.
469 pthread_mutex_lock(&timer_signal
.lock
);
472 * Ensure we don't have any signal queued for this channel.
475 ret
= sigemptyset(&pending_set
);
477 PERROR("sigemptyset");
479 ret
= sigpending(&pending_set
);
481 PERROR("sigpending");
483 if (!sigismember(&pending_set
, signr
))
489 * From this point, no new signal handler will be fired that
490 * would try to access "chan". However, we still need to wait
491 * for any currently executing handler to complete.
494 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
498 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
501 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
503 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
507 pthread_mutex_unlock(&timer_signal
.lock
);
511 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
514 struct itimerspec its
;
517 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
520 chan
->switch_timer_enabled
= 1;
522 lib_ring_buffer_setup_timer_thread();
524 sev
.sigev_notify
= SIGEV_SIGNAL
;
525 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
526 sev
.sigev_value
.sival_ptr
= chan
;
527 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
529 PERROR("timer_create");
532 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
533 its
.it_value
.tv_nsec
= chan
->switch_timer_interval
% 1000000;
534 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
535 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
537 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
539 PERROR("timer_settime");
544 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
548 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
551 ret
= timer_delete(chan
->switch_timer
);
553 PERROR("timer_delete");
556 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
558 chan
->switch_timer
= 0;
559 chan
->switch_timer_enabled
= 0;
563 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
565 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
567 struct itimerspec its
;
570 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
571 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
574 chan
->read_timer_enabled
= 1;
576 lib_ring_buffer_setup_timer_thread();
578 sev
.sigev_notify
= SIGEV_SIGNAL
;
579 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
580 sev
.sigev_value
.sival_ptr
= chan
;
581 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
583 PERROR("timer_create");
586 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
587 its
.it_value
.tv_nsec
= chan
->read_timer_interval
% 1000000;
588 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
589 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
591 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
593 PERROR("timer_settime");
598 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
600 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
603 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
604 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
607 ret
= timer_delete(chan
->read_timer
);
609 PERROR("timer_delete");
613 * do one more check to catch data that has been written in the last
616 lib_ring_buffer_channel_do_read(chan
);
618 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
620 chan
->read_timer
= 0;
621 chan
->read_timer_enabled
= 0;
624 static void channel_unregister_notifiers(struct channel
*chan
,
625 struct lttng_ust_shm_handle
*handle
)
627 lib_ring_buffer_channel_switch_timer_stop(chan
);
628 lib_ring_buffer_channel_read_timer_stop(chan
);
631 static void channel_print_errors(struct channel
*chan
,
632 struct lttng_ust_shm_handle
*handle
)
634 const struct lttng_ust_lib_ring_buffer_config
*config
=
635 &chan
->backend
.config
;
638 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
639 for_each_possible_cpu(cpu
) {
640 struct lttng_ust_lib_ring_buffer
*buf
=
641 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
642 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
645 struct lttng_ust_lib_ring_buffer
*buf
=
646 shmp(handle
, chan
->backend
.buf
[0].shmp
);
648 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
652 static void channel_free(struct channel
*chan
,
653 struct lttng_ust_shm_handle
*handle
)
655 channel_backend_free(&chan
->backend
, handle
);
656 /* chan is freed by shm teardown */
657 shm_object_table_destroy(handle
->table
);
662 * channel_create - Create channel.
663 * @config: ring buffer instance configuration
664 * @name: name of the channel
665 * @priv_data: ring buffer client private data area pointer (output)
666 * @priv_data_size: length, in bytes, of the private data area.
667 * @priv_data_init: initialization data for private data.
668 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
669 * address mapping. It is used only by RING_BUFFER_STATIC
670 * configuration. It can be set to NULL for other backends.
671 * @subbuf_size: subbuffer size
672 * @num_subbuf: number of subbuffers
673 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
674 * padding to let readers get those sub-buffers.
675 * Used for live streaming.
676 * @read_timer_interval: Time interval (in us) to wake up pending readers.
679 * Returns NULL on failure.
681 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
684 size_t priv_data_align
,
685 size_t priv_data_size
,
686 void *priv_data_init
,
687 void *buf_addr
, size_t subbuf_size
,
688 size_t num_subbuf
, unsigned int switch_timer_interval
,
689 unsigned int read_timer_interval
)
692 size_t shmsize
, chansize
;
693 struct channel
*chan
;
694 struct lttng_ust_shm_handle
*handle
;
695 struct shm_object
*shmobj
;
696 unsigned int nr_streams
;
698 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
699 nr_streams
= num_possible_cpus();
703 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
704 read_timer_interval
))
707 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
711 /* Allocate table for channel + per-cpu buffers */
712 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
714 goto error_table_alloc
;
716 /* Calculate the shm allocation layout */
717 shmsize
= sizeof(struct channel
);
718 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
719 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
722 shmsize
+= offset_align(shmsize
, priv_data_align
);
723 shmsize
+= priv_data_size
;
725 /* Allocate normal memory for channel (not shared) */
726 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
);
729 /* struct channel is at object 0, offset 0 (hardcoded) */
730 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
731 assert(handle
->chan
._ref
.index
== 0);
732 assert(handle
->chan
._ref
.offset
== 0);
733 chan
= shmp(handle
, handle
->chan
);
736 chan
->nr_streams
= nr_streams
;
738 /* space for private data */
739 if (priv_data_size
) {
740 DECLARE_SHMP(void, priv_data_alloc
);
742 align_shm(shmobj
, priv_data_align
);
743 chan
->priv_data_offset
= shmobj
->allocated_len
;
744 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
745 if (!shmp(handle
, priv_data_alloc
))
747 *priv_data
= channel_get_private(chan
);
748 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
750 chan
->priv_data_offset
= -1;
755 ret
= channel_backend_init(&chan
->backend
, name
, config
,
756 subbuf_size
, num_subbuf
, handle
);
758 goto error_backend_init
;
760 chan
->handle
= handle
;
761 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
763 chan
->switch_timer_interval
= switch_timer_interval
;
764 chan
->read_timer_interval
= read_timer_interval
;
765 lib_ring_buffer_channel_switch_timer_start(chan
);
766 lib_ring_buffer_channel_read_timer_start(chan
);
772 shm_object_table_destroy(handle
->table
);
778 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
779 uint64_t memory_map_size
,
782 struct lttng_ust_shm_handle
*handle
;
783 struct shm_object
*object
;
785 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
789 /* Allocate table for channel + per-cpu buffers */
790 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
792 goto error_table_alloc
;
793 /* Add channel object */
794 object
= shm_object_table_append_mem(handle
->table
, data
,
795 memory_map_size
, wakeup_fd
);
797 goto error_table_object
;
798 /* struct channel is at object 0, offset 0 (hardcoded) */
799 handle
->chan
._ref
.index
= 0;
800 handle
->chan
._ref
.offset
= 0;
804 shm_object_table_destroy(handle
->table
);
810 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
811 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
812 uint64_t memory_map_size
)
814 struct shm_object
*object
;
816 /* Add stream object */
817 object
= shm_object_table_append_shm(handle
->table
,
818 shm_fd
, wakeup_fd
, stream_nr
,
825 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
827 assert(handle
->table
);
828 return handle
->table
->allocated_len
- 1;
832 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
834 channel_free(chan
, handle
);
838 * channel_destroy - Finalize, wait for q.s. and destroy channel.
839 * @chan: channel to destroy
842 * Call "destroy" callback, finalize channels, decrement the channel
843 * reference count. Note that when readers have completed data
844 * consumption of finalized channels, get_subbuf() will return -ENODATA.
845 * They should release their handle at that point.
847 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
852 * Note: the consumer takes care of finalizing and
853 * switching the buffers.
855 channel_unregister_notifiers(chan
, handle
);
857 * The consumer prints errors.
859 channel_print_errors(chan
, handle
);
863 * sessiond/consumer are keeping a reference on the shm file
864 * descriptor directly. No need to refcount.
866 channel_release(chan
, handle
);
870 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
871 const struct lttng_ust_lib_ring_buffer_config
*config
,
872 struct channel
*chan
, int cpu
,
873 struct lttng_ust_shm_handle
*handle
,
874 int *shm_fd
, int *wait_fd
,
876 uint64_t *memory_map_size
)
880 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
883 if (cpu
>= num_possible_cpus())
886 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
887 *shm_fd
= shm_get_shm_fd(handle
, ref
);
888 *wait_fd
= shm_get_wait_fd(handle
, ref
);
889 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
890 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
892 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
895 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
896 struct channel
*chan
,
897 struct lttng_ust_shm_handle
*handle
)
901 ref
= &handle
->chan
._ref
;
902 return shm_close_wait_fd(handle
, ref
);
905 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
906 struct channel
*chan
,
907 struct lttng_ust_shm_handle
*handle
)
911 ref
= &handle
->chan
._ref
;
912 return shm_close_wakeup_fd(handle
, ref
);
915 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
916 struct channel
*chan
,
917 struct lttng_ust_shm_handle
*handle
,
922 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
925 if (cpu
>= num_possible_cpus())
928 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
929 return shm_close_wait_fd(handle
, ref
);
932 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
933 struct channel
*chan
,
934 struct lttng_ust_shm_handle
*handle
,
940 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
943 if (cpu
>= num_possible_cpus())
946 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
947 pthread_mutex_lock(&wakeup_fd_mutex
);
948 ret
= shm_close_wakeup_fd(handle
, ref
);
949 pthread_mutex_unlock(&wakeup_fd_mutex
);
953 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
954 struct lttng_ust_shm_handle
*handle
)
956 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
962 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
963 struct lttng_ust_shm_handle
*handle
)
965 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
967 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
969 uatomic_dec(&buf
->active_readers
);
973 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
975 * @consumed: consumed count indicating the position where to read
976 * @produced: produced count, indicates position when to stop reading
978 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
979 * data to read at consumed position, or 0 if the get operation succeeds.
982 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
983 unsigned long *consumed
, unsigned long *produced
,
984 struct lttng_ust_shm_handle
*handle
)
986 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
987 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
988 unsigned long consumed_cur
, write_offset
;
991 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
993 * Read finalized before counters.
996 consumed_cur
= uatomic_read(&buf
->consumed
);
998 * No need to issue a memory barrier between consumed count read and
999 * write offset read, because consumed count can only change
1000 * concurrently in overwrite mode, and we keep a sequence counter
1001 * identifier derived from the write offset to check we are getting
1002 * the same sub-buffer we are expecting (the sub-buffers are atomically
1003 * "tagged" upon writes, tags are checked upon read).
1005 write_offset
= v_read(config
, &buf
->offset
);
1008 * Check that we are not about to read the same subbuffer in
1009 * which the writer head is.
1011 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1015 *consumed
= consumed_cur
;
1016 *produced
= subbuf_trunc(write_offset
, chan
);
1022 * The memory barriers __wait_event()/wake_up_interruptible() take care
1023 * of "raw_spin_is_locked" memory ordering.
1032 * lib_ring_buffer_move_consumer - move consumed counter forward
1034 * @consumed_new: new consumed count value
1036 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1037 unsigned long consumed_new
,
1038 struct lttng_ust_shm_handle
*handle
)
1040 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1041 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1042 unsigned long consumed
;
1044 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1047 * Only push the consumed value forward.
1048 * If the consumed cmpxchg fails, this is because we have been pushed by
1049 * the writer in flight recorder mode.
1051 consumed
= uatomic_read(&buf
->consumed
);
1052 while ((long) consumed
- (long) consumed_new
< 0)
1053 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1058 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1060 * @consumed: consumed count indicating the position where to read
1062 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1063 * data to read at consumed position, or 0 if the get operation succeeds.
1065 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1066 unsigned long consumed
,
1067 struct lttng_ust_shm_handle
*handle
)
1069 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1070 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1071 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1072 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1075 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1077 * Read finalized before counters.
1080 consumed_cur
= uatomic_read(&buf
->consumed
);
1081 consumed_idx
= subbuf_index(consumed
, chan
);
1082 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
1084 * Make sure we read the commit count before reading the buffer
1085 * data and the write offset. Correct consumed offset ordering
1086 * wrt commit count is insured by the use of cmpxchg to update
1087 * the consumed offset.
1090 * Local rmb to match the remote wmb to read the commit count
1091 * before the buffer data and the write offset.
1095 write_offset
= v_read(config
, &buf
->offset
);
1098 * Check that the buffer we are getting is after or at consumed_cur
1101 if ((long) subbuf_trunc(consumed
, chan
)
1102 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1106 * Check that the subbuffer we are trying to consume has been
1107 * already fully committed. There are a few causes that can make
1108 * this unavailability situation occur:
1110 * Temporary (short-term) situation:
1111 * - Application is running on a different CPU, between reserve
1112 * and commit ring buffer operations,
1113 * - Application is preempted between reserve and commit ring
1114 * buffer operations,
1116 * Long-term situation:
1117 * - Application is stopped (SIGSTOP) between reserve and commit
1118 * ring buffer operations. Could eventually be resumed by
1120 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1121 * reserve and commit ring buffer operation.
1123 * From a consumer perspective, handling short-term
1124 * unavailability situations is performed by retrying a few
1125 * times after a delay. Handling long-term unavailability
1126 * situations is handled by failing to get the sub-buffer.
1128 * In all of those situations, if the application is taking a
1129 * long time to perform its commit after ring buffer space
1130 * reservation, we can end up in a situation where the producer
1131 * will fill the ring buffer and try to write into the same
1132 * sub-buffer again (which has a missing commit). This is
1133 * handled by the producer in the sub-buffer switch handling
1134 * code of the reserve routine by detecting unbalanced
1135 * reserve/commit counters and discarding all further events
1136 * until the situation is resolved in those situations. Two
1137 * scenarios can occur:
1139 * 1) The application causing the reserve/commit counters to be
1140 * unbalanced has been terminated. In this situation, all
1141 * further events will be discarded in the buffers, and no
1142 * further buffer data will be readable by the consumer
1143 * daemon. Tearing down the UST tracing session and starting
1144 * anew is a work-around for those situations. Note that this
1145 * only affects per-UID tracing. In per-PID tracing, the
1146 * application vanishes with the termination, and therefore
1147 * no more data needs to be written to the buffers.
1148 * 2) The application causing the unbalance has been delayed for
1149 * a long time, but will eventually try to increment the
1150 * commit counter after eventually writing to the sub-buffer.
1151 * This situation can cause events to be discarded until the
1152 * application resumes its operations.
1154 if (((commit_count
- chan
->backend
.subbuf_size
)
1155 & chan
->commit_count_mask
)
1156 - (buf_trunc(consumed
, chan
)
1157 >> chan
->backend
.num_subbuf_order
)
1159 if (nr_retry
-- > 0) {
1160 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1161 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1169 * Check that we are not about to read the same subbuffer in
1170 * which the writer head is.
1172 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1177 * Failure to get the subbuffer causes a busy-loop retry without going
1178 * to a wait queue. These are caused by short-lived race windows where
1179 * the writer is getting access to a subbuffer we were trying to get
1180 * access to. Also checks that the "consumed" buffer count we are
1181 * looking for matches the one contained in the subbuffer id.
1183 * The short-lived race window described here can be affected by
1184 * application signals and preemption, thus requiring to bound
1185 * the loop to a maximum number of retry.
1187 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1188 consumed_idx
, buf_trunc_val(consumed
, chan
),
1191 if (nr_retry
-- > 0) {
1192 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1193 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1199 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1201 buf
->get_subbuf_consumed
= consumed
;
1202 buf
->get_subbuf
= 1;
1208 * The memory barriers __wait_event()/wake_up_interruptible() take care
1209 * of "raw_spin_is_locked" memory ordering.
1218 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1221 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1222 struct lttng_ust_shm_handle
*handle
)
1224 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1225 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1226 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1227 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1229 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1231 if (!buf
->get_subbuf
) {
1233 * Reader puts a subbuffer it did not get.
1235 CHAN_WARN_ON(chan
, 1);
1238 consumed
= buf
->get_subbuf_consumed
;
1239 buf
->get_subbuf
= 0;
1242 * Clear the records_unread counter. (overruns counter)
1243 * Can still be non-zero if a file reader simply grabbed the data
1244 * without using iterators.
1245 * Can be below zero if an iterator is used on a snapshot more than
1248 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1249 v_add(config
, v_read(config
,
1250 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
1251 &bufb
->records_read
);
1252 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
1253 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1254 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1255 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1258 * Exchange the reader subbuffer with the one we put in its place in the
1259 * writer subbuffer table. Expect the original consumed count. If
1260 * update_read_sb_index fails, this is because the writer updated the
1261 * subbuffer concurrently. We should therefore keep the subbuffer we
1262 * currently have: it has become invalid to try reading this sub-buffer
1263 * consumed count value anyway.
1265 consumed_idx
= subbuf_index(consumed
, chan
);
1266 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1267 consumed_idx
, buf_trunc_val(consumed
, chan
),
1270 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1271 * if the writer concurrently updated it.
1276 * cons_offset is an iterator on all subbuffer offsets between the reader
1277 * position and the writer position. (inclusive)
1280 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1281 struct channel
*chan
,
1282 unsigned long cons_offset
,
1284 struct lttng_ust_shm_handle
*handle
)
1286 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1287 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1289 cons_idx
= subbuf_index(cons_offset
, chan
);
1290 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
1291 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
1293 if (subbuf_offset(commit_count
, chan
) != 0)
1294 DBG("ring buffer %s, cpu %d: "
1295 "commit count in subbuffer %lu,\n"
1296 "expecting multiples of %lu bytes\n"
1297 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1298 chan
->backend
.name
, cpu
, cons_idx
,
1299 chan
->backend
.subbuf_size
,
1300 commit_count
, commit_count_sb
);
1302 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1303 chan
->backend
.name
, cpu
, commit_count
);
1307 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1308 struct channel
*chan
,
1309 void *priv
, int cpu
,
1310 struct lttng_ust_shm_handle
*handle
)
1312 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1313 unsigned long write_offset
, cons_offset
;
1316 * No need to order commit_count, write_offset and cons_offset reads
1317 * because we execute at teardown when no more writer nor reader
1318 * references are left.
1320 write_offset
= v_read(config
, &buf
->offset
);
1321 cons_offset
= uatomic_read(&buf
->consumed
);
1322 if (write_offset
!= cons_offset
)
1323 DBG("ring buffer %s, cpu %d: "
1324 "non-consumed data\n"
1325 " [ %lu bytes written, %lu bytes read ]\n",
1326 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1328 for (cons_offset
= uatomic_read(&buf
->consumed
);
1329 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1332 cons_offset
= subbuf_align(cons_offset
, chan
))
1333 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1338 void lib_ring_buffer_print_errors(struct channel
*chan
,
1339 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1340 struct lttng_ust_shm_handle
*handle
)
1342 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1343 void *priv
= channel_get_private(chan
);
1345 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1346 DBG("ring buffer %s: %lu records written, "
1347 "%lu records overrun\n",
1349 v_read(config
, &buf
->records_count
),
1350 v_read(config
, &buf
->records_overrun
));
1352 DBG("ring buffer %s, cpu %d: %lu records written, "
1353 "%lu records overrun\n",
1354 chan
->backend
.name
, cpu
,
1355 v_read(config
, &buf
->records_count
),
1356 v_read(config
, &buf
->records_overrun
));
1358 if (v_read(config
, &buf
->records_lost_full
)
1359 || v_read(config
, &buf
->records_lost_wrap
)
1360 || v_read(config
, &buf
->records_lost_big
))
1361 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1362 " [ %lu buffer full, %lu nest buffer wrap-around, "
1363 "%lu event too big ]\n",
1364 chan
->backend
.name
, cpu
,
1365 v_read(config
, &buf
->records_lost_full
),
1366 v_read(config
, &buf
->records_lost_wrap
),
1367 v_read(config
, &buf
->records_lost_big
));
1369 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1373 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1375 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1378 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1379 struct channel
*chan
,
1380 struct switch_offsets
*offsets
,
1382 struct lttng_ust_shm_handle
*handle
)
1384 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1385 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1386 unsigned long commit_count
;
1388 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1391 * Order all writes to buffer before the commit count update that will
1392 * determine that the subbuffer is full.
1395 v_add(config
, config
->cb
.subbuffer_header_size(),
1396 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1397 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1398 /* Check if the written buffer has to be delivered */
1399 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1400 commit_count
, oldidx
, handle
, tsc
);
1401 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1402 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1403 commit_count
, handle
);
1407 * lib_ring_buffer_switch_old_end: switch old subbuffer
1409 * Note : offset_old should never be 0 here. It is ok, because we never perform
1410 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1411 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1415 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1416 struct channel
*chan
,
1417 struct switch_offsets
*offsets
,
1419 struct lttng_ust_shm_handle
*handle
)
1421 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1422 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1423 unsigned long commit_count
, padding_size
, data_size
;
1425 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1426 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1427 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1431 * Order all writes to buffer before the commit count update that will
1432 * determine that the subbuffer is full.
1435 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1436 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1437 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1438 commit_count
, oldidx
, handle
, tsc
);
1439 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1440 offsets
->old
+ padding_size
, commit_count
, handle
);
1444 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1446 * This code can be executed unordered : writers may already have written to the
1447 * sub-buffer before this code gets executed, caution. The commit makes sure
1448 * that this code is executed before the deliver of this sub-buffer.
1451 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1452 struct channel
*chan
,
1453 struct switch_offsets
*offsets
,
1455 struct lttng_ust_shm_handle
*handle
)
1457 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1458 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1459 unsigned long commit_count
;
1461 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1464 * Order all writes to buffer before the commit count update that will
1465 * determine that the subbuffer is full.
1468 v_add(config
, config
->cb
.subbuffer_header_size(),
1469 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1470 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1471 /* Check if the written buffer has to be delivered */
1472 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1473 commit_count
, beginidx
, handle
, tsc
);
1474 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1475 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1476 commit_count
, handle
);
1480 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1482 * Calls subbuffer_set_data_size() to set the data size of the current
1483 * sub-buffer. We do not need to perform check_deliver nor commit here,
1484 * since this task will be done by the "commit" of the event for which
1485 * we are currently doing the space reservation.
1488 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1489 struct channel
*chan
,
1490 struct switch_offsets
*offsets
,
1492 struct lttng_ust_shm_handle
*handle
)
1494 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1495 unsigned long endidx
, data_size
;
1497 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1498 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1499 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1506 * !0 if execution must be aborted.
1509 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1510 struct lttng_ust_lib_ring_buffer
*buf
,
1511 struct channel
*chan
,
1512 struct switch_offsets
*offsets
,
1514 struct lttng_ust_shm_handle
*handle
)
1516 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1517 unsigned long off
, reserve_commit_diff
;
1519 offsets
->begin
= v_read(config
, &buf
->offset
);
1520 offsets
->old
= offsets
->begin
;
1521 offsets
->switch_old_start
= 0;
1522 off
= subbuf_offset(offsets
->begin
, chan
);
1524 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1527 * Ensure we flush the header of an empty subbuffer when doing the
1528 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1529 * total data gathering duration even if there were no records saved
1530 * after the last buffer switch.
1531 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1532 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1533 * subbuffer header as appropriate.
1534 * The next record that reserves space will be responsible for
1535 * populating the following subbuffer header. We choose not to populate
1536 * the next subbuffer header here because we want to be able to use
1537 * SWITCH_ACTIVE for periodical buffer flush, which must
1538 * guarantee that all the buffer content (records and header
1539 * timestamps) are visible to the reader. This is required for
1540 * quiescence guarantees for the fusion merge.
1542 if (mode
!= SWITCH_FLUSH
&& !off
)
1543 return -1; /* we do not have to switch : buffer is empty */
1545 if (caa_unlikely(off
== 0)) {
1546 unsigned long sb_index
, commit_count
;
1549 * We are performing a SWITCH_FLUSH. At this stage, there are no
1550 * concurrent writes into the buffer.
1552 * The client does not save any header information. Don't
1553 * switch empty subbuffer on finalize, because it is invalid to
1554 * deliver a completely empty subbuffer.
1556 if (!config
->cb
.subbuffer_header_size())
1559 /* Test new buffer integrity */
1560 sb_index
= subbuf_index(offsets
->begin
, chan
);
1561 commit_count
= v_read(config
,
1562 &shmp_index(handle
, buf
->commit_cold
,
1564 reserve_commit_diff
=
1565 (buf_trunc(offsets
->begin
, chan
)
1566 >> chan
->backend
.num_subbuf_order
)
1567 - (commit_count
& chan
->commit_count_mask
);
1568 if (caa_likely(reserve_commit_diff
== 0)) {
1569 /* Next subbuffer not being written to. */
1570 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1571 subbuf_trunc(offsets
->begin
, chan
)
1572 - subbuf_trunc((unsigned long)
1573 uatomic_read(&buf
->consumed
), chan
)
1574 >= chan
->backend
.buf_size
)) {
1576 * We do not overwrite non consumed buffers
1577 * and we are full : don't switch.
1582 * Next subbuffer not being written to, and we
1583 * are either in overwrite mode or the buffer is
1584 * not full. It's safe to write in this new
1590 * Next subbuffer reserve offset does not match the
1591 * commit offset. Don't perform switch in
1592 * producer-consumer and overwrite mode. Caused by
1593 * either a writer OOPS or too many nested writes over a
1594 * reserve/commit pair.
1600 * Need to write the subbuffer start header on finalize.
1602 offsets
->switch_old_start
= 1;
1604 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1605 /* Note: old points to the next subbuf at offset 0 */
1606 offsets
->end
= offsets
->begin
;
1611 * Force a sub-buffer switch. This operation is completely reentrant : can be
1612 * called while tracing is active with absolutely no lock held.
1614 * Note, however, that as a v_cmpxchg is used for some atomic
1615 * operations, this function must be called from the CPU which owns the buffer
1616 * for a ACTIVE flush.
1618 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1619 struct lttng_ust_shm_handle
*handle
)
1621 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1622 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1623 struct switch_offsets offsets
;
1624 unsigned long oldidx
;
1630 * Perform retryable operations.
1633 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1635 return; /* Switch not needed */
1636 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1640 * Atomically update last_tsc. This update races against concurrent
1641 * atomic updates, but the race will always cause supplementary full TSC
1642 * records, never the opposite (missing a full TSC record when it would
1645 save_last_tsc(config
, buf
, tsc
);
1648 * Push the reader if necessary
1650 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1652 oldidx
= subbuf_index(offsets
.old
, chan
);
1653 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1656 * May need to populate header start on SWITCH_FLUSH.
1658 if (offsets
.switch_old_start
) {
1659 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1660 offsets
.old
+= config
->cb
.subbuffer_header_size();
1664 * Switch old subbuffer.
1666 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1672 * -ENOSPC if event size is too large for packet.
1673 * -ENOBUFS if there is currently not enough space in buffer for the event.
1674 * -EIO if data cannot be written into the buffer for any other reason.
1677 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1678 struct channel
*chan
,
1679 struct switch_offsets
*offsets
,
1680 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1682 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1683 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1684 unsigned long reserve_commit_diff
, offset_cmp
;
1687 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1688 offsets
->old
= offsets
->begin
;
1689 offsets
->switch_new_start
= 0;
1690 offsets
->switch_new_end
= 0;
1691 offsets
->switch_old_end
= 0;
1692 offsets
->pre_header_padding
= 0;
1694 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1695 if ((int64_t) ctx
->tsc
== -EIO
)
1698 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1699 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1701 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1702 offsets
->switch_new_start
= 1; /* For offsets->begin */
1704 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1706 &offsets
->pre_header_padding
,
1709 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1712 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1713 offsets
->size
> chan
->backend
.subbuf_size
)) {
1714 offsets
->switch_old_end
= 1; /* For offsets->old */
1715 offsets
->switch_new_start
= 1; /* For offsets->begin */
1718 if (caa_unlikely(offsets
->switch_new_start
)) {
1719 unsigned long sb_index
, commit_count
;
1722 * We are typically not filling the previous buffer completely.
1724 if (caa_likely(offsets
->switch_old_end
))
1725 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1726 offsets
->begin
= offsets
->begin
1727 + config
->cb
.subbuffer_header_size();
1728 /* Test new buffer integrity */
1729 sb_index
= subbuf_index(offsets
->begin
, chan
);
1731 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1732 * lib_ring_buffer_check_deliver() has the matching
1733 * memory barriers required around commit_cold cc_sb
1734 * updates to ensure reserve and commit counter updates
1735 * are not seen reordered when updated by another CPU.
1738 commit_count
= v_read(config
,
1739 &shmp_index(handle
, buf
->commit_cold
,
1741 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1743 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1745 * The reserve counter have been concurrently updated
1746 * while we read the commit counter. This means the
1747 * commit counter we read might not match buf->offset
1748 * due to concurrent update. We therefore need to retry.
1752 reserve_commit_diff
=
1753 (buf_trunc(offsets
->begin
, chan
)
1754 >> chan
->backend
.num_subbuf_order
)
1755 - (commit_count
& chan
->commit_count_mask
);
1756 if (caa_likely(reserve_commit_diff
== 0)) {
1757 /* Next subbuffer not being written to. */
1758 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1759 subbuf_trunc(offsets
->begin
, chan
)
1760 - subbuf_trunc((unsigned long)
1761 uatomic_read(&buf
->consumed
), chan
)
1762 >= chan
->backend
.buf_size
)) {
1763 unsigned long nr_lost
;
1766 * We do not overwrite non consumed buffers
1767 * and we are full : record is lost.
1769 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1770 v_inc(config
, &buf
->records_lost_full
);
1771 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1772 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1773 nr_lost
+ 1, chan
->backend
.name
,
1779 * Next subbuffer not being written to, and we
1780 * are either in overwrite mode or the buffer is
1781 * not full. It's safe to write in this new
1786 unsigned long nr_lost
;
1789 * Next subbuffer reserve offset does not match the
1790 * commit offset, and this did not involve update to the
1791 * reserve counter. Drop record in producer-consumer and
1792 * overwrite mode. Caused by either a writer OOPS or too
1793 * many nested writes over a reserve/commit pair.
1795 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1796 v_inc(config
, &buf
->records_lost_wrap
);
1797 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1798 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1799 nr_lost
+ 1, chan
->backend
.name
,
1805 config
->cb
.record_header_size(config
, chan
,
1807 &offsets
->pre_header_padding
,
1810 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1813 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1814 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1815 unsigned long nr_lost
;
1818 * Record too big for subbuffers, report error, don't
1819 * complete the sub-buffer switch.
1821 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1822 v_inc(config
, &buf
->records_lost_big
);
1823 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1824 DBG("%lu or more records lost in (%s:%d) record size "
1825 " of %zu bytes is too large for buffer\n",
1826 nr_lost
+ 1, chan
->backend
.name
,
1827 buf
->backend
.cpu
, offsets
->size
);
1832 * We just made a successful buffer switch and the
1833 * record fits in the new subbuffer. Let's write.
1838 * Record fits in the current buffer and we are not on a switch
1839 * boundary. It's safe to write.
1842 offsets
->end
= offsets
->begin
+ offsets
->size
;
1844 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1846 * The offset_end will fall at the very beginning of the next
1849 offsets
->switch_new_end
= 1; /* For offsets->begin */
1855 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1856 * @ctx: ring buffer context.
1858 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1859 * -EIO for other errors, else returns 0.
1860 * It will take care of sub-buffer switching.
1862 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1864 struct channel
*chan
= ctx
->chan
;
1865 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1866 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1867 struct lttng_ust_lib_ring_buffer
*buf
;
1868 struct switch_offsets offsets
;
1871 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1872 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1874 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1880 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1882 if (caa_unlikely(ret
))
1884 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1889 * Atomically update last_tsc. This update races against concurrent
1890 * atomic updates, but the race will always cause supplementary full TSC
1891 * records, never the opposite (missing a full TSC record when it would
1894 save_last_tsc(config
, buf
, ctx
->tsc
);
1897 * Push the reader if necessary
1899 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1902 * Clear noref flag for this subbuffer.
1904 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1905 subbuf_index(offsets
.end
- 1, chan
),
1909 * Switch old subbuffer if needed.
1911 if (caa_unlikely(offsets
.switch_old_end
)) {
1912 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1913 subbuf_index(offsets
.old
- 1, chan
),
1915 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1919 * Populate new subbuffer.
1921 if (caa_unlikely(offsets
.switch_new_start
))
1922 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1924 if (caa_unlikely(offsets
.switch_new_end
))
1925 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1927 ctx
->slot_size
= offsets
.size
;
1928 ctx
->pre_offset
= offsets
.begin
;
1929 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1934 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1936 void lttng_fixup_ringbuffer_tls(void)
1938 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
1941 void lib_ringbuffer_signal_init(void)
1947 * Block signal for entire process, so only our thread processes
1951 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
1954 PERROR("pthread_sigmask");