perf stat: Switch to cpu version of cpu_map__get()
authorIan Rogers <irogers@google.com>
Wed, 5 Jan 2022 06:13:08 +0000 (22:13 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 12 Jan 2022 17:28:21 +0000 (14:28 -0300)
Avoid possible bugs where the wrong index is passed with the cpu_map.

Reviewed-by: James Clark <james.clark@arm.com>
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: 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-6-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-stat.c
tools/perf/util/stat-display.c
tools/perf/util/stat.h

index f6ca2b054c5b08f1659958b9cffafaac0f665e22..9791ae9b1a539af74fb8c4955087f30d355bb795 100644 (file)
@@ -1323,69 +1323,63 @@ static struct option stat_options[] = {
 };
 
 static struct aggr_cpu_id perf_stat__get_socket(struct perf_stat_config *config __maybe_unused,
-                                struct perf_cpu_map *map, int cpu)
+                                               int cpu)
 {
-       return cpu_map__get_socket(map, cpu, NULL);
+       return cpu_map__get_socket_aggr_by_cpu(cpu, /*data=*/NULL);
 }
 
 static struct aggr_cpu_id perf_stat__get_die(struct perf_stat_config *config __maybe_unused,
-                             struct perf_cpu_map *map, int cpu)
+                                            int cpu)
 {
-       return cpu_map__get_die(map, cpu, NULL);
+       return cpu_map__get_die_aggr_by_cpu(cpu, /*data=*/NULL);
 }
 
 static struct aggr_cpu_id perf_stat__get_core(struct perf_stat_config *config __maybe_unused,
-                              struct perf_cpu_map *map, int cpu)
+                                             int cpu)
 {
-       return cpu_map__get_core(map, cpu, NULL);
+       return cpu_map__get_core_aggr_by_cpu(cpu, /*data=*/NULL);
 }
 
 static struct aggr_cpu_id perf_stat__get_node(struct perf_stat_config *config __maybe_unused,
-                              struct perf_cpu_map *map, int cpu)
+                                             int cpu)
 {
-       return cpu_map__get_node(map, cpu, NULL);
+       return cpu_map__get_node_aggr_by_cpu(cpu, /*data=*/NULL);
 }
 
 static struct aggr_cpu_id perf_stat__get_aggr(struct perf_stat_config *config,
-                              aggr_get_id_t get_id, struct perf_cpu_map *map, int idx)
+                                             aggr_get_id_t get_id, int cpu)
 {
-       int cpu;
        struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
 
-       if (idx >= map->nr)
-               return id;
-
-       cpu = map->map[idx];
-
        if (cpu_map__aggr_cpu_id_is_empty(config->cpus_aggr_map->map[cpu]))
-               config->cpus_aggr_map->map[cpu] = get_id(config, map, idx);
+               config->cpus_aggr_map->map[cpu] = get_id(config, cpu);
 
        id = config->cpus_aggr_map->map[cpu];
        return id;
 }
 
 static struct aggr_cpu_id perf_stat__get_socket_cached(struct perf_stat_config *config,
-                                       struct perf_cpu_map *map, int idx)
+                                                      int cpu)
 {
-       return perf_stat__get_aggr(config, perf_stat__get_socket, map, idx);
+       return perf_stat__get_aggr(config, perf_stat__get_socket, cpu);
 }
 
 static struct aggr_cpu_id perf_stat__get_die_cached(struct perf_stat_config *config,
-                                       struct perf_cpu_map *map, int idx)
+                                                   int cpu)
 {
-       return perf_stat__get_aggr(config, perf_stat__get_die, map, idx);
+       return perf_stat__get_aggr(config, perf_stat__get_die, cpu);
 }
 
 static struct aggr_cpu_id perf_stat__get_core_cached(struct perf_stat_config *config,
-                                     struct perf_cpu_map *map, int idx)
+                                                    int cpu)
 {
-       return perf_stat__get_aggr(config, perf_stat__get_core, map, idx);
+       return perf_stat__get_aggr(config, perf_stat__get_core, cpu);
 }
 
 static struct aggr_cpu_id perf_stat__get_node_cached(struct perf_stat_config *config,
-                                     struct perf_cpu_map *map, int idx)
+                                                    int cpu)
 {
-       return perf_stat__get_aggr(config, perf_stat__get_node, map, idx);
+       return perf_stat__get_aggr(config, perf_stat__get_node, cpu);
 }
 
 static bool term_percore_set(void)
@@ -1483,8 +1477,9 @@ static void perf_stat__exit_aggr_mode(void)
        stat_config.cpus_aggr_map = NULL;
 }
 
-static inline int perf_env__get_cpu(struct perf_env *env, struct perf_cpu_map *map, int idx)
+static inline int perf_env__get_cpu(void *data, struct perf_cpu_map *map, int idx)
 {
+       struct perf_env *env = data;
        int cpu;
 
        if (idx > map->nr)
@@ -1498,10 +1493,9 @@ static inline int perf_env__get_cpu(struct perf_env *env, struct perf_cpu_map *m
        return cpu;
 }
 
-static struct aggr_cpu_id perf_env__get_socket(struct perf_cpu_map *map, int idx, void *data)
+static struct aggr_cpu_id perf_env__get_socket_aggr_by_cpu(int cpu, void *data)
 {
        struct perf_env *env = data;
-       int cpu = perf_env__get_cpu(env, map, idx);
        struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
 
        if (cpu != -1)
@@ -1510,11 +1504,15 @@ static struct aggr_cpu_id perf_env__get_socket(struct perf_cpu_map *map, int idx
        return id;
 }
 
-static struct aggr_cpu_id perf_env__get_die(struct perf_cpu_map *map, int idx, void *data)
+static struct aggr_cpu_id perf_env__get_socket(struct perf_cpu_map *map, int idx, void *data)
+{
+       return perf_env__get_socket_aggr_by_cpu(perf_env__get_cpu(data, map, idx), data);
+}
+
+static struct aggr_cpu_id perf_env__get_die_aggr_by_cpu(int cpu, void *data)
 {
        struct perf_env *env = data;
        struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
-       int cpu = perf_env__get_cpu(env, map, idx);
 
        if (cpu != -1) {
                /*
@@ -1529,11 +1527,15 @@ static struct aggr_cpu_id perf_env__get_die(struct perf_cpu_map *map, int idx, v
        return id;
 }
 
-static struct aggr_cpu_id perf_env__get_core(struct perf_cpu_map *map, int idx, void *data)
+static struct aggr_cpu_id perf_env__get_die(struct perf_cpu_map *map, int idx, void *data)
+{
+       return perf_env__get_die_aggr_by_cpu(perf_env__get_cpu(data, map, idx), data);
+}
+
+static struct aggr_cpu_id perf_env__get_core_aggr_by_cpu(int cpu, void *data)
 {
        struct perf_env *env = data;
        struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
-       int cpu = perf_env__get_cpu(env, map, idx);
 
        if (cpu != -1) {
                /*
@@ -1549,15 +1551,24 @@ static struct aggr_cpu_id perf_env__get_core(struct perf_cpu_map *map, int idx,
        return id;
 }
 
-static struct aggr_cpu_id perf_env__get_node(struct perf_cpu_map *map, int idx, void *data)
+static struct aggr_cpu_id perf_env__get_core(struct perf_cpu_map *map, int idx, void *data)
+{
+       return perf_env__get_core_aggr_by_cpu(perf_env__get_cpu(data, map, idx), data);
+}
+
+static struct aggr_cpu_id perf_env__get_node_aggr_by_cpu(int cpu, void *data)
 {
-       int cpu = perf_env__get_cpu(data, map, idx);
        struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
 
        id.node = perf_env__numa_node(data, cpu);
        return id;
 }
 
+static struct aggr_cpu_id perf_env__get_node(struct perf_cpu_map *map, int idx, void *data)
+{
+       return perf_env__get_node_aggr_by_cpu(perf_env__get_cpu(data, map, idx), data);
+}
+
 static int perf_env__build_socket_map(struct perf_env *env, struct perf_cpu_map *cpus,
                                      struct cpu_aggr_map **sockp)
 {
@@ -1583,26 +1594,26 @@ static int perf_env__build_node_map(struct perf_env *env, struct perf_cpu_map *c
 }
 
 static struct aggr_cpu_id perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused,
-                                     struct perf_cpu_map *map, int idx)
+                                                    int cpu)
 {
-       return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
+       return perf_env__get_socket_aggr_by_cpu(cpu, &perf_stat.session->header.env);
 }
 static struct aggr_cpu_id perf_stat__get_die_file(struct perf_stat_config *config __maybe_unused,
-                                  struct perf_cpu_map *map, int idx)
+                                                 int cpu)
 {
-       return perf_env__get_die(map, idx, &perf_stat.session->header.env);
+       return perf_env__get_die_aggr_by_cpu(cpu, &perf_stat.session->header.env);
 }
 
 static struct aggr_cpu_id perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused,
-                                   struct perf_cpu_map *map, int idx)
+                                                  int cpu)
 {
-       return perf_env__get_core(map, idx, &perf_stat.session->header.env);
+       return perf_env__get_core_aggr_by_cpu(cpu, &perf_stat.session->header.env);
 }
 
 static struct aggr_cpu_id perf_stat__get_node_file(struct perf_stat_config *config __maybe_unused,
-                                   struct perf_cpu_map *map, int idx)
+                                                  int cpu)
 {
-       return perf_env__get_node(map, idx, &perf_stat.session->header.env);
+       return perf_env__get_node_aggr_by_cpu(cpu, &perf_stat.session->header.env);
 }
 
 static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
index efab39a759ff7fbf3c96f9a152ce25620aa15620..6c40b91d5e3269a108cc80088a70d5d470dcac30 100644 (file)
@@ -341,8 +341,7 @@ static int first_shadow_cpu(struct perf_stat_config *config,
 
        cpus = evsel__cpus(evsel);
        perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
-               if (cpu_map__compare_aggr_cpu_id(config->aggr_get_id(config, cpus, idx),
-                                                id))
+               if (cpu_map__compare_aggr_cpu_id(config->aggr_get_id(config, cpu), id))
                        return cpu;
        }
        return 0;
@@ -525,8 +524,7 @@ static void aggr_update_shadow(struct perf_stat_config *config,
                        cpus = evsel__cpus(counter);
                        val = 0;
                        perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
-                               (void)cpu;
-                               s2 = config->aggr_get_id(config, cpus, idx);
+                               s2 = config->aggr_get_id(config, cpu);
                                if (!cpu_map__compare_aggr_cpu_id(s2, id))
                                        continue;
                                val += perf_counts(counter->counts, idx, 0)->val;
@@ -642,8 +640,7 @@ static void aggr_cb(struct perf_stat_config *config,
        perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
                struct perf_counts_values *counts;
 
-               (void)cpu;
-               s2 = config->aggr_get_id(config, cpus, idx);
+               s2 = config->aggr_get_id(config, cpu);
                if (!cpu_map__compare_aggr_cpu_id(s2, ad->id))
                        continue;
                if (first)
@@ -1217,7 +1214,7 @@ static void print_percore_thread(struct perf_stat_config *config,
 
        cpus = evsel__cpus(counter);
        perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
-               s2 = config->aggr_get_id(config, cpus, idx);
+               s2 = config->aggr_get_id(config, cpu);
                for (s = 0; s < config->aggr_map->nr; s++) {
                        id = config->aggr_map->map[s];
                        if (cpu_map__compare_aggr_cpu_id(s2, id))
index 32c8527de3478737b5ee80a2ecf4dd97fe5af512..32cf241862292f4a9a1d71a63c36068aac366349 100644 (file)
@@ -108,8 +108,7 @@ struct runtime_stat {
        struct rblist value_list;
 };
 
-typedef struct aggr_cpu_id (*aggr_get_id_t)(struct perf_stat_config *config,
-                            struct perf_cpu_map *m, int cpu);
+typedef struct aggr_cpu_id (*aggr_get_id_t)(struct perf_stat_config *config, int cpu);
 
 struct perf_stat_config {
        enum aggr_mode           aggr_mode;