Commit 8b464eac authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf evlist: Avoid iteration for empty evlist.

As seen with 'perf stat --null ..' and reported in:
https://lore.kernel.org/lkml/YjCLcpcX2peeQVCH@kernel.org/

v2. Avoids setting evsel in the empty list case as suggested by Jiri Olsa.

    Committer testing:

Before:

  $  perf stat --null sleep 1
  Segmentation fault (core dumped)
  $

After:

  $  perf stat --null sleep 1

   Performance counter stats for 'sleep 1':

         1.010340646 seconds time elapsed

         0.001420000 seconds user
         0.000000000 seconds sys
  $

Fixes: 472832d2 ("perf evlist: Refactor evlist__for_each_cpu()")
Reported-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Tested-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20220317231643.550902-1-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 3cf6a32f
...@@ -346,7 +346,7 @@ struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affin ...@@ -346,7 +346,7 @@ struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affin
{ {
struct evlist_cpu_iterator itr = { struct evlist_cpu_iterator itr = {
.container = evlist, .container = evlist,
.evsel = evlist__first(evlist), .evsel = NULL,
.cpu_map_idx = 0, .cpu_map_idx = 0,
.evlist_cpu_map_idx = 0, .evlist_cpu_map_idx = 0,
.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus), .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
...@@ -354,17 +354,23 @@ struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affin ...@@ -354,17 +354,23 @@ struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affin
.affinity = affinity, .affinity = affinity,
}; };
if (evlist__empty(evlist)) {
/* Ensure the empty list doesn't iterate. */
itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
} else {
itr.evsel = evlist__first(evlist);
if (itr.affinity) { if (itr.affinity) {
itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0); itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
affinity__set(itr.affinity, itr.cpu.cpu); affinity__set(itr.affinity, itr.cpu.cpu);
itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu); itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
/* /*
* If this CPU isn't in the evsel's cpu map then advance through * If this CPU isn't in the evsel's cpu map then advance
* the list. * through the list.
*/ */
if (itr.cpu_map_idx == -1) if (itr.cpu_map_idx == -1)
evlist_cpu_iterator__next(&itr); evlist_cpu_iterator__next(&itr);
} }
}
return itr; return itr;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment