Skip to contentSkip to conversation

perf tools: Backward ring buffer support

PATCH Superseded Wang Nan 3 participants 10 messages Apr 27, 2016 – May 9, 2016 perf events

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

v2 cover perf tools: Backward ring buffer support

Commit 9ecda41acb97 ("perf/core: Add ::write_backward attribute to perf event") introduces backward ring buffer. This 4 patches add basic support for reading from it, and add a new test case for it.

v1 -> v2:
Patch 1/5 in v1 has been collected by perf/core, so remove it; Change function names in patch 2/5 in v1 (1/4 in v2):

      __perf_evlist__mmap_read -> perf_mmap__read

Wang Nan (4):
perf tools: Extract perf_mmap__read()
perf tools: Rename variable in perf_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                |  95 +++++++++++++++-----
 tools/perf/util/evlist.h                |   4 +
 6 files changed, 233 insertions(+), 23 deletions(-)
 create mode 100644 tools/perf/tests/backward-ring-buffer.c
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Zefan Li <[email protected]>
Signature · 1 line
1.8.3.4
author
#1 lore raw

PATCH v2 1/4 perf tools: Extract perf_mmap__read()

Extract event reader from perf_evlist__mmap_read() to perf__mmap_read(). Future commit will feed it with manually computed 'head' and 'old' pointers.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/evlist.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)
tools/perf/util/evlist.c +24 -15
index 85271e5..96c7191 100644
@@ -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_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,29 @@ 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_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
#2 lore raw

[tip:perf/core] perf evlist: Extract perf_mmap__read()

perf evlist: Extract perf_mmap__read()
Commit-ID:  0f4ccd11813f59d766039dfdd13aa98245a67294
Gitweb:     http://git.kernel.org/tip/0f4ccd11813f59d766039dfdd13aa98245a67294
Author:     Wang Nan <[email protected]>
AuthorDate: Wed, 27 Apr 2016 02:19:20 +0000
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 5 May 2016 21:04:03 -0300

perf evlist: Extract perf_mmap__read()

Extract event reader from perf_evlist__mmap_read() to perf__mmap_read(). Future commit will feed it with manually computed 'head' and 'old' pointers.

---
 tools/perf/util/evlist.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)
tools/perf/util/evlist.c +24 -15
index 85271e5..96c7191 100644
@@ -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_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,29 @@ 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_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;
author
#3 lore raw

PATCH v2 2/4 perf tools: Rename variable in perf_mmap__read()

In reply to Wang Nan

In perf_mmap__read(), give better names to pointers. Origianl name 'old' and 'head' directly related to pointers in ring buffer control page. For backward ring buffer, the meaning of 'head' point is not 'the first byte of free space', but 'the first byte of the last record'. To reduce confusion, rename 'old' to 'start', 'head' to 'end'. 'start' -> 'end' is the records should be read from.

Change parameter order.

Change 'overwrite' to 'check_messup'. When reading from 'head', no need to check messup for for backward ring buffer.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/evlist.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
tools/perf/util/evlist.c +15 -14
index 96c7191..17cd014 100644
@@ -679,30 +679,31 @@ static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
 	return NULL;
 }
 
+/* When check_messup is true, 'end' must points to a good entry */
 static union perf_event *
-perf_mmap__read(struct perf_mmap *md, bool overwrite, u64 head,
-		u64 old, u64 *prev)
+perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start,
+		u64 end, u64 *prev)
 {
 	unsigned char *data = md->base + page_size;
 	union perf_event *event = NULL;
-	int diff = head - old;
+	int diff = end - start;
 
-	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.
 		 *
-		 * If we somehow ended up ahead of the head, we got messed up.
+		 * If we somehow ended up ahead of the 'end', we got messed up.
 		 *
-		 * In either case, truncate and restart at head.
+		 * In either case, truncate and restart at 'end'.
 		 */
 		if (diff > md->mask / 2 || diff < 0) {
 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
 
 			/*
-			 * head points to a known good entry, start there.
+			 * 'end' points to a known good entry, start there.
 			 */
-			old = head;
+			start = end;
 			diff = 0;
 		}
 	}
@@ -710,7 +711,7 @@ perf_mmap__read(struct perf_mmap *md, bool overwrite, u64 head,
 	if (diff >= (int)sizeof(event->header)) {
 		size_t size;
 
-		event = (union perf_event *)&data[old & md->mask];
+		event = (union perf_event *)&data[start & md->mask];
 		size = event->header.size;
 
 		if (size < sizeof(event->header) || diff < (int)size) {
@@ -722,8 +723,8 @@ perf_mmap__read(struct perf_mmap *md, bool overwrite, u64 head,
 		 * Event straddles the mmap boundary -- header should always
 		 * be inside due to u64 alignment of output.
 		 */
-		if ((old & md->mask) + size != ((old + size) & md->mask)) {
-			unsigned int offset = old;
+		if ((start & md->mask) + size != ((start + size) & md->mask)) {
+			unsigned int offset = start;
 			unsigned int len = min(sizeof(*event), size), cpy;
 			void *dst = md->event_copy;
 
@@ -738,12 +739,12 @@ perf_mmap__read(struct perf_mmap *md, bool overwrite, u64 head,
 			event = (union perf_event *) md->event_copy;
 		}
 
-		old += size;
+		start += size;
 	}
 
 broken_event:
 	if (prev)
-		*prev = old;
+		*prev = start;
 
 	return event;
 }
@@ -762,7 +763,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
 
 	head = perf_mmap__read_head(md);
 
-	return perf_mmap__read(md, evlist->overwrite, head, old, &md->prev);
+	return perf_mmap__read(md, evlist->overwrite, old, head, &md->prev);
 }
 
 static bool perf_mmap__empty(struct perf_mmap *md)
Signature · 1 line
1.8.3.4
#4 lore raw

[tip:perf/core] perf evlist: Rename variable in perf_mmap__read()

perf evlist: Rename variable in perf_mmap__read()
Commit-ID:  b6b85dad30ad7e7394990e2317a780577974a4e6
Gitweb:     http://git.kernel.org/tip/b6b85dad30ad7e7394990e2317a780577974a4e6
Author:     Wang Nan <[email protected]>
AuthorDate: Wed, 27 Apr 2016 02:19:21 +0000
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 5 May 2016 21:04:04 -0300

perf evlist: Rename variable in perf_mmap__read()

In perf_mmap__read(), give better names to pointers. Original name 'old' and 'head' directly related to pointers in ring buffer control page. For backward ring buffer, the meaning of 'head' point is not 'the first byte of free space', but 'the first byte of the last record'. To reduce confusion, rename 'old' to 'start', 'head' to 'end'. 'start' -> 'end' is the direction the records should be read from.

Change parameter order.

Change 'overwrite' to 'check_messup'. When reading from 'head', no need to check messup for for backward ring buffer.

---
 tools/perf/util/evlist.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
tools/perf/util/evlist.c +15 -14
index 96c7191..17cd014 100644
@@ -679,30 +679,31 @@ static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
 	return NULL;
 }
 
+/* When check_messup is true, 'end' must points to a good entry */
 static union perf_event *
-perf_mmap__read(struct perf_mmap *md, bool overwrite, u64 head,
-		u64 old, u64 *prev)
+perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start,
+		u64 end, u64 *prev)
 {
 	unsigned char *data = md->base + page_size;
 	union perf_event *event = NULL;
-	int diff = head - old;
+	int diff = end - start;
 
-	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.
 		 *
-		 * If we somehow ended up ahead of the head, we got messed up.
+		 * If we somehow ended up ahead of the 'end', we got messed up.
 		 *
-		 * In either case, truncate and restart at head.
+		 * In either case, truncate and restart at 'end'.
 		 */
 		if (diff > md->mask / 2 || diff < 0) {
 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
 
 			/*
-			 * head points to a known good entry, start there.
+			 * 'end' points to a known good entry, start there.
 			 */
-			old = head;
+			start = end;
 			diff = 0;
 		}
 	}
@@ -710,7 +711,7 @@ perf_mmap__read(struct perf_mmap *md, bool overwrite, u64 head,
 	if (diff >= (int)sizeof(event->header)) {
 		size_t size;
 
-		event = (union perf_event *)&data[old & md->mask];
+		event = (union perf_event *)&data[start & md->mask];
 		size = event->header.size;
 
 		if (size < sizeof(event->header) || diff < (int)size) {
@@ -722,8 +723,8 @@ perf_mmap__read(struct perf_mmap *md, bool overwrite, u64 head,
 		 * Event straddles the mmap boundary -- header should always
 		 * be inside due to u64 alignment of output.
 		 */
-		if ((old & md->mask) + size != ((old + size) & md->mask)) {
-			unsigned int offset = old;
+		if ((start & md->mask) + size != ((start + size) & md->mask)) {
+			unsigned int offset = start;
 			unsigned int len = min(sizeof(*event), size), cpy;
 			void *dst = md->event_copy;
 
@@ -738,12 +739,12 @@ perf_mmap__read(struct perf_mmap *md, bool overwrite, u64 head,
 			event = (union perf_event *) md->event_copy;
 		}
 
-		old += size;
+		start += size;
 	}
 
 broken_event:
 	if (prev)
-		*prev = old;
+		*prev = start;
 
 	return event;
 }
@@ -762,7 +763,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
 
 	head = perf_mmap__read_head(md);
 
-	return perf_mmap__read(md, evlist->overwrite, head, old, &md->prev);
+	return perf_mmap__read(md, evlist->overwrite, old, head, &md->prev);
 }
 
 static bool perf_mmap__empty(struct perf_mmap *md)
author
#5 lore raw

PATCH v2 3/4 perf tools: Support reading from backward ring buffer

In reply to Wang Nan

perf_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 make it remember the start position after each reading.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/evlist.c | 39 +++++++++++++++++++++++++++++++++++++++
 tools/perf/util/evlist.h |  4 ++++
 2 files changed, 43 insertions(+)
tools/perf/util/evlist.c +39 -0
index 17cd014..2e0b7b0 100644
@@ -766,6 +766,45 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
 	return perf_mmap__read(md, evlist->overwrite, old, head, &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_mmap__read(md, false, start, end, &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
index 208897a..85d1b59 100644
@@ -129,6 +129,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
#6 lore raw

Re: [PATCH v2 3/4] perf tools: Support reading from backward ring buffer

Em Wed, Apr 27, 2016 at 02:19:22AM +0000, Wang Nan escreveu:

Quoted 30 lines

perf_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 make it remember the start position after each reading.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/evlist.c | 39 +++++++++++++++++++++++++++++++++++++++
 tools/perf/util/evlist.h |  4 ++++
 2 files changed, 43 insertions(+)
tools/perf/util/evlist.c +4 -0
index 17cd014..2e0b7b0 100644
@@ -766,6 +766,45 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
 	return perf_mmap__read(md, evlist->overwrite, old, head, &md->prev);
 }
 
+union perf_event *
+perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx)
+{
+	struct perf_mmap *md = &evlist->mmap[idx];

tools/perf/util/evlist.c union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) · line 782

Quoted 10 lines
tools/perf/util/evlist.c +10 -0
+	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 */

What is this comment for, can you ellaborate? Are you double sure this arithmetic with u64, negative values, that -head below are all ok?

I've applied the first two patches in this series.

I also need to check why we now need that catchup thing :-\

Quoted 45 lines
tools/perf/util/evlist.c +25 -0
+	head = perf_mmap__read_head(md);
+
+	if (!head)
+		return NULL;
+
+	end = head + md->mask + 1;
+
+	if ((end - head) > -head)
+		end = 0;
+
+	return perf_mmap__read(md, false, start, end, &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
index 208897a..85d1b59 100644
@@ -129,6 +129,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
#7 lore raw

Re: [PATCH v2 3/4] perf tools: Support reading from backward ring buffer

Quoted 27 lines · Arnaldo Carvalho de Melo

Em Wed, Apr 27, 2016 at 02:19:22AM +0000, Wang Nan escreveu:

Quoted 26 lines

perf_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 make it remember the start position after each reading.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Zefan Li <[email protected]>
---
  tools/perf/util/evlist.c | 39 +++++++++++++++++++++++++++++++++++++++
  tools/perf/util/evlist.h |  4 ++++
  2 files changed, 43 insertions(+)
tools/perf/util/evlist.c +0 -0
index 17cd014..2e0b7b0 100644
@@ -766,6 +766,45 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
  	return perf_mmap__read(md, evlist->overwrite, old, head, &md->prev);
  }
 
Quoted 16 lines
Quoted 4 lines
tools/perf/util/evlist.c +4 -0
+union perf_event *
+perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx)
+{
+	struct perf_mmap *md = &evlist->mmap[idx];

tools/perf/util/evlist.c union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) · line 782

Quoted 10 lines
tools/perf/util/evlist.c +10 -0
+	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 */

What is this comment for, can you ellaborate? Are you double sure this arithmetic with u64, negative values, that -head below are all ok?

Yes. In backward ring buffer, kernel write data from '0'. Each time it write a record, it minus sizeof(record) from 'head' pointer. So '-head' means number of bytes already written.

Quoted 3 lines

I've applied the first two patches in this series.

I also need to check why we now need that catchup thing :-\

I think catchup is necessary. I tried to get rid of it by setting md->prev to -1 and initialize it to 0 when reading forwardly and to head when reading backwardly, but it require too much code adjustments.

Quoted 10 lines

tools/perf/util/evlist.c union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) · line 792

Quoted 10 lines
tools/perf/util/evlist.c +10 -0
+	head = perf_mmap__read_head(md);
+
+	if (!head)
+		return NULL;
+
+	end = head + md->mask + 1;
+
+	if ((end - head) > -head)
+		end = 0;
+

This '-head' is used to detect wrapping. Never use positive pointer.

Quoted 35 lines
Quoted 35 lines
tools/perf/util/evlist.c +15 -0
+	return perf_mmap__read(md, false, start, end, &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
index 208897a..85d1b59 100644
@@ -129,6 +129,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
#8 lore raw

Re: [PATCH v2 3/4] perf tools: Support reading from backward ring buffer

Quoted 52 lines · Wangnan (F)
Quoted 36 lines · Arnaldo Carvalho de Melo

Em Wed, Apr 27, 2016 at 02:19:22AM +0000, Wang Nan escreveu:

Quoted 35 lines

perf_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 make it remember the start position after each reading.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Zefan Li <[email protected]>
---
  tools/perf/util/evlist.c | 39 +++++++++++++++++++++++++++++++++++++++
  tools/perf/util/evlist.h |  4 ++++
  2 files changed, 43 insertions(+)
tools/perf/util/evlist.c +7 -0
index 17cd014..2e0b7b0 100644
@@ -766,6 +766,45 @@ union perf_event *perf_evlist__mmap_read(struct 
 perf_evlist *evlist, int idx)
      return perf_mmap__read(md, evlist->overwrite, old, head, 
 &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;
+
+    /*
Quoted 8 lines

tools/perf/util/evlist.c union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) · line 782

Quoted 6 lines
tools/perf/util/evlist.c +6 -0
+     * Check if event was unmapped due to a POLLHUP/POLLERR.
+     */
+    if (!atomic_read(&md->refcnt))
+        return NULL;
+
+    /* NOTE: head is negative in this case */

What is this comment for, can you ellaborate? Are you double sure this arithmetic with u64, negative values, that -head below are all ok?

Yes. In backward ring buffer, kernel write data from '0'. Each time it write a record, it minus sizeof(record) from 'head' pointer. So '-head' means number of bytes already written.

Quoted 9 lines
Quoted 3 lines

I've applied the first two patches in this series.

I also need to check why we now need that catchup thing :-\

I think catchup is necessary. I tried to get rid of it by setting md->prev to -1 and initialize it to 0 when reading forwardly and to head when reading backwardly, but it require too much code adjustments.

In addtion, reader of backward ring buffer tends to fetch the most recent record. Since each reading gets earlier record, a catchup function is always required.

Thank you.

Quoted 52 lines
Quoted 10 lines

tools/perf/util/evlist.c union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) · line 792

Quoted 10 lines
tools/perf/util/evlist.c +10 -0
+    head = perf_mmap__read_head(md);
+
+    if (!head)
+        return NULL;
+
+    end = head + md->mask + 1;
+
+    if ((end - head) > -head)
+        end = 0;
+

This '-head' is used to detect wrapping. Never use positive pointer.

Quoted 38 lines
Quoted 38 lines
tools/perf/util/evlist.c +15 -0
+    return perf_mmap__read(md, false, start, end, &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 +3 -0
index 208897a..85d1b59 100644
@@ -129,6 +129,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
author
#9 lore raw

PATCH v2 4/4 perf tests: Add test to check backward ring buffer

In reply to Wang Nan

This test checks reading from backward ring buffer.

Test result:

# ~/perf test 'ring buffer'
45: Test backward 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 makes it overwritable. Check the correctness of the buffer.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Zefan Li <[email protected]>
---
 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
index 449fe97..66a2898 100644
@@ -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
index 93c4670..0e95c20 100644
@@ -208,6 +208,10 @@ static struct test generic_tests[] = {
 		.func = test__event_times,
 	},
 	{
+		.desc = "Test backward reading from ring buffer",
+		.func = test__backward_ring_buffer,
+	},
+	{
 		.func = NULL,
 	},
 };
tools/perf/tests/tests.h +1 -0
index 0fc9469..c57e72c 100644
@@ -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__backward_ring_buffer(int subtest);
 
 #if defined(__arm__) || defined(__aarch64__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
Signature · 1 line
1.8.3.4