perf evsel: Derive CPUs and threads in alloc_counts
authorIan Rogers <irogers@google.com>
Wed, 5 Jan 2022 06:13:29 +0000 (22:13 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 12 Jan 2022 17:28:22 +0000 (14:28 -0300)
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>
tools/perf/tests/openat-syscall-all-cpus.c
tools/perf/util/counts.c
tools/perf/util/counts.h
tools/perf/util/evsel.c
tools/perf/util/stat.c

index cd3dd463783fd047b8ac7caa3719dd01a940e218..544db0839b3b9cccc130aa81eb89096504be3552 100644 (file)
@@ -85,15 +85,7 @@ static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __mayb
                CPU_CLR(cpus->map[cpu], &cpu_set);
        }
 
-       /*
-        * Here we need to explicitly preallocate the counts, as if
-        * we use the auto allocation it will allocate just for 1 cpu,
-        * as we start by cpu 0.
-        */
-       if (evsel__alloc_counts(evsel, cpus->nr, 1) < 0) {
-               pr_debug("evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
-               goto out_close_fd;
-       }
+       evsel->core.cpus = perf_cpu_map__get(cpus);
 
        err = 0;
 
index 582f3aeaf5e45091314476128f6127833cea002a..2b81707b9dbaef155eb3b14dde3f262376685bf4 100644 (file)
@@ -4,6 +4,7 @@
 #include <string.h>
 #include "evsel.h"
 #include "counts.h"
+#include <perf/threadmap.h>
 #include <linux/zalloc.h>
 
 struct perf_counts *perf_counts__new(int ncpus, int nthreads)
@@ -55,9 +56,12 @@ void evsel__reset_counts(struct evsel *evsel)
        perf_counts__reset(evsel->counts);
 }
 
-int evsel__alloc_counts(struct evsel *evsel, int ncpus, int nthreads)
+int evsel__alloc_counts(struct evsel *evsel)
 {
-       evsel->counts = perf_counts__new(ncpus, nthreads);
+       struct perf_cpu_map *cpus = evsel__cpus(evsel);
+       int nthreads = perf_thread_map__nr(evsel->core.threads);
+
+       evsel->counts = perf_counts__new(cpus ? cpus->nr : 1, nthreads);
        return evsel->counts != NULL ? 0 : -ENOMEM;
 }
 
index 7ff36bf6d64485c04ae2a77c070b50cbbbba2709..3e275e9c60d10a7ad4d149208c149fd3f454803b 100644 (file)
@@ -40,7 +40,7 @@ void perf_counts__delete(struct perf_counts *counts);
 void perf_counts__reset(struct perf_counts *counts);
 
 void evsel__reset_counts(struct evsel *evsel);
-int evsel__alloc_counts(struct evsel *evsel, int ncpus, int nthreads);
+int evsel__alloc_counts(struct evsel *evsel);
 void evsel__free_counts(struct evsel *evsel);
 
 #endif /* __PERF_COUNTS_H */
index a0acf53a25109e84748c67db8df3fa1ffa5a2fd7..2de569a1a272ccdda5f94532333d48d1a08b3e1f 100644 (file)
@@ -1578,7 +1578,7 @@ int __evsel__read_on_cpu(struct evsel *evsel, int cpu, int thread, bool scale)
        if (FD(evsel, cpu, thread) < 0)
                return -EINVAL;
 
-       if (evsel->counts == NULL && evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
+       if (evsel->counts == NULL && evsel__alloc_counts(evsel) < 0)
                return -ENOMEM;
 
        if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
index c69b221f5e3e9b3bf3cae7ec42846773d2c7de36..995cb5003133c95067e63b47c93f84de3dc16a06 100644 (file)
@@ -152,11 +152,13 @@ static void evsel__free_stat_priv(struct evsel *evsel)
        zfree(&evsel->stats);
 }
 
-static int evsel__alloc_prev_raw_counts(struct evsel *evsel, int ncpus, int nthreads)
+static int evsel__alloc_prev_raw_counts(struct evsel *evsel)
 {
+       int cpu_map_nr = evsel__nr_cpus(evsel);
+       int nthreads = perf_thread_map__nr(evsel->core.threads);
        struct perf_counts *counts;
 
-       counts = perf_counts__new(ncpus, nthreads);
+       counts = perf_counts__new(cpu_map_nr, nthreads);
        if (counts)
                evsel->prev_raw_counts = counts;
 
@@ -177,12 +179,9 @@ static void evsel__reset_prev_raw_counts(struct evsel *evsel)
 
 static int evsel__alloc_stats(struct evsel *evsel, bool alloc_raw)
 {
-       int ncpus = evsel__nr_cpus(evsel);
-       int nthreads = perf_thread_map__nr(evsel->core.threads);
-
        if (evsel__alloc_stat_priv(evsel) < 0 ||
-           evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
-           (alloc_raw && evsel__alloc_prev_raw_counts(evsel, ncpus, nthreads) < 0))
+           evsel__alloc_counts(evsel) < 0 ||
+           (alloc_raw && evsel__alloc_prev_raw_counts(evsel) < 0))
                return -ENOMEM;
 
        return 0;