2 * ring_buffer_frontend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
41 * Bob Wisniewski <bob@watson.ibm.com>
43 * Buffer reader semantic :
46 * while buffer is not finalized and empty
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
55 #include <sys/types.h>
62 #include <urcu/compiler.h>
64 #include <urcu/tls-compat.h>
69 #include <lttng/ringbuffer-config.h>
75 #include "../liblttng-ust/compat.h" /* For ENODATA */
78 #define max(a, b) ((a) > (b) ? (a) : (b))
81 /* Print DBG() messages about events lost only every 1048576 hits */
82 #define DBG_PRINT_NR_LOST (1UL << 20)
84 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
85 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
86 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
87 #define CLOCKID CLOCK_MONOTONIC
88 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
89 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
92 * Non-static to ensure the compiler does not optimize away the xor.
94 uint8_t lttng_crash_magic_xor
[] = RB_CRASH_DUMP_ABI_MAGIC_XOR
;
97 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
98 * close(2) to close the fd returned by shm_open.
99 * shm_unlink releases the shared memory object name.
100 * ftruncate(2) sets the size of the memory object.
101 * mmap/munmap maps the shared memory obj to a virtual address in the
102 * calling proceess (should be done both in libust and consumer).
103 * See shm_overview(7) for details.
104 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
107 * Since we don't need to access the object using its name, we can
108 * immediately shm_unlink(3) it, and only keep the handle with its file
113 * Internal structure representing offsets to use at a sub-buffer switch.
115 struct switch_offsets
{
116 unsigned long begin
, end
, old
;
117 size_t pre_header_padding
, size
;
118 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
122 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
125 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
128 static pthread_mutex_t wakeup_fd_mutex
= PTHREAD_MUTEX_INITIALIZER
;
131 void lib_ring_buffer_print_errors(struct channel
*chan
,
132 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
133 struct lttng_ust_shm_handle
*handle
);
136 * Handle timer teardown race wrt memory free of private data by
137 * ring buffer signals are handled by a single thread, which permits
138 * a synchronization point between handling of each signal.
139 * Protected by the lock within the structure.
141 struct timer_signal_data
{
142 pthread_t tid
; /* thread id managing signals */
145 pthread_mutex_t lock
;
148 static struct timer_signal_data timer_signal
= {
152 .lock
= PTHREAD_MUTEX_INITIALIZER
,
156 * lib_ring_buffer_reset - Reset ring buffer to initial values.
159 * Effectively empty the ring buffer. Should be called when the buffer is not
160 * used for writing. The ring buffer can be opened for reading, but the reader
161 * should not be using the iterator concurrently with reset. The previous
162 * current iterator record is reset.
164 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
165 struct lttng_ust_shm_handle
*handle
)
167 struct channel
*chan
;
168 const struct lttng_ust_lib_ring_buffer_config
*config
;
171 chan
= shmp(handle
, buf
->backend
.chan
);
174 config
= &chan
->backend
.config
;
176 * Reset iterator first. It will put the subbuffer if it currently holds
179 v_set(config
, &buf
->offset
, 0);
180 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
181 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
182 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
183 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
185 uatomic_set(&buf
->consumed
, 0);
186 uatomic_set(&buf
->record_disabled
, 0);
187 v_set(config
, &buf
->last_tsc
, 0);
188 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
189 /* Don't reset number of active readers */
190 v_set(config
, &buf
->records_lost_full
, 0);
191 v_set(config
, &buf
->records_lost_wrap
, 0);
192 v_set(config
, &buf
->records_lost_big
, 0);
193 v_set(config
, &buf
->records_count
, 0);
194 v_set(config
, &buf
->records_overrun
, 0);
199 * channel_reset - Reset channel to initial values.
202 * Effectively empty the channel. Should be called when the channel is not used
203 * for writing. The channel can be opened for reading, but the reader should not
204 * be using the iterator concurrently with reset. The previous current iterator
207 void channel_reset(struct channel
*chan
)
210 * Reset iterators first. Will put the subbuffer if held for reading.
212 uatomic_set(&chan
->record_disabled
, 0);
213 /* Don't reset commit_count_mask, still valid */
214 channel_backend_reset(&chan
->backend
);
215 /* Don't reset switch/read timer interval */
216 /* Don't reset notifiers and notifier enable bits */
217 /* Don't reset reader reference count */
221 void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config
*config
,
222 struct lttng_crash_abi
*crash_abi
,
223 struct lttng_ust_lib_ring_buffer
*buf
,
224 struct channel_backend
*chanb
,
225 struct shm_object
*shmobj
,
226 struct lttng_ust_shm_handle
*handle
)
230 for (i
= 0; i
< RB_CRASH_DUMP_ABI_MAGIC_LEN
; i
++)
231 crash_abi
->magic
[i
] = lttng_crash_magic_xor
[i
] ^ 0xFF;
232 crash_abi
->mmap_length
= shmobj
->memory_map_size
;
233 crash_abi
->endian
= RB_CRASH_ENDIAN
;
234 crash_abi
->major
= RB_CRASH_DUMP_ABI_MAJOR
;
235 crash_abi
->minor
= RB_CRASH_DUMP_ABI_MINOR
;
236 crash_abi
->word_size
= sizeof(unsigned long);
237 crash_abi
->layout_type
= LTTNG_CRASH_TYPE_UST
;
239 /* Offset of fields */
240 crash_abi
->offset
.prod_offset
=
241 (uint32_t) ((char *) &buf
->offset
- (char *) buf
);
242 crash_abi
->offset
.consumed_offset
=
243 (uint32_t) ((char *) &buf
->consumed
- (char *) buf
);
244 crash_abi
->offset
.commit_hot_array
=
245 (uint32_t) ((char *) shmp(handle
, buf
->commit_hot
) - (char *) buf
);
246 crash_abi
->offset
.commit_hot_seq
=
247 offsetof(struct commit_counters_hot
, seq
);
248 crash_abi
->offset
.buf_wsb_array
=
249 (uint32_t) ((char *) shmp(handle
, buf
->backend
.buf_wsb
) - (char *) buf
);
250 crash_abi
->offset
.buf_wsb_id
=
251 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
, id
);
252 crash_abi
->offset
.sb_array
=
253 (uint32_t) ((char *) shmp(handle
, buf
->backend
.array
) - (char *) buf
);
254 crash_abi
->offset
.sb_array_shmp_offset
=
255 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
,
257 crash_abi
->offset
.sb_backend_p_offset
=
258 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages
,
262 crash_abi
->length
.prod_offset
= sizeof(buf
->offset
);
263 crash_abi
->length
.consumed_offset
= sizeof(buf
->consumed
);
264 crash_abi
->length
.commit_hot_seq
=
265 sizeof(((struct commit_counters_hot
*) NULL
)->seq
);
266 crash_abi
->length
.buf_wsb_id
=
267 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer
*) NULL
)->id
);
268 crash_abi
->length
.sb_array_shmp_offset
=
269 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*) NULL
)->shmp
._ref
.offset
);
270 crash_abi
->length
.sb_backend_p_offset
=
271 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages
*) NULL
)->p
._ref
.offset
);
274 crash_abi
->stride
.commit_hot_array
=
275 sizeof(struct commit_counters_hot
);
276 crash_abi
->stride
.buf_wsb_array
=
277 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
);
278 crash_abi
->stride
.sb_array
=
279 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
);
281 /* Buffer constants */
282 crash_abi
->buf_size
= chanb
->buf_size
;
283 crash_abi
->subbuf_size
= chanb
->subbuf_size
;
284 crash_abi
->num_subbuf
= chanb
->num_subbuf
;
285 crash_abi
->mode
= (uint32_t) chanb
->config
.mode
;
287 if (config
->cb
.content_size_field
) {
288 size_t offset
, length
;
290 config
->cb
.content_size_field(config
, &offset
, &length
);
291 crash_abi
->offset
.content_size
= offset
;
292 crash_abi
->length
.content_size
= length
;
294 crash_abi
->offset
.content_size
= 0;
295 crash_abi
->length
.content_size
= 0;
297 if (config
->cb
.packet_size_field
) {
298 size_t offset
, length
;
300 config
->cb
.packet_size_field(config
, &offset
, &length
);
301 crash_abi
->offset
.packet_size
= offset
;
302 crash_abi
->length
.packet_size
= length
;
304 crash_abi
->offset
.packet_size
= 0;
305 crash_abi
->length
.packet_size
= 0;
310 * Must be called under cpu hotplug protection.
312 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
313 struct channel_backend
*chanb
, int cpu
,
314 struct lttng_ust_shm_handle
*handle
,
315 struct shm_object
*shmobj
)
317 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
318 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
319 void *priv
= channel_get_private(chan
);
320 size_t subbuf_header_size
;
324 /* Test for cpu hotplug */
325 if (buf
->backend
.allocated
)
328 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
329 set_shmp(buf
->commit_hot
,
331 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
332 if (!shmp(handle
, buf
->commit_hot
)) {
336 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
337 set_shmp(buf
->commit_cold
,
339 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
340 if (!shmp(handle
, buf
->commit_cold
)) {
345 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
346 cpu
, handle
, shmobj
);
352 * Write the subbuffer header for first subbuffer so we know the total
353 * duration of data gathering.
355 subbuf_header_size
= config
->cb
.subbuffer_header_size();
356 v_set(config
, &buf
->offset
, subbuf_header_size
);
357 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
358 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
359 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
360 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
361 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->seq
);
363 if (config
->cb
.buffer_create
) {
364 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
369 init_crash_abi(config
, &buf
->crash_abi
, buf
, chanb
, shmobj
, handle
);
371 buf
->backend
.allocated
= 1;
376 /* commit_cold will be freed by shm teardown */
378 /* commit_hot will be freed by shm teardown */
384 void lib_ring_buffer_channel_switch_timer(int sig
, siginfo_t
*si
, void *uc
)
386 const struct lttng_ust_lib_ring_buffer_config
*config
;
387 struct lttng_ust_shm_handle
*handle
;
388 struct channel
*chan
;
391 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
393 chan
= si
->si_value
.sival_ptr
;
394 handle
= chan
->handle
;
395 config
= &chan
->backend
.config
;
397 DBG("Switch timer for channel %p\n", chan
);
400 * Only flush buffers periodically if readers are active.
402 pthread_mutex_lock(&wakeup_fd_mutex
);
403 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
404 for_each_possible_cpu(cpu
) {
405 struct lttng_ust_lib_ring_buffer
*buf
=
406 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
410 if (uatomic_read(&buf
->active_readers
))
411 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
415 struct lttng_ust_lib_ring_buffer
*buf
=
416 shmp(handle
, chan
->backend
.buf
[0].shmp
);
420 if (uatomic_read(&buf
->active_readers
))
421 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
,
424 pthread_mutex_unlock(&wakeup_fd_mutex
);
429 void lib_ring_buffer_channel_do_read(struct channel
*chan
)
431 const struct lttng_ust_lib_ring_buffer_config
*config
;
432 struct lttng_ust_shm_handle
*handle
;
435 handle
= chan
->handle
;
436 config
= &chan
->backend
.config
;
439 * Only flush buffers periodically if readers are active.
441 pthread_mutex_lock(&wakeup_fd_mutex
);
442 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
443 for_each_possible_cpu(cpu
) {
444 struct lttng_ust_lib_ring_buffer
*buf
=
445 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
449 if (uatomic_read(&buf
->active_readers
)
450 && lib_ring_buffer_poll_deliver(config
, buf
,
452 lib_ring_buffer_wakeup(buf
, handle
);
456 struct lttng_ust_lib_ring_buffer
*buf
=
457 shmp(handle
, chan
->backend
.buf
[0].shmp
);
461 if (uatomic_read(&buf
->active_readers
)
462 && lib_ring_buffer_poll_deliver(config
, buf
,
464 lib_ring_buffer_wakeup(buf
, handle
);
467 pthread_mutex_unlock(&wakeup_fd_mutex
);
471 void lib_ring_buffer_channel_read_timer(int sig
, siginfo_t
*si
, void *uc
)
473 struct channel
*chan
;
475 assert(CMM_LOAD_SHARED(timer_signal
.tid
) == pthread_self());
476 chan
= si
->si_value
.sival_ptr
;
477 DBG("Read timer for channel %p\n", chan
);
478 lib_ring_buffer_channel_do_read(chan
);
483 void rb_setmask(sigset_t
*mask
)
487 ret
= sigemptyset(mask
);
489 PERROR("sigemptyset");
491 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_FLUSH
);
495 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_READ
);
499 ret
= sigaddset(mask
, LTTNG_UST_RB_SIG_TEARDOWN
);
506 void *sig_thread(void *arg
)
512 /* Only self thread will receive signal mask. */
514 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
517 signr
= sigwaitinfo(&mask
, &info
);
520 PERROR("sigwaitinfo");
523 if (signr
== LTTNG_UST_RB_SIG_FLUSH
) {
524 lib_ring_buffer_channel_switch_timer(info
.si_signo
,
526 } else if (signr
== LTTNG_UST_RB_SIG_READ
) {
527 lib_ring_buffer_channel_read_timer(info
.si_signo
,
529 } else if (signr
== LTTNG_UST_RB_SIG_TEARDOWN
) {
531 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
534 ERR("Unexptected signal %d\n", info
.si_signo
);
541 * Ensure only a single thread listens on the timer signal.
544 void lib_ring_buffer_setup_timer_thread(void)
549 pthread_mutex_lock(&timer_signal
.lock
);
550 if (timer_signal
.setup_done
)
553 ret
= pthread_create(&thread
, NULL
, &sig_thread
, NULL
);
556 PERROR("pthread_create");
558 ret
= pthread_detach(thread
);
561 PERROR("pthread_detach");
563 timer_signal
.setup_done
= 1;
565 pthread_mutex_unlock(&timer_signal
.lock
);
569 * Wait for signal-handling thread quiescent state.
572 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr
)
574 sigset_t pending_set
;
578 * We need to be the only thread interacting with the thread
579 * that manages signals for teardown synchronization.
581 pthread_mutex_lock(&timer_signal
.lock
);
584 * Ensure we don't have any signal queued for this channel.
587 ret
= sigemptyset(&pending_set
);
589 PERROR("sigemptyset");
591 ret
= sigpending(&pending_set
);
593 PERROR("sigpending");
595 if (!sigismember(&pending_set
, signr
))
601 * From this point, no new signal handler will be fired that
602 * would try to access "chan". However, we still need to wait
603 * for any currently executing handler to complete.
606 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
610 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
613 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN
);
615 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
))
619 pthread_mutex_unlock(&timer_signal
.lock
);
623 void lib_ring_buffer_channel_switch_timer_start(struct channel
*chan
)
626 struct itimerspec its
;
629 if (!chan
->switch_timer_interval
|| chan
->switch_timer_enabled
)
632 chan
->switch_timer_enabled
= 1;
634 lib_ring_buffer_setup_timer_thread();
636 sev
.sigev_notify
= SIGEV_SIGNAL
;
637 sev
.sigev_signo
= LTTNG_UST_RB_SIG_FLUSH
;
638 sev
.sigev_value
.sival_ptr
= chan
;
639 ret
= timer_create(CLOCKID
, &sev
, &chan
->switch_timer
);
641 PERROR("timer_create");
644 its
.it_value
.tv_sec
= chan
->switch_timer_interval
/ 1000000;
645 its
.it_value
.tv_nsec
= (chan
->switch_timer_interval
% 1000000) * 1000;
646 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
647 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
649 ret
= timer_settime(chan
->switch_timer
, 0, &its
, NULL
);
651 PERROR("timer_settime");
656 void lib_ring_buffer_channel_switch_timer_stop(struct channel
*chan
)
660 if (!chan
->switch_timer_interval
|| !chan
->switch_timer_enabled
)
663 ret
= timer_delete(chan
->switch_timer
);
665 PERROR("timer_delete");
668 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH
);
670 chan
->switch_timer
= 0;
671 chan
->switch_timer_enabled
= 0;
675 void lib_ring_buffer_channel_read_timer_start(struct channel
*chan
)
677 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
679 struct itimerspec its
;
682 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
683 || !chan
->read_timer_interval
|| chan
->read_timer_enabled
)
686 chan
->read_timer_enabled
= 1;
688 lib_ring_buffer_setup_timer_thread();
690 sev
.sigev_notify
= SIGEV_SIGNAL
;
691 sev
.sigev_signo
= LTTNG_UST_RB_SIG_READ
;
692 sev
.sigev_value
.sival_ptr
= chan
;
693 ret
= timer_create(CLOCKID
, &sev
, &chan
->read_timer
);
695 PERROR("timer_create");
698 its
.it_value
.tv_sec
= chan
->read_timer_interval
/ 1000000;
699 its
.it_value
.tv_nsec
= (chan
->read_timer_interval
% 1000000) * 1000;
700 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
701 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
703 ret
= timer_settime(chan
->read_timer
, 0, &its
, NULL
);
705 PERROR("timer_settime");
710 void lib_ring_buffer_channel_read_timer_stop(struct channel
*chan
)
712 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
715 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
716 || !chan
->read_timer_interval
|| !chan
->read_timer_enabled
)
719 ret
= timer_delete(chan
->read_timer
);
721 PERROR("timer_delete");
725 * do one more check to catch data that has been written in the last
728 lib_ring_buffer_channel_do_read(chan
);
730 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ
);
732 chan
->read_timer
= 0;
733 chan
->read_timer_enabled
= 0;
736 static void channel_unregister_notifiers(struct channel
*chan
,
737 struct lttng_ust_shm_handle
*handle
)
739 lib_ring_buffer_channel_switch_timer_stop(chan
);
740 lib_ring_buffer_channel_read_timer_stop(chan
);
743 static void channel_print_errors(struct channel
*chan
,
744 struct lttng_ust_shm_handle
*handle
)
746 const struct lttng_ust_lib_ring_buffer_config
*config
=
747 &chan
->backend
.config
;
750 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
751 for_each_possible_cpu(cpu
) {
752 struct lttng_ust_lib_ring_buffer
*buf
=
753 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
754 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
757 struct lttng_ust_lib_ring_buffer
*buf
=
758 shmp(handle
, chan
->backend
.buf
[0].shmp
);
760 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
764 static void channel_free(struct channel
*chan
,
765 struct lttng_ust_shm_handle
*handle
)
767 channel_backend_free(&chan
->backend
, handle
);
768 /* chan is freed by shm teardown */
769 shm_object_table_destroy(handle
->table
);
774 * channel_create - Create channel.
775 * @config: ring buffer instance configuration
776 * @name: name of the channel
777 * @priv_data: ring buffer client private data area pointer (output)
778 * @priv_data_size: length, in bytes, of the private data area.
779 * @priv_data_init: initialization data for private data.
780 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
781 * address mapping. It is used only by RING_BUFFER_STATIC
782 * configuration. It can be set to NULL for other backends.
783 * @subbuf_size: subbuffer size
784 * @num_subbuf: number of subbuffers
785 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
786 * padding to let readers get those sub-buffers.
787 * Used for live streaming.
788 * @read_timer_interval: Time interval (in us) to wake up pending readers.
789 * @stream_fds: array of stream file descriptors.
790 * @nr_stream_fds: number of file descriptors in array.
793 * Returns NULL on failure.
795 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
798 size_t priv_data_align
,
799 size_t priv_data_size
,
800 void *priv_data_init
,
801 void *buf_addr
, size_t subbuf_size
,
802 size_t num_subbuf
, unsigned int switch_timer_interval
,
803 unsigned int read_timer_interval
,
804 const int *stream_fds
, int nr_stream_fds
)
807 size_t shmsize
, chansize
;
808 struct channel
*chan
;
809 struct lttng_ust_shm_handle
*handle
;
810 struct shm_object
*shmobj
;
811 unsigned int nr_streams
;
813 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
814 nr_streams
= num_possible_cpus();
818 if (nr_stream_fds
!= nr_streams
)
821 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
822 read_timer_interval
))
825 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
829 /* Allocate table for channel + per-cpu buffers */
830 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
832 goto error_table_alloc
;
834 /* Calculate the shm allocation layout */
835 shmsize
= sizeof(struct channel
);
836 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
837 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
840 shmsize
+= offset_align(shmsize
, priv_data_align
);
841 shmsize
+= priv_data_size
;
843 /* Allocate normal memory for channel (not shared) */
844 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
,
848 /* struct channel is at object 0, offset 0 (hardcoded) */
849 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
850 assert(handle
->chan
._ref
.index
== 0);
851 assert(handle
->chan
._ref
.offset
== 0);
852 chan
= shmp(handle
, handle
->chan
);
855 chan
->nr_streams
= nr_streams
;
857 /* space for private data */
858 if (priv_data_size
) {
859 DECLARE_SHMP(void, priv_data_alloc
);
861 align_shm(shmobj
, priv_data_align
);
862 chan
->priv_data_offset
= shmobj
->allocated_len
;
863 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
864 if (!shmp(handle
, priv_data_alloc
))
866 *priv_data
= channel_get_private(chan
);
867 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
869 chan
->priv_data_offset
= -1;
874 ret
= channel_backend_init(&chan
->backend
, name
, config
,
875 subbuf_size
, num_subbuf
, handle
,
878 goto error_backend_init
;
880 chan
->handle
= handle
;
881 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
883 chan
->switch_timer_interval
= switch_timer_interval
;
884 chan
->read_timer_interval
= read_timer_interval
;
885 lib_ring_buffer_channel_switch_timer_start(chan
);
886 lib_ring_buffer_channel_read_timer_start(chan
);
892 shm_object_table_destroy(handle
->table
);
898 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
899 uint64_t memory_map_size
,
902 struct lttng_ust_shm_handle
*handle
;
903 struct shm_object
*object
;
905 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
909 /* Allocate table for channel + per-cpu buffers */
910 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
912 goto error_table_alloc
;
913 /* Add channel object */
914 object
= shm_object_table_append_mem(handle
->table
, data
,
915 memory_map_size
, wakeup_fd
);
917 goto error_table_object
;
918 /* struct channel is at object 0, offset 0 (hardcoded) */
919 handle
->chan
._ref
.index
= 0;
920 handle
->chan
._ref
.offset
= 0;
924 shm_object_table_destroy(handle
->table
);
930 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
931 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
932 uint64_t memory_map_size
)
934 struct shm_object
*object
;
936 /* Add stream object */
937 object
= shm_object_table_append_shm(handle
->table
,
938 shm_fd
, wakeup_fd
, stream_nr
,
945 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
947 assert(handle
->table
);
948 return handle
->table
->allocated_len
- 1;
952 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
954 channel_free(chan
, handle
);
958 * channel_destroy - Finalize, wait for q.s. and destroy channel.
959 * @chan: channel to destroy
962 * Call "destroy" callback, finalize channels, decrement the channel
963 * reference count. Note that when readers have completed data
964 * consumption of finalized channels, get_subbuf() will return -ENODATA.
965 * They should release their handle at that point.
967 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
972 * Note: the consumer takes care of finalizing and
973 * switching the buffers.
975 channel_unregister_notifiers(chan
, handle
);
977 * The consumer prints errors.
979 channel_print_errors(chan
, handle
);
983 * sessiond/consumer are keeping a reference on the shm file
984 * descriptor directly. No need to refcount.
986 channel_release(chan
, handle
);
990 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
991 const struct lttng_ust_lib_ring_buffer_config
*config
,
992 struct channel
*chan
, int cpu
,
993 struct lttng_ust_shm_handle
*handle
,
994 int *shm_fd
, int *wait_fd
,
996 uint64_t *memory_map_size
)
1000 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1003 if (cpu
>= num_possible_cpus())
1006 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1007 *shm_fd
= shm_get_shm_fd(handle
, ref
);
1008 *wait_fd
= shm_get_wait_fd(handle
, ref
);
1009 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
1010 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
1012 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
1015 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1016 struct channel
*chan
,
1017 struct lttng_ust_shm_handle
*handle
)
1019 struct shm_ref
*ref
;
1021 ref
= &handle
->chan
._ref
;
1022 return shm_close_wait_fd(handle
, ref
);
1025 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1026 struct channel
*chan
,
1027 struct lttng_ust_shm_handle
*handle
)
1029 struct shm_ref
*ref
;
1031 ref
= &handle
->chan
._ref
;
1032 return shm_close_wakeup_fd(handle
, ref
);
1035 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1036 struct channel
*chan
,
1037 struct lttng_ust_shm_handle
*handle
,
1040 struct shm_ref
*ref
;
1042 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1045 if (cpu
>= num_possible_cpus())
1048 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1049 return shm_close_wait_fd(handle
, ref
);
1052 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
1053 struct channel
*chan
,
1054 struct lttng_ust_shm_handle
*handle
,
1057 struct shm_ref
*ref
;
1060 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
1063 if (cpu
>= num_possible_cpus())
1066 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
1067 pthread_mutex_lock(&wakeup_fd_mutex
);
1068 ret
= shm_close_wakeup_fd(handle
, ref
);
1069 pthread_mutex_unlock(&wakeup_fd_mutex
);
1073 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
1074 struct lttng_ust_shm_handle
*handle
)
1076 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
1082 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
1083 struct lttng_ust_shm_handle
*handle
)
1085 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1087 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1089 uatomic_dec(&buf
->active_readers
);
1093 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1095 * @consumed: consumed count indicating the position where to read
1096 * @produced: produced count, indicates position when to stop reading
1098 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1099 * data to read at consumed position, or 0 if the get operation succeeds.
1102 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
1103 unsigned long *consumed
, unsigned long *produced
,
1104 struct lttng_ust_shm_handle
*handle
)
1106 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1107 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1108 unsigned long consumed_cur
, write_offset
;
1111 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1113 * Read finalized before counters.
1116 consumed_cur
= uatomic_read(&buf
->consumed
);
1118 * No need to issue a memory barrier between consumed count read and
1119 * write offset read, because consumed count can only change
1120 * concurrently in overwrite mode, and we keep a sequence counter
1121 * identifier derived from the write offset to check we are getting
1122 * the same sub-buffer we are expecting (the sub-buffers are atomically
1123 * "tagged" upon writes, tags are checked upon read).
1125 write_offset
= v_read(config
, &buf
->offset
);
1128 * Check that we are not about to read the same subbuffer in
1129 * which the writer head is.
1131 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
1135 *consumed
= consumed_cur
;
1136 *produced
= subbuf_trunc(write_offset
, chan
);
1142 * The memory barriers __wait_event()/wake_up_interruptible() take care
1143 * of "raw_spin_is_locked" memory ordering.
1152 * lib_ring_buffer_move_consumer - move consumed counter forward
1154 * @consumed_new: new consumed count value
1156 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
1157 unsigned long consumed_new
,
1158 struct lttng_ust_shm_handle
*handle
)
1160 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1161 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1162 unsigned long consumed
;
1164 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1167 * Only push the consumed value forward.
1168 * If the consumed cmpxchg fails, this is because we have been pushed by
1169 * the writer in flight recorder mode.
1171 consumed
= uatomic_read(&buf
->consumed
);
1172 while ((long) consumed
- (long) consumed_new
< 0)
1173 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
1178 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1180 * @consumed: consumed count indicating the position where to read
1182 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1183 * data to read at consumed position, or 0 if the get operation succeeds.
1185 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1186 unsigned long consumed
,
1187 struct lttng_ust_shm_handle
*handle
)
1189 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1190 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1191 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1192 int ret
, finalized
, nr_retry
= LTTNG_UST_RING_BUFFER_GET_RETRY
;
1195 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
1197 * Read finalized before counters.
1200 consumed_cur
= uatomic_read(&buf
->consumed
);
1201 consumed_idx
= subbuf_index(consumed
, chan
);
1202 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
1204 * Make sure we read the commit count before reading the buffer
1205 * data and the write offset. Correct consumed offset ordering
1206 * wrt commit count is insured by the use of cmpxchg to update
1207 * the consumed offset.
1210 * Local rmb to match the remote wmb to read the commit count
1211 * before the buffer data and the write offset.
1215 write_offset
= v_read(config
, &buf
->offset
);
1218 * Check that the buffer we are getting is after or at consumed_cur
1221 if ((long) subbuf_trunc(consumed
, chan
)
1222 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1226 * Check that the subbuffer we are trying to consume has been
1227 * already fully committed. There are a few causes that can make
1228 * this unavailability situation occur:
1230 * Temporary (short-term) situation:
1231 * - Application is running on a different CPU, between reserve
1232 * and commit ring buffer operations,
1233 * - Application is preempted between reserve and commit ring
1234 * buffer operations,
1236 * Long-term situation:
1237 * - Application is stopped (SIGSTOP) between reserve and commit
1238 * ring buffer operations. Could eventually be resumed by
1240 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1241 * reserve and commit ring buffer operation.
1243 * From a consumer perspective, handling short-term
1244 * unavailability situations is performed by retrying a few
1245 * times after a delay. Handling long-term unavailability
1246 * situations is handled by failing to get the sub-buffer.
1248 * In all of those situations, if the application is taking a
1249 * long time to perform its commit after ring buffer space
1250 * reservation, we can end up in a situation where the producer
1251 * will fill the ring buffer and try to write into the same
1252 * sub-buffer again (which has a missing commit). This is
1253 * handled by the producer in the sub-buffer switch handling
1254 * code of the reserve routine by detecting unbalanced
1255 * reserve/commit counters and discarding all further events
1256 * until the situation is resolved in those situations. Two
1257 * scenarios can occur:
1259 * 1) The application causing the reserve/commit counters to be
1260 * unbalanced has been terminated. In this situation, all
1261 * further events will be discarded in the buffers, and no
1262 * further buffer data will be readable by the consumer
1263 * daemon. Tearing down the UST tracing session and starting
1264 * anew is a work-around for those situations. Note that this
1265 * only affects per-UID tracing. In per-PID tracing, the
1266 * application vanishes with the termination, and therefore
1267 * no more data needs to be written to the buffers.
1268 * 2) The application causing the unbalance has been delayed for
1269 * a long time, but will eventually try to increment the
1270 * commit counter after eventually writing to the sub-buffer.
1271 * This situation can cause events to be discarded until the
1272 * application resumes its operations.
1274 if (((commit_count
- chan
->backend
.subbuf_size
)
1275 & chan
->commit_count_mask
)
1276 - (buf_trunc(consumed
, chan
)
1277 >> chan
->backend
.num_subbuf_order
)
1279 if (nr_retry
-- > 0) {
1280 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1281 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1289 * Check that we are not about to read the same subbuffer in
1290 * which the writer head is.
1292 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1297 * Failure to get the subbuffer causes a busy-loop retry without going
1298 * to a wait queue. These are caused by short-lived race windows where
1299 * the writer is getting access to a subbuffer we were trying to get
1300 * access to. Also checks that the "consumed" buffer count we are
1301 * looking for matches the one contained in the subbuffer id.
1303 * The short-lived race window described here can be affected by
1304 * application signals and preemption, thus requiring to bound
1305 * the loop to a maximum number of retry.
1307 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1308 consumed_idx
, buf_trunc_val(consumed
, chan
),
1311 if (nr_retry
-- > 0) {
1312 if (nr_retry
<= (LTTNG_UST_RING_BUFFER_GET_RETRY
>> 1))
1313 (void) poll(NULL
, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS
);
1319 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1321 buf
->get_subbuf_consumed
= consumed
;
1322 buf
->get_subbuf
= 1;
1328 * The memory barriers __wait_event()/wake_up_interruptible() take care
1329 * of "raw_spin_is_locked" memory ordering.
1338 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1341 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
1342 struct lttng_ust_shm_handle
*handle
)
1344 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1345 struct channel
*chan
= shmp(handle
, bufb
->chan
);
1346 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1347 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1349 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
1351 if (!buf
->get_subbuf
) {
1353 * Reader puts a subbuffer it did not get.
1355 CHAN_WARN_ON(chan
, 1);
1358 consumed
= buf
->get_subbuf_consumed
;
1359 buf
->get_subbuf
= 0;
1362 * Clear the records_unread counter. (overruns counter)
1363 * Can still be non-zero if a file reader simply grabbed the data
1364 * without using iterators.
1365 * Can be below zero if an iterator is used on a snapshot more than
1368 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1369 v_add(config
, v_read(config
,
1370 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
1371 &bufb
->records_read
);
1372 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
1373 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1374 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1375 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1378 * Exchange the reader subbuffer with the one we put in its place in the
1379 * writer subbuffer table. Expect the original consumed count. If
1380 * update_read_sb_index fails, this is because the writer updated the
1381 * subbuffer concurrently. We should therefore keep the subbuffer we
1382 * currently have: it has become invalid to try reading this sub-buffer
1383 * consumed count value anyway.
1385 consumed_idx
= subbuf_index(consumed
, chan
);
1386 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1387 consumed_idx
, buf_trunc_val(consumed
, chan
),
1390 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1391 * if the writer concurrently updated it.
1396 * cons_offset is an iterator on all subbuffer offsets between the reader
1397 * position and the writer position. (inclusive)
1400 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1401 struct channel
*chan
,
1402 unsigned long cons_offset
,
1404 struct lttng_ust_shm_handle
*handle
)
1406 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1407 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1409 cons_idx
= subbuf_index(cons_offset
, chan
);
1410 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
1411 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
1413 if (subbuf_offset(commit_count
, chan
) != 0)
1414 DBG("ring buffer %s, cpu %d: "
1415 "commit count in subbuffer %lu,\n"
1416 "expecting multiples of %lu bytes\n"
1417 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1418 chan
->backend
.name
, cpu
, cons_idx
,
1419 chan
->backend
.subbuf_size
,
1420 commit_count
, commit_count_sb
);
1422 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1423 chan
->backend
.name
, cpu
, commit_count
);
1427 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1428 struct channel
*chan
,
1429 void *priv
, int cpu
,
1430 struct lttng_ust_shm_handle
*handle
)
1432 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1433 unsigned long write_offset
, cons_offset
;
1436 * No need to order commit_count, write_offset and cons_offset reads
1437 * because we execute at teardown when no more writer nor reader
1438 * references are left.
1440 write_offset
= v_read(config
, &buf
->offset
);
1441 cons_offset
= uatomic_read(&buf
->consumed
);
1442 if (write_offset
!= cons_offset
)
1443 DBG("ring buffer %s, cpu %d: "
1444 "non-consumed data\n"
1445 " [ %lu bytes written, %lu bytes read ]\n",
1446 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1448 for (cons_offset
= uatomic_read(&buf
->consumed
);
1449 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1452 cons_offset
= subbuf_align(cons_offset
, chan
))
1453 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1458 void lib_ring_buffer_print_errors(struct channel
*chan
,
1459 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1460 struct lttng_ust_shm_handle
*handle
)
1462 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1463 void *priv
= channel_get_private(chan
);
1465 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1466 DBG("ring buffer %s: %lu records written, "
1467 "%lu records overrun\n",
1469 v_read(config
, &buf
->records_count
),
1470 v_read(config
, &buf
->records_overrun
));
1472 DBG("ring buffer %s, cpu %d: %lu records written, "
1473 "%lu records overrun\n",
1474 chan
->backend
.name
, cpu
,
1475 v_read(config
, &buf
->records_count
),
1476 v_read(config
, &buf
->records_overrun
));
1478 if (v_read(config
, &buf
->records_lost_full
)
1479 || v_read(config
, &buf
->records_lost_wrap
)
1480 || v_read(config
, &buf
->records_lost_big
))
1481 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1482 " [ %lu buffer full, %lu nest buffer wrap-around, "
1483 "%lu event too big ]\n",
1484 chan
->backend
.name
, cpu
,
1485 v_read(config
, &buf
->records_lost_full
),
1486 v_read(config
, &buf
->records_lost_wrap
),
1487 v_read(config
, &buf
->records_lost_big
));
1489 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1493 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1495 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1496 * active or at buffer finalization (destroy).
1499 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1500 struct channel
*chan
,
1501 struct switch_offsets
*offsets
,
1503 struct lttng_ust_shm_handle
*handle
)
1505 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1506 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1507 unsigned long commit_count
;
1509 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1512 * Order all writes to buffer before the commit count update that will
1513 * determine that the subbuffer is full.
1516 v_add(config
, config
->cb
.subbuffer_header_size(),
1517 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1518 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1519 /* Check if the written buffer has to be delivered */
1520 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1521 commit_count
, oldidx
, handle
, tsc
);
1522 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1523 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1524 commit_count
, handle
);
1528 * lib_ring_buffer_switch_old_end: switch old subbuffer
1530 * Note : offset_old should never be 0 here. It is ok, because we never perform
1531 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1532 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1536 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1537 struct channel
*chan
,
1538 struct switch_offsets
*offsets
,
1540 struct lttng_ust_shm_handle
*handle
)
1542 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1543 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1544 unsigned long commit_count
, padding_size
, data_size
;
1546 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1547 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1548 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1552 * Order all writes to buffer before the commit count update that will
1553 * determine that the subbuffer is full.
1556 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1557 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1558 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1559 commit_count
, oldidx
, handle
, tsc
);
1560 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1561 offsets
->old
+ padding_size
, commit_count
, handle
);
1565 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1567 * This code can be executed unordered : writers may already have written to the
1568 * sub-buffer before this code gets executed, caution. The commit makes sure
1569 * that this code is executed before the deliver of this sub-buffer.
1572 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1573 struct channel
*chan
,
1574 struct switch_offsets
*offsets
,
1576 struct lttng_ust_shm_handle
*handle
)
1578 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1579 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1580 unsigned long commit_count
;
1582 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1585 * Order all writes to buffer before the commit count update that will
1586 * determine that the subbuffer is full.
1589 v_add(config
, config
->cb
.subbuffer_header_size(),
1590 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1591 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1592 /* Check if the written buffer has to be delivered */
1593 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1594 commit_count
, beginidx
, handle
, tsc
);
1595 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1596 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1597 commit_count
, handle
);
1601 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1603 * Calls subbuffer_set_data_size() to set the data size of the current
1604 * sub-buffer. We do not need to perform check_deliver nor commit here,
1605 * since this task will be done by the "commit" of the event for which
1606 * we are currently doing the space reservation.
1609 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1610 struct channel
*chan
,
1611 struct switch_offsets
*offsets
,
1613 struct lttng_ust_shm_handle
*handle
)
1615 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1616 unsigned long endidx
, data_size
;
1618 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1619 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1620 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1627 * !0 if execution must be aborted.
1630 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1631 struct lttng_ust_lib_ring_buffer
*buf
,
1632 struct channel
*chan
,
1633 struct switch_offsets
*offsets
,
1635 struct lttng_ust_shm_handle
*handle
)
1637 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1638 unsigned long off
, reserve_commit_diff
;
1640 offsets
->begin
= v_read(config
, &buf
->offset
);
1641 offsets
->old
= offsets
->begin
;
1642 offsets
->switch_old_start
= 0;
1643 off
= subbuf_offset(offsets
->begin
, chan
);
1645 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1648 * Ensure we flush the header of an empty subbuffer when doing the
1649 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1650 * total data gathering duration even if there were no records saved
1651 * after the last buffer switch.
1652 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1653 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1654 * subbuffer header as appropriate.
1655 * The next record that reserves space will be responsible for
1656 * populating the following subbuffer header. We choose not to populate
1657 * the next subbuffer header here because we want to be able to use
1658 * SWITCH_ACTIVE for periodical buffer flush, which must
1659 * guarantee that all the buffer content (records and header
1660 * timestamps) are visible to the reader. This is required for
1661 * quiescence guarantees for the fusion merge.
1663 if (mode
!= SWITCH_FLUSH
&& !off
)
1664 return -1; /* we do not have to switch : buffer is empty */
1666 if (caa_unlikely(off
== 0)) {
1667 unsigned long sb_index
, commit_count
;
1670 * We are performing a SWITCH_FLUSH. There may be concurrent
1671 * writes into the buffer if e.g. invoked while performing a
1672 * snapshot on an active trace.
1674 * If the client does not save any header information
1675 * (sub-buffer header size == 0), don't switch empty subbuffer
1676 * on finalize, because it is invalid to deliver a completely
1679 if (!config
->cb
.subbuffer_header_size())
1682 /* Test new buffer integrity */
1683 sb_index
= subbuf_index(offsets
->begin
, chan
);
1684 commit_count
= v_read(config
,
1685 &shmp_index(handle
, buf
->commit_cold
,
1687 reserve_commit_diff
=
1688 (buf_trunc(offsets
->begin
, chan
)
1689 >> chan
->backend
.num_subbuf_order
)
1690 - (commit_count
& chan
->commit_count_mask
);
1691 if (caa_likely(reserve_commit_diff
== 0)) {
1692 /* Next subbuffer not being written to. */
1693 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1694 subbuf_trunc(offsets
->begin
, chan
)
1695 - subbuf_trunc((unsigned long)
1696 uatomic_read(&buf
->consumed
), chan
)
1697 >= chan
->backend
.buf_size
)) {
1699 * We do not overwrite non consumed buffers
1700 * and we are full : don't switch.
1705 * Next subbuffer not being written to, and we
1706 * are either in overwrite mode or the buffer is
1707 * not full. It's safe to write in this new
1713 * Next subbuffer reserve offset does not match the
1714 * commit offset. Don't perform switch in
1715 * producer-consumer and overwrite mode. Caused by
1716 * either a writer OOPS or too many nested writes over a
1717 * reserve/commit pair.
1723 * Need to write the subbuffer start header on finalize.
1725 offsets
->switch_old_start
= 1;
1727 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1728 /* Note: old points to the next subbuf at offset 0 */
1729 offsets
->end
= offsets
->begin
;
1734 * Force a sub-buffer switch. This operation is completely reentrant : can be
1735 * called while tracing is active with absolutely no lock held.
1737 * Note, however, that as a v_cmpxchg is used for some atomic
1738 * operations, this function must be called from the CPU which owns the buffer
1739 * for a ACTIVE flush.
1741 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1742 struct lttng_ust_shm_handle
*handle
)
1744 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1745 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1746 struct switch_offsets offsets
;
1747 unsigned long oldidx
;
1753 * Perform retryable operations.
1756 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1758 return; /* Switch not needed */
1759 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1763 * Atomically update last_tsc. This update races against concurrent
1764 * atomic updates, but the race will always cause supplementary full TSC
1765 * records, never the opposite (missing a full TSC record when it would
1768 save_last_tsc(config
, buf
, tsc
);
1771 * Push the reader if necessary
1773 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1775 oldidx
= subbuf_index(offsets
.old
, chan
);
1776 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1779 * May need to populate header start on SWITCH_FLUSH.
1781 if (offsets
.switch_old_start
) {
1782 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1783 offsets
.old
+= config
->cb
.subbuffer_header_size();
1787 * Switch old subbuffer.
1789 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1795 * -ENOSPC if event size is too large for packet.
1796 * -ENOBUFS if there is currently not enough space in buffer for the event.
1797 * -EIO if data cannot be written into the buffer for any other reason.
1800 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1801 struct channel
*chan
,
1802 struct switch_offsets
*offsets
,
1803 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1805 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1806 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1807 unsigned long reserve_commit_diff
, offset_cmp
;
1810 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1811 offsets
->old
= offsets
->begin
;
1812 offsets
->switch_new_start
= 0;
1813 offsets
->switch_new_end
= 0;
1814 offsets
->switch_old_end
= 0;
1815 offsets
->pre_header_padding
= 0;
1817 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1818 if ((int64_t) ctx
->tsc
== -EIO
)
1821 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1822 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1824 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1825 offsets
->switch_new_start
= 1; /* For offsets->begin */
1827 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1829 &offsets
->pre_header_padding
,
1832 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1835 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1836 offsets
->size
> chan
->backend
.subbuf_size
)) {
1837 offsets
->switch_old_end
= 1; /* For offsets->old */
1838 offsets
->switch_new_start
= 1; /* For offsets->begin */
1841 if (caa_unlikely(offsets
->switch_new_start
)) {
1842 unsigned long sb_index
, commit_count
;
1845 * We are typically not filling the previous buffer completely.
1847 if (caa_likely(offsets
->switch_old_end
))
1848 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1849 offsets
->begin
= offsets
->begin
1850 + config
->cb
.subbuffer_header_size();
1851 /* Test new buffer integrity */
1852 sb_index
= subbuf_index(offsets
->begin
, chan
);
1854 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1855 * lib_ring_buffer_check_deliver() has the matching
1856 * memory barriers required around commit_cold cc_sb
1857 * updates to ensure reserve and commit counter updates
1858 * are not seen reordered when updated by another CPU.
1861 commit_count
= v_read(config
,
1862 &shmp_index(handle
, buf
->commit_cold
,
1864 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1866 if (caa_unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1868 * The reserve counter have been concurrently updated
1869 * while we read the commit counter. This means the
1870 * commit counter we read might not match buf->offset
1871 * due to concurrent update. We therefore need to retry.
1875 reserve_commit_diff
=
1876 (buf_trunc(offsets
->begin
, chan
)
1877 >> chan
->backend
.num_subbuf_order
)
1878 - (commit_count
& chan
->commit_count_mask
);
1879 if (caa_likely(reserve_commit_diff
== 0)) {
1880 /* Next subbuffer not being written to. */
1881 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1882 subbuf_trunc(offsets
->begin
, chan
)
1883 - subbuf_trunc((unsigned long)
1884 uatomic_read(&buf
->consumed
), chan
)
1885 >= chan
->backend
.buf_size
)) {
1886 unsigned long nr_lost
;
1889 * We do not overwrite non consumed buffers
1890 * and we are full : record is lost.
1892 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1893 v_inc(config
, &buf
->records_lost_full
);
1894 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1895 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1896 nr_lost
+ 1, chan
->backend
.name
,
1902 * Next subbuffer not being written to, and we
1903 * are either in overwrite mode or the buffer is
1904 * not full. It's safe to write in this new
1909 unsigned long nr_lost
;
1912 * Next subbuffer reserve offset does not match the
1913 * commit offset, and this did not involve update to the
1914 * reserve counter. Drop record in producer-consumer and
1915 * overwrite mode. Caused by either a writer OOPS or too
1916 * many nested writes over a reserve/commit pair.
1918 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1919 v_inc(config
, &buf
->records_lost_wrap
);
1920 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1921 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1922 nr_lost
+ 1, chan
->backend
.name
,
1928 config
->cb
.record_header_size(config
, chan
,
1930 &offsets
->pre_header_padding
,
1933 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1936 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1937 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1938 unsigned long nr_lost
;
1941 * Record too big for subbuffers, report error, don't
1942 * complete the sub-buffer switch.
1944 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1945 v_inc(config
, &buf
->records_lost_big
);
1946 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1947 DBG("%lu or more records lost in (%s:%d) record size "
1948 " of %zu bytes is too large for buffer\n",
1949 nr_lost
+ 1, chan
->backend
.name
,
1950 buf
->backend
.cpu
, offsets
->size
);
1955 * We just made a successful buffer switch and the
1956 * record fits in the new subbuffer. Let's write.
1961 * Record fits in the current buffer and we are not on a switch
1962 * boundary. It's safe to write.
1965 offsets
->end
= offsets
->begin
+ offsets
->size
;
1967 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1969 * The offset_end will fall at the very beginning of the next
1972 offsets
->switch_new_end
= 1; /* For offsets->begin */
1978 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1979 * @ctx: ring buffer context.
1981 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1982 * -EIO for other errors, else returns 0.
1983 * It will take care of sub-buffer switching.
1985 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1987 struct channel
*chan
= ctx
->chan
;
1988 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1989 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1990 struct lttng_ust_lib_ring_buffer
*buf
;
1991 struct switch_offsets offsets
;
1994 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1995 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1997 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
2003 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
2005 if (caa_unlikely(ret
))
2007 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
2012 * Atomically update last_tsc. This update races against concurrent
2013 * atomic updates, but the race will always cause supplementary full TSC
2014 * records, never the opposite (missing a full TSC record when it would
2017 save_last_tsc(config
, buf
, ctx
->tsc
);
2020 * Push the reader if necessary
2022 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
2025 * Clear noref flag for this subbuffer.
2027 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2028 subbuf_index(offsets
.end
- 1, chan
),
2032 * Switch old subbuffer if needed.
2034 if (caa_unlikely(offsets
.switch_old_end
)) {
2035 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
2036 subbuf_index(offsets
.old
- 1, chan
),
2038 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2042 * Populate new subbuffer.
2044 if (caa_unlikely(offsets
.switch_new_start
))
2045 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2047 if (caa_unlikely(offsets
.switch_new_end
))
2048 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
2050 ctx
->slot_size
= offsets
.size
;
2051 ctx
->pre_offset
= offsets
.begin
;
2052 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
2057 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2059 void lttng_fixup_ringbuffer_tls(void)
2061 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));
2064 void lib_ringbuffer_signal_init(void)
2070 * Block signal for entire process, so only our thread processes
2074 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
2077 PERROR("pthread_sigmask");