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/percpu-defs.h"
67 * Internal structure representing offsets to use at a sub-buffer switch.
69 struct switch_offsets
{
70 unsigned long begin
, end
, old
;
71 size_t pre_header_padding
, size
;
72 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
83 static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier
);
84 #endif /* CONFIG_NO_HZ */
86 static DEFINE_PER_CPU(spinlock_t
, ring_buffer_nohz_lock
);
88 DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
89 EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting
);
92 void lib_ring_buffer_print_errors(struct channel
*chan
,
93 struct lib_ring_buffer
*buf
, int cpu
);
95 void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
96 enum switch_mode mode
);
99 * Must be called under cpu hotplug protection.
101 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
103 struct channel
*chan
= buf
->backend
.chan
;
105 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
106 kfree(buf
->commit_hot
);
107 kfree(buf
->commit_cold
);
109 lib_ring_buffer_backend_free(&buf
->backend
);
113 * lib_ring_buffer_reset - Reset ring buffer to initial values.
116 * Effectively empty the ring buffer. Should be called when the buffer is not
117 * used for writing. The ring buffer can be opened for reading, but the reader
118 * should not be using the iterator concurrently with reset. The previous
119 * current iterator record is reset.
121 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
123 struct channel
*chan
= buf
->backend
.chan
;
124 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
128 * Reset iterator first. It will put the subbuffer if it currently holds
131 lib_ring_buffer_iterator_reset(buf
);
132 v_set(config
, &buf
->offset
, 0);
133 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
134 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
135 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
136 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
138 atomic_long_set(&buf
->consumed
, 0);
139 atomic_set(&buf
->record_disabled
, 0);
140 v_set(config
, &buf
->last_tsc
, 0);
141 lib_ring_buffer_backend_reset(&buf
->backend
);
142 /* Don't reset number of active readers */
143 v_set(config
, &buf
->records_lost_full
, 0);
144 v_set(config
, &buf
->records_lost_wrap
, 0);
145 v_set(config
, &buf
->records_lost_big
, 0);
146 v_set(config
, &buf
->records_count
, 0);
147 v_set(config
, &buf
->records_overrun
, 0);
150 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
153 * channel_reset - Reset channel to initial values.
156 * Effectively empty the channel. Should be called when the channel is not used
157 * for writing. The channel can be opened for reading, but the reader should not
158 * be using the iterator concurrently with reset. The previous current iterator
161 void channel_reset(struct channel
*chan
)
164 * Reset iterators first. Will put the subbuffer if held for reading.
166 channel_iterator_reset(chan
);
167 atomic_set(&chan
->record_disabled
, 0);
168 /* Don't reset commit_count_mask, still valid */
169 channel_backend_reset(&chan
->backend
);
170 /* Don't reset switch/read timer interval */
171 /* Don't reset notifiers and notifier enable bits */
172 /* Don't reset reader reference count */
174 EXPORT_SYMBOL_GPL(channel_reset
);
177 * Must be called under cpu hotplug protection.
179 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
180 struct channel_backend
*chanb
, int cpu
)
182 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
183 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
184 void *priv
= chanb
->priv
;
185 size_t subbuf_header_size
;
189 /* Test for cpu hotplug */
190 if (buf
->backend
.allocated
)
194 * Paranoia: per cpu dynamic allocation is not officially documented as
195 * zeroing the memory, so let's do it here too, just in case.
197 memset(buf
, 0, sizeof(*buf
));
199 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
204 kzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
205 * chan
->backend
.num_subbuf
,
206 1 << INTERNODE_CACHE_SHIFT
),
207 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
208 if (!buf
->commit_hot
) {
214 kzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
215 * chan
->backend
.num_subbuf
,
216 1 << INTERNODE_CACHE_SHIFT
),
217 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
218 if (!buf
->commit_cold
) {
223 init_waitqueue_head(&buf
->read_wait
);
224 init_waitqueue_head(&buf
->write_wait
);
225 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
228 * Write the subbuffer header for first subbuffer so we know the total
229 * duration of data gathering.
231 subbuf_header_size
= config
->cb
.subbuffer_header_size();
232 v_set(config
, &buf
->offset
, subbuf_header_size
);
233 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
234 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
235 config
->cb
.buffer_begin(buf
, tsc
, 0);
236 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
238 if (config
->cb
.buffer_create
) {
239 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
245 * Ensure the buffer is ready before setting it to allocated and setting
247 * Used for cpu hotplug vs cpumask iteration.
250 buf
->backend
.allocated
= 1;
252 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
253 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
254 chan
->backend
.cpumask
));
255 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
262 kfree(buf
->commit_cold
);
264 kfree(buf
->commit_hot
);
266 lib_ring_buffer_backend_free(&buf
->backend
);
270 static void switch_buffer_timer(unsigned long data
)
272 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
273 struct channel
*chan
= buf
->backend
.chan
;
274 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
277 * Only flush buffers periodically if readers are active.
279 if (atomic_long_read(&buf
->active_readers
))
280 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
282 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
283 mod_timer_pinned(&buf
->switch_timer
,
284 jiffies
+ chan
->switch_timer_interval
);
286 mod_timer(&buf
->switch_timer
,
287 jiffies
+ chan
->switch_timer_interval
);
291 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
293 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
295 struct channel
*chan
= buf
->backend
.chan
;
296 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
298 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
300 init_timer(&buf
->switch_timer
);
301 buf
->switch_timer
.function
= switch_buffer_timer
;
302 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
303 buf
->switch_timer
.data
= (unsigned long)buf
;
304 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
305 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
307 add_timer(&buf
->switch_timer
);
308 buf
->switch_timer_enabled
= 1;
312 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
314 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
316 struct channel
*chan
= buf
->backend
.chan
;
318 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
321 del_timer_sync(&buf
->switch_timer
);
322 buf
->switch_timer_enabled
= 0;
326 * Polling timer to check the channels for data.
328 static void read_buffer_timer(unsigned long data
)
330 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
331 struct channel
*chan
= buf
->backend
.chan
;
332 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
334 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
336 if (atomic_long_read(&buf
->active_readers
)
337 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
338 wake_up_interruptible(&buf
->read_wait
);
339 wake_up_interruptible(&chan
->read_wait
);
342 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
343 mod_timer_pinned(&buf
->read_timer
,
344 jiffies
+ chan
->read_timer_interval
);
346 mod_timer(&buf
->read_timer
,
347 jiffies
+ chan
->read_timer_interval
);
351 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
353 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
355 struct channel
*chan
= buf
->backend
.chan
;
356 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
358 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
359 || !chan
->read_timer_interval
360 || buf
->read_timer_enabled
)
363 init_timer(&buf
->read_timer
);
364 buf
->read_timer
.function
= read_buffer_timer
;
365 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
366 buf
->read_timer
.data
= (unsigned long)buf
;
368 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
369 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
371 add_timer(&buf
->read_timer
);
372 buf
->read_timer_enabled
= 1;
376 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
378 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
380 struct channel
*chan
= buf
->backend
.chan
;
381 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
383 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
384 || !chan
->read_timer_interval
385 || !buf
->read_timer_enabled
)
388 del_timer_sync(&buf
->read_timer
);
390 * do one more check to catch data that has been written in the last
393 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
394 wake_up_interruptible(&buf
->read_wait
);
395 wake_up_interruptible(&chan
->read_wait
);
397 buf
->read_timer_enabled
= 0;
400 #ifdef CONFIG_HOTPLUG_CPU
402 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
403 * @nb: notifier block
404 * @action: hotplug action to take
407 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
410 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
411 unsigned long action
,
414 unsigned int cpu
= (unsigned long)hcpu
;
415 struct channel
*chan
= container_of(nb
, struct channel
,
417 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
418 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
420 if (!chan
->cpu_hp_enable
)
423 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
426 case CPU_DOWN_FAILED
:
427 case CPU_DOWN_FAILED_FROZEN
:
429 case CPU_ONLINE_FROZEN
:
430 wake_up_interruptible(&chan
->hp_wait
);
431 lib_ring_buffer_start_switch_timer(buf
);
432 lib_ring_buffer_start_read_timer(buf
);
435 case CPU_DOWN_PREPARE
:
436 case CPU_DOWN_PREPARE_FROZEN
:
437 lib_ring_buffer_stop_switch_timer(buf
);
438 lib_ring_buffer_stop_read_timer(buf
);
442 case CPU_DEAD_FROZEN
:
444 * Performing a buffer switch on a remote CPU. Performed by
445 * the CPU responsible for doing the hotunplug after the target
446 * CPU stopped running completely. Ensures that all data
447 * from that remote CPU is flushed.
449 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
458 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
460 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
461 * that wake-up-tracing generated events are flushed before going idle (in
462 * tick_nohz). We test if the spinlock is locked to deal with the race where
463 * readers try to sample the ring buffer before we perform the switch. We let
464 * the readers retry in that case. If there is data in the buffer, the wake up
465 * is going to forbid the CPU running the reader thread from going idle.
467 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
471 struct channel
*chan
= container_of(nb
, struct channel
,
473 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
474 struct lib_ring_buffer
*buf
;
475 int cpu
= smp_processor_id();
477 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
479 * We don't support keeping the system idle with global buffers
480 * and streaming active. In order to do so, we would need to
481 * sample a non-nohz-cpumask racelessly with the nohz updates
482 * without adding synchronization overhead to nohz. Leave this
483 * use-case out for now.
488 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
490 case TICK_NOHZ_FLUSH
:
491 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
492 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
493 && chan
->read_timer_interval
494 && atomic_long_read(&buf
->active_readers
)
495 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
496 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
497 wake_up_interruptible(&buf
->read_wait
);
498 wake_up_interruptible(&chan
->read_wait
);
500 if (chan
->switch_timer_interval
)
501 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
502 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
505 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
506 lib_ring_buffer_stop_switch_timer(buf
);
507 lib_ring_buffer_stop_read_timer(buf
);
508 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
510 case TICK_NOHZ_RESTART
:
511 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
512 lib_ring_buffer_start_read_timer(buf
);
513 lib_ring_buffer_start_switch_timer(buf
);
514 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
521 void notrace
lib_ring_buffer_tick_nohz_flush(void)
523 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
527 void notrace
lib_ring_buffer_tick_nohz_stop(void)
529 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
533 void notrace
lib_ring_buffer_tick_nohz_restart(void)
535 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
538 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
543 static void channel_unregister_notifiers(struct channel
*chan
)
545 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
548 channel_iterator_unregister_notifiers(chan
);
549 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
552 * Remove the nohz notifier first, so we are certain we stop
555 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
556 &chan
->tick_nohz_notifier
);
558 * ring_buffer_nohz_lock will not be needed below, because
559 * we just removed the notifiers, which were the only source of
562 #endif /* CONFIG_NO_HZ */
563 #ifdef CONFIG_HOTPLUG_CPU
565 chan
->cpu_hp_enable
= 0;
566 for_each_online_cpu(cpu
) {
567 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
569 lib_ring_buffer_stop_switch_timer(buf
);
570 lib_ring_buffer_stop_read_timer(buf
);
573 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
575 for_each_possible_cpu(cpu
) {
576 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
578 lib_ring_buffer_stop_switch_timer(buf
);
579 lib_ring_buffer_stop_read_timer(buf
);
583 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
585 lib_ring_buffer_stop_switch_timer(buf
);
586 lib_ring_buffer_stop_read_timer(buf
);
588 channel_backend_unregister_notifiers(&chan
->backend
);
591 static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer
*buf
)
593 if (!buf
->quiescent
) {
594 buf
->quiescent
= true;
595 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
599 static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer
*buf
)
601 buf
->quiescent
= false;
604 void lib_ring_buffer_set_quiescent_channel(struct channel
*chan
)
607 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
609 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
611 for_each_channel_cpu(cpu
, chan
) {
612 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
615 lib_ring_buffer_set_quiescent(buf
);
619 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
621 lib_ring_buffer_set_quiescent(buf
);
624 EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel
);
626 void lib_ring_buffer_clear_quiescent_channel(struct channel
*chan
)
629 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
631 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
633 for_each_channel_cpu(cpu
, chan
) {
634 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
637 lib_ring_buffer_clear_quiescent(buf
);
641 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
643 lib_ring_buffer_clear_quiescent(buf
);
646 EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel
);
648 static void channel_free(struct channel
*chan
)
650 if (chan
->backend
.release_priv_ops
) {
651 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
653 channel_iterator_free(chan
);
654 channel_backend_free(&chan
->backend
);
659 * channel_create - Create channel.
660 * @config: ring buffer instance configuration
661 * @name: name of the channel
662 * @priv: ring buffer client private data
663 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
664 * address mapping. It is used only by RING_BUFFER_STATIC
665 * configuration. It can be set to NULL for other backends.
666 * @subbuf_size: subbuffer size
667 * @num_subbuf: number of subbuffers
668 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
669 * padding to let readers get those sub-buffers.
670 * Used for live streaming.
671 * @read_timer_interval: Time interval (in us) to wake up pending readers.
674 * Returns NULL on failure.
676 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
677 const char *name
, void *priv
, void *buf_addr
,
679 size_t num_subbuf
, unsigned int switch_timer_interval
,
680 unsigned int read_timer_interval
)
683 struct channel
*chan
;
685 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
686 read_timer_interval
))
689 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
693 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
694 subbuf_size
, num_subbuf
);
698 ret
= channel_iterator_init(chan
);
700 goto error_free_backend
;
702 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
703 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
704 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
705 kref_init(&chan
->ref
);
706 init_waitqueue_head(&chan
->read_wait
);
707 init_waitqueue_head(&chan
->hp_wait
);
709 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
710 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
711 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
712 chan
->tick_nohz_notifier
.notifier_call
=
713 ring_buffer_tick_nohz_callback
;
714 chan
->tick_nohz_notifier
.priority
= ~0U;
715 atomic_notifier_chain_register(&tick_nohz_notifier
,
716 &chan
->tick_nohz_notifier
);
717 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
720 * In case of non-hotplug cpu, if the ring-buffer is allocated
721 * in early initcall, it will not be notified of secondary cpus.
722 * In that off case, we need to allocate for all possible cpus.
724 #ifdef CONFIG_HOTPLUG_CPU
725 chan
->cpu_hp_notifier
.notifier_call
=
726 lib_ring_buffer_cpu_hp_callback
;
727 chan
->cpu_hp_notifier
.priority
= 6;
728 register_cpu_notifier(&chan
->cpu_hp_notifier
);
731 for_each_online_cpu(cpu
) {
732 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
734 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
735 lib_ring_buffer_start_switch_timer(buf
);
736 lib_ring_buffer_start_read_timer(buf
);
737 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
739 chan
->cpu_hp_enable
= 1;
742 for_each_possible_cpu(cpu
) {
743 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
745 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
746 lib_ring_buffer_start_switch_timer(buf
);
747 lib_ring_buffer_start_read_timer(buf
);
748 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
752 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
754 lib_ring_buffer_start_switch_timer(buf
);
755 lib_ring_buffer_start_read_timer(buf
);
761 channel_backend_free(&chan
->backend
);
766 EXPORT_SYMBOL_GPL(channel_create
);
769 void channel_release(struct kref
*kref
)
771 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
776 * channel_destroy - Finalize, wait for q.s. and destroy channel.
777 * @chan: channel to destroy
780 * Call "destroy" callback, finalize channels, and then decrement the
781 * channel reference count. Note that when readers have completed data
782 * consumption of finalized channels, get_subbuf() will return -ENODATA.
783 * They should release their handle at that point. Returns the private
786 void *channel_destroy(struct channel
*chan
)
789 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
792 channel_unregister_notifiers(chan
);
794 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
796 * No need to hold cpu hotplug, because all notifiers have been
799 for_each_channel_cpu(cpu
, chan
) {
800 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
803 if (config
->cb
.buffer_finalize
)
804 config
->cb
.buffer_finalize(buf
,
807 if (buf
->backend
.allocated
)
808 lib_ring_buffer_set_quiescent(buf
);
810 * Perform flush before writing to finalized.
813 ACCESS_ONCE(buf
->finalized
) = 1;
814 wake_up_interruptible(&buf
->read_wait
);
817 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
819 if (config
->cb
.buffer_finalize
)
820 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
821 if (buf
->backend
.allocated
)
822 lib_ring_buffer_set_quiescent(buf
);
824 * Perform flush before writing to finalized.
827 ACCESS_ONCE(buf
->finalized
) = 1;
828 wake_up_interruptible(&buf
->read_wait
);
830 ACCESS_ONCE(chan
->finalized
) = 1;
831 wake_up_interruptible(&chan
->hp_wait
);
832 wake_up_interruptible(&chan
->read_wait
);
833 priv
= chan
->backend
.priv
;
834 kref_put(&chan
->ref
, channel_release
);
837 EXPORT_SYMBOL_GPL(channel_destroy
);
839 struct lib_ring_buffer
*channel_get_ring_buffer(
840 const struct lib_ring_buffer_config
*config
,
841 struct channel
*chan
, int cpu
)
843 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
844 return chan
->backend
.buf
;
846 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
848 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
850 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
852 struct channel
*chan
= buf
->backend
.chan
;
854 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
856 kref_get(&chan
->ref
);
857 lttng_smp_mb__after_atomic();
860 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
862 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
864 struct channel
*chan
= buf
->backend
.chan
;
866 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
867 lttng_smp_mb__before_atomic();
868 atomic_long_dec(&buf
->active_readers
);
869 kref_put(&chan
->ref
, channel_release
);
871 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
874 * Promote compiler barrier to a smp_mb().
875 * For the specific ring buffer case, this IPI call should be removed if the
876 * architecture does not reorder writes. This should eventually be provided by
877 * a separate architecture-specific infrastructure.
879 static void remote_mb(void *info
)
885 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
887 * @consumed: consumed count indicating the position where to read
888 * @produced: produced count, indicates position when to stop reading
890 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
891 * data to read at consumed position, or 0 if the get operation succeeds.
892 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
895 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
896 unsigned long *consumed
, unsigned long *produced
)
898 struct channel
*chan
= buf
->backend
.chan
;
899 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
900 unsigned long consumed_cur
, write_offset
;
904 finalized
= ACCESS_ONCE(buf
->finalized
);
906 * Read finalized before counters.
909 consumed_cur
= atomic_long_read(&buf
->consumed
);
911 * No need to issue a memory barrier between consumed count read and
912 * write offset read, because consumed count can only change
913 * concurrently in overwrite mode, and we keep a sequence counter
914 * identifier derived from the write offset to check we are getting
915 * the same sub-buffer we are expecting (the sub-buffers are atomically
916 * "tagged" upon writes, tags are checked upon read).
918 write_offset
= v_read(config
, &buf
->offset
);
921 * Check that we are not about to read the same subbuffer in
922 * which the writer head is.
924 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
928 *consumed
= consumed_cur
;
929 *produced
= subbuf_trunc(write_offset
, chan
);
935 * The memory barriers __wait_event()/wake_up_interruptible() take care
936 * of "raw_spin_is_locked" memory ordering.
940 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
945 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
948 * lib_ring_buffer_put_snapshot - move consumed counter forward
950 * Should only be called from consumer context.
952 * @consumed_new: new consumed count value
954 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
955 unsigned long consumed_new
)
957 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
958 struct channel
*chan
= bufb
->chan
;
959 unsigned long consumed
;
961 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
964 * Only push the consumed value forward.
965 * If the consumed cmpxchg fails, this is because we have been pushed by
966 * the writer in flight recorder mode.
968 consumed
= atomic_long_read(&buf
->consumed
);
969 while ((long) consumed
- (long) consumed_new
< 0)
970 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
972 /* Wake-up the metadata producer */
973 wake_up_interruptible(&buf
->write_wait
);
975 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
978 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
980 * @consumed: consumed count indicating the position where to read
982 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
983 * data to read at consumed position, or 0 if the get operation succeeds.
984 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
986 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
987 unsigned long consumed
)
989 struct channel
*chan
= buf
->backend
.chan
;
990 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
991 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
995 if (buf
->get_subbuf
) {
997 * Reader is trying to get a subbuffer twice.
999 CHAN_WARN_ON(chan
, 1);
1003 finalized
= ACCESS_ONCE(buf
->finalized
);
1005 * Read finalized before counters.
1008 consumed_cur
= atomic_long_read(&buf
->consumed
);
1009 consumed_idx
= subbuf_index(consumed
, chan
);
1010 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
1012 * Make sure we read the commit count before reading the buffer
1013 * data and the write offset. Correct consumed offset ordering
1014 * wrt commit count is insured by the use of cmpxchg to update
1015 * the consumed offset.
1016 * smp_call_function_single can fail if the remote CPU is offline,
1017 * this is OK because then there is no wmb to execute there.
1018 * If our thread is executing on the same CPU as the on the buffers
1019 * belongs to, we don't have to synchronize it at all. If we are
1020 * migrated, the scheduler will take care of the memory barriers.
1021 * Normally, smp_call_function_single() should ensure program order when
1022 * executing the remote function, which implies that it surrounds the
1023 * function execution with :
1034 * However, smp_call_function_single() does not seem to clearly execute
1035 * such barriers. It depends on spinlock semantic to provide the barrier
1036 * before executing the IPI and, when busy-looping, csd_lock_wait only
1037 * executes smp_mb() when it has to wait for the other CPU.
1039 * I don't trust this code. Therefore, let's add the smp_mb() sequence
1040 * required ourself, even if duplicated. It has no performance impact
1043 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
1044 * read and write vs write. They do not ensure core synchronization. We
1045 * really have to ensure total order between the 3 barriers running on
1048 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1049 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
1050 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
1051 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
1052 /* Total order with IPI handler smp_mb() */
1054 smp_call_function_single(buf
->backend
.cpu
,
1055 remote_mb
, NULL
, 1);
1056 /* Total order with IPI handler smp_mb() */
1060 /* Total order with IPI handler smp_mb() */
1062 smp_call_function(remote_mb
, NULL
, 1);
1063 /* Total order with IPI handler smp_mb() */
1068 * Local rmb to match the remote wmb to read the commit count
1069 * before the buffer data and the write offset.
1074 write_offset
= v_read(config
, &buf
->offset
);
1077 * Check that the buffer we are getting is after or at consumed_cur
1080 if ((long) subbuf_trunc(consumed
, chan
)
1081 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1085 * Check that the subbuffer we are trying to consume has been
1086 * already fully committed.
1088 if (((commit_count
- chan
->backend
.subbuf_size
)
1089 & chan
->commit_count_mask
)
1090 - (buf_trunc(consumed
, chan
)
1091 >> chan
->backend
.num_subbuf_order
)
1096 * Check that we are not about to read the same subbuffer in
1097 * which the writer head is.
1099 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1104 * Failure to get the subbuffer causes a busy-loop retry without going
1105 * to a wait queue. These are caused by short-lived race windows where
1106 * the writer is getting access to a subbuffer we were trying to get
1107 * access to. Also checks that the "consumed" buffer count we are
1108 * looking for matches the one contained in the subbuffer id.
1110 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1111 consumed_idx
, buf_trunc_val(consumed
, chan
));
1114 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1116 buf
->get_subbuf_consumed
= consumed
;
1117 buf
->get_subbuf
= 1;
1123 * The memory barriers __wait_event()/wake_up_interruptible() take care
1124 * of "raw_spin_is_locked" memory ordering.
1128 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1133 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1136 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1139 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1141 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1142 struct channel
*chan
= bufb
->chan
;
1143 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1144 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1146 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1148 if (!buf
->get_subbuf
) {
1150 * Reader puts a subbuffer it did not get.
1152 CHAN_WARN_ON(chan
, 1);
1155 consumed
= buf
->get_subbuf_consumed
;
1156 buf
->get_subbuf
= 0;
1159 * Clear the records_unread counter. (overruns counter)
1160 * Can still be non-zero if a file reader simply grabbed the data
1161 * without using iterators.
1162 * Can be below zero if an iterator is used on a snapshot more than
1165 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1166 v_add(config
, v_read(config
,
1167 &bufb
->array
[read_sb_bindex
]->records_unread
),
1168 &bufb
->records_read
);
1169 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1170 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1171 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1172 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1175 * Exchange the reader subbuffer with the one we put in its place in the
1176 * writer subbuffer table. Expect the original consumed count. If
1177 * update_read_sb_index fails, this is because the writer updated the
1178 * subbuffer concurrently. We should therefore keep the subbuffer we
1179 * currently have: it has become invalid to try reading this sub-buffer
1180 * consumed count value anyway.
1182 consumed_idx
= subbuf_index(consumed
, chan
);
1183 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1184 consumed_idx
, buf_trunc_val(consumed
, chan
));
1186 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1187 * if the writer concurrently updated it.
1190 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1193 * cons_offset is an iterator on all subbuffer offsets between the reader
1194 * position and the writer position. (inclusive)
1197 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1198 struct channel
*chan
,
1199 unsigned long cons_offset
,
1202 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1203 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1205 cons_idx
= subbuf_index(cons_offset
, chan
);
1206 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1207 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1209 if (subbuf_offset(commit_count
, chan
) != 0)
1211 "ring buffer %s, cpu %d: "
1212 "commit count in subbuffer %lu,\n"
1213 "expecting multiples of %lu bytes\n"
1214 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1215 chan
->backend
.name
, cpu
, cons_idx
,
1216 chan
->backend
.subbuf_size
,
1217 commit_count
, commit_count_sb
);
1219 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1220 chan
->backend
.name
, cpu
, commit_count
);
1224 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1225 struct channel
*chan
,
1226 void *priv
, int cpu
)
1228 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1229 unsigned long write_offset
, cons_offset
;
1232 * No need to order commit_count, write_offset and cons_offset reads
1233 * because we execute at teardown when no more writer nor reader
1234 * references are left.
1236 write_offset
= v_read(config
, &buf
->offset
);
1237 cons_offset
= atomic_long_read(&buf
->consumed
);
1238 if (write_offset
!= cons_offset
)
1240 "ring buffer %s, cpu %d: "
1241 "non-consumed data\n"
1242 " [ %lu bytes written, %lu bytes read ]\n",
1243 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1245 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1246 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1249 cons_offset
= subbuf_align(cons_offset
, chan
))
1250 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1255 void lib_ring_buffer_print_errors(struct channel
*chan
,
1256 struct lib_ring_buffer
*buf
, int cpu
)
1258 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1259 void *priv
= chan
->backend
.priv
;
1261 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1262 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1263 "%lu records overrun\n",
1265 v_read(config
, &buf
->records_count
),
1266 v_read(config
, &buf
->records_overrun
));
1268 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1269 "%lu records overrun\n",
1270 chan
->backend
.name
, cpu
,
1271 v_read(config
, &buf
->records_count
),
1272 v_read(config
, &buf
->records_overrun
));
1274 if (v_read(config
, &buf
->records_lost_full
)
1275 || v_read(config
, &buf
->records_lost_wrap
)
1276 || v_read(config
, &buf
->records_lost_big
))
1278 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1279 " [ %lu buffer full, %lu nest buffer wrap-around, "
1280 "%lu event too big ]\n",
1281 chan
->backend
.name
, cpu
,
1282 v_read(config
, &buf
->records_lost_full
),
1283 v_read(config
, &buf
->records_lost_wrap
),
1284 v_read(config
, &buf
->records_lost_big
));
1286 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1290 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1292 * Only executed by SWITCH_FLUSH, which can be issued while tracing is active
1293 * or at buffer finalization (destroy).
1296 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1297 struct channel
*chan
,
1298 struct switch_offsets
*offsets
,
1301 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1302 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1303 unsigned long commit_count
;
1305 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1308 * Order all writes to buffer before the commit count update that will
1309 * determine that the subbuffer is full.
1311 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1313 * Must write slot data before incrementing commit count. This
1314 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1320 v_add(config
, config
->cb
.subbuffer_header_size(),
1321 &buf
->commit_hot
[oldidx
].cc
);
1322 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1323 /* Check if the written buffer has to be delivered */
1324 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1325 commit_count
, oldidx
, tsc
);
1326 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1327 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1332 * lib_ring_buffer_switch_old_end: switch old subbuffer
1334 * Note : offset_old should never be 0 here. It is ok, because we never perform
1335 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1336 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1340 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1341 struct channel
*chan
,
1342 struct switch_offsets
*offsets
,
1345 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1346 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1347 unsigned long commit_count
, padding_size
, data_size
;
1349 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1350 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1351 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1354 * Order all writes to buffer before the commit count update that will
1355 * determine that the subbuffer is full.
1357 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1359 * Must write slot data before incrementing commit count. This
1360 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1366 v_add(config
, padding_size
, &buf
->commit_hot
[oldidx
].cc
);
1367 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1368 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1369 commit_count
, oldidx
, tsc
);
1370 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1371 offsets
->old
+ padding_size
, commit_count
);
1375 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1377 * This code can be executed unordered : writers may already have written to the
1378 * sub-buffer before this code gets executed, caution. The commit makes sure
1379 * that this code is executed before the deliver of this sub-buffer.
1382 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1383 struct channel
*chan
,
1384 struct switch_offsets
*offsets
,
1387 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1388 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1389 unsigned long commit_count
;
1391 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1394 * Order all writes to buffer before the commit count update that will
1395 * determine that the subbuffer is full.
1397 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1399 * Must write slot data before incrementing commit count. This
1400 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1406 v_add(config
, config
->cb
.subbuffer_header_size(),
1407 &buf
->commit_hot
[beginidx
].cc
);
1408 commit_count
= v_read(config
, &buf
->commit_hot
[beginidx
].cc
);
1409 /* Check if the written buffer has to be delivered */
1410 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1411 commit_count
, beginidx
, tsc
);
1412 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1413 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1418 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1420 * Calls subbuffer_set_data_size() to set the data size of the current
1421 * sub-buffer. We do not need to perform check_deliver nor commit here,
1422 * since this task will be done by the "commit" of the event for which
1423 * we are currently doing the space reservation.
1426 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1427 struct channel
*chan
,
1428 struct switch_offsets
*offsets
,
1431 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1432 unsigned long endidx
, data_size
;
1434 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1435 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1436 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1442 * !0 if execution must be aborted.
1445 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1446 struct lib_ring_buffer
*buf
,
1447 struct channel
*chan
,
1448 struct switch_offsets
*offsets
,
1451 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1452 unsigned long off
, reserve_commit_diff
;
1454 offsets
->begin
= v_read(config
, &buf
->offset
);
1455 offsets
->old
= offsets
->begin
;
1456 offsets
->switch_old_start
= 0;
1457 off
= subbuf_offset(offsets
->begin
, chan
);
1459 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1462 * Ensure we flush the header of an empty subbuffer when doing the
1463 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1464 * total data gathering duration even if there were no records saved
1465 * after the last buffer switch.
1466 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1467 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1468 * subbuffer header as appropriate.
1469 * The next record that reserves space will be responsible for
1470 * populating the following subbuffer header. We choose not to populate
1471 * the next subbuffer header here because we want to be able to use
1472 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1473 * buffer flush, which must guarantee that all the buffer content
1474 * (records and header timestamps) are visible to the reader. This is
1475 * required for quiescence guarantees for the fusion merge.
1477 if (mode
!= SWITCH_FLUSH
&& !off
)
1478 return -1; /* we do not have to switch : buffer is empty */
1480 if (unlikely(off
== 0)) {
1481 unsigned long sb_index
, commit_count
;
1484 * We are performing a SWITCH_FLUSH. There may be concurrent
1485 * writes into the buffer if e.g. invoked while performing a
1486 * snapshot on an active trace.
1488 * If the client does not save any header information (sub-buffer
1489 * header size == 0), don't switch empty subbuffer on finalize,
1490 * because it is invalid to deliver a completely empty
1493 if (!config
->cb
.subbuffer_header_size())
1496 /* Test new buffer integrity */
1497 sb_index
= subbuf_index(offsets
->begin
, chan
);
1498 commit_count
= v_read(config
,
1499 &buf
->commit_cold
[sb_index
].cc_sb
);
1500 reserve_commit_diff
=
1501 (buf_trunc(offsets
->begin
, chan
)
1502 >> chan
->backend
.num_subbuf_order
)
1503 - (commit_count
& chan
->commit_count_mask
);
1504 if (likely(reserve_commit_diff
== 0)) {
1505 /* Next subbuffer not being written to. */
1506 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1507 subbuf_trunc(offsets
->begin
, chan
)
1508 - subbuf_trunc((unsigned long)
1509 atomic_long_read(&buf
->consumed
), chan
)
1510 >= chan
->backend
.buf_size
)) {
1512 * We do not overwrite non consumed buffers
1513 * and we are full : don't switch.
1518 * Next subbuffer not being written to, and we
1519 * are either in overwrite mode or the buffer is
1520 * not full. It's safe to write in this new
1526 * Next subbuffer reserve offset does not match the
1527 * commit offset. Don't perform switch in
1528 * producer-consumer and overwrite mode. Caused by
1529 * either a writer OOPS or too many nested writes over a
1530 * reserve/commit pair.
1536 * Need to write the subbuffer start header on finalize.
1538 offsets
->switch_old_start
= 1;
1540 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1541 /* Note: old points to the next subbuf at offset 0 */
1542 offsets
->end
= offsets
->begin
;
1547 * Force a sub-buffer switch. This operation is completely reentrant : can be
1548 * called while tracing is active with absolutely no lock held.
1550 * Note, however, that as a v_cmpxchg is used for some atomic
1551 * operations, this function must be called from the CPU which owns the buffer
1552 * for a ACTIVE flush.
1554 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1556 struct channel
*chan
= buf
->backend
.chan
;
1557 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1558 struct switch_offsets offsets
;
1559 unsigned long oldidx
;
1565 * Perform retryable operations.
1568 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1570 return; /* Switch not needed */
1571 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1575 * Atomically update last_tsc. This update races against concurrent
1576 * atomic updates, but the race will always cause supplementary full TSC
1577 * records, never the opposite (missing a full TSC record when it would
1580 save_last_tsc(config
, buf
, tsc
);
1583 * Push the reader if necessary
1585 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1587 oldidx
= subbuf_index(offsets
.old
, chan
);
1588 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1591 * May need to populate header start on SWITCH_FLUSH.
1593 if (offsets
.switch_old_start
) {
1594 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1595 offsets
.old
+= config
->cb
.subbuffer_header_size();
1599 * Switch old subbuffer.
1601 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1603 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1605 static void remote_switch(void *info
)
1607 struct lib_ring_buffer
*buf
= info
;
1609 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1612 static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
,
1613 enum switch_mode mode
)
1615 struct channel
*chan
= buf
->backend
.chan
;
1616 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1620 * With global synchronization we don't need to use the IPI scheme.
1622 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1623 lib_ring_buffer_switch_slow(buf
, mode
);
1628 * Taking lock on CPU hotplug to ensure two things: first, that the
1629 * target cpu is not taken concurrently offline while we are within
1630 * smp_call_function_single() (I don't trust that get_cpu() on the
1631 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1632 * confirmed)). Secondly, if it happens that the CPU is not online, our
1633 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1634 * CPU hotplug handlers, which can also perform a remote subbuffer
1638 ret
= smp_call_function_single(buf
->backend
.cpu
,
1639 remote_switch
, buf
, 1);
1641 /* Remote CPU is offline, do it ourself. */
1642 lib_ring_buffer_switch_slow(buf
, mode
);
1647 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1649 _lib_ring_buffer_switch_remote(buf
, SWITCH_ACTIVE
);
1651 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1653 /* Switch sub-buffer even if current sub-buffer is empty. */
1654 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
)
1656 _lib_ring_buffer_switch_remote(buf
, SWITCH_FLUSH
);
1658 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty
);
1663 * -ENOSPC if event size is too large for packet.
1664 * -ENOBUFS if there is currently not enough space in buffer for the event.
1665 * -EIO if data cannot be written into the buffer for any other reason.
1668 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1669 struct channel
*chan
,
1670 struct switch_offsets
*offsets
,
1671 struct lib_ring_buffer_ctx
*ctx
)
1673 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1674 unsigned long reserve_commit_diff
, offset_cmp
;
1677 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1678 offsets
->old
= offsets
->begin
;
1679 offsets
->switch_new_start
= 0;
1680 offsets
->switch_new_end
= 0;
1681 offsets
->switch_old_end
= 0;
1682 offsets
->pre_header_padding
= 0;
1684 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1685 if ((int64_t) ctx
->tsc
== -EIO
)
1688 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1689 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1691 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1692 offsets
->switch_new_start
= 1; /* For offsets->begin */
1694 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1696 &offsets
->pre_header_padding
,
1699 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1702 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1703 offsets
->size
> chan
->backend
.subbuf_size
)) {
1704 offsets
->switch_old_end
= 1; /* For offsets->old */
1705 offsets
->switch_new_start
= 1; /* For offsets->begin */
1708 if (unlikely(offsets
->switch_new_start
)) {
1709 unsigned long sb_index
, commit_count
;
1712 * We are typically not filling the previous buffer completely.
1714 if (likely(offsets
->switch_old_end
))
1715 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1716 offsets
->begin
= offsets
->begin
1717 + config
->cb
.subbuffer_header_size();
1718 /* Test new buffer integrity */
1719 sb_index
= subbuf_index(offsets
->begin
, chan
);
1721 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1722 * lib_ring_buffer_check_deliver() has the matching
1723 * memory barriers required around commit_cold cc_sb
1724 * updates to ensure reserve and commit counter updates
1725 * are not seen reordered when updated by another CPU.
1728 commit_count
= v_read(config
,
1729 &buf
->commit_cold
[sb_index
].cc_sb
);
1730 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1732 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1734 * The reserve counter have been concurrently updated
1735 * while we read the commit counter. This means the
1736 * commit counter we read might not match buf->offset
1737 * due to concurrent update. We therefore need to retry.
1741 reserve_commit_diff
=
1742 (buf_trunc(offsets
->begin
, chan
)
1743 >> chan
->backend
.num_subbuf_order
)
1744 - (commit_count
& chan
->commit_count_mask
);
1745 if (likely(reserve_commit_diff
== 0)) {
1746 /* Next subbuffer not being written to. */
1747 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1748 subbuf_trunc(offsets
->begin
, chan
)
1749 - subbuf_trunc((unsigned long)
1750 atomic_long_read(&buf
->consumed
), chan
)
1751 >= chan
->backend
.buf_size
)) {
1753 * We do not overwrite non consumed buffers
1754 * and we are full : record is lost.
1756 v_inc(config
, &buf
->records_lost_full
);
1760 * Next subbuffer not being written to, and we
1761 * are either in overwrite mode or the buffer is
1762 * not full. It's safe to write in this new
1768 * Next subbuffer reserve offset does not match the
1769 * commit offset, and this did not involve update to the
1770 * reserve counter. Drop record in producer-consumer and
1771 * overwrite mode. Caused by either a writer OOPS or
1772 * too many nested writes over a reserve/commit pair.
1774 v_inc(config
, &buf
->records_lost_wrap
);
1778 config
->cb
.record_header_size(config
, chan
,
1780 &offsets
->pre_header_padding
,
1783 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1786 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1787 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1789 * Record too big for subbuffers, report error, don't
1790 * complete the sub-buffer switch.
1792 v_inc(config
, &buf
->records_lost_big
);
1796 * We just made a successful buffer switch and the
1797 * record fits in the new subbuffer. Let's write.
1802 * Record fits in the current buffer and we are not on a switch
1803 * boundary. It's safe to write.
1806 offsets
->end
= offsets
->begin
+ offsets
->size
;
1808 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1810 * The offset_end will fall at the very beginning of the next
1813 offsets
->switch_new_end
= 1; /* For offsets->begin */
1819 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1820 * @ctx: ring buffer context.
1822 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1823 * -EIO for other errors, else returns 0.
1824 * It will take care of sub-buffer switching.
1826 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1828 struct channel
*chan
= ctx
->chan
;
1829 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1830 struct lib_ring_buffer
*buf
;
1831 struct switch_offsets offsets
;
1834 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1835 buf
= per_cpu_ptr(chan
->backend
.buf
, ctx
->cpu
);
1837 buf
= chan
->backend
.buf
;
1843 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1847 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1852 * Atomically update last_tsc. This update races against concurrent
1853 * atomic updates, but the race will always cause supplementary full TSC
1854 * records, never the opposite (missing a full TSC record when it would
1857 save_last_tsc(config
, buf
, ctx
->tsc
);
1860 * Push the reader if necessary
1862 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1865 * Clear noref flag for this subbuffer.
1867 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1868 subbuf_index(offsets
.end
- 1, chan
));
1871 * Switch old subbuffer if needed.
1873 if (unlikely(offsets
.switch_old_end
)) {
1874 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1875 subbuf_index(offsets
.old
- 1, chan
));
1876 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1880 * Populate new subbuffer.
1882 if (unlikely(offsets
.switch_new_start
))
1883 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1885 if (unlikely(offsets
.switch_new_end
))
1886 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1888 ctx
->slot_size
= offsets
.size
;
1889 ctx
->pre_offset
= offsets
.begin
;
1890 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1893 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
1895 int __init
init_lib_ring_buffer_frontend(void)
1899 for_each_possible_cpu(cpu
)
1900 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
1904 module_init(init_lib_ring_buffer_frontend
);
1906 void __exit
exit_lib_ring_buffer_frontend(void)
1910 module_exit(exit_lib_ring_buffer_frontend
);