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 void lib_ring_buffer_print_errors(struct channel
*chan
,
117 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
118 struct lttng_ust_shm_handle
*handle
);
121 * Handle timer teardown race wrt memory free of private data by
122 * ring buffer signals are handled by a single thread, which permits
123 * a synchronization point between handling of each signal.
124 * Protected by the ust mutex.
126 struct timer_signal_data
{
127 pthread_t tid
; /* thread id managing signals */
132 static struct timer_signal_data timer_signal
;
135 * lib_ring_buffer_reset - Reset ring buffer to initial values.
138 * Effectively empty the ring buffer. Should be called when the buffer is not
139 * used for writing. The ring buffer can be opened for reading, but the reader
140 * should not be using the iterator concurrently with reset. The previous
141 * current iterator record is reset.
143 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
144 struct lttng_ust_shm_handle
*handle
)
146 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
147 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
151 * Reset iterator first. It will put the subbuffer if it currently holds
154 v_set(config
, &buf
->offset
, 0);
155 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
156 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
157 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
158 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
160 uatomic_set(&buf
->consumed
, 0);
161 uatomic_set(&buf
->record_disabled
, 0);
162 v_set(config
, &buf
->last_tsc
, 0);
163 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
164 /* Don't reset number of active readers */
165 v_set(config
, &buf
->records_lost_full
, 0);
166 v_set(config
, &buf
->records_lost_wrap
, 0);
167 v_set(config
, &buf
->records_lost_big
, 0);
168 v_set(config
, &buf
->records_count
, 0);
169 v_set(config
, &buf
->records_overrun
, 0);
174 * channel_reset - Reset channel to initial values.
177 * Effectively empty the channel. Should be called when the channel is not used
178 * for writing. The channel can be opened for reading, but the reader should not
179 * be using the iterator concurrently with reset. The previous current iterator
182 void channel_reset(struct channel
*chan
)
185 * Reset iterators first. Will put the subbuffer if held for reading.
187 uatomic_set(&chan
->record_disabled
, 0);
188 /* Don't reset commit_count_mask, still valid */
189 channel_backend_reset(&chan
->backend
);
190 /* Don't reset switch/read timer interval */
191 /* Don't reset notifiers and notifier enable bits */
192 /* Don't reset reader reference count */
196 * Must be called under cpu hotplug protection.
198 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
199 struct channel_backend
*chanb
, int cpu
,
200 struct lttng_ust_shm_handle
*handle
,
201 struct shm_object
*shmobj
)
203 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
204 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
205 void *priv
= channel_get_private(chan
);
206 size_t subbuf_header_size
;
210 /* Test for cpu hotplug */
211 if (buf
->backend
.allocated
)
214 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
215 cpu
, handle
, shmobj
);
219 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
220 set_shmp(buf
->commit_hot
,
222 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
223 if (!shmp(handle
, buf
->commit_hot
)) {
228 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
229 set_shmp(buf
->commit_cold
,
231 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
232 if (!shmp(handle
, buf
->commit_cold
)) {
238 * Write the subbuffer header for first subbuffer so we know the total
239 * duration of data gathering.
241 subbuf_header_size
= config
->cb
.subbuffer_header_size();
242 v_set(config
, &buf
->offset
, subbuf_header_size
);
243 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
244 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
245 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
246 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
248 if (config
->cb
.buffer_create
) {
249 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
253 buf
->backend
.allocated
= 1;
258 /* commit_cold will be freed by shm teardown */
260 /* commit_hot will be freed by shm teardown */
266 static void switch_buffer_timer(unsigned long data
)
268 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
269 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
270 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
273 * Only flush buffers periodically if readers are active.
275 if (uatomic_read(&buf
->active_readers
))
276 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
279 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
280 // mod_timer_pinned(&buf->switch_timer,
281 // jiffies + chan->switch_timer_interval);
283 // mod_timer(&buf->switch_timer,
284 // jiffies + chan->switch_timer_interval);
289 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
291 const struct lttng_ust_lib_ring_buffer_config
*config
;
292 struct lttng_ust_shm_handle
*handle
;
293 struct channel
*chan
;
296 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
298 chan
= si
->si_value
.sival_ptr
;
299 handle
= chan
->handle
;
300 config
= &chan
->backend
.config
;
302 DBG("Timer for channel %p\n", chan
);
304 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
305 for_each_possible_cpu(cpu
) {
306 struct lttng_ust_lib_ring_buffer
*buf
=
307 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
309 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
313 struct lttng_ust_lib_ring_buffer
*buf
=
314 shmp(handle
, chan
->backend
.buf
[0].shmp
);
316 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
323 void rb_setmask(sigset_t
*mask
)
327 ret
= sigemptyset(mask
);
329 PERROR("sigemptyset");
331 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG
);
335 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
342 void *sig_thread(void *arg
)
348 /* Only self thread will receive signal mask. */
350 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
353 signr
= sigwaitinfo(&mask
, &info
);
355 PERROR("sigwaitinfo");
358 if (signr
== LTTNG_UST_RB_SIG
) {
359 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
361 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
363 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
366 ERR("Unexptected signal %d\n", info
.si_signo
);
373 * Called with ust_lock() held.
374 * Ensure only a single thread listens on the timer signal.
377 void lib_ring_buffer_setup_timer_thread(void)
382 if (timer_signal
.setup_done
)
385 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
388 PERROR("pthread_create");
390 ret
= pthread_detach(thread
);
393 PERROR("pthread_detach");
395 timer_signal
.setup_done
= 1;
399 * Called with ust_lock() held.
402 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
405 struct itimerspec its
;
408 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
411 chan
->switch_timer_enabled
= 1;
413 lib_ring_buffer_setup_timer_thread();
415 sev
.sigev_notify
= SIGEV_SIGNAL
;
416 sev
.sigev_signo
= LTTNG_UST_RB_SIG
;
417 sev
.sigev_value
.sival_ptr
= chan
;
418 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
420 PERROR("timer_create");
423 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
424 its
.it_value
.tv_nsec
= chan
->switch_timer_interval
% 1000000;
425 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
426 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
428 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
430 PERROR("timer_settime");
435 * Called with ust_lock() held.
438 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
440 sigset_t pending_set
;
441 int sig_is_pending
, ret
;
443 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
446 ret
= timer_delete(chan
->switch_timer
);
448 PERROR("timer_delete");
452 * Ensure we don't have any signal queued for this channel.
455 ret
= sigemptyset(&pending_set
);
457 PERROR("sigemptyset");
459 ret
= sigpending(&pending_set
);
461 PERROR("sigpending");
463 sig_is_pending
= sigismember(&pending_set
, LTTNG_UST_RB_SIG
);
470 * From this point, no new signal handler will be fired that
471 * would try to access "chan". However, we still need to wait
472 * for any currently executing handler to complete.
475 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
479 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
482 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
484 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
488 chan
->switch_timer
= 0;
489 chan
->switch_timer_enabled
= 0;
494 * Polling timer to check the channels for data.
496 static void read_buffer_timer(unsigned long data
)
498 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
499 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
500 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
502 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
504 if (uatomic_read(&buf
->active_readers
))
505 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
507 //wake_up_interruptible(&buf->read_wait);
508 //wake_up_interruptible(&chan->read_wait);
512 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
513 // mod_timer_pinned(&buf->read_timer,
514 // jiffies + chan->read_timer_interval);
516 // mod_timer(&buf->read_timer,
517 // jiffies + chan->read_timer_interval);
521 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
522 struct lttng_ust_shm_handle
*handle
)
524 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
525 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
527 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
528 || !chan
->read_timer_interval
529 || buf
->read_timer_enabled
)
533 //init_timer(&buf->read_timer);
534 //buf->read_timer.function = read_buffer_timer;
535 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
536 //buf->read_timer.data = (unsigned long)buf;
538 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
539 // add_timer_on(&buf->read_timer, buf->backend.cpu);
541 // add_timer(&buf->read_timer);
542 buf
->read_timer_enabled
= 1;
545 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
546 struct lttng_ust_shm_handle
*handle
)
548 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
549 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
551 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
552 || !chan
->read_timer_interval
553 || !buf
->read_timer_enabled
)
557 //del_timer_sync(&buf->read_timer);
559 * do one more check to catch data that has been written in the last
562 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
564 //wake_up_interruptible(&buf->read_wait);
565 //wake_up_interruptible(&chan->read_wait);
567 buf
->read_timer_enabled
= 0;
570 static void channel_unregister_notifiers(struct channel
*chan
,
571 struct lttng_ust_shm_handle
*handle
)
573 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
576 lib_ring_buffer_channel_switch_timer_stop(chan
);
577 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
578 for_each_possible_cpu(cpu
) {
579 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
581 lib_ring_buffer_stop_read_timer(buf
, handle
);
584 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
586 lib_ring_buffer_stop_read_timer(buf
, handle
);
588 //channel_backend_unregister_notifiers(&chan->backend);
591 static void channel_print_errors(struct channel
*chan
,
592 struct lttng_ust_shm_handle
*handle
)
594 const struct lttng_ust_lib_ring_buffer_config
*config
=
595 &chan
->backend
.config
;
598 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
599 for_each_possible_cpu(cpu
) {
600 struct lttng_ust_lib_ring_buffer
*buf
=
601 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
602 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
605 struct lttng_ust_lib_ring_buffer
*buf
=
606 shmp(handle
, chan
->backend
.buf
[0].shmp
);
608 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
612 static void channel_free(struct channel
*chan
,
613 struct lttng_ust_shm_handle
*handle
)
615 channel_print_errors(chan
, handle
);
616 channel_backend_free(&chan
->backend
, handle
);
617 /* chan is freed by shm teardown */
618 shm_object_table_destroy(handle
->table
);
623 * channel_create - Create channel.
624 * @config: ring buffer instance configuration
625 * @name: name of the channel
626 * @priv_data: ring buffer client private data area pointer (output)
627 * @priv_data_size: length, in bytes, of the private data area.
628 * @priv_data_init: initialization data for private data.
629 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
630 * address mapping. It is used only by RING_BUFFER_STATIC
631 * configuration. It can be set to NULL for other backends.
632 * @subbuf_size: subbuffer size
633 * @num_subbuf: number of subbuffers
634 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
635 * padding to let readers get those sub-buffers.
636 * Used for live streaming.
637 * @read_timer_interval: Time interval (in us) to wake up pending readers.
640 * Returns NULL on failure.
642 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
645 size_t priv_data_align
,
646 size_t priv_data_size
,
647 void *priv_data_init
,
648 void *buf_addr
, size_t subbuf_size
,
649 size_t num_subbuf
, unsigned int switch_timer_interval
,
650 unsigned int read_timer_interval
)
653 size_t shmsize
, chansize
;
654 struct channel
*chan
;
655 struct lttng_ust_shm_handle
*handle
;
656 struct shm_object
*shmobj
;
657 unsigned int nr_streams
;
659 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
660 nr_streams
= num_possible_cpus();
664 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
665 read_timer_interval
))
668 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
672 /* Allocate table for channel + per-cpu buffers */
673 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
675 goto error_table_alloc
;
677 /* Calculate the shm allocation layout */
678 shmsize
= sizeof(struct channel
);
679 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
680 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
683 shmsize
+= offset_align(shmsize
, priv_data_align
);
684 shmsize
+= priv_data_size
;
686 /* Allocate normal memory for channel (not shared) */
687 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
);
690 /* struct channel is at object 0, offset 0 (hardcoded) */
691 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
692 assert(handle
->chan
._ref
.index
== 0);
693 assert(handle
->chan
._ref
.offset
== 0);
694 chan
= shmp(handle
, handle
->chan
);
697 chan
->nr_streams
= nr_streams
;
699 /* space for private data */
700 if (priv_data_size
) {
701 DECLARE_SHMP(void, priv_data_alloc
);
703 align_shm(shmobj
, priv_data_align
);
704 chan
->priv_data_offset
= shmobj
->allocated_len
;
705 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
706 if (!shmp(handle
, priv_data_alloc
))
708 *priv_data
= channel_get_private(chan
);
709 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
711 chan
->priv_data_offset
= -1;
716 ret
= channel_backend_init(&chan
->backend
, name
, config
,
717 subbuf_size
, num_subbuf
, handle
);
719 goto error_backend_init
;
721 chan
->handle
= handle
;
722 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
723 chan
->switch_timer_interval
= switch_timer_interval
;
726 //chan->read_timer_interval = read_timer_interval;
727 //init_waitqueue_head(&chan->read_wait);
728 //init_waitqueue_head(&chan->hp_wait);
730 lib_ring_buffer_channel_switch_timer_start(chan
);
731 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
733 * In case of non-hotplug cpu, if the ring-buffer is allocated
734 * in early initcall, it will not be notified of secondary cpus.
735 * In that off case, we need to allocate for all possible cpus.
737 for_each_possible_cpu(cpu
) {
738 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
739 lib_ring_buffer_start_read_timer(buf
, handle
);
742 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
744 lib_ring_buffer_start_read_timer(buf
, handle
);
750 shm_object_table_destroy(handle
->table
);
756 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
757 uint64_t memory_map_size
)
759 struct lttng_ust_shm_handle
*handle
;
760 struct shm_object
*object
;
762 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
766 /* Allocate table for channel + per-cpu buffers */
767 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
769 goto error_table_alloc
;
770 /* Add channel object */
771 object
= shm_object_table_append_mem(handle
->table
, data
,
774 goto error_table_object
;
775 /* struct channel is at object 0, offset 0 (hardcoded) */
776 handle
->chan
._ref
.index
= 0;
777 handle
->chan
._ref
.offset
= 0;
781 shm_object_table_destroy(handle
->table
);
787 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
788 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
789 uint64_t memory_map_size
)
791 struct shm_object
*object
;
793 /* Add stream object */
794 object
= shm_object_table_append_shm(handle
->table
,
795 shm_fd
, wakeup_fd
, stream_nr
,
802 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
804 assert(handle
->table
);
805 return handle
->table
->allocated_len
- 1;
809 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
811 channel_free(chan
, handle
);
815 * channel_destroy - Finalize, wait for q.s. and destroy channel.
816 * @chan: channel to destroy
819 * Call "destroy" callback, finalize channels, decrement the channel
820 * reference count. Note that when readers have completed data
821 * consumption of finalized channels, get_subbuf() will return -ENODATA.
822 * They should release their handle at that point.
824 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
829 * Note: the consumer takes care of finalizing and
830 * switching the buffers.
832 channel_unregister_notifiers(chan
, handle
);
836 * sessiond/consumer are keeping a reference on the shm file
837 * descriptor directly. No need to refcount.
839 channel_release(chan
, handle
);
843 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
844 const struct lttng_ust_lib_ring_buffer_config
*config
,
845 struct channel
*chan
, int cpu
,
846 struct lttng_ust_shm_handle
*handle
,
847 int *shm_fd
, int *wait_fd
,
849 uint64_t *memory_map_size
)
853 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
856 if (cpu
>= num_possible_cpus())
859 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
860 *shm_fd
= shm_get_shm_fd(handle
, ref
);
861 *wait_fd
= shm_get_wait_fd(handle
, ref
);
862 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
863 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
865 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
868 int ring_buffer_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
869 struct channel
*chan
,
870 struct lttng_ust_shm_handle
*handle
,
875 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
878 if (cpu
>= num_possible_cpus())
881 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
882 return shm_close_wait_fd(handle
, ref
);
885 int ring_buffer_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
886 struct channel
*chan
,
887 struct lttng_ust_shm_handle
*handle
,
892 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
895 if (cpu
>= num_possible_cpus())
898 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
899 return shm_close_wakeup_fd(handle
, ref
);
902 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
903 struct lttng_ust_shm_handle
*handle
)
905 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
911 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
912 struct lttng_ust_shm_handle
*handle
)
914 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
916 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
918 uatomic_dec(&buf
->active_readers
);
922 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
924 * @consumed: consumed count indicating the position where to read
925 * @produced: produced count, indicates position when to stop reading
927 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
928 * data to read at consumed position, or 0 if the get operation succeeds.
931 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
932 unsigned long *consumed
, unsigned long *produced
,
933 struct lttng_ust_shm_handle
*handle
)
935 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
936 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
937 unsigned long consumed_cur
, write_offset
;
940 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
942 * Read finalized before counters.
945 consumed_cur
= uatomic_read(&buf
->consumed
);
947 * No need to issue a memory barrier between consumed count read and
948 * write offset read, because consumed count can only change
949 * concurrently in overwrite mode, and we keep a sequence counter
950 * identifier derived from the write offset to check we are getting
951 * the same sub-buffer we are expecting (the sub-buffers are atomically
952 * "tagged" upon writes, tags are checked upon read).
954 write_offset
= v_read(config
, &buf
->offset
);
957 * Check that we are not about to read the same subbuffer in
958 * which the writer head is.
960 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
964 *consumed
= consumed_cur
;
965 *produced
= subbuf_trunc(write_offset
, chan
);
971 * The memory barriers __wait_event()/wake_up_interruptible() take care
972 * of "raw_spin_is_locked" memory ordering.
981 * lib_ring_buffer_put_snapshot - move consumed counter forward
983 * @consumed_new: new consumed count value
985 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
986 unsigned long consumed_new
,
987 struct lttng_ust_shm_handle
*handle
)
989 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
990 struct channel
*chan
= shmp(handle
, bufb
->chan
);
991 unsigned long consumed
;
993 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
996 * Only push the consumed value forward.
997 * If the consumed cmpxchg fails, this is because we have been pushed by
998 * the writer in flight recorder mode.
1000 consumed
= uatomic_read(&buf
->consumed
);
1001 while ((long) consumed
- (long) consumed_new
< 0)
1002 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1007 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1009 * @consumed: consumed count indicating the position where to read
1011 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1012 * data to read at consumed position, or 0 if the get operation succeeds.
1014 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1015 unsigned long consumed
,
1016 struct lttng_ust_shm_handle
*handle
)
1018 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1019 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1020 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1025 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1027 * Read finalized before counters.
1030 consumed_cur
= uatomic_read(&buf
->consumed
);
1031 consumed_idx
= subbuf_index(consumed
, chan
);
1032 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
1034 * Make sure we read the commit count before reading the buffer
1035 * data and the write offset. Correct consumed offset ordering
1036 * wrt commit count is insured by the use of cmpxchg to update
1037 * the consumed offset.
1040 * Local rmb to match the remote wmb to read the commit count
1041 * before the buffer data and the write offset.
1045 write_offset
= v_read(config
, &buf
->offset
);
1048 * Check that the buffer we are getting is after or at consumed_cur
1051 if ((long) subbuf_trunc(consumed
, chan
)
1052 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1056 * Check that the subbuffer we are trying to consume has been
1057 * already fully committed.
1059 if (((commit_count
- chan
->backend
.subbuf_size
)
1060 & chan
->commit_count_mask
)
1061 - (buf_trunc(consumed_cur
, chan
)
1062 >> chan
->backend
.num_subbuf_order
)
1067 * Check that we are not about to read the same subbuffer in
1068 * which the writer head is.
1070 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1075 * Failure to get the subbuffer causes a busy-loop retry without going
1076 * to a wait queue. These are caused by short-lived race windows where
1077 * the writer is getting access to a subbuffer we were trying to get
1078 * access to. Also checks that the "consumed" buffer count we are
1079 * looking for matches the one contained in the subbuffer id.
1081 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1082 consumed_idx
, buf_trunc_val(consumed
, chan
),
1086 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1088 buf
->get_subbuf_consumed
= consumed
;
1089 buf
->get_subbuf
= 1;
1095 * The memory barriers __wait_event()/wake_up_interruptible() take care
1096 * of "raw_spin_is_locked" memory ordering.
1105 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1108 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1109 struct lttng_ust_shm_handle
*handle
)
1111 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1112 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1113 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1114 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1116 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1118 if (!buf
->get_subbuf
) {
1120 * Reader puts a subbuffer it did not get.
1122 CHAN_WARN_ON(chan
, 1);
1125 consumed
= buf
->get_subbuf_consumed
;
1126 buf
->get_subbuf
= 0;
1129 * Clear the records_unread counter. (overruns counter)
1130 * Can still be non-zero if a file reader simply grabbed the data
1131 * without using iterators.
1132 * Can be below zero if an iterator is used on a snapshot more than
1135 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1136 v_add(config
, v_read(config
,
1137 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
1138 &bufb
->records_read
);
1139 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
1140 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1141 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1142 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1145 * Exchange the reader subbuffer with the one we put in its place in the
1146 * writer subbuffer table. Expect the original consumed count. If
1147 * update_read_sb_index fails, this is because the writer updated the
1148 * subbuffer concurrently. We should therefore keep the subbuffer we
1149 * currently have: it has become invalid to try reading this sub-buffer
1150 * consumed count value anyway.
1152 consumed_idx
= subbuf_index(consumed
, chan
);
1153 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1154 consumed_idx
, buf_trunc_val(consumed
, chan
),
1157 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1158 * if the writer concurrently updated it.
1163 * cons_offset is an iterator on all subbuffer offsets between the reader
1164 * position and the writer position. (inclusive)
1167 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1168 struct channel
*chan
,
1169 unsigned long cons_offset
,
1171 struct lttng_ust_shm_handle
*handle
)
1173 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1174 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1176 cons_idx
= subbuf_index(cons_offset
, chan
);
1177 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
1178 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
1180 if (subbuf_offset(commit_count
, chan
) != 0)
1181 DBG("ring buffer %s, cpu %d: "
1182 "commit count in subbuffer %lu,\n"
1183 "expecting multiples of %lu bytes\n"
1184 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1185 chan
->backend
.name
, cpu
, cons_idx
,
1186 chan
->backend
.subbuf_size
,
1187 commit_count
, commit_count_sb
);
1189 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1190 chan
->backend
.name
, cpu
, commit_count
);
1194 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1195 struct channel
*chan
,
1196 void *priv
, int cpu
,
1197 struct lttng_ust_shm_handle
*handle
)
1199 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1200 unsigned long write_offset
, cons_offset
;
1203 * No need to order commit_count, write_offset and cons_offset reads
1204 * because we execute at teardown when no more writer nor reader
1205 * references are left.
1207 write_offset
= v_read(config
, &buf
->offset
);
1208 cons_offset
= uatomic_read(&buf
->consumed
);
1209 if (write_offset
!= cons_offset
)
1210 DBG("ring buffer %s, cpu %d: "
1211 "non-consumed data\n"
1212 " [ %lu bytes written, %lu bytes read ]\n",
1213 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1215 for (cons_offset
= uatomic_read(&buf
->consumed
);
1216 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1219 cons_offset
= subbuf_align(cons_offset
, chan
))
1220 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1225 void lib_ring_buffer_print_errors(struct channel
*chan
,
1226 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1227 struct lttng_ust_shm_handle
*handle
)
1229 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1230 void *priv
= channel_get_private(chan
);
1232 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1233 DBG("ring buffer %s: %lu records written, "
1234 "%lu records overrun\n",
1236 v_read(config
, &buf
->records_count
),
1237 v_read(config
, &buf
->records_overrun
));
1239 DBG("ring buffer %s, cpu %d: %lu records written, "
1240 "%lu records overrun\n",
1241 chan
->backend
.name
, cpu
,
1242 v_read(config
, &buf
->records_count
),
1243 v_read(config
, &buf
->records_overrun
));
1245 if (v_read(config
, &buf
->records_lost_full
)
1246 || v_read(config
, &buf
->records_lost_wrap
)
1247 || v_read(config
, &buf
->records_lost_big
))
1248 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1249 " [ %lu buffer full, %lu nest buffer wrap-around, "
1250 "%lu event too big ]\n",
1251 chan
->backend
.name
, cpu
,
1252 v_read(config
, &buf
->records_lost_full
),
1253 v_read(config
, &buf
->records_lost_wrap
),
1254 v_read(config
, &buf
->records_lost_big
));
1256 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1260 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1262 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1265 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1266 struct channel
*chan
,
1267 struct switch_offsets
*offsets
,
1269 struct lttng_ust_shm_handle
*handle
)
1271 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1272 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1273 unsigned long commit_count
;
1275 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1278 * Order all writes to buffer before the commit count update that will
1279 * determine that the subbuffer is full.
1282 v_add(config
, config
->cb
.subbuffer_header_size(),
1283 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1284 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1285 /* Check if the written buffer has to be delivered */
1286 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1287 commit_count
, oldidx
, handle
);
1288 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1289 offsets
->old
, commit_count
,
1290 config
->cb
.subbuffer_header_size(),
1295 * lib_ring_buffer_switch_old_end: switch old subbuffer
1297 * Note : offset_old should never be 0 here. It is ok, because we never perform
1298 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1299 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1303 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1304 struct channel
*chan
,
1305 struct switch_offsets
*offsets
,
1307 struct lttng_ust_shm_handle
*handle
)
1309 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1310 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1311 unsigned long commit_count
, padding_size
, data_size
;
1313 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1314 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1315 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1319 * Order all writes to buffer before the commit count update that will
1320 * determine that the subbuffer is full.
1323 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1324 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1325 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1326 commit_count
, oldidx
, handle
);
1327 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1328 offsets
->old
, commit_count
,
1329 padding_size
, handle
);
1333 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1335 * This code can be executed unordered : writers may already have written to the
1336 * sub-buffer before this code gets executed, caution. The commit makes sure
1337 * that this code is executed before the deliver of this sub-buffer.
1340 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1341 struct channel
*chan
,
1342 struct switch_offsets
*offsets
,
1344 struct lttng_ust_shm_handle
*handle
)
1346 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1347 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1348 unsigned long commit_count
;
1350 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1353 * Order all writes to buffer before the commit count update that will
1354 * determine that the subbuffer is full.
1357 v_add(config
, config
->cb
.subbuffer_header_size(),
1358 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1359 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1360 /* Check if the written buffer has to be delivered */
1361 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1362 commit_count
, beginidx
, handle
);
1363 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1364 offsets
->begin
, commit_count
,
1365 config
->cb
.subbuffer_header_size(),
1370 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1372 * The only remaining threads could be the ones with pending commits. They will
1373 * have to do the deliver themselves.
1376 void lib_ring_buffer_switch_new_end(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 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1384 unsigned long commit_count
, padding_size
, data_size
;
1386 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1387 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1388 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1392 * Order all writes to buffer before the commit count update that will
1393 * determine that the subbuffer is full.
1396 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1397 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1398 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1399 commit_count
, endidx
, handle
);
1400 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1401 offsets
->end
, commit_count
,
1402 padding_size
, handle
);
1408 * !0 if execution must be aborted.
1411 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1412 struct lttng_ust_lib_ring_buffer
*buf
,
1413 struct channel
*chan
,
1414 struct switch_offsets
*offsets
,
1417 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1420 offsets
->begin
= v_read(config
, &buf
->offset
);
1421 offsets
->old
= offsets
->begin
;
1422 offsets
->switch_old_start
= 0;
1423 off
= subbuf_offset(offsets
->begin
, chan
);
1425 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1428 * Ensure we flush the header of an empty subbuffer when doing the
1429 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1430 * total data gathering duration even if there were no records saved
1431 * after the last buffer switch.
1432 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1433 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1434 * subbuffer header as appropriate.
1435 * The next record that reserves space will be responsible for
1436 * populating the following subbuffer header. We choose not to populate
1437 * the next subbuffer header here because we want to be able to use
1438 * SWITCH_ACTIVE for periodical buffer flush, which must
1439 * guarantee that all the buffer content (records and header
1440 * timestamps) are visible to the reader. This is required for
1441 * quiescence guarantees for the fusion merge.
1443 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1444 if (caa_unlikely(off
== 0)) {
1446 * A final flush that encounters an empty
1447 * sub-buffer cannot switch buffer if a
1448 * reader is located within this sub-buffer.
1449 * Anyway, the purpose of final flushing of a
1450 * sub-buffer at offset 0 is to handle the case
1451 * of entirely empty stream.
1453 if (caa_unlikely(subbuf_trunc(offsets
->begin
, chan
)
1454 - subbuf_trunc((unsigned long)
1455 uatomic_read(&buf
->consumed
), chan
)
1456 >= chan
->backend
.buf_size
))
1459 * The client does not save any header information.
1460 * Don't switch empty subbuffer on finalize, because it
1461 * is invalid to deliver a completely empty subbuffer.
1463 if (!config
->cb
.subbuffer_header_size())
1466 * Need to write the subbuffer start header on finalize.
1468 offsets
->switch_old_start
= 1;
1470 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1472 return -1; /* we do not have to switch : buffer is empty */
1473 /* Note: old points to the next subbuf at offset 0 */
1474 offsets
->end
= offsets
->begin
;
1479 * Force a sub-buffer switch. This operation is completely reentrant : can be
1480 * called while tracing is active with absolutely no lock held.
1482 * Note, however, that as a v_cmpxchg is used for some atomic
1483 * operations, this function must be called from the CPU which owns the buffer
1484 * for a ACTIVE flush.
1486 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1487 struct lttng_ust_shm_handle
*handle
)
1489 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1490 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1491 struct switch_offsets offsets
;
1492 unsigned long oldidx
;
1498 * Perform retryable operations.
1501 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1503 return; /* Switch not needed */
1504 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1508 * Atomically update last_tsc. This update races against concurrent
1509 * atomic updates, but the race will always cause supplementary full TSC
1510 * records, never the opposite (missing a full TSC record when it would
1513 save_last_tsc(config
, buf
, tsc
);
1516 * Push the reader if necessary
1518 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1520 oldidx
= subbuf_index(offsets
.old
, chan
);
1521 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1524 * May need to populate header start on SWITCH_FLUSH.
1526 if (offsets
.switch_old_start
) {
1527 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1528 offsets
.old
+= config
->cb
.subbuffer_header_size();
1532 * Switch old subbuffer.
1534 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1540 * -ENOSPC if event size is too large for packet.
1541 * -ENOBUFS if there is currently not enough space in buffer for the event.
1542 * -EIO if data cannot be written into the buffer for any other reason.
1545 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1546 struct channel
*chan
,
1547 struct switch_offsets
*offsets
,
1548 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1550 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1551 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1552 unsigned long reserve_commit_diff
;
1554 offsets
->begin
= v_read(config
, &buf
->offset
);
1555 offsets
->old
= offsets
->begin
;
1556 offsets
->switch_new_start
= 0;
1557 offsets
->switch_new_end
= 0;
1558 offsets
->switch_old_end
= 0;
1559 offsets
->pre_header_padding
= 0;
1561 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1562 if ((int64_t) ctx
->tsc
== -EIO
)
1565 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1566 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1568 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1569 offsets
->switch_new_start
= 1; /* For offsets->begin */
1571 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1573 &offsets
->pre_header_padding
,
1576 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1579 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1580 offsets
->size
> chan
->backend
.subbuf_size
)) {
1581 offsets
->switch_old_end
= 1; /* For offsets->old */
1582 offsets
->switch_new_start
= 1; /* For offsets->begin */
1585 if (caa_unlikely(offsets
->switch_new_start
)) {
1586 unsigned long sb_index
;
1589 * We are typically not filling the previous buffer completely.
1591 if (caa_likely(offsets
->switch_old_end
))
1592 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1593 offsets
->begin
= offsets
->begin
1594 + config
->cb
.subbuffer_header_size();
1595 /* Test new buffer integrity */
1596 sb_index
= subbuf_index(offsets
->begin
, chan
);
1597 reserve_commit_diff
=
1598 (buf_trunc(offsets
->begin
, chan
)
1599 >> chan
->backend
.num_subbuf_order
)
1600 - ((unsigned long) v_read(config
,
1601 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1602 & chan
->commit_count_mask
);
1603 if (caa_likely(reserve_commit_diff
== 0)) {
1604 /* Next subbuffer not being written to. */
1605 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1606 subbuf_trunc(offsets
->begin
, chan
)
1607 - subbuf_trunc((unsigned long)
1608 uatomic_read(&buf
->consumed
), chan
)
1609 >= chan
->backend
.buf_size
)) {
1610 unsigned long nr_lost
;
1613 * We do not overwrite non consumed buffers
1614 * and we are full : record is lost.
1616 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1617 v_inc(config
, &buf
->records_lost_full
);
1618 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1619 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1620 nr_lost
+ 1, chan
->backend
.name
,
1626 * Next subbuffer not being written to, and we
1627 * are either in overwrite mode or the buffer is
1628 * not full. It's safe to write in this new
1633 unsigned long nr_lost
;
1636 * Next subbuffer reserve offset does not match the
1637 * commit offset. Drop record in producer-consumer and
1638 * overwrite mode. Caused by either a writer OOPS or too
1639 * many nested writes over a reserve/commit pair.
1641 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1642 v_inc(config
, &buf
->records_lost_wrap
);
1643 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1644 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1645 nr_lost
+ 1, chan
->backend
.name
,
1651 config
->cb
.record_header_size(config
, chan
,
1653 &offsets
->pre_header_padding
,
1656 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1659 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1660 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1661 unsigned long nr_lost
;
1664 * Record too big for subbuffers, report error, don't
1665 * complete the sub-buffer switch.
1667 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1668 v_inc(config
, &buf
->records_lost_big
);
1669 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1670 DBG("%lu or more records lost in (%s:%d) record size "
1671 " of %zu bytes is too large for buffer\n",
1672 nr_lost
+ 1, chan
->backend
.name
,
1673 buf
->backend
.cpu
, offsets
->size
);
1678 * We just made a successful buffer switch and the
1679 * record fits in the new subbuffer. Let's write.
1684 * Record fits in the current buffer and we are not on a switch
1685 * boundary. It's safe to write.
1688 offsets
->end
= offsets
->begin
+ offsets
->size
;
1690 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1692 * The offset_end will fall at the very beginning of the next
1695 offsets
->switch_new_end
= 1; /* For offsets->begin */
1701 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1702 * @ctx: ring buffer context.
1704 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1705 * -EIO for other errors, else returns 0.
1706 * It will take care of sub-buffer switching.
1708 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1710 struct channel
*chan
= ctx
->chan
;
1711 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1712 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1713 struct lttng_ust_lib_ring_buffer
*buf
;
1714 struct switch_offsets offsets
;
1717 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1718 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1720 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1726 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1728 if (caa_unlikely(ret
))
1730 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1735 * Atomically update last_tsc. This update races against concurrent
1736 * atomic updates, but the race will always cause supplementary full TSC
1737 * records, never the opposite (missing a full TSC record when it would
1740 save_last_tsc(config
, buf
, ctx
->tsc
);
1743 * Push the reader if necessary
1745 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1748 * Clear noref flag for this subbuffer.
1750 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1751 subbuf_index(offsets
.end
- 1, chan
),
1755 * Switch old subbuffer if needed.
1757 if (caa_unlikely(offsets
.switch_old_end
)) {
1758 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1759 subbuf_index(offsets
.old
- 1, chan
),
1761 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1765 * Populate new subbuffer.
1767 if (caa_unlikely(offsets
.switch_new_start
))
1768 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1770 if (caa_unlikely(offsets
.switch_new_end
))
1771 lib_ring_buffer_switch_new_end(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");