Adding perf_evlist__for_each_mmap function and exporting
it in perf/evlist.h header, so user can iterate through
'struct perf_mmap' objects.

Adding internal perf_mmap__link function to do the actual
linking.

Link: http://lkml.kernel.org/n/tip-5h62ytvpxgozhqix2y41c...@git.kernel.org
Signed-off-by: Jiri Olsa <jo...@kernel.org>
---
 tools/perf/lib/evlist.c                | 13 +++++++++++++
 tools/perf/lib/include/internal/mmap.h |  2 ++
 tools/perf/lib/include/perf/evlist.h   |  7 +++++++
 tools/perf/lib/libperf.map             |  1 +
 tools/perf/lib/mmap.c                  |  6 ++++++
 tools/perf/util/evlist.c               |  1 +
 6 files changed, 30 insertions(+)

diff --git a/tools/perf/lib/evlist.c b/tools/perf/lib/evlist.c
index ee1d364460e6..ca98aac80ce1 100644
--- a/tools/perf/lib/evlist.c
+++ b/tools/perf/lib/evlist.c
@@ -382,6 +382,7 @@ perf_evlist__mmap_cb_new(struct perf_evlist *evlist, bool 
overwrite, int idx)
 
        maps = overwrite ? evlist->mmap_ovw : evlist->mmap;
        maps[idx] = map;
+       perf_mmap__link(idx ? maps[idx - 1] : NULL, map);
        return map;
 }
 
@@ -584,3 +585,15 @@ void perf_evlist__munmap(struct perf_evlist *evlist)
        zfree(&evlist->mmap);
        zfree(&evlist->mmap_ovw);
 }
+
+struct perf_mmap*
+perf_evlist__next_mmap(struct perf_evlist *evlist, struct perf_mmap *map)
+{
+       if (map)
+               return map->next;
+
+       if (!evlist->mmap)
+               return NULL;
+
+       return evlist->mmap[0];
+}
diff --git a/tools/perf/lib/include/internal/mmap.h 
b/tools/perf/lib/include/internal/mmap.h
index 98e31f8ab461..f0fdf9b5a1d5 100644
--- a/tools/perf/lib/include/internal/mmap.h
+++ b/tools/perf/lib/include/internal/mmap.h
@@ -33,6 +33,7 @@ struct perf_mmap {
        u64                      flush;
        libperf_unmap_cb_t       unmap_cb;
        char                     event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8);
+       struct perf_mmap        *next;
 };
 
 struct perf_mmap_param {
@@ -54,4 +55,5 @@ void perf_mmap__put(struct perf_mmap *map);
 
 u64 perf_mmap__read_head(struct perf_mmap *map);
 
+void perf_mmap__link(struct perf_mmap *prev, struct perf_mmap *map);
 #endif /* __LIBPERF_INTERNAL_MMAP_H */
diff --git a/tools/perf/lib/include/perf/evlist.h 
b/tools/perf/lib/include/perf/evlist.h
index 16f526e74d13..9c3f341dc261 100644
--- a/tools/perf/lib/include/perf/evlist.h
+++ b/tools/perf/lib/include/perf/evlist.h
@@ -38,4 +38,11 @@ LIBPERF_API int perf_evlist__filter_pollfd(struct 
perf_evlist *evlist,
 LIBPERF_API int perf_evlist__mmap(struct perf_evlist *evlist, int pages);
 LIBPERF_API void perf_evlist__munmap(struct perf_evlist *evlist);
 
+LIBPERF_API struct perf_mmap *perf_evlist__next_mmap(struct perf_evlist 
*evlist,
+                                                    struct perf_mmap *map);
+#define perf_evlist__for_each_mmap(evlist, pos)                        \
+       for ((pos) = perf_evlist__next_mmap((evlist), NULL);    \
+            (pos) != NULL;                                     \
+            (pos) = perf_evlist__next_mmap((evlist), (pos)))
+
 #endif /* __LIBPERF_EVLIST_H */
diff --git a/tools/perf/lib/libperf.map b/tools/perf/lib/libperf.map
index 90aee2a635b2..265188126b08 100644
--- a/tools/perf/lib/libperf.map
+++ b/tools/perf/lib/libperf.map
@@ -42,6 +42,7 @@ LIBPERF_0.0.1 {
                perf_evlist__mmap;
                perf_evlist__munmap;
                perf_evlist__filter_pollfd;
+               perf_evlist__next_mmap;
                perf_mmap__consume;
                perf_mmap__read_init;
                perf_mmap__read_done;
diff --git a/tools/perf/lib/mmap.c b/tools/perf/lib/mmap.c
index f8816c7bee87..453c8a57a895 100644
--- a/tools/perf/lib/mmap.c
+++ b/tools/perf/lib/mmap.c
@@ -281,3 +281,9 @@ union perf_event *perf_mmap__read_event(struct perf_mmap 
*map)
 
        return event;
 }
+
+void perf_mmap__link(struct perf_mmap *prev, struct perf_mmap *map)
+{
+       if (prev)
+               prev->next = map;
+}
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 9edb0855f711..1e703eb11500 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -591,6 +591,7 @@ static struct mmap *evlist__alloc_mmap(struct evlist 
*evlist,
                 * thus does perf_mmap__get() on it.
                 */
                perf_mmap__init(&map[i].core, overwrite, perf_mmap__unmap_cb);
+               perf_mmap__link(i ? &map[i - 1].core : NULL, &map[i].core);
        }
 
        return map;
-- 
2.21.0

Reply via email to