[PATCH 04/17] perf tools: Operate multiple channels

From: Wang Nan
Date: Fri May 13 2016 - 03:56:47 EST


Before this patch perf operates on only the first channel. Make perf
mmap and read from multiple channels.

Signed-off-by: Wang Nan <wangnan0@xxxxxxxxxx>
Signed-off-by: He Kuang <hekuang@xxxxxxxxxx>
Cc: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@xxxxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Zefan Li <lizefan@xxxxxxxxxx>
Cc: pi3orama@xxxxxxx
---
tools/perf/builtin-record.c | 3 ++-
tools/perf/util/evlist.c | 52 ++++++++++++++++++++++++++++++++-------------
tools/perf/util/evlist.h | 2 +-
3 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 3140378..21ef8a0 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -426,8 +426,9 @@ static int record__mmap_read_all(struct record *rec)
u64 bytes_written = rec->bytes_written;
int i;
int rc = 0;
+ int total_mmaps = perf_evlist__mmap_nr(rec->evlist);

- for (i = 0; i < rec->evlist->nr_mmaps; i++) {
+ for (i = 0; i < total_mmaps; i++) {
struct auxtrace_mmap *mm = &rec->evlist->mmap[i].auxtrace_mmap;

if (rec->evlist->mmap[i].base) {
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 47a8f1f..eefa33b 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -947,6 +947,16 @@ static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap);
}

+static void
+__perf_evlist__munmap_all(struct perf_evlist *evlist)
+{
+ int ch, i, idx = 0;
+
+ for (ch = 0; ch < perf_evlist__channel_nr(evlist); ch++)
+ for (i = 0; i < evlist->nr_mmaps; i++)
+ __perf_evlist__munmap(evlist, idx++);
+}
+
void perf_evlist__munmap(struct perf_evlist *evlist)
{
int i;
@@ -1054,26 +1064,38 @@ perf_evlist__channel_complete(struct perf_evlist *evlist)
return 0;
}

-static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
+static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int _idx,
struct mmap_params *mp, int cpu,
- int thread, int *output)
+ int thread, int *outputs)
{
struct perf_evsel *evsel;

evlist__for_each(evlist, evsel) {
- int fd;
+ int fd, channel, idx, err;
+
+ channel = perf_evlist__channel_find(evlist, evsel, false);
+ if (channel < 0) {
+ pr_err("ERROR: unable to find suitable channel for %s\n",
+ evsel->name);
+ return -1;
+ }
+
+ idx = _idx;
+ err = perf_evlist__channel_idx(evlist, &channel, &idx);
+ if (err < 0)
+ return err;

if (evsel->system_wide && thread)
continue;

fd = FD(evsel, cpu, thread);

- if (*output == -1) {
- *output = fd;
- if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
+ if (outputs[channel] == -1) {
+ outputs[channel] = fd;
+ if (__perf_evlist__mmap(evlist, idx, mp, outputs[channel]) < 0)
return -1;
} else {
- if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
+ if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, outputs[channel]) != 0)
return -1;

perf_evlist__mmap_get(evlist, idx);
@@ -1113,14 +1135,15 @@ static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,

pr_debug2("perf event ring buffer mmapped per cpu\n");
for (cpu = 0; cpu < nr_cpus; cpu++) {
- int output = -1;
+ int outputs[PERF_EVLIST__NR_CHANNELS];

+ memset(outputs, -1, sizeof(outputs));
auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
true);

for (thread = 0; thread < nr_threads; thread++) {
if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
- thread, &output))
+ thread, outputs))
goto out_unmap;
}
}
@@ -1128,8 +1151,7 @@ static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
return 0;

out_unmap:
- for (cpu = 0; cpu < nr_cpus; cpu++)
- __perf_evlist__munmap(evlist, cpu);
+ __perf_evlist__munmap_all(evlist);
return -1;
}

@@ -1141,21 +1163,21 @@ static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,

pr_debug2("perf event ring buffer mmapped per thread\n");
for (thread = 0; thread < nr_threads; thread++) {
- int output = -1;
+ int outputs[PERF_EVLIST__NR_CHANNELS];

+ memset(outputs, -1, sizeof(outputs));
auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
false);

if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
- &output))
+ outputs))
goto out_unmap;
}

return 0;

out_unmap:
- for (thread = 0; thread < nr_threads; thread++)
- __perf_evlist__munmap(evlist, thread);
+ __perf_evlist__munmap_all(evlist);
return -1;
}

diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 4cb5d3a..188f0c7 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -20,7 +20,7 @@ struct record_opts;
#define PERF_EVLIST__HLIST_BITS 8
#define PERF_EVLIST__HLIST_SIZE (1 << PERF_EVLIST__HLIST_BITS)

-#define PERF_EVLIST__NR_CHANNELS 1
+#define PERF_EVLIST__NR_CHANNELS 2
enum perf_evlist_mmap_flag {
PERF_EVLIST__CHANNEL_ENABLED = 1,
};
--
1.8.3.4