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
346{346{
347 struct evlist_cpu_iterator itr = {347 struct evlist_cpu_iterator itr = {
348 .container = evlist,348 .container = evlist,
349 .evsel = evlist__first(evlist),349 .evsel = NULL,
350 .cpu_map_idx = 0,350 .cpu_map_idx = 0,
351 .evlist_cpu_map_idx = 0,351 .evlist_cpu_map_idx = 0,
352 .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),352 .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
354 .affinity = affinity,354 .affinity = affinity,
355 };355 };
356 356
357 if (itr.affinity) {357 if (evlist__empty(evlist)) {
358 itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);358 /* Ensure the empty list doesn't iterate. */
359 affinity__set(itr.affinity, itr.cpu.cpu);359 itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
360 itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);360 } else {
361 /*361 itr.evsel = evlist__first(evlist);
362 * If this CPU isn't in the evsel's cpu map then advance through362 if (itr.affinity) {
363 * the list.363 itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
364 */364 affinity__set(itr.affinity, itr.cpu.cpu);
365 if (itr.cpu_map_idx == -1)365 itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
366 evlist_cpu_iterator__next(&itr);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 }
367 }373 }
368 return itr;374 return itr;
369}375}