git: c5400dc5421c - main - libpmc: Strip leading zeros from the cpuid model
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 10 Nov 2026 15:59:56 UTC
The branch main has been updated by mhorne:
URL: https://cgit.FreeBSD.org/src/commit/?id=c5400dc5421cce1be7152f401dc09725036d0963
commit c5400dc5421cce1be7152f401dc09725036d0963
Author: Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-10 15:50:57 +0000
Commit: Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-10 15:59:54 +0000
libpmc: Strip leading zeros from the cpuid model
Upstream's mapfile.csv lists models without leading zeros, so entries
for models below 10h had to be fixed up by hand on every import. Strip
them before matching and use the entries as imported.
Add tests to validate the functionality.
Reviewed by: mhorne, Ali Mashtizadeh <ali@mashtizadeh.com>
Sponsored by: Advanced Micro Devices, Inc.
MFC after: 1 week
Signed-off-by: Osvaldo Janeri Filho <ojanerif@amd.com>
Differential Revision: https://reviews.freebsd.org/D60790
---
lib/libpmc/libpmc_pmu_util.c | 30 ++++++++++++
lib/libpmc/libpmcinternal.h | 1 +
lib/libpmc/pmu-events/arch/x86/mapfile.csv | 8 +--
tests/sys/pmc/pmc_mapfile_test.c | 79 ++++++++++++++++++++++++++++--
4 files changed, 111 insertions(+), 7 deletions(-)
diff --git a/lib/libpmc/libpmc_pmu_util.c b/lib/libpmc/libpmc_pmu_util.c
index 6472dd8311de..53d9d8766add 100644
--- a/lib/libpmc/libpmc_pmu_util.c
+++ b/lib/libpmc/libpmc_pmu_util.c
@@ -49,6 +49,8 @@
#include <pmclog.h>
#include <assert.h>
#include <libpmcstat.h>
+
+#include "libpmcinternal.h"
#include "pmu-events/pmu-events.h"
struct pmu_alias {
@@ -251,6 +253,33 @@ struct pmu_event_desc {
uint8_t ped_pebs;
};
+/*
+ * hwpmc zero-pads the model field of kern.hwpmc.cpuid for AMD/Hygon x86 CPUs
+ * -- see the "%02X" in the snprintf() calls in hwpmc_amd.c -- so e.g. Zen 5
+ * model 2h is reported as "AuthenticAMD-26-02-<stepping>".
+ * pmu-events/arch/x86/mapfile.csv lists those low models without the leading
+ * zero, so it has to be stripped before matching.
+ *
+ * This is scoped tightly to the "<vendor>-<family>-<model>[-<stepping>]"
+ * shape that only AuthenticAMD/HygonGenuine strings use; any other cpuid
+ * string can never reach the strip below and comes back unchanged. Notably,
+ * arm64's bare "0x<MIDR>" (hwpmc_arm64.c) and powerpc's bare "%08x" PVR
+ * (hwpmc_powerpc.c).
+ */
+void
+_pmc_cpuid_strip_model_zero(char *buf)
+{
+ char *p;
+
+ if (strncmp(buf, "AuthenticAMD-", sizeof("AuthenticAMD-") - 1) != 0 &&
+ strncmp(buf, "HygonGenuine-", sizeof("HygonGenuine-") - 1) != 0)
+ return;
+ if ((p = strchr(buf, '-')) == NULL || (p = strchr(p + 1, '-')) == NULL)
+ return;
+ while (p[1] == '0' && p[2] != '-' && p[2] != '\0')
+ memmove(p + 1, p + 2, strlen(p + 1));
+}
+
static const struct pmu_events_map *
pmu_events_map_get(const char *cpuid)
{
@@ -268,6 +297,7 @@ pmu_events_map_get(const char *cpuid)
(void *)NULL, 0) == -1)
return (NULL);
}
+ _pmc_cpuid_strip_model_zero(buf);
for (pme = pmu_events_map; pme->cpuid != NULL; pme++) {
if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) {
printf("regex '%s' failed to compile, ignoring\n",
diff --git a/lib/libpmc/libpmcinternal.h b/lib/libpmc/libpmcinternal.h
index 960843b3aea8..7de9d540027c 100644
--- a/lib/libpmc/libpmcinternal.h
+++ b/lib/libpmc/libpmcinternal.h
@@ -33,5 +33,6 @@
* Prototypes.
*/
const char *_pmc_name_of_event(enum pmc_event _ev, enum pmc_cputype _cpu);
+void _pmc_cpuid_strip_model_zero(char *_buf);
#endif /* LIBPMC_INTERNAL_H */
diff --git a/lib/libpmc/pmu-events/arch/x86/mapfile.csv b/lib/libpmc/pmu-events/arch/x86/mapfile.csv
index 05480113bd1c..0b1a8b38dd28 100644
--- a/lib/libpmc/pmu-events/arch/x86/mapfile.csv
+++ b/lib/libpmc/pmu-events/arch/x86/mapfile.csv
@@ -55,10 +55,10 @@ GenuineIntel-6-BE,v1,alderlaken,core
GenuineIntel-6-AA,v1,meteorlake,core
GenuineIntel-6-AC,v1,meteorlake,core
GenuineIntel-6-B5,v1,meteorlake,core
-AuthenticAMD-23-[012][0-9A-F],v2,amdzen1,core
+AuthenticAMD-23-([12][0-9A-F]|[0-9A-F]),v2,amdzen1,core
AuthenticAMD-23-[[:xdigit:]]+,v1,amdzen2,core
-AuthenticAMD-25-[0245][[:xdigit:]],v1,amdzen3,core
+AuthenticAMD-25-([245][[:xdigit:]]|[[:xdigit:]]),v1,amdzen3,core
AuthenticAMD-25-[[:xdigit:]]+,v1,amdzen4,core
-AuthenticAMD-26-[012467][[:xdigit:]],v1,amdzen5,core
+AuthenticAMD-26-([12467][[:xdigit:]]|[[:xdigit:]]),v1,amdzen5,core
AuthenticAMD-26-[[:xdigit:]]+,v1,amdzen6,core
-HygonGenuine-24-00,v2,amdzen1,core
+HygonGenuine-24-0,v2,amdzen1,core
diff --git a/tests/sys/pmc/pmc_mapfile_test.c b/tests/sys/pmc/pmc_mapfile_test.c
index 91a9d6e84bf7..0474919903a1 100644
--- a/tests/sys/pmc/pmc_mapfile_test.c
+++ b/tests/sys/pmc/pmc_mapfile_test.c
@@ -9,16 +9,33 @@
/*
* libpmc picks its pmu-events table by matching kern.hwpmc.cpuid against
* mapfile.csv. hwpmc prints the model with two digits, so Turin (Family
- * 1Ah, model 02h) is "AuthenticAMD-26-02-<stepping>". For Zen 5 and Zen 6
- * cpuid strings, look up an event that only one of amdzen5 and amdzen6 has.
- * No PMC hardware is needed.
+ * 1Ah, model 02h) is "AuthenticAMD-26-02-<stepping>". For each cpuid
+ * string, look up an event that only the expected table has. No PMC
+ * hardware is needed.
*/
#include <sys/param.h>
+#include <string.h>
+
#include <atf-c.h>
#include <pmc.h>
+/*
+ * _pmc_cpuid_strip_model_zero() is an internal libpmc interface that
+ * pmu_events_map_get() uses to undo hwpmc's zero-padding of the AMD/Hygon
+ * model field before matching it against mapfile.csv.
+ *
+ * It is not part of the installed pmc.h, so declare it here to exercise it
+ * directly: this is the only way to check, byte for byte, that cpuid strings
+ * outside its AMD/Hygon vendor-prefixed shape come back completely unmodified.
+ */
+void _pmc_cpuid_strip_model_zero(char *);
+
+#define ZEN1_ONLY_EVENT "bp_l1_tlb_miss_l2_miss"
+#define ZEN2_ONLY_EVENT "bp_l1_tlb_miss_l2_tlb_miss"
+#define ZEN3_ONLY_EVENT "bp_l1_btb_correct"
+#define ZEN4_ONLY_EVENT "bp_l1_tlb_fetch_hit.all"
#define ZEN5_ONLY_EVENT "bp_l2_btb_correct"
#define ZEN6_ONLY_EVENT "bp_fe_redir.all"
@@ -78,10 +95,66 @@ ATF_TC_BODY(zen6_models, tc)
"amdzen6");
}
+ATF_TC(low_models);
+ATF_TC_HEAD(low_models, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Models below 10h, with or without the leading zero");
+}
+ATF_TC_BODY(low_models, tc)
+{
+ check_table("AuthenticAMD-23-01-0", ZEN1_ONLY_EVENT, ZEN2_ONLY_EVENT,
+ "amdzen1");
+ check_table("HygonGenuine-24-00-0", ZEN1_ONLY_EVENT, ZEN2_ONLY_EVENT,
+ "amdzen1");
+ check_table("AuthenticAMD-25-01-0", ZEN3_ONLY_EVENT, ZEN4_ONLY_EVENT,
+ "amdzen3");
+ check_table("AuthenticAMD-26-2-0", ZEN5_ONLY_EVENT, ZEN6_ONLY_EVENT,
+ "amdzen5");
+}
+
+/*
+ * Only AuthenticAMD/HygonGenuine cpuid strings use the
+ * "<vendor>-<family>-<model>[-<stepping>]" shape that needs the model's
+ * leading zero stripped. Every other cpuid shape must come back unchanged, in
+ * particular arm64's bare "0x<MIDR>" and powerpc's bare "%08x" PVR. Also,
+ * check a string that merely happens to contain two dashes and a zero, to
+ * confirm the vendor-prefix check (not "has a dash") is what actually gates
+ * the strip.
+ */
+static const char *const non_amd_cpuids[] = {
+ "0x00000000410fd020", /* arm64 MIDR, cortex-a34 */
+ "0x00000000410fd030", /* arm64 MIDR, cortex-a53 */
+ "00390202", /* powerpc PVR */
+ "NotAVendor-1-00-1", /* two dashes, but not AuthenticAMD/HygonGenuine */
+};
+
+ATF_TC(non_amd_cpuid_unchanged);
+ATF_TC_HEAD(non_amd_cpuid_unchanged, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Non-AMD/Hygon cpuid strings are never touched by the "
+ "model zero-strip, e.g. arm64's bare 0x<MIDR>");
+}
+ATF_TC_BODY(non_amd_cpuid_unchanged, tc)
+{
+ char buf[64];
+ size_t i;
+
+ for (i = 0; i < nitems(non_amd_cpuids); i++) {
+ strlcpy(buf, non_amd_cpuids[i], sizeof(buf));
+ _pmc_cpuid_strip_model_zero(buf);
+ ATF_CHECK_MSG(strcmp(buf, non_amd_cpuids[i]) == 0,
+ "%s was modified to %s", non_amd_cpuids[i], buf);
+ }
+}
+
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, zen5_models);
ATF_TP_ADD_TC(tp, zen6_models);
+ ATF_TP_ADD_TC(tp, low_models);
+ ATF_TP_ADD_TC(tp, non_amd_cpuid_unchanged);
return (atf_no_error());
}