Skip to content

Commit 0568c6e

Browse files
vdonnefortrostedt
authored andcommitted
ring-buffer: Check for empty ring-buffer with rb_num_of_entries()
Currently there are two ways of identifying an empty ring-buffer. One relying on the current status of the commit / reader page (rb_per_cpu_empty()) and the other on the write and read counters (rb_num_of_entries() used in rb_get_reader_page()). with rb_num_of_entries(). This intends to ease later introduction of ring-buffer writers which are out of the kernel control and with whom, the only information available is through the meta-page counters. Link: https://lore.kernel.org/[email protected] Signed-off-by: Vincent Donnefort <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 5bc55a3 commit 0568c6e

File tree

1 file changed

+14
-45
lines changed

1 file changed

+14
-45
lines changed

kernel/trace/ring_buffer.c

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4682,40 +4682,22 @@ int ring_buffer_write(struct trace_buffer *buffer,
46824682
}
46834683
EXPORT_SYMBOL_GPL(ring_buffer_write);
46844684

4685-
static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
4685+
/*
4686+
* The total entries in the ring buffer is the running counter
4687+
* of entries entered into the ring buffer, minus the sum of
4688+
* the entries read from the ring buffer and the number of
4689+
* entries that were overwritten.
4690+
*/
4691+
static inline unsigned long
4692+
rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
46864693
{
4687-
struct buffer_page *reader = cpu_buffer->reader_page;
4688-
struct buffer_page *head = rb_set_head_page(cpu_buffer);
4689-
struct buffer_page *commit = cpu_buffer->commit_page;
4690-
4691-
/* In case of error, head will be NULL */
4692-
if (unlikely(!head))
4693-
return true;
4694-
4695-
/* Reader should exhaust content in reader page */
4696-
if (reader->read != rb_page_size(reader))
4697-
return false;
4698-
4699-
/*
4700-
* If writers are committing on the reader page, knowing all
4701-
* committed content has been read, the ring buffer is empty.
4702-
*/
4703-
if (commit == reader)
4704-
return true;
4705-
4706-
/*
4707-
* If writers are committing on a page other than reader page
4708-
* and head page, there should always be content to read.
4709-
*/
4710-
if (commit != head)
4711-
return false;
4694+
return local_read(&cpu_buffer->entries) -
4695+
(local_read(&cpu_buffer->overrun) + cpu_buffer->read);
4696+
}
47124697

4713-
/*
4714-
* Writers are committing on the head page, we just need
4715-
* to care about there're committed data, and the reader will
4716-
* swap reader page with head page when it is to read data.
4717-
*/
4718-
return rb_page_commit(commit) == 0;
4698+
static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
4699+
{
4700+
return !rb_num_of_entries(cpu_buffer);
47194701
}
47204702

47214703
/**
@@ -4861,19 +4843,6 @@ void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
48614843
}
48624844
EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
48634845

4864-
/*
4865-
* The total entries in the ring buffer is the running counter
4866-
* of entries entered into the ring buffer, minus the sum of
4867-
* the entries read from the ring buffer and the number of
4868-
* entries that were overwritten.
4869-
*/
4870-
static inline unsigned long
4871-
rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
4872-
{
4873-
return local_read(&cpu_buffer->entries) -
4874-
(local_read(&cpu_buffer->overrun) + cpu_buffer->read);
4875-
}
4876-
48774846
/**
48784847
* ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
48794848
* @buffer: The ring buffer

0 commit comments

Comments
 (0)