2 * ring_buffer_frontend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
41 * Bob Wisniewski <bob@watson.ibm.com>
43 * Buffer reader semantic :
46 * while buffer is not finalized and empty
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
55 #include <sys/types.h>
62 #include <urcu/compiler.h>
64 #include <urcu/tls-compat.h>
68 #include <lttng/ringbuffer-config.h>
74 #include "../liblttng-ust/compat.h" /* For ENODATA */
77 #define max(a, b) ((a) > (b) ? (a) : (b))
80 /* Print DBG() messages about events lost only every 1048576 hits */
81 #define DBG_PRINT_NR_LOST (1UL << 20)
83 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
84 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
85 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
86 #define CLOCKID CLOCK_MONOTONIC
89 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
90 * close(2) to close the fd returned by shm_open.
91 * shm_unlink releases the shared memory object name.
92 * ftruncate(2) sets the size of the memory object.
93 * mmap/munmap maps the shared memory obj to a virtual address in the
94 * calling proceess (should be done both in libust and consumer).
95 * See shm_overview(7) for details.
96 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
99 * Since we don't need to access the object using its name, we can
100 * immediately shm_unlink(3) it, and only keep the handle with its file
105 * Internal structure representing offsets to use at a sub-buffer switch.
107 struct switch_offsets
{
108 unsigned long begin
, end
, old
;
109 size_t pre_header_padding
, size
;
110 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
114 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
117 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
120 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
123 void lib_ring_buffer_print_errors(struct channel
*chan
,
124 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
125 struct lttng_ust_shm_handle
*handle
);
128 * Handle timer teardown race wrt memory free of private data by
129 * ring buffer signals are handled by a single thread, which permits
130 * a synchronization point between handling of each signal.
131 * Protected by the ust mutex.
133 struct timer_signal_data
{
134 pthread_t tid
; /* thread id managing signals */
139 static struct timer_signal_data timer_signal
;
142 * lib_ring_buffer_reset - Reset ring buffer to initial values.
145 * Effectively empty the ring buffer. Should be called when the buffer is not
146 * used for writing. The ring buffer can be opened for reading, but the reader
147 * should not be using the iterator concurrently with reset. The previous
148 * current iterator record is reset.
150 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
151 struct lttng_ust_shm_handle
*handle
)
153 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
154 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
158 * Reset iterator first. It will put the subbuffer if it currently holds
161 v_set(config
, &buf
->offset
, 0);
162 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
163 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
164 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
165 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
167 uatomic_set(&buf
->consumed
, 0);
168 uatomic_set(&buf
->record_disabled
, 0);
169 v_set(config
, &buf
->last_tsc
, 0);
170 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
171 /* Don't reset number of active readers */
172 v_set(config
, &buf
->records_lost_full
, 0);
173 v_set(config
, &buf
->records_lost_wrap
, 0);
174 v_set(config
, &buf
->records_lost_big
, 0);
175 v_set(config
, &buf
->records_count
, 0);
176 v_set(config
, &buf
->records_overrun
, 0);
181 * channel_reset - Reset channel to initial values.
184 * Effectively empty the channel. Should be called when the channel is not used
185 * for writing. The channel can be opened for reading, but the reader should not
186 * be using the iterator concurrently with reset. The previous current iterator
189 void channel_reset(struct channel
*chan
)
192 * Reset iterators first. Will put the subbuffer if held for reading.
194 uatomic_set(&chan
->record_disabled
, 0);
195 /* Don't reset commit_count_mask, still valid */
196 channel_backend_reset(&chan
->backend
);
197 /* Don't reset switch/read timer interval */
198 /* Don't reset notifiers and notifier enable bits */
199 /* Don't reset reader reference count */
203 * Must be called under cpu hotplug protection.
205 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
206 struct channel_backend
*chanb
, int cpu
,
207 struct lttng_ust_shm_handle
*handle
,
208 struct shm_object
*shmobj
)
210 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
211 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
212 void *priv
= channel_get_private(chan
);
213 size_t subbuf_header_size
;
217 /* Test for cpu hotplug */
218 if (buf
->backend
.allocated
)
221 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
222 cpu
, handle
, shmobj
);
226 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
227 set_shmp(buf
->commit_hot
,
229 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
230 if (!shmp(handle
, buf
->commit_hot
)) {
235 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
236 set_shmp(buf
->commit_cold
,
238 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
239 if (!shmp(handle
, buf
->commit_cold
)) {
245 * Write the subbuffer header for first subbuffer so we know the total
246 * duration of data gathering.
248 subbuf_header_size
= config
->cb
.subbuffer_header_size();
249 v_set(config
, &buf
->offset
, subbuf_header_size
);
250 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
251 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
252 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
253 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
255 if (config
->cb
.buffer_create
) {
256 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
260 buf
->backend
.allocated
= 1;
265 /* commit_cold will be freed by shm teardown */
267 /* commit_hot will be freed by shm teardown */
273 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
275 const struct lttng_ust_lib_ring_buffer_config
*config
;
276 struct lttng_ust_shm_handle
*handle
;
277 struct channel
*chan
;
280 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
282 chan
= si
->si_value
.sival_ptr
;
283 handle
= chan
->handle
;
284 config
= &chan
->backend
.config
;
286 DBG("Switch timer for channel %p\n", chan
);
289 * Only flush buffers periodically if readers are active.
291 pthread_mutex_lock(&wakeup_fd_mutex
);
292 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
293 for_each_possible_cpu(cpu
) {
294 struct lttng_ust_lib_ring_buffer
*buf
=
295 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
296 if (uatomic_read(&buf
->active_readers
))
297 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
301 struct lttng_ust_lib_ring_buffer
*buf
=
302 shmp(handle
, chan
->backend
.buf
[0].shmp
);
304 if (uatomic_read(&buf
->active_readers
))
305 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
308 pthread_mutex_unlock(&wakeup_fd_mutex
);
313 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
315 const struct lttng_ust_lib_ring_buffer_config
*config
;
316 struct lttng_ust_shm_handle
*handle
;
319 handle
= chan
->handle
;
320 config
= &chan
->backend
.config
;
323 * Only flush buffers periodically if readers are active.
325 pthread_mutex_lock(&wakeup_fd_mutex
);
326 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
327 for_each_possible_cpu(cpu
) {
328 struct lttng_ust_lib_ring_buffer
*buf
=
329 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
331 if (uatomic_read(&buf
->active_readers
)
332 && lib_ring_buffer_poll_deliver(config
, buf
,
334 lib_ring_buffer_wakeup(buf
, handle
);
338 struct lttng_ust_lib_ring_buffer
*buf
=
339 shmp(handle
, chan
->backend
.buf
[0].shmp
);
341 if (uatomic_read(&buf
->active_readers
)
342 && lib_ring_buffer_poll_deliver(config
, buf
,
344 lib_ring_buffer_wakeup(buf
, handle
);
347 pthread_mutex_unlock(&wakeup_fd_mutex
);
351 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
353 struct channel
*chan
;
355 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
356 chan
= si
->si_value
.sival_ptr
;
357 DBG("Read timer for channel %p\n", chan
);
358 lib_ring_buffer_channel_do_read(chan
);
363 void rb_setmask(sigset_t
*mask
)
367 ret
= sigemptyset(mask
);
369 PERROR("sigemptyset");
371 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
375 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
379 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
386 void *sig_thread(void *arg
)
392 /* Only self thread will receive signal mask. */
394 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
397 signr
= sigwaitinfo(&mask
, &info
);
400 PERROR("sigwaitinfo");
403 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
404 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
406 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
407 lib_ring_buffer_channel_read_timer(info
.si_signo
,
409 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
411 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
414 ERR("Unexptected signal %d\n", info
.si_signo
);
421 * Called with ust_lock() held.
422 * Ensure only a single thread listens on the timer signal.
425 void lib_ring_buffer_setup_timer_thread(void)
430 if (timer_signal
.setup_done
)
433 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
436 PERROR("pthread_create");
438 ret
= pthread_detach(thread
);
441 PERROR("pthread_detach");
443 timer_signal
.setup_done
= 1;
447 * Called with ust_lock() held.
450 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
453 struct itimerspec its
;
456 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
459 chan
->switch_timer_enabled
= 1;
461 lib_ring_buffer_setup_timer_thread();
463 sev
.sigev_notify
= SIGEV_SIGNAL
;
464 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
465 sev
.sigev_value
.sival_ptr
= chan
;
466 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
468 PERROR("timer_create");
471 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
472 its
.it_value
.tv_nsec
= chan
->switch_timer_interval
% 1000000;
473 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
474 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
476 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
478 PERROR("timer_settime");
483 * Called with ust_lock() held.
486 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
488 sigset_t pending_set
;
491 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
494 ret
= timer_delete(chan
->switch_timer
);
496 PERROR("timer_delete");
500 * Ensure we don't have any signal queued for this channel.
503 ret
= sigemptyset(&pending_set
);
505 PERROR("sigemptyset");
507 ret
= sigpending(&pending_set
);
509 PERROR("sigpending");
511 if (!sigismember(&pending_set
, LTTNG_UST_RB_SIG_FLUSH
))
517 * From this point, no new signal handler will be fired that
518 * would try to access "chan". However, we still need to wait
519 * for any currently executing handler to complete.
522 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
526 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
529 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
531 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
535 chan
->switch_timer
= 0;
536 chan
->switch_timer_enabled
= 0;
540 * Called with ust_lock() held.
543 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
545 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
547 struct itimerspec its
;
550 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
551 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
554 chan
->read_timer_enabled
= 1;
556 lib_ring_buffer_setup_timer_thread();
558 sev
.sigev_notify
= SIGEV_SIGNAL
;
559 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
560 sev
.sigev_value
.sival_ptr
= chan
;
561 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
563 PERROR("timer_create");
566 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
567 its
.it_value
.tv_nsec
= chan
->read_timer_interval
% 1000000;
568 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
569 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
571 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
573 PERROR("timer_settime");
578 * Called with ust_lock() held.
581 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
583 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
584 sigset_t pending_set
;
587 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
588 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
591 ret
= timer_delete(chan
->read_timer
);
593 PERROR("timer_delete");
597 * do one more check to catch data that has been written in the last
600 lib_ring_buffer_channel_do_read(chan
);
604 * Ensure we don't have any signal queued for this channel.
607 ret
= sigemptyset(&pending_set
);
609 PERROR("sigemptyset");
611 ret
= sigpending(&pending_set
);
613 PERROR("sigpending");
615 if (!sigismember(&pending_set
, LTTNG_UST_RB_SIG_READ
))
621 * From this point, no new signal handler will be fired that
622 * would try to access "chan". However, we still need to wait
623 * for any currently executing handler to complete.
626 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
630 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
633 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
635 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
639 chan
->read_timer
= 0;
640 chan
->read_timer_enabled
= 0;
643 static void channel_unregister_notifiers(struct channel
*chan
,
644 struct lttng_ust_shm_handle
*handle
)
646 lib_ring_buffer_channel_switch_timer_stop(chan
);
647 lib_ring_buffer_channel_read_timer_stop(chan
);
650 static void channel_print_errors(struct channel
*chan
,
651 struct lttng_ust_shm_handle
*handle
)
653 const struct lttng_ust_lib_ring_buffer_config
*config
=
654 &chan
->backend
.config
;
657 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
658 for_each_possible_cpu(cpu
) {
659 struct lttng_ust_lib_ring_buffer
*buf
=
660 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
661 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
664 struct lttng_ust_lib_ring_buffer
*buf
=
665 shmp(handle
, chan
->backend
.buf
[0].shmp
);
667 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
671 static void channel_free(struct channel
*chan
,
672 struct lttng_ust_shm_handle
*handle
)
674 channel_backend_free(&chan
->backend
, handle
);
675 /* chan is freed by shm teardown */
676 shm_object_table_destroy(handle
->table
);
681 * channel_create - Create channel.
682 * @config: ring buffer instance configuration
683 * @name: name of the channel
684 * @priv_data: ring buffer client private data area pointer (output)
685 * @priv_data_size: length, in bytes, of the private data area.
686 * @priv_data_init: initialization data for private data.
687 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
688 * address mapping. It is used only by RING_BUFFER_STATIC
689 * configuration. It can be set to NULL for other backends.
690 * @subbuf_size: subbuffer size
691 * @num_subbuf: number of subbuffers
692 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
693 * padding to let readers get those sub-buffers.
694 * Used for live streaming.
695 * @read_timer_interval: Time interval (in us) to wake up pending readers.
698 * Returns NULL on failure.
700 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
703 size_t priv_data_align
,
704 size_t priv_data_size
,
705 void *priv_data_init
,
706 void *buf_addr
, size_t subbuf_size
,
707 size_t num_subbuf
, unsigned int switch_timer_interval
,
708 unsigned int read_timer_interval
)
711 size_t shmsize
, chansize
;
712 struct channel
*chan
;
713 struct lttng_ust_shm_handle
*handle
;
714 struct shm_object
*shmobj
;
715 unsigned int nr_streams
;
717 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
718 nr_streams
= num_possible_cpus();
722 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
723 read_timer_interval
))
726 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
730 /* Allocate table for channel + per-cpu buffers */
731 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
733 goto error_table_alloc
;
735 /* Calculate the shm allocation layout */
736 shmsize
= sizeof(struct channel
);
737 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
738 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
741 shmsize
+= offset_align(shmsize
, priv_data_align
);
742 shmsize
+= priv_data_size
;
744 /* Allocate normal memory for channel (not shared) */
745 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
);
748 /* struct channel is at object 0, offset 0 (hardcoded) */
749 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
750 assert(handle
->chan
._ref
.index
== 0);
751 assert(handle
->chan
._ref
.offset
== 0);
752 chan
= shmp(handle
, handle
->chan
);
755 chan
->nr_streams
= nr_streams
;
757 /* space for private data */
758 if (priv_data_size
) {
759 DECLARE_SHMP(void, priv_data_alloc
);
761 align_shm(shmobj
, priv_data_align
);
762 chan
->priv_data_offset
= shmobj
->allocated_len
;
763 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
764 if (!shmp(handle
, priv_data_alloc
))
766 *priv_data
= channel_get_private(chan
);
767 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
769 chan
->priv_data_offset
= -1;
774 ret
= channel_backend_init(&chan
->backend
, name
, config
,
775 subbuf_size
, num_subbuf
, handle
);
777 goto error_backend_init
;
779 chan
->handle
= handle
;
780 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
782 chan
->switch_timer_interval
= switch_timer_interval
;
783 chan
->read_timer_interval
= read_timer_interval
;
784 lib_ring_buffer_channel_switch_timer_start(chan
);
785 lib_ring_buffer_channel_read_timer_start(chan
);
791 shm_object_table_destroy(handle
->table
);
797 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
798 uint64_t memory_map_size
,
801 struct lttng_ust_shm_handle
*handle
;
802 struct shm_object
*object
;
804 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
808 /* Allocate table for channel + per-cpu buffers */
809 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
811 goto error_table_alloc
;
812 /* Add channel object */
813 object
= shm_object_table_append_mem(handle
->table
, data
,
814 memory_map_size
, wakeup_fd
);
816 goto error_table_object
;
817 /* struct channel is at object 0, offset 0 (hardcoded) */
818 handle
->chan
._ref
.index
= 0;
819 handle
->chan
._ref
.offset
= 0;
823 shm_object_table_destroy(handle
->table
);
829 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
830 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
831 uint64_t memory_map_size
)
833 struct shm_object
*object
;
835 /* Add stream object */
836 object
= shm_object_table_append_shm(handle
->table
,
837 shm_fd
, wakeup_fd
, stream_nr
,
844 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
846 assert(handle
->table
);
847 return handle
->table
->allocated_len
- 1;
851 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
853 channel_free(chan
, handle
);
857 * channel_destroy - Finalize, wait for q.s. and destroy channel.
858 * @chan: channel to destroy
861 * Call "destroy" callback, finalize channels, decrement the channel
862 * reference count. Note that when readers have completed data
863 * consumption of finalized channels, get_subbuf() will return -ENODATA.
864 * They should release their handle at that point.
866 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
871 * Note: the consumer takes care of finalizing and
872 * switching the buffers.
874 channel_unregister_notifiers(chan
, handle
);
876 * The consumer prints errors.
878 channel_print_errors(chan
, handle
);
882 * sessiond/consumer are keeping a reference on the shm file
883 * descriptor directly. No need to refcount.
885 channel_release(chan
, handle
);
889 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
890 const struct lttng_ust_lib_ring_buffer_config
*config
,
891 struct channel
*chan
, int cpu
,
892 struct lttng_ust_shm_handle
*handle
,
893 int *shm_fd
, int *wait_fd
,
895 uint64_t *memory_map_size
)
899 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
902 if (cpu
>= num_possible_cpus())
905 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
906 *shm_fd
= shm_get_shm_fd(handle
, ref
);
907 *wait_fd
= shm_get_wait_fd(handle
, ref
);
908 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
909 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
911 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
914 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
915 struct channel
*chan
,
916 struct lttng_ust_shm_handle
*handle
)
920 ref
= &handle
->chan
._ref
;
921 return shm_close_wait_fd(handle
, ref
);
924 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
925 struct channel
*chan
,
926 struct lttng_ust_shm_handle
*handle
)
930 ref
= &handle
->chan
._ref
;
931 return shm_close_wakeup_fd(handle
, ref
);
934 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
935 struct channel
*chan
,
936 struct lttng_ust_shm_handle
*handle
,
941 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
944 if (cpu
>= num_possible_cpus())
947 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
948 return shm_close_wait_fd(handle
, ref
);
951 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
952 struct channel
*chan
,
953 struct lttng_ust_shm_handle
*handle
,
959 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
962 if (cpu
>= num_possible_cpus())
965 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
966 pthread_mutex_lock(&wakeup_fd_mutex
);
967 ret
= shm_close_wakeup_fd(handle
, ref
);
968 pthread_mutex_unlock(&wakeup_fd_mutex
);
972 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
973 struct lttng_ust_shm_handle
*handle
)
975 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
981 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
982 struct lttng_ust_shm_handle
*handle
)
984 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
986 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
988 uatomic_dec(&buf
->active_readers
);
992 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
994 * @consumed: consumed count indicating the position where to read
995 * @produced: produced count, indicates position when to stop reading
997 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
998 * data to read at consumed position, or 0 if the get operation succeeds.
1001 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1002 unsigned long *consumed
, unsigned long *produced
,
1003 struct lttng_ust_shm_handle
*handle
)
1005 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1006 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1007 unsigned long consumed_cur
, write_offset
;
1010 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1012 * Read finalized before counters.
1015 consumed_cur
= uatomic_read(&buf
->consumed
);
1017 * No need to issue a memory barrier between consumed count read and
1018 * write offset read, because consumed count can only change
1019 * concurrently in overwrite mode, and we keep a sequence counter
1020 * identifier derived from the write offset to check we are getting
1021 * the same sub-buffer we are expecting (the sub-buffers are atomically
1022 * "tagged" upon writes, tags are checked upon read).
1024 write_offset
= v_read(config
, &buf
->offset
);
1027 * Check that we are not about to read the same subbuffer in
1028 * which the writer head is.
1030 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1034 *consumed
= consumed_cur
;
1035 *produced
= subbuf_trunc(write_offset
, chan
);
1041 * The memory barriers __wait_event()/wake_up_interruptible() take care
1042 * of "raw_spin_is_locked" memory ordering.
1051 * lib_ring_buffer_put_snapshot - move consumed counter forward
1053 * @consumed_new: new consumed count value
1055 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1056 unsigned long consumed_new
,
1057 struct lttng_ust_shm_handle
*handle
)
1059 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1060 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1061 unsigned long consumed
;
1063 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1066 * Only push the consumed value forward.
1067 * If the consumed cmpxchg fails, this is because we have been pushed by
1068 * the writer in flight recorder mode.
1070 consumed
= uatomic_read(&buf
->consumed
);
1071 while ((long) consumed
- (long) consumed_new
< 0)
1072 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1077 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1079 * @consumed: consumed count indicating the position where to read
1081 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1082 * data to read at consumed position, or 0 if the get operation succeeds.
1084 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1085 unsigned long consumed
,
1086 struct lttng_ust_shm_handle
*handle
)
1088 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1089 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1090 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1095 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1097 * Read finalized before counters.
1100 consumed_cur
= uatomic_read(&buf
->consumed
);
1101 consumed_idx
= subbuf_index(consumed
, chan
);
1102 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
1104 * Make sure we read the commit count before reading the buffer
1105 * data and the write offset. Correct consumed offset ordering
1106 * wrt commit count is insured by the use of cmpxchg to update
1107 * the consumed offset.
1110 * Local rmb to match the remote wmb to read the commit count
1111 * before the buffer data and the write offset.
1115 write_offset
= v_read(config
, &buf
->offset
);
1118 * Check that the buffer we are getting is after or at consumed_cur
1121 if ((long) subbuf_trunc(consumed
, chan
)
1122 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1126 * Check that the subbuffer we are trying to consume has been
1127 * already fully committed.
1129 if (((commit_count
- chan
->backend
.subbuf_size
)
1130 & chan
->commit_count_mask
)
1131 - (buf_trunc(consumed_cur
, chan
)
1132 >> chan
->backend
.num_subbuf_order
)
1137 * Check that we are not about to read the same subbuffer in
1138 * which the writer head is.
1140 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1145 * Failure to get the subbuffer causes a busy-loop retry without going
1146 * to a wait queue. These are caused by short-lived race windows where
1147 * the writer is getting access to a subbuffer we were trying to get
1148 * access to. Also checks that the "consumed" buffer count we are
1149 * looking for matches the one contained in the subbuffer id.
1151 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1152 consumed_idx
, buf_trunc_val(consumed
, chan
),
1156 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1158 buf
->get_subbuf_consumed
= consumed
;
1159 buf
->get_subbuf
= 1;
1165 * The memory barriers __wait_event()/wake_up_interruptible() take care
1166 * of "raw_spin_is_locked" memory ordering.
1175 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1178 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1179 struct lttng_ust_shm_handle
*handle
)
1181 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1182 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1183 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1184 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1186 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1188 if (!buf
->get_subbuf
) {
1190 * Reader puts a subbuffer it did not get.
1192 CHAN_WARN_ON(chan
, 1);
1195 consumed
= buf
->get_subbuf_consumed
;
1196 buf
->get_subbuf
= 0;
1199 * Clear the records_unread counter. (overruns counter)
1200 * Can still be non-zero if a file reader simply grabbed the data
1201 * without using iterators.
1202 * Can be below zero if an iterator is used on a snapshot more than
1205 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1206 v_add(config
, v_read(config
,
1207 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
1208 &bufb
->records_read
);
1209 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
1210 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1211 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1212 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1215 * Exchange the reader subbuffer with the one we put in its place in the
1216 * writer subbuffer table. Expect the original consumed count. If
1217 * update_read_sb_index fails, this is because the writer updated the
1218 * subbuffer concurrently. We should therefore keep the subbuffer we
1219 * currently have: it has become invalid to try reading this sub-buffer
1220 * consumed count value anyway.
1222 consumed_idx
= subbuf_index(consumed
, chan
);
1223 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1224 consumed_idx
, buf_trunc_val(consumed
, chan
),
1227 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1228 * if the writer concurrently updated it.
1233 * cons_offset is an iterator on all subbuffer offsets between the reader
1234 * position and the writer position. (inclusive)
1237 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1238 struct channel
*chan
,
1239 unsigned long cons_offset
,
1241 struct lttng_ust_shm_handle
*handle
)
1243 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1244 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1246 cons_idx
= subbuf_index(cons_offset
, chan
);
1247 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
1248 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
1250 if (subbuf_offset(commit_count
, chan
) != 0)
1251 DBG("ring buffer %s, cpu %d: "
1252 "commit count in subbuffer %lu,\n"
1253 "expecting multiples of %lu bytes\n"
1254 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1255 chan
->backend
.name
, cpu
, cons_idx
,
1256 chan
->backend
.subbuf_size
,
1257 commit_count
, commit_count_sb
);
1259 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1260 chan
->backend
.name
, cpu
, commit_count
);
1264 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1265 struct channel
*chan
,
1266 void *priv
, int cpu
,
1267 struct lttng_ust_shm_handle
*handle
)
1269 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1270 unsigned long write_offset
, cons_offset
;
1273 * No need to order commit_count, write_offset and cons_offset reads
1274 * because we execute at teardown when no more writer nor reader
1275 * references are left.
1277 write_offset
= v_read(config
, &buf
->offset
);
1278 cons_offset
= uatomic_read(&buf
->consumed
);
1279 if (write_offset
!= cons_offset
)
1280 DBG("ring buffer %s, cpu %d: "
1281 "non-consumed data\n"
1282 " [ %lu bytes written, %lu bytes read ]\n",
1283 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1285 for (cons_offset
= uatomic_read(&buf
->consumed
);
1286 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1289 cons_offset
= subbuf_align(cons_offset
, chan
))
1290 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1295 void lib_ring_buffer_print_errors(struct channel
*chan
,
1296 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1297 struct lttng_ust_shm_handle
*handle
)
1299 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1300 void *priv
= channel_get_private(chan
);
1302 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1303 DBG("ring buffer %s: %lu records written, "
1304 "%lu records overrun\n",
1306 v_read(config
, &buf
->records_count
),
1307 v_read(config
, &buf
->records_overrun
));
1309 DBG("ring buffer %s, cpu %d: %lu records written, "
1310 "%lu records overrun\n",
1311 chan
->backend
.name
, cpu
,
1312 v_read(config
, &buf
->records_count
),
1313 v_read(config
, &buf
->records_overrun
));
1315 if (v_read(config
, &buf
->records_lost_full
)
1316 || v_read(config
, &buf
->records_lost_wrap
)
1317 || v_read(config
, &buf
->records_lost_big
))
1318 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1319 " [ %lu buffer full, %lu nest buffer wrap-around, "
1320 "%lu event too big ]\n",
1321 chan
->backend
.name
, cpu
,
1322 v_read(config
, &buf
->records_lost_full
),
1323 v_read(config
, &buf
->records_lost_wrap
),
1324 v_read(config
, &buf
->records_lost_big
));
1326 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1330 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1332 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1335 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1336 struct channel
*chan
,
1337 struct switch_offsets
*offsets
,
1339 struct lttng_ust_shm_handle
*handle
)
1341 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1342 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1343 unsigned long commit_count
;
1345 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1348 * Order all writes to buffer before the commit count update that will
1349 * determine that the subbuffer is full.
1352 v_add(config
, config
->cb
.subbuffer_header_size(),
1353 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1354 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1355 /* Check if the written buffer has to be delivered */
1356 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1357 commit_count
, oldidx
, handle
);
1358 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1359 offsets
->old
, commit_count
,
1360 config
->cb
.subbuffer_header_size(),
1365 * lib_ring_buffer_switch_old_end: switch old subbuffer
1367 * Note : offset_old should never be 0 here. It is ok, because we never perform
1368 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1369 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1373 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1374 struct channel
*chan
,
1375 struct switch_offsets
*offsets
,
1377 struct lttng_ust_shm_handle
*handle
)
1379 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1380 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1381 unsigned long commit_count
, padding_size
, data_size
;
1383 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1384 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1385 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1389 * Order all writes to buffer before the commit count update that will
1390 * determine that the subbuffer is full.
1393 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1394 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1395 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1396 commit_count
, oldidx
, handle
);
1397 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1398 offsets
->old
, commit_count
,
1399 padding_size
, handle
);
1403 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1405 * This code can be executed unordered : writers may already have written to the
1406 * sub-buffer before this code gets executed, caution. The commit makes sure
1407 * that this code is executed before the deliver of this sub-buffer.
1410 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1411 struct channel
*chan
,
1412 struct switch_offsets
*offsets
,
1414 struct lttng_ust_shm_handle
*handle
)
1416 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1417 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1418 unsigned long commit_count
;
1420 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1423 * Order all writes to buffer before the commit count update that will
1424 * determine that the subbuffer is full.
1427 v_add(config
, config
->cb
.subbuffer_header_size(),
1428 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1429 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1430 /* Check if the written buffer has to be delivered */
1431 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1432 commit_count
, beginidx
, handle
);
1433 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1434 offsets
->begin
, commit_count
,
1435 config
->cb
.subbuffer_header_size(),
1440 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1442 * The only remaining threads could be the ones with pending commits. They will
1443 * have to do the deliver themselves.
1446 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1447 struct channel
*chan
,
1448 struct switch_offsets
*offsets
,
1450 struct lttng_ust_shm_handle
*handle
)
1452 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1453 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1454 unsigned long commit_count
, padding_size
, data_size
;
1456 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1457 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1458 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1462 * Order all writes to buffer before the commit count update that will
1463 * determine that the subbuffer is full.
1466 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1467 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1468 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1469 commit_count
, endidx
, handle
);
1470 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1471 offsets
->end
, commit_count
,
1472 padding_size
, handle
);
1478 * !0 if execution must be aborted.
1481 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1482 struct lttng_ust_lib_ring_buffer
*buf
,
1483 struct channel
*chan
,
1484 struct switch_offsets
*offsets
,
1487 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1490 offsets
->begin
= v_read(config
, &buf
->offset
);
1491 offsets
->old
= offsets
->begin
;
1492 offsets
->switch_old_start
= 0;
1493 off
= subbuf_offset(offsets
->begin
, chan
);
1495 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1498 * Ensure we flush the header of an empty subbuffer when doing the
1499 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1500 * total data gathering duration even if there were no records saved
1501 * after the last buffer switch.
1502 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1503 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1504 * subbuffer header as appropriate.
1505 * The next record that reserves space will be responsible for
1506 * populating the following subbuffer header. We choose not to populate
1507 * the next subbuffer header here because we want to be able to use
1508 * SWITCH_ACTIVE for periodical buffer flush, which must
1509 * guarantee that all the buffer content (records and header
1510 * timestamps) are visible to the reader. This is required for
1511 * quiescence guarantees for the fusion merge.
1513 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1514 if (caa_unlikely(off
== 0)) {
1516 * A final flush that encounters an empty
1517 * sub-buffer cannot switch buffer if a
1518 * reader is located within this sub-buffer.
1519 * Anyway, the purpose of final flushing of a
1520 * sub-buffer at offset 0 is to handle the case
1521 * of entirely empty stream.
1523 if (caa_unlikely(subbuf_trunc(offsets
->begin
, chan
)
1524 - subbuf_trunc((unsigned long)
1525 uatomic_read(&buf
->consumed
), chan
)
1526 >= chan
->backend
.buf_size
))
1529 * The client does not save any header information.
1530 * Don't switch empty subbuffer on finalize, because it
1531 * is invalid to deliver a completely empty subbuffer.
1533 if (!config
->cb
.subbuffer_header_size())
1536 * Need to write the subbuffer start header on finalize.
1538 offsets
->switch_old_start
= 1;
1540 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1542 return -1; /* we do not have to switch : buffer is empty */
1543 /* Note: old points to the next subbuf at offset 0 */
1544 offsets
->end
= offsets
->begin
;
1549 * Force a sub-buffer switch. This operation is completely reentrant : can be
1550 * called while tracing is active with absolutely no lock held.
1552 * Note, however, that as a v_cmpxchg is used for some atomic
1553 * operations, this function must be called from the CPU which owns the buffer
1554 * for a ACTIVE flush.
1556 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1557 struct lttng_ust_shm_handle
*handle
)
1559 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1560 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1561 struct switch_offsets offsets
;
1562 unsigned long oldidx
;
1568 * Perform retryable operations.
1571 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1573 return; /* Switch not needed */
1574 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1578 * Atomically update last_tsc. This update races against concurrent
1579 * atomic updates, but the race will always cause supplementary full TSC
1580 * records, never the opposite (missing a full TSC record when it would
1583 save_last_tsc(config
, buf
, tsc
);
1586 * Push the reader if necessary
1588 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1590 oldidx
= subbuf_index(offsets
.old
, chan
);
1591 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1594 * May need to populate header start on SWITCH_FLUSH.
1596 if (offsets
.switch_old_start
) {
1597 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1598 offsets
.old
+= config
->cb
.subbuffer_header_size();
1602 * Switch old subbuffer.
1604 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1610 * -ENOSPC if event size is too large for packet.
1611 * -ENOBUFS if there is currently not enough space in buffer for the event.
1612 * -EIO if data cannot be written into the buffer for any other reason.
1615 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1616 struct channel
*chan
,
1617 struct switch_offsets
*offsets
,
1618 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1620 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1621 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1622 unsigned long reserve_commit_diff
;
1624 offsets
->begin
= v_read(config
, &buf
->offset
);
1625 offsets
->old
= offsets
->begin
;
1626 offsets
->switch_new_start
= 0;
1627 offsets
->switch_new_end
= 0;
1628 offsets
->switch_old_end
= 0;
1629 offsets
->pre_header_padding
= 0;
1631 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1632 if ((int64_t) ctx
->tsc
== -EIO
)
1635 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1636 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1638 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1639 offsets
->switch_new_start
= 1; /* For offsets->begin */
1641 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1643 &offsets
->pre_header_padding
,
1646 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1649 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1650 offsets
->size
> chan
->backend
.subbuf_size
)) {
1651 offsets
->switch_old_end
= 1; /* For offsets->old */
1652 offsets
->switch_new_start
= 1; /* For offsets->begin */
1655 if (caa_unlikely(offsets
->switch_new_start
)) {
1656 unsigned long sb_index
;
1659 * We are typically not filling the previous buffer completely.
1661 if (caa_likely(offsets
->switch_old_end
))
1662 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1663 offsets
->begin
= offsets
->begin
1664 + config
->cb
.subbuffer_header_size();
1665 /* Test new buffer integrity */
1666 sb_index
= subbuf_index(offsets
->begin
, chan
);
1667 reserve_commit_diff
=
1668 (buf_trunc(offsets
->begin
, chan
)
1669 >> chan
->backend
.num_subbuf_order
)
1670 - ((unsigned long) v_read(config
,
1671 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1672 & chan
->commit_count_mask
);
1673 if (caa_likely(reserve_commit_diff
== 0)) {
1674 /* Next subbuffer not being written to. */
1675 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1676 subbuf_trunc(offsets
->begin
, chan
)
1677 - subbuf_trunc((unsigned long)
1678 uatomic_read(&buf
->consumed
), chan
)
1679 >= chan
->backend
.buf_size
)) {
1680 unsigned long nr_lost
;
1683 * We do not overwrite non consumed buffers
1684 * and we are full : record is lost.
1686 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1687 v_inc(config
, &buf
->records_lost_full
);
1688 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1689 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1690 nr_lost
+ 1, chan
->backend
.name
,
1696 * Next subbuffer not being written to, and we
1697 * are either in overwrite mode or the buffer is
1698 * not full. It's safe to write in this new
1703 unsigned long nr_lost
;
1706 * Next subbuffer reserve offset does not match the
1707 * commit offset. Drop record in producer-consumer and
1708 * overwrite mode. Caused by either a writer OOPS or too
1709 * many nested writes over a reserve/commit pair.
1711 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1712 v_inc(config
, &buf
->records_lost_wrap
);
1713 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1714 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1715 nr_lost
+ 1, chan
->backend
.name
,
1721 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 unsigned long nr_lost
;
1734 * Record too big for subbuffers, report error, don't
1735 * complete the sub-buffer switch.
1737 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1738 v_inc(config
, &buf
->records_lost_big
);
1739 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1740 DBG("%lu or more records lost in (%s:%d) record size "
1741 " of %zu bytes is too large for buffer\n",
1742 nr_lost
+ 1, chan
->backend
.name
,
1743 buf
->backend
.cpu
, offsets
->size
);
1748 * We just made a successful buffer switch and the
1749 * record fits in the new subbuffer. Let's write.
1754 * Record fits in the current buffer and we are not on a switch
1755 * boundary. It's safe to write.
1758 offsets
->end
= offsets
->begin
+ offsets
->size
;
1760 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1762 * The offset_end will fall at the very beginning of the next
1765 offsets
->switch_new_end
= 1; /* For offsets->begin */
1771 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1772 * @ctx: ring buffer context.
1774 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1775 * -EIO for other errors, else returns 0.
1776 * It will take care of sub-buffer switching.
1778 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1780 struct channel
*chan
= ctx
->chan
;
1781 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1782 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1783 struct lttng_ust_lib_ring_buffer
*buf
;
1784 struct switch_offsets offsets
;
1787 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1788 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1790 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1796 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1798 if (caa_unlikely(ret
))
1800 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1805 * Atomically update last_tsc. This update races against concurrent
1806 * atomic updates, but the race will always cause supplementary full TSC
1807 * records, never the opposite (missing a full TSC record when it would
1810 save_last_tsc(config
, buf
, ctx
->tsc
);
1813 * Push the reader if necessary
1815 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1818 * Clear noref flag for this subbuffer.
1820 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1821 subbuf_index(offsets
.end
- 1, chan
),
1825 * Switch old subbuffer if needed.
1827 if (caa_unlikely(offsets
.switch_old_end
)) {
1828 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1829 subbuf_index(offsets
.old
- 1, chan
),
1831 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1835 * Populate new subbuffer.
1837 if (caa_unlikely(offsets
.switch_new_start
))
1838 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1840 if (caa_unlikely(offsets
.switch_new_end
))
1841 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1843 ctx
->slot_size
= offsets
.size
;
1844 ctx
->pre_offset
= offsets
.begin
;
1845 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1850 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1852 void lttng_fixup_ringbuffer_tls(void)
1854 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
1857 void lib_ringbuffer_signal_init(void)
1863 * Block signal for entire process, so only our thread processes
1867 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
1870 PERROR("pthread_sigmask");