Linux Perf Users
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>, Jiri Olsa <jolsa@kernel.org>,
	 Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	 James Clark <james.clark@linaro.org>
Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 14/30] perf header: Support CPUID matching across all architectures
Date: Fri, 13 Nov 2026 11:53:19 -0800	[thread overview]
Message-ID: <20261113195335.2865725-15-irogers@google.com> (raw)
In-Reply-To: <20261113195335.2865725-1-irogers@google.com>

strcmp_cpuid_str() had architecture specific overrides of a __weak
regular expression matching implementation in arch/x86/util/header.c
and arch/arm64/util/header.c, so a CPUID could only be compared using
the rules of the architecture perf was built for. In addition, the
HEADER_CPUID strings written by get_cpuid() on x86 ("vendor,family,
model,step" in decimal) and PowerPC ("pvr_ver,pvr_rev" in decimal)
differ from the get_cpuid_str() formats ("vendor-family-model-stepping"
in hex and "0x%.8lx") matched against the pmu-events mapfile.csv
tables. As a result, finding the core events or metrics table for the
CPUID of a perf_env, such as from a perf.data file recorded on a
different architecture, failed.

Move the architecture specific comparisons into util/header.c and
select them using the ELF machine of the CPUIDs: EM_NONE for the test
and common tables compares exactly, arm64 compares MIDR part, variant
and revision fields, x86 matches with an optional stepping and other
architectures match a regular expression. Convert x86 and PowerPC
HEADER_CPUID strings before matching.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/arch/arm64/util/header.c |  48 ---------
 tools/perf/arch/x86/util/header.c   |  64 -----------
 tools/perf/util/header.c            | 161 +++++++++++++++++++++++++++-
 3 files changed, 156 insertions(+), 117 deletions(-)

diff --git a/tools/perf/arch/arm64/util/header.c b/tools/perf/arch/arm64/util/header.c
index ee28add836b5..181836613087 100644
--- a/tools/perf/arch/arm64/util/header.c
+++ b/tools/perf/arch/arm64/util/header.c
@@ -1,5 +1,3 @@
-#include <linux/bits.h>
-#include <linux/bitfield.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <perf/cpumap.h>
@@ -10,8 +8,6 @@
 
 #define MIDR "/regs/identification/midr_el1"
 #define MIDR_SIZE 19
-#define MIDR_REVISION_MASK      GENMASK(3, 0)
-#define MIDR_VARIANT_MASK	GENMASK(23, 20)
 
 static int _get_cpuid(char *buf, size_t sz, struct perf_cpu cpu)
 {
@@ -79,47 +75,3 @@ char *get_cpuid_str(struct perf_cpu cpu)
 
 	return buf;
 }
-
-/*
- * Return 0 if idstr is a higher or equal to version of the same part as
- * mapcpuid. Therefore, if mapcpuid has 0 for revision and variant then any
- * version of idstr will match as long as it's the same CPU type.
- *
- * Return 1 if the CPU type is different or the version of idstr is lower.
- */
-int strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char *mapcpuid, const char *idstr)
-{
-	u64 map_id = strtoull(mapcpuid, NULL, 16);
-	char map_id_variant = FIELD_GET(MIDR_VARIANT_MASK, map_id);
-	char map_id_revision = FIELD_GET(MIDR_REVISION_MASK, map_id);
-	u64 id = strtoull(idstr, NULL, 16);
-	char id_variant = FIELD_GET(MIDR_VARIANT_MASK, id);
-	char id_revision = FIELD_GET(MIDR_REVISION_MASK, id);
-	u64 id_fields = ~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK);
-
-	/* Compare without version first */
-	if ((map_id & id_fields) != (id & id_fields))
-		return 1;
-
-	/*
-	 * ID matches, now compare version.
-	 *
-	 * Arm revisions (like r0p0) are compared here like two digit semver
-	 * values eg. 1.3 < 2.0 < 2.1 < 2.2.
-	 *
-	 *  r = high value = 'Variant' field in MIDR
-	 *  p = low value  = 'Revision' field in MIDR
-	 *
-	 */
-	if (id_variant > map_id_variant)
-		return 0;
-
-	if (id_variant == map_id_variant && id_revision >= map_id_revision)
-		return 0;
-
-	/*
-	 * variant is less than mapfile variant or variants are the same but
-	 * the revision doesn't match. Return no match.
-	 */
-	return 1;
-}
diff --git a/tools/perf/arch/x86/util/header.c b/tools/perf/arch/x86/util/header.c
index b65856f3b846..5203e5cd151c 100644
--- a/tools/perf/arch/x86/util/header.c
+++ b/tools/perf/arch/x86/util/header.c
@@ -5,7 +5,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <regex.h>
 
 #include "../../../util/debug.h"
 #include "../../../util/header.h"
@@ -73,66 +72,3 @@ char *get_cpuid_str(struct perf_cpu cpu __maybe_unused)
 	}
 	return buf;
 }
-
-/* Full CPUID format for x86 is vendor-family-model-stepping */
-static bool is_full_cpuid(const char *id)
-{
-	const char *tmp = id;
-	int count = 0;
-
-	while ((tmp = strchr(tmp, '-')) != NULL) {
-		count++;
-		tmp++;
-	}
-
-	if (count == 3)
-		return true;
-
-	return false;
-}
-
-int strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char *mapcpuid, const char *id)
-{
-	regex_t re;
-	regmatch_t pmatch[1];
-	int match;
-	bool full_mapcpuid = is_full_cpuid(mapcpuid);
-	bool full_cpuid = is_full_cpuid(id);
-
-	/*
-	 * Full CPUID format is required to identify a platform.
-	 * Error out if the cpuid string is incomplete.
-	 */
-	if (full_mapcpuid && !full_cpuid) {
-		pr_info("Invalid CPUID %s. Full CPUID is required, "
-			"vendor-family-model-stepping\n", id);
-		return 1;
-	}
-
-	if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
-		/* Warn unable to generate match particular string. */
-		pr_info("Invalid regular expression %s\n", mapcpuid);
-		return 1;
-	}
-
-	match = !regexec(&re, id, 1, pmatch, 0);
-	regfree(&re);
-	if (match) {
-		size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
-		size_t cpuid_len;
-
-		/* If the full CPUID format isn't required,
-		 * ignoring the stepping.
-		 */
-		if (!full_mapcpuid && full_cpuid)
-			cpuid_len = strrchr(id, '-') - id;
-		else
-			cpuid_len = strlen(id);
-
-		/* Verify the entire string matched. */
-		if (match_len == cpuid_len)
-			return 0;
-	}
-
-	return 1;
-}
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 38525b17f26d..a442f2a904a9 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -11,7 +11,10 @@
 
 #include <byteswap.h>
 #include <dirent.h>
+#include <elf.h>
+#include <linux/bitfield.h>
 #include <linux/bitops.h>
+#include <linux/bits.h>
 #include <linux/compiler.h>
 #include <linux/ctype.h>
 #include <linux/kernel.h>
@@ -960,12 +963,70 @@ char *get_cpuid_allow_env_override(struct perf_cpu cpu)
 	return cpuid;
 }
 
-/* Return zero when the cpuid from the mapfile.csv matches the
- * cpuid string generated on this platform.
- * Otherwise return non-zero.
+#define MIDR_REVISION_MASK	GENMASK(3, 0)
+#define MIDR_VARIANT_MASK	GENMASK(23, 20)
+
+/* Is @id a full x86 vendor-family-model-stepping CPUID, with 3 hyphens? */
+static bool is_full_x86_cpuid(const char *id)
+{
+	const char *tmp = id;
+	int count = 0;
+
+	while ((tmp = strchr(tmp, '-')) != NULL) {
+		count++;
+		tmp++;
+	}
+
+	return count == 3;
+}
+
+/*
+ * Compare arm64 MIDR values. Return 0 if @idstr is the same CPU type as
+ * @mapcpuid and its version, the Variant[23:20] and Revision[3:0] fields, is
+ * greater than or equal to that of @mapcpuid. Therefore, if @mapcpuid has 0 for
+ * the revision and variant then any version of @idstr will match as long as
+ * it's the same CPU type.
+ *
+ * Return 1 if the CPU type is different or the version of @idstr is lower.
  */
-int __weak strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char *mapcpuid,
-			    const char *cpuid)
+static int strcmp_arm64_cpuid_str(const char *mapcpuid, const char *idstr)
+{
+	u64 map_id = strtoull(mapcpuid, NULL, 16);
+	char map_id_variant = FIELD_GET(MIDR_VARIANT_MASK, map_id);
+	char map_id_revision = FIELD_GET(MIDR_REVISION_MASK, map_id);
+	u64 id = strtoull(idstr, NULL, 16);
+	char id_variant = FIELD_GET(MIDR_VARIANT_MASK, id);
+	char id_revision = FIELD_GET(MIDR_REVISION_MASK, id);
+	u64 id_fields = ~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK);
+
+	/* Compare without version first. */
+	if ((map_id & id_fields) != (id & id_fields))
+		return 1;
+
+	/*
+	 * ID matches, now compare version.
+	 *
+	 * Arm revisions (like r0p0) are compared here like two digit semver
+	 * values eg. 1.3 < 2.0 < 2.1 < 2.2.
+	 *
+	 *  r = high value = 'Variant' field in MIDR
+	 *  p = low value  = 'Revision' field in MIDR
+	 */
+	if (id_variant > map_id_variant)
+		return 0;
+
+	if (id_variant == map_id_variant && id_revision >= map_id_revision)
+		return 0;
+
+	/*
+	 * Variant is less than mapfile variant or variants are the same but
+	 * the revision doesn't match. Return no match.
+	 */
+	return 1;
+}
+
+/* Return 0 if all of @cpuid matches the extended regular expression @mapcpuid. */
+static int strcmp_regex_cpuid_str(const char *mapcpuid, const char *cpuid)
 {
 	regex_t re;
 	regmatch_t pmatch[1];
@@ -989,6 +1050,96 @@ int __weak strcmp_cpuid_str(uint16_t e_machine __maybe_unused, const char *mapcp
 	return 1;
 }
 
+/*
+ * Compare x86 CPUIDs of the form vendor-family-model[-stepping]. A full
+ * @mapcpuid is stepping specific and requires a full @cpuid, otherwise the
+ * stepping of a full @cpuid is ignored.
+ */
+static int strcmp_x86_cpuid_str(const char *mapcpuid, const char *cpuid)
+{
+	unsigned int family, model, step;
+	char norm_buf[128], buf[128];
+	char vendor[32], extra;
+	bool full_mapcpuid, full_cpuid;
+
+	/* x86 get_cpuid() writes "vendor,family,model,step" in decimal. */
+	if (sscanf(cpuid, "%31[^,],%u,%u,%u%c", vendor, &family, &model, &step, &extra) == 4) {
+		snprintf(norm_buf, sizeof(norm_buf), "%s-%u-%X-%X", vendor, family, model, step);
+		cpuid = norm_buf;
+	}
+
+	full_mapcpuid = is_full_x86_cpuid(mapcpuid);
+	full_cpuid = is_full_x86_cpuid(cpuid);
+
+	/*
+	 * Full CPUID format is required to identify a stepping specific
+	 * platform. Error out if the cpuid string is incomplete.
+	 */
+	if (full_mapcpuid && !full_cpuid) {
+		pr_info("Invalid CPUID %s. Full CPUID is required, vendor-family-model-stepping\n",
+			cpuid);
+		return 1;
+	}
+
+	/* If the full CPUID format isn't required, ignore the stepping. */
+	if (!full_mapcpuid && full_cpuid) {
+		int len = strrchr(cpuid, '-') - cpuid;
+
+		snprintf(buf, sizeof(buf), "%.*s", len, cpuid);
+		cpuid = buf;
+	}
+	return strcmp_regex_cpuid_str(mapcpuid, cpuid);
+}
+
+/* Compare PowerPC PVR CPUIDs, matching @cpuid against the regex @mapcpuid. */
+static int strcmp_powerpc_cpuid_str(const char *mapcpuid, const char *cpuid)
+{
+	unsigned long pvr_ver, pvr_rev;
+	char norm_buf[32], extra;
+
+	/* PowerPC get_cpuid() writes "pvr_ver,pvr_rev" in decimal. */
+	if (sscanf(cpuid, "%lu,%lu%c", &pvr_ver, &pvr_rev, &extra) == 2) {
+		unsigned long pvr = ((pvr_ver & 0xffff) << 16) | (pvr_rev & 0xffff);
+
+		snprintf(norm_buf, sizeof(norm_buf), "0x%.8lx", pvr);
+		cpuid = norm_buf;
+	}
+	return strcmp_regex_cpuid_str(mapcpuid, cpuid);
+}
+
+/**
+ * strcmp_cpuid_str - Compare a pmu-events mapfile.csv CPUID with a CPUID.
+ * @e_machine: The ELF machine of the CPUIDs' architecture.
+ * @mapcpuid: The CPUID from the mapfile.csv, a regular expression for most
+ *            architectures.
+ * @cpuid: The CPUID from get_cpuid_str() or HEADER_CPUID.
+ *
+ * The test and common tables use EM_NONE and are compared exactly, arm64
+ * compares MIDR values and other architectures match @cpuid against the
+ * regular expression @mapcpuid. HEADER_CPUID values written by get_cpuid() on
+ * x86 and PowerPC differ from the get_cpuid_str() format of the mapfile.csv
+ * and are converted before matching.
+ *
+ * Return: 0 if the CPUIDs match, otherwise 1.
+ */
+int strcmp_cpuid_str(uint16_t e_machine, const char *mapcpuid, const char *cpuid)
+{
+	switch (e_machine) {
+	case EM_NONE:
+		return strcmp(mapcpuid, cpuid) ? 1 : 0;
+	case EM_AARCH64:
+		return strcmp_arm64_cpuid_str(mapcpuid, cpuid);
+	case EM_386:
+	case EM_X86_64:
+		return strcmp_x86_cpuid_str(mapcpuid, cpuid);
+	case EM_PPC:
+	case EM_PPC64:
+		return strcmp_powerpc_cpuid_str(mapcpuid, cpuid);
+	default:
+		return strcmp_regex_cpuid_str(mapcpuid, cpuid);
+	}
+}
+
 /*
  * default get_cpuid(): nothing gets recorded
  * actual implementation must be in arch/$(SRCARCH)/util/header.c
-- 
2.56.1.703.gf848ed807c-goog


  parent reply	other threads:[~2026-11-13 19:54 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 19:53 [PATCH v2 00/30] perf: Per-env PMUs, metric event sharing, and recorded event metrics Ian Rogers
2026-11-13 19:53 ` [PATCH v2 01/30] perf pmus: Don't skip core PMUs as duplicates Ian Rogers
2026-11-13 20:04   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 02/30] perf script: Fix metrics computed from samples Ian Rogers
2026-11-13 20:05   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 03/30] perf script: Don't drop metric samples for CPUs missing from the evsel Ian Rogers
2026-11-13 20:02   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 04/30] perf metricgroup: Don't prefer tool events for displaying a metric Ian Rogers
2026-11-13 19:59   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 05/30] perf env: Make lazy perf_env initialization thread-safe Ian Rogers
2026-11-13 20:05   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 06/30] perf env: Add host_env() singleton Ian Rogers
2026-11-13 20:47   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 07/30] perf pmu: Add from_env parameter to perf_pmu__init() Ian Rogers
2026-11-13 20:10   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 08/30] perf pmus: Introduce struct perf_pmus and perf_env__pmus() Ian Rogers
2026-11-13 20:36   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 09/30] perf pmus: Pass struct perf_pmus to find helpers Ian Rogers
2026-11-13 20:11   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 10/30] perf pmus: Pass struct perf_pmus to scan helpers Ian Rogers
2026-11-13 20:06   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 11/30] perf pmu-events: Add e_machine to struct pmu_events_map Ian Rogers
2026-11-13 20:12   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 12/30] perf pmu-events: Pass e_machine to find_core_*_table() Ian Rogers
2026-11-13 20:05   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 13/30] perf header: Pass e_machine to strcmp_cpuid_str() Ian Rogers
2026-11-13 20:01   ` sashiko-bot
2026-11-13 19:53 ` Ian Rogers [this message]
2026-11-13 20:05   ` [PATCH v2 14/30] perf header: Support CPUID matching across all architectures sashiko-bot
2026-11-13 19:53 ` [PATCH v2 15/30] perf test: Make the CPUID matching test architecture independent Ian Rogers
2026-11-13 20:02   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 16/30] perf test: Add CPUID matching tests for other architectures Ian Rogers
2026-11-13 20:03   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 17/30] perf dwarf-regs: Add e_machine__same_arch() Ian Rogers
2026-11-13 20:00   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 18/30] perf metricgroup: Pass struct perf_pmus to metricgroup__for_each_metric() Ian Rogers
2026-11-13 20:37   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 19/30] perf metricgroup: Move matching of metrics to recorded events from perf script Ian Rogers
2026-11-13 20:59   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 20/30] perf parse-events: Decode metric-id terms when adding events Ian Rogers
2026-11-13 20:33   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 21/30] perf evsel: Generalize metric_id to a set of metric_ids Ian Rogers
2026-11-13 20:31   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 22/30] perf pmu: Add perf_pmu__for_each_name_from_config() Ian Rogers
2026-11-13 20:13   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 23/30] perf metricgroup: Share metric events by their counter encoding Ian Rogers
2026-11-13 20:59   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 24/30] perf stat: Share events between metrics and command line events Ian Rogers
2026-11-13 20:14   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 25/30] perf pmus: Add perf_pmus__read_from_env() Ian Rogers
2026-11-13 21:05   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 26/30] perf metricgroup: Improve matching of metrics to recorded events Ian Rogers
2026-11-13 20:37   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 27/30] perf metricgroup: Add metricgroup__add_recorded_metrics() Ian Rogers
2026-11-13 20:42   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 28/30] perf stat report: Display metrics for recorded events Ian Rogers
2026-11-13 20:44   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 29/30] perf script: Display metrics for perf stat record data Ian Rogers
2026-11-13 20:47   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 30/30] perf test: Add stat record metrics test Ian Rogers
2026-11-13 20:14   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261113195335.2865725-15-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox