perf tests: Add test to check backward ring buffer
cover perf tests: Add test to check backward ring buffer
This 4 patches add a new test case, test reading from backward ring bfufer implemented by [1].
[1]: http://lkml.kernel.org/g/[email protected]
Wang Nan (4):
perf tools: Enforce ring buffer reading
perf tools: Extract __perf_evlist__mmap_read() perf tools: Support reading from backward ring buffer perf tests: Add test to check backward ring buffer
tools/perf/tests/Build | 1 + tools/perf/tests/backward-ring-buffer.c | 151 ++++++++++++++++++++++++++++++++ tools/perf/tests/builtin-test.c | 4 + tools/perf/tests/tests.h | 1 + tools/perf/util/evlist.c | 87 ++++++++++++++---- tools/perf/util/evlist.h | 4 + 6 files changed, 233 insertions(+), 15 deletions(-) create mode 100644 tools/perf/tests/backward-ring-buffer.c
Signature · 1 line
1.8.3.4
PATCH 1/4 perf tools: Enforce ring buffer reading
Don't even consider reading data after 'head' pointer.
Following commits will feed perf_evlist__mmap_read() with some 'head' pointers not maintained by kernel. If 'head' pointer breaks an event, we should avoid reading from the broken event. This can happen in backward ring buffer.
For example:
old head
| |
V V
+---+------+----------+----+-----+--+
|..E|D....D|C........C|B..B|A....|E.|
+---+------+----------+----+-----+--+'old' pointer points to the beginning of 'A' and trying read from it, but 'A' has been overwritten. In this case, don't try to read from 'A', simply return NULL.
--- tools/perf/util/evlist.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/evlist.c +10 -2
@@ -684,6 +684,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)struct perf_mmap *md = &evlist->mmap[idx];u64 head;u64 old = md->prev; +int diff;unsigned char *data = md->base + page_size;union perf_event *event =NULL ;
@@ -694,6 +695,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)return NULL ; head =perf_mmap__read_head (md); + diff = head - old;if (evlist->overwrite) {/* * If we're further behind than half the buffer, there's a chance
@@ -703,7 +705,6 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)* * In either case, truncate and restart at head. */ -int diff = head - old;if (diff > md->mask /2 || diff <0 ) {fprintf (stderr,"WARNING: failed to keep up with mmap data.\n" );
@@ -711,15 +712,21 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)* head points to a known good entry, start there. */ old = head; + diff =0 ; } } -if (old != head) { +if (diff >= (int )sizeof (event->header)) {size_t size; event = (union perf_event *)&data[old & md->mask]; size = event->header.size; +if (size <sizeof (event->header) || diff < (int )size) { + event =NULL ; + goto broken_event; + } +/* * Event straddles the mmap boundary -- header should always * be inside due to u64 alignment of output.
@@ -743,6 +750,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) old += size; } +broken_event: md->prev = old;return event;
Signature · 1 line
1.8.3.4
PATCH 2/4 perf tools: Extract __perf_evlist__mmap_read()
In reply to Wang NanExtract event reader to __perf_evlist__mmap_read(). Future commit will feed it with manually computed 'head' and 'old' pointers.
--- tools/perf/util/evlist.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-)
tools/perf/util/evlist.c +25 -15
@@ -679,24 +679,15 @@ static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,return NULL ; } -union perf_event *perf_evlist__mmap_read (struct perf_evlist *evlist,int idx) +static union perf_event * +__perf_evlist__mmap_read (struct perf_mmap *md,bool overwrite,u64 head, +u64 old,u64 *prev) { -struct perf_mmap *md = &evlist->mmap[idx]; -u64 head; -u64 old = md->prev; -int diff;unsigned char *data = md->base + page_size;union perf_event *event =NULL ; +int diff = head - old; -/* -* Check if event was unmapped due to a POLLHUP/POLLERR. -*/ -if (!atomic_read (&md->refcnt)) -return NULL ; - - head =perf_mmap__read_head (md); - diff = head - old; -if (evlist->overwrite) { +if (overwrite) {/* * If we're further behind than half the buffer, there's a chance * the writer will bite our tail and mess up the samples under us.
@@ -751,11 +742,30 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) } broken_event: - md->prev = old; +if (prev) + *prev = old;return event; } +union perf_event *perf_evlist__mmap_read (struct perf_evlist *evlist,int idx) +{ +struct perf_mmap *md = &evlist->mmap[idx]; +u64 head; +u64 old = md->prev; + +/* +* Check if event was unmapped due to a POLLHUP/POLLERR. +*/ +if (!atomic_read (&md->refcnt)) +return NULL ; + + head =perf_mmap__read_head (md); + +return __perf_evlist__mmap_read (md, evlist->overwrite, head, + old, &md->prev); +} +static bool perf_mmap__empty (struct perf_mmap *md) {return perf_mmap__read_head (md) == md->prev && !md->auxtrace_mmap.base;
Signature · 1 line
1.8.3.4
PATCH 3/4 perf tools: Support reading from backward ring buffer
In reply to Wang Nanperf_evlist__mmap_read_backward() is introduced for reading backward ring buffer. Different from reading forward, before reading caller needs to call perf_evlist__mmap_read_catchup() first.
Backward ring buffer should be read from 'head' pointer, not '0'. perf_evlist__mmap_read_catchup() saves 'head' to 'md->prev', then use it to remember the start position after each reading.
--- tools/perf/util/evlist.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- tools/perf/util/evlist.h | 4 ++++ 2 files changed, 46 insertions(+), 3 deletions(-)
tools/perf/util/evlist.c +42 -3
@@ -680,14 +680,14 @@ static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, }static union perf_event * -__perf_evlist__mmap_read (struct perf_mmap *md,bool overwrite,u64 head, -u64 old,u64 *prev) +__perf_evlist__mmap_read (struct perf_mmap *md,bool check_messup, +u64 head,u64 old,u64 *prev) {unsigned char *data = md->base + page_size;union perf_event *event =NULL ;int diff = head - old; -if (overwrite) { +if (check_messup) {/* * If we're further behind than half the buffer, there's a chance * the writer will bite our tail and mess up the samples under us.
@@ -766,6 +766,45 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) old, &md->prev); } +union perf_event * +perf_evlist__mmap_read_backward (struct perf_evlist *evlist,int idx) +{ +struct perf_mmap *md = &evlist->mmap[idx]; +u64 head, end; +u64 start = md->prev; + +/* +* Check if event was unmapped due to a POLLHUP/POLLERR. +*/ +if (!atomic_read (&md->refcnt)) +return NULL ; + +/* NOTE: head is negative in this case */ + head =perf_mmap__read_head (md); + +if (!head) +return NULL ; + + end = head + md->mask +1 ; + +if ((end - head) > -head) + end =0 ; + +return __perf_evlist__mmap_read (md, false, end, start, &md->prev); +} + +void perf_evlist__mmap_read_catchup (struct perf_evlist *evlist,int idx) +{ +struct perf_mmap *md = &evlist->mmap[idx]; +u64 head; + +if (!atomic_read (&md->refcnt)) +return ; + + head =perf_mmap__read_head (md); + md->prev = head; +} +static bool perf_mmap__empty (struct perf_mmap *md) {return perf_mmap__read_head (md) == md->prev && !md->auxtrace_mmap.base;
tools/perf/util/evlist.h +4 -0
@@ -118,6 +118,10 @@ struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id);union perf_event *perf_evlist__mmap_read (struct perf_evlist *evlist,int idx); +union perf_event *perf_evlist__mmap_read_backward (struct perf_evlist *evlist, +int idx); +void perf_evlist__mmap_read_catchup (struct perf_evlist *evlist,int idx); +void perf_evlist__mmap_consume (struct perf_evlist *evlist,int idx);int perf_evlist__open (struct perf_evlist *evlist);
Signature · 1 line
1.8.3.4
PATCH 4/4 perf tests: Add test to check backward ring buffer
In reply to Wang NanThis test checks reading from backward ring buffer.
Test result:
# ~/perf test 'ring buffer'
45: Test reading from ring buffer : Ok
Test case is a while loop which calls prctl(PR_SET_NAME) multiple times. Each prctl should issue 2 events: one PERF_RECORD_SAMPLE, one PERF_RECORD_COMM.
The first round creates a relative large ring buffer (256 pages). It can afford all events. Read from it and check the count of each type of events.
The second round creates a small ring buffer (1 page) and make it overwritable. Check the correctness of the buffer.
--- tools/perf/tests/Build | 1 + tools/perf/tests/backward-ring-buffer.c | 151 ++++++++++++++++++++++++++++++++ tools/perf/tests/builtin-test.c | 4 + tools/perf/tests/tests.h | 1 + 4 files changed, 157 insertions(+) create mode 100644 tools/perf/tests/backward-ring-buffer.c
tools/perf/tests/Build +1 -0
@@ -38,6 +38,7 @@ perf-y += cpumap.o perf-y += stat.o perf-y += event_update.o perf-y += event-times.o +perf-y += backward-ring-buffer.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)
tools/perf/tests/backward-ring-buffer.c new file +151 -0
151 lines not on this page · raw message
tools/perf/tests/builtin-test.c +4 -0
@@ -208,6 +208,10 @@ static struct test generic_tests[] = { .func = test__event_times, }, { + .desc = "Test reading from ring buffer", + .func = test__ring_buffer, + }, + { .func = NULL, }, };
tools/perf/tests/tests.h +1 -0
@@ -86,6 +86,7 @@ int test__synthesize_stat(int subtest);int test__synthesize_stat_round (int subtest);int test__event_update (int subtest);int test__event_times (int subtest); +int test__ring_buffer (int subtest);#if defined(__arm__) || defined(__aarch64__)#ifdef HAVE_DWARF_UNWIND_SUPPORT
Signature · 1 line
1.8.3.4