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

perf header: Pass a perf_cpu rather than a PMU to get_cpuid_str

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>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
494c403f 7463ee17

+57 -67
+2 -12
tools/perf/arch/arm64/util/arm-spe.c
··· 23 23 #include "../../../util/debug.h" 24 24 #include "../../../util/auxtrace.h" 25 25 #include "../../../util/record.h" 26 + #include "../../../util/header.h" 26 27 #include "../../../util/arm-spe.h" 27 28 #include <tools/libc_compat.h> // reallocarray 28 29 ··· 86 85 struct arm_spe_recording *sper = 87 86 container_of(itr, struct arm_spe_recording, itr); 88 87 struct perf_pmu *pmu = NULL; 89 - struct perf_pmu tmp_pmu; 90 - char cpu_id_str[16]; 91 88 char *cpuid = NULL; 92 89 u64 val; 93 90 94 - snprintf(cpu_id_str, sizeof(cpu_id_str), "%d", cpu.cpu); 95 - tmp_pmu.cpus = perf_cpu_map__new(cpu_id_str); 96 - if (!tmp_pmu.cpus) 97 - return -ENOMEM; 98 - 99 91 /* Read CPU MIDR */ 100 - cpuid = perf_pmu__getcpuid(&tmp_pmu); 101 - 102 - /* The CPU map will not be used anymore, release it */ 103 - perf_cpu_map__put(tmp_pmu.cpus); 104 - 92 + cpuid = get_cpuid_allow_env_override(cpu); 105 93 if (!cpuid) 106 94 return -ENOMEM; 107 95 val = strtol(cpuid, NULL, 16);
+4 -8
tools/perf/arch/arm64/util/header.c
··· 62 62 return EINVAL; 63 63 } 64 64 65 - char *get_cpuid_str(struct perf_pmu *pmu) 65 + char *get_cpuid_str(struct perf_cpu cpu) 66 66 { 67 - char *buf = NULL; 67 + char *buf = malloc(MIDR_SIZE); 68 68 int res; 69 69 70 - if (!pmu || !pmu->cpus) 71 - return NULL; 72 - 73 - buf = malloc(MIDR_SIZE); 74 70 if (!buf) 75 71 return NULL; 76 72 77 73 /* read midr from list of cpus mapped to this pmu */ 78 - res = get_cpuid(buf, MIDR_SIZE, perf_cpu_map__min(pmu->cpus)); 74 + res = get_cpuid(buf, MIDR_SIZE, cpu); 79 75 if (res) { 80 - pr_err("failed to get cpuid string for PMU %s\n", pmu->name); 76 + pr_err("failed to get cpuid string for CPU %d\n", cpu.cpu); 81 77 free(buf); 82 78 buf = NULL; 83 79 }
+1 -1
tools/perf/arch/loongarch/util/header.c
··· 90 90 return ret; 91 91 } 92 92 93 - char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused) 93 + char *get_cpuid_str(struct perf_cpu cpu __maybe_unused) 94 94 { 95 95 return _get_cpuid(); 96 96 }
+1 -1
tools/perf/arch/powerpc/util/header.c
··· 42 42 } 43 43 44 44 char * 45 - get_cpuid_str(struct perf_pmu *pmu __maybe_unused) 45 + get_cpuid_str(struct perf_cpu cpu __maybe_unused) 46 46 { 47 47 char *bufp; 48 48 unsigned long pvr;
+1 -1
tools/perf/arch/riscv/util/header.c
··· 98 98 } 99 99 100 100 char * 101 - get_cpuid_str(struct perf_pmu *pmu __maybe_unused) 101 + get_cpuid_str(struct perf_cpu cpu __maybe_unused) 102 102 { 103 103 return _get_cpuid(); 104 104 }
+2 -2
tools/perf/arch/s390/util/header.c
··· 137 137 return (nbytes >= sz) ? ENOBUFS : 0; 138 138 } 139 139 140 - char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused) 140 + char *get_cpuid_str(struct perf_cpu cpu) 141 141 { 142 142 char *buf = malloc(128); 143 143 144 - if (buf && get_cpuid(buf, 128)) 144 + if (buf && get_cpuid(buf, 128, cpu)) 145 145 zfree(&buf); 146 146 return buf; 147 147 }
+1 -2
tools/perf/arch/x86/util/header.c
··· 63 63 return __get_cpuid(buffer, sz, "%s,%u,%u,%u$"); 64 64 } 65 65 66 - char * 67 - get_cpuid_str(struct perf_pmu *pmu __maybe_unused) 66 + char *get_cpuid_str(struct perf_cpu cpu __maybe_unused) 68 67 { 69 68 char *buf = malloc(128); 70 69
+4 -1
tools/perf/pmu-events/empty-pmu-events.c
··· 515 515 } last_map_search; 516 516 static bool has_last_result, has_last_map_search; 517 517 const struct pmu_events_map *map = NULL; 518 + struct perf_cpu cpu = {-1}; 518 519 char *cpuid = NULL; 519 520 size_t i; 520 521 521 522 if (has_last_result && last_result.pmu == pmu) 522 523 return last_result.map; 523 524 524 - cpuid = perf_pmu__getcpuid(pmu); 525 + if (pmu) 526 + cpu = perf_cpu_map__min(pmu->cpus); 527 + cpuid = get_cpuid_allow_env_override(cpu); 525 528 526 529 /* 527 530 * On some platforms which uses cpus map, cpuid can be NULL for
+4 -1
tools/perf/pmu-events/jevents.py
··· 1031 1031 } last_map_search; 1032 1032 static bool has_last_result, has_last_map_search; 1033 1033 const struct pmu_events_map *map = NULL; 1034 + struct perf_cpu cpu = {-1}; 1034 1035 char *cpuid = NULL; 1035 1036 size_t i; 1036 1037 1037 1038 if (has_last_result && last_result.pmu == pmu) 1038 1039 return last_result.map; 1039 1040 1040 - cpuid = perf_pmu__getcpuid(pmu); 1041 + if (pmu) 1042 + cpu = perf_cpu_map__min(pmu->cpus); 1043 + cpuid = get_cpuid_allow_env_override(cpu); 1041 1044 1042 1045 /* 1043 1046 * On some platforms which uses cpus map, cpuid can be NULL for
+3 -4
tools/perf/tests/expr.c
··· 4 4 #include "util/expr.h" 5 5 #include "util/hashmap.h" 6 6 #include "util/header.h" 7 - #include "util/pmu.h" 8 - #include "util/pmus.h" 9 7 #include "util/smt.h" 10 8 #include "tests.h" 9 + #include <perf/cpumap.h> 11 10 #include <math.h> 12 11 #include <stdlib.h> 13 12 #include <string.h> ··· 77 78 struct expr_parse_ctx *ctx; 78 79 bool is_intel = false; 79 80 char strcmp_cpuid_buf[256]; 80 - struct perf_pmu *pmu = perf_pmus__find_core_pmu(); 81 - char *cpuid = perf_pmu__getcpuid(pmu); 81 + struct perf_cpu cpu = {-1}; 82 + char *cpuid = get_cpuid_allow_env_override(cpu); 82 83 char *escaped_cpuid1, *escaped_cpuid2; 83 84 84 85 TEST_ASSERT_VAL("get_cpuid", cpuid);
+3 -3
tools/perf/util/expr.c
··· 8 8 #include "debug.h" 9 9 #include "evlist.h" 10 10 #include "expr.h" 11 - #include "pmu.h" 12 11 #include "smt.h" 13 12 #include "tool_pmu.h" 14 13 #include <util/expr-bison.h> ··· 15 16 #include "util/hashmap.h" 16 17 #include "util/header.h" 17 18 #include "util/pmu.h" 19 + #include <perf/cpumap.h> 18 20 #include <linux/err.h> 19 21 #include <linux/kernel.h> 20 22 #include <linux/zalloc.h> ··· 456 456 bool compute_ids __maybe_unused, const char *test_id) 457 457 { 458 458 double ret; 459 - struct perf_pmu *pmu = perf_pmus__find_core_pmu(); 460 - char *cpuid = perf_pmu__getcpuid(pmu); 459 + struct perf_cpu cpu = {-1}; 460 + char *cpuid = get_cpuid_allow_env_override(cpu); 461 461 462 462 if (!cpuid) 463 463 return NAN;
+21 -1
tools/perf/util/header.c
··· 819 819 * Each architecture should provide a more precise id string that 820 820 * can be use to match the architecture's "mapfile". 821 821 */ 822 - char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused) 822 + char * __weak get_cpuid_str(struct perf_cpu cpu __maybe_unused) 823 823 { 824 824 return NULL; 825 + } 826 + 827 + char *get_cpuid_allow_env_override(struct perf_cpu cpu) 828 + { 829 + char *cpuid; 830 + static bool printed; 831 + 832 + cpuid = getenv("PERF_CPUID"); 833 + if (cpuid) 834 + cpuid = strdup(cpuid); 835 + if (!cpuid) 836 + cpuid = get_cpuid_str(cpu); 837 + if (!cpuid) 838 + return NULL; 839 + 840 + if (!printed) { 841 + pr_debug("Using CPUID %s\n", cpuid); 842 + printed = true; 843 + } 844 + return cpuid; 825 845 } 826 846 827 847 /* Return zero when the cpuid from the mapfile.csv matches the
+10 -9
tools/perf/util/header.h
··· 10 10 #include <linux/bitmap.h> 11 11 #include <linux/types.h> 12 12 #include "env.h" 13 - #include "pmu.h" 14 13 #include <perf/cpumap.h> 14 + 15 + struct evlist; 16 + union perf_event; 17 + struct perf_header; 18 + struct perf_session; 19 + struct perf_tool; 15 20 16 21 enum { 17 22 HEADER_RESERVED = 0, /* always cleared */ ··· 97 92 u64 size; 98 93 }; 99 94 100 - struct perf_header; 101 - 102 95 int perf_file_header__read(struct perf_file_header *header, 103 96 struct perf_header *ph, int fd); 104 97 ··· 127 124 bool full_only; 128 125 bool synthesize; 129 126 }; 130 - 131 - struct evlist; 132 - struct perf_session; 133 - struct perf_tool; 134 - union perf_event; 135 127 136 128 extern const char perf_version_string[]; 137 129 ··· 202 204 */ 203 205 int get_cpuid(char *buffer, size_t sz, struct perf_cpu cpu); 204 206 205 - char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused); 207 + char *get_cpuid_str(struct perf_cpu cpu); 208 + 209 + char *get_cpuid_allow_env_override(struct perf_cpu cpu); 210 + 206 211 int strcmp_cpuid_str(const char *s1, const char *s2); 207 212 #endif /* __PERF_HEADER_H */
-20
tools/perf/util/pmu.c
··· 819 819 return file_available(path); 820 820 } 821 821 822 - char *perf_pmu__getcpuid(struct perf_pmu *pmu) 823 - { 824 - char *cpuid; 825 - static bool printed; 826 - 827 - cpuid = getenv("PERF_CPUID"); 828 - if (cpuid) 829 - cpuid = strdup(cpuid); 830 - if (!cpuid) 831 - cpuid = get_cpuid_str(pmu); 832 - if (!cpuid) 833 - return NULL; 834 - 835 - if (!printed) { 836 - pr_debug("Using CPUID %s\n", cpuid); 837 - printed = true; 838 - } 839 - return cpuid; 840 - } 841 - 842 822 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void) 843 823 { 844 824 return perf_pmu__find_metrics_table(NULL);
-1
tools/perf/util/pmu.h
··· 262 262 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, 263 263 const struct pmu_events_table *table); 264 264 265 - char *perf_pmu__getcpuid(struct perf_pmu *pmu); 266 265 const struct pmu_metrics_table *pmu_metrics_table__find(void); 267 266 bool pmu_uncore_identifier_match(const char *compat, const char *id); 268 267