[PATCH V2 2/8] perf mmap: Store mmap scope in struct perf_mmap

From: kan . liang
Date: Tue Mar 06 2018 - 10:38:35 EST


From: Kan Liang <kan.liang@xxxxxxxxxxxxxxx>

There are too many boilerplates for the perf_mmap__read*() interfaces.

The 'start' and 'end' should be stored in struct perf_mmap at
initialization. They will be used later.

The old 'startp' and 'endp' are used by perf_mmap__read_event() now.
They cannot be removed. So the old 'startp/endp' and new
'md->start/md->end' will exist simultaneously now.
The old one will be removed later.

Signed-off-by: Kan Liang <kan.liang@xxxxxxxxxxxxxxx>
---

Changes since V1:
- Using the 'overwrite' which set in previous patch
- Add the reason of keeping old starp/end in the change log

tools/perf/util/mmap.c | 12 ++++++++----
tools/perf/util/mmap.h | 2 ++
2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/mmap.c b/tools/perf/util/mmap.c
index 4f27c46..09acaf7 100644
--- a/tools/perf/util/mmap.c
+++ b/tools/perf/util/mmap.c
@@ -250,13 +250,15 @@ int perf_mmap__read_init(struct perf_mmap *md, bool overwrite,

*startp = overwrite ? head : old;
*endp = overwrite ? old : head;
+ md->start = md->overwrite ? head : old;
+ md->end = md->overwrite ? old : head;

- if (*startp == *endp)
+ if (md->start == md->end)
return -EAGAIN;

- size = *endp - *startp;
+ size = md->end - md->start;
if (size > (unsigned long)(md->mask) + 1) {
- if (!overwrite) {
+ if (!md->overwrite) {
WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");

md->prev = head;
@@ -268,8 +270,10 @@ int perf_mmap__read_init(struct perf_mmap *md, bool overwrite,
* Backward ring buffer is full. We still have a chance to read
* most of data from it.
*/
- if (overwrite_rb_find_range(data, md->mask, head, startp, endp))
+ if (overwrite_rb_find_range(data, md->mask, head, &md->start, &md->end))
return -EINVAL;
+ *startp = md->start;
+ *endp = md->end;
}

return 0;
diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h
index 7113797..9359e93 100644
--- a/tools/perf/util/mmap.h
+++ b/tools/perf/util/mmap.h
@@ -20,6 +20,8 @@ struct perf_mmap {
int fd;
refcount_t refcnt;
u64 prev;
+ u64 start;
+ u64 end;
bool overwrite;
struct auxtrace_mmap auxtrace_mmap;
char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8);
--
2.4.11