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) * 1000;
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) * 1000;
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 by SWITCH_FLUSH, which can be issued while tracing is
1390 * active or at buffer finalization (destroy).
1393 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1394 struct channel
*chan
,
1395 struct switch_offsets
*offsets
,
1397 struct lttng_ust_shm_handle
*handle
)
1399 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1400 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1401 unsigned long commit_count
;
1403 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1406 * Order all writes to buffer before the commit count update that will
1407 * determine that the subbuffer is full.
1410 v_add(config
, config
->cb
.subbuffer_header_size(),
1411 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1412 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1413 /* Check if the written buffer has to be delivered */
1414 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1415 commit_count
, oldidx
, handle
, tsc
);
1416 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1417 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1418 commit_count
, handle
);
1422 * lib_ring_buffer_switch_old_end: switch old subbuffer
1424 * Note : offset_old should never be 0 here. It is ok, because we never perform
1425 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1426 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1430 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1431 struct channel
*chan
,
1432 struct switch_offsets
*offsets
,
1434 struct lttng_ust_shm_handle
*handle
)
1436 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1437 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1438 unsigned long commit_count
, padding_size
, data_size
;
1440 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1441 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1442 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1446 * Order all writes to buffer before the commit count update that will
1447 * determine that the subbuffer is full.
1450 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1451 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1452 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1453 commit_count
, oldidx
, handle
, tsc
);
1454 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1455 offsets
->old
+ padding_size
, commit_count
, handle
);
1459 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1461 * This code can be executed unordered : writers may already have written to the
1462 * sub-buffer before this code gets executed, caution. The commit makes sure
1463 * that this code is executed before the deliver of this sub-buffer.
1466 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1467 struct channel
*chan
,
1468 struct switch_offsets
*offsets
,
1470 struct lttng_ust_shm_handle
*handle
)
1472 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1473 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1474 unsigned long commit_count
;
1476 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1479 * Order all writes to buffer before the commit count update that will
1480 * determine that the subbuffer is full.
1483 v_add(config
, config
->cb
.subbuffer_header_size(),
1484 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1485 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1486 /* Check if the written buffer has to be delivered */
1487 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1488 commit_count
, beginidx
, handle
, tsc
);
1489 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1490 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1491 commit_count
, handle
);
1495 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1497 * Calls subbuffer_set_data_size() to set the data size of the current
1498 * sub-buffer. We do not need to perform check_deliver nor commit here,
1499 * since this task will be done by the "commit" of the event for which
1500 * we are currently doing the space reservation.
1503 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1504 struct channel
*chan
,
1505 struct switch_offsets
*offsets
,
1507 struct lttng_ust_shm_handle
*handle
)
1509 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1510 unsigned long endidx
, data_size
;
1512 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1513 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1514 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1521 * !0 if execution must be aborted.
1524 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1525 struct lttng_ust_lib_ring_buffer
*buf
,
1526 struct channel
*chan
,
1527 struct switch_offsets
*offsets
,
1529 struct lttng_ust_shm_handle
*handle
)
1531 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1532 unsigned long off
, reserve_commit_diff
;
1534 offsets
->begin
= v_read(config
, &buf
->offset
);
1535 offsets
->old
= offsets
->begin
;
1536 offsets
->switch_old_start
= 0;
1537 off
= subbuf_offset(offsets
->begin
, chan
);
1539 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1542 * Ensure we flush the header of an empty subbuffer when doing the
1543 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1544 * total data gathering duration even if there were no records saved
1545 * after the last buffer switch.
1546 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1547 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1548 * subbuffer header as appropriate.
1549 * The next record that reserves space will be responsible for
1550 * populating the following subbuffer header. We choose not to populate
1551 * the next subbuffer header here because we want to be able to use
1552 * SWITCH_ACTIVE for periodical buffer flush, which must
1553 * guarantee that all the buffer content (records and header
1554 * timestamps) are visible to the reader. This is required for
1555 * quiescence guarantees for the fusion merge.
1557 if (mode
!= SWITCH_FLUSH
&& !off
)
1558 return -1; /* we do not have to switch : buffer is empty */
1560 if (caa_unlikely(off
== 0)) {
1561 unsigned long sb_index
, commit_count
;
1564 * We are performing a SWITCH_FLUSH. There may be concurrent
1565 * writes into the buffer if e.g. invoked while performing a
1566 * snapshot on an active trace.
1568 * If the client does not save any header information
1569 * (sub-buffer header size == 0), don't switch empty subbuffer
1570 * on finalize, because it is invalid to deliver a completely
1573 if (!config
->cb
.subbuffer_header_size())
1576 /* Test new buffer integrity */
1577 sb_index
= subbuf_index(offsets
->begin
, chan
);
1578 commit_count
= v_read(config
,
1579 &shmp_index(handle
, buf
->commit_cold
,
1581 reserve_commit_diff
=
1582 (buf_trunc(offsets
->begin
, chan
)
1583 >> chan
->backend
.num_subbuf_order
)
1584 - (commit_count
& chan
->commit_count_mask
);
1585 if (caa_likely(reserve_commit_diff
== 0)) {
1586 /* Next subbuffer not being written to. */
1587 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1588 subbuf_trunc(offsets
->begin
, chan
)
1589 - subbuf_trunc((unsigned long)
1590 uatomic_read(&buf
->consumed
), chan
)
1591 >= chan
->backend
.buf_size
)) {
1593 * We do not overwrite non consumed buffers
1594 * and we are full : don't switch.
1599 * Next subbuffer not being written to, and we
1600 * are either in overwrite mode or the buffer is
1601 * not full. It's safe to write in this new
1607 * Next subbuffer reserve offset does not match the
1608 * commit offset. Don't perform switch in
1609 * producer-consumer and overwrite mode. Caused by
1610 * either a writer OOPS or too many nested writes over a
1611 * reserve/commit pair.
1617 * Need to write the subbuffer start header on finalize.
1619 offsets
->switch_old_start
= 1;
1621 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1622 /* Note: old points to the next subbuf at offset 0 */
1623 offsets
->end
= offsets
->begin
;
1628 * Force a sub-buffer switch. This operation is completely reentrant : can be
1629 * called while tracing is active with absolutely no lock held.
1631 * Note, however, that as a v_cmpxchg is used for some atomic
1632 * operations, this function must be called from the CPU which owns the buffer
1633 * for a ACTIVE flush.
1635 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1636 struct lttng_ust_shm_handle
*handle
)
1638 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1639 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1640 struct switch_offsets offsets
;
1641 unsigned long oldidx
;
1647 * Perform retryable operations.
1650 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1652 return; /* Switch not needed */
1653 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1657 * Atomically update last_tsc. This update races against concurrent
1658 * atomic updates, but the race will always cause supplementary full TSC
1659 * records, never the opposite (missing a full TSC record when it would
1662 save_last_tsc(config
, buf
, tsc
);
1665 * Push the reader if necessary
1667 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1669 oldidx
= subbuf_index(offsets
.old
, chan
);
1670 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1673 * May need to populate header start on SWITCH_FLUSH.
1675 if (offsets
.switch_old_start
) {
1676 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1677 offsets
.old
+= config
->cb
.subbuffer_header_size();
1681 * Switch old subbuffer.
1683 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1689 * -ENOSPC if event size is too large for packet.
1690 * -ENOBUFS if there is currently not enough space in buffer for the event.
1691 * -EIO if data cannot be written into the buffer for any other reason.
1694 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1695 struct channel
*chan
,
1696 struct switch_offsets
*offsets
,
1697 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1699 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1700 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1701 unsigned long reserve_commit_diff
, offset_cmp
;
1704 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1705 offsets
->old
= offsets
->begin
;
1706 offsets
->switch_new_start
= 0;
1707 offsets
->switch_new_end
= 0;
1708 offsets
->switch_old_end
= 0;
1709 offsets
->pre_header_padding
= 0;
1711 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1712 if ((int64_t) ctx
->tsc
== -EIO
)
1715 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1716 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1718 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1719 offsets
->switch_new_start
= 1; /* For offsets->begin */
1721 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1723 &offsets
->pre_header_padding
,
1726 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1729 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1730 offsets
->size
> chan
->backend
.subbuf_size
)) {
1731 offsets
->switch_old_end
= 1; /* For offsets->old */
1732 offsets
->switch_new_start
= 1; /* For offsets->begin */
1735 if (caa_unlikely(offsets
->switch_new_start
)) {
1736 unsigned long sb_index
, commit_count
;
1739 * We are typically not filling the previous buffer completely.
1741 if (caa_likely(offsets
->switch_old_end
))
1742 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1743 offsets
->begin
= offsets
->begin
1744 + config
->cb
.subbuffer_header_size();
1745 /* Test new buffer integrity */
1746 sb_index
= subbuf_index(offsets
->begin
, chan
);
1748 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1749 * lib_ring_buffer_check_deliver() has the matching
1750 * memory barriers required around commit_cold cc_sb
1751 * updates to ensure reserve and commit counter updates
1752 * are not seen reordered when updated by another CPU.
1755 commit_count
= v_read(config
,
1756 &shmp_index(handle
, buf
->commit_cold
,
1758 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1760 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1762 * The reserve counter have been concurrently updated
1763 * while we read the commit counter. This means the
1764 * commit counter we read might not match buf->offset
1765 * due to concurrent update. We therefore need to retry.
1769 reserve_commit_diff
=
1770 (buf_trunc(offsets
->begin
, chan
)
1771 >> chan
->backend
.num_subbuf_order
)
1772 - (commit_count
& chan
->commit_count_mask
);
1773 if (caa_likely(reserve_commit_diff
== 0)) {
1774 /* Next subbuffer not being written to. */
1775 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1776 subbuf_trunc(offsets
->begin
, chan
)
1777 - subbuf_trunc((unsigned long)
1778 uatomic_read(&buf
->consumed
), chan
)
1779 >= chan
->backend
.buf_size
)) {
1780 unsigned long nr_lost
;
1783 * We do not overwrite non consumed buffers
1784 * and we are full : record is lost.
1786 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1787 v_inc(config
, &buf
->records_lost_full
);
1788 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1789 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1790 nr_lost
+ 1, chan
->backend
.name
,
1796 * Next subbuffer not being written to, and we
1797 * are either in overwrite mode or the buffer is
1798 * not full. It's safe to write in this new
1803 unsigned long nr_lost
;
1806 * Next subbuffer reserve offset does not match the
1807 * commit offset, and this did not involve update to the
1808 * reserve counter. Drop record in producer-consumer and
1809 * overwrite mode. Caused by either a writer OOPS or too
1810 * many nested writes over a reserve/commit pair.
1812 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1813 v_inc(config
, &buf
->records_lost_wrap
);
1814 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1815 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1816 nr_lost
+ 1, chan
->backend
.name
,
1822 config
->cb
.record_header_size(config
, chan
,
1824 &offsets
->pre_header_padding
,
1827 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1830 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1831 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1832 unsigned long nr_lost
;
1835 * Record too big for subbuffers, report error, don't
1836 * complete the sub-buffer switch.
1838 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1839 v_inc(config
, &buf
->records_lost_big
);
1840 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1841 DBG("%lu or more records lost in (%s:%d) record size "
1842 " of %zu bytes is too large for buffer\n",
1843 nr_lost
+ 1, chan
->backend
.name
,
1844 buf
->backend
.cpu
, offsets
->size
);
1849 * We just made a successful buffer switch and the
1850 * record fits in the new subbuffer. Let's write.
1855 * Record fits in the current buffer and we are not on a switch
1856 * boundary. It's safe to write.
1859 offsets
->end
= offsets
->begin
+ offsets
->size
;
1861 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1863 * The offset_end will fall at the very beginning of the next
1866 offsets
->switch_new_end
= 1; /* For offsets->begin */
1872 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1873 * @ctx: ring buffer context.
1875 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1876 * -EIO for other errors, else returns 0.
1877 * It will take care of sub-buffer switching.
1879 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1881 struct channel
*chan
= ctx
->chan
;
1882 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1883 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1884 struct lttng_ust_lib_ring_buffer
*buf
;
1885 struct switch_offsets offsets
;
1888 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1889 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1891 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1897 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1899 if (caa_unlikely(ret
))
1901 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1906 * Atomically update last_tsc. This update races against concurrent
1907 * atomic updates, but the race will always cause supplementary full TSC
1908 * records, never the opposite (missing a full TSC record when it would
1911 save_last_tsc(config
, buf
, ctx
->tsc
);
1914 * Push the reader if necessary
1916 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1919 * Clear noref flag for this subbuffer.
1921 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1922 subbuf_index(offsets
.end
- 1, chan
),
1926 * Switch old subbuffer if needed.
1928 if (caa_unlikely(offsets
.switch_old_end
)) {
1929 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1930 subbuf_index(offsets
.old
- 1, chan
),
1932 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1936 * Populate new subbuffer.
1938 if (caa_unlikely(offsets
.switch_new_start
))
1939 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1941 if (caa_unlikely(offsets
.switch_new_end
))
1942 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1944 ctx
->slot_size
= offsets
.size
;
1945 ctx
->pre_offset
= offsets
.begin
;
1946 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1951 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1953 void lttng_fixup_ringbuffer_tls(void)
1955 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
1958 void lib_ringbuffer_signal_init(void)
1964 * Block signal for entire process, so only our thread processes
1968 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
1971 PERROR("pthread_sigmask");