Skip to content

Commit 8b464ea

Browse files
captain5050acmel
authored andcommitted
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: 472832d ("perf evlist: Refactor evlist__for_each_cpu()") Reported-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Ian Rogers <irogers@google.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Acked-by: Jiri 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.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 3cf6a32 commit 8b464ea

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

tools/perf/util/evlist.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -346,24 +346,30 @@ struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affin
346346
{
347347
struct evlist_cpu_iterator itr = {
348348
.container = evlist,
349-
.evsel = evlist__first(evlist),
349+
.evsel = NULL,
350350
.cpu_map_idx = 0,
351351
.evlist_cpu_map_idx = 0,
352352
.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
353353
.cpu = (struct perf_cpu){ .cpu = -1},
354354
.affinity = affinity,
355355
};
356356

357-
if (itr.affinity) {
358-
itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
359-
affinity__set(itr.affinity, itr.cpu.cpu);
360-
itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
361-
/*
362-
* If this CPU isn't in the evsel's cpu map then advance through
363-
* the list.
364-
*/
365-
if (itr.cpu_map_idx == -1)
366-
evlist_cpu_iterator__next(&itr);
357+
if (evlist__empty(evlist)) {
358+
/* Ensure the empty list doesn't iterate. */
359+
itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
360+
} else {
361+
itr.evsel = evlist__first(evlist);
362+
if (itr.affinity) {
363+
itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
364+
affinity__set(itr.affinity, itr.cpu.cpu);
365+
itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
366+
/*
367+
* If this CPU isn't in the evsel's cpu map then advance
368+
* through the list.
369+
*/
370+
if (itr.cpu_map_idx == -1)
371+
evlist_cpu_iterator__next(&itr);
372+
}
367373
}
368374
return itr;
369375
}

0 commit comments

Comments
 (0)