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>
69 #include <lttng/ringbuffer-config.h>
75 #include "../liblttng-ust/compat.h" /* For ENODATA */
78 #define max(a, b) ((a) > (b) ? (a) : (b))
81 /* Print DBG() messages about events lost only every 1048576 hits */
82 #define DBG_PRINT_NR_LOST (1UL << 20)
84 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
85 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
86 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
87 #define CLOCKID CLOCK_MONOTONIC
88 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
89 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
92 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
93 * close(2) to close the fd returned by shm_open.
94 * shm_unlink releases the shared memory object name.
95 * ftruncate(2) sets the size of the memory object.
96 * mmap/munmap maps the shared memory obj to a virtual address in the
97 * calling proceess (should be done both in libust and consumer).
98 * See shm_overview(7) for details.
99 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
102 * Since we don't need to access the object using its name, we can
103 * immediately shm_unlink(3) it, and only keep the handle with its file
108 * Internal structure representing offsets to use at a sub-buffer switch.
110 struct switch_offsets
{
111 unsigned long begin
, end
, old
;
112 size_t pre_header_padding
, size
;
113 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
117 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
120 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
123 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
126 void lib_ring_buffer_print_errors(struct channel
*chan
,
127 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
128 struct lttng_ust_shm_handle
*handle
);
131 * Handle timer teardown race wrt memory free of private data by
132 * ring buffer signals are handled by a single thread, which permits
133 * a synchronization point between handling of each signal.
134 * Protected by the lock within the structure.
136 struct timer_signal_data
{
137 pthread_t tid
; /* thread id managing signals */
140 pthread_mutex_t lock
;
143 static struct timer_signal_data timer_signal
= {
147 .lock
= PTHREAD_MUTEX_INITIALIZER
,
151 * lib_ring_buffer_reset - Reset ring buffer to initial values.
154 * Effectively empty the ring buffer. Should be called when the buffer is not
155 * used for writing. The ring buffer can be opened for reading, but the reader
156 * should not be using the iterator concurrently with reset. The previous
157 * current iterator record is reset.
159 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
160 struct lttng_ust_shm_handle
*handle
)
162 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
163 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
167 * Reset iterator first. It will put the subbuffer if it currently holds
170 v_set(config
, &buf
->offset
, 0);
171 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
172 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
173 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
174 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
176 uatomic_set(&buf
->consumed
, 0);
177 uatomic_set(&buf
->record_disabled
, 0);
178 v_set(config
, &buf
->last_tsc
, 0);
179 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
180 /* Don't reset number of active readers */
181 v_set(config
, &buf
->records_lost_full
, 0);
182 v_set(config
, &buf
->records_lost_wrap
, 0);
183 v_set(config
, &buf
->records_lost_big
, 0);
184 v_set(config
, &buf
->records_count
, 0);
185 v_set(config
, &buf
->records_overrun
, 0);
190 * channel_reset - Reset channel to initial values.
193 * Effectively empty the channel. Should be called when the channel is not used
194 * for writing. The channel can be opened for reading, but the reader should not
195 * be using the iterator concurrently with reset. The previous current iterator
198 void channel_reset(struct channel
*chan
)
201 * Reset iterators first. Will put the subbuffer if held for reading.
203 uatomic_set(&chan
->record_disabled
, 0);
204 /* Don't reset commit_count_mask, still valid */
205 channel_backend_reset(&chan
->backend
);
206 /* Don't reset switch/read timer interval */
207 /* Don't reset notifiers and notifier enable bits */
208 /* Don't reset reader reference count */
212 * Must be called under cpu hotplug protection.
214 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
215 struct channel_backend
*chanb
, int cpu
,
216 struct lttng_ust_shm_handle
*handle
,
217 struct shm_object
*shmobj
)
219 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
220 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
221 void *priv
= channel_get_private(chan
);
222 size_t subbuf_header_size
;
226 /* Test for cpu hotplug */
227 if (buf
->backend
.allocated
)
230 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
231 cpu
, handle
, shmobj
);
235 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
236 set_shmp(buf
->commit_hot
,
238 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
239 if (!shmp(handle
, buf
->commit_hot
)) {
244 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
245 set_shmp(buf
->commit_cold
,
247 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
248 if (!shmp(handle
, buf
->commit_cold
)) {
254 * Write the subbuffer header for first subbuffer so we know the total
255 * duration of data gathering.
257 subbuf_header_size
= config
->cb
.subbuffer_header_size();
258 v_set(config
, &buf
->offset
, subbuf_header_size
);
259 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
260 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
261 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
262 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
264 if (config
->cb
.buffer_create
) {
265 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
269 buf
->backend
.allocated
= 1;
274 /* commit_cold will be freed by shm teardown */
276 /* commit_hot will be freed by shm teardown */
282 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
284 const struct lttng_ust_lib_ring_buffer_config
*config
;
285 struct lttng_ust_shm_handle
*handle
;
286 struct channel
*chan
;
289 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
291 chan
= si
->si_value
.sival_ptr
;
292 handle
= chan
->handle
;
293 config
= &chan
->backend
.config
;
295 DBG("Switch timer for channel %p\n", chan
);
298 * Only flush buffers periodically if readers are active.
300 pthread_mutex_lock(&wakeup_fd_mutex
);
301 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
302 for_each_possible_cpu(cpu
) {
303 struct lttng_ust_lib_ring_buffer
*buf
=
304 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
305 if (uatomic_read(&buf
->active_readers
))
306 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
310 struct lttng_ust_lib_ring_buffer
*buf
=
311 shmp(handle
, chan
->backend
.buf
[0].shmp
);
313 if (uatomic_read(&buf
->active_readers
))
314 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
317 pthread_mutex_unlock(&wakeup_fd_mutex
);
322 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
324 const struct lttng_ust_lib_ring_buffer_config
*config
;
325 struct lttng_ust_shm_handle
*handle
;
328 handle
= chan
->handle
;
329 config
= &chan
->backend
.config
;
332 * Only flush buffers periodically if readers are active.
334 pthread_mutex_lock(&wakeup_fd_mutex
);
335 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
336 for_each_possible_cpu(cpu
) {
337 struct lttng_ust_lib_ring_buffer
*buf
=
338 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
340 if (uatomic_read(&buf
->active_readers
)
341 && lib_ring_buffer_poll_deliver(config
, buf
,
343 lib_ring_buffer_wakeup(buf
, handle
);
347 struct lttng_ust_lib_ring_buffer
*buf
=
348 shmp(handle
, chan
->backend
.buf
[0].shmp
);
350 if (uatomic_read(&buf
->active_readers
)
351 && lib_ring_buffer_poll_deliver(config
, buf
,
353 lib_ring_buffer_wakeup(buf
, handle
);
356 pthread_mutex_unlock(&wakeup_fd_mutex
);
360 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
362 struct channel
*chan
;
364 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
365 chan
= si
->si_value
.sival_ptr
;
366 DBG("Read timer for channel %p\n", chan
);
367 lib_ring_buffer_channel_do_read(chan
);
372 void rb_setmask(sigset_t
*mask
)
376 ret
= sigemptyset(mask
);
378 PERROR("sigemptyset");
380 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
384 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
388 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
395 void *sig_thread(void *arg
)
401 /* Only self thread will receive signal mask. */
403 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
406 signr
= sigwaitinfo(&mask
, &info
);
409 PERROR("sigwaitinfo");
412 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
413 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
415 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
416 lib_ring_buffer_channel_read_timer(info
.si_signo
,
418 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
420 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
423 ERR("Unexptected signal %d\n", info
.si_signo
);
430 * Ensure only a single thread listens on the timer signal.
433 void lib_ring_buffer_setup_timer_thread(void)
438 pthread_mutex_lock(&timer_signal
.lock
);
439 if (timer_signal
.setup_done
)
442 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
445 PERROR("pthread_create");
447 ret
= pthread_detach(thread
);
450 PERROR("pthread_detach");
452 timer_signal
.setup_done
= 1;
454 pthread_mutex_unlock(&timer_signal
.lock
);
458 * Wait for signal-handling thread quiescent state.
461 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
463 sigset_t pending_set
;
467 * We need to be the only thread interacting with the thread
468 * that manages signals for teardown synchronization.
470 pthread_mutex_lock(&timer_signal
.lock
);
473 * Ensure we don't have any signal queued for this channel.
476 ret
= sigemptyset(&pending_set
);
478 PERROR("sigemptyset");
480 ret
= sigpending(&pending_set
);
482 PERROR("sigpending");
484 if (!sigismember(&pending_set
, signr
))
490 * From this point, no new signal handler will be fired that
491 * would try to access "chan". However, we still need to wait
492 * for any currently executing handler to complete.
495 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
499 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
502 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
504 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
508 pthread_mutex_unlock(&timer_signal
.lock
);
512 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
515 struct itimerspec its
;
518 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
521 chan
->switch_timer_enabled
= 1;
523 lib_ring_buffer_setup_timer_thread();
525 sev
.sigev_notify
= SIGEV_SIGNAL
;
526 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
527 sev
.sigev_value
.sival_ptr
= chan
;
528 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
530 PERROR("timer_create");
533 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
534 its
.it_value
.tv_nsec
= chan
->switch_timer_interval
% 1000000;
535 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
536 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
538 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
540 PERROR("timer_settime");
545 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
549 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
552 ret
= timer_delete(chan
->switch_timer
);
554 PERROR("timer_delete");
557 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
559 chan
->switch_timer
= 0;
560 chan
->switch_timer_enabled
= 0;
564 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
566 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
568 struct itimerspec its
;
571 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
572 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
575 chan
->read_timer_enabled
= 1;
577 lib_ring_buffer_setup_timer_thread();
579 sev
.sigev_notify
= SIGEV_SIGNAL
;
580 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
581 sev
.sigev_value
.sival_ptr
= chan
;
582 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
584 PERROR("timer_create");
587 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
588 its
.it_value
.tv_nsec
= chan
->read_timer_interval
% 1000000;
589 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
590 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
592 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
594 PERROR("timer_settime");
599 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
601 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
604 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
605 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
608 ret
= timer_delete(chan
->read_timer
);
610 PERROR("timer_delete");
614 * do one more check to catch data that has been written in the last
617 lib_ring_buffer_channel_do_read(chan
);
619 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
621 chan
->read_timer
= 0;
622 chan
->read_timer_enabled
= 0;
625 static void channel_unregister_notifiers(struct channel
*chan
,
626 struct lttng_ust_shm_handle
*handle
)
628 lib_ring_buffer_channel_switch_timer_stop(chan
);
629 lib_ring_buffer_channel_read_timer_stop(chan
);
632 static void channel_print_errors(struct channel
*chan
,
633 struct lttng_ust_shm_handle
*handle
)
635 const struct lttng_ust_lib_ring_buffer_config
*config
=
636 &chan
->backend
.config
;
639 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
640 for_each_possible_cpu(cpu
) {
641 struct lttng_ust_lib_ring_buffer
*buf
=
642 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
643 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
646 struct lttng_ust_lib_ring_buffer
*buf
=
647 shmp(handle
, chan
->backend
.buf
[0].shmp
);
649 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
653 static void channel_free(struct channel
*chan
,
654 struct lttng_ust_shm_handle
*handle
)
656 channel_backend_free(&chan
->backend
, handle
);
657 /* chan is freed by shm teardown */
658 shm_object_table_destroy(handle
->table
);
663 * channel_create - Create channel.
664 * @config: ring buffer instance configuration
665 * @name: name of the channel
666 * @priv_data: ring buffer client private data area pointer (output)
667 * @priv_data_size: length, in bytes, of the private data area.
668 * @priv_data_init: initialization data for private data.
669 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
670 * address mapping. It is used only by RING_BUFFER_STATIC
671 * configuration. It can be set to NULL for other backends.
672 * @subbuf_size: subbuffer size
673 * @num_subbuf: number of subbuffers
674 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
675 * padding to let readers get those sub-buffers.
676 * Used for live streaming.
677 * @read_timer_interval: Time interval (in us) to wake up pending readers.
680 * Returns NULL on failure.
682 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
685 size_t priv_data_align
,
686 size_t priv_data_size
,
687 void *priv_data_init
,
688 void *buf_addr
, size_t subbuf_size
,
689 size_t num_subbuf
, unsigned int switch_timer_interval
,
690 unsigned int read_timer_interval
)
693 size_t shmsize
, chansize
;
694 struct channel
*chan
;
695 struct lttng_ust_shm_handle
*handle
;
696 struct shm_object
*shmobj
;
697 unsigned int nr_streams
;
699 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
700 nr_streams
= num_possible_cpus();
704 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
705 read_timer_interval
))
708 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
712 /* Allocate table for channel + per-cpu buffers */
713 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
715 goto error_table_alloc
;
717 /* Calculate the shm allocation layout */
718 shmsize
= sizeof(struct channel
);
719 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
720 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
723 shmsize
+= offset_align(shmsize
, priv_data_align
);
724 shmsize
+= priv_data_size
;
726 /* Allocate normal memory for channel (not shared) */
727 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
);
730 /* struct channel is at object 0, offset 0 (hardcoded) */
731 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
732 assert(handle
->chan
._ref
.index
== 0);
733 assert(handle
->chan
._ref
.offset
== 0);
734 chan
= shmp(handle
, handle
->chan
);
737 chan
->nr_streams
= nr_streams
;
739 /* space for private data */
740 if (priv_data_size
) {
741 DECLARE_SHMP(void, priv_data_alloc
);
743 align_shm(shmobj
, priv_data_align
);
744 chan
->priv_data_offset
= shmobj
->allocated_len
;
745 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
746 if (!shmp(handle
, priv_data_alloc
))
748 *priv_data
= channel_get_private(chan
);
749 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
751 chan
->priv_data_offset
= -1;
756 ret
= channel_backend_init(&chan
->backend
, name
, config
,
757 subbuf_size
, num_subbuf
, handle
);
759 goto error_backend_init
;
761 chan
->handle
= handle
;
762 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
764 chan
->switch_timer_interval
= switch_timer_interval
;
765 chan
->read_timer_interval
= read_timer_interval
;
766 lib_ring_buffer_channel_switch_timer_start(chan
);
767 lib_ring_buffer_channel_read_timer_start(chan
);
773 shm_object_table_destroy(handle
->table
);
779 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
780 uint64_t memory_map_size
,
783 struct lttng_ust_shm_handle
*handle
;
784 struct shm_object
*object
;
786 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
790 /* Allocate table for channel + per-cpu buffers */
791 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
793 goto error_table_alloc
;
794 /* Add channel object */
795 object
= shm_object_table_append_mem(handle
->table
, data
,
796 memory_map_size
, wakeup_fd
);
798 goto error_table_object
;
799 /* struct channel is at object 0, offset 0 (hardcoded) */
800 handle
->chan
._ref
.index
= 0;
801 handle
->chan
._ref
.offset
= 0;
805 shm_object_table_destroy(handle
->table
);
811 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
812 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
813 uint64_t memory_map_size
)
815 struct shm_object
*object
;
817 /* Add stream object */
818 object
= shm_object_table_append_shm(handle
->table
,
819 shm_fd
, wakeup_fd
, stream_nr
,
826 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
828 assert(handle
->table
);
829 return handle
->table
->allocated_len
- 1;
833 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
835 channel_free(chan
, handle
);
839 * channel_destroy - Finalize, wait for q.s. and destroy channel.
840 * @chan: channel to destroy
843 * Call "destroy" callback, finalize channels, decrement the channel
844 * reference count. Note that when readers have completed data
845 * consumption of finalized channels, get_subbuf() will return -ENODATA.
846 * They should release their handle at that point.
848 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
853 * Note: the consumer takes care of finalizing and
854 * switching the buffers.
856 channel_unregister_notifiers(chan
, handle
);
858 * The consumer prints errors.
860 channel_print_errors(chan
, handle
);
864 * sessiond/consumer are keeping a reference on the shm file
865 * descriptor directly. No need to refcount.
867 channel_release(chan
, handle
);
871 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
872 const struct lttng_ust_lib_ring_buffer_config
*config
,
873 struct channel
*chan
, int cpu
,
874 struct lttng_ust_shm_handle
*handle
,
875 int *shm_fd
, int *wait_fd
,
877 uint64_t *memory_map_size
)
881 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
884 if (cpu
>= num_possible_cpus())
887 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
888 *shm_fd
= shm_get_shm_fd(handle
, ref
);
889 *wait_fd
= shm_get_wait_fd(handle
, ref
);
890 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
891 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
893 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
896 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
897 struct channel
*chan
,
898 struct lttng_ust_shm_handle
*handle
)
902 ref
= &handle
->chan
._ref
;
903 return shm_close_wait_fd(handle
, ref
);
906 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
907 struct channel
*chan
,
908 struct lttng_ust_shm_handle
*handle
)
912 ref
= &handle
->chan
._ref
;
913 return shm_close_wakeup_fd(handle
, ref
);
916 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
917 struct channel
*chan
,
918 struct lttng_ust_shm_handle
*handle
,
923 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
926 if (cpu
>= num_possible_cpus())
929 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
930 return shm_close_wait_fd(handle
, ref
);
933 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
934 struct channel
*chan
,
935 struct lttng_ust_shm_handle
*handle
,
941 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
944 if (cpu
>= num_possible_cpus())
947 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
948 pthread_mutex_lock(&wakeup_fd_mutex
);
949 ret
= shm_close_wakeup_fd(handle
, ref
);
950 pthread_mutex_unlock(&wakeup_fd_mutex
);
954 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
955 struct lttng_ust_shm_handle
*handle
)
957 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
963 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
964 struct lttng_ust_shm_handle
*handle
)
966 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
968 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
970 uatomic_dec(&buf
->active_readers
);
974 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
976 * @consumed: consumed count indicating the position where to read
977 * @produced: produced count, indicates position when to stop reading
979 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
980 * data to read at consumed position, or 0 if the get operation succeeds.
983 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
984 unsigned long *consumed
, unsigned long *produced
,
985 struct lttng_ust_shm_handle
*handle
)
987 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
988 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
989 unsigned long consumed_cur
, write_offset
;
992 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
994 * Read finalized before counters.
997 consumed_cur
= uatomic_read(&buf
->consumed
);
999 * No need to issue a memory barrier between consumed count read and
1000 * write offset read, because consumed count can only change
1001 * concurrently in overwrite mode, and we keep a sequence counter
1002 * identifier derived from the write offset to check we are getting
1003 * the same sub-buffer we are expecting (the sub-buffers are atomically
1004 * "tagged" upon writes, tags are checked upon read).
1006 write_offset
= v_read(config
, &buf
->offset
);
1009 * Check that we are not about to read the same subbuffer in
1010 * which the writer head is.
1012 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1016 *consumed
= consumed_cur
;
1017 *produced
= subbuf_trunc(write_offset
, chan
);
1023 * The memory barriers __wait_event()/wake_up_interruptible() take care
1024 * of "raw_spin_is_locked" memory ordering.
1033 * lib_ring_buffer_move_consumer - move consumed counter forward
1035 * @consumed_new: new consumed count value
1037 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1038 unsigned long consumed_new
,
1039 struct lttng_ust_shm_handle
*handle
)
1041 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1042 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1043 unsigned long consumed
;
1045 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1048 * Only push the consumed value forward.
1049 * If the consumed cmpxchg fails, this is because we have been pushed by
1050 * the writer in flight recorder mode.
1052 consumed
= uatomic_read(&buf
->consumed
);
1053 while ((long) consumed
- (long) consumed_new
< 0)
1054 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1059 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1061 * @consumed: consumed count indicating the position where to read
1063 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1064 * data to read at consumed position, or 0 if the get operation succeeds.
1066 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1067 unsigned long consumed
,
1068 struct lttng_ust_shm_handle
*handle
)
1070 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1071 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1072 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1073 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1076 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1078 * Read finalized before counters.
1081 consumed_cur
= uatomic_read(&buf
->consumed
);
1082 consumed_idx
= subbuf_index(consumed
, chan
);
1083 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
1085 * Make sure we read the commit count before reading the buffer
1086 * data and the write offset. Correct consumed offset ordering
1087 * wrt commit count is insured by the use of cmpxchg to update
1088 * the consumed offset.
1091 * Local rmb to match the remote wmb to read the commit count
1092 * before the buffer data and the write offset.
1096 write_offset
= v_read(config
, &buf
->offset
);
1099 * Check that the buffer we are getting is after or at consumed_cur
1102 if ((long) subbuf_trunc(consumed
, chan
)
1103 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1107 * Check that the subbuffer we are trying to consume has been
1108 * already fully committed. There are a few causes that can make
1109 * this unavailability situation occur:
1111 * Temporary (short-term) situation:
1112 * - Application is running on a different CPU, between reserve
1113 * and commit ring buffer operations,
1114 * - Application is preempted between reserve and commit ring
1115 * buffer operations,
1117 * Long-term situation:
1118 * - Application is stopped (SIGSTOP) between reserve and commit
1119 * ring buffer operations. Could eventually be resumed by
1121 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1122 * reserve and commit ring buffer operation.
1124 * From a consumer perspective, handling short-term
1125 * unavailability situations is performed by retrying a few
1126 * times after a delay. Handling long-term unavailability
1127 * situations is handled by failing to get the sub-buffer.
1129 * In all of those situations, if the application is taking a
1130 * long time to perform its commit after ring buffer space
1131 * reservation, we can end up in a situation where the producer
1132 * will fill the ring buffer and try to write into the same
1133 * sub-buffer again (which has a missing commit). This is
1134 * handled by the producer in the sub-buffer switch handling
1135 * code of the reserve routine by detecting unbalanced
1136 * reserve/commit counters and discarding all further events
1137 * until the situation is resolved in those situations. Two
1138 * scenarios can occur:
1140 * 1) The application causing the reserve/commit counters to be
1141 * unbalanced has been terminated. In this situation, all
1142 * further events will be discarded in the buffers, and no
1143 * further buffer data will be readable by the consumer
1144 * daemon. Tearing down the UST tracing session and starting
1145 * anew is a work-around for those situations. Note that this
1146 * only affects per-UID tracing. In per-PID tracing, the
1147 * application vanishes with the termination, and therefore
1148 * no more data needs to be written to the buffers.
1149 * 2) The application causing the unbalance has been delayed for
1150 * a long time, but will eventually try to increment the
1151 * commit counter after eventually writing to the sub-buffer.
1152 * This situation can cause events to be discarded until the
1153 * application resumes its operations.
1155 if (((commit_count
- chan
->backend
.subbuf_size
)
1156 & chan
->commit_count_mask
)
1157 - (buf_trunc(consumed
, chan
)
1158 >> chan
->backend
.num_subbuf_order
)
1160 if (nr_retry
-- > 0) {
1161 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1162 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1170 * Check that we are not about to read the same subbuffer in
1171 * which the writer head is.
1173 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1178 * Failure to get the subbuffer causes a busy-loop retry without going
1179 * to a wait queue. These are caused by short-lived race windows where
1180 * the writer is getting access to a subbuffer we were trying to get
1181 * access to. Also checks that the "consumed" buffer count we are
1182 * looking for matches the one contained in the subbuffer id.
1184 * The short-lived race window described here can be affected by
1185 * application signals and preemption, thus requiring to bound
1186 * the loop to a maximum number of retry.
1188 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1189 consumed_idx
, buf_trunc_val(consumed
, chan
),
1192 if (nr_retry
-- > 0) {
1193 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1194 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1200 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1202 buf
->get_subbuf_consumed
= consumed
;
1203 buf
->get_subbuf
= 1;
1209 * The memory barriers __wait_event()/wake_up_interruptible() take care
1210 * of "raw_spin_is_locked" memory ordering.
1219 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1222 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1223 struct lttng_ust_shm_handle
*handle
)
1225 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1226 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1227 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1228 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1230 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1232 if (!buf
->get_subbuf
) {
1234 * Reader puts a subbuffer it did not get.
1236 CHAN_WARN_ON(chan
, 1);
1239 consumed
= buf
->get_subbuf_consumed
;
1240 buf
->get_subbuf
= 0;
1243 * Clear the records_unread counter. (overruns counter)
1244 * Can still be non-zero if a file reader simply grabbed the data
1245 * without using iterators.
1246 * Can be below zero if an iterator is used on a snapshot more than
1249 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1250 v_add(config
, v_read(config
,
1251 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
1252 &bufb
->records_read
);
1253 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
1254 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1255 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1256 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1259 * Exchange the reader subbuffer with the one we put in its place in the
1260 * writer subbuffer table. Expect the original consumed count. If
1261 * update_read_sb_index fails, this is because the writer updated the
1262 * subbuffer concurrently. We should therefore keep the subbuffer we
1263 * currently have: it has become invalid to try reading this sub-buffer
1264 * consumed count value anyway.
1266 consumed_idx
= subbuf_index(consumed
, chan
);
1267 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1268 consumed_idx
, buf_trunc_val(consumed
, chan
),
1271 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1272 * if the writer concurrently updated it.
1277 * cons_offset is an iterator on all subbuffer offsets between the reader
1278 * position and the writer position. (inclusive)
1281 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1282 struct channel
*chan
,
1283 unsigned long cons_offset
,
1285 struct lttng_ust_shm_handle
*handle
)
1287 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1288 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1290 cons_idx
= subbuf_index(cons_offset
, chan
);
1291 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
1292 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
1294 if (subbuf_offset(commit_count
, chan
) != 0)
1295 DBG("ring buffer %s, cpu %d: "
1296 "commit count in subbuffer %lu,\n"
1297 "expecting multiples of %lu bytes\n"
1298 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1299 chan
->backend
.name
, cpu
, cons_idx
,
1300 chan
->backend
.subbuf_size
,
1301 commit_count
, commit_count_sb
);
1303 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1304 chan
->backend
.name
, cpu
, commit_count
);
1308 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1309 struct channel
*chan
,
1310 void *priv
, int cpu
,
1311 struct lttng_ust_shm_handle
*handle
)
1313 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1314 unsigned long write_offset
, cons_offset
;
1317 * No need to order commit_count, write_offset and cons_offset reads
1318 * because we execute at teardown when no more writer nor reader
1319 * references are left.
1321 write_offset
= v_read(config
, &buf
->offset
);
1322 cons_offset
= uatomic_read(&buf
->consumed
);
1323 if (write_offset
!= cons_offset
)
1324 DBG("ring buffer %s, cpu %d: "
1325 "non-consumed data\n"
1326 " [ %lu bytes written, %lu bytes read ]\n",
1327 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1329 for (cons_offset
= uatomic_read(&buf
->consumed
);
1330 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1333 cons_offset
= subbuf_align(cons_offset
, chan
))
1334 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1339 void lib_ring_buffer_print_errors(struct channel
*chan
,
1340 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1341 struct lttng_ust_shm_handle
*handle
)
1343 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1344 void *priv
= channel_get_private(chan
);
1346 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1347 DBG("ring buffer %s: %lu records written, "
1348 "%lu records overrun\n",
1350 v_read(config
, &buf
->records_count
),
1351 v_read(config
, &buf
->records_overrun
));
1353 DBG("ring buffer %s, cpu %d: %lu records written, "
1354 "%lu records overrun\n",
1355 chan
->backend
.name
, cpu
,
1356 v_read(config
, &buf
->records_count
),
1357 v_read(config
, &buf
->records_overrun
));
1359 if (v_read(config
, &buf
->records_lost_full
)
1360 || v_read(config
, &buf
->records_lost_wrap
)
1361 || v_read(config
, &buf
->records_lost_big
))
1362 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1363 " [ %lu buffer full, %lu nest buffer wrap-around, "
1364 "%lu event too big ]\n",
1365 chan
->backend
.name
, cpu
,
1366 v_read(config
, &buf
->records_lost_full
),
1367 v_read(config
, &buf
->records_lost_wrap
),
1368 v_read(config
, &buf
->records_lost_big
));
1370 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1374 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1376 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1379 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1380 struct channel
*chan
,
1381 struct switch_offsets
*offsets
,
1383 struct lttng_ust_shm_handle
*handle
)
1385 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1386 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1387 unsigned long commit_count
;
1389 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1392 * Order all writes to buffer before the commit count update that will
1393 * determine that the subbuffer is full.
1396 v_add(config
, config
->cb
.subbuffer_header_size(),
1397 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1398 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1399 /* Check if the written buffer has to be delivered */
1400 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1401 commit_count
, oldidx
, handle
, tsc
);
1402 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1403 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1404 commit_count
, handle
);
1408 * lib_ring_buffer_switch_old_end: switch old subbuffer
1410 * Note : offset_old should never be 0 here. It is ok, because we never perform
1411 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1412 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1416 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1417 struct channel
*chan
,
1418 struct switch_offsets
*offsets
,
1420 struct lttng_ust_shm_handle
*handle
)
1422 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1423 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1424 unsigned long commit_count
, padding_size
, data_size
;
1426 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1427 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1428 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1432 * Order all writes to buffer before the commit count update that will
1433 * determine that the subbuffer is full.
1436 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1437 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1438 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1439 commit_count
, oldidx
, handle
, tsc
);
1440 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1441 offsets
->old
+ padding_size
, commit_count
, handle
);
1445 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1447 * This code can be executed unordered : writers may already have written to the
1448 * sub-buffer before this code gets executed, caution. The commit makes sure
1449 * that this code is executed before the deliver of this sub-buffer.
1452 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1453 struct channel
*chan
,
1454 struct switch_offsets
*offsets
,
1456 struct lttng_ust_shm_handle
*handle
)
1458 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1459 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1460 unsigned long commit_count
;
1462 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1465 * Order all writes to buffer before the commit count update that will
1466 * determine that the subbuffer is full.
1469 v_add(config
, config
->cb
.subbuffer_header_size(),
1470 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1471 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1472 /* Check if the written buffer has to be delivered */
1473 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1474 commit_count
, beginidx
, handle
, tsc
);
1475 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1476 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1477 commit_count
, handle
);
1481 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1483 * Calls subbuffer_set_data_size() to set the data size of the current
1484 * sub-buffer. We do not need to perform check_deliver nor commit here,
1485 * since this task will be done by the "commit" of the event for which
1486 * we are currently doing the space reservation.
1489 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1490 struct channel
*chan
,
1491 struct switch_offsets
*offsets
,
1493 struct lttng_ust_shm_handle
*handle
)
1495 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1496 unsigned long endidx
, data_size
;
1498 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1499 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1500 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1507 * !0 if execution must be aborted.
1510 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1511 struct lttng_ust_lib_ring_buffer
*buf
,
1512 struct channel
*chan
,
1513 struct switch_offsets
*offsets
,
1515 struct lttng_ust_shm_handle
*handle
)
1517 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1518 unsigned long off
, reserve_commit_diff
;
1520 offsets
->begin
= v_read(config
, &buf
->offset
);
1521 offsets
->old
= offsets
->begin
;
1522 offsets
->switch_old_start
= 0;
1523 off
= subbuf_offset(offsets
->begin
, chan
);
1525 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1528 * Ensure we flush the header of an empty subbuffer when doing the
1529 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1530 * total data gathering duration even if there were no records saved
1531 * after the last buffer switch.
1532 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1533 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1534 * subbuffer header as appropriate.
1535 * The next record that reserves space will be responsible for
1536 * populating the following subbuffer header. We choose not to populate
1537 * the next subbuffer header here because we want to be able to use
1538 * SWITCH_ACTIVE for periodical buffer flush, which must
1539 * guarantee that all the buffer content (records and header
1540 * timestamps) are visible to the reader. This is required for
1541 * quiescence guarantees for the fusion merge.
1543 if (mode
!= SWITCH_FLUSH
&& !off
)
1544 return -1; /* we do not have to switch : buffer is empty */
1546 if (caa_unlikely(off
== 0)) {
1547 unsigned long sb_index
, commit_count
;
1550 * We are performing a SWITCH_FLUSH. At this stage, there are no
1551 * concurrent writes into the buffer.
1553 * The client does not save any header information. Don't
1554 * switch empty subbuffer on finalize, because it is invalid to
1555 * deliver a completely empty subbuffer.
1557 if (!config
->cb
.subbuffer_header_size())
1560 /* Test new buffer integrity */
1561 sb_index
= subbuf_index(offsets
->begin
, chan
);
1562 commit_count
= v_read(config
,
1563 &shmp_index(handle
, buf
->commit_cold
,
1565 reserve_commit_diff
=
1566 (buf_trunc(offsets
->begin
, chan
)
1567 >> chan
->backend
.num_subbuf_order
)
1568 - (commit_count
& chan
->commit_count_mask
);
1569 if (caa_likely(reserve_commit_diff
== 0)) {
1570 /* Next subbuffer not being written to. */
1571 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1572 subbuf_trunc(offsets
->begin
, chan
)
1573 - subbuf_trunc((unsigned long)
1574 uatomic_read(&buf
->consumed
), chan
)
1575 >= chan
->backend
.buf_size
)) {
1577 * We do not overwrite non consumed buffers
1578 * and we are full : don't switch.
1583 * Next subbuffer not being written to, and we
1584 * are either in overwrite mode or the buffer is
1585 * not full. It's safe to write in this new
1591 * Next subbuffer reserve offset does not match the
1592 * commit offset. Don't perform switch in
1593 * producer-consumer and overwrite mode. Caused by
1594 * either a writer OOPS or too many nested writes over a
1595 * reserve/commit pair.
1601 * Need to write the subbuffer start header on finalize.
1603 offsets
->switch_old_start
= 1;
1605 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1606 /* Note: old points to the next subbuf at offset 0 */
1607 offsets
->end
= offsets
->begin
;
1612 * Force a sub-buffer switch. This operation is completely reentrant : can be
1613 * called while tracing is active with absolutely no lock held.
1615 * Note, however, that as a v_cmpxchg is used for some atomic
1616 * operations, this function must be called from the CPU which owns the buffer
1617 * for a ACTIVE flush.
1619 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1620 struct lttng_ust_shm_handle
*handle
)
1622 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1623 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1624 struct switch_offsets offsets
;
1625 unsigned long oldidx
;
1631 * Perform retryable operations.
1634 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1636 return; /* Switch not needed */
1637 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1641 * Atomically update last_tsc. This update races against concurrent
1642 * atomic updates, but the race will always cause supplementary full TSC
1643 * records, never the opposite (missing a full TSC record when it would
1646 save_last_tsc(config
, buf
, tsc
);
1649 * Push the reader if necessary
1651 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1653 oldidx
= subbuf_index(offsets
.old
, chan
);
1654 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1657 * May need to populate header start on SWITCH_FLUSH.
1659 if (offsets
.switch_old_start
) {
1660 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1661 offsets
.old
+= config
->cb
.subbuffer_header_size();
1665 * Switch old subbuffer.
1667 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1673 * -ENOSPC if event size is too large for packet.
1674 * -ENOBUFS if there is currently not enough space in buffer for the event.
1675 * -EIO if data cannot be written into the buffer for any other reason.
1678 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1679 struct channel
*chan
,
1680 struct switch_offsets
*offsets
,
1681 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1683 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1684 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1685 unsigned long reserve_commit_diff
, offset_cmp
;
1688 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1689 offsets
->old
= offsets
->begin
;
1690 offsets
->switch_new_start
= 0;
1691 offsets
->switch_new_end
= 0;
1692 offsets
->switch_old_end
= 0;
1693 offsets
->pre_header_padding
= 0;
1695 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1696 if ((int64_t) ctx
->tsc
== -EIO
)
1699 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1700 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1702 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1703 offsets
->switch_new_start
= 1; /* For offsets->begin */
1705 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1707 &offsets
->pre_header_padding
,
1710 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1713 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1714 offsets
->size
> chan
->backend
.subbuf_size
)) {
1715 offsets
->switch_old_end
= 1; /* For offsets->old */
1716 offsets
->switch_new_start
= 1; /* For offsets->begin */
1719 if (caa_unlikely(offsets
->switch_new_start
)) {
1720 unsigned long sb_index
, commit_count
;
1723 * We are typically not filling the previous buffer completely.
1725 if (caa_likely(offsets
->switch_old_end
))
1726 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1727 offsets
->begin
= offsets
->begin
1728 + config
->cb
.subbuffer_header_size();
1729 /* Test new buffer integrity */
1730 sb_index
= subbuf_index(offsets
->begin
, chan
);
1732 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1733 * lib_ring_buffer_check_deliver() has the matching
1734 * memory barriers required around commit_cold cc_sb
1735 * updates to ensure reserve and commit counter updates
1736 * are not seen reordered when updated by another CPU.
1739 commit_count
= v_read(config
,
1740 &shmp_index(handle
, buf
->commit_cold
,
1742 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1744 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1746 * The reserve counter have been concurrently updated
1747 * while we read the commit counter. This means the
1748 * commit counter we read might not match buf->offset
1749 * due to concurrent update. We therefore need to retry.
1753 reserve_commit_diff
=
1754 (buf_trunc(offsets
->begin
, chan
)
1755 >> chan
->backend
.num_subbuf_order
)
1756 - (commit_count
& chan
->commit_count_mask
);
1757 if (caa_likely(reserve_commit_diff
== 0)) {
1758 /* Next subbuffer not being written to. */
1759 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1760 subbuf_trunc(offsets
->begin
, chan
)
1761 - subbuf_trunc((unsigned long)
1762 uatomic_read(&buf
->consumed
), chan
)
1763 >= chan
->backend
.buf_size
)) {
1764 unsigned long nr_lost
;
1767 * We do not overwrite non consumed buffers
1768 * and we are full : record is lost.
1770 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1771 v_inc(config
, &buf
->records_lost_full
);
1772 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1773 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1774 nr_lost
+ 1, chan
->backend
.name
,
1780 * Next subbuffer not being written to, and we
1781 * are either in overwrite mode or the buffer is
1782 * not full. It's safe to write in this new
1787 unsigned long nr_lost
;
1790 * Next subbuffer reserve offset does not match the
1791 * commit offset, and this did not involve update to the
1792 * reserve counter. Drop record in producer-consumer and
1793 * overwrite mode. Caused by either a writer OOPS or too
1794 * many nested writes over a reserve/commit pair.
1796 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1797 v_inc(config
, &buf
->records_lost_wrap
);
1798 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1799 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1800 nr_lost
+ 1, chan
->backend
.name
,
1806 config
->cb
.record_header_size(config
, chan
,
1808 &offsets
->pre_header_padding
,
1811 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1814 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1815 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1816 unsigned long nr_lost
;
1819 * Record too big for subbuffers, report error, don't
1820 * complete the sub-buffer switch.
1822 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1823 v_inc(config
, &buf
->records_lost_big
);
1824 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1825 DBG("%lu or more records lost in (%s:%d) record size "
1826 " of %zu bytes is too large for buffer\n",
1827 nr_lost
+ 1, chan
->backend
.name
,
1828 buf
->backend
.cpu
, offsets
->size
);
1833 * We just made a successful buffer switch and the
1834 * record fits in the new subbuffer. Let's write.
1839 * Record fits in the current buffer and we are not on a switch
1840 * boundary. It's safe to write.
1843 offsets
->end
= offsets
->begin
+ offsets
->size
;
1845 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1847 * The offset_end will fall at the very beginning of the next
1850 offsets
->switch_new_end
= 1; /* For offsets->begin */
1856 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1857 * @ctx: ring buffer context.
1859 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1860 * -EIO for other errors, else returns 0.
1861 * It will take care of sub-buffer switching.
1863 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1865 struct channel
*chan
= ctx
->chan
;
1866 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1867 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1868 struct lttng_ust_lib_ring_buffer
*buf
;
1869 struct switch_offsets offsets
;
1872 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1873 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1875 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1881 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1883 if (caa_unlikely(ret
))
1885 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1890 * Atomically update last_tsc. This update races against concurrent
1891 * atomic updates, but the race will always cause supplementary full TSC
1892 * records, never the opposite (missing a full TSC record when it would
1895 save_last_tsc(config
, buf
, ctx
->tsc
);
1898 * Push the reader if necessary
1900 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1903 * Clear noref flag for this subbuffer.
1905 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1906 subbuf_index(offsets
.end
- 1, chan
),
1910 * Switch old subbuffer if needed.
1912 if (caa_unlikely(offsets
.switch_old_end
)) {
1913 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1914 subbuf_index(offsets
.old
- 1, chan
),
1916 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1920 * Populate new subbuffer.
1922 if (caa_unlikely(offsets
.switch_new_start
))
1923 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1925 if (caa_unlikely(offsets
.switch_new_end
))
1926 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1928 ctx
->slot_size
= offsets
.size
;
1929 ctx
->pre_offset
= offsets
.begin
;
1930 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1935 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1937 void lttng_fixup_ringbuffer_tls(void)
1939 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
1942 void lib_ringbuffer_signal_init(void)
1948 * Block signal for entire process, so only our thread processes
1952 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
1955 PERROR("pthread_sigmask");