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
;
163 const struct lttng_ust_lib_ring_buffer_config
*config
;
166 chan
= shmp(handle
, buf
->backend
.chan
);
169 config
= &chan
->backend
.config
;
171 * Reset iterator first. It will put the subbuffer if it currently holds
174 v_set(config
, &buf
->offset
, 0);
175 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
176 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
177 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
178 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
180 uatomic_set(&buf
->consumed
, 0);
181 uatomic_set(&buf
->record_disabled
, 0);
182 v_set(config
, &buf
->last_tsc
, 0);
183 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
184 /* Don't reset number of active readers */
185 v_set(config
, &buf
->records_lost_full
, 0);
186 v_set(config
, &buf
->records_lost_wrap
, 0);
187 v_set(config
, &buf
->records_lost_big
, 0);
188 v_set(config
, &buf
->records_count
, 0);
189 v_set(config
, &buf
->records_overrun
, 0);
194 * channel_reset - Reset channel to initial values.
197 * Effectively empty the channel. Should be called when the channel is not used
198 * for writing. The channel can be opened for reading, but the reader should not
199 * be using the iterator concurrently with reset. The previous current iterator
202 void channel_reset(struct channel
*chan
)
205 * Reset iterators first. Will put the subbuffer if held for reading.
207 uatomic_set(&chan
->record_disabled
, 0);
208 /* Don't reset commit_count_mask, still valid */
209 channel_backend_reset(&chan
->backend
);
210 /* Don't reset switch/read timer interval */
211 /* Don't reset notifiers and notifier enable bits */
212 /* Don't reset reader reference count */
216 * Must be called under cpu hotplug protection.
218 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
219 struct channel_backend
*chanb
, int cpu
,
220 struct lttng_ust_shm_handle
*handle
,
221 struct shm_object
*shmobj
)
223 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
224 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
225 void *priv
= channel_get_private(chan
);
226 size_t subbuf_header_size
;
230 /* Test for cpu hotplug */
231 if (buf
->backend
.allocated
)
234 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
235 cpu
, handle
, shmobj
);
239 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
240 set_shmp(buf
->commit_hot
,
242 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
243 if (!shmp(handle
, buf
->commit_hot
)) {
248 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
249 set_shmp(buf
->commit_cold
,
251 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
252 if (!shmp(handle
, buf
->commit_cold
)) {
258 * Write the subbuffer header for first subbuffer so we know the total
259 * duration of data gathering.
261 subbuf_header_size
= config
->cb
.subbuffer_header_size();
262 v_set(config
, &buf
->offset
, subbuf_header_size
);
263 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
264 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
265 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
266 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
268 if (config
->cb
.buffer_create
) {
269 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
273 buf
->backend
.allocated
= 1;
278 /* commit_cold will be freed by shm teardown */
280 /* commit_hot will be freed by shm teardown */
286 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
288 const struct lttng_ust_lib_ring_buffer_config
*config
;
289 struct lttng_ust_shm_handle
*handle
;
290 struct channel
*chan
;
293 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
295 chan
= si
->si_value
.sival_ptr
;
296 handle
= chan
->handle
;
297 config
= &chan
->backend
.config
;
299 DBG("Switch timer for channel %p\n", chan
);
302 * Only flush buffers periodically if readers are active.
304 pthread_mutex_lock(&wakeup_fd_mutex
);
305 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
306 for_each_possible_cpu(cpu
) {
307 struct lttng_ust_lib_ring_buffer
*buf
=
308 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
312 if (uatomic_read(&buf
->active_readers
))
313 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
317 struct lttng_ust_lib_ring_buffer
*buf
=
318 shmp(handle
, chan
->backend
.buf
[0].shmp
);
322 if (uatomic_read(&buf
->active_readers
))
323 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
326 pthread_mutex_unlock(&wakeup_fd_mutex
);
331 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
333 const struct lttng_ust_lib_ring_buffer_config
*config
;
334 struct lttng_ust_shm_handle
*handle
;
337 handle
= chan
->handle
;
338 config
= &chan
->backend
.config
;
341 * Only flush buffers periodically if readers are active.
343 pthread_mutex_lock(&wakeup_fd_mutex
);
344 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
345 for_each_possible_cpu(cpu
) {
346 struct lttng_ust_lib_ring_buffer
*buf
=
347 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
351 if (uatomic_read(&buf
->active_readers
)
352 && lib_ring_buffer_poll_deliver(config
, buf
,
354 lib_ring_buffer_wakeup(buf
, handle
);
358 struct lttng_ust_lib_ring_buffer
*buf
=
359 shmp(handle
, chan
->backend
.buf
[0].shmp
);
363 if (uatomic_read(&buf
->active_readers
)
364 && lib_ring_buffer_poll_deliver(config
, buf
,
366 lib_ring_buffer_wakeup(buf
, handle
);
369 pthread_mutex_unlock(&wakeup_fd_mutex
);
373 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
375 struct channel
*chan
;
377 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
378 chan
= si
->si_value
.sival_ptr
;
379 DBG("Read timer for channel %p\n", chan
);
380 lib_ring_buffer_channel_do_read(chan
);
385 void rb_setmask(sigset_t
*mask
)
389 ret
= sigemptyset(mask
);
391 PERROR("sigemptyset");
393 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
397 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
401 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
408 void *sig_thread(void *arg
)
414 /* Only self thread will receive signal mask. */
416 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
419 signr
= sigwaitinfo(&mask
, &info
);
422 PERROR("sigwaitinfo");
425 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
426 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
428 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
429 lib_ring_buffer_channel_read_timer(info
.si_signo
,
431 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
433 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
436 ERR("Unexptected signal %d\n", info
.si_signo
);
443 * Ensure only a single thread listens on the timer signal.
446 void lib_ring_buffer_setup_timer_thread(void)
451 pthread_mutex_lock(&timer_signal
.lock
);
452 if (timer_signal
.setup_done
)
455 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
458 PERROR("pthread_create");
460 ret
= pthread_detach(thread
);
463 PERROR("pthread_detach");
465 timer_signal
.setup_done
= 1;
467 pthread_mutex_unlock(&timer_signal
.lock
);
471 * Wait for signal-handling thread quiescent state.
474 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
476 sigset_t pending_set
;
480 * We need to be the only thread interacting with the thread
481 * that manages signals for teardown synchronization.
483 pthread_mutex_lock(&timer_signal
.lock
);
486 * Ensure we don't have any signal queued for this channel.
489 ret
= sigemptyset(&pending_set
);
491 PERROR("sigemptyset");
493 ret
= sigpending(&pending_set
);
495 PERROR("sigpending");
497 if (!sigismember(&pending_set
, signr
))
503 * From this point, no new signal handler will be fired that
504 * would try to access "chan". However, we still need to wait
505 * for any currently executing handler to complete.
508 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
512 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
515 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
517 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
521 pthread_mutex_unlock(&timer_signal
.lock
);
525 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
528 struct itimerspec its
;
531 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
534 chan
->switch_timer_enabled
= 1;
536 lib_ring_buffer_setup_timer_thread();
538 sev
.sigev_notify
= SIGEV_SIGNAL
;
539 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
540 sev
.sigev_value
.sival_ptr
= chan
;
541 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
543 PERROR("timer_create");
546 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
547 its
.it_value
.tv_nsec
= chan
->switch_timer_interval
% 1000000;
548 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
549 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
551 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
553 PERROR("timer_settime");
558 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
562 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
565 ret
= timer_delete(chan
->switch_timer
);
567 PERROR("timer_delete");
570 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
572 chan
->switch_timer
= 0;
573 chan
->switch_timer_enabled
= 0;
577 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
579 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
581 struct itimerspec its
;
584 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
585 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
588 chan
->read_timer_enabled
= 1;
590 lib_ring_buffer_setup_timer_thread();
592 sev
.sigev_notify
= SIGEV_SIGNAL
;
593 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
594 sev
.sigev_value
.sival_ptr
= chan
;
595 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
597 PERROR("timer_create");
600 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
601 its
.it_value
.tv_nsec
= chan
->read_timer_interval
% 1000000;
602 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
603 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
605 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
607 PERROR("timer_settime");
612 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
614 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
617 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
618 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
621 ret
= timer_delete(chan
->read_timer
);
623 PERROR("timer_delete");
627 * do one more check to catch data that has been written in the last
630 lib_ring_buffer_channel_do_read(chan
);
632 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
634 chan
->read_timer
= 0;
635 chan
->read_timer_enabled
= 0;
638 static void channel_unregister_notifiers(struct channel
*chan
,
639 struct lttng_ust_shm_handle
*handle
)
641 lib_ring_buffer_channel_switch_timer_stop(chan
);
642 lib_ring_buffer_channel_read_timer_stop(chan
);
645 static void channel_print_errors(struct channel
*chan
,
646 struct lttng_ust_shm_handle
*handle
)
648 const struct lttng_ust_lib_ring_buffer_config
*config
=
649 &chan
->backend
.config
;
652 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
653 for_each_possible_cpu(cpu
) {
654 struct lttng_ust_lib_ring_buffer
*buf
=
655 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
656 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
659 struct lttng_ust_lib_ring_buffer
*buf
=
660 shmp(handle
, chan
->backend
.buf
[0].shmp
);
662 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
666 static void channel_free(struct channel
*chan
,
667 struct lttng_ust_shm_handle
*handle
)
669 channel_backend_free(&chan
->backend
, handle
);
670 /* chan is freed by shm teardown */
671 shm_object_table_destroy(handle
->table
);
676 * channel_create - Create channel.
677 * @config: ring buffer instance configuration
678 * @name: name of the channel
679 * @priv_data: ring buffer client private data area pointer (output)
680 * @priv_data_size: length, in bytes, of the private data area.
681 * @priv_data_init: initialization data for private data.
682 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
683 * address mapping. It is used only by RING_BUFFER_STATIC
684 * configuration. It can be set to NULL for other backends.
685 * @subbuf_size: subbuffer size
686 * @num_subbuf: number of subbuffers
687 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
688 * padding to let readers get those sub-buffers.
689 * Used for live streaming.
690 * @read_timer_interval: Time interval (in us) to wake up pending readers.
693 * Returns NULL on failure.
695 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
698 size_t priv_data_align
,
699 size_t priv_data_size
,
700 void *priv_data_init
,
701 void *buf_addr
, size_t subbuf_size
,
702 size_t num_subbuf
, unsigned int switch_timer_interval
,
703 unsigned int read_timer_interval
)
706 size_t shmsize
, chansize
;
707 struct channel
*chan
;
708 struct lttng_ust_shm_handle
*handle
;
709 struct shm_object
*shmobj
;
710 unsigned int nr_streams
;
712 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
713 nr_streams
= num_possible_cpus();
717 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
718 read_timer_interval
))
721 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
725 /* Allocate table for channel + per-cpu buffers */
726 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
728 goto error_table_alloc
;
730 /* Calculate the shm allocation layout */
731 shmsize
= sizeof(struct channel
);
732 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
733 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
736 shmsize
+= offset_align(shmsize
, priv_data_align
);
737 shmsize
+= priv_data_size
;
739 /* Allocate normal memory for channel (not shared) */
740 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
);
743 /* struct channel is at object 0, offset 0 (hardcoded) */
744 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
745 assert(handle
->chan
._ref
.index
== 0);
746 assert(handle
->chan
._ref
.offset
== 0);
747 chan
= shmp(handle
, handle
->chan
);
750 chan
->nr_streams
= nr_streams
;
752 /* space for private data */
753 if (priv_data_size
) {
754 DECLARE_SHMP(void, priv_data_alloc
);
756 align_shm(shmobj
, priv_data_align
);
757 chan
->priv_data_offset
= shmobj
->allocated_len
;
758 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
759 if (!shmp(handle
, priv_data_alloc
))
761 *priv_data
= channel_get_private(chan
);
762 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
764 chan
->priv_data_offset
= -1;
769 ret
= channel_backend_init(&chan
->backend
, name
, config
,
770 subbuf_size
, num_subbuf
, handle
);
772 goto error_backend_init
;
774 chan
->handle
= handle
;
775 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
777 chan
->switch_timer_interval
= switch_timer_interval
;
778 chan
->read_timer_interval
= read_timer_interval
;
779 lib_ring_buffer_channel_switch_timer_start(chan
);
780 lib_ring_buffer_channel_read_timer_start(chan
);
786 shm_object_table_destroy(handle
->table
);
792 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
793 uint64_t memory_map_size
,
796 struct lttng_ust_shm_handle
*handle
;
797 struct shm_object
*object
;
799 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
803 /* Allocate table for channel + per-cpu buffers */
804 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
806 goto error_table_alloc
;
807 /* Add channel object */
808 object
= shm_object_table_append_mem(handle
->table
, data
,
809 memory_map_size
, wakeup_fd
);
811 goto error_table_object
;
812 /* struct channel is at object 0, offset 0 (hardcoded) */
813 handle
->chan
._ref
.index
= 0;
814 handle
->chan
._ref
.offset
= 0;
818 shm_object_table_destroy(handle
->table
);
824 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
825 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
826 uint64_t memory_map_size
)
828 struct shm_object
*object
;
830 /* Add stream object */
831 object
= shm_object_table_append_shm(handle
->table
,
832 shm_fd
, wakeup_fd
, stream_nr
,
839 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
841 assert(handle
->table
);
842 return handle
->table
->allocated_len
- 1;
846 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
848 channel_free(chan
, handle
);
852 * channel_destroy - Finalize, wait for q.s. and destroy channel.
853 * @chan: channel to destroy
856 * Call "destroy" callback, finalize channels, decrement the channel
857 * reference count. Note that when readers have completed data
858 * consumption of finalized channels, get_subbuf() will return -ENODATA.
859 * They should release their handle at that point.
861 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
866 * Note: the consumer takes care of finalizing and
867 * switching the buffers.
869 channel_unregister_notifiers(chan
, handle
);
871 * The consumer prints errors.
873 channel_print_errors(chan
, handle
);
877 * sessiond/consumer are keeping a reference on the shm file
878 * descriptor directly. No need to refcount.
880 channel_release(chan
, handle
);
884 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
885 const struct lttng_ust_lib_ring_buffer_config
*config
,
886 struct channel
*chan
, int cpu
,
887 struct lttng_ust_shm_handle
*handle
,
888 int *shm_fd
, int *wait_fd
,
890 uint64_t *memory_map_size
)
894 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
897 if (cpu
>= num_possible_cpus())
900 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
901 *shm_fd
= shm_get_shm_fd(handle
, ref
);
902 *wait_fd
= shm_get_wait_fd(handle
, ref
);
903 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
904 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
906 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
909 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
910 struct channel
*chan
,
911 struct lttng_ust_shm_handle
*handle
)
915 ref
= &handle
->chan
._ref
;
916 return shm_close_wait_fd(handle
, ref
);
919 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
920 struct channel
*chan
,
921 struct lttng_ust_shm_handle
*handle
)
925 ref
= &handle
->chan
._ref
;
926 return shm_close_wakeup_fd(handle
, ref
);
929 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
930 struct channel
*chan
,
931 struct lttng_ust_shm_handle
*handle
,
936 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
939 if (cpu
>= num_possible_cpus())
942 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
943 return shm_close_wait_fd(handle
, ref
);
946 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
947 struct channel
*chan
,
948 struct lttng_ust_shm_handle
*handle
,
954 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
957 if (cpu
>= num_possible_cpus())
960 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
961 pthread_mutex_lock(&wakeup_fd_mutex
);
962 ret
= shm_close_wakeup_fd(handle
, ref
);
963 pthread_mutex_unlock(&wakeup_fd_mutex
);
967 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
968 struct lttng_ust_shm_handle
*handle
)
970 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
976 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
977 struct lttng_ust_shm_handle
*handle
)
979 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
981 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
983 uatomic_dec(&buf
->active_readers
);
987 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
989 * @consumed: consumed count indicating the position where to read
990 * @produced: produced count, indicates position when to stop reading
992 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
993 * data to read at consumed position, or 0 if the get operation succeeds.
996 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
997 unsigned long *consumed
, unsigned long *produced
,
998 struct lttng_ust_shm_handle
*handle
)
1000 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1001 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1002 unsigned long consumed_cur
, write_offset
;
1005 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1007 * Read finalized before counters.
1010 consumed_cur
= uatomic_read(&buf
->consumed
);
1012 * No need to issue a memory barrier between consumed count read and
1013 * write offset read, because consumed count can only change
1014 * concurrently in overwrite mode, and we keep a sequence counter
1015 * identifier derived from the write offset to check we are getting
1016 * the same sub-buffer we are expecting (the sub-buffers are atomically
1017 * "tagged" upon writes, tags are checked upon read).
1019 write_offset
= v_read(config
, &buf
->offset
);
1022 * Check that we are not about to read the same subbuffer in
1023 * which the writer head is.
1025 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1029 *consumed
= consumed_cur
;
1030 *produced
= subbuf_trunc(write_offset
, chan
);
1036 * The memory barriers __wait_event()/wake_up_interruptible() take care
1037 * of "raw_spin_is_locked" memory ordering.
1046 * lib_ring_buffer_move_consumer - move consumed counter forward
1048 * @consumed_new: new consumed count value
1050 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1051 unsigned long consumed_new
,
1052 struct lttng_ust_shm_handle
*handle
)
1054 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1055 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1056 unsigned long consumed
;
1058 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1061 * Only push the consumed value forward.
1062 * If the consumed cmpxchg fails, this is because we have been pushed by
1063 * the writer in flight recorder mode.
1065 consumed
= uatomic_read(&buf
->consumed
);
1066 while ((long) consumed
- (long) consumed_new
< 0)
1067 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1072 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1074 * @consumed: consumed count indicating the position where to read
1076 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1077 * data to read at consumed position, or 0 if the get operation succeeds.
1079 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1080 unsigned long consumed
,
1081 struct lttng_ust_shm_handle
*handle
)
1083 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1084 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1085 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1086 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1089 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1091 * Read finalized before counters.
1094 consumed_cur
= uatomic_read(&buf
->consumed
);
1095 consumed_idx
= subbuf_index(consumed
, chan
);
1096 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
1098 * Make sure we read the commit count before reading the buffer
1099 * data and the write offset. Correct consumed offset ordering
1100 * wrt commit count is insured by the use of cmpxchg to update
1101 * the consumed offset.
1104 * Local rmb to match the remote wmb to read the commit count
1105 * before the buffer data and the write offset.
1109 write_offset
= v_read(config
, &buf
->offset
);
1112 * Check that the buffer we are getting is after or at consumed_cur
1115 if ((long) subbuf_trunc(consumed
, chan
)
1116 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1120 * Check that the subbuffer we are trying to consume has been
1121 * already fully committed. There are a few causes that can make
1122 * this unavailability situation occur:
1124 * Temporary (short-term) situation:
1125 * - Application is running on a different CPU, between reserve
1126 * and commit ring buffer operations,
1127 * - Application is preempted between reserve and commit ring
1128 * buffer operations,
1130 * Long-term situation:
1131 * - Application is stopped (SIGSTOP) between reserve and commit
1132 * ring buffer operations. Could eventually be resumed by
1134 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1135 * reserve and commit ring buffer operation.
1137 * From a consumer perspective, handling short-term
1138 * unavailability situations is performed by retrying a few
1139 * times after a delay. Handling long-term unavailability
1140 * situations is handled by failing to get the sub-buffer.
1142 * In all of those situations, if the application is taking a
1143 * long time to perform its commit after ring buffer space
1144 * reservation, we can end up in a situation where the producer
1145 * will fill the ring buffer and try to write into the same
1146 * sub-buffer again (which has a missing commit). This is
1147 * handled by the producer in the sub-buffer switch handling
1148 * code of the reserve routine by detecting unbalanced
1149 * reserve/commit counters and discarding all further events
1150 * until the situation is resolved in those situations. Two
1151 * scenarios can occur:
1153 * 1) The application causing the reserve/commit counters to be
1154 * unbalanced has been terminated. In this situation, all
1155 * further events will be discarded in the buffers, and no
1156 * further buffer data will be readable by the consumer
1157 * daemon. Tearing down the UST tracing session and starting
1158 * anew is a work-around for those situations. Note that this
1159 * only affects per-UID tracing. In per-PID tracing, the
1160 * application vanishes with the termination, and therefore
1161 * no more data needs to be written to the buffers.
1162 * 2) The application causing the unbalance has been delayed for
1163 * a long time, but will eventually try to increment the
1164 * commit counter after eventually writing to the sub-buffer.
1165 * This situation can cause events to be discarded until the
1166 * application resumes its operations.
1168 if (((commit_count
- chan
->backend
.subbuf_size
)
1169 & chan
->commit_count_mask
)
1170 - (buf_trunc(consumed
, chan
)
1171 >> chan
->backend
.num_subbuf_order
)
1173 if (nr_retry
-- > 0) {
1174 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1175 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1183 * Check that we are not about to read the same subbuffer in
1184 * which the writer head is.
1186 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1191 * Failure to get the subbuffer causes a busy-loop retry without going
1192 * to a wait queue. These are caused by short-lived race windows where
1193 * the writer is getting access to a subbuffer we were trying to get
1194 * access to. Also checks that the "consumed" buffer count we are
1195 * looking for matches the one contained in the subbuffer id.
1197 * The short-lived race window described here can be affected by
1198 * application signals and preemption, thus requiring to bound
1199 * the loop to a maximum number of retry.
1201 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1202 consumed_idx
, buf_trunc_val(consumed
, chan
),
1205 if (nr_retry
-- > 0) {
1206 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1207 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1213 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1215 buf
->get_subbuf_consumed
= consumed
;
1216 buf
->get_subbuf
= 1;
1222 * The memory barriers __wait_event()/wake_up_interruptible() take care
1223 * of "raw_spin_is_locked" memory ordering.
1232 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1235 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1236 struct lttng_ust_shm_handle
*handle
)
1238 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1239 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1240 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1241 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1243 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1245 if (!buf
->get_subbuf
) {
1247 * Reader puts a subbuffer it did not get.
1249 CHAN_WARN_ON(chan
, 1);
1252 consumed
= buf
->get_subbuf_consumed
;
1253 buf
->get_subbuf
= 0;
1256 * Clear the records_unread counter. (overruns counter)
1257 * Can still be non-zero if a file reader simply grabbed the data
1258 * without using iterators.
1259 * Can be below zero if an iterator is used on a snapshot more than
1262 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1263 v_add(config
, v_read(config
,
1264 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
1265 &bufb
->records_read
);
1266 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
1267 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1268 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1269 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1272 * Exchange the reader subbuffer with the one we put in its place in the
1273 * writer subbuffer table. Expect the original consumed count. If
1274 * update_read_sb_index fails, this is because the writer updated the
1275 * subbuffer concurrently. We should therefore keep the subbuffer we
1276 * currently have: it has become invalid to try reading this sub-buffer
1277 * consumed count value anyway.
1279 consumed_idx
= subbuf_index(consumed
, chan
);
1280 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1281 consumed_idx
, buf_trunc_val(consumed
, chan
),
1284 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1285 * if the writer concurrently updated it.
1290 * cons_offset is an iterator on all subbuffer offsets between the reader
1291 * position and the writer position. (inclusive)
1294 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1295 struct channel
*chan
,
1296 unsigned long cons_offset
,
1298 struct lttng_ust_shm_handle
*handle
)
1300 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1301 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1303 cons_idx
= subbuf_index(cons_offset
, chan
);
1304 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
1305 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
1307 if (subbuf_offset(commit_count
, chan
) != 0)
1308 DBG("ring buffer %s, cpu %d: "
1309 "commit count in subbuffer %lu,\n"
1310 "expecting multiples of %lu bytes\n"
1311 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1312 chan
->backend
.name
, cpu
, cons_idx
,
1313 chan
->backend
.subbuf_size
,
1314 commit_count
, commit_count_sb
);
1316 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1317 chan
->backend
.name
, cpu
, commit_count
);
1321 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1322 struct channel
*chan
,
1323 void *priv
, int cpu
,
1324 struct lttng_ust_shm_handle
*handle
)
1326 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1327 unsigned long write_offset
, cons_offset
;
1330 * No need to order commit_count, write_offset and cons_offset reads
1331 * because we execute at teardown when no more writer nor reader
1332 * references are left.
1334 write_offset
= v_read(config
, &buf
->offset
);
1335 cons_offset
= uatomic_read(&buf
->consumed
);
1336 if (write_offset
!= cons_offset
)
1337 DBG("ring buffer %s, cpu %d: "
1338 "non-consumed data\n"
1339 " [ %lu bytes written, %lu bytes read ]\n",
1340 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1342 for (cons_offset
= uatomic_read(&buf
->consumed
);
1343 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1346 cons_offset
= subbuf_align(cons_offset
, chan
))
1347 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1352 void lib_ring_buffer_print_errors(struct channel
*chan
,
1353 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1354 struct lttng_ust_shm_handle
*handle
)
1356 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1357 void *priv
= channel_get_private(chan
);
1359 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1360 DBG("ring buffer %s: %lu records written, "
1361 "%lu records overrun\n",
1363 v_read(config
, &buf
->records_count
),
1364 v_read(config
, &buf
->records_overrun
));
1366 DBG("ring buffer %s, cpu %d: %lu records written, "
1367 "%lu records overrun\n",
1368 chan
->backend
.name
, cpu
,
1369 v_read(config
, &buf
->records_count
),
1370 v_read(config
, &buf
->records_overrun
));
1372 if (v_read(config
, &buf
->records_lost_full
)
1373 || v_read(config
, &buf
->records_lost_wrap
)
1374 || v_read(config
, &buf
->records_lost_big
))
1375 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1376 " [ %lu buffer full, %lu nest buffer wrap-around, "
1377 "%lu event too big ]\n",
1378 chan
->backend
.name
, cpu
,
1379 v_read(config
, &buf
->records_lost_full
),
1380 v_read(config
, &buf
->records_lost_wrap
),
1381 v_read(config
, &buf
->records_lost_big
));
1383 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1387 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1389 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1392 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1393 struct channel
*chan
,
1394 struct switch_offsets
*offsets
,
1396 struct lttng_ust_shm_handle
*handle
)
1398 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1399 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1400 unsigned long commit_count
;
1402 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1405 * Order all writes to buffer before the commit count update that will
1406 * determine that the subbuffer is full.
1409 v_add(config
, config
->cb
.subbuffer_header_size(),
1410 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1411 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1412 /* Check if the written buffer has to be delivered */
1413 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1414 commit_count
, oldidx
, handle
, tsc
);
1415 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1416 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1417 commit_count
, handle
);
1421 * lib_ring_buffer_switch_old_end: switch old subbuffer
1423 * Note : offset_old should never be 0 here. It is ok, because we never perform
1424 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1425 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1429 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1430 struct channel
*chan
,
1431 struct switch_offsets
*offsets
,
1433 struct lttng_ust_shm_handle
*handle
)
1435 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1436 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1437 unsigned long commit_count
, padding_size
, data_size
;
1439 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1440 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1441 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1445 * Order all writes to buffer before the commit count update that will
1446 * determine that the subbuffer is full.
1449 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1450 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1451 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1452 commit_count
, oldidx
, handle
, tsc
);
1453 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1454 offsets
->old
+ padding_size
, commit_count
, handle
);
1458 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1460 * This code can be executed unordered : writers may already have written to the
1461 * sub-buffer before this code gets executed, caution. The commit makes sure
1462 * that this code is executed before the deliver of this sub-buffer.
1465 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1466 struct channel
*chan
,
1467 struct switch_offsets
*offsets
,
1469 struct lttng_ust_shm_handle
*handle
)
1471 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1472 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1473 unsigned long commit_count
;
1475 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1478 * Order all writes to buffer before the commit count update that will
1479 * determine that the subbuffer is full.
1482 v_add(config
, config
->cb
.subbuffer_header_size(),
1483 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1484 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1485 /* Check if the written buffer has to be delivered */
1486 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1487 commit_count
, beginidx
, handle
, tsc
);
1488 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1489 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1490 commit_count
, handle
);
1494 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1496 * Calls subbuffer_set_data_size() to set the data size of the current
1497 * sub-buffer. We do not need to perform check_deliver nor commit here,
1498 * since this task will be done by the "commit" of the event for which
1499 * we are currently doing the space reservation.
1502 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1503 struct channel
*chan
,
1504 struct switch_offsets
*offsets
,
1506 struct lttng_ust_shm_handle
*handle
)
1508 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1509 unsigned long endidx
, data_size
;
1511 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1512 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1513 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1520 * !0 if execution must be aborted.
1523 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1524 struct lttng_ust_lib_ring_buffer
*buf
,
1525 struct channel
*chan
,
1526 struct switch_offsets
*offsets
,
1528 struct lttng_ust_shm_handle
*handle
)
1530 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1531 unsigned long off
, reserve_commit_diff
;
1533 offsets
->begin
= v_read(config
, &buf
->offset
);
1534 offsets
->old
= offsets
->begin
;
1535 offsets
->switch_old_start
= 0;
1536 off
= subbuf_offset(offsets
->begin
, chan
);
1538 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1541 * Ensure we flush the header of an empty subbuffer when doing the
1542 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1543 * total data gathering duration even if there were no records saved
1544 * after the last buffer switch.
1545 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1546 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1547 * subbuffer header as appropriate.
1548 * The next record that reserves space will be responsible for
1549 * populating the following subbuffer header. We choose not to populate
1550 * the next subbuffer header here because we want to be able to use
1551 * SWITCH_ACTIVE for periodical buffer flush, which must
1552 * guarantee that all the buffer content (records and header
1553 * timestamps) are visible to the reader. This is required for
1554 * quiescence guarantees for the fusion merge.
1556 if (mode
!= SWITCH_FLUSH
&& !off
)
1557 return -1; /* we do not have to switch : buffer is empty */
1559 if (caa_unlikely(off
== 0)) {
1560 unsigned long sb_index
, commit_count
;
1563 * We are performing a SWITCH_FLUSH. At this stage, there are no
1564 * concurrent writes into the buffer.
1566 * The client does not save any header information. Don't
1567 * switch empty subbuffer on finalize, because it is invalid to
1568 * deliver a completely empty subbuffer.
1570 if (!config
->cb
.subbuffer_header_size())
1573 /* Test new buffer integrity */
1574 sb_index
= subbuf_index(offsets
->begin
, chan
);
1575 commit_count
= v_read(config
,
1576 &shmp_index(handle
, buf
->commit_cold
,
1578 reserve_commit_diff
=
1579 (buf_trunc(offsets
->begin
, chan
)
1580 >> chan
->backend
.num_subbuf_order
)
1581 - (commit_count
& chan
->commit_count_mask
);
1582 if (caa_likely(reserve_commit_diff
== 0)) {
1583 /* Next subbuffer not being written to. */
1584 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1585 subbuf_trunc(offsets
->begin
, chan
)
1586 - subbuf_trunc((unsigned long)
1587 uatomic_read(&buf
->consumed
), chan
)
1588 >= chan
->backend
.buf_size
)) {
1590 * We do not overwrite non consumed buffers
1591 * and we are full : don't switch.
1596 * Next subbuffer not being written to, and we
1597 * are either in overwrite mode or the buffer is
1598 * not full. It's safe to write in this new
1604 * Next subbuffer reserve offset does not match the
1605 * commit offset. Don't perform switch in
1606 * producer-consumer and overwrite mode. Caused by
1607 * either a writer OOPS or too many nested writes over a
1608 * reserve/commit pair.
1614 * Need to write the subbuffer start header on finalize.
1616 offsets
->switch_old_start
= 1;
1618 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1619 /* Note: old points to the next subbuf at offset 0 */
1620 offsets
->end
= offsets
->begin
;
1625 * Force a sub-buffer switch. This operation is completely reentrant : can be
1626 * called while tracing is active with absolutely no lock held.
1628 * Note, however, that as a v_cmpxchg is used for some atomic
1629 * operations, this function must be called from the CPU which owns the buffer
1630 * for a ACTIVE flush.
1632 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1633 struct lttng_ust_shm_handle
*handle
)
1635 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1636 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1637 struct switch_offsets offsets
;
1638 unsigned long oldidx
;
1644 * Perform retryable operations.
1647 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1649 return; /* Switch not needed */
1650 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1654 * Atomically update last_tsc. This update races against concurrent
1655 * atomic updates, but the race will always cause supplementary full TSC
1656 * records, never the opposite (missing a full TSC record when it would
1659 save_last_tsc(config
, buf
, tsc
);
1662 * Push the reader if necessary
1664 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1666 oldidx
= subbuf_index(offsets
.old
, chan
);
1667 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1670 * May need to populate header start on SWITCH_FLUSH.
1672 if (offsets
.switch_old_start
) {
1673 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1674 offsets
.old
+= config
->cb
.subbuffer_header_size();
1678 * Switch old subbuffer.
1680 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1686 * -ENOSPC if event size is too large for packet.
1687 * -ENOBUFS if there is currently not enough space in buffer for the event.
1688 * -EIO if data cannot be written into the buffer for any other reason.
1691 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1692 struct channel
*chan
,
1693 struct switch_offsets
*offsets
,
1694 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1696 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1697 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1698 unsigned long reserve_commit_diff
, offset_cmp
;
1701 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1702 offsets
->old
= offsets
->begin
;
1703 offsets
->switch_new_start
= 0;
1704 offsets
->switch_new_end
= 0;
1705 offsets
->switch_old_end
= 0;
1706 offsets
->pre_header_padding
= 0;
1708 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1709 if ((int64_t) ctx
->tsc
== -EIO
)
1712 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1713 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1715 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1716 offsets
->switch_new_start
= 1; /* For offsets->begin */
1718 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1720 &offsets
->pre_header_padding
,
1723 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1726 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1727 offsets
->size
> chan
->backend
.subbuf_size
)) {
1728 offsets
->switch_old_end
= 1; /* For offsets->old */
1729 offsets
->switch_new_start
= 1; /* For offsets->begin */
1732 if (caa_unlikely(offsets
->switch_new_start
)) {
1733 unsigned long sb_index
, commit_count
;
1736 * We are typically not filling the previous buffer completely.
1738 if (caa_likely(offsets
->switch_old_end
))
1739 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1740 offsets
->begin
= offsets
->begin
1741 + config
->cb
.subbuffer_header_size();
1742 /* Test new buffer integrity */
1743 sb_index
= subbuf_index(offsets
->begin
, chan
);
1745 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1746 * lib_ring_buffer_check_deliver() has the matching
1747 * memory barriers required around commit_cold cc_sb
1748 * updates to ensure reserve and commit counter updates
1749 * are not seen reordered when updated by another CPU.
1752 commit_count
= v_read(config
,
1753 &shmp_index(handle
, buf
->commit_cold
,
1755 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1757 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1759 * The reserve counter have been concurrently updated
1760 * while we read the commit counter. This means the
1761 * commit counter we read might not match buf->offset
1762 * due to concurrent update. We therefore need to retry.
1766 reserve_commit_diff
=
1767 (buf_trunc(offsets
->begin
, chan
)
1768 >> chan
->backend
.num_subbuf_order
)
1769 - (commit_count
& chan
->commit_count_mask
);
1770 if (caa_likely(reserve_commit_diff
== 0)) {
1771 /* Next subbuffer not being written to. */
1772 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1773 subbuf_trunc(offsets
->begin
, chan
)
1774 - subbuf_trunc((unsigned long)
1775 uatomic_read(&buf
->consumed
), chan
)
1776 >= chan
->backend
.buf_size
)) {
1777 unsigned long nr_lost
;
1780 * We do not overwrite non consumed buffers
1781 * and we are full : record is lost.
1783 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1784 v_inc(config
, &buf
->records_lost_full
);
1785 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1786 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1787 nr_lost
+ 1, chan
->backend
.name
,
1793 * Next subbuffer not being written to, and we
1794 * are either in overwrite mode or the buffer is
1795 * not full. It's safe to write in this new
1800 unsigned long nr_lost
;
1803 * Next subbuffer reserve offset does not match the
1804 * commit offset, and this did not involve update to the
1805 * reserve counter. Drop record in producer-consumer and
1806 * overwrite mode. Caused by either a writer OOPS or too
1807 * many nested writes over a reserve/commit pair.
1809 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1810 v_inc(config
, &buf
->records_lost_wrap
);
1811 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1812 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1813 nr_lost
+ 1, chan
->backend
.name
,
1819 config
->cb
.record_header_size(config
, chan
,
1821 &offsets
->pre_header_padding
,
1824 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1827 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1828 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1829 unsigned long nr_lost
;
1832 * Record too big for subbuffers, report error, don't
1833 * complete the sub-buffer switch.
1835 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1836 v_inc(config
, &buf
->records_lost_big
);
1837 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1838 DBG("%lu or more records lost in (%s:%d) record size "
1839 " of %zu bytes is too large for buffer\n",
1840 nr_lost
+ 1, chan
->backend
.name
,
1841 buf
->backend
.cpu
, offsets
->size
);
1846 * We just made a successful buffer switch and the
1847 * record fits in the new subbuffer. Let's write.
1852 * Record fits in the current buffer and we are not on a switch
1853 * boundary. It's safe to write.
1856 offsets
->end
= offsets
->begin
+ offsets
->size
;
1858 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1860 * The offset_end will fall at the very beginning of the next
1863 offsets
->switch_new_end
= 1; /* For offsets->begin */
1869 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1870 * @ctx: ring buffer context.
1872 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1873 * -EIO for other errors, else returns 0.
1874 * It will take care of sub-buffer switching.
1876 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1878 struct channel
*chan
= ctx
->chan
;
1879 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1880 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1881 struct lttng_ust_lib_ring_buffer
*buf
;
1882 struct switch_offsets offsets
;
1885 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1886 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1888 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1894 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1896 if (caa_unlikely(ret
))
1898 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1903 * Atomically update last_tsc. This update races against concurrent
1904 * atomic updates, but the race will always cause supplementary full TSC
1905 * records, never the opposite (missing a full TSC record when it would
1908 save_last_tsc(config
, buf
, ctx
->tsc
);
1911 * Push the reader if necessary
1913 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1916 * Clear noref flag for this subbuffer.
1918 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1919 subbuf_index(offsets
.end
- 1, chan
),
1923 * Switch old subbuffer if needed.
1925 if (caa_unlikely(offsets
.switch_old_end
)) {
1926 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1927 subbuf_index(offsets
.old
- 1, chan
),
1929 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1933 * Populate new subbuffer.
1935 if (caa_unlikely(offsets
.switch_new_start
))
1936 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1938 if (caa_unlikely(offsets
.switch_new_end
))
1939 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1941 ctx
->slot_size
= offsets
.size
;
1942 ctx
->pre_offset
= offsets
.begin
;
1943 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1948 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1950 void lttng_fixup_ringbuffer_tls(void)
1952 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
1955 void lib_ringbuffer_signal_init(void)
1961 * Block signal for entire process, so only our thread processes
1965 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
1968 PERROR("pthread_sigmask");