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 SIGRTMIN
84 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 1
85 #define CLOCKID CLOCK_MONOTONIC
88 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
89 * close(2) to close the fd returned by shm_open.
90 * shm_unlink releases the shared memory object name.
91 * ftruncate(2) sets the size of the memory object.
92 * mmap/munmap maps the shared memory obj to a virtual address in the
93 * calling proceess (should be done both in libust and consumer).
94 * See shm_overview(7) for details.
95 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
98 * Since we don't need to access the object using its name, we can
99 * immediately shm_unlink(3) it, and only keep the handle with its file
104 * Internal structure representing offsets to use at a sub-buffer switch.
106 struct switch_offsets
{
107 unsigned long begin
, end
, old
;
108 size_t pre_header_padding
, size
;
109 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
: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 ust mutex.
132 struct timer_signal_data
{
133 pthread_t tid
; /* thread id managing signals */
138 static struct timer_signal_data timer_signal
;
141 * lib_ring_buffer_reset - Reset ring buffer to initial values.
144 * Effectively empty the ring buffer. Should be called when the buffer is not
145 * used for writing. The ring buffer can be opened for reading, but the reader
146 * should not be using the iterator concurrently with reset. The previous
147 * current iterator record is reset.
149 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
150 struct lttng_ust_shm_handle
*handle
)
152 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
153 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
157 * Reset iterator first. It will put the subbuffer if it currently holds
160 v_set(config
, &buf
->offset
, 0);
161 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
162 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
163 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
164 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
166 uatomic_set(&buf
->consumed
, 0);
167 uatomic_set(&buf
->record_disabled
, 0);
168 v_set(config
, &buf
->last_tsc
, 0);
169 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
170 /* Don't reset number of active readers */
171 v_set(config
, &buf
->records_lost_full
, 0);
172 v_set(config
, &buf
->records_lost_wrap
, 0);
173 v_set(config
, &buf
->records_lost_big
, 0);
174 v_set(config
, &buf
->records_count
, 0);
175 v_set(config
, &buf
->records_overrun
, 0);
180 * channel_reset - Reset channel to initial values.
183 * Effectively empty the channel. Should be called when the channel is not used
184 * for writing. The channel can be opened for reading, but the reader should not
185 * be using the iterator concurrently with reset. The previous current iterator
188 void channel_reset(struct channel
*chan
)
191 * Reset iterators first. Will put the subbuffer if held for reading.
193 uatomic_set(&chan
->record_disabled
, 0);
194 /* Don't reset commit_count_mask, still valid */
195 channel_backend_reset(&chan
->backend
);
196 /* Don't reset switch/read timer interval */
197 /* Don't reset notifiers and notifier enable bits */
198 /* Don't reset reader reference count */
202 * Must be called under cpu hotplug protection.
204 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
205 struct channel_backend
*chanb
, int cpu
,
206 struct lttng_ust_shm_handle
*handle
,
207 struct shm_object
*shmobj
)
209 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
210 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
211 void *priv
= channel_get_private(chan
);
212 size_t subbuf_header_size
;
216 /* Test for cpu hotplug */
217 if (buf
->backend
.allocated
)
220 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
221 cpu
, handle
, shmobj
);
225 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
226 set_shmp(buf
->commit_hot
,
228 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
229 if (!shmp(handle
, buf
->commit_hot
)) {
234 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
235 set_shmp(buf
->commit_cold
,
237 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
238 if (!shmp(handle
, buf
->commit_cold
)) {
244 * Write the subbuffer header for first subbuffer so we know the total
245 * duration of data gathering.
247 subbuf_header_size
= config
->cb
.subbuffer_header_size();
248 v_set(config
, &buf
->offset
, subbuf_header_size
);
249 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
250 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
251 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
252 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
254 if (config
->cb
.buffer_create
) {
255 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
259 buf
->backend
.allocated
= 1;
264 /* commit_cold will be freed by shm teardown */
266 /* commit_hot will be freed by shm teardown */
272 static void switch_buffer_timer(unsigned long data
)
274 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
275 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
276 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
279 * Only flush buffers periodically if readers are active.
281 if (uatomic_read(&buf
->active_readers
))
282 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
285 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
286 // mod_timer_pinned(&buf->switch_timer,
287 // jiffies + chan->switch_timer_interval);
289 // mod_timer(&buf->switch_timer,
290 // jiffies + chan->switch_timer_interval);
295 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
297 const struct lttng_ust_lib_ring_buffer_config
*config
;
298 struct lttng_ust_shm_handle
*handle
;
299 struct channel
*chan
;
302 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
304 chan
= si
->si_value
.sival_ptr
;
305 handle
= chan
->handle
;
306 config
= &chan
->backend
.config
;
308 DBG("Timer for channel %p\n", chan
);
310 pthread_mutex_lock(&wakeup_fd_mutex
);
311 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
312 for_each_possible_cpu(cpu
) {
313 struct lttng_ust_lib_ring_buffer
*buf
=
314 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
316 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
320 struct lttng_ust_lib_ring_buffer
*buf
=
321 shmp(handle
, chan
->backend
.buf
[0].shmp
);
323 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
326 pthread_mutex_unlock(&wakeup_fd_mutex
);
331 void rb_setmask(sigset_t
*mask
)
335 ret
= sigemptyset(mask
);
337 PERROR("sigemptyset");
339 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG
);
343 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
350 void *sig_thread(void *arg
)
356 /* Only self thread will receive signal mask. */
358 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
361 signr
= sigwaitinfo(&mask
, &info
);
363 PERROR("sigwaitinfo");
366 if (signr
== LTTNG_UST_RB_SIG
) {
367 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
369 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
371 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
374 ERR("Unexptected signal %d\n", info
.si_signo
);
381 * Called with ust_lock() held.
382 * Ensure only a single thread listens on the timer signal.
385 void lib_ring_buffer_setup_timer_thread(void)
390 if (timer_signal
.setup_done
)
393 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
396 PERROR("pthread_create");
398 ret
= pthread_detach(thread
);
401 PERROR("pthread_detach");
403 timer_signal
.setup_done
= 1;
407 * Called with ust_lock() held.
410 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
413 struct itimerspec its
;
416 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
419 chan
->switch_timer_enabled
= 1;
421 lib_ring_buffer_setup_timer_thread();
423 sev
.sigev_notify
= SIGEV_SIGNAL
;
424 sev
.sigev_signo
= LTTNG_UST_RB_SIG
;
425 sev
.sigev_value
.sival_ptr
= chan
;
426 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
428 PERROR("timer_create");
431 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
432 its
.it_value
.tv_nsec
= chan
->switch_timer_interval
% 1000000;
433 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
434 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
436 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
438 PERROR("timer_settime");
443 * Called with ust_lock() held.
446 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
448 sigset_t pending_set
;
449 int sig_is_pending
, ret
;
451 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
454 ret
= timer_delete(chan
->switch_timer
);
456 PERROR("timer_delete");
460 * Ensure we don't have any signal queued for this channel.
463 ret
= sigemptyset(&pending_set
);
465 PERROR("sigemptyset");
467 ret
= sigpending(&pending_set
);
469 PERROR("sigpending");
471 sig_is_pending
= sigismember(&pending_set
, LTTNG_UST_RB_SIG
);
478 * From this point, no new signal handler will be fired that
479 * would try to access "chan". However, we still need to wait
480 * for any currently executing handler to complete.
483 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
487 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
490 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
492 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
496 chan
->switch_timer
= 0;
497 chan
->switch_timer_enabled
= 0;
502 * Polling timer to check the channels for data.
504 static void read_buffer_timer(unsigned long data
)
506 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
507 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
508 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
510 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
512 if (uatomic_read(&buf
->active_readers
))
513 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
515 //wake_up_interruptible(&buf->read_wait);
516 //wake_up_interruptible(&chan->read_wait);
520 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
521 // mod_timer_pinned(&buf->read_timer,
522 // jiffies + chan->read_timer_interval);
524 // mod_timer(&buf->read_timer,
525 // jiffies + chan->read_timer_interval);
529 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
530 struct lttng_ust_shm_handle
*handle
)
532 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
533 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
535 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
536 || !chan
->read_timer_interval
537 || buf
->read_timer_enabled
)
541 //init_timer(&buf->read_timer);
542 //buf->read_timer.function = read_buffer_timer;
543 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
544 //buf->read_timer.data = (unsigned long)buf;
546 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
547 // add_timer_on(&buf->read_timer, buf->backend.cpu);
549 // add_timer(&buf->read_timer);
550 buf
->read_timer_enabled
= 1;
553 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
554 struct lttng_ust_shm_handle
*handle
)
556 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
557 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
559 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
560 || !chan
->read_timer_interval
561 || !buf
->read_timer_enabled
)
565 //del_timer_sync(&buf->read_timer);
567 * do one more check to catch data that has been written in the last
570 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
572 //wake_up_interruptible(&buf->read_wait);
573 //wake_up_interruptible(&chan->read_wait);
575 buf
->read_timer_enabled
= 0;
578 static void channel_unregister_notifiers(struct channel
*chan
,
579 struct lttng_ust_shm_handle
*handle
)
581 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
584 lib_ring_buffer_channel_switch_timer_stop(chan
);
585 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
586 for_each_possible_cpu(cpu
) {
587 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
589 lib_ring_buffer_stop_read_timer(buf
, handle
);
592 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
594 lib_ring_buffer_stop_read_timer(buf
, handle
);
596 //channel_backend_unregister_notifiers(&chan->backend);
599 static void channel_print_errors(struct channel
*chan
,
600 struct lttng_ust_shm_handle
*handle
)
602 const struct lttng_ust_lib_ring_buffer_config
*config
=
603 &chan
->backend
.config
;
606 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
607 for_each_possible_cpu(cpu
) {
608 struct lttng_ust_lib_ring_buffer
*buf
=
609 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
610 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
613 struct lttng_ust_lib_ring_buffer
*buf
=
614 shmp(handle
, chan
->backend
.buf
[0].shmp
);
616 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
620 static void channel_free(struct channel
*chan
,
621 struct lttng_ust_shm_handle
*handle
)
623 channel_backend_free(&chan
->backend
, handle
);
624 /* chan is freed by shm teardown */
625 shm_object_table_destroy(handle
->table
);
630 * channel_create - Create channel.
631 * @config: ring buffer instance configuration
632 * @name: name of the channel
633 * @priv_data: ring buffer client private data area pointer (output)
634 * @priv_data_size: length, in bytes, of the private data area.
635 * @priv_data_init: initialization data for private data.
636 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
637 * address mapping. It is used only by RING_BUFFER_STATIC
638 * configuration. It can be set to NULL for other backends.
639 * @subbuf_size: subbuffer size
640 * @num_subbuf: number of subbuffers
641 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
642 * padding to let readers get those sub-buffers.
643 * Used for live streaming.
644 * @read_timer_interval: Time interval (in us) to wake up pending readers.
647 * Returns NULL on failure.
649 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
652 size_t priv_data_align
,
653 size_t priv_data_size
,
654 void *priv_data_init
,
655 void *buf_addr
, size_t subbuf_size
,
656 size_t num_subbuf
, unsigned int switch_timer_interval
,
657 unsigned int read_timer_interval
)
660 size_t shmsize
, chansize
;
661 struct channel
*chan
;
662 struct lttng_ust_shm_handle
*handle
;
663 struct shm_object
*shmobj
;
664 unsigned int nr_streams
;
666 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
667 nr_streams
= num_possible_cpus();
671 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
672 read_timer_interval
))
675 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
679 /* Allocate table for channel + per-cpu buffers */
680 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
682 goto error_table_alloc
;
684 /* Calculate the shm allocation layout */
685 shmsize
= sizeof(struct channel
);
686 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
687 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
690 shmsize
+= offset_align(shmsize
, priv_data_align
);
691 shmsize
+= priv_data_size
;
693 /* Allocate normal memory for channel (not shared) */
694 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
);
697 /* struct channel is at object 0, offset 0 (hardcoded) */
698 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
699 assert(handle
->chan
._ref
.index
== 0);
700 assert(handle
->chan
._ref
.offset
== 0);
701 chan
= shmp(handle
, handle
->chan
);
704 chan
->nr_streams
= nr_streams
;
706 /* space for private data */
707 if (priv_data_size
) {
708 DECLARE_SHMP(void, priv_data_alloc
);
710 align_shm(shmobj
, priv_data_align
);
711 chan
->priv_data_offset
= shmobj
->allocated_len
;
712 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
713 if (!shmp(handle
, priv_data_alloc
))
715 *priv_data
= channel_get_private(chan
);
716 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
718 chan
->priv_data_offset
= -1;
723 ret
= channel_backend_init(&chan
->backend
, name
, config
,
724 subbuf_size
, num_subbuf
, handle
);
726 goto error_backend_init
;
728 chan
->handle
= handle
;
729 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
730 chan
->switch_timer_interval
= switch_timer_interval
;
733 //chan->read_timer_interval = read_timer_interval;
734 //init_waitqueue_head(&chan->read_wait);
735 //init_waitqueue_head(&chan->hp_wait);
737 lib_ring_buffer_channel_switch_timer_start(chan
);
738 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
740 * In case of non-hotplug cpu, if the ring-buffer is allocated
741 * in early initcall, it will not be notified of secondary cpus.
742 * In that off case, we need to allocate for all possible cpus.
744 for_each_possible_cpu(cpu
) {
745 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
746 lib_ring_buffer_start_read_timer(buf
, handle
);
749 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
751 lib_ring_buffer_start_read_timer(buf
, handle
);
757 shm_object_table_destroy(handle
->table
);
763 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
764 uint64_t memory_map_size
,
767 struct lttng_ust_shm_handle
*handle
;
768 struct shm_object
*object
;
770 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
774 /* Allocate table for channel + per-cpu buffers */
775 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
777 goto error_table_alloc
;
778 /* Add channel object */
779 object
= shm_object_table_append_mem(handle
->table
, data
,
780 memory_map_size
, wakeup_fd
);
782 goto error_table_object
;
783 /* struct channel is at object 0, offset 0 (hardcoded) */
784 handle
->chan
._ref
.index
= 0;
785 handle
->chan
._ref
.offset
= 0;
789 shm_object_table_destroy(handle
->table
);
795 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
796 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
797 uint64_t memory_map_size
)
799 struct shm_object
*object
;
801 /* Add stream object */
802 object
= shm_object_table_append_shm(handle
->table
,
803 shm_fd
, wakeup_fd
, stream_nr
,
810 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
812 assert(handle
->table
);
813 return handle
->table
->allocated_len
- 1;
817 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
819 channel_free(chan
, handle
);
823 * channel_destroy - Finalize, wait for q.s. and destroy channel.
824 * @chan: channel to destroy
827 * Call "destroy" callback, finalize channels, decrement the channel
828 * reference count. Note that when readers have completed data
829 * consumption of finalized channels, get_subbuf() will return -ENODATA.
830 * They should release their handle at that point.
832 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
837 * Note: the consumer takes care of finalizing and
838 * switching the buffers.
840 channel_unregister_notifiers(chan
, handle
);
842 * The consumer prints errors.
844 channel_print_errors(chan
, handle
);
848 * sessiond/consumer are keeping a reference on the shm file
849 * descriptor directly. No need to refcount.
851 channel_release(chan
, handle
);
855 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
856 const struct lttng_ust_lib_ring_buffer_config
*config
,
857 struct channel
*chan
, int cpu
,
858 struct lttng_ust_shm_handle
*handle
,
859 int *shm_fd
, int *wait_fd
,
861 uint64_t *memory_map_size
)
865 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
868 if (cpu
>= num_possible_cpus())
871 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
872 *shm_fd
= shm_get_shm_fd(handle
, ref
);
873 *wait_fd
= shm_get_wait_fd(handle
, ref
);
874 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
875 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
877 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
880 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
881 struct channel
*chan
,
882 struct lttng_ust_shm_handle
*handle
)
886 ref
= &handle
->chan
._ref
;
887 return shm_close_wait_fd(handle
, ref
);
890 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
891 struct channel
*chan
,
892 struct lttng_ust_shm_handle
*handle
)
896 ref
= &handle
->chan
._ref
;
897 return shm_close_wakeup_fd(handle
, ref
);
900 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
901 struct channel
*chan
,
902 struct lttng_ust_shm_handle
*handle
,
907 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
910 if (cpu
>= num_possible_cpus())
913 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
914 return shm_close_wait_fd(handle
, ref
);
917 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
918 struct channel
*chan
,
919 struct lttng_ust_shm_handle
*handle
,
925 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
928 if (cpu
>= num_possible_cpus())
931 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
932 pthread_mutex_lock(&wakeup_fd_mutex
);
933 ret
= shm_close_wakeup_fd(handle
, ref
);
934 pthread_mutex_unlock(&wakeup_fd_mutex
);
938 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
939 struct lttng_ust_shm_handle
*handle
)
941 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
947 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
948 struct lttng_ust_shm_handle
*handle
)
950 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
952 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
954 uatomic_dec(&buf
->active_readers
);
958 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
960 * @consumed: consumed count indicating the position where to read
961 * @produced: produced count, indicates position when to stop reading
963 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
964 * data to read at consumed position, or 0 if the get operation succeeds.
967 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
968 unsigned long *consumed
, unsigned long *produced
,
969 struct lttng_ust_shm_handle
*handle
)
971 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
972 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
973 unsigned long consumed_cur
, write_offset
;
976 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
978 * Read finalized before counters.
981 consumed_cur
= uatomic_read(&buf
->consumed
);
983 * No need to issue a memory barrier between consumed count read and
984 * write offset read, because consumed count can only change
985 * concurrently in overwrite mode, and we keep a sequence counter
986 * identifier derived from the write offset to check we are getting
987 * the same sub-buffer we are expecting (the sub-buffers are atomically
988 * "tagged" upon writes, tags are checked upon read).
990 write_offset
= v_read(config
, &buf
->offset
);
993 * Check that we are not about to read the same subbuffer in
994 * which the writer head is.
996 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1000 *consumed
= consumed_cur
;
1001 *produced
= subbuf_trunc(write_offset
, chan
);
1007 * The memory barriers __wait_event()/wake_up_interruptible() take care
1008 * of "raw_spin_is_locked" memory ordering.
1017 * lib_ring_buffer_put_snapshot - move consumed counter forward
1019 * @consumed_new: new consumed count value
1021 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1022 unsigned long consumed_new
,
1023 struct lttng_ust_shm_handle
*handle
)
1025 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1026 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1027 unsigned long consumed
;
1029 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1032 * Only push the consumed value forward.
1033 * If the consumed cmpxchg fails, this is because we have been pushed by
1034 * the writer in flight recorder mode.
1036 consumed
= uatomic_read(&buf
->consumed
);
1037 while ((long) consumed
- (long) consumed_new
< 0)
1038 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1043 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1045 * @consumed: consumed count indicating the position where to read
1047 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1048 * data to read at consumed position, or 0 if the get operation succeeds.
1050 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1051 unsigned long consumed
,
1052 struct lttng_ust_shm_handle
*handle
)
1054 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1055 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1056 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1061 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1063 * Read finalized before counters.
1066 consumed_cur
= uatomic_read(&buf
->consumed
);
1067 consumed_idx
= subbuf_index(consumed
, chan
);
1068 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
1070 * Make sure we read the commit count before reading the buffer
1071 * data and the write offset. Correct consumed offset ordering
1072 * wrt commit count is insured by the use of cmpxchg to update
1073 * the consumed offset.
1076 * Local rmb to match the remote wmb to read the commit count
1077 * before the buffer data and the write offset.
1081 write_offset
= v_read(config
, &buf
->offset
);
1084 * Check that the buffer we are getting is after or at consumed_cur
1087 if ((long) subbuf_trunc(consumed
, chan
)
1088 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1092 * Check that the subbuffer we are trying to consume has been
1093 * already fully committed.
1095 if (((commit_count
- chan
->backend
.subbuf_size
)
1096 & chan
->commit_count_mask
)
1097 - (buf_trunc(consumed_cur
, chan
)
1098 >> chan
->backend
.num_subbuf_order
)
1103 * Check that we are not about to read the same subbuffer in
1104 * which the writer head is.
1106 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1111 * Failure to get the subbuffer causes a busy-loop retry without going
1112 * to a wait queue. These are caused by short-lived race windows where
1113 * the writer is getting access to a subbuffer we were trying to get
1114 * access to. Also checks that the "consumed" buffer count we are
1115 * looking for matches the one contained in the subbuffer id.
1117 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1118 consumed_idx
, buf_trunc_val(consumed
, chan
),
1122 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1124 buf
->get_subbuf_consumed
= consumed
;
1125 buf
->get_subbuf
= 1;
1131 * The memory barriers __wait_event()/wake_up_interruptible() take care
1132 * of "raw_spin_is_locked" memory ordering.
1141 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1144 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1145 struct lttng_ust_shm_handle
*handle
)
1147 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1148 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1149 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1150 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1152 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1154 if (!buf
->get_subbuf
) {
1156 * Reader puts a subbuffer it did not get.
1158 CHAN_WARN_ON(chan
, 1);
1161 consumed
= buf
->get_subbuf_consumed
;
1162 buf
->get_subbuf
= 0;
1165 * Clear the records_unread counter. (overruns counter)
1166 * Can still be non-zero if a file reader simply grabbed the data
1167 * without using iterators.
1168 * Can be below zero if an iterator is used on a snapshot more than
1171 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1172 v_add(config
, v_read(config
,
1173 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
1174 &bufb
->records_read
);
1175 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
1176 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1177 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1178 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1181 * Exchange the reader subbuffer with the one we put in its place in the
1182 * writer subbuffer table. Expect the original consumed count. If
1183 * update_read_sb_index fails, this is because the writer updated the
1184 * subbuffer concurrently. We should therefore keep the subbuffer we
1185 * currently have: it has become invalid to try reading this sub-buffer
1186 * consumed count value anyway.
1188 consumed_idx
= subbuf_index(consumed
, chan
);
1189 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1190 consumed_idx
, buf_trunc_val(consumed
, chan
),
1193 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1194 * if the writer concurrently updated it.
1199 * cons_offset is an iterator on all subbuffer offsets between the reader
1200 * position and the writer position. (inclusive)
1203 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1204 struct channel
*chan
,
1205 unsigned long cons_offset
,
1207 struct lttng_ust_shm_handle
*handle
)
1209 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1210 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1212 cons_idx
= subbuf_index(cons_offset
, chan
);
1213 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
1214 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
1216 if (subbuf_offset(commit_count
, chan
) != 0)
1217 DBG("ring buffer %s, cpu %d: "
1218 "commit count in subbuffer %lu,\n"
1219 "expecting multiples of %lu bytes\n"
1220 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1221 chan
->backend
.name
, cpu
, cons_idx
,
1222 chan
->backend
.subbuf_size
,
1223 commit_count
, commit_count_sb
);
1225 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1226 chan
->backend
.name
, cpu
, commit_count
);
1230 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1231 struct channel
*chan
,
1232 void *priv
, int cpu
,
1233 struct lttng_ust_shm_handle
*handle
)
1235 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1236 unsigned long write_offset
, cons_offset
;
1239 * No need to order commit_count, write_offset and cons_offset reads
1240 * because we execute at teardown when no more writer nor reader
1241 * references are left.
1243 write_offset
= v_read(config
, &buf
->offset
);
1244 cons_offset
= uatomic_read(&buf
->consumed
);
1245 if (write_offset
!= cons_offset
)
1246 DBG("ring buffer %s, cpu %d: "
1247 "non-consumed data\n"
1248 " [ %lu bytes written, %lu bytes read ]\n",
1249 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1251 for (cons_offset
= uatomic_read(&buf
->consumed
);
1252 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1255 cons_offset
= subbuf_align(cons_offset
, chan
))
1256 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1261 void lib_ring_buffer_print_errors(struct channel
*chan
,
1262 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1263 struct lttng_ust_shm_handle
*handle
)
1265 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1266 void *priv
= channel_get_private(chan
);
1268 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1269 DBG("ring buffer %s: %lu records written, "
1270 "%lu records overrun\n",
1272 v_read(config
, &buf
->records_count
),
1273 v_read(config
, &buf
->records_overrun
));
1275 DBG("ring buffer %s, cpu %d: %lu records written, "
1276 "%lu records overrun\n",
1277 chan
->backend
.name
, cpu
,
1278 v_read(config
, &buf
->records_count
),
1279 v_read(config
, &buf
->records_overrun
));
1281 if (v_read(config
, &buf
->records_lost_full
)
1282 || v_read(config
, &buf
->records_lost_wrap
)
1283 || v_read(config
, &buf
->records_lost_big
))
1284 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1285 " [ %lu buffer full, %lu nest buffer wrap-around, "
1286 "%lu event too big ]\n",
1287 chan
->backend
.name
, cpu
,
1288 v_read(config
, &buf
->records_lost_full
),
1289 v_read(config
, &buf
->records_lost_wrap
),
1290 v_read(config
, &buf
->records_lost_big
));
1292 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1296 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1298 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1301 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1302 struct channel
*chan
,
1303 struct switch_offsets
*offsets
,
1305 struct lttng_ust_shm_handle
*handle
)
1307 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1308 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1309 unsigned long commit_count
;
1311 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1314 * Order all writes to buffer before the commit count update that will
1315 * determine that the subbuffer is full.
1318 v_add(config
, config
->cb
.subbuffer_header_size(),
1319 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1320 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1321 /* Check if the written buffer has to be delivered */
1322 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1323 commit_count
, oldidx
, handle
);
1324 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1325 offsets
->old
, commit_count
,
1326 config
->cb
.subbuffer_header_size(),
1331 * lib_ring_buffer_switch_old_end: switch old subbuffer
1333 * Note : offset_old should never be 0 here. It is ok, because we never perform
1334 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1335 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1339 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1340 struct channel
*chan
,
1341 struct switch_offsets
*offsets
,
1343 struct lttng_ust_shm_handle
*handle
)
1345 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1346 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1347 unsigned long commit_count
, padding_size
, data_size
;
1349 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1350 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1351 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1355 * Order all writes to buffer before the commit count update that will
1356 * determine that the subbuffer is full.
1359 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1360 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1361 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1362 commit_count
, oldidx
, handle
);
1363 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1364 offsets
->old
, commit_count
,
1365 padding_size
, handle
);
1369 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1371 * This code can be executed unordered : writers may already have written to the
1372 * sub-buffer before this code gets executed, caution. The commit makes sure
1373 * that this code is executed before the deliver of this sub-buffer.
1376 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1377 struct channel
*chan
,
1378 struct switch_offsets
*offsets
,
1380 struct lttng_ust_shm_handle
*handle
)
1382 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1383 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1384 unsigned long commit_count
;
1386 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1389 * Order all writes to buffer before the commit count update that will
1390 * determine that the subbuffer is full.
1393 v_add(config
, config
->cb
.subbuffer_header_size(),
1394 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1395 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1396 /* Check if the written buffer has to be delivered */
1397 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1398 commit_count
, beginidx
, handle
);
1399 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1400 offsets
->begin
, commit_count
,
1401 config
->cb
.subbuffer_header_size(),
1406 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1408 * The only remaining threads could be the ones with pending commits. They will
1409 * have to do the deliver themselves.
1412 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1413 struct channel
*chan
,
1414 struct switch_offsets
*offsets
,
1416 struct lttng_ust_shm_handle
*handle
)
1418 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1419 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1420 unsigned long commit_count
, padding_size
, data_size
;
1422 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1423 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1424 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1428 * Order all writes to buffer before the commit count update that will
1429 * determine that the subbuffer is full.
1432 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1433 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1434 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1435 commit_count
, endidx
, handle
);
1436 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1437 offsets
->end
, commit_count
,
1438 padding_size
, handle
);
1444 * !0 if execution must be aborted.
1447 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1448 struct lttng_ust_lib_ring_buffer
*buf
,
1449 struct channel
*chan
,
1450 struct switch_offsets
*offsets
,
1453 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1456 offsets
->begin
= v_read(config
, &buf
->offset
);
1457 offsets
->old
= offsets
->begin
;
1458 offsets
->switch_old_start
= 0;
1459 off
= subbuf_offset(offsets
->begin
, chan
);
1461 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1464 * Ensure we flush the header of an empty subbuffer when doing the
1465 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1466 * total data gathering duration even if there were no records saved
1467 * after the last buffer switch.
1468 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1469 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1470 * subbuffer header as appropriate.
1471 * The next record that reserves space will be responsible for
1472 * populating the following subbuffer header. We choose not to populate
1473 * the next subbuffer header here because we want to be able to use
1474 * SWITCH_ACTIVE for periodical buffer flush, which must
1475 * guarantee that all the buffer content (records and header
1476 * timestamps) are visible to the reader. This is required for
1477 * quiescence guarantees for the fusion merge.
1479 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1480 if (caa_unlikely(off
== 0)) {
1482 * A final flush that encounters an empty
1483 * sub-buffer cannot switch buffer if a
1484 * reader is located within this sub-buffer.
1485 * Anyway, the purpose of final flushing of a
1486 * sub-buffer at offset 0 is to handle the case
1487 * of entirely empty stream.
1489 if (caa_unlikely(subbuf_trunc(offsets
->begin
, chan
)
1490 - subbuf_trunc((unsigned long)
1491 uatomic_read(&buf
->consumed
), chan
)
1492 >= chan
->backend
.buf_size
))
1495 * The client does not save any header information.
1496 * Don't switch empty subbuffer on finalize, because it
1497 * is invalid to deliver a completely empty subbuffer.
1499 if (!config
->cb
.subbuffer_header_size())
1502 * Need to write the subbuffer start header on finalize.
1504 offsets
->switch_old_start
= 1;
1506 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1508 return -1; /* we do not have to switch : buffer is empty */
1509 /* Note: old points to the next subbuf at offset 0 */
1510 offsets
->end
= offsets
->begin
;
1515 * Force a sub-buffer switch. This operation is completely reentrant : can be
1516 * called while tracing is active with absolutely no lock held.
1518 * Note, however, that as a v_cmpxchg is used for some atomic
1519 * operations, this function must be called from the CPU which owns the buffer
1520 * for a ACTIVE flush.
1522 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1523 struct lttng_ust_shm_handle
*handle
)
1525 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1526 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1527 struct switch_offsets offsets
;
1528 unsigned long oldidx
;
1534 * Perform retryable operations.
1537 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1539 return; /* Switch not needed */
1540 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1544 * Atomically update last_tsc. This update races against concurrent
1545 * atomic updates, but the race will always cause supplementary full TSC
1546 * records, never the opposite (missing a full TSC record when it would
1549 save_last_tsc(config
, buf
, tsc
);
1552 * Push the reader if necessary
1554 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1556 oldidx
= subbuf_index(offsets
.old
, chan
);
1557 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1560 * May need to populate header start on SWITCH_FLUSH.
1562 if (offsets
.switch_old_start
) {
1563 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1564 offsets
.old
+= config
->cb
.subbuffer_header_size();
1568 * Switch old subbuffer.
1570 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1576 * -ENOSPC if event size is too large for packet.
1577 * -ENOBUFS if there is currently not enough space in buffer for the event.
1578 * -EIO if data cannot be written into the buffer for any other reason.
1581 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1582 struct channel
*chan
,
1583 struct switch_offsets
*offsets
,
1584 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1586 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1587 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1588 unsigned long reserve_commit_diff
;
1590 offsets
->begin
= v_read(config
, &buf
->offset
);
1591 offsets
->old
= offsets
->begin
;
1592 offsets
->switch_new_start
= 0;
1593 offsets
->switch_new_end
= 0;
1594 offsets
->switch_old_end
= 0;
1595 offsets
->pre_header_padding
= 0;
1597 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1598 if ((int64_t) ctx
->tsc
== -EIO
)
1601 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1602 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1604 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1605 offsets
->switch_new_start
= 1; /* For offsets->begin */
1607 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1609 &offsets
->pre_header_padding
,
1612 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1615 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1616 offsets
->size
> chan
->backend
.subbuf_size
)) {
1617 offsets
->switch_old_end
= 1; /* For offsets->old */
1618 offsets
->switch_new_start
= 1; /* For offsets->begin */
1621 if (caa_unlikely(offsets
->switch_new_start
)) {
1622 unsigned long sb_index
;
1625 * We are typically not filling the previous buffer completely.
1627 if (caa_likely(offsets
->switch_old_end
))
1628 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1629 offsets
->begin
= offsets
->begin
1630 + config
->cb
.subbuffer_header_size();
1631 /* Test new buffer integrity */
1632 sb_index
= subbuf_index(offsets
->begin
, chan
);
1633 reserve_commit_diff
=
1634 (buf_trunc(offsets
->begin
, chan
)
1635 >> chan
->backend
.num_subbuf_order
)
1636 - ((unsigned long) v_read(config
,
1637 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1638 & chan
->commit_count_mask
);
1639 if (caa_likely(reserve_commit_diff
== 0)) {
1640 /* Next subbuffer not being written to. */
1641 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1642 subbuf_trunc(offsets
->begin
, chan
)
1643 - subbuf_trunc((unsigned long)
1644 uatomic_read(&buf
->consumed
), chan
)
1645 >= chan
->backend
.buf_size
)) {
1646 unsigned long nr_lost
;
1649 * We do not overwrite non consumed buffers
1650 * and we are full : record is lost.
1652 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1653 v_inc(config
, &buf
->records_lost_full
);
1654 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1655 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1656 nr_lost
+ 1, chan
->backend
.name
,
1662 * Next subbuffer not being written to, and we
1663 * are either in overwrite mode or the buffer is
1664 * not full. It's safe to write in this new
1669 unsigned long nr_lost
;
1672 * Next subbuffer reserve offset does not match the
1673 * commit offset. Drop record in producer-consumer and
1674 * overwrite mode. Caused by either a writer OOPS or too
1675 * many nested writes over a reserve/commit pair.
1677 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1678 v_inc(config
, &buf
->records_lost_wrap
);
1679 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1680 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1681 nr_lost
+ 1, chan
->backend
.name
,
1687 config
->cb
.record_header_size(config
, chan
,
1689 &offsets
->pre_header_padding
,
1692 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1695 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1696 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1697 unsigned long nr_lost
;
1700 * Record too big for subbuffers, report error, don't
1701 * complete the sub-buffer switch.
1703 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1704 v_inc(config
, &buf
->records_lost_big
);
1705 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1706 DBG("%lu or more records lost in (%s:%d) record size "
1707 " of %zu bytes is too large for buffer\n",
1708 nr_lost
+ 1, chan
->backend
.name
,
1709 buf
->backend
.cpu
, offsets
->size
);
1714 * We just made a successful buffer switch and the
1715 * record fits in the new subbuffer. Let's write.
1720 * Record fits in the current buffer and we are not on a switch
1721 * boundary. It's safe to write.
1724 offsets
->end
= offsets
->begin
+ offsets
->size
;
1726 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1728 * The offset_end will fall at the very beginning of the next
1731 offsets
->switch_new_end
= 1; /* For offsets->begin */
1737 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1738 * @ctx: ring buffer context.
1740 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1741 * -EIO for other errors, else returns 0.
1742 * It will take care of sub-buffer switching.
1744 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1746 struct channel
*chan
= ctx
->chan
;
1747 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1748 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1749 struct lttng_ust_lib_ring_buffer
*buf
;
1750 struct switch_offsets offsets
;
1753 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1754 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1756 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1762 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1764 if (caa_unlikely(ret
))
1766 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1771 * Atomically update last_tsc. This update races against concurrent
1772 * atomic updates, but the race will always cause supplementary full TSC
1773 * records, never the opposite (missing a full TSC record when it would
1776 save_last_tsc(config
, buf
, ctx
->tsc
);
1779 * Push the reader if necessary
1781 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1784 * Clear noref flag for this subbuffer.
1786 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1787 subbuf_index(offsets
.end
- 1, chan
),
1791 * Switch old subbuffer if needed.
1793 if (caa_unlikely(offsets
.switch_old_end
)) {
1794 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1795 subbuf_index(offsets
.old
- 1, chan
),
1797 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1801 * Populate new subbuffer.
1803 if (caa_unlikely(offsets
.switch_new_start
))
1804 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1806 if (caa_unlikely(offsets
.switch_new_end
))
1807 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1809 ctx
->slot_size
= offsets
.size
;
1810 ctx
->pre_offset
= offsets
.begin
;
1811 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1816 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1818 void lttng_fixup_ringbuffer_tls(void)
1820 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
1823 void lib_ringbuffer_signal_init(void)
1829 * Block signal for entire process, so only our thread processes
1833 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
1836 PERROR("pthread_sigmask");