[PATCH V2 4/8] perf tools: introduce perf_mmap__read_event
From: kan . liang
Date: Wed Dec 06 2017 - 18:34:31 EST
From: Kan Liang <kan.liang@xxxxxxxxx>
Currently, there is no generic function to read event from ring buffer,
which support both overwirte and non-overwrite mode.
Indroduce perf_mmap__read_event to do so.
The usage is as below.
perf_mmap__read_catchup()
while(event = perf_mmap__read_event()) {
//process the event
perf_mmap__consume()
}
perf_mmap__read_done()
Signed-off-by: Kan Liang <kan.liang@xxxxxxxxx>
---
tools/perf/util/mmap.c | 25 +++++++++++++++++++++++++
tools/perf/util/mmap.h | 3 +++
2 files changed, 28 insertions(+)
diff --git a/tools/perf/util/mmap.c b/tools/perf/util/mmap.c
index 61237eb..7c4c69a 100644
--- a/tools/perf/util/mmap.c
+++ b/tools/perf/util/mmap.c
@@ -146,6 +146,31 @@ void perf_mmap__read_done(struct perf_mmap *map)
map->prev = perf_mmap__read_head(map);
}
+
+/*
+ * Read event from ring buffer. Return one event for each call.
+ * Support both overwirte and non-overwrite mode.
+ * The start and end are only available for overwirte mode, which
+ * pause the ringbuffer.
+ *
+ * Usage:
+ * perf_mmap__read_catchup
+ * while(event = perf_mmap__read_event) {
+ * //process the event
+ * perf_mmap__consume
+ * }
+ * perf_mmap__read_done
+ */
+union perf_event *perf_mmap__read_event(struct perf_mmap *map,
+ bool overwrite,
+ u64 *start, u64 end)
+{
+ if (overwrite)
+ return perf_mmap__read_backward(map, start, end);
+ else
+ return perf_mmap__read_forward(map);
+}
+
static bool perf_mmap__empty(struct perf_mmap *map)
{
return perf_mmap__read_head(map) == map->prev && !map->auxtrace_mmap.base;
diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h
index a91222e..7082a7c 100644
--- a/tools/perf/util/mmap.h
+++ b/tools/perf/util/mmap.h
@@ -91,6 +91,9 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
union perf_event *perf_mmap__read_forward(struct perf_mmap *map);
union perf_event *perf_mmap__read_backward(struct perf_mmap *map,
u64 *start, u64 end);
+union perf_event *perf_mmap__read_event(struct perf_mmap *map,
+ bool overwrite,
+ u64 *start, u64 end);
int perf_mmap__push(struct perf_mmap *md, bool backward,
void *to, int push(void *to, void *buf, size_t size));
--
2.5.5