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_old_start
:1, switch_old_end
:1;
113 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
116 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
119 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
122 void lib_ring_buffer_print_errors(struct channel
*chan
,
123 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
124 struct lttng_ust_shm_handle
*handle
);
127 * Handle timer teardown race wrt memory free of private data by
128 * ring buffer signals are handled by a single thread, which permits
129 * a synchronization point between handling of each signal.
130 * Protected by the lock within the structure.
132 struct timer_signal_data
{
133 pthread_t tid
; /* thread id managing signals */
136 pthread_mutex_t lock
;
139 static struct timer_signal_data timer_signal
= {
143 .lock
= PTHREAD_MUTEX_INITIALIZER
,
147 * lib_ring_buffer_reset - Reset ring buffer to initial values.
150 * Effectively empty the ring buffer. Should be called when the buffer is not
151 * used for writing. The ring buffer can be opened for reading, but the reader
152 * should not be using the iterator concurrently with reset. The previous
153 * current iterator record is reset.
155 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
156 struct lttng_ust_shm_handle
*handle
)
158 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
159 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
163 * Reset iterator first. It will put the subbuffer if it currently holds
166 v_set(config
, &buf
->offset
, 0);
167 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
168 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
169 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
170 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
172 uatomic_set(&buf
->consumed
, 0);
173 uatomic_set(&buf
->record_disabled
, 0);
174 v_set(config
, &buf
->last_tsc
, 0);
175 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
176 /* Don't reset number of active readers */
177 v_set(config
, &buf
->records_lost_full
, 0);
178 v_set(config
, &buf
->records_lost_wrap
, 0);
179 v_set(config
, &buf
->records_lost_big
, 0);
180 v_set(config
, &buf
->records_count
, 0);
181 v_set(config
, &buf
->records_overrun
, 0);
186 * channel_reset - Reset channel to initial values.
189 * Effectively empty the channel. Should be called when the channel is not used
190 * for writing. The channel can be opened for reading, but the reader should not
191 * be using the iterator concurrently with reset. The previous current iterator
194 void channel_reset(struct channel
*chan
)
197 * Reset iterators first. Will put the subbuffer if held for reading.
199 uatomic_set(&chan
->record_disabled
, 0);
200 /* Don't reset commit_count_mask, still valid */
201 channel_backend_reset(&chan
->backend
);
202 /* Don't reset switch/read timer interval */
203 /* Don't reset notifiers and notifier enable bits */
204 /* Don't reset reader reference count */
208 * Must be called under cpu hotplug protection.
210 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
211 struct channel_backend
*chanb
, int cpu
,
212 struct lttng_ust_shm_handle
*handle
,
213 struct shm_object
*shmobj
)
215 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
216 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
217 void *priv
= channel_get_private(chan
);
218 size_t subbuf_header_size
;
222 /* Test for cpu hotplug */
223 if (buf
->backend
.allocated
)
226 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
227 cpu
, handle
, shmobj
);
231 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
232 set_shmp(buf
->commit_hot
,
234 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
235 if (!shmp(handle
, buf
->commit_hot
)) {
240 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
241 set_shmp(buf
->commit_cold
,
243 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
244 if (!shmp(handle
, buf
->commit_cold
)) {
250 * Write the subbuffer header for first subbuffer so we know the total
251 * duration of data gathering.
253 subbuf_header_size
= config
->cb
.subbuffer_header_size();
254 v_set(config
, &buf
->offset
, subbuf_header_size
);
255 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
256 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
257 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
258 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
260 if (config
->cb
.buffer_create
) {
261 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
265 buf
->backend
.allocated
= 1;
270 /* commit_cold will be freed by shm teardown */
272 /* commit_hot will be freed by shm teardown */
278 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
280 const struct lttng_ust_lib_ring_buffer_config
*config
;
281 struct lttng_ust_shm_handle
*handle
;
282 struct channel
*chan
;
285 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
287 chan
= si
->si_value
.sival_ptr
;
288 handle
= chan
->handle
;
289 config
= &chan
->backend
.config
;
291 DBG("Switch timer for channel %p\n", chan
);
294 * Only flush buffers periodically if readers are active.
296 pthread_mutex_lock(&wakeup_fd_mutex
);
297 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
298 for_each_possible_cpu(cpu
) {
299 struct lttng_ust_lib_ring_buffer
*buf
=
300 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
301 if (uatomic_read(&buf
->active_readers
))
302 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
306 struct lttng_ust_lib_ring_buffer
*buf
=
307 shmp(handle
, chan
->backend
.buf
[0].shmp
);
309 if (uatomic_read(&buf
->active_readers
))
310 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
313 pthread_mutex_unlock(&wakeup_fd_mutex
);
318 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
320 const struct lttng_ust_lib_ring_buffer_config
*config
;
321 struct lttng_ust_shm_handle
*handle
;
324 handle
= chan
->handle
;
325 config
= &chan
->backend
.config
;
328 * Only flush buffers periodically if readers are active.
330 pthread_mutex_lock(&wakeup_fd_mutex
);
331 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
332 for_each_possible_cpu(cpu
) {
333 struct lttng_ust_lib_ring_buffer
*buf
=
334 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
336 if (uatomic_read(&buf
->active_readers
)
337 && lib_ring_buffer_poll_deliver(config
, buf
,
339 lib_ring_buffer_wakeup(buf
, handle
);
343 struct lttng_ust_lib_ring_buffer
*buf
=
344 shmp(handle
, chan
->backend
.buf
[0].shmp
);
346 if (uatomic_read(&buf
->active_readers
)
347 && lib_ring_buffer_poll_deliver(config
, buf
,
349 lib_ring_buffer_wakeup(buf
, handle
);
352 pthread_mutex_unlock(&wakeup_fd_mutex
);
356 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
358 struct channel
*chan
;
360 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
361 chan
= si
->si_value
.sival_ptr
;
362 DBG("Read timer for channel %p\n", chan
);
363 lib_ring_buffer_channel_do_read(chan
);
368 void rb_setmask(sigset_t
*mask
)
372 ret
= sigemptyset(mask
);
374 PERROR("sigemptyset");
376 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
380 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
384 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
391 void *sig_thread(void *arg
)
397 /* Only self thread will receive signal mask. */
399 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
402 signr
= sigwaitinfo(&mask
, &info
);
405 PERROR("sigwaitinfo");
408 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
409 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
411 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
412 lib_ring_buffer_channel_read_timer(info
.si_signo
,
414 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
416 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
419 ERR("Unexptected signal %d\n", info
.si_signo
);
426 * Ensure only a single thread listens on the timer signal.
429 void lib_ring_buffer_setup_timer_thread(void)
434 pthread_mutex_lock(&timer_signal
.lock
);
435 if (timer_signal
.setup_done
)
438 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
441 PERROR("pthread_create");
443 ret
= pthread_detach(thread
);
446 PERROR("pthread_detach");
448 timer_signal
.setup_done
= 1;
450 pthread_mutex_unlock(&timer_signal
.lock
);
454 * Wait for signal-handling thread quiescent state.
457 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
459 sigset_t pending_set
;
463 * We need to be the only thread interacting with the thread
464 * that manages signals for teardown synchronization.
466 pthread_mutex_lock(&timer_signal
.lock
);
469 * Ensure we don't have any signal queued for this channel.
472 ret
= sigemptyset(&pending_set
);
474 PERROR("sigemptyset");
476 ret
= sigpending(&pending_set
);
478 PERROR("sigpending");
480 if (!sigismember(&pending_set
, signr
))
486 * From this point, no new signal handler will be fired that
487 * would try to access "chan". However, we still need to wait
488 * for any currently executing handler to complete.
491 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
495 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
498 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
500 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
504 pthread_mutex_unlock(&timer_signal
.lock
);
508 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
511 struct itimerspec its
;
514 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
517 chan
->switch_timer_enabled
= 1;
519 lib_ring_buffer_setup_timer_thread();
521 sev
.sigev_notify
= SIGEV_SIGNAL
;
522 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
523 sev
.sigev_value
.sival_ptr
= chan
;
524 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
526 PERROR("timer_create");
529 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
530 its
.it_value
.tv_nsec
= chan
->switch_timer_interval
% 1000000;
531 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
532 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
534 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
536 PERROR("timer_settime");
541 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
545 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
548 ret
= timer_delete(chan
->switch_timer
);
550 PERROR("timer_delete");
553 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
555 chan
->switch_timer
= 0;
556 chan
->switch_timer_enabled
= 0;
560 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
562 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
564 struct itimerspec its
;
567 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
568 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
571 chan
->read_timer_enabled
= 1;
573 lib_ring_buffer_setup_timer_thread();
575 sev
.sigev_notify
= SIGEV_SIGNAL
;
576 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
577 sev
.sigev_value
.sival_ptr
= chan
;
578 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
580 PERROR("timer_create");
583 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
584 its
.it_value
.tv_nsec
= chan
->read_timer_interval
% 1000000;
585 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
586 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
588 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
590 PERROR("timer_settime");
595 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
597 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
600 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
601 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
604 ret
= timer_delete(chan
->read_timer
);
606 PERROR("timer_delete");
610 * do one more check to catch data that has been written in the last
613 lib_ring_buffer_channel_do_read(chan
);
615 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
617 chan
->read_timer
= 0;
618 chan
->read_timer_enabled
= 0;
621 static void channel_unregister_notifiers(struct channel
*chan
,
622 struct lttng_ust_shm_handle
*handle
)
624 lib_ring_buffer_channel_switch_timer_stop(chan
);
625 lib_ring_buffer_channel_read_timer_stop(chan
);
628 static void channel_print_errors(struct channel
*chan
,
629 struct lttng_ust_shm_handle
*handle
)
631 const struct lttng_ust_lib_ring_buffer_config
*config
=
632 &chan
->backend
.config
;
635 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
636 for_each_possible_cpu(cpu
) {
637 struct lttng_ust_lib_ring_buffer
*buf
=
638 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
639 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
642 struct lttng_ust_lib_ring_buffer
*buf
=
643 shmp(handle
, chan
->backend
.buf
[0].shmp
);
645 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
649 static void channel_free(struct channel
*chan
,
650 struct lttng_ust_shm_handle
*handle
)
652 channel_backend_free(&chan
->backend
, handle
);
653 /* chan is freed by shm teardown */
654 shm_object_table_destroy(handle
->table
);
659 * channel_create - Create channel.
660 * @config: ring buffer instance configuration
661 * @name: name of the channel
662 * @priv_data: ring buffer client private data area pointer (output)
663 * @priv_data_size: length, in bytes, of the private data area.
664 * @priv_data_init: initialization data for private data.
665 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
666 * address mapping. It is used only by RING_BUFFER_STATIC
667 * configuration. It can be set to NULL for other backends.
668 * @subbuf_size: subbuffer size
669 * @num_subbuf: number of subbuffers
670 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
671 * padding to let readers get those sub-buffers.
672 * Used for live streaming.
673 * @read_timer_interval: Time interval (in us) to wake up pending readers.
676 * Returns NULL on failure.
678 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
681 size_t priv_data_align
,
682 size_t priv_data_size
,
683 void *priv_data_init
,
684 void *buf_addr
, size_t subbuf_size
,
685 size_t num_subbuf
, unsigned int switch_timer_interval
,
686 unsigned int read_timer_interval
)
689 size_t shmsize
, chansize
;
690 struct channel
*chan
;
691 struct lttng_ust_shm_handle
*handle
;
692 struct shm_object
*shmobj
;
693 unsigned int nr_streams
;
695 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
696 nr_streams
= num_possible_cpus();
700 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
701 read_timer_interval
))
704 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
708 /* Allocate table for channel + per-cpu buffers */
709 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
711 goto error_table_alloc
;
713 /* Calculate the shm allocation layout */
714 shmsize
= sizeof(struct channel
);
715 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
716 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
719 shmsize
+= offset_align(shmsize
, priv_data_align
);
720 shmsize
+= priv_data_size
;
722 /* Allocate normal memory for channel (not shared) */
723 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
);
726 /* struct channel is at object 0, offset 0 (hardcoded) */
727 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
728 assert(handle
->chan
._ref
.index
== 0);
729 assert(handle
->chan
._ref
.offset
== 0);
730 chan
= shmp(handle
, handle
->chan
);
733 chan
->nr_streams
= nr_streams
;
735 /* space for private data */
736 if (priv_data_size
) {
737 DECLARE_SHMP(void, priv_data_alloc
);
739 align_shm(shmobj
, priv_data_align
);
740 chan
->priv_data_offset
= shmobj
->allocated_len
;
741 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
742 if (!shmp(handle
, priv_data_alloc
))
744 *priv_data
= channel_get_private(chan
);
745 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
747 chan
->priv_data_offset
= -1;
752 ret
= channel_backend_init(&chan
->backend
, name
, config
,
753 subbuf_size
, num_subbuf
, handle
);
755 goto error_backend_init
;
757 chan
->handle
= handle
;
758 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
760 chan
->switch_timer_interval
= switch_timer_interval
;
761 chan
->read_timer_interval
= read_timer_interval
;
762 lib_ring_buffer_channel_switch_timer_start(chan
);
763 lib_ring_buffer_channel_read_timer_start(chan
);
769 shm_object_table_destroy(handle
->table
);
775 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
776 uint64_t memory_map_size
,
779 struct lttng_ust_shm_handle
*handle
;
780 struct shm_object
*object
;
782 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
786 /* Allocate table for channel + per-cpu buffers */
787 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
789 goto error_table_alloc
;
790 /* Add channel object */
791 object
= shm_object_table_append_mem(handle
->table
, data
,
792 memory_map_size
, wakeup_fd
);
794 goto error_table_object
;
795 /* struct channel is at object 0, offset 0 (hardcoded) */
796 handle
->chan
._ref
.index
= 0;
797 handle
->chan
._ref
.offset
= 0;
801 shm_object_table_destroy(handle
->table
);
807 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
808 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
809 uint64_t memory_map_size
)
811 struct shm_object
*object
;
813 /* Add stream object */
814 object
= shm_object_table_append_shm(handle
->table
,
815 shm_fd
, wakeup_fd
, stream_nr
,
822 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
824 assert(handle
->table
);
825 return handle
->table
->allocated_len
- 1;
829 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
831 channel_free(chan
, handle
);
835 * channel_destroy - Finalize, wait for q.s. and destroy channel.
836 * @chan: channel to destroy
839 * Call "destroy" callback, finalize channels, decrement the channel
840 * reference count. Note that when readers have completed data
841 * consumption of finalized channels, get_subbuf() will return -ENODATA.
842 * They should release their handle at that point.
844 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
849 * Note: the consumer takes care of finalizing and
850 * switching the buffers.
852 channel_unregister_notifiers(chan
, handle
);
854 * The consumer prints errors.
856 channel_print_errors(chan
, handle
);
860 * sessiond/consumer are keeping a reference on the shm file
861 * descriptor directly. No need to refcount.
863 channel_release(chan
, handle
);
867 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
868 const struct lttng_ust_lib_ring_buffer_config
*config
,
869 struct channel
*chan
, int cpu
,
870 struct lttng_ust_shm_handle
*handle
,
871 int *shm_fd
, int *wait_fd
,
873 uint64_t *memory_map_size
)
877 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
880 if (cpu
>= num_possible_cpus())
883 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
884 *shm_fd
= shm_get_shm_fd(handle
, ref
);
885 *wait_fd
= shm_get_wait_fd(handle
, ref
);
886 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
887 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
889 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
892 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
893 struct channel
*chan
,
894 struct lttng_ust_shm_handle
*handle
)
898 ref
= &handle
->chan
._ref
;
899 return shm_close_wait_fd(handle
, ref
);
902 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
903 struct channel
*chan
,
904 struct lttng_ust_shm_handle
*handle
)
908 ref
= &handle
->chan
._ref
;
909 return shm_close_wakeup_fd(handle
, ref
);
912 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
913 struct channel
*chan
,
914 struct lttng_ust_shm_handle
*handle
,
919 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
922 if (cpu
>= num_possible_cpus())
925 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
926 return shm_close_wait_fd(handle
, ref
);
929 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
930 struct channel
*chan
,
931 struct lttng_ust_shm_handle
*handle
,
937 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
940 if (cpu
>= num_possible_cpus())
943 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
944 pthread_mutex_lock(&wakeup_fd_mutex
);
945 ret
= shm_close_wakeup_fd(handle
, ref
);
946 pthread_mutex_unlock(&wakeup_fd_mutex
);
950 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
951 struct lttng_ust_shm_handle
*handle
)
953 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
959 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
960 struct lttng_ust_shm_handle
*handle
)
962 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
964 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
966 uatomic_dec(&buf
->active_readers
);
970 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
972 * @consumed: consumed count indicating the position where to read
973 * @produced: produced count, indicates position when to stop reading
975 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
976 * data to read at consumed position, or 0 if the get operation succeeds.
979 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
980 unsigned long *consumed
, unsigned long *produced
,
981 struct lttng_ust_shm_handle
*handle
)
983 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
984 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
985 unsigned long consumed_cur
, write_offset
;
988 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
990 * Read finalized before counters.
993 consumed_cur
= uatomic_read(&buf
->consumed
);
995 * No need to issue a memory barrier between consumed count read and
996 * write offset read, because consumed count can only change
997 * concurrently in overwrite mode, and we keep a sequence counter
998 * identifier derived from the write offset to check we are getting
999 * the same sub-buffer we are expecting (the sub-buffers are atomically
1000 * "tagged" upon writes, tags are checked upon read).
1002 write_offset
= v_read(config
, &buf
->offset
);
1005 * Check that we are not about to read the same subbuffer in
1006 * which the writer head is.
1008 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1012 *consumed
= consumed_cur
;
1013 *produced
= subbuf_trunc(write_offset
, chan
);
1019 * The memory barriers __wait_event()/wake_up_interruptible() take care
1020 * of "raw_spin_is_locked" memory ordering.
1029 * lib_ring_buffer_move_consumer - move consumed counter forward
1031 * @consumed_new: new consumed count value
1033 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1034 unsigned long consumed_new
,
1035 struct lttng_ust_shm_handle
*handle
)
1037 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1038 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1039 unsigned long consumed
;
1041 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1044 * Only push the consumed value forward.
1045 * If the consumed cmpxchg fails, this is because we have been pushed by
1046 * the writer in flight recorder mode.
1048 consumed
= uatomic_read(&buf
->consumed
);
1049 while ((long) consumed
- (long) consumed_new
< 0)
1050 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1055 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1057 * @consumed: consumed count indicating the position where to read
1059 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1060 * data to read at consumed position, or 0 if the get operation succeeds.
1062 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1063 unsigned long consumed
,
1064 struct lttng_ust_shm_handle
*handle
)
1066 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1067 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1068 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1073 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1075 * Read finalized before counters.
1078 consumed_cur
= uatomic_read(&buf
->consumed
);
1079 consumed_idx
= subbuf_index(consumed
, chan
);
1080 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
1082 * Make sure we read the commit count before reading the buffer
1083 * data and the write offset. Correct consumed offset ordering
1084 * wrt commit count is insured by the use of cmpxchg to update
1085 * the consumed offset.
1088 * Local rmb to match the remote wmb to read the commit count
1089 * before the buffer data and the write offset.
1093 write_offset
= v_read(config
, &buf
->offset
);
1096 * Check that the buffer we are getting is after or at consumed_cur
1099 if ((long) subbuf_trunc(consumed
, chan
)
1100 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1104 * Check that the subbuffer we are trying to consume has been
1105 * already fully committed.
1107 if (((commit_count
- chan
->backend
.subbuf_size
)
1108 & chan
->commit_count_mask
)
1109 - (buf_trunc(consumed_cur
, chan
)
1110 >> chan
->backend
.num_subbuf_order
)
1115 * Check that we are not about to read the same subbuffer in
1116 * which the writer head is.
1118 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1123 * Failure to get the subbuffer causes a busy-loop retry without going
1124 * to a wait queue. These are caused by short-lived race windows where
1125 * the writer is getting access to a subbuffer we were trying to get
1126 * access to. Also checks that the "consumed" buffer count we are
1127 * looking for matches the one contained in the subbuffer id.
1129 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1130 consumed_idx
, buf_trunc_val(consumed
, chan
),
1134 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1136 buf
->get_subbuf_consumed
= consumed
;
1137 buf
->get_subbuf
= 1;
1143 * The memory barriers __wait_event()/wake_up_interruptible() take care
1144 * of "raw_spin_is_locked" memory ordering.
1153 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1156 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1157 struct lttng_ust_shm_handle
*handle
)
1159 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1160 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1161 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1162 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1164 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1166 if (!buf
->get_subbuf
) {
1168 * Reader puts a subbuffer it did not get.
1170 CHAN_WARN_ON(chan
, 1);
1173 consumed
= buf
->get_subbuf_consumed
;
1174 buf
->get_subbuf
= 0;
1177 * Clear the records_unread counter. (overruns counter)
1178 * Can still be non-zero if a file reader simply grabbed the data
1179 * without using iterators.
1180 * Can be below zero if an iterator is used on a snapshot more than
1183 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1184 v_add(config
, v_read(config
,
1185 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
1186 &bufb
->records_read
);
1187 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
1188 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1189 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1190 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1193 * Exchange the reader subbuffer with the one we put in its place in the
1194 * writer subbuffer table. Expect the original consumed count. If
1195 * update_read_sb_index fails, this is because the writer updated the
1196 * subbuffer concurrently. We should therefore keep the subbuffer we
1197 * currently have: it has become invalid to try reading this sub-buffer
1198 * consumed count value anyway.
1200 consumed_idx
= subbuf_index(consumed
, chan
);
1201 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1202 consumed_idx
, buf_trunc_val(consumed
, chan
),
1205 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1206 * if the writer concurrently updated it.
1211 * cons_offset is an iterator on all subbuffer offsets between the reader
1212 * position and the writer position. (inclusive)
1215 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1216 struct channel
*chan
,
1217 unsigned long cons_offset
,
1219 struct lttng_ust_shm_handle
*handle
)
1221 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1222 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1224 cons_idx
= subbuf_index(cons_offset
, chan
);
1225 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
1226 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
1228 if (subbuf_offset(commit_count
, chan
) != 0)
1229 DBG("ring buffer %s, cpu %d: "
1230 "commit count in subbuffer %lu,\n"
1231 "expecting multiples of %lu bytes\n"
1232 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1233 chan
->backend
.name
, cpu
, cons_idx
,
1234 chan
->backend
.subbuf_size
,
1235 commit_count
, commit_count_sb
);
1237 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1238 chan
->backend
.name
, cpu
, commit_count
);
1242 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1243 struct channel
*chan
,
1244 void *priv
, int cpu
,
1245 struct lttng_ust_shm_handle
*handle
)
1247 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1248 unsigned long write_offset
, cons_offset
;
1251 * No need to order commit_count, write_offset and cons_offset reads
1252 * because we execute at teardown when no more writer nor reader
1253 * references are left.
1255 write_offset
= v_read(config
, &buf
->offset
);
1256 cons_offset
= uatomic_read(&buf
->consumed
);
1257 if (write_offset
!= cons_offset
)
1258 DBG("ring buffer %s, cpu %d: "
1259 "non-consumed data\n"
1260 " [ %lu bytes written, %lu bytes read ]\n",
1261 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1263 for (cons_offset
= uatomic_read(&buf
->consumed
);
1264 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1267 cons_offset
= subbuf_align(cons_offset
, chan
))
1268 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1273 void lib_ring_buffer_print_errors(struct channel
*chan
,
1274 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1275 struct lttng_ust_shm_handle
*handle
)
1277 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1278 void *priv
= channel_get_private(chan
);
1280 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1281 DBG("ring buffer %s: %lu records written, "
1282 "%lu records overrun\n",
1284 v_read(config
, &buf
->records_count
),
1285 v_read(config
, &buf
->records_overrun
));
1287 DBG("ring buffer %s, cpu %d: %lu records written, "
1288 "%lu records overrun\n",
1289 chan
->backend
.name
, cpu
,
1290 v_read(config
, &buf
->records_count
),
1291 v_read(config
, &buf
->records_overrun
));
1293 if (v_read(config
, &buf
->records_lost_full
)
1294 || v_read(config
, &buf
->records_lost_wrap
)
1295 || v_read(config
, &buf
->records_lost_big
))
1296 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1297 " [ %lu buffer full, %lu nest buffer wrap-around, "
1298 "%lu event too big ]\n",
1299 chan
->backend
.name
, cpu
,
1300 v_read(config
, &buf
->records_lost_full
),
1301 v_read(config
, &buf
->records_lost_wrap
),
1302 v_read(config
, &buf
->records_lost_big
));
1304 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1308 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1310 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1313 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1314 struct channel
*chan
,
1315 struct switch_offsets
*offsets
,
1317 struct lttng_ust_shm_handle
*handle
)
1319 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1320 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1321 unsigned long commit_count
;
1323 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1326 * Order all writes to buffer before the commit count update that will
1327 * determine that the subbuffer is full.
1330 v_add(config
, config
->cb
.subbuffer_header_size(),
1331 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1332 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1333 /* Check if the written buffer has to be delivered */
1334 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1335 commit_count
, oldidx
, handle
);
1336 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1337 offsets
->old
, commit_count
,
1338 config
->cb
.subbuffer_header_size(),
1343 * lib_ring_buffer_switch_old_end: switch old subbuffer
1345 * Note : offset_old should never be 0 here. It is ok, because we never perform
1346 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1347 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1351 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1352 struct channel
*chan
,
1353 struct switch_offsets
*offsets
,
1355 struct lttng_ust_shm_handle
*handle
)
1357 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1358 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1359 unsigned long commit_count
, padding_size
, data_size
;
1361 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1362 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1363 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1367 * Order all writes to buffer before the commit count update that will
1368 * determine that the subbuffer is full.
1371 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1372 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1373 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1374 commit_count
, oldidx
, handle
);
1375 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1376 offsets
->old
, commit_count
,
1377 padding_size
, handle
);
1381 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1383 * This code can be executed unordered : writers may already have written to the
1384 * sub-buffer before this code gets executed, caution. The commit makes sure
1385 * that this code is executed before the deliver of this sub-buffer.
1388 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1389 struct channel
*chan
,
1390 struct switch_offsets
*offsets
,
1392 struct lttng_ust_shm_handle
*handle
)
1394 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1395 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1396 unsigned long commit_count
;
1398 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1401 * Order all writes to buffer before the commit count update that will
1402 * determine that the subbuffer is full.
1405 v_add(config
, config
->cb
.subbuffer_header_size(),
1406 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1407 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1408 /* Check if the written buffer has to be delivered */
1409 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1410 commit_count
, beginidx
, handle
);
1411 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1412 offsets
->begin
, commit_count
,
1413 config
->cb
.subbuffer_header_size(),
1420 * !0 if execution must be aborted.
1423 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1424 struct lttng_ust_lib_ring_buffer
*buf
,
1425 struct channel
*chan
,
1426 struct switch_offsets
*offsets
,
1429 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1432 offsets
->begin
= v_read(config
, &buf
->offset
);
1433 offsets
->old
= offsets
->begin
;
1434 offsets
->switch_old_start
= 0;
1435 off
= subbuf_offset(offsets
->begin
, chan
);
1437 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1440 * Ensure we flush the header of an empty subbuffer when doing the
1441 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1442 * total data gathering duration even if there were no records saved
1443 * after the last buffer switch.
1444 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1445 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1446 * subbuffer header as appropriate.
1447 * The next record that reserves space will be responsible for
1448 * populating the following subbuffer header. We choose not to populate
1449 * the next subbuffer header here because we want to be able to use
1450 * SWITCH_ACTIVE for periodical buffer flush, which must
1451 * guarantee that all the buffer content (records and header
1452 * timestamps) are visible to the reader. This is required for
1453 * quiescence guarantees for the fusion merge.
1455 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1456 if (caa_unlikely(off
== 0)) {
1458 * A final flush that encounters an empty
1459 * sub-buffer cannot switch buffer if a
1460 * reader is located within this sub-buffer.
1461 * Anyway, the purpose of final flushing of a
1462 * sub-buffer at offset 0 is to handle the case
1463 * of entirely empty stream.
1465 if (caa_unlikely(subbuf_trunc(offsets
->begin
, chan
)
1466 - subbuf_trunc((unsigned long)
1467 uatomic_read(&buf
->consumed
), chan
)
1468 >= chan
->backend
.buf_size
))
1471 * The client does not save any header information.
1472 * Don't switch empty subbuffer on finalize, because it
1473 * is invalid to deliver a completely empty subbuffer.
1475 if (!config
->cb
.subbuffer_header_size())
1478 * Need to write the subbuffer start header on finalize.
1480 offsets
->switch_old_start
= 1;
1482 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1484 return -1; /* we do not have to switch : buffer is empty */
1485 /* Note: old points to the next subbuf at offset 0 */
1486 offsets
->end
= offsets
->begin
;
1491 * Force a sub-buffer switch. This operation is completely reentrant : can be
1492 * called while tracing is active with absolutely no lock held.
1494 * Note, however, that as a v_cmpxchg is used for some atomic
1495 * operations, this function must be called from the CPU which owns the buffer
1496 * for a ACTIVE flush.
1498 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1499 struct lttng_ust_shm_handle
*handle
)
1501 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1502 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1503 struct switch_offsets offsets
;
1504 unsigned long oldidx
;
1510 * Perform retryable operations.
1513 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1515 return; /* Switch not needed */
1516 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1520 * Atomically update last_tsc. This update races against concurrent
1521 * atomic updates, but the race will always cause supplementary full TSC
1522 * records, never the opposite (missing a full TSC record when it would
1525 save_last_tsc(config
, buf
, tsc
);
1528 * Push the reader if necessary
1530 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1532 oldidx
= subbuf_index(offsets
.old
, chan
);
1533 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1536 * May need to populate header start on SWITCH_FLUSH.
1538 if (offsets
.switch_old_start
) {
1539 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1540 offsets
.old
+= config
->cb
.subbuffer_header_size();
1544 * Switch old subbuffer.
1546 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1552 * -ENOSPC if event size is too large for packet.
1553 * -ENOBUFS if there is currently not enough space in buffer for the event.
1554 * -EIO if data cannot be written into the buffer for any other reason.
1557 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1558 struct channel
*chan
,
1559 struct switch_offsets
*offsets
,
1560 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1562 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1563 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1564 unsigned long reserve_commit_diff
;
1566 offsets
->begin
= v_read(config
, &buf
->offset
);
1567 offsets
->old
= offsets
->begin
;
1568 offsets
->switch_new_start
= 0;
1569 offsets
->switch_old_end
= 0;
1570 offsets
->pre_header_padding
= 0;
1572 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1573 if ((int64_t) ctx
->tsc
== -EIO
)
1576 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1577 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1579 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1580 offsets
->switch_new_start
= 1; /* For offsets->begin */
1582 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1584 &offsets
->pre_header_padding
,
1587 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1590 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1591 offsets
->size
> chan
->backend
.subbuf_size
)) {
1592 offsets
->switch_old_end
= 1; /* For offsets->old */
1593 offsets
->switch_new_start
= 1; /* For offsets->begin */
1596 if (caa_unlikely(offsets
->switch_new_start
)) {
1597 unsigned long sb_index
;
1600 * We are typically not filling the previous buffer completely.
1602 if (caa_likely(offsets
->switch_old_end
))
1603 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1604 offsets
->begin
= offsets
->begin
1605 + config
->cb
.subbuffer_header_size();
1606 /* Test new buffer integrity */
1607 sb_index
= subbuf_index(offsets
->begin
, chan
);
1608 reserve_commit_diff
=
1609 (buf_trunc(offsets
->begin
, chan
)
1610 >> chan
->backend
.num_subbuf_order
)
1611 - ((unsigned long) v_read(config
,
1612 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1613 & chan
->commit_count_mask
);
1614 if (caa_likely(reserve_commit_diff
== 0)) {
1615 /* Next subbuffer not being written to. */
1616 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1617 subbuf_trunc(offsets
->begin
, chan
)
1618 - subbuf_trunc((unsigned long)
1619 uatomic_read(&buf
->consumed
), chan
)
1620 >= chan
->backend
.buf_size
)) {
1621 unsigned long nr_lost
;
1624 * We do not overwrite non consumed buffers
1625 * and we are full : record is lost.
1627 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1628 v_inc(config
, &buf
->records_lost_full
);
1629 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1630 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1631 nr_lost
+ 1, chan
->backend
.name
,
1637 * Next subbuffer not being written to, and we
1638 * are either in overwrite mode or the buffer is
1639 * not full. It's safe to write in this new
1644 unsigned long nr_lost
;
1647 * Next subbuffer reserve offset does not match the
1648 * commit offset. Drop record in producer-consumer and
1649 * overwrite mode. Caused by either a writer OOPS or too
1650 * many nested writes over a reserve/commit pair.
1652 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1653 v_inc(config
, &buf
->records_lost_wrap
);
1654 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1655 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1656 nr_lost
+ 1, chan
->backend
.name
,
1662 config
->cb
.record_header_size(config
, chan
,
1664 &offsets
->pre_header_padding
,
1667 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1670 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1671 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1672 unsigned long nr_lost
;
1675 * Record too big for subbuffers, report error, don't
1676 * complete the sub-buffer switch.
1678 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1679 v_inc(config
, &buf
->records_lost_big
);
1680 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1681 DBG("%lu or more records lost in (%s:%d) record size "
1682 " of %zu bytes is too large for buffer\n",
1683 nr_lost
+ 1, chan
->backend
.name
,
1684 buf
->backend
.cpu
, offsets
->size
);
1689 * We just made a successful buffer switch and the
1690 * record fits in the new subbuffer. Let's write.
1695 * Record fits in the current buffer and we are not on a switch
1696 * boundary. It's safe to write.
1699 offsets
->end
= offsets
->begin
+ offsets
->size
;
1704 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1705 * @ctx: ring buffer context.
1707 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1708 * -EIO for other errors, else returns 0.
1709 * It will take care of sub-buffer switching.
1711 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1713 struct channel
*chan
= ctx
->chan
;
1714 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1715 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1716 struct lttng_ust_lib_ring_buffer
*buf
;
1717 struct switch_offsets offsets
;
1720 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1721 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1723 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1729 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1731 if (caa_unlikely(ret
))
1733 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1738 * Atomically update last_tsc. This update races against concurrent
1739 * atomic updates, but the race will always cause supplementary full TSC
1740 * records, never the opposite (missing a full TSC record when it would
1743 save_last_tsc(config
, buf
, ctx
->tsc
);
1746 * Push the reader if necessary
1748 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1751 * Clear noref flag for this subbuffer.
1753 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1754 subbuf_index(offsets
.end
- 1, chan
),
1758 * Switch old subbuffer if needed.
1760 if (caa_unlikely(offsets
.switch_old_end
)) {
1761 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1762 subbuf_index(offsets
.old
- 1, chan
),
1764 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1768 * Populate new subbuffer.
1770 if (caa_unlikely(offsets
.switch_new_start
))
1771 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1773 ctx
->slot_size
= offsets
.size
;
1774 ctx
->pre_offset
= offsets
.begin
;
1775 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1780 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1782 void lttng_fixup_ringbuffer_tls(void)
1784 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
1787 void lib_ringbuffer_signal_init(void)
1793 * Block signal for entire process, so only our thread processes
1797 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
1800 PERROR("pthread_sigmask");