Skip to content

Commit 72c9ddf

Browse files
davem330rostedt
authored andcommitted
ring-buffer: Make non-consuming read less expensive with lots of cpus.
When performing a non-consuming read, a synchronize_sched() is performed once for every cpu which is actively tracing. This is very expensive, and can make it take several seconds to open up the 'trace' file with lots of cpus. Only one synchronize_sched() call is actually necessary. What is desired is for all cpus to see the disabling state change. So we transform the existing sequence: for_each_cpu() { ring_buffer_read_start(); } where each ring_buffer_start() call performs a synchronize_sched(), into the following: for_each_cpu() { ring_buffer_read_prepare(); } ring_buffer_read_prepare_sync(); for_each_cpu() { ring_buffer_read_start(); } wherein only the single ring_buffer_read_prepare_sync() call needs to do the synchronize_sched(). The first phase, via ring_buffer_read_prepare(), allocates the 'iter' memory and increments ->record_disabled. In the second phase, ring_buffer_read_prepare_sync() makes sure this ->record_disabled state is visible fully to all cpus. And in the final third phase, the ring_buffer_read_start() calls reset the 'iter' objects allocated in the first phase since we now know that none of the cpus are adding trace entries any more. This makes openning the 'trace' file nearly instantaneous on a sparc64 Niagara2 box with 128 cpus tracing. Signed-off-by: David S. Miller <davem@davemloft.net> LKML-Reference: <20100420.154711.11246950.davem@davemloft.net> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 62b915f commit 72c9ddf

3 files changed

Lines changed: 65 additions & 14 deletions

File tree

‎include/linux/ring_buffer.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
127127
unsigned long *lost_events);
128128

129129
struct ring_buffer_iter *
130-
ring_buffer_read_start(struct ring_buffer *buffer, int cpu);
130+
ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu);
131+
void ring_buffer_read_prepare_sync(void);
132+
void ring_buffer_read_start(struct ring_buffer_iter *iter);
131133
void ring_buffer_read_finish(struct ring_buffer_iter *iter);
132134

133135
struct ring_buffer_event *

‎kernel/trace/ring_buffer.c‎

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3332,23 +3332,30 @@ ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
33323332
EXPORT_SYMBOL_GPL(ring_buffer_consume);
33333333

33343334
/**
3335-
* ring_buffer_read_start - start a non consuming read of the buffer
3335+
* ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
33363336
* @buffer: The ring buffer to read from
33373337
* @cpu: The cpu buffer to iterate over
33383338
*
3339-
* This starts up an iteration through the buffer. It also disables
3340-
* the recording to the buffer until the reading is finished.
3341-
* This prevents the reading from being corrupted. This is not
3342-
* a consuming read, so a producer is not expected.
3339+
* This performs the initial preparations necessary to iterate
3340+
* through the buffer. Memory is allocated, buffer recording
3341+
* is disabled, and the iterator pointer is returned to the caller.
33433342
*
3344-
* Must be paired with ring_buffer_finish.
3343+
* Disabling buffer recordng prevents the reading from being
3344+
* corrupted. This is not a consuming read, so a producer is not
3345+
* expected.
3346+
*
3347+
* After a sequence of ring_buffer_read_prepare calls, the user is
3348+
* expected to make at least one call to ring_buffer_prepare_sync.
3349+
* Afterwards, ring_buffer_read_start is invoked to get things going
3350+
* for real.
3351+
*
3352+
* This overall must be paired with ring_buffer_finish.
33453353
*/
33463354
struct ring_buffer_iter *
3347-
ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3355+
ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
33483356
{
33493357
struct ring_buffer_per_cpu *cpu_buffer;
33503358
struct ring_buffer_iter *iter;
3351-
unsigned long flags;
33523359

33533360
if (!cpumask_test_cpu(cpu, buffer->cpumask))
33543361
return NULL;
@@ -3362,15 +3369,52 @@ ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
33623369
iter->cpu_buffer = cpu_buffer;
33633370

33643371
atomic_inc(&cpu_buffer->record_disabled);
3372+
3373+
return iter;
3374+
}
3375+
EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3376+
3377+
/**
3378+
* ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3379+
*
3380+
* All previously invoked ring_buffer_read_prepare calls to prepare
3381+
* iterators will be synchronized. Afterwards, read_buffer_read_start
3382+
* calls on those iterators are allowed.
3383+
*/
3384+
void
3385+
ring_buffer_read_prepare_sync(void)
3386+
{
33653387
synchronize_sched();
3388+
}
3389+
EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3390+
3391+
/**
3392+
* ring_buffer_read_start - start a non consuming read of the buffer
3393+
* @iter: The iterator returned by ring_buffer_read_prepare
3394+
*
3395+
* This finalizes the startup of an iteration through the buffer.
3396+
* The iterator comes from a call to ring_buffer_read_prepare and
3397+
* an intervening ring_buffer_read_prepare_sync must have been
3398+
* performed.
3399+
*
3400+
* Must be paired with ring_buffer_finish.
3401+
*/
3402+
void
3403+
ring_buffer_read_start(struct ring_buffer_iter *iter)
3404+
{
3405+
struct ring_buffer_per_cpu *cpu_buffer;
3406+
unsigned long flags;
3407+
3408+
if (!iter)
3409+
return;
3410+
3411+
cpu_buffer = iter->cpu_buffer;
33663412

33673413
spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
33683414
arch_spin_lock(&cpu_buffer->lock);
33693415
rb_iter_reset(iter);
33703416
arch_spin_unlock(&cpu_buffer->lock);
33713417
spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3372-
3373-
return iter;
33743418
}
33753419
EXPORT_SYMBOL_GPL(ring_buffer_read_start);
33763420

‎kernel/trace/trace.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,15 +2201,20 @@ __tracing_open(struct inode *inode, struct file *file)
22012201

22022202
if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
22032203
for_each_tracing_cpu(cpu) {
2204-
22052204
iter->buffer_iter[cpu] =
2206-
ring_buffer_read_start(iter->tr->buffer, cpu);
2205+
ring_buffer_read_prepare(iter->tr->buffer, cpu);
2206+
}
2207+
ring_buffer_read_prepare_sync();
2208+
for_each_tracing_cpu(cpu) {
2209+
ring_buffer_read_start(iter->buffer_iter[cpu]);
22072210
tracing_iter_reset(iter, cpu);
22082211
}
22092212
} else {
22102213
cpu = iter->cpu_file;
22112214
iter->buffer_iter[cpu] =
2212-
ring_buffer_read_start(iter->tr->buffer, cpu);
2215+
ring_buffer_read_prepare(iter->tr->buffer, cpu);
2216+
ring_buffer_read_prepare_sync();
2217+
ring_buffer_read_start(iter->buffer_iter[cpu]);
22132218
tracing_iter_reset(iter, cpu);
22142219
}
22152220

0 commit comments

Comments
 (0)