]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf header: Pass a perf_cpu rather than a PMU to get_cpuid_str
authorIan Rogers <irogers@google.com>
Thu, 7 Nov 2024 16:20:33 +0000 (08:20 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Sat, 16 Nov 2024 19:40:30 +0000 (16:40 -0300)
On ARM the cpuid is dependent on the core type of the CPU in
question. The PMU was passed for the sake of the CPU map but this
means in places a temporary PMU is created just to pass a CPU
value. Just pass the CPU and fix up the callers.

As there are no longer PMU users in header.h, shuffle forward
declarations earlier to work around build failures.

Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Xu Yang <xu.yang_2@nxp.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ben Zong-You Xie <ben717@andestech.com>
Cc: Benjamin Gray <bgray@linux.ibm.com>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Clément Le Goffic <clement.legoffic@foss.st.com>
Cc: Dima Kogan <dima@secretsauce.net>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yicong Yang <yangyicong@hisilicon.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-riscv@lists.infradead.org
Link: https://lore.kernel.org/r/20241107162035.52206-7-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
15 files changed:
tools/perf/arch/arm64/util/arm-spe.c
tools/perf/arch/arm64/util/header.c
tools/perf/arch/loongarch/util/header.c
tools/perf/arch/powerpc/util/header.c
tools/perf/arch/riscv/util/header.c
tools/perf/arch/s390/util/header.c
tools/perf/arch/x86/util/header.c
tools/perf/pmu-events/empty-pmu-events.c
tools/perf/pmu-events/jevents.py
tools/perf/tests/expr.c
tools/perf/util/expr.c
tools/perf/util/header.c
tools/perf/util/header.h
tools/perf/util/pmu.c
tools/perf/util/pmu.h

index 7ccdea3cd461a4bb19aede0abd73b7409580b398..22b19dcc6beb8ec2f1f1abfa09888be84146cb03 100644 (file)
@@ -23,6 +23,7 @@
 #include "../../../util/debug.h"
 #include "../../../util/auxtrace.h"
 #include "../../../util/record.h"
+#include "../../../util/header.h"
 #include "../../../util/arm-spe.h"
 #include <tools/libc_compat.h> // reallocarray
 
@@ -85,22 +86,11 @@ static int arm_spe_save_cpu_header(struct auxtrace_record *itr,
        struct arm_spe_recording *sper =
                        container_of(itr, struct arm_spe_recording, itr);
        struct perf_pmu *pmu = NULL;
-       struct perf_pmu tmp_pmu;
-       char cpu_id_str[16];
        char *cpuid = NULL;
        u64 val;
 
-       snprintf(cpu_id_str, sizeof(cpu_id_str), "%d", cpu.cpu);
-       tmp_pmu.cpus = perf_cpu_map__new(cpu_id_str);
-       if (!tmp_pmu.cpus)
-               return -ENOMEM;
-
        /* Read CPU MIDR */
-       cpuid = perf_pmu__getcpuid(&tmp_pmu);
-
-       /* The CPU map will not be used anymore, release it */
-       perf_cpu_map__put(tmp_pmu.cpus);
-
+       cpuid = get_cpuid_allow_env_override(cpu);
        if (!cpuid)
                return -ENOMEM;
        val = strtol(cpuid, NULL, 16);
index f0907daad3ae76745f5208ff194c2f9b0d19e3a9..f445a2dd6293442645404d7c42c3f7a2992bd9c9 100644 (file)
@@ -62,22 +62,18 @@ int get_cpuid(char *buf, size_t sz, struct perf_cpu cpu)
        return EINVAL;
 }
 
-char *get_cpuid_str(struct perf_pmu *pmu)
+char *get_cpuid_str(struct perf_cpu cpu)
 {
-       char *buf = NULL;
+       char *buf = malloc(MIDR_SIZE);
        int res;
 
-       if (!pmu || !pmu->cpus)
-               return NULL;
-
-       buf = malloc(MIDR_SIZE);
        if (!buf)
                return NULL;
 
        /* read midr from list of cpus mapped to this pmu */
-       res = get_cpuid(buf, MIDR_SIZE, perf_cpu_map__min(pmu->cpus));
+       res = get_cpuid(buf, MIDR_SIZE, cpu);
        if (res) {
-               pr_err("failed to get cpuid string for PMU %s\n", pmu->name);
+               pr_err("failed to get cpuid string for CPU %d\n", cpu.cpu);
                free(buf);
                buf = NULL;
        }
index f1f0b116962d3a6f1d2594485a47ca52cb9dc7da..0c6d823334a21df85117e5cb1000b296166d0e6d 100644 (file)
@@ -90,7 +90,7 @@ out_free:
        return ret;
 }
 
-char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
+char *get_cpuid_str(struct perf_cpu cpu __maybe_unused)
 {
        return _get_cpuid();
 }
index 6d1a63a2922f84e4149a47911cc90c11baf4a9b6..c7df534dbf8f845a00faf639c4d3c7f82e1287d1 100644 (file)
@@ -42,7 +42,7 @@ get_cpuid(char *buffer, size_t sz, struct perf_cpu cpu __maybe_unused)
 }
 
 char *
-get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
+get_cpuid_str(struct perf_cpu cpu __maybe_unused)
 {
        char *bufp;
        unsigned long pvr;
index ebac294c877f0ba6c7de258819ee48982fda65e4..4b839203d4a54d78fbdc56474c95d21388c127cf 100644 (file)
@@ -98,7 +98,7 @@ free:
 }
 
 char *
-get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
+get_cpuid_str(struct perf_cpu cpu __maybe_unused)
 {
        return _get_cpuid();
 }
index 2add1a5612420aea01c6433359b8c4090e3eab83..db54677a17d24a8cd2af8bc7338895080dd6f5d4 100644 (file)
@@ -137,11 +137,11 @@ skip_sysinfo:
        return (nbytes >= sz) ? ENOBUFS : 0;
 }
 
-char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
+char *get_cpuid_str(struct perf_cpu cpu)
 {
        char *buf = malloc(128);
 
-       if (buf && get_cpuid(buf, 128))
+       if (buf && get_cpuid(buf, 128, cpu))
                zfree(&buf);
        return buf;
 }
index 690f86cbbb1ca3d993240fa4a1c1d25a8399b96e..412977f8aa8374de8221bb5f4517c94a52431c4e 100644 (file)
@@ -63,8 +63,7 @@ get_cpuid(char *buffer, size_t sz, struct perf_cpu cpu __maybe_unused)
        return __get_cpuid(buffer, sz, "%s,%u,%u,%u$");
 }
 
-char *
-get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
+char *get_cpuid_str(struct perf_cpu cpu __maybe_unused)
 {
        char *buf = malloc(128);
 
index b8719dab264d9cfea0c5b05b765f0f748e51b456..51ca8d61c15d83bc178d330f2939d36ae45bbf8f 100644 (file)
@@ -515,13 +515,16 @@ static const struct pmu_events_map *map_for_pmu(struct perf_pmu *pmu)
         } last_map_search;
         static bool has_last_result, has_last_map_search;
         const struct pmu_events_map *map = NULL;
+        struct perf_cpu cpu = {-1};
         char *cpuid = NULL;
         size_t i;
 
         if (has_last_result && last_result.pmu == pmu)
                 return last_result.map;
 
-        cpuid = perf_pmu__getcpuid(pmu);
+        if (pmu)
+                cpu = perf_cpu_map__min(pmu->cpus);
+        cpuid = get_cpuid_allow_env_override(cpu);
 
         /*
          * On some platforms which uses cpus map, cpuid can be NULL for
index 70f4fd5395fb68e979c64dd6cfdd6b6b19bd6705..b5ff872e2beb3d1c1f785f47cd09c3057f60eaa2 100755 (executable)
@@ -1031,13 +1031,16 @@ static const struct pmu_events_map *map_for_pmu(struct perf_pmu *pmu)
         } last_map_search;
         static bool has_last_result, has_last_map_search;
         const struct pmu_events_map *map = NULL;
+        struct perf_cpu cpu = {-1};
         char *cpuid = NULL;
         size_t i;
 
         if (has_last_result && last_result.pmu == pmu)
                 return last_result.map;
 
-        cpuid = perf_pmu__getcpuid(pmu);
+        if (pmu)
+                cpu = perf_cpu_map__min(pmu->cpus);
+        cpuid = get_cpuid_allow_env_override(cpu);
 
         /*
          * On some platforms which uses cpus map, cpuid can be NULL for
index d60f1ac1d720eb78a760422dd3a0efd1a42e5b27..41ff1affdfcdf31c899d449e403e9b61288cd359 100644 (file)
@@ -4,10 +4,9 @@
 #include "util/expr.h"
 #include "util/hashmap.h"
 #include "util/header.h"
-#include "util/pmu.h"
-#include "util/pmus.h"
 #include "util/smt.h"
 #include "tests.h"
+#include <perf/cpumap.h>
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
@@ -78,8 +77,8 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u
        struct expr_parse_ctx *ctx;
        bool is_intel = false;
        char strcmp_cpuid_buf[256];
-       struct perf_pmu *pmu = perf_pmus__find_core_pmu();
-       char *cpuid = perf_pmu__getcpuid(pmu);
+       struct perf_cpu cpu = {-1};
+       char *cpuid = get_cpuid_allow_env_override(cpu);
        char *escaped_cpuid1, *escaped_cpuid2;
 
        TEST_ASSERT_VAL("get_cpuid", cpuid);
index 5e3732bc2fa52a426181c8774d534085cd157550..f289044a1f7c633f09be78d567f1d73cebd9041a 100644 (file)
@@ -8,7 +8,6 @@
 #include "debug.h"
 #include "evlist.h"
 #include "expr.h"
-#include "pmu.h"
 #include "smt.h"
 #include "tool_pmu.h"
 #include <util/expr-bison.h>
@@ -16,6 +15,7 @@
 #include "util/hashmap.h"
 #include "util/header.h"
 #include "util/pmu.h"
+#include <perf/cpumap.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/zalloc.h>
@@ -456,8 +456,8 @@ double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
                       bool compute_ids __maybe_unused, const char *test_id)
 {
        double ret;
-       struct perf_pmu *pmu = perf_pmus__find_core_pmu();
-       char *cpuid = perf_pmu__getcpuid(pmu);
+       struct perf_cpu cpu = {-1};
+       char *cpuid = get_cpuid_allow_env_override(cpu);
 
        if (!cpuid)
                return NAN;
index ecf08b458b056bd58688cfef95585d2bb0088aa3..3451e542b69a8c603a8a39d02c5afa08ad5cd61e 100644 (file)
@@ -819,11 +819,31 @@ static int write_group_desc(struct feat_fd *ff,
  * Each architecture should provide a more precise id string that
  * can be use to match the architecture's "mapfile".
  */
-char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
+char * __weak get_cpuid_str(struct perf_cpu cpu __maybe_unused)
 {
        return NULL;
 }
 
+char *get_cpuid_allow_env_override(struct perf_cpu cpu)
+{
+       char *cpuid;
+       static bool printed;
+
+       cpuid = getenv("PERF_CPUID");
+       if (cpuid)
+               cpuid = strdup(cpuid);
+       if (!cpuid)
+               cpuid = get_cpuid_str(cpu);
+       if (!cpuid)
+               return NULL;
+
+       if (!printed) {
+               pr_debug("Using CPUID %s\n", cpuid);
+               printed = true;
+       }
+       return cpuid;
+}
+
 /* Return zero when the cpuid from the mapfile.csv matches the
  * cpuid string generated on this platform.
  * Otherwise return non-zero.
index 3bb768455a60b65d14a3c97ab4d82016f28bb349..5201af6305f4bf8ee10e6fbad15f82ebdd6fd1b6 100644 (file)
 #include <linux/bitmap.h>
 #include <linux/types.h>
 #include "env.h"
-#include "pmu.h"
 #include <perf/cpumap.h>
 
+struct evlist;
+union perf_event;
+struct perf_header;
+struct perf_session;
+struct perf_tool;
+
 enum {
        HEADER_RESERVED         = 0,    /* always cleared */
        HEADER_FIRST_FEATURE    = 1,
@@ -92,8 +97,6 @@ struct perf_pipe_file_header {
        u64                             size;
 };
 
-struct perf_header;
-
 int perf_file_header__read(struct perf_file_header *header,
                           struct perf_header *ph, int fd);
 
@@ -125,11 +128,6 @@ struct perf_header_feature_ops {
        bool       synthesize;
 };
 
-struct evlist;
-struct perf_session;
-struct perf_tool;
-union perf_event;
-
 extern const char perf_version_string[];
 
 int perf_session__read_header(struct perf_session *session);
@@ -204,6 +202,9 @@ int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp);
  */
 int get_cpuid(char *buffer, size_t sz, struct perf_cpu cpu);
 
-char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused);
+char *get_cpuid_str(struct perf_cpu cpu);
+
+char *get_cpuid_allow_env_override(struct perf_cpu cpu);
+
 int strcmp_cpuid_str(const char *s1, const char *s2);
 #endif /* __PERF_HEADER_H */
index a02df2c80f421a0504e09d18852d567f18aa603c..13067b9901a63d0ffa7366b87b957cecf50fafba 100644 (file)
@@ -819,26 +819,6 @@ static int is_sysfs_pmu_core(const char *name)
        return file_available(path);
 }
 
-char *perf_pmu__getcpuid(struct perf_pmu *pmu)
-{
-       char *cpuid;
-       static bool printed;
-
-       cpuid = getenv("PERF_CPUID");
-       if (cpuid)
-               cpuid = strdup(cpuid);
-       if (!cpuid)
-               cpuid = get_cpuid_str(pmu);
-       if (!cpuid)
-               return NULL;
-
-       if (!printed) {
-               pr_debug("Using CPUID %s\n", cpuid);
-               printed = true;
-       }
-       return cpuid;
-}
-
 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
 {
        return perf_pmu__find_metrics_table(NULL);
index d511bf7cc9d0637f8543c5617818fa3562564494..bd45d4af416021097506c17068ca49914c054663 100644 (file)
@@ -262,7 +262,6 @@ void perf_pmu__arch_init(struct perf_pmu *pmu);
 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu,
                               const struct pmu_events_table *table);
 
-char *perf_pmu__getcpuid(struct perf_pmu *pmu);
 const struct pmu_metrics_table *pmu_metrics_table__find(void);
 bool pmu_uncore_identifier_match(const char *compat, const char *id);