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
54 #include <linux/delay.h>
55 #include <linux/module.h>
56 #include <linux/percpu.h>
58 #include <wrapper/ringbuffer/config.h>
59 #include <wrapper/ringbuffer/backend.h>
60 #include <wrapper/ringbuffer/frontend.h>
61 #include <wrapper/ringbuffer/iterator.h>
62 #include <wrapper/ringbuffer/nohz.h>
63 #include <wrapper/atomic.h>
64 #include <wrapper/kref.h>
65 #include <wrapper/percpu-defs.h>
68 * Internal structure representing offsets to use at a sub-buffer switch.
70 struct switch_offsets
{
71 unsigned long begin
, end
, old
;
72 size_t pre_header_padding
, size
;
73 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
84 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
85 #endif /* CONFIG_NO_HZ */
87 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
89 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
90 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
93 void lib_ring_buffer_print_errors(struct channel
*chan
,
94 struct lib_ring_buffer
*buf
, int cpu
);
96 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
97 enum switch_mode mode
);
100 * Must be called under cpu hotplug protection.
102 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
104 struct channel
*chan
= buf
->backend
.chan
;
106 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
107 kfree(buf
->commit_hot
);
108 kfree(buf
->commit_cold
);
110 lib_ring_buffer_backend_free(&buf
->backend
);
114 * lib_ring_buffer_reset - Reset ring buffer to initial values.
117 * Effectively empty the ring buffer. Should be called when the buffer is not
118 * used for writing. The ring buffer can be opened for reading, but the reader
119 * should not be using the iterator concurrently with reset. The previous
120 * current iterator record is reset.
122 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
124 struct channel
*chan
= buf
->backend
.chan
;
125 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
129 * Reset iterator first. It will put the subbuffer if it currently holds
132 lib_ring_buffer_iterator_reset(buf
);
133 v_set(config
, &buf
->offset
, 0);
134 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
135 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
136 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
137 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
139 atomic_long_set(&buf
->consumed
, 0);
140 atomic_set(&buf
->record_disabled
, 0);
141 v_set(config
, &buf
->last_tsc
, 0);
142 lib_ring_buffer_backend_reset(&buf
->backend
);
143 /* Don't reset number of active readers */
144 v_set(config
, &buf
->records_lost_full
, 0);
145 v_set(config
, &buf
->records_lost_wrap
, 0);
146 v_set(config
, &buf
->records_lost_big
, 0);
147 v_set(config
, &buf
->records_count
, 0);
148 v_set(config
, &buf
->records_overrun
, 0);
151 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
154 * channel_reset - Reset channel to initial values.
157 * Effectively empty the channel. Should be called when the channel is not used
158 * for writing. The channel can be opened for reading, but the reader should not
159 * be using the iterator concurrently with reset. The previous current iterator
162 void channel_reset(struct channel
*chan
)
165 * Reset iterators first. Will put the subbuffer if held for reading.
167 channel_iterator_reset(chan
);
168 atomic_set(&chan
->record_disabled
, 0);
169 /* Don't reset commit_count_mask, still valid */
170 channel_backend_reset(&chan
->backend
);
171 /* Don't reset switch/read timer interval */
172 /* Don't reset notifiers and notifier enable bits */
173 /* Don't reset reader reference count */
175 EXPORT_SYMBOL_GPL(channel_reset
);
178 * Must be called under cpu hotplug protection.
180 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
181 struct channel_backend
*chanb
, int cpu
)
183 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
184 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
185 void *priv
= chanb
->priv
;
186 size_t subbuf_header_size
;
190 /* Test for cpu hotplug */
191 if (buf
->backend
.allocated
)
195 * Paranoia: per cpu dynamic allocation is not officially documented as
196 * zeroing the memory, so let's do it here too, just in case.
198 memset(buf
, 0, sizeof(*buf
));
200 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
205 kzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
206 * chan
->backend
.num_subbuf
,
207 1 << INTERNODE_CACHE_SHIFT
),
208 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
209 if (!buf
->commit_hot
) {
215 kzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
216 * chan
->backend
.num_subbuf
,
217 1 << INTERNODE_CACHE_SHIFT
),
218 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
219 if (!buf
->commit_cold
) {
224 init_waitqueue_head(&buf
->read_wait
);
225 init_waitqueue_head(&buf
->write_wait
);
226 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
229 * Write the subbuffer header for first subbuffer so we know the total
230 * duration of data gathering.
232 subbuf_header_size
= config
->cb
.subbuffer_header_size();
233 v_set(config
, &buf
->offset
, subbuf_header_size
);
234 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
235 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
236 config
->cb
.buffer_begin(buf
, tsc
, 0);
237 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
239 if (config
->cb
.buffer_create
) {
240 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
246 * Ensure the buffer is ready before setting it to allocated and setting
248 * Used for cpu hotplug vs cpumask iteration.
251 buf
->backend
.allocated
= 1;
253 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
254 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
255 chan
->backend
.cpumask
));
256 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
263 kfree(buf
->commit_cold
);
265 kfree(buf
->commit_hot
);
267 lib_ring_buffer_backend_free(&buf
->backend
);
271 static void switch_buffer_timer(unsigned long data
)
273 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
274 struct channel
*chan
= buf
->backend
.chan
;
275 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
278 * Only flush buffers periodically if readers are active.
280 if (atomic_long_read(&buf
->active_readers
))
281 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
283 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
284 mod_timer_pinned(&buf
->switch_timer
,
285 jiffies
+ chan
->switch_timer_interval
);
287 mod_timer(&buf
->switch_timer
,
288 jiffies
+ chan
->switch_timer_interval
);
292 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
294 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
296 struct channel
*chan
= buf
->backend
.chan
;
297 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
299 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
301 init_timer(&buf
->switch_timer
);
302 buf
->switch_timer
.function
= switch_buffer_timer
;
303 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
304 buf
->switch_timer
.data
= (unsigned long)buf
;
305 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
306 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
308 add_timer(&buf
->switch_timer
);
309 buf
->switch_timer_enabled
= 1;
313 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
315 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
317 struct channel
*chan
= buf
->backend
.chan
;
319 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
322 del_timer_sync(&buf
->switch_timer
);
323 buf
->switch_timer_enabled
= 0;
327 * Polling timer to check the channels for data.
329 static void read_buffer_timer(unsigned long data
)
331 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
332 struct channel
*chan
= buf
->backend
.chan
;
333 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
335 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
337 if (atomic_long_read(&buf
->active_readers
)
338 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
339 wake_up_interruptible(&buf
->read_wait
);
340 wake_up_interruptible(&chan
->read_wait
);
343 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
344 mod_timer_pinned(&buf
->read_timer
,
345 jiffies
+ chan
->read_timer_interval
);
347 mod_timer(&buf
->read_timer
,
348 jiffies
+ chan
->read_timer_interval
);
352 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
354 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
356 struct channel
*chan
= buf
->backend
.chan
;
357 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
359 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
360 || !chan
->read_timer_interval
361 || buf
->read_timer_enabled
)
364 init_timer(&buf
->read_timer
);
365 buf
->read_timer
.function
= read_buffer_timer
;
366 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
367 buf
->read_timer
.data
= (unsigned long)buf
;
369 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
370 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
372 add_timer(&buf
->read_timer
);
373 buf
->read_timer_enabled
= 1;
377 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
379 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
381 struct channel
*chan
= buf
->backend
.chan
;
382 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
384 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
385 || !chan
->read_timer_interval
386 || !buf
->read_timer_enabled
)
389 del_timer_sync(&buf
->read_timer
);
391 * do one more check to catch data that has been written in the last
394 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
395 wake_up_interruptible(&buf
->read_wait
);
396 wake_up_interruptible(&chan
->read_wait
);
398 buf
->read_timer_enabled
= 0;
401 #ifdef CONFIG_HOTPLUG_CPU
403 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
404 * @nb: notifier block
405 * @action: hotplug action to take
408 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
411 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
412 unsigned long action
,
415 unsigned int cpu
= (unsigned long)hcpu
;
416 struct channel
*chan
= container_of(nb
, struct channel
,
418 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
419 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
421 if (!chan
->cpu_hp_enable
)
424 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
427 case CPU_DOWN_FAILED
:
428 case CPU_DOWN_FAILED_FROZEN
:
430 case CPU_ONLINE_FROZEN
:
431 wake_up_interruptible(&chan
->hp_wait
);
432 lib_ring_buffer_start_switch_timer(buf
);
433 lib_ring_buffer_start_read_timer(buf
);
436 case CPU_DOWN_PREPARE
:
437 case CPU_DOWN_PREPARE_FROZEN
:
438 lib_ring_buffer_stop_switch_timer(buf
);
439 lib_ring_buffer_stop_read_timer(buf
);
443 case CPU_DEAD_FROZEN
:
445 * Performing a buffer switch on a remote CPU. Performed by
446 * the CPU responsible for doing the hotunplug after the target
447 * CPU stopped running completely. Ensures that all data
448 * from that remote CPU is flushed.
450 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
459 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
461 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
462 * that wake-up-tracing generated events are flushed before going idle (in
463 * tick_nohz). We test if the spinlock is locked to deal with the race where
464 * readers try to sample the ring buffer before we perform the switch. We let
465 * the readers retry in that case. If there is data in the buffer, the wake up
466 * is going to forbid the CPU running the reader thread from going idle.
468 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
472 struct channel
*chan
= container_of(nb
, struct channel
,
474 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
475 struct lib_ring_buffer
*buf
;
476 int cpu
= smp_processor_id();
478 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
480 * We don't support keeping the system idle with global buffers
481 * and streaming active. In order to do so, we would need to
482 * sample a non-nohz-cpumask racelessly with the nohz updates
483 * without adding synchronization overhead to nohz. Leave this
484 * use-case out for now.
489 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
491 case TICK_NOHZ_FLUSH
:
492 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
493 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
494 && chan
->read_timer_interval
495 && atomic_long_read(&buf
->active_readers
)
496 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
497 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
498 wake_up_interruptible(&buf
->read_wait
);
499 wake_up_interruptible(&chan
->read_wait
);
501 if (chan
->switch_timer_interval
)
502 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
503 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
506 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
507 lib_ring_buffer_stop_switch_timer(buf
);
508 lib_ring_buffer_stop_read_timer(buf
);
509 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
511 case TICK_NOHZ_RESTART
:
512 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
513 lib_ring_buffer_start_read_timer(buf
);
514 lib_ring_buffer_start_switch_timer(buf
);
515 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
522 void notrace
lib_ring_buffer_tick_nohz_flush(void)
524 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
528 void notrace
lib_ring_buffer_tick_nohz_stop(void)
530 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
534 void notrace
lib_ring_buffer_tick_nohz_restart(void)
536 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
539 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
544 static void channel_unregister_notifiers(struct channel
*chan
)
546 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
549 channel_iterator_unregister_notifiers(chan
);
550 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
553 * Remove the nohz notifier first, so we are certain we stop
556 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
557 &chan
->tick_nohz_notifier
);
559 * ring_buffer_nohz_lock will not be needed below, because
560 * we just removed the notifiers, which were the only source of
563 #endif /* CONFIG_NO_HZ */
564 #ifdef CONFIG_HOTPLUG_CPU
566 chan
->cpu_hp_enable
= 0;
567 for_each_online_cpu(cpu
) {
568 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
570 lib_ring_buffer_stop_switch_timer(buf
);
571 lib_ring_buffer_stop_read_timer(buf
);
574 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
576 for_each_possible_cpu(cpu
) {
577 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
579 lib_ring_buffer_stop_switch_timer(buf
);
580 lib_ring_buffer_stop_read_timer(buf
);
584 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
586 lib_ring_buffer_stop_switch_timer(buf
);
587 lib_ring_buffer_stop_read_timer(buf
);
589 channel_backend_unregister_notifiers(&chan
->backend
);
592 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
594 if (!buf
->quiescent
) {
595 buf
->quiescent
= true;
596 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
600 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
602 buf
->quiescent
= false;
605 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
608 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
610 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
612 for_each_channel_cpu(cpu
, chan
) {
613 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
616 lib_ring_buffer_set_quiescent(buf
);
620 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
622 lib_ring_buffer_set_quiescent(buf
);
625 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
627 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
630 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
632 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
634 for_each_channel_cpu(cpu
, chan
) {
635 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
638 lib_ring_buffer_clear_quiescent(buf
);
642 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
644 lib_ring_buffer_clear_quiescent(buf
);
647 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
649 static void channel_free(struct channel
*chan
)
651 if (chan
->backend
.release_priv_ops
) {
652 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
654 channel_iterator_free(chan
);
655 channel_backend_free(&chan
->backend
);
660 * channel_create - Create channel.
661 * @config: ring buffer instance configuration
662 * @name: name of the channel
663 * @priv: ring buffer client private data
664 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
665 * address mapping. It is used only by RING_BUFFER_STATIC
666 * configuration. It can be set to NULL for other backends.
667 * @subbuf_size: subbuffer size
668 * @num_subbuf: number of subbuffers
669 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
670 * padding to let readers get those sub-buffers.
671 * Used for live streaming.
672 * @read_timer_interval: Time interval (in us) to wake up pending readers.
675 * Returns NULL on failure.
677 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
678 const char *name
, void *priv
, void *buf_addr
,
680 size_t num_subbuf
, unsigned int switch_timer_interval
,
681 unsigned int read_timer_interval
)
684 struct channel
*chan
;
686 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
687 read_timer_interval
))
690 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
694 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
695 subbuf_size
, num_subbuf
);
699 ret
= channel_iterator_init(chan
);
701 goto error_free_backend
;
703 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
704 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
705 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
706 kref_init(&chan
->ref
);
707 init_waitqueue_head(&chan
->read_wait
);
708 init_waitqueue_head(&chan
->hp_wait
);
710 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
711 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
712 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
713 chan
->tick_nohz_notifier
.notifier_call
=
714 ring_buffer_tick_nohz_callback
;
715 chan
->tick_nohz_notifier
.priority
= ~0U;
716 atomic_notifier_chain_register(&tick_nohz_notifier
,
717 &chan
->tick_nohz_notifier
);
718 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
721 * In case of non-hotplug cpu, if the ring-buffer is allocated
722 * in early initcall, it will not be notified of secondary cpus.
723 * In that off case, we need to allocate for all possible cpus.
725 #ifdef CONFIG_HOTPLUG_CPU
726 chan
->cpu_hp_notifier
.notifier_call
=
727 lib_ring_buffer_cpu_hp_callback
;
728 chan
->cpu_hp_notifier
.priority
= 6;
729 register_cpu_notifier(&chan
->cpu_hp_notifier
);
732 for_each_online_cpu(cpu
) {
733 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
735 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
736 lib_ring_buffer_start_switch_timer(buf
);
737 lib_ring_buffer_start_read_timer(buf
);
738 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
740 chan
->cpu_hp_enable
= 1;
743 for_each_possible_cpu(cpu
) {
744 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
746 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
747 lib_ring_buffer_start_switch_timer(buf
);
748 lib_ring_buffer_start_read_timer(buf
);
749 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
753 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
755 lib_ring_buffer_start_switch_timer(buf
);
756 lib_ring_buffer_start_read_timer(buf
);
762 channel_backend_free(&chan
->backend
);
767 EXPORT_SYMBOL_GPL(channel_create
);
770 void channel_release(struct kref
*kref
)
772 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
777 * channel_destroy - Finalize, wait for q.s. and destroy channel.
778 * @chan: channel to destroy
781 * Call "destroy" callback, finalize channels, and then decrement the
782 * channel reference count. Note that when readers have completed data
783 * consumption of finalized channels, get_subbuf() will return -ENODATA.
784 * They should release their handle at that point. Returns the private
787 void *channel_destroy(struct channel
*chan
)
790 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
793 channel_unregister_notifiers(chan
);
795 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
797 * No need to hold cpu hotplug, because all notifiers have been
800 for_each_channel_cpu(cpu
, chan
) {
801 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
804 if (config
->cb
.buffer_finalize
)
805 config
->cb
.buffer_finalize(buf
,
808 if (buf
->backend
.allocated
)
809 lib_ring_buffer_set_quiescent(buf
);
811 * Perform flush before writing to finalized.
814 ACCESS_ONCE(buf
->finalized
) = 1;
815 wake_up_interruptible(&buf
->read_wait
);
818 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
820 if (config
->cb
.buffer_finalize
)
821 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
822 if (buf
->backend
.allocated
)
823 lib_ring_buffer_set_quiescent(buf
);
825 * Perform flush before writing to finalized.
828 ACCESS_ONCE(buf
->finalized
) = 1;
829 wake_up_interruptible(&buf
->read_wait
);
831 ACCESS_ONCE(chan
->finalized
) = 1;
832 wake_up_interruptible(&chan
->hp_wait
);
833 wake_up_interruptible(&chan
->read_wait
);
834 priv
= chan
->backend
.priv
;
835 kref_put(&chan
->ref
, channel_release
);
838 EXPORT_SYMBOL_GPL(channel_destroy
);
840 struct lib_ring_buffer
*channel_get_ring_buffer(
841 const struct lib_ring_buffer_config
*config
,
842 struct channel
*chan
, int cpu
)
844 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
845 return chan
->backend
.buf
;
847 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
849 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
851 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
853 struct channel
*chan
= buf
->backend
.chan
;
855 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
857 if (!lttng_kref_get(&chan
->ref
)) {
858 atomic_long_dec(&buf
->active_readers
);
861 lttng_smp_mb__after_atomic();
864 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
866 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
868 struct channel
*chan
= buf
->backend
.chan
;
870 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
871 lttng_smp_mb__before_atomic();
872 atomic_long_dec(&buf
->active_readers
);
873 kref_put(&chan
->ref
, channel_release
);
875 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
878 * Promote compiler barrier to a smp_mb().
879 * For the specific ring buffer case, this IPI call should be removed if the
880 * architecture does not reorder writes. This should eventually be provided by
881 * a separate architecture-specific infrastructure.
883 static void remote_mb(void *info
)
889 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
891 * @consumed: consumed count indicating the position where to read
892 * @produced: produced count, indicates position when to stop reading
894 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
895 * data to read at consumed position, or 0 if the get operation succeeds.
896 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
899 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
900 unsigned long *consumed
, unsigned long *produced
)
902 struct channel
*chan
= buf
->backend
.chan
;
903 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
904 unsigned long consumed_cur
, write_offset
;
908 * First, ensure we perform a "final" flush onto the stream. This will
909 * ensure we create a packet of padding if we encounter an empty
910 * packet. This ensures the time-stamps right before the snapshot is
911 * used as end of packet timestamp.
914 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
917 finalized
= ACCESS_ONCE(buf
->finalized
);
919 * Read finalized before counters.
922 consumed_cur
= atomic_long_read(&buf
->consumed
);
924 * No need to issue a memory barrier between consumed count read and
925 * write offset read, because consumed count can only change
926 * concurrently in overwrite mode, and we keep a sequence counter
927 * identifier derived from the write offset to check we are getting
928 * the same sub-buffer we are expecting (the sub-buffers are atomically
929 * "tagged" upon writes, tags are checked upon read).
931 write_offset
= v_read(config
, &buf
->offset
);
934 * Check that we are not about to read the same subbuffer in
935 * which the writer head is.
937 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
941 *consumed
= consumed_cur
;
942 *produced
= subbuf_trunc(write_offset
, chan
);
948 * The memory barriers __wait_event()/wake_up_interruptible() take care
949 * of "raw_spin_is_locked" memory ordering.
953 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
958 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
961 * lib_ring_buffer_put_snapshot - move consumed counter forward
963 * Should only be called from consumer context.
965 * @consumed_new: new consumed count value
967 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
968 unsigned long consumed_new
)
970 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
971 struct channel
*chan
= bufb
->chan
;
972 unsigned long consumed
;
974 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
977 * Only push the consumed value forward.
978 * If the consumed cmpxchg fails, this is because we have been pushed by
979 * the writer in flight recorder mode.
981 consumed
= atomic_long_read(&buf
->consumed
);
982 while ((long) consumed
- (long) consumed_new
< 0)
983 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
985 /* Wake-up the metadata producer */
986 wake_up_interruptible(&buf
->write_wait
);
988 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
991 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
993 * @consumed: consumed count indicating the position where to read
995 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
996 * data to read at consumed position, or 0 if the get operation succeeds.
997 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
999 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
1000 unsigned long consumed
)
1002 struct channel
*chan
= buf
->backend
.chan
;
1003 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1004 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
1008 if (buf
->get_subbuf
) {
1010 * Reader is trying to get a subbuffer twice.
1012 CHAN_WARN_ON(chan
, 1);
1016 finalized
= ACCESS_ONCE(buf
->finalized
);
1018 * Read finalized before counters.
1021 consumed_cur
= atomic_long_read(&buf
->consumed
);
1022 consumed_idx
= subbuf_index(consumed
, chan
);
1023 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1025 * Make sure we read the commit count before reading the buffer
1026 * data and the write offset. Correct consumed offset ordering
1027 * wrt commit count is insured by the use of cmpxchg to update
1028 * the consumed offset.
1029 * smp_call_function_single can fail if the remote CPU is offline,
1030 * this is OK because then there is no wmb to execute there.
1031 * If our thread is executing on the same CPU as the on the buffers
1032 * belongs to, we don't have to synchronize it at all. If we are
1033 * migrated, the scheduler will take care of the memory barriers.
1034 * Normally, smp_call_function_single() should ensure program order when
1035 * executing the remote function, which implies that it surrounds the
1036 * function execution with :
1047 * However, smp_call_function_single() does not seem to clearly execute
1048 * such barriers. It depends on spinlock semantic to provide the barrier
1049 * before executing the IPI and, when busy-looping, csd_lock_wait only
1050 * executes smp_mb() when it has to wait for the other CPU.
1052 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1053 * required ourself, even if duplicated. It has no performance impact
1056 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1057 * read and write vs write. They do not ensure core synchronization. We
1058 * really have to ensure total order between the 3 barriers running on
1061 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1062 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1063 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1064 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1065 /* Total order with IPI handler smp_mb() */
1067 smp_call_function_single(buf
->backend
.cpu
,
1068 remote_mb
, NULL
, 1);
1069 /* Total order with IPI handler smp_mb() */
1073 /* Total order with IPI handler smp_mb() */
1075 smp_call_function(remote_mb
, NULL
, 1);
1076 /* Total order with IPI handler smp_mb() */
1081 * Local rmb to match the remote wmb to read the commit count
1082 * before the buffer data and the write offset.
1087 write_offset
= v_read(config
, &buf
->offset
);
1090 * Check that the buffer we are getting is after or at consumed_cur
1093 if ((long) subbuf_trunc(consumed
, chan
)
1094 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1098 * Check that the subbuffer we are trying to consume has been
1099 * already fully committed.
1101 if (((commit_count
- chan
->backend
.subbuf_size
)
1102 & chan
->commit_count_mask
)
1103 - (buf_trunc(consumed
, chan
)
1104 >> chan
->backend
.num_subbuf_order
)
1109 * Check that we are not about to read the same subbuffer in
1110 * which the writer head is.
1112 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1117 * Failure to get the subbuffer causes a busy-loop retry without going
1118 * to a wait queue. These are caused by short-lived race windows where
1119 * the writer is getting access to a subbuffer we were trying to get
1120 * access to. Also checks that the "consumed" buffer count we are
1121 * looking for matches the one contained in the subbuffer id.
1123 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1124 consumed_idx
, buf_trunc_val(consumed
, chan
));
1127 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1129 buf
->get_subbuf_consumed
= consumed
;
1130 buf
->get_subbuf
= 1;
1136 * The memory barriers __wait_event()/wake_up_interruptible() take care
1137 * of "raw_spin_is_locked" memory ordering.
1141 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1146 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1149 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1152 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1154 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1155 struct channel
*chan
= bufb
->chan
;
1156 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1157 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1159 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1161 if (!buf
->get_subbuf
) {
1163 * Reader puts a subbuffer it did not get.
1165 CHAN_WARN_ON(chan
, 1);
1168 consumed
= buf
->get_subbuf_consumed
;
1169 buf
->get_subbuf
= 0;
1172 * Clear the records_unread counter. (overruns counter)
1173 * Can still be non-zero if a file reader simply grabbed the data
1174 * without using iterators.
1175 * Can be below zero if an iterator is used on a snapshot more than
1178 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1179 v_add(config
, v_read(config
,
1180 &bufb
->array
[read_sb_bindex
]->records_unread
),
1181 &bufb
->records_read
);
1182 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1183 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1184 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1185 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1188 * Exchange the reader subbuffer with the one we put in its place in the
1189 * writer subbuffer table. Expect the original consumed count. If
1190 * update_read_sb_index fails, this is because the writer updated the
1191 * subbuffer concurrently. We should therefore keep the subbuffer we
1192 * currently have: it has become invalid to try reading this sub-buffer
1193 * consumed count value anyway.
1195 consumed_idx
= subbuf_index(consumed
, chan
);
1196 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1197 consumed_idx
, buf_trunc_val(consumed
, chan
));
1199 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1200 * if the writer concurrently updated it.
1203 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1206 * cons_offset is an iterator on all subbuffer offsets between the reader
1207 * position and the writer position. (inclusive)
1210 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1211 struct channel
*chan
,
1212 unsigned long cons_offset
,
1215 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1216 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1218 cons_idx
= subbuf_index(cons_offset
, chan
);
1219 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1220 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1222 if (subbuf_offset(commit_count
, chan
) != 0)
1224 "ring buffer %s, cpu %d: "
1225 "commit count in subbuffer %lu,\n"
1226 "expecting multiples of %lu bytes\n"
1227 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1228 chan
->backend
.name
, cpu
, cons_idx
,
1229 chan
->backend
.subbuf_size
,
1230 commit_count
, commit_count_sb
);
1232 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1233 chan
->backend
.name
, cpu
, commit_count
);
1237 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1238 struct channel
*chan
,
1239 void *priv
, int cpu
)
1241 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1242 unsigned long write_offset
, cons_offset
;
1245 * No need to order commit_count, write_offset and cons_offset reads
1246 * because we execute at teardown when no more writer nor reader
1247 * references are left.
1249 write_offset
= v_read(config
, &buf
->offset
);
1250 cons_offset
= atomic_long_read(&buf
->consumed
);
1251 if (write_offset
!= cons_offset
)
1253 "ring buffer %s, cpu %d: "
1254 "non-consumed data\n"
1255 " [ %lu bytes written, %lu bytes read ]\n",
1256 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1258 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1259 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1262 cons_offset
= subbuf_align(cons_offset
, chan
))
1263 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1268 void lib_ring_buffer_print_errors(struct channel
*chan
,
1269 struct lib_ring_buffer
*buf
, int cpu
)
1271 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1272 void *priv
= chan
->backend
.priv
;
1274 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1275 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1276 "%lu records overrun\n",
1278 v_read(config
, &buf
->records_count
),
1279 v_read(config
, &buf
->records_overrun
));
1281 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1282 "%lu records overrun\n",
1283 chan
->backend
.name
, cpu
,
1284 v_read(config
, &buf
->records_count
),
1285 v_read(config
, &buf
->records_overrun
));
1287 if (v_read(config
, &buf
->records_lost_full
)
1288 || v_read(config
, &buf
->records_lost_wrap
)
1289 || v_read(config
, &buf
->records_lost_big
))
1291 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1292 " [ %lu buffer full, %lu nest buffer wrap-around, "
1293 "%lu event too big ]\n",
1294 chan
->backend
.name
, cpu
,
1295 v_read(config
, &buf
->records_lost_full
),
1296 v_read(config
, &buf
->records_lost_wrap
),
1297 v_read(config
, &buf
->records_lost_big
));
1299 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1303 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1305 * Only executed by SWITCH_FLUSH, which can be issued while tracing is active
1306 * or at buffer finalization (destroy).
1309 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1310 struct channel
*chan
,
1311 struct switch_offsets
*offsets
,
1314 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1315 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1316 unsigned long commit_count
;
1318 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1321 * Order all writes to buffer before the commit count update that will
1322 * determine that the subbuffer is full.
1324 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1326 * Must write slot data before incrementing commit count. This
1327 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1333 v_add(config
, config
->cb
.subbuffer_header_size(),
1334 &buf
->commit_hot
[oldidx
].cc
);
1335 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1336 /* Check if the written buffer has to be delivered */
1337 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1338 commit_count
, oldidx
, tsc
);
1339 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1340 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1345 * lib_ring_buffer_switch_old_end: switch old subbuffer
1347 * Note : offset_old should never be 0 here. It is ok, because we never perform
1348 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1349 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1353 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1354 struct channel
*chan
,
1355 struct switch_offsets
*offsets
,
1358 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1359 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1360 unsigned long commit_count
, padding_size
, data_size
;
1362 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1363 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1364 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1367 * Order all writes to buffer before the commit count update that will
1368 * determine that the subbuffer is full.
1370 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1372 * Must write slot data before incrementing commit count. This
1373 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1379 v_add(config
, padding_size
, &buf
->commit_hot
[oldidx
].cc
);
1380 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1381 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1382 commit_count
, oldidx
, tsc
);
1383 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1384 offsets
->old
+ padding_size
, commit_count
);
1388 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1390 * This code can be executed unordered : writers may already have written to the
1391 * sub-buffer before this code gets executed, caution. The commit makes sure
1392 * that this code is executed before the deliver of this sub-buffer.
1395 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1396 struct channel
*chan
,
1397 struct switch_offsets
*offsets
,
1400 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1401 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1402 unsigned long commit_count
;
1404 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1407 * Order all writes to buffer before the commit count update that will
1408 * determine that the subbuffer is full.
1410 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1412 * Must write slot data before incrementing commit count. This
1413 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1419 v_add(config
, config
->cb
.subbuffer_header_size(),
1420 &buf
->commit_hot
[beginidx
].cc
);
1421 commit_count
= v_read(config
, &buf
->commit_hot
[beginidx
].cc
);
1422 /* Check if the written buffer has to be delivered */
1423 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1424 commit_count
, beginidx
, tsc
);
1425 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1426 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1431 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1433 * Calls subbuffer_set_data_size() to set the data size of the current
1434 * sub-buffer. We do not need to perform check_deliver nor commit here,
1435 * since this task will be done by the "commit" of the event for which
1436 * we are currently doing the space reservation.
1439 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1440 struct channel
*chan
,
1441 struct switch_offsets
*offsets
,
1444 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1445 unsigned long endidx
, data_size
;
1447 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1448 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1449 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1455 * !0 if execution must be aborted.
1458 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1459 struct lib_ring_buffer
*buf
,
1460 struct channel
*chan
,
1461 struct switch_offsets
*offsets
,
1464 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1465 unsigned long off
, reserve_commit_diff
;
1467 offsets
->begin
= v_read(config
, &buf
->offset
);
1468 offsets
->old
= offsets
->begin
;
1469 offsets
->switch_old_start
= 0;
1470 off
= subbuf_offset(offsets
->begin
, chan
);
1472 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1475 * Ensure we flush the header of an empty subbuffer when doing the
1476 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1477 * total data gathering duration even if there were no records saved
1478 * after the last buffer switch.
1479 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1480 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1481 * subbuffer header as appropriate.
1482 * The next record that reserves space will be responsible for
1483 * populating the following subbuffer header. We choose not to populate
1484 * the next subbuffer header here because we want to be able to use
1485 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1486 * buffer flush, which must guarantee that all the buffer content
1487 * (records and header timestamps) are visible to the reader. This is
1488 * required for quiescence guarantees for the fusion merge.
1490 if (mode
!= SWITCH_FLUSH
&& !off
)
1491 return -1; /* we do not have to switch : buffer is empty */
1493 if (unlikely(off
== 0)) {
1494 unsigned long sb_index
, commit_count
;
1497 * We are performing a SWITCH_FLUSH. There may be concurrent
1498 * writes into the buffer if e.g. invoked while performing a
1499 * snapshot on an active trace.
1501 * If the client does not save any header information (sub-buffer
1502 * header size == 0), don't switch empty subbuffer on finalize,
1503 * because it is invalid to deliver a completely empty
1506 if (!config
->cb
.subbuffer_header_size())
1509 /* Test new buffer integrity */
1510 sb_index
= subbuf_index(offsets
->begin
, chan
);
1511 commit_count
= v_read(config
,
1512 &buf
->commit_cold
[sb_index
].cc_sb
);
1513 reserve_commit_diff
=
1514 (buf_trunc(offsets
->begin
, chan
)
1515 >> chan
->backend
.num_subbuf_order
)
1516 - (commit_count
& chan
->commit_count_mask
);
1517 if (likely(reserve_commit_diff
== 0)) {
1518 /* Next subbuffer not being written to. */
1519 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1520 subbuf_trunc(offsets
->begin
, chan
)
1521 - subbuf_trunc((unsigned long)
1522 atomic_long_read(&buf
->consumed
), chan
)
1523 >= chan
->backend
.buf_size
)) {
1525 * We do not overwrite non consumed buffers
1526 * and we are full : don't switch.
1531 * Next subbuffer not being written to, and we
1532 * are either in overwrite mode or the buffer is
1533 * not full. It's safe to write in this new
1539 * Next subbuffer reserve offset does not match the
1540 * commit offset. Don't perform switch in
1541 * producer-consumer and overwrite mode. Caused by
1542 * either a writer OOPS or too many nested writes over a
1543 * reserve/commit pair.
1549 * Need to write the subbuffer start header on finalize.
1551 offsets
->switch_old_start
= 1;
1553 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1554 /* Note: old points to the next subbuf at offset 0 */
1555 offsets
->end
= offsets
->begin
;
1560 * Force a sub-buffer switch. This operation is completely reentrant : can be
1561 * called while tracing is active with absolutely no lock held.
1563 * Note, however, that as a v_cmpxchg is used for some atomic
1564 * operations, this function must be called from the CPU which owns the buffer
1565 * for a ACTIVE flush.
1567 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1569 struct channel
*chan
= buf
->backend
.chan
;
1570 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1571 struct switch_offsets offsets
;
1572 unsigned long oldidx
;
1578 * Perform retryable operations.
1581 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1583 return; /* Switch not needed */
1584 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1588 * Atomically update last_tsc. This update races against concurrent
1589 * atomic updates, but the race will always cause supplementary full TSC
1590 * records, never the opposite (missing a full TSC record when it would
1593 save_last_tsc(config
, buf
, tsc
);
1596 * Push the reader if necessary
1598 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1600 oldidx
= subbuf_index(offsets
.old
, chan
);
1601 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1604 * May need to populate header start on SWITCH_FLUSH.
1606 if (offsets
.switch_old_start
) {
1607 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1608 offsets
.old
+= config
->cb
.subbuffer_header_size();
1612 * Switch old subbuffer.
1614 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1616 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1618 static void remote_switch(void *info
)
1620 struct lib_ring_buffer
*buf
= info
;
1622 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1625 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1626 enum switch_mode mode
)
1628 struct channel
*chan
= buf
->backend
.chan
;
1629 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1633 * With global synchronization we don't need to use the IPI scheme.
1635 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1636 lib_ring_buffer_switch_slow(buf
, mode
);
1641 * Taking lock on CPU hotplug to ensure two things: first, that the
1642 * target cpu is not taken concurrently offline while we are within
1643 * smp_call_function_single() (I don't trust that get_cpu() on the
1644 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1645 * confirmed)). Secondly, if it happens that the CPU is not online, our
1646 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1647 * CPU hotplug handlers, which can also perform a remote subbuffer
1651 ret
= smp_call_function_single(buf
->backend
.cpu
,
1652 remote_switch
, buf
, 1);
1654 /* Remote CPU is offline, do it ourself. */
1655 lib_ring_buffer_switch_slow(buf
, mode
);
1660 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1662 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1664 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1669 * -ENOSPC if event size is too large for packet.
1670 * -ENOBUFS if there is currently not enough space in buffer for the event.
1671 * -EIO if data cannot be written into the buffer for any other reason.
1674 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1675 struct channel
*chan
,
1676 struct switch_offsets
*offsets
,
1677 struct lib_ring_buffer_ctx
*ctx
)
1679 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1680 unsigned long reserve_commit_diff
, offset_cmp
;
1683 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1684 offsets
->old
= offsets
->begin
;
1685 offsets
->switch_new_start
= 0;
1686 offsets
->switch_new_end
= 0;
1687 offsets
->switch_old_end
= 0;
1688 offsets
->pre_header_padding
= 0;
1690 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1691 if ((int64_t) ctx
->tsc
== -EIO
)
1694 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1695 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1697 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1698 offsets
->switch_new_start
= 1; /* For offsets->begin */
1700 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1702 &offsets
->pre_header_padding
,
1705 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1708 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1709 offsets
->size
> chan
->backend
.subbuf_size
)) {
1710 offsets
->switch_old_end
= 1; /* For offsets->old */
1711 offsets
->switch_new_start
= 1; /* For offsets->begin */
1714 if (unlikely(offsets
->switch_new_start
)) {
1715 unsigned long sb_index
, commit_count
;
1718 * We are typically not filling the previous buffer completely.
1720 if (likely(offsets
->switch_old_end
))
1721 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1722 offsets
->begin
= offsets
->begin
1723 + config
->cb
.subbuffer_header_size();
1724 /* Test new buffer integrity */
1725 sb_index
= subbuf_index(offsets
->begin
, chan
);
1727 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1728 * lib_ring_buffer_check_deliver() has the matching
1729 * memory barriers required around commit_cold cc_sb
1730 * updates to ensure reserve and commit counter updates
1731 * are not seen reordered when updated by another CPU.
1734 commit_count
= v_read(config
,
1735 &buf
->commit_cold
[sb_index
].cc_sb
);
1736 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1738 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1740 * The reserve counter have been concurrently updated
1741 * while we read the commit counter. This means the
1742 * commit counter we read might not match buf->offset
1743 * due to concurrent update. We therefore need to retry.
1747 reserve_commit_diff
=
1748 (buf_trunc(offsets
->begin
, chan
)
1749 >> chan
->backend
.num_subbuf_order
)
1750 - (commit_count
& chan
->commit_count_mask
);
1751 if (likely(reserve_commit_diff
== 0)) {
1752 /* Next subbuffer not being written to. */
1753 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1754 subbuf_trunc(offsets
->begin
, chan
)
1755 - subbuf_trunc((unsigned long)
1756 atomic_long_read(&buf
->consumed
), chan
)
1757 >= chan
->backend
.buf_size
)) {
1759 * We do not overwrite non consumed buffers
1760 * and we are full : record is lost.
1762 v_inc(config
, &buf
->records_lost_full
);
1766 * Next subbuffer not being written to, and we
1767 * are either in overwrite mode or the buffer is
1768 * not full. It's safe to write in this new
1774 * Next subbuffer reserve offset does not match the
1775 * commit offset, and this did not involve update to the
1776 * reserve counter. Drop record in producer-consumer and
1777 * overwrite mode. Caused by either a writer OOPS or
1778 * too many nested writes over a reserve/commit pair.
1780 v_inc(config
, &buf
->records_lost_wrap
);
1784 config
->cb
.record_header_size(config
, chan
,
1786 &offsets
->pre_header_padding
,
1789 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1792 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1793 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1795 * Record too big for subbuffers, report error, don't
1796 * complete the sub-buffer switch.
1798 v_inc(config
, &buf
->records_lost_big
);
1802 * We just made a successful buffer switch and the
1803 * record fits in the new subbuffer. Let's write.
1808 * Record fits in the current buffer and we are not on a switch
1809 * boundary. It's safe to write.
1812 offsets
->end
= offsets
->begin
+ offsets
->size
;
1814 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1816 * The offset_end will fall at the very beginning of the next
1819 offsets
->switch_new_end
= 1; /* For offsets->begin */
1825 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1826 * @ctx: ring buffer context.
1828 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1829 * -EIO for other errors, else returns 0.
1830 * It will take care of sub-buffer switching.
1832 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1834 struct channel
*chan
= ctx
->chan
;
1835 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1836 struct lib_ring_buffer
*buf
;
1837 struct switch_offsets offsets
;
1840 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1841 buf
= per_cpu_ptr(chan
->backend
.buf
, ctx
->cpu
);
1843 buf
= chan
->backend
.buf
;
1849 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1853 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1858 * Atomically update last_tsc. This update races against concurrent
1859 * atomic updates, but the race will always cause supplementary full TSC
1860 * records, never the opposite (missing a full TSC record when it would
1863 save_last_tsc(config
, buf
, ctx
->tsc
);
1866 * Push the reader if necessary
1868 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1871 * Clear noref flag for this subbuffer.
1873 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1874 subbuf_index(offsets
.end
- 1, chan
));
1877 * Switch old subbuffer if needed.
1879 if (unlikely(offsets
.switch_old_end
)) {
1880 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1881 subbuf_index(offsets
.old
- 1, chan
));
1882 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1886 * Populate new subbuffer.
1888 if (unlikely(offsets
.switch_new_start
))
1889 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1891 if (unlikely(offsets
.switch_new_end
))
1892 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1894 ctx
->slot_size
= offsets
.size
;
1895 ctx
->pre_offset
= offsets
.begin
;
1896 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1899 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
1901 int __init
init_lib_ring_buffer_frontend(void)
1905 for_each_possible_cpu(cpu
)
1906 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
1910 module_init(init_lib_ring_buffer_frontend
);
1912 void __exit
exit_lib_ring_buffer_frontend(void)
1916 module_exit(exit_lib_ring_buffer_frontend
);