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
);
96 * Must be called under cpu hotplug protection.
98 void lib_ring_buffer_free(struct lib_ring_buffer
*buf
)
100 struct channel
*chan
= buf
->backend
.chan
;
102 lib_ring_buffer_print_errors(chan
, buf
, buf
->backend
.cpu
);
103 kfree(buf
->commit_hot
);
104 kfree(buf
->commit_cold
);
106 lib_ring_buffer_backend_free(&buf
->backend
);
110 * lib_ring_buffer_reset - Reset ring buffer to initial values.
113 * Effectively empty the ring buffer. Should be called when the buffer is not
114 * used for writing. The ring buffer can be opened for reading, but the reader
115 * should not be using the iterator concurrently with reset. The previous
116 * current iterator record is reset.
118 void lib_ring_buffer_reset(struct lib_ring_buffer
*buf
)
120 struct channel
*chan
= buf
->backend
.chan
;
121 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
125 * Reset iterator first. It will put the subbuffer if it currently holds
128 lib_ring_buffer_iterator_reset(buf
);
129 v_set(config
, &buf
->offset
, 0);
130 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
131 v_set(config
, &buf
->commit_hot
[i
].cc
, 0);
132 v_set(config
, &buf
->commit_hot
[i
].seq
, 0);
133 v_set(config
, &buf
->commit_cold
[i
].cc_sb
, 0);
135 atomic_long_set(&buf
->consumed
, 0);
136 atomic_set(&buf
->record_disabled
, 0);
137 v_set(config
, &buf
->last_tsc
, 0);
138 lib_ring_buffer_backend_reset(&buf
->backend
);
139 /* Don't reset number of active readers */
140 v_set(config
, &buf
->records_lost_full
, 0);
141 v_set(config
, &buf
->records_lost_wrap
, 0);
142 v_set(config
, &buf
->records_lost_big
, 0);
143 v_set(config
, &buf
->records_count
, 0);
144 v_set(config
, &buf
->records_overrun
, 0);
147 EXPORT_SYMBOL_GPL(lib_ring_buffer_reset
);
150 * channel_reset - Reset channel to initial values.
153 * Effectively empty the channel. Should be called when the channel is not used
154 * for writing. The channel can be opened for reading, but the reader should not
155 * be using the iterator concurrently with reset. The previous current iterator
158 void channel_reset(struct channel
*chan
)
161 * Reset iterators first. Will put the subbuffer if held for reading.
163 channel_iterator_reset(chan
);
164 atomic_set(&chan
->record_disabled
, 0);
165 /* Don't reset commit_count_mask, still valid */
166 channel_backend_reset(&chan
->backend
);
167 /* Don't reset switch/read timer interval */
168 /* Don't reset notifiers and notifier enable bits */
169 /* Don't reset reader reference count */
171 EXPORT_SYMBOL_GPL(channel_reset
);
174 * Must be called under cpu hotplug protection.
176 int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
177 struct channel_backend
*chanb
, int cpu
)
179 const struct lib_ring_buffer_config
*config
= &chanb
->config
;
180 struct channel
*chan
= container_of(chanb
, struct channel
, backend
);
181 void *priv
= chanb
->priv
;
182 size_t subbuf_header_size
;
186 /* Test for cpu hotplug */
187 if (buf
->backend
.allocated
)
191 * Paranoia: per cpu dynamic allocation is not officially documented as
192 * zeroing the memory, so let's do it here too, just in case.
194 memset(buf
, 0, sizeof(*buf
));
196 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
, cpu
);
201 kzalloc_node(ALIGN(sizeof(*buf
->commit_hot
)
202 * chan
->backend
.num_subbuf
,
203 1 << INTERNODE_CACHE_SHIFT
),
204 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
205 if (!buf
->commit_hot
) {
211 kzalloc_node(ALIGN(sizeof(*buf
->commit_cold
)
212 * chan
->backend
.num_subbuf
,
213 1 << INTERNODE_CACHE_SHIFT
),
214 GFP_KERNEL
, cpu_to_node(max(cpu
, 0)));
215 if (!buf
->commit_cold
) {
220 init_waitqueue_head(&buf
->read_wait
);
221 init_waitqueue_head(&buf
->write_wait
);
222 raw_spin_lock_init(&buf
->raw_tick_nohz_spinlock
);
225 * Write the subbuffer header for first subbuffer so we know the total
226 * duration of data gathering.
228 subbuf_header_size
= config
->cb
.subbuffer_header_size();
229 v_set(config
, &buf
->offset
, subbuf_header_size
);
230 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_wsb
[0].id
);
231 tsc
= config
->cb
.ring_buffer_clock_read(buf
->backend
.chan
);
232 config
->cb
.buffer_begin(buf
, tsc
, 0);
233 v_add(config
, subbuf_header_size
, &buf
->commit_hot
[0].cc
);
235 if (config
->cb
.buffer_create
) {
236 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
);
242 * Ensure the buffer is ready before setting it to allocated and setting
244 * Used for cpu hotplug vs cpumask iteration.
247 buf
->backend
.allocated
= 1;
249 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
250 CHAN_WARN_ON(chan
, cpumask_test_cpu(cpu
,
251 chan
->backend
.cpumask
));
252 cpumask_set_cpu(cpu
, chan
->backend
.cpumask
);
259 kfree(buf
->commit_cold
);
261 kfree(buf
->commit_hot
);
263 lib_ring_buffer_backend_free(&buf
->backend
);
267 static void switch_buffer_timer(unsigned long data
)
269 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
270 struct channel
*chan
= buf
->backend
.chan
;
271 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
274 * Only flush buffers periodically if readers are active.
276 if (atomic_long_read(&buf
->active_readers
))
277 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
279 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
280 mod_timer_pinned(&buf
->switch_timer
,
281 jiffies
+ chan
->switch_timer_interval
);
283 mod_timer(&buf
->switch_timer
,
284 jiffies
+ chan
->switch_timer_interval
);
288 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
290 static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer
*buf
)
292 struct channel
*chan
= buf
->backend
.chan
;
293 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
295 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
297 init_timer(&buf
->switch_timer
);
298 buf
->switch_timer
.function
= switch_buffer_timer
;
299 buf
->switch_timer
.expires
= jiffies
+ chan
->switch_timer_interval
;
300 buf
->switch_timer
.data
= (unsigned long)buf
;
301 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
302 add_timer_on(&buf
->switch_timer
, buf
->backend
.cpu
);
304 add_timer(&buf
->switch_timer
);
305 buf
->switch_timer_enabled
= 1;
309 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
311 static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer
*buf
)
313 struct channel
*chan
= buf
->backend
.chan
;
315 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
318 del_timer_sync(&buf
->switch_timer
);
319 buf
->switch_timer_enabled
= 0;
323 * Polling timer to check the channels for data.
325 static void read_buffer_timer(unsigned long data
)
327 struct lib_ring_buffer
*buf
= (struct lib_ring_buffer
*)data
;
328 struct channel
*chan
= buf
->backend
.chan
;
329 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
331 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
333 if (atomic_long_read(&buf
->active_readers
)
334 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
335 wake_up_interruptible(&buf
->read_wait
);
336 wake_up_interruptible(&chan
->read_wait
);
339 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
340 mod_timer_pinned(&buf
->read_timer
,
341 jiffies
+ chan
->read_timer_interval
);
343 mod_timer(&buf
->read_timer
,
344 jiffies
+ chan
->read_timer_interval
);
348 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
350 static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer
*buf
)
352 struct channel
*chan
= buf
->backend
.chan
;
353 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
355 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
356 || !chan
->read_timer_interval
357 || buf
->read_timer_enabled
)
360 init_timer(&buf
->read_timer
);
361 buf
->read_timer
.function
= read_buffer_timer
;
362 buf
->read_timer
.expires
= jiffies
+ chan
->read_timer_interval
;
363 buf
->read_timer
.data
= (unsigned long)buf
;
365 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
366 add_timer_on(&buf
->read_timer
, buf
->backend
.cpu
);
368 add_timer(&buf
->read_timer
);
369 buf
->read_timer_enabled
= 1;
373 * Called with ring_buffer_nohz_lock held for per-cpu buffers.
375 static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer
*buf
)
377 struct channel
*chan
= buf
->backend
.chan
;
378 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
380 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
381 || !chan
->read_timer_interval
382 || !buf
->read_timer_enabled
)
385 del_timer_sync(&buf
->read_timer
);
387 * do one more check to catch data that has been written in the last
390 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
391 wake_up_interruptible(&buf
->read_wait
);
392 wake_up_interruptible(&chan
->read_wait
);
394 buf
->read_timer_enabled
= 0;
397 #ifdef CONFIG_HOTPLUG_CPU
399 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
400 * @nb: notifier block
401 * @action: hotplug action to take
404 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
407 int lib_ring_buffer_cpu_hp_callback(struct notifier_block
*nb
,
408 unsigned long action
,
411 unsigned int cpu
= (unsigned long)hcpu
;
412 struct channel
*chan
= container_of(nb
, struct channel
,
414 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
415 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
417 if (!chan
->cpu_hp_enable
)
420 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
423 case CPU_DOWN_FAILED
:
424 case CPU_DOWN_FAILED_FROZEN
:
426 case CPU_ONLINE_FROZEN
:
427 wake_up_interruptible(&chan
->hp_wait
);
428 lib_ring_buffer_start_switch_timer(buf
);
429 lib_ring_buffer_start_read_timer(buf
);
432 case CPU_DOWN_PREPARE
:
433 case CPU_DOWN_PREPARE_FROZEN
:
434 lib_ring_buffer_stop_switch_timer(buf
);
435 lib_ring_buffer_stop_read_timer(buf
);
439 case CPU_DEAD_FROZEN
:
441 * Performing a buffer switch on a remote CPU. Performed by
442 * the CPU responsible for doing the hotunplug after the target
443 * CPU stopped running completely. Ensures that all data
444 * from that remote CPU is flushed.
446 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
455 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
457 * For per-cpu buffers, call the reader wakeups before switching the buffer, so
458 * that wake-up-tracing generated events are flushed before going idle (in
459 * tick_nohz). We test if the spinlock is locked to deal with the race where
460 * readers try to sample the ring buffer before we perform the switch. We let
461 * the readers retry in that case. If there is data in the buffer, the wake up
462 * is going to forbid the CPU running the reader thread from going idle.
464 static int notrace
ring_buffer_tick_nohz_callback(struct notifier_block
*nb
,
468 struct channel
*chan
= container_of(nb
, struct channel
,
470 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
471 struct lib_ring_buffer
*buf
;
472 int cpu
= smp_processor_id();
474 if (config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
) {
476 * We don't support keeping the system idle with global buffers
477 * and streaming active. In order to do so, we would need to
478 * sample a non-nohz-cpumask racelessly with the nohz updates
479 * without adding synchronization overhead to nohz. Leave this
480 * use-case out for now.
485 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
487 case TICK_NOHZ_FLUSH
:
488 raw_spin_lock(&buf
->raw_tick_nohz_spinlock
);
489 if (config
->wakeup
== RING_BUFFER_WAKEUP_BY_TIMER
490 && chan
->read_timer_interval
491 && atomic_long_read(&buf
->active_readers
)
492 && (lib_ring_buffer_poll_deliver(config
, buf
, chan
)
493 || lib_ring_buffer_pending_data(config
, buf
, chan
))) {
494 wake_up_interruptible(&buf
->read_wait
);
495 wake_up_interruptible(&chan
->read_wait
);
497 if (chan
->switch_timer_interval
)
498 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
499 raw_spin_unlock(&buf
->raw_tick_nohz_spinlock
);
502 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
503 lib_ring_buffer_stop_switch_timer(buf
);
504 lib_ring_buffer_stop_read_timer(buf
);
505 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
507 case TICK_NOHZ_RESTART
:
508 spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
509 lib_ring_buffer_start_read_timer(buf
);
510 lib_ring_buffer_start_switch_timer(buf
);
511 spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock
));
518 void notrace
lib_ring_buffer_tick_nohz_flush(void)
520 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_FLUSH
,
524 void notrace
lib_ring_buffer_tick_nohz_stop(void)
526 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_STOP
,
530 void notrace
lib_ring_buffer_tick_nohz_restart(void)
532 atomic_notifier_call_chain(&tick_nohz_notifier
, TICK_NOHZ_RESTART
,
535 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
540 static void channel_unregister_notifiers(struct channel
*chan
)
542 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
545 channel_iterator_unregister_notifiers(chan
);
546 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
549 * Remove the nohz notifier first, so we are certain we stop
552 atomic_notifier_chain_unregister(&tick_nohz_notifier
,
553 &chan
->tick_nohz_notifier
);
555 * ring_buffer_nohz_lock will not be needed below, because
556 * we just removed the notifiers, which were the only source of
559 #endif /* CONFIG_NO_HZ */
560 #ifdef CONFIG_HOTPLUG_CPU
562 chan
->cpu_hp_enable
= 0;
563 for_each_online_cpu(cpu
) {
564 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
566 lib_ring_buffer_stop_switch_timer(buf
);
567 lib_ring_buffer_stop_read_timer(buf
);
570 unregister_cpu_notifier(&chan
->cpu_hp_notifier
);
572 for_each_possible_cpu(cpu
) {
573 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
575 lib_ring_buffer_stop_switch_timer(buf
);
576 lib_ring_buffer_stop_read_timer(buf
);
580 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
582 lib_ring_buffer_stop_switch_timer(buf
);
583 lib_ring_buffer_stop_read_timer(buf
);
585 channel_backend_unregister_notifiers(&chan
->backend
);
588 static void channel_free(struct channel
*chan
)
590 if (chan
->backend
.release_priv_ops
) {
591 chan
->backend
.release_priv_ops(chan
->backend
.priv_ops
);
593 channel_iterator_free(chan
);
594 channel_backend_free(&chan
->backend
);
599 * channel_create - Create channel.
600 * @config: ring buffer instance configuration
601 * @name: name of the channel
602 * @priv: ring buffer client private data
603 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
604 * address mapping. It is used only by RING_BUFFER_STATIC
605 * configuration. It can be set to NULL for other backends.
606 * @subbuf_size: subbuffer size
607 * @num_subbuf: number of subbuffers
608 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
609 * padding to let readers get those sub-buffers.
610 * Used for live streaming.
611 * @read_timer_interval: Time interval (in us) to wake up pending readers.
614 * Returns NULL on failure.
616 struct channel
*channel_create(const struct lib_ring_buffer_config
*config
,
617 const char *name
, void *priv
, void *buf_addr
,
619 size_t num_subbuf
, unsigned int switch_timer_interval
,
620 unsigned int read_timer_interval
)
623 struct channel
*chan
;
625 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
626 read_timer_interval
))
629 chan
= kzalloc(sizeof(struct channel
), GFP_KERNEL
);
633 ret
= channel_backend_init(&chan
->backend
, name
, config
, priv
,
634 subbuf_size
, num_subbuf
);
638 ret
= channel_iterator_init(chan
);
640 goto error_free_backend
;
642 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
643 chan
->switch_timer_interval
= usecs_to_jiffies(switch_timer_interval
);
644 chan
->read_timer_interval
= usecs_to_jiffies(read_timer_interval
);
645 kref_init(&chan
->ref
);
646 init_waitqueue_head(&chan
->read_wait
);
647 init_waitqueue_head(&chan
->hp_wait
);
649 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
650 #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
651 /* Only benefit from NO_HZ idle with per-cpu buffers for now. */
652 chan
->tick_nohz_notifier
.notifier_call
=
653 ring_buffer_tick_nohz_callback
;
654 chan
->tick_nohz_notifier
.priority
= ~0U;
655 atomic_notifier_chain_register(&tick_nohz_notifier
,
656 &chan
->tick_nohz_notifier
);
657 #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
660 * In case of non-hotplug cpu, if the ring-buffer is allocated
661 * in early initcall, it will not be notified of secondary cpus.
662 * In that off case, we need to allocate for all possible cpus.
664 #ifdef CONFIG_HOTPLUG_CPU
665 chan
->cpu_hp_notifier
.notifier_call
=
666 lib_ring_buffer_cpu_hp_callback
;
667 chan
->cpu_hp_notifier
.priority
= 6;
668 register_cpu_notifier(&chan
->cpu_hp_notifier
);
671 for_each_online_cpu(cpu
) {
672 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
674 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
675 lib_ring_buffer_start_switch_timer(buf
);
676 lib_ring_buffer_start_read_timer(buf
);
677 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
679 chan
->cpu_hp_enable
= 1;
682 for_each_possible_cpu(cpu
) {
683 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
685 spin_lock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
686 lib_ring_buffer_start_switch_timer(buf
);
687 lib_ring_buffer_start_read_timer(buf
);
688 spin_unlock(&per_cpu(ring_buffer_nohz_lock
, cpu
));
692 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
694 lib_ring_buffer_start_switch_timer(buf
);
695 lib_ring_buffer_start_read_timer(buf
);
701 channel_backend_free(&chan
->backend
);
706 EXPORT_SYMBOL_GPL(channel_create
);
709 void channel_release(struct kref
*kref
)
711 struct channel
*chan
= container_of(kref
, struct channel
, ref
);
716 * channel_destroy - Finalize, wait for q.s. and destroy channel.
717 * @chan: channel to destroy
720 * Call "destroy" callback, finalize channels, and then decrement the
721 * channel reference count. Note that when readers have completed data
722 * consumption of finalized channels, get_subbuf() will return -ENODATA.
723 * They should release their handle at that point. Returns the private
726 void *channel_destroy(struct channel
*chan
)
729 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
732 channel_unregister_notifiers(chan
);
734 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
736 * No need to hold cpu hotplug, because all notifiers have been
739 for_each_channel_cpu(cpu
, chan
) {
740 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
,
743 if (config
->cb
.buffer_finalize
)
744 config
->cb
.buffer_finalize(buf
,
747 if (buf
->backend
.allocated
)
748 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
750 * Perform flush before writing to finalized.
753 ACCESS_ONCE(buf
->finalized
) = 1;
754 wake_up_interruptible(&buf
->read_wait
);
757 struct lib_ring_buffer
*buf
= chan
->backend
.buf
;
759 if (config
->cb
.buffer_finalize
)
760 config
->cb
.buffer_finalize(buf
, chan
->backend
.priv
, -1);
761 if (buf
->backend
.allocated
)
762 lib_ring_buffer_switch_slow(buf
, SWITCH_FLUSH
);
764 * Perform flush before writing to finalized.
767 ACCESS_ONCE(buf
->finalized
) = 1;
768 wake_up_interruptible(&buf
->read_wait
);
770 ACCESS_ONCE(chan
->finalized
) = 1;
771 wake_up_interruptible(&chan
->hp_wait
);
772 wake_up_interruptible(&chan
->read_wait
);
773 priv
= chan
->backend
.priv
;
774 kref_put(&chan
->ref
, channel_release
);
777 EXPORT_SYMBOL_GPL(channel_destroy
);
779 struct lib_ring_buffer
*channel_get_ring_buffer(
780 const struct lib_ring_buffer_config
*config
,
781 struct channel
*chan
, int cpu
)
783 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
)
784 return chan
->backend
.buf
;
786 return per_cpu_ptr(chan
->backend
.buf
, cpu
);
788 EXPORT_SYMBOL_GPL(channel_get_ring_buffer
);
790 int lib_ring_buffer_open_read(struct lib_ring_buffer
*buf
)
792 struct channel
*chan
= buf
->backend
.chan
;
794 if (!atomic_long_add_unless(&buf
->active_readers
, 1, 1))
796 kref_get(&chan
->ref
);
797 lttng_smp_mb__after_atomic();
800 EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read
);
802 void lib_ring_buffer_release_read(struct lib_ring_buffer
*buf
)
804 struct channel
*chan
= buf
->backend
.chan
;
806 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
807 lttng_smp_mb__before_atomic();
808 atomic_long_dec(&buf
->active_readers
);
809 kref_put(&chan
->ref
, channel_release
);
811 EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read
);
814 * Promote compiler barrier to a smp_mb().
815 * For the specific ring buffer case, this IPI call should be removed if the
816 * architecture does not reorder writes. This should eventually be provided by
817 * a separate architecture-specific infrastructure.
819 static void remote_mb(void *info
)
825 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
827 * @consumed: consumed count indicating the position where to read
828 * @produced: produced count, indicates position when to stop reading
830 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
831 * data to read at consumed position, or 0 if the get operation succeeds.
832 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
835 int lib_ring_buffer_snapshot(struct lib_ring_buffer
*buf
,
836 unsigned long *consumed
, unsigned long *produced
)
838 struct channel
*chan
= buf
->backend
.chan
;
839 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
840 unsigned long consumed_cur
, write_offset
;
844 finalized
= ACCESS_ONCE(buf
->finalized
);
846 * Read finalized before counters.
849 consumed_cur
= atomic_long_read(&buf
->consumed
);
851 * No need to issue a memory barrier between consumed count read and
852 * write offset read, because consumed count can only change
853 * concurrently in overwrite mode, and we keep a sequence counter
854 * identifier derived from the write offset to check we are getting
855 * the same sub-buffer we are expecting (the sub-buffers are atomically
856 * "tagged" upon writes, tags are checked upon read).
858 write_offset
= v_read(config
, &buf
->offset
);
861 * Check that we are not about to read the same subbuffer in
862 * which the writer head is.
864 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
868 *consumed
= consumed_cur
;
869 *produced
= subbuf_trunc(write_offset
, chan
);
875 * The memory barriers __wait_event()/wake_up_interruptible() take care
876 * of "raw_spin_is_locked" memory ordering.
880 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
885 EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot
);
888 * lib_ring_buffer_put_snapshot - move consumed counter forward
890 * Should only be called from consumer context.
892 * @consumed_new: new consumed count value
894 void lib_ring_buffer_move_consumer(struct lib_ring_buffer
*buf
,
895 unsigned long consumed_new
)
897 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
898 struct channel
*chan
= bufb
->chan
;
899 unsigned long consumed
;
901 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
904 * Only push the consumed value forward.
905 * If the consumed cmpxchg fails, this is because we have been pushed by
906 * the writer in flight recorder mode.
908 consumed
= atomic_long_read(&buf
->consumed
);
909 while ((long) consumed
- (long) consumed_new
< 0)
910 consumed
= atomic_long_cmpxchg(&buf
->consumed
, consumed
,
912 /* Wake-up the metadata producer */
913 wake_up_interruptible(&buf
->write_wait
);
915 EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer
);
918 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
920 * @consumed: consumed count indicating the position where to read
922 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
923 * data to read at consumed position, or 0 if the get operation succeeds.
924 * Busy-loop trying to get data if the tick_nohz sequence lock is held.
926 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer
*buf
,
927 unsigned long consumed
)
929 struct channel
*chan
= buf
->backend
.chan
;
930 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
931 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
935 if (buf
->get_subbuf
) {
937 * Reader is trying to get a subbuffer twice.
939 CHAN_WARN_ON(chan
, 1);
943 finalized
= ACCESS_ONCE(buf
->finalized
);
945 * Read finalized before counters.
948 consumed_cur
= atomic_long_read(&buf
->consumed
);
949 consumed_idx
= subbuf_index(consumed
, chan
);
950 commit_count
= v_read(config
, &buf
->commit_cold
[consumed_idx
].cc_sb
);
952 * Make sure we read the commit count before reading the buffer
953 * data and the write offset. Correct consumed offset ordering
954 * wrt commit count is insured by the use of cmpxchg to update
955 * the consumed offset.
956 * smp_call_function_single can fail if the remote CPU is offline,
957 * this is OK because then there is no wmb to execute there.
958 * If our thread is executing on the same CPU as the on the buffers
959 * belongs to, we don't have to synchronize it at all. If we are
960 * migrated, the scheduler will take care of the memory barriers.
961 * Normally, smp_call_function_single() should ensure program order when
962 * executing the remote function, which implies that it surrounds the
963 * function execution with :
974 * However, smp_call_function_single() does not seem to clearly execute
975 * such barriers. It depends on spinlock semantic to provide the barrier
976 * before executing the IPI and, when busy-looping, csd_lock_wait only
977 * executes smp_mb() when it has to wait for the other CPU.
979 * I don't trust this code. Therefore, let's add the smp_mb() sequence
980 * required ourself, even if duplicated. It has no performance impact
983 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
984 * read and write vs write. They do not ensure core synchronization. We
985 * really have to ensure total order between the 3 barriers running on
988 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
989 if (config
->sync
== RING_BUFFER_SYNC_PER_CPU
990 && config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
991 if (raw_smp_processor_id() != buf
->backend
.cpu
) {
992 /* Total order with IPI handler smp_mb() */
994 smp_call_function_single(buf
->backend
.cpu
,
996 /* Total order with IPI handler smp_mb() */
1000 /* Total order with IPI handler smp_mb() */
1002 smp_call_function(remote_mb
, NULL
, 1);
1003 /* Total order with IPI handler smp_mb() */
1008 * Local rmb to match the remote wmb to read the commit count
1009 * before the buffer data and the write offset.
1014 write_offset
= v_read(config
, &buf
->offset
);
1017 * Check that the buffer we are getting is after or at consumed_cur
1020 if ((long) subbuf_trunc(consumed
, chan
)
1021 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
1025 * Check that the subbuffer we are trying to consume has been
1026 * already fully committed.
1028 if (((commit_count
- chan
->backend
.subbuf_size
)
1029 & chan
->commit_count_mask
)
1030 - (buf_trunc(consumed
, chan
)
1031 >> chan
->backend
.num_subbuf_order
)
1036 * Check that we are not about to read the same subbuffer in
1037 * which the writer head is.
1039 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed
, chan
)
1044 * Failure to get the subbuffer causes a busy-loop retry without going
1045 * to a wait queue. These are caused by short-lived race windows where
1046 * the writer is getting access to a subbuffer we were trying to get
1047 * access to. Also checks that the "consumed" buffer count we are
1048 * looking for matches the one contained in the subbuffer id.
1050 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1051 consumed_idx
, buf_trunc_val(consumed
, chan
));
1054 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
1056 buf
->get_subbuf_consumed
= consumed
;
1057 buf
->get_subbuf
= 1;
1063 * The memory barriers __wait_event()/wake_up_interruptible() take care
1064 * of "raw_spin_is_locked" memory ordering.
1068 else if (raw_spin_is_locked(&buf
->raw_tick_nohz_spinlock
))
1073 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf
);
1076 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1079 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer
*buf
)
1081 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
1082 struct channel
*chan
= bufb
->chan
;
1083 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1084 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
1086 CHAN_WARN_ON(chan
, atomic_long_read(&buf
->active_readers
) != 1);
1088 if (!buf
->get_subbuf
) {
1090 * Reader puts a subbuffer it did not get.
1092 CHAN_WARN_ON(chan
, 1);
1095 consumed
= buf
->get_subbuf_consumed
;
1096 buf
->get_subbuf
= 0;
1099 * Clear the records_unread counter. (overruns counter)
1100 * Can still be non-zero if a file reader simply grabbed the data
1101 * without using iterators.
1102 * Can be below zero if an iterator is used on a snapshot more than
1105 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
1106 v_add(config
, v_read(config
,
1107 &bufb
->array
[read_sb_bindex
]->records_unread
),
1108 &bufb
->records_read
);
1109 v_set(config
, &bufb
->array
[read_sb_bindex
]->records_unread
, 0);
1110 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
1111 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
1112 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
1115 * Exchange the reader subbuffer with the one we put in its place in the
1116 * writer subbuffer table. Expect the original consumed count. If
1117 * update_read_sb_index fails, this is because the writer updated the
1118 * subbuffer concurrently. We should therefore keep the subbuffer we
1119 * currently have: it has become invalid to try reading this sub-buffer
1120 * consumed count value anyway.
1122 consumed_idx
= subbuf_index(consumed
, chan
);
1123 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
1124 consumed_idx
, buf_trunc_val(consumed
, chan
));
1126 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1127 * if the writer concurrently updated it.
1130 EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf
);
1133 * cons_offset is an iterator on all subbuffer offsets between the reader
1134 * position and the writer position. (inclusive)
1137 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer
*buf
,
1138 struct channel
*chan
,
1139 unsigned long cons_offset
,
1142 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1143 unsigned long cons_idx
, commit_count
, commit_count_sb
;
1145 cons_idx
= subbuf_index(cons_offset
, chan
);
1146 commit_count
= v_read(config
, &buf
->commit_hot
[cons_idx
].cc
);
1147 commit_count_sb
= v_read(config
, &buf
->commit_cold
[cons_idx
].cc_sb
);
1149 if (subbuf_offset(commit_count
, chan
) != 0)
1151 "ring buffer %s, cpu %d: "
1152 "commit count in subbuffer %lu,\n"
1153 "expecting multiples of %lu bytes\n"
1154 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1155 chan
->backend
.name
, cpu
, cons_idx
,
1156 chan
->backend
.subbuf_size
,
1157 commit_count
, commit_count_sb
);
1159 printk(KERN_DEBUG
"ring buffer: %s, cpu %d: %lu bytes committed\n",
1160 chan
->backend
.name
, cpu
, commit_count
);
1164 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer
*buf
,
1165 struct channel
*chan
,
1166 void *priv
, int cpu
)
1168 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1169 unsigned long write_offset
, cons_offset
;
1172 * No need to order commit_count, write_offset and cons_offset reads
1173 * because we execute at teardown when no more writer nor reader
1174 * references are left.
1176 write_offset
= v_read(config
, &buf
->offset
);
1177 cons_offset
= atomic_long_read(&buf
->consumed
);
1178 if (write_offset
!= cons_offset
)
1180 "ring buffer %s, cpu %d: "
1181 "non-consumed data\n"
1182 " [ %lu bytes written, %lu bytes read ]\n",
1183 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1185 for (cons_offset
= atomic_long_read(&buf
->consumed
);
1186 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1189 cons_offset
= subbuf_align(cons_offset
, chan
))
1190 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1195 void lib_ring_buffer_print_errors(struct channel
*chan
,
1196 struct lib_ring_buffer
*buf
, int cpu
)
1198 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1199 void *priv
= chan
->backend
.priv
;
1201 if (!strcmp(chan
->backend
.name
, "relay-metadata")) {
1202 printk(KERN_DEBUG
"ring buffer %s: %lu records written, "
1203 "%lu records overrun\n",
1205 v_read(config
, &buf
->records_count
),
1206 v_read(config
, &buf
->records_overrun
));
1208 printk(KERN_DEBUG
"ring buffer %s, cpu %d: %lu records written, "
1209 "%lu records overrun\n",
1210 chan
->backend
.name
, cpu
,
1211 v_read(config
, &buf
->records_count
),
1212 v_read(config
, &buf
->records_overrun
));
1214 if (v_read(config
, &buf
->records_lost_full
)
1215 || v_read(config
, &buf
->records_lost_wrap
)
1216 || v_read(config
, &buf
->records_lost_big
))
1218 "ring buffer %s, cpu %d: records were lost. Caused by:\n"
1219 " [ %lu buffer full, %lu nest buffer wrap-around, "
1220 "%lu event too big ]\n",
1221 chan
->backend
.name
, cpu
,
1222 v_read(config
, &buf
->records_lost_full
),
1223 v_read(config
, &buf
->records_lost_wrap
),
1224 v_read(config
, &buf
->records_lost_big
));
1226 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
);
1230 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1232 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1235 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer
*buf
,
1236 struct channel
*chan
,
1237 struct switch_offsets
*offsets
,
1240 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1241 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1242 unsigned long commit_count
;
1244 config
->cb
.buffer_begin(buf
, tsc
, oldidx
);
1247 * Order all writes to buffer before the commit count update that will
1248 * determine that the subbuffer is full.
1250 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1252 * Must write slot data before incrementing commit count. This
1253 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1259 v_add(config
, config
->cb
.subbuffer_header_size(),
1260 &buf
->commit_hot
[oldidx
].cc
);
1261 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1262 /* Check if the written buffer has to be delivered */
1263 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1264 commit_count
, oldidx
, tsc
);
1265 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1266 offsets
->old
+ config
->cb
.subbuffer_header_size(),
1271 * lib_ring_buffer_switch_old_end: switch old subbuffer
1273 * Note : offset_old should never be 0 here. It is ok, because we never perform
1274 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1275 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1279 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer
*buf
,
1280 struct channel
*chan
,
1281 struct switch_offsets
*offsets
,
1284 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1285 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1286 unsigned long commit_count
, padding_size
, data_size
;
1288 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1289 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1290 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
);
1293 * Order all writes to buffer before the commit count update that will
1294 * determine that the subbuffer is full.
1296 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1298 * Must write slot data before incrementing commit count. This
1299 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1305 v_add(config
, padding_size
, &buf
->commit_hot
[oldidx
].cc
);
1306 commit_count
= v_read(config
, &buf
->commit_hot
[oldidx
].cc
);
1307 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1308 commit_count
, oldidx
, tsc
);
1309 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1310 offsets
->old
+ padding_size
, commit_count
);
1314 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1316 * This code can be executed unordered : writers may already have written to the
1317 * sub-buffer before this code gets executed, caution. The commit makes sure
1318 * that this code is executed before the deliver of this sub-buffer.
1321 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer
*buf
,
1322 struct channel
*chan
,
1323 struct switch_offsets
*offsets
,
1326 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1327 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1328 unsigned long commit_count
;
1330 config
->cb
.buffer_begin(buf
, tsc
, beginidx
);
1333 * Order all writes to buffer before the commit count update that will
1334 * determine that the subbuffer is full.
1336 if (config
->ipi
== RING_BUFFER_IPI_BARRIER
) {
1338 * Must write slot data before incrementing commit count. This
1339 * compiler barrier is upgraded into a smp_mb() by the IPI sent
1345 v_add(config
, config
->cb
.subbuffer_header_size(),
1346 &buf
->commit_hot
[beginidx
].cc
);
1347 commit_count
= v_read(config
, &buf
->commit_hot
[beginidx
].cc
);
1348 /* Check if the written buffer has to be delivered */
1349 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1350 commit_count
, beginidx
, tsc
);
1351 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1352 offsets
->begin
+ config
->cb
.subbuffer_header_size(),
1357 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1359 * Calls subbuffer_set_data_size() to set the data size of the current
1360 * sub-buffer. We do not need to perform check_deliver nor commit here,
1361 * since this task will be done by the "commit" of the event for which
1362 * we are currently doing the space reservation.
1365 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer
*buf
,
1366 struct channel
*chan
,
1367 struct switch_offsets
*offsets
,
1370 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1371 unsigned long endidx
, data_size
;
1373 endidx
= subbuf_index(offsets
->end
- 1, chan
);
1374 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1375 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
);
1381 * !0 if execution must be aborted.
1384 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1385 struct lib_ring_buffer
*buf
,
1386 struct channel
*chan
,
1387 struct switch_offsets
*offsets
,
1390 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1391 unsigned long off
, reserve_commit_diff
;
1393 offsets
->begin
= v_read(config
, &buf
->offset
);
1394 offsets
->old
= offsets
->begin
;
1395 offsets
->switch_old_start
= 0;
1396 off
= subbuf_offset(offsets
->begin
, chan
);
1398 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1401 * Ensure we flush the header of an empty subbuffer when doing the
1402 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1403 * total data gathering duration even if there were no records saved
1404 * after the last buffer switch.
1405 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1406 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1407 * subbuffer header as appropriate.
1408 * The next record that reserves space will be responsible for
1409 * populating the following subbuffer header. We choose not to populate
1410 * the next subbuffer header here because we want to be able to use
1411 * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop
1412 * buffer flush, which must guarantee that all the buffer content
1413 * (records and header timestamps) are visible to the reader. This is
1414 * required for quiescence guarantees for the fusion merge.
1416 if (mode
!= SWITCH_FLUSH
&& !off
)
1417 return -1; /* we do not have to switch : buffer is empty */
1419 if (unlikely(off
== 0)) {
1420 unsigned long sb_index
, commit_count
;
1423 * We are performing a SWITCH_FLUSH. At this stage, there are no
1424 * concurrent writes into the buffer.
1426 * The client does not save any header information. Don't
1427 * switch empty subbuffer on finalize, because it is invalid to
1428 * deliver a completely empty subbuffer.
1430 if (!config
->cb
.subbuffer_header_size())
1433 /* Test new buffer integrity */
1434 sb_index
= subbuf_index(offsets
->begin
, chan
);
1435 commit_count
= v_read(config
,
1436 &buf
->commit_cold
[sb_index
].cc_sb
);
1437 reserve_commit_diff
=
1438 (buf_trunc(offsets
->begin
, chan
)
1439 >> chan
->backend
.num_subbuf_order
)
1440 - (commit_count
& chan
->commit_count_mask
);
1441 if (likely(reserve_commit_diff
== 0)) {
1442 /* Next subbuffer not being written to. */
1443 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1444 subbuf_trunc(offsets
->begin
, chan
)
1445 - subbuf_trunc((unsigned long)
1446 atomic_long_read(&buf
->consumed
), chan
)
1447 >= chan
->backend
.buf_size
)) {
1449 * We do not overwrite non consumed buffers
1450 * and we are full : don't switch.
1455 * Next subbuffer not being written to, and we
1456 * are either in overwrite mode or the buffer is
1457 * not full. It's safe to write in this new
1463 * Next subbuffer reserve offset does not match the
1464 * commit offset. Don't perform switch in
1465 * producer-consumer and overwrite mode. Caused by
1466 * either a writer OOPS or too many nested writes over a
1467 * reserve/commit pair.
1473 * Need to write the subbuffer start header on finalize.
1475 offsets
->switch_old_start
= 1;
1477 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1478 /* Note: old points to the next subbuf at offset 0 */
1479 offsets
->end
= offsets
->begin
;
1484 * Force a sub-buffer switch. This operation is completely reentrant : can be
1485 * called while tracing is active with absolutely no lock held.
1487 * Note, however, that as a v_cmpxchg is used for some atomic
1488 * operations, this function must be called from the CPU which owns the buffer
1489 * for a ACTIVE flush.
1491 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
, enum switch_mode mode
)
1493 struct channel
*chan
= buf
->backend
.chan
;
1494 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1495 struct switch_offsets offsets
;
1496 unsigned long oldidx
;
1502 * Perform retryable operations.
1505 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1507 return; /* Switch not needed */
1508 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1512 * Atomically update last_tsc. This update races against concurrent
1513 * atomic updates, but the race will always cause supplementary full TSC
1514 * records, never the opposite (missing a full TSC record when it would
1517 save_last_tsc(config
, buf
, tsc
);
1520 * Push the reader if necessary
1522 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1524 oldidx
= subbuf_index(offsets
.old
, chan
);
1525 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
);
1528 * May need to populate header start on SWITCH_FLUSH.
1530 if (offsets
.switch_old_start
) {
1531 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
);
1532 offsets
.old
+= config
->cb
.subbuffer_header_size();
1536 * Switch old subbuffer.
1538 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
);
1540 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow
);
1542 static void remote_switch(void *info
)
1544 struct lib_ring_buffer
*buf
= info
;
1546 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1549 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
)
1551 struct channel
*chan
= buf
->backend
.chan
;
1552 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1556 * With global synchronization we don't need to use the IPI scheme.
1558 if (config
->sync
== RING_BUFFER_SYNC_GLOBAL
) {
1559 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1564 * Taking lock on CPU hotplug to ensure two things: first, that the
1565 * target cpu is not taken concurrently offline while we are within
1566 * smp_call_function_single() (I don't trust that get_cpu() on the
1567 * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be
1568 * confirmed)). Secondly, if it happens that the CPU is not online, our
1569 * own call to lib_ring_buffer_switch_slow() needs to be protected from
1570 * CPU hotplug handlers, which can also perform a remote subbuffer
1574 ret
= smp_call_function_single(buf
->backend
.cpu
,
1575 remote_switch
, buf
, 1);
1577 /* Remote CPU is offline, do it ourself. */
1578 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
1582 EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote
);
1587 * -ENOSPC if event size is too large for packet.
1588 * -ENOBUFS if there is currently not enough space in buffer for the event.
1589 * -EIO if data cannot be written into the buffer for any other reason.
1592 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer
*buf
,
1593 struct channel
*chan
,
1594 struct switch_offsets
*offsets
,
1595 struct lib_ring_buffer_ctx
*ctx
)
1597 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1598 unsigned long reserve_commit_diff
, offset_cmp
;
1601 offsets
->begin
= offset_cmp
= v_read(config
, &buf
->offset
);
1602 offsets
->old
= offsets
->begin
;
1603 offsets
->switch_new_start
= 0;
1604 offsets
->switch_new_end
= 0;
1605 offsets
->switch_old_end
= 0;
1606 offsets
->pre_header_padding
= 0;
1608 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1609 if ((int64_t) ctx
->tsc
== -EIO
)
1612 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1613 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1615 if (unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1616 offsets
->switch_new_start
= 1; /* For offsets->begin */
1618 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1620 &offsets
->pre_header_padding
,
1623 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1626 if (unlikely(subbuf_offset(offsets
->begin
, chan
) +
1627 offsets
->size
> chan
->backend
.subbuf_size
)) {
1628 offsets
->switch_old_end
= 1; /* For offsets->old */
1629 offsets
->switch_new_start
= 1; /* For offsets->begin */
1632 if (unlikely(offsets
->switch_new_start
)) {
1633 unsigned long sb_index
, commit_count
;
1636 * We are typically not filling the previous buffer completely.
1638 if (likely(offsets
->switch_old_end
))
1639 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1640 offsets
->begin
= offsets
->begin
1641 + config
->cb
.subbuffer_header_size();
1642 /* Test new buffer integrity */
1643 sb_index
= subbuf_index(offsets
->begin
, chan
);
1645 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1646 * lib_ring_buffer_check_deliver() has the matching
1647 * memory barriers required around commit_cold cc_sb
1648 * updates to ensure reserve and commit counter updates
1649 * are not seen reordered when updated by another CPU.
1652 commit_count
= v_read(config
,
1653 &buf
->commit_cold
[sb_index
].cc_sb
);
1654 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1656 if (unlikely(offset_cmp
!= v_read(config
, &buf
->offset
))) {
1658 * The reserve counter have been concurrently updated
1659 * while we read the commit counter. This means the
1660 * commit counter we read might not match buf->offset
1661 * due to concurrent update. We therefore need to retry.
1665 reserve_commit_diff
=
1666 (buf_trunc(offsets
->begin
, chan
)
1667 >> chan
->backend
.num_subbuf_order
)
1668 - (commit_count
& chan
->commit_count_mask
);
1669 if (likely(reserve_commit_diff
== 0)) {
1670 /* Next subbuffer not being written to. */
1671 if (unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1672 subbuf_trunc(offsets
->begin
, chan
)
1673 - subbuf_trunc((unsigned long)
1674 atomic_long_read(&buf
->consumed
), chan
)
1675 >= chan
->backend
.buf_size
)) {
1677 * We do not overwrite non consumed buffers
1678 * and we are full : record is lost.
1680 v_inc(config
, &buf
->records_lost_full
);
1684 * Next subbuffer not being written to, and we
1685 * are either in overwrite mode or the buffer is
1686 * not full. It's safe to write in this new
1692 * Next subbuffer reserve offset does not match the
1693 * commit offset, and this did not involve update to the
1694 * reserve counter. Drop record in producer-consumer and
1695 * overwrite mode. Caused by either a writer OOPS or
1696 * too many nested writes over a reserve/commit pair.
1698 v_inc(config
, &buf
->records_lost_wrap
);
1702 config
->cb
.record_header_size(config
, chan
,
1704 &offsets
->pre_header_padding
,
1707 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1710 if (unlikely(subbuf_offset(offsets
->begin
, chan
)
1711 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1713 * Record too big for subbuffers, report error, don't
1714 * complete the sub-buffer switch.
1716 v_inc(config
, &buf
->records_lost_big
);
1720 * We just made a successful buffer switch and the
1721 * record fits in the new subbuffer. Let's write.
1726 * Record fits in the current buffer and we are not on a switch
1727 * boundary. It's safe to write.
1730 offsets
->end
= offsets
->begin
+ offsets
->size
;
1732 if (unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1734 * The offset_end will fall at the very beginning of the next
1737 offsets
->switch_new_end
= 1; /* For offsets->begin */
1743 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1744 * @ctx: ring buffer context.
1746 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1747 * -EIO for other errors, else returns 0.
1748 * It will take care of sub-buffer switching.
1750 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
)
1752 struct channel
*chan
= ctx
->chan
;
1753 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1754 struct lib_ring_buffer
*buf
;
1755 struct switch_offsets offsets
;
1758 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1759 buf
= per_cpu_ptr(chan
->backend
.buf
, ctx
->cpu
);
1761 buf
= chan
->backend
.buf
;
1767 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1771 } while (unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1776 * Atomically update last_tsc. This update races against concurrent
1777 * atomic updates, but the race will always cause supplementary full TSC
1778 * records, never the opposite (missing a full TSC record when it would
1781 save_last_tsc(config
, buf
, ctx
->tsc
);
1784 * Push the reader if necessary
1786 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1789 * Clear noref flag for this subbuffer.
1791 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1792 subbuf_index(offsets
.end
- 1, chan
));
1795 * Switch old subbuffer if needed.
1797 if (unlikely(offsets
.switch_old_end
)) {
1798 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1799 subbuf_index(offsets
.old
- 1, chan
));
1800 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
);
1804 * Populate new subbuffer.
1806 if (unlikely(offsets
.switch_new_start
))
1807 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
);
1809 if (unlikely(offsets
.switch_new_end
))
1810 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
);
1812 ctx
->slot_size
= offsets
.size
;
1813 ctx
->pre_offset
= offsets
.begin
;
1814 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1817 EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow
);
1819 int __init
init_lib_ring_buffer_frontend(void)
1823 for_each_possible_cpu(cpu
)
1824 spin_lock_init(&per_cpu(ring_buffer_nohz_lock
, cpu
));
1828 module_init(init_lib_ring_buffer_frontend
);
1830 void __exit
exit_lib_ring_buffer_frontend(void)
1834 module_exit(exit_lib_ring_buffer_frontend
);