perf_counts clean up and perf stat report bug fix
Subsystems (4)
Ian Rogers
reviewercover perf_counts clean up and perf stat report bug fix
perf_counts takes a CPU map index as an argument, however, there were a few places where this hadn't been cleaned up and the index was called cpu. In part this led to the bug discovered by Michael Petlan in:
https://lore.kernel.org/linux-perf-users/CAP-5=fWQR=sCuiSMktvUtcbOLidEpUJLCybVF6=BRvORcDOq+g@mail.gmail.com/
Fix the bug, tidy up more of the arguments passed to perf_counts, add a test to ensure the bug isn't reintroduced and add a helper macro to iterate over just CPU map indices.
Ian Rogers (5):
perf stat: Fix and validate inputs in stat events perf stat: Add stat record+report test
perf cpumap: Add perf_cpu_map__for_each_idx perf bpf_counter: Tidy use of CPU map index perf stat: Make use of index clearer with perf_counts
tools/lib/perf/include/perf/cpumap.h | 3 ++ tools/perf/tests/shell/stat.sh | 13 ++++++ tools/perf/util/bpf_counter.c | 61 ++++++++++++++++------------ tools/perf/util/stat-display.c | 22 +++++----- tools/perf/util/stat.c | 27 ++++++++---- 5 files changed, 81 insertions(+), 45 deletions(-)
Signature · 1 line
2.36.1.124.g0e6072fb45-goog
Ian Rogers
reviewerPATCH 1/5 perf stat: Fix and validate inputs in stat events
Stat events can come from disk and so need a degree of validation. They contain a CPU which needs looking up via CPU map to access a counter. Add the CPU to index translation, alongside validity checking.
Discussion thread:
https://lore.kernel.org/linux-perf-users/CAP-5=fWQR=sCuiSMktvUtcbOLidEpUJLCybVF6=BRvORcDOq+g@mail.gmail.com/
--- tools/perf/util/stat.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/stat.c +14 -3
@@ -474,9 +474,10 @@ int perf_stat_process_counter(struct perf_stat_config *config,int perf_event__process_stat_event (struct perf_session *session,union perf_event *event) { -struct perf_counts_values count; +struct perf_counts_values count, *ptr;struct perf_record_stat *st = &event->stat;struct evsel *counter; +int cpu_map_idx; count.val = st->val; count.ena = st->ena;
@@ -487,8 +488,18 @@ int perf_event__process_stat_event(struct perf_session *session,pr_err ("Failed to resolve counter for stat event.\n" );return -EINVAL ; } - - *perf_counts (counter->counts, st->cpu, st->thread) = count; + cpu_map_idx =perf_cpu_map__idx (evsel__cpus (counter), (struct perf_cpu ){.cpu = st->cpu}); +if (cpu_map_idx ==-1 ) { +pr_err ("Invalid CPU %d for event %s.\n" , st->cpu,evsel__name (counter)); +return -EINVAL ; + } + ptr =perf_counts (counter->counts, cpu_map_idx, st->thread); +if (ptr ==NULL ) { +pr_err ("Failed to find perf count for CPU %d thread %d on event %s.\n" , + st->cpu, st->thread,evsel__name (counter)); +return -EINVAL ; + } + *ptr = count; counter->supported = true;return 0 ; }
Signature · 1 line
2.36.1.124.g0e6072fb45-goog
Ian Rogers
reviewerPATCH 2/5 perf stat: Add stat record+report test
In reply to Ian RogersThis would have caught:
https://lore.kernel.org/linux-perf-users/CAP-5=fWQR=sCuiSMktvUtcbOLidEpUJLCybVF6=BRvORcDOq+g@mail.gmail.com/
--- tools/perf/tests/shell/stat.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+)
tools/perf/tests/shell/stat.sh +13 -0
@@ -16,6 +16,18 @@ test_default_stat() { echo "Basic stat command test [Success]" } +test_stat_record_report() { + echo "stat record and report test" + if ! perf stat record -o - true | perf stat report -i - 2>&1 | \ + egrep -q "Performance counter stats for 'pipe':" + then + echo "stat record and report test [Failed]" + err=1 + return + fi + echo "stat record and report test [Success]" +} + test_topdown_groups() { # Topdown events must be grouped with the slots event first. Test that # parse-events reorders this.
@@ -62,6 +74,7 @@ test_topdown_weak_groups() { } test_default_stat +test_stat_record_report test_topdown_groups test_topdown_weak_groups exit $err
Signature · 1 line
2.36.1.124.g0e6072fb45-goog
Ian Rogers
reviewerPATCH 3/5 perf cpumap: Add perf_cpu_map__for_each_idx
In reply to Ian RogersA variant of perf_cpu_map__for_each_cpu that just iterates index values without the corresponding load of the CPU.
--- tools/lib/perf/include/perf/cpumap.h | 3 +++ 1 file changed, 3 insertions(+)
tools/lib/perf/include/perf/cpumap.h +3 -0
@@ -31,4 +31,7 @@ LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_c (idx) <perf_cpu_map__nr (cpus); \ (idx)++, (cpu) = perf_cpu_map__cpu(cpus, idx)) +#define perf_cpu_map__for_each_idx (idx, cpus) \ + for ((idx) = 0; (idx) < perf_cpu_map__nr(cpus); (idx)++) +#endif /* __LIBPERF_CPUMAP_H */
Signature · 1 line
2.36.1.124.g0e6072fb45-goog
Ian Rogers
reviewerPATCH 4/5 perf bpf_counter: Tidy use of CPU map index
In reply to Ian RogersBPF counters are typically running across all CPUs and so the CPU map index and CPU number are the same. There may be cases with offline CPUs where this isn't the case and so ensure the cpu map index for perf_counts is going to be a valid index by explicitly iterating over the CPU map. This also makes it clearer that users of perf_counts are using an index. Collapse some multiple uses of perf_counts into single uses.
--- tools/perf/util/bpf_counter.c | 61 ++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 26 deletions(-)
tools/perf/util/bpf_counter.c +35 -26
@@ -224,25 +224,25 @@ static int bpf_program_profiler__disable(struct evsel *evsel)static int bpf_program_profiler__read (struct evsel *evsel) { -// perf_cpu_map uses /sys/devices/system/cpu/online -int num_cpu =evsel__nr_cpus (evsel);// BPF_MAP_TYPE_PERCPU_ARRAY uses /sys/devices/system/cpu/possible // Sometimes possible > online, like on a Ryzen 3900X that has 24 // threads but its possible showed 0-31 -acme int num_cpu_bpf =libbpf_num_possible_cpus ();struct bpf_perf_event_value values[num_cpu_bpf];struct bpf_counter *counter; +struct perf_counts_values *counts;int reading_map_fd;__u32 key =0 ; -int err, cpu; +int err, idx, bpf_cpu;if (list_empty (&evsel->bpf_counter_list))return -EAGAIN ; -for (cpu =0 ; cpu < num_cpu; cpu++) { -perf_counts (evsel->counts, cpu,0 )->val =0 ; -perf_counts (evsel->counts, cpu,0 )->ena =0 ; -perf_counts (evsel->counts, cpu,0 )->run =0 ; +perf_cpu_map__for_each_idx (idx,evsel__cpus (evsel)) { + counts =perf_counts (evsel->counts, idx,0 ); + counts->val =0 ; + counts->ena =0 ; + counts->run =0 ; }list_for_each_entry (counter, &evsel->bpf_counter_list, list) {struct bpf_prog_profiler_bpf *skel = counter->skel;
@@ -256,10 +256,15 @@ static int bpf_program_profiler__read(struct evsel *evsel)return err; } -for (cpu =0 ; cpu < num_cpu; cpu++) { -perf_counts (evsel->counts, cpu,0 )->val += values[cpu].counter; -perf_counts (evsel->counts, cpu,0 )->ena += values[cpu].enabled; -perf_counts (evsel->counts, cpu,0 )->run += values[cpu].running; +for (bpf_cpu =0 ; bpf_cpu < num_cpu_bpf; bpf_cpu++) { + idx =perf_cpu_map__idx (evsel__cpus (evsel), + (struct perf_cpu ){.cpu = bpf_cpu}); +if (idx ==-1 ) +continue ; + counts =perf_counts (evsel->counts, idx,0 ); + counts->val += values[bpf_cpu].counter; + counts->ena += values[bpf_cpu].enabled; + counts->run += values[bpf_cpu].running; } }return 0 ;
@@ -621,6 +626,7 @@ static int bperf__read(struct evsel *evsel)struct bperf_follower_bpf *skel = evsel->follower_skel;__u32 num_cpu_bpf =cpu__max_cpu ().cpu;struct bpf_perf_event_value values[num_cpu_bpf]; +struct perf_counts_values *counts;int reading_map_fd, err =0 ;__u32 i;int j;
@@ -639,29 +645,32 @@ static int bperf__read(struct evsel *evsel)case BPERF_FILTER_GLOBAL :assert (i ==0 ); -perf_cpu_map__for_each_cpu (entry, j, all_cpu_map) { - cpu = entry.cpu; -perf_counts (evsel->counts, cpu,0 )->val = values[cpu].counter; -perf_counts (evsel->counts, cpu,0 )->ena = values[cpu].enabled; -perf_counts (evsel->counts, cpu,0 )->run = values[cpu].running; +perf_cpu_map__for_each_cpu (entry, j,evsel__cpus (evsel)) { + counts =perf_counts (evsel->counts, j,0 ); + counts->val = values[entry.cpu].counter; + counts->ena = values[entry.cpu].enabled; + counts->run = values[entry.cpu].running; }break ;case BPERF_FILTER_CPU : - cpu = evsel->core.cpus->map[i].cpu; -perf_counts (evsel->counts, i,0 )->val = values[cpu].counter; -perf_counts (evsel->counts, i,0 )->ena = values[cpu].enabled; -perf_counts (evsel->counts, i,0 )->run = values[cpu].running; + cpu =perf_cpu_map__cpu (evsel__cpus (evsel), i).cpu; +assert (cpu >=0 ); + counts =perf_counts (evsel->counts, i,0 ); + counts->val = values[cpu].counter; + counts->ena = values[cpu].enabled; + counts->run = values[cpu].running;break ;case BPERF_FILTER_PID :case BPERF_FILTER_TGID : -perf_counts (evsel->counts,0 , i)->val =0 ; -perf_counts (evsel->counts,0 , i)->ena =0 ; -perf_counts (evsel->counts,0 , i)->run =0 ; + counts =perf_counts (evsel->counts,0 , i); + counts->val =0 ; + counts->ena =0 ; + counts->run =0 ;for (cpu =0 ; cpu < num_cpu_bpf; cpu++) { -perf_counts (evsel->counts,0 , i)->val += values[cpu].counter; -perf_counts (evsel->counts,0 , i)->ena += values[cpu].enabled; -perf_counts (evsel->counts,0 , i)->run += values[cpu].running; + counts->val += values[cpu].counter; + counts->ena += values[cpu].enabled; + counts->run += values[cpu].running; }break ;default :
Signature · 1 line
2.36.1.124.g0e6072fb45-goog
Ian Rogers
reviewerPATCH 5/5 perf stat: Make use of index clearer with perf_counts
In reply to Ian RogersTry to disambiguate further when perf_counts is being accessed it is with a cpu map index rather than a CPU.
--- tools/perf/util/stat-display.c | 22 ++++++++++++---------- tools/perf/util/stat.c | 10 ++++------ 2 files changed, 16 insertions(+), 16 deletions(-)
tools/perf/util/stat-display.c +12 -10
@@ -764,11 +764,11 @@ static int cmp_val(const void *a, const void *b)static struct perf_aggr_thread_value *sort_aggr_thread (struct evsel *counter, -int nthreads,int ncpus,int *ret,struct target *_target) { -int cpu, thread, i =0 ; +int nthreads =perf_thread_map__nr (counter->core.threads); +int i =0 ;double uval;struct perf_aggr_thread_value *buf;
@@ -776,13 +776,17 @@ static struct perf_aggr_thread_value *sort_aggr_thread(if (!buf)return NULL ; -for (thread =0 ; thread < nthreads; thread++) { +for (int thread =0 ; thread < nthreads; thread++) { +int idx;u64 ena =0 , run =0 , val =0 ; -for (cpu =0 ; cpu < ncpus; cpu++) { - val +=perf_counts (counter->counts, cpu, thread)->val; - ena +=perf_counts (counter->counts, cpu, thread)->ena; - run +=perf_counts (counter->counts, cpu, thread)->run; +perf_cpu_map__for_each_idx (idx,evsel__cpus (counter)) { +struct perf_counts_values *counts = +perf_counts (counter->counts, idx, thread); + + val += counts->val; + ena += counts->ena; + run += counts->run; } uval = val * counter->scale;
@@ -817,13 +821,11 @@ static void print_aggr_thread(struct perf_stat_config *config, struct evsel *counter, char *prefix) { FILE *output = config->output; -int nthreads =perf_thread_map__nr (counter->core.threads); -int ncpus =perf_cpu_map__nr (counter->core.cpus);int thread, sorted_threads;struct aggr_cpu_id id;struct perf_aggr_thread_value *buf; - buf =sort_aggr_thread (counter, nthreads, ncpus, &sorted_threads, _target); + buf =sort_aggr_thread (counter, &sorted_threads, _target);if (!buf) {perror ("cannot sort aggr thread" );return ;
tools/perf/util/stat.c +4 -6
@@ -237,14 +237,12 @@ void evlist__reset_prev_raw_counts(struct evlist *evlist)static void evsel__copy_prev_raw_counts (struct evsel *evsel) { -int ncpus =evsel__nr_cpus (evsel); -int nthreads =perf_thread_map__nr (evsel->core.threads); +int idx, nthreads =perf_thread_map__nr (evsel->core.threads);for (int thread =0 ; thread < nthreads; thread++) { -for (int cpu =0 ; cpu < ncpus; cpu++) { - *perf_counts (evsel->counts, cpu, thread) = - *perf_counts (evsel->prev_raw_counts, cpu, - thread); +perf_cpu_map__for_each_idx (idx,evsel__cpus (evsel)) { + *perf_counts (evsel->counts, idx, thread) = + *perf_counts (evsel->prev_raw_counts, idx, thread); } }
Signature · 1 line
2.36.1.124.g0e6072fb45-goog
Arnaldo Carvalho de Melo
maintainerEm Wed, May 18, 2022 at 08:20:00PM -0700, Ian Rogers escreveu:
Quoted 8 lines
perf_countstakes a CPU map index as an argument, however, there were a few places where this hadn't been cleaned up and the index was called cpu. In part this led to the bug discovered by Michael Petlan in:
https://lore.kernel.org/linux-perf-users/CAP-5=fWQR=sCuiSMktvUtcbOLidEpUJLCybVF6=BRvORcDOq+g@mail.gmail.com/Fix the bug, tidy up more of the arguments passed to
perf_counts, add a test to ensure the bug isn't reintroduced and add a helper macro to iterate over just CPU map indices.
Applied 2-5 to perf/core. The first is already in 5.18
Thanks,
- Arnaldo
Quoted 16 lines
Ian Rogers (5):
perf stat: Fix and validate inputs in stat events perf stat: Add stat record+report test
perf cpumap: Addperf_cpu_map__for_each_idxperfbpf_counter: Tidy use of CPU map index perf stat: Make use of index clearer withperf_countstools/lib/perf/include/perf/cpumap.h | 3 ++ tools/perf/tests/shell/stat.sh | 13 ++++++ tools/perf/util/bpf_counter.c | 61 ++++++++++++++++------------ tools/perf/util/stat-display.c | 22 +++++----- tools/perf/util/stat.c | 27 ++++++++---- 5 files changed, 81 insertions(+), 45 deletions(-)Signature · 1 line
2.36.1.124.g0e6072fb45-goog
Signature · 1 line
- Arnaldo