[PATCH 55/73] libperf: Add perf_evlist_mmap_ops::new callback

From: Jiri Olsa
Date: Fri Sep 13 2019 - 09:26:18 EST


Adding perf_evlist_mmap_ops::new callback to be called
in mmap_per_evsel to get/allocate the new 'struct perf_mmap'
object.

Adding libperf's perf_evlist__mmap_cb_new function as
libperf's new callback.

Link: http://lkml.kernel.org/n/tip-smr1w2e6j37ncbmsd1eet228@xxxxxxxxxxxxxx
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/perf/lib/evlist.c | 32 +++++++++++++++++-------
tools/perf/lib/include/internal/evlist.h | 3 +++
2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/tools/perf/lib/evlist.c b/tools/perf/lib/evlist.c
index 67b5f14f738f..8caced7d489a 100644
--- a/tools/perf/lib/evlist.c
+++ b/tools/perf/lib/evlist.c
@@ -327,9 +327,23 @@ static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,

#define FD(e, x, y) (*(int *) xyarray__entry(e->fd, x, y))

+static struct perf_mmap*
+perf_evlist__mmap_cb_new(struct perf_evlist *evlist, bool overwrite, int idx)
+{
+ struct perf_mmap **maps, *map;
+
+ map = perf_mmap__new(overwrite, NULL);
+ if (map == NULL)
+ return NULL;
+
+ maps = overwrite ? evlist->mmap_ovw : evlist->mmap;
+ maps[idx] = map;
+ return map;
+}
+
static int
-mmap_per_evsel(struct perf_evlist *evlist, int idx,
- struct perf_mmap_param *mp, int cpu_idx,
+mmap_per_evsel(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops,
+ int idx, struct perf_mmap_param *mp, int cpu_idx,
int thread, int *_output, int *_output_overwrite)
{
int evlist_cpu = perf_cpu_map__cpu(evlist->cpus, cpu_idx);
@@ -348,18 +362,16 @@ mmap_per_evsel(struct perf_evlist *evlist, int idx,
if (cpu == -1)
continue;

- map = perf_mmap__new(overwrite, NULL);
+ map = ops->new(evlist, overwrite, idx);
if (map == NULL)
return -ENOMEM;

if (overwrite) {
mp->prot = PROT_READ;
output = _output_overwrite;
- evlist->mmap_ovw[idx] = map;
} else {
mp->prot = PROT_READ | PROT_WRITE;
output = _output;
- evlist->mmap[idx] = map;
}

fd = FD(evsel, cpu, thread);
@@ -410,7 +422,7 @@ mmap_per_thread(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops,
if (ops->idx)
ops->idx(evlist, mp, thread, false);

- if (mmap_per_evsel(evlist, thread, mp, 0, thread,
+ if (mmap_per_evsel(evlist, ops, thread, mp, 0, thread,
&output, &output_overwrite))
goto out_unmap;
}
@@ -438,7 +450,7 @@ mmap_per_cpu(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops,
ops->idx(evlist, mp, cpu, true);

for (thread = 0; thread < nr_threads; thread++) {
- if (mmap_per_evsel(evlist, cpu, mp, cpu,
+ if (mmap_per_evsel(evlist, ops, cpu, mp, cpu,
thread, &output, &output_overwrite))
goto out_unmap;
}
@@ -459,7 +471,7 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
const struct perf_cpu_map *cpus = evlist->cpus;
const struct perf_thread_map *threads = evlist->threads;

- if (!ops)
+ if (!ops || !ops->new)
return -EINVAL;

perf_evlist__for_each_entry(evlist, evsel) {
@@ -478,7 +490,9 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
int perf_evlist__mmap(struct perf_evlist *evlist, int pages)
{
struct perf_mmap_param mp;
- struct perf_evlist_mmap_ops ops = { 0 };
+ struct perf_evlist_mmap_ops ops = {
+ .new = perf_evlist__mmap_cb_new,
+ };

if (!evlist->mmap && perf_evlist__alloc_maps(evlist))
return -ENOMEM;
diff --git a/tools/perf/lib/include/internal/evlist.h b/tools/perf/lib/include/internal/evlist.h
index 64522b8237be..3cb836912ee6 100644
--- a/tools/perf/lib/include/internal/evlist.h
+++ b/tools/perf/lib/include/internal/evlist.h
@@ -29,9 +29,12 @@ struct perf_evlist {

typedef void
(*perf_evlist_mmap__cb_idx_t)(struct perf_evlist*, struct perf_mmap_param*, int, bool);
+typedef struct perf_mmap*
+(*perf_evlist_mmap__cb_new_t)(struct perf_evlist*, bool, int);

struct perf_evlist_mmap_ops {
perf_evlist_mmap__cb_idx_t idx;
+ perf_evlist_mmap__cb_new_t new;
};

int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);
--
2.21.0