Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf evsel: Derive CPUs and threads in alloc_counts

Passing the number of CPUs and threads allows for an evsel's counts to
be mismatched to its cpu map. To avoid this always derive the counts
size from the cpu map. Change openat-syscall-all-cpus to set the cpus
to allow for this to work.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John Garry <john.garry@huawei.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paul Clarke <pc@us.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Riccardo Mancini <rickyman7@gmail.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Vineet Singh <vineet.singh@intel.com>
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: zhengjun.xing@intel.com
Link: https://lore.kernel.org/r/20220105061351.120843-27-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
2ca0a371 7365f105

+15 -20
+1 -9
tools/perf/tests/openat-syscall-all-cpus.c
··· 85 85 CPU_CLR(cpus->map[cpu], &cpu_set); 86 86 } 87 87 88 - /* 89 - * Here we need to explicitly preallocate the counts, as if 90 - * we use the auto allocation it will allocate just for 1 cpu, 91 - * as we start by cpu 0. 92 - */ 93 - if (evsel__alloc_counts(evsel, cpus->nr, 1) < 0) { 94 - pr_debug("evsel__alloc_counts(ncpus=%d)\n", cpus->nr); 95 - goto out_close_fd; 96 - } 88 + evsel->core.cpus = perf_cpu_map__get(cpus); 97 89 98 90 err = 0; 99 91
+6 -2
tools/perf/util/counts.c
··· 4 4 #include <string.h> 5 5 #include "evsel.h" 6 6 #include "counts.h" 7 + #include <perf/threadmap.h> 7 8 #include <linux/zalloc.h> 8 9 9 10 struct perf_counts *perf_counts__new(int ncpus, int nthreads) ··· 56 55 perf_counts__reset(evsel->counts); 57 56 } 58 57 59 - int evsel__alloc_counts(struct evsel *evsel, int ncpus, int nthreads) 58 + int evsel__alloc_counts(struct evsel *evsel) 60 59 { 61 - evsel->counts = perf_counts__new(ncpus, nthreads); 60 + struct perf_cpu_map *cpus = evsel__cpus(evsel); 61 + int nthreads = perf_thread_map__nr(evsel->core.threads); 62 + 63 + evsel->counts = perf_counts__new(cpus ? cpus->nr : 1, nthreads); 62 64 return evsel->counts != NULL ? 0 : -ENOMEM; 63 65 } 64 66
+1 -1
tools/perf/util/counts.h
··· 40 40 void perf_counts__reset(struct perf_counts *counts); 41 41 42 42 void evsel__reset_counts(struct evsel *evsel); 43 - int evsel__alloc_counts(struct evsel *evsel, int ncpus, int nthreads); 43 + int evsel__alloc_counts(struct evsel *evsel); 44 44 void evsel__free_counts(struct evsel *evsel); 45 45 46 46 #endif /* __PERF_COUNTS_H */
+1 -1
tools/perf/util/evsel.c
··· 1578 1578 if (FD(evsel, cpu, thread) < 0) 1579 1579 return -EINVAL; 1580 1580 1581 - if (evsel->counts == NULL && evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0) 1581 + if (evsel->counts == NULL && evsel__alloc_counts(evsel) < 0) 1582 1582 return -ENOMEM; 1583 1583 1584 1584 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
+6 -7
tools/perf/util/stat.c
··· 152 152 zfree(&evsel->stats); 153 153 } 154 154 155 - static int evsel__alloc_prev_raw_counts(struct evsel *evsel, int ncpus, int nthreads) 155 + static int evsel__alloc_prev_raw_counts(struct evsel *evsel) 156 156 { 157 + int cpu_map_nr = evsel__nr_cpus(evsel); 158 + int nthreads = perf_thread_map__nr(evsel->core.threads); 157 159 struct perf_counts *counts; 158 160 159 - counts = perf_counts__new(ncpus, nthreads); 161 + counts = perf_counts__new(cpu_map_nr, nthreads); 160 162 if (counts) 161 163 evsel->prev_raw_counts = counts; 162 164 ··· 179 177 180 178 static int evsel__alloc_stats(struct evsel *evsel, bool alloc_raw) 181 179 { 182 - int ncpus = evsel__nr_cpus(evsel); 183 - int nthreads = perf_thread_map__nr(evsel->core.threads); 184 - 185 180 if (evsel__alloc_stat_priv(evsel) < 0 || 186 - evsel__alloc_counts(evsel, ncpus, nthreads) < 0 || 187 - (alloc_raw && evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0)) 181 + evsel__alloc_counts(evsel) < 0 || 182 + (alloc_raw && evsel__alloc_prev_raw_counts(evsel) < 0)) 188 183 return -ENOMEM; 189 184 190 185 return 0;