History

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 $

Ian Rogers committed 4 years ago

Commit 8b464ea1 parent 3cf6a321 file · +17 −11Browse files

Unified · Split

tools/perf/util/evlist.c+17−11
@@ -346,7 +346,7 @@ struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affin · 345 unmodified lines
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),
@@ -354,16 +354,22 @@ struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affin · 1 unmodified line
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 }