git: 7c743e3ba6c1 - main - tests/sys/pmc/amd: add IBS, row allocation, PerfEvtSel and PerfMonV2 tests
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 17 Nov 2026 14:02:42 UTC
The branch main has been updated by mhorne:
URL: https://cgit.FreeBSD.org/src/commit/?id=7c743e3ba6c1efc4aca9c0bb59e952cbdb63c1c7
commit 7c743e3ba6c1efc4aca9c0bb59e952cbdb63c1c7
Author: Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-17 13:52:03 +0000
Commit: Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-17 14:02:32 +0000
tests/sys/pmc/amd: add IBS, row allocation, PerfEvtSel and PerfMonV2 tests
- pmc_ibs_test: the IBS class.
- pmc_ibs_fetch_op_test: Fetch and Op together (572cd669d6) and IBS
interrupt accounting (c97cea955f).
- pmcstat_ibs_test: pmcstat records and replays IBS samples.
- pmc_rowalloc_test: core row allocation.
- pmc_start_msr_test: PerfEvtSel.En and PerfCntrGlobalCtl after pmc_start().
- pmu_perfmonv2_test: the PerfMonV2 global-control path.
- amd_pmc_test_common.h: also match the AMD-<n> row names of D60100.
Reviewed by: mhorne
Sponsored by: Advanced Micro Devices, Inc.
Signed-off-by: Osvaldo Janeri Filho <ojanerif@amd.com>
Signed-off-by: Davi C.A <davi.chavesazevedo@amd.com>
Differential Revision: https://reviews.freebsd.org/D61063
---
tests/sys/pmc/amd/Makefile | 12 +
tests/sys/pmc/amd/amd_pmc_test_common.h | 22 +-
tests/sys/pmc/amd/pmc_ibs_fetch_op_test.c | 338 ++++++++++++++++++++
tests/sys/pmc/amd/pmc_ibs_test.c | 502 ++++++++++++++++++++++++++++++
tests/sys/pmc/amd/pmc_rowalloc_test.c | 286 +++++++++++++++++
tests/sys/pmc/amd/pmc_start_msr_test.c | 264 ++++++++++++++++
tests/sys/pmc/amd/pmcstat_ibs_test.sh | 64 ++++
tests/sys/pmc/amd/pmu_perfmonv2_test.c | 186 +++++++++++
8 files changed, 1669 insertions(+), 5 deletions(-)
diff --git a/tests/sys/pmc/amd/Makefile b/tests/sys/pmc/amd/Makefile
index 486cf61589ea..577517b1aa9e 100644
--- a/tests/sys/pmc/amd/Makefile
+++ b/tests/sys/pmc/amd/Makefile
@@ -1,16 +1,28 @@
TESTSDIR= ${TESTSBASE}/sys/pmc/amd
+ATF_TESTS_C+= pmc_ibs_fetch_op_test
+ATF_TESTS_C+= pmc_ibs_test
+ATF_TESTS_C+= pmc_rowalloc_test
+ATF_TESTS_C+= pmc_start_msr_test
ATF_TESTS_C+= pmu_detect_test
ATF_TESTS_C+= pmu_event_neg_test
ATF_TESTS_C+= pmu_event_test
ATF_TESTS_C+= pmu_mux_test
+ATF_TESTS_C+= pmu_perfmonv2_test
ATF_TESTS_C+= rapl_domains_test
ATF_TESTS_SH+= pmc_frontend_test
+ATF_TESTS_SH+= pmcstat_ibs_test
# These need the counters of cpu0 to themselves.
+TEST_METADATA.pmc_ibs_fetch_op_test+= is_exclusive="true"
+TEST_METADATA.pmc_ibs_test+= is_exclusive="true"
+TEST_METADATA.pmc_rowalloc_test+= is_exclusive="true"
+TEST_METADATA.pmc_start_msr_test+= is_exclusive="true"
TEST_METADATA.pmu_event_neg_test+= is_exclusive="true"
TEST_METADATA.pmu_event_test+= is_exclusive="true"
TEST_METADATA.pmu_mux_test+= is_exclusive="true"
+TEST_METADATA.pmu_perfmonv2_test+= is_exclusive="true"
+TEST_METADATA.pmcstat_ibs_test+= is_exclusive="true"
TEST_METADATA.rapl_domains_test+= is_exclusive="true"
LIBADD+= pmc
diff --git a/tests/sys/pmc/amd/amd_pmc_test_common.h b/tests/sys/pmc/amd/amd_pmc_test_common.h
index 543d5afe3a75..e2c82546ad4a 100644
--- a/tests/sys/pmc/amd/amd_pmc_test_common.h
+++ b/tests/sys/pmc/amd/amd_pmc_test_common.h
@@ -24,6 +24,7 @@
#include <pmc.h>
#include <stdbool.h>
#include <stdint.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -270,11 +271,23 @@ amd_test_count_pmc_rows_with_prefix(int cpu, const char *prefix)
}
/*
- * Count the AMD core counter rows ("K8-<n>") on a CPU. The L3, DF and UMC
- * rows share the K8 class and prefix ("K8-L3-", "K8-DF-", "K8-UMC") and are
- * not core counters. With free_only, count only rows that are free.
+ * Return the core counter number of an AMD row name, or -1 for other rows
+ * (L3, DF, UMC). Rows are "AMD-<n>", or "K8-<n>" on older kernels.
*/
static inline int
+amd_test_core_index(const char *name)
+{
+ unsigned int n;
+ char extra;
+
+ if (sscanf(name, "AMD-%u%c", &n, &extra) == 1 ||
+ sscanf(name, "K8-%u%c", &n, &extra) == 1)
+ return ((int)n);
+ return (-1);
+}
+
+/* Count the AMD core counter rows on a CPU; with free_only, the free ones. */
+static inline int
amd_test_count_core_rows(int cpu, bool free_only)
{
struct pmc_pmcinfo *pmcinfo;
@@ -293,8 +306,7 @@ amd_test_count_core_rows(int cpu, bool free_only)
count = 0;
for (i = 0; i < npmc; i++) {
p = &pmcinfo->pm_pmcs[i];
- if (strncmp(p->pm_name, "K8-", 3) != 0 ||
- p->pm_name[3] < '0' || p->pm_name[3] > '9')
+ if (amd_test_core_index(p->pm_name) < 0)
continue;
if (free_only && p->pm_rowdisp != PMC_DISP_FREE)
continue;
diff --git a/tests/sys/pmc/amd/pmc_ibs_fetch_op_test.c b/tests/sys/pmc/amd/pmc_ibs_fetch_op_test.c
new file mode 100644
index 000000000000..02025ff636e7
--- /dev/null
+++ b/tests/sys/pmc/amd/pmc_ibs_fetch_op_test.c
@@ -0,0 +1,338 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Author: Davi C.A <davi.chavesazevedo@amd.com>
+ * Author: Osvaldo Janeri Filho <ojanerif@amd.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/param.h>
+#include <sys/cpuset.h>
+#include <sys/event.h>
+#include <sys/pmc.h>
+#include <sys/pmclog.h>
+#include <sys/sysctl.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <dev/hwpmc/hwpmc_ibs.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "amd_pmc_test_common.h"
+
+#define FETCH 0x01
+#define OP 0x02
+#define WORK_SECONDS 2
+#define LOG_WAIT 5
+#define MIN_PERCENT 25
+
+struct episode {
+ int error;
+ char what[64];
+ uint64_t samples[2]; /* FETCH, OP */
+ uint64_t dropped;
+ uint64_t processed; /* kern.hwpmc.stats deltas */
+ uint64_t ignored;
+ uint64_t bad; /* foreign PMC, wrong CPU or type */
+};
+
+static void
+fail_step(struct episode *ep, const char *what)
+{
+ if (ep->error == 0) {
+ ep->error = errno != 0 ? errno : EPROTO;
+ strlcpy(ep->what, what, sizeof(ep->what));
+ }
+}
+
+static uint64_t
+stat(const char *name)
+{
+ uint64_t v;
+ size_t len;
+
+ len = sizeof(v);
+ if (sysctlbyname(name, &v, &len, NULL, 0) != 0)
+ return (0);
+ return (v);
+}
+
+/* Keeps both the fetch and the op pipe busy. */
+static void
+workload(void)
+{
+ static volatile uint64_t table[4096];
+ struct timespec end, now;
+ uint64_t v;
+ int i;
+
+ clock_gettime(CLOCK_MONOTONIC, &end);
+ end.tv_sec += WORK_SECONDS;
+ v = 1;
+ do {
+ for (i = 0; i < 100000; i++) {
+ v = v * 6364136223846793005ULL + 1442695040888963407ULL;
+ if ((v >> 61) & 1)
+ table[v & (nitems(table) - 1)] += v;
+ else
+ v ^= table[(v >> 17) & (nitems(table) - 1)];
+ }
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ } while (timespeccmp(&now, &end, <));
+}
+
+static void
+drain(void *parser, int kq, const pmc_id_t *ids, int cpu, struct episode *ep)
+{
+ static const uint8_t type[2] = {
+ PMC_CC_MULTIPART_IBS_FETCH, PMC_CC_MULTIPART_IBS_OP
+ };
+ struct pmclog_ev ev;
+ struct kevent ke;
+ struct timespec deadline, left, now;
+ const struct pmclog_ev_callchain *cc;
+ int k;
+
+ clock_gettime(CLOCK_MONOTONIC, &deadline);
+ deadline.tv_sec += LOG_WAIT;
+ for (;;) {
+ memset(&ev, 0, sizeof(ev));
+ if (pmclog_read(parser, &ev) == 0) {
+ if (ev.pl_type == PMCLOG_TYPE_DROPNOTIFY)
+ ep->dropped++;
+ if (ev.pl_type != PMCLOG_TYPE_CALLCHAIN)
+ continue;
+ cc = &ev.pl_u.pl_cc;
+ for (k = 0; k < 2; k++)
+ if (ids[k] != PMC_ID_INVALID &&
+ cc->pl_pmcid == ids[k])
+ break;
+ if (k == 2 || cc->pl_npc == 0 ||
+ PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cc->pl_cpuflags) !=
+ (unsigned int)cpu ||
+ ((const uint8_t *)cc->pl_pc)[0] != type[k])
+ ep->bad++;
+ else
+ ep->samples[k]++;
+ continue;
+ }
+ if (ev.pl_state != PMCLOG_EOF) {
+ errno = EPROTO;
+ fail_step(ep, "pmclog_read");
+ return;
+ }
+ if (ev.pl_type == PMCLOG_TYPE_CLOSELOG)
+ return;
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ if (timespeccmp(&now, &deadline, >=)) {
+ errno = ETIMEDOUT;
+ fail_step(ep, "waiting for CLOSELOG");
+ return;
+ }
+ timespecsub(&deadline, &now, &left);
+ (void)kevent(kq, NULL, 0, &ke, 1, &left);
+ }
+}
+
+/* A closed log leaves shutdown state on its owner: use a fresh process. */
+static void
+run_episode(int cpu, int engines, struct episode *ep)
+{
+ static const char *const event[2] = { "IBS-FETCH", "IBS-OP" };
+ static const pmc_value_t rate[2] = {
+ IBS_FETCH_MIN_RATE, IBS_OP_MIN_RATE
+ };
+ char path[] = "ibs-fo.XXXXXX";
+ struct kevent ke;
+ cpuset_t set;
+ pmc_id_t ids[2] = { PMC_ID_INVALID, PMC_ID_INVALID };
+ void *parser;
+ int fd, k, kq, reader;
+
+ memset(ep, 0, sizeof(*ep));
+ CPU_ZERO(&set);
+ CPU_SET(cpu, &set);
+ if (pmc_init() != 0 || cpuset_setaffinity(CPU_LEVEL_WHICH,
+ CPU_WHICH_TID, -1, sizeof(set), &set) != 0) {
+ fail_step(ep, "pmc_init or pinning");
+ return;
+ }
+ if ((fd = mkstemp(path)) < 0 ||
+ (reader = open(path, O_RDONLY)) < 0 || unlink(path) != 0 ||
+ (parser = pmclog_open(reader)) == NULL ||
+ (kq = kqueue()) < 0) {
+ fail_step(ep, "log setup");
+ return;
+ }
+ EV_SET(&ke, reader, EVFILT_VNODE, EV_ADD | EV_CLEAR,
+ NOTE_WRITE | NOTE_EXTEND, 0, NULL);
+ if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0 ||
+ pmc_configure_logfile(fd) != 0) {
+ fail_step(ep, "pmc_configure_logfile");
+ return;
+ }
+
+ ep->processed = stat("kern.hwpmc.stats.intr_processed");
+ ep->ignored = stat("kern.hwpmc.stats.intr_ignored");
+ for (k = 0; k < 2; k++)
+ if ((engines & (1 << k)) != 0 && (pmc_allocate(event[k],
+ PMC_MODE_SS, 0, cpu, &ids[k], rate[k]) != 0 ||
+ pmc_start(ids[k]) != 0))
+ fail_step(ep, event[k]);
+ if (ep->error == 0)
+ workload();
+ for (k = 0; k < 2; k++) {
+ if (ids[k] == PMC_ID_INVALID)
+ continue;
+ (void)pmc_stop(ids[k]);
+ if (pmc_release(ids[k]) != 0)
+ fail_step(ep, "pmc_release");
+ }
+ if (pmc_flush_logfile() != 0 || pmc_close_logfile() != 0)
+ fail_step(ep, "closing the log");
+ else
+ drain(parser, kq, ids, cpu, ep);
+ ep->processed = stat("kern.hwpmc.stats.intr_processed") - ep->processed;
+ ep->ignored = stat("kern.hwpmc.stats.intr_ignored") - ep->ignored;
+ (void)pmc_configure_logfile(-1);
+}
+
+static void
+episode(int cpu, int engines, struct episode *ep)
+{
+ pid_t child;
+ ssize_t n;
+ int fds[2], status;
+
+ ATF_REQUIRE(pipe(fds) == 0);
+ child = fork();
+ ATF_REQUIRE(child >= 0);
+ if (child == 0) {
+ close(fds[0]);
+ run_episode(cpu, engines, ep);
+ n = write(fds[1], ep, sizeof(*ep));
+ _exit(n == (ssize_t)sizeof(*ep) ? 0 : 1);
+ }
+ close(fds[1]);
+ n = read(fds[0], ep, sizeof(*ep));
+ close(fds[0]);
+ ATF_REQUIRE(waitpid(child, &status, 0) == child);
+ ATF_REQUIRE_MSG(n == (ssize_t)sizeof(*ep) && WIFEXITED(status) &&
+ WEXITSTATUS(status) == 0, "episode owner failed: status %#x",
+ status);
+ printf("engines %#x: fetch %ju op %ju dropped %ju bad %ju, "
+ "interrupts %ju processed %ju ignored\n", engines,
+ (uintmax_t)ep->samples[0], (uintmax_t)ep->samples[1],
+ (uintmax_t)ep->dropped, (uintmax_t)ep->bad,
+ (uintmax_t)ep->processed, (uintmax_t)ep->ignored);
+ ATF_REQUIRE_MSG(ep->error == 0, "%s failed: %s", ep->what,
+ strerror(ep->error));
+ ATF_REQUIRE_MSG(ep->dropped == 0 && ep->bad == 0,
+ "%ju dropped buffers, %ju malformed or misrouted samples",
+ (uintmax_t)ep->dropped, (uintmax_t)ep->bad);
+}
+
+/* Skip unless IBS Fetch and Op sampling work; return the CPU to use. */
+static int
+setup(void)
+{
+ cpuset_t mask;
+ u_int regs[4];
+ int cpu;
+
+ amd_test_skip_unless_amd();
+ do_cpuid(0x80000000, regs);
+ if (regs[0] < CPUID_IBSID)
+ atf_tc_skip("CPU lacks the IBS CPUID leaf");
+ do_cpuid(CPUID_IBSID, regs);
+ if ((regs[0] & (CPUID_IBSID_FETCHSAM | CPUID_IBSID_OPSAM)) !=
+ (CPUID_IBSID_FETCHSAM | CPUID_IBSID_OPSAM))
+ atf_tc_skip("CPU lacks IBS Fetch or Op sampling");
+ amd_test_skip_unless_hwpmc();
+
+ ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
+ sizeof(mask), &mask) == 0);
+ cpu = CPU_FFS(&mask) - 1;
+ ATF_REQUIRE(cpu >= 0);
+ return (cpu);
+}
+
+/*
+ * Regression test for 572cd669d6: pmc_ibs_intr() skipped a valid Op sample
+ * when Fetch was valid in the same NMI, and IBS-OP starved.
+ */
+ATF_TC(fetch_and_op_together);
+ATF_TC_HEAD(fetch_and_op_together, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "IBS-FETCH and IBS-OP system sampling on one CPU each deliver at "
+ "least a quarter of the samples they deliver alone");
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "timeout", "60");
+}
+ATF_TC_BODY(fetch_and_op_together, tc)
+{
+ struct episode both, fetch, op;
+ int cpu;
+
+ cpu = setup();
+
+ episode(cpu, FETCH, &fetch);
+ episode(cpu, OP, &op);
+ ATF_REQUIRE_MSG(fetch.samples[0] > 0 && op.samples[1] > 0,
+ "an engine alone delivered no samples: fetch %ju, op %ju",
+ (uintmax_t)fetch.samples[0], (uintmax_t)op.samples[1]);
+ episode(cpu, FETCH | OP, &both);
+
+ ATF_CHECK_MSG(both.samples[0] * 100 >= fetch.samples[0] * MIN_PERCENT,
+ "IBS-FETCH starved beside IBS-OP: %ju samples, %ju alone",
+ (uintmax_t)both.samples[0], (uintmax_t)fetch.samples[0]);
+ ATF_CHECK_MSG(both.samples[1] * 100 >= op.samples[1] * MIN_PERCENT,
+ "IBS-OP starved beside IBS-FETCH: %ju samples, %ju alone",
+ (uintmax_t)both.samples[1], (uintmax_t)op.samples[1]);
+}
+
+/* c97cea955f: an IBS interrupt was counted as processed twice. */
+ATF_TC(interrupts_counted_once);
+ATF_TC_HEAD(interrupts_counted_once, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Each IBS-FETCH sampling interrupt is counted once in "
+ "kern.hwpmc.stats.intr_processed and none in intr_ignored");
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "timeout", "30");
+}
+ATF_TC_BODY(interrupts_counted_once, tc)
+{
+ struct episode ep;
+
+ episode(setup(), FETCH, &ep);
+ ATF_CHECK_MSG(ep.ignored == 0, "%ju interrupts counted as ignored",
+ (uintmax_t)ep.ignored);
+ ATF_CHECK_MSG(ep.processed >= ep.samples[0] &&
+ ep.processed <= ep.samples[0] + ep.samples[0] / 10 + 16,
+ "%ju interrupts processed for %ju samples",
+ (uintmax_t)ep.processed, (uintmax_t)ep.samples[0]);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, fetch_and_op_together);
+ ATF_TP_ADD_TC(tp, interrupts_counted_once);
+
+ return (atf_no_error());
+}
diff --git a/tests/sys/pmc/amd/pmc_ibs_test.c b/tests/sys/pmc/amd/pmc_ibs_test.c
new file mode 100644
index 000000000000..cd99cf459929
--- /dev/null
+++ b/tests/sys/pmc/amd/pmc_ibs_test.c
@@ -0,0 +1,502 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Author: Davi C.A <davi.chavesazevedo@amd.com>
+ * Author: Osvaldo Janeri Filho <ojanerif@amd.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/param.h>
+#include <sys/cpuset.h>
+#include <sys/event.h>
+#include <sys/pmc.h>
+#include <sys/pmclog.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <dev/hwpmc/hwpmc_ibs.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "amd_pmc_test_common.h"
+
+#define IBS_RATE IBS_FETCH_MIN_RATE
+#define IBS_CAPS (PMC_CAP_INTERRUPT | PMC_CAP_SYSTEM | \
+ PMC_CAP_EDGE | PMC_CAP_QUALIFIER | \
+ PMC_CAP_PRECISE)
+#define LOG_WAIT_SECONDS 5
+
+static const struct pmc_classinfo *
+ibs_classinfo(void)
+{
+ const struct pmc_cpuinfo *cpuinfo;
+
+ ATF_REQUIRE_MSG(pmc_cpuinfo(&cpuinfo) == 0,
+ "pmc_cpuinfo() failed: %s", strerror(errno));
+ return (amd_test_find_classinfo(cpuinfo, PMC_CLASS_IBS));
+}
+
+static void
+skip_unless_ibs(void)
+{
+ u_int regs[4];
+
+ amd_test_skip_unless_amd();
+ do_cpuid(0x80000000, regs);
+ if (regs[0] < CPUID_IBSID)
+ atf_tc_skip("CPU lacks the IBS CPUID leaf");
+ do_cpuid(CPUID_IBSID, regs);
+ if ((regs[0] & (CPUID_IBSID_IBSFFV | CPUID_IBSID_FETCHSAM |
+ CPUID_IBSID_OPSAM)) != (CPUID_IBSID_IBSFFV |
+ CPUID_IBSID_FETCHSAM | CPUID_IBSID_OPSAM))
+ atf_tc_skip("CPU lacks IBS Fetch or Op sampling");
+ amd_test_skip_unless_hwpmc();
+ if (ibs_classinfo() == NULL)
+ atf_tc_skip("hwpmc does not expose PMC_CLASS_IBS");
+}
+
+static int
+pin_cpu(void)
+{
+ cpuset_t mask;
+ int cpu;
+
+ ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
+ sizeof(mask), &mask) == 0);
+ cpu = CPU_FFS(&mask) - 1;
+ ATF_REQUIRE(cpu >= 0);
+ CPU_ZERO(&mask);
+ CPU_SET(cpu, &mask);
+ ATF_REQUIRE_MSG(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
+ sizeof(mask), &mask) == 0, "cannot bind to cpu%d: %s", cpu,
+ strerror(errno));
+ return (cpu);
+}
+
+static int
+alloc_errno(const char *event, enum pmc_mode mode, int cpu,
+ pmc_value_t rate)
+{
+ pmc_id_t id;
+
+ id = PMC_ID_INVALID;
+ errno = 0;
+ if (pmc_allocate(event, mode, 0, cpu, &id, rate) == 0) {
+ amd_test_release_pmc(id);
+ return (0);
+ }
+ return (errno);
+}
+
+static pmc_id_t
+alloc_attach_fetch(pid_t pid, pmc_value_t rate)
+{
+ pmc_id_t id;
+ int error;
+
+ id = PMC_ID_INVALID;
+ ATF_REQUIRE_MSG(pmc_allocate("IBS-FETCH", PMC_MODE_TS, 0, PMC_CPU_ANY,
+ &id, rate) == 0, "pmc_allocate(TS IBS-FETCH) failed: %s",
+ strerror(errno));
+ if (pmc_attach(id, pid) != 0) {
+ error = errno;
+ amd_test_release_pmc(id);
+ atf_tc_fail("pmc_attach(IBS-FETCH, %d) failed: %s", pid,
+ strerror(error));
+ }
+ return (id);
+}
+
+ATF_TC(class_info);
+ATF_TC_HEAD(class_info, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "pmc_cpuinfo() reports PMC_CLASS_IBS with two PMCs, width 0 and "
+ "the interrupt, system, edge, qualifier and precise capabilities");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(class_info, tc)
+{
+ const struct pmc_classinfo *ci;
+
+ skip_unless_ibs();
+ ci = ibs_classinfo();
+ ATF_CHECK_EQ_MSG(ci->pm_num, 2, "IBS class has %u PMCs", ci->pm_num);
+ ATF_CHECK_EQ_MSG(ci->pm_width, 0, "IBS class width is %u",
+ ci->pm_width);
+ ATF_CHECK_MSG((ci->pm_caps & IBS_CAPS) == IBS_CAPS,
+ "IBS class caps %#x lack %#x", ci->pm_caps, IBS_CAPS);
+}
+
+ATF_TC(class_and_event_names);
+ATF_TC_HEAD(class_and_event_names, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "libpmc names the class IBS and lists the FETCH and OP events");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(class_and_event_names, tc)
+{
+ const char **names;
+ const char *name;
+ bool fetch, op;
+ int i, n;
+
+ skip_unless_ibs();
+ name = pmc_name_of_class(PMC_CLASS_IBS);
+ ATF_REQUIRE(name != NULL);
+ ATF_CHECK_STREQ(name, "IBS");
+
+ ATF_REQUIRE_MSG(pmc_event_names_of_class(PMC_CLASS_IBS, &names,
+ &n) == 0, "pmc_event_names_of_class() failed: %s",
+ strerror(errno));
+ fetch = op = false;
+ for (i = 0; i < n; i++) {
+ fetch |= strcasecmp(names[i], "FETCH") == 0;
+ op |= strcasecmp(names[i], "OP") == 0;
+ }
+ free(names);
+ ATF_CHECK_MSG(fetch && op, "IBS events: FETCH %s, OP %s",
+ fetch ? "listed" : "missing", op ? "listed" : "missing");
+}
+
+ATF_TC(pmcinfo_rows);
+ATF_TC_HEAD(pmcinfo_rows, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "pmc_pmcinfo() has exactly one IBS-FETCH and one IBS-OP row");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(pmcinfo_rows, tc)
+{
+ size_t fetch, op;
+ int cpu;
+
+ skip_unless_ibs();
+ cpu = pin_cpu();
+ fetch = amd_test_count_pmc_rows_with_prefix(cpu, "IBS-FETCH");
+ op = amd_test_count_pmc_rows_with_prefix(cpu, "IBS-OP");
+ ATF_CHECK_MSG(fetch == 1 && op == 1,
+ "cpu%d has %zu IBS-FETCH and %zu IBS-OP rows", cpu, fetch, op);
+}
+
+ATF_TC(alloc_system_sampling);
+ATF_TC_HEAD(alloc_system_sampling, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "IBS-FETCH and IBS-OP allocate as system-sampling PMCs; IBS-FETCH "
+ "reports the IBS capabilities and width 0");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(alloc_system_sampling, tc)
+{
+ pmc_id_t fetch, op;
+ uint32_t caps, width;
+ int cpu;
+
+ skip_unless_ibs();
+ cpu = pin_cpu();
+ fetch = op = PMC_ID_INVALID;
+ ATF_REQUIRE_MSG(pmc_allocate("IBS-FETCH", PMC_MODE_SS, 0, cpu, &fetch,
+ IBS_RATE) == 0, "pmc_allocate(IBS-FETCH) failed: %s",
+ strerror(errno));
+ ATF_CHECK_MSG(pmc_allocate("IBS-OP", PMC_MODE_SS, 0, cpu, &op,
+ IBS_RATE) == 0, "pmc_allocate(IBS-OP) failed: %s",
+ strerror(errno));
+
+ caps = 0;
+ width = UINT32_MAX;
+ ATF_CHECK(pmc_capabilities(fetch, &caps) == 0);
+ ATF_CHECK(pmc_width(fetch, &width) == 0);
+ ATF_CHECK_MSG((caps & IBS_CAPS) == IBS_CAPS,
+ "IBS-FETCH caps %#x lack %#x", caps, IBS_CAPS);
+ ATF_CHECK_EQ_MSG(width, 0, "IBS-FETCH width is %u", width);
+
+ amd_test_release_pmc(op);
+ amd_test_release_pmc(fetch);
+}
+
+ATF_TC(alloc_refusals);
+ATF_TC_HEAD(alloc_refusals, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "IBS allocation fails with EINVAL in counting mode, below the "
+ "minimum sample rate and with an unknown qualifier");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(alloc_refusals, tc)
+{
+ int cpu, error;
+
+ skip_unless_ibs();
+ cpu = pin_cpu();
+
+ error = alloc_errno("IBS-FETCH", PMC_MODE_SC, cpu, IBS_RATE);
+ ATF_CHECK_MSG(error == EINVAL, "counting mode: errno %d (%s)", error,
+ strerror(error));
+ error = alloc_errno("IBS-OP", PMC_MODE_SS, cpu, IBS_OP_MIN_RATE - 1);
+ ATF_CHECK_MSG(error == EINVAL, "rate %d: errno %d (%s)",
+ IBS_OP_MIN_RATE - 1, error, strerror(error));
+ error = alloc_errno("IBS-OP,no_such_qualifier", PMC_MODE_SS, cpu,
+ IBS_RATE);
+ ATF_CHECK_MSG(error == EINVAL, "unknown qualifier: errno %d (%s)",
+ error, strerror(error));
+}
+
+ATF_TC(fetch_attach_detach);
+ATF_TC_HEAD(fetch_attach_detach, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "A process-mode IBS-FETCH PMC attaches to its owner, detaches and "
+ "is released");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(fetch_attach_detach, tc)
+{
+ pmc_id_t id;
+
+ skip_unless_ibs();
+ (void)pin_cpu();
+ id = alloc_attach_fetch(getpid(), IBS_RATE);
+ ATF_CHECK_MSG(pmc_detach(id, getpid()) == 0,
+ "pmc_detach() failed: %s", strerror(errno));
+ amd_test_release_pmc(id);
+}
+
+ATF_TC(fetch_self_count);
+ATF_TC_HEAD(fetch_self_count, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "A running IBS-FETCH PMC attached to its owner reads a non-zero "
+ "count while the owner makes system calls");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(fetch_self_count, tc)
+{
+ pmc_id_t id;
+ pmc_value_t v;
+ int i, round;
+
+ skip_unless_ibs();
+ (void)pin_cpu();
+ id = alloc_attach_fetch(getpid(), IBS_RATE);
+ ATF_REQUIRE_MSG(pmc_start(id) == 0, "pmc_start() failed: %s",
+ strerror(errno));
+
+ /* The count restarts after each sample, so one read may see 0. */
+ v = 0;
+ for (round = 0; round < 100 && v == 0; round++) {
+ for (i = 0; i < 10000; i++)
+ (void)getppid();
+ ATF_REQUIRE(pmc_read(id, &v) == 0);
+ }
+ ATF_CHECK(pmc_stop(id) == 0);
+ ATF_CHECK(pmc_detach(id, getpid()) == 0);
+ amd_test_release_pmc(id);
+ ATF_CHECK_MSG(v != 0, "IBS-FETCH count stayed 0");
+}
+
+/* Wait for a sample of pid, or for CLOSELOG if closing. */
+static int
+wait_log(void *parser, int kq, pmc_id_t id, pid_t pid, bool closing,
+ bool *sampled, bool *dropped)
+{
+ struct pmclog_ev ev;
+ struct kevent ke;
+ struct timespec deadline, left, now;
+ const struct pmclog_ev_callchain *cc;
+ const uint8_t *hdr;
+
+ ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &deadline) == 0);
+ deadline.tv_sec += LOG_WAIT_SECONDS;
+ for (;;) {
+ if (!closing && pmc_flush_logfile() != 0)
+ return (errno);
+ memset(&ev, 0, sizeof(ev));
+ if (pmclog_read(parser, &ev) == 0) {
+ if (ev.pl_type == PMCLOG_TYPE_DROPNOTIFY)
+ *dropped = true;
+ if (ev.pl_type != PMCLOG_TYPE_CALLCHAIN)
+ continue;
+ cc = &ev.pl_u.pl_cc;
+ hdr = (const uint8_t *)cc->pl_pc;
+ if (cc->pl_pmcid != id || cc->pl_pid != (uint32_t)pid ||
+ (cc->pl_cpuflags & PMC_CC_F_MULTIPART) == 0 ||
+ cc->pl_npc == 0 ||
+ hdr[0] != PMC_CC_MULTIPART_IBS_FETCH)
+ return (EPROTO);
+ *sampled = true;
+ if (!closing)
+ return (0);
+ continue;
+ }
+ if (ev.pl_state != PMCLOG_EOF)
+ return (EPROTO);
+ if (ev.pl_type == PMCLOG_TYPE_CLOSELOG)
+ return (closing ? 0 : EPROTO);
+ ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
+ if (timespeccmp(&now, &deadline, >=))
+ return (ETIMEDOUT);
+ timespecsub(&deadline, &now, &left);
+ if (kevent(kq, NULL, 0, &ke, 1, &left) < 0 && errno != EINTR)
+ return (errno);
+ }
+}
+
+ATF_TC(fetch_child_samples);
+ATF_TC_HEAD(fetch_child_samples, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "An IBS-FETCH PMC attached to a child writes IBS-FETCH samples of "
+ "that child to the owner's log, without drops");
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "timeout", "30");
+}
+ATF_TC_BODY(fetch_child_samples, tc)
+{
+ struct kevent ke;
+ char path[] = "ibs-fetch.XXXXXX";
+ volatile uint64_t x;
+ uint64_t i;
+ pmc_id_t id;
+ pid_t child;
+ const char *what;
+ void *parser;
+ bool configured, dropped, sampled;
+ int error, fd, kq, reader, status;
+
+ skip_unless_ibs();
+ (void)pin_cpu();
+
+ fd = mkstemp(path);
+ ATF_REQUIRE(fd >= 0);
+ reader = open(path, O_RDONLY);
+ ATF_REQUIRE(reader >= 0);
+ ATF_REQUIRE(unlink(path) == 0);
+ parser = pmclog_open(reader);
+ ATF_REQUIRE(parser != NULL);
+ kq = kqueue();
+ ATF_REQUIRE(kq >= 0);
+ EV_SET(&ke, reader, EVFILT_VNODE, EV_ADD | EV_CLEAR,
+ NOTE_WRITE | NOTE_EXTEND, 0, NULL);
+ ATF_REQUIRE(kevent(kq, &ke, 1, NULL, 0, NULL) == 0);
+
+ child = fork();
+ ATF_REQUIRE(child >= 0);
+ if (child == 0) {
+ if (raise(SIGSTOP) != 0)
+ _exit(1);
+ x = 1;
+ for (i = 0; i < 500000000; i++)
+ x = x * 1664525U + 1013904223U;
+ _exit(0);
+ }
+ /* From here on, kill and reap the child before any failure. */
+ what = NULL;
+ configured = false;
+ id = PMC_ID_INVALID;
+ if (waitpid(child, &status, WUNTRACED) != child || !WIFSTOPPED(status))
+ what = "stopping the child";
+ else if (pmc_configure_logfile(fd) != 0)
+ what = "pmc_configure_logfile";
+ else {
+ configured = true;
+ if (pmc_allocate("IBS-FETCH", PMC_MODE_TS, 0, PMC_CPU_ANY, &id,
+ IBS_FETCH_MAX_RATE) != 0)
+ what = "pmc_allocate";
+ else if (pmc_attach(id, child) != 0)
+ what = "pmc_attach";
+ else if (pmc_start(id) != 0)
+ what = "pmc_start";
+ else if (kill(child, SIGCONT) != 0)
+ what = "kill(SIGCONT)";
+ }
+ error = what != NULL ? errno : 0;
+
+ sampled = dropped = false;
+ if (error == 0)
+ error = wait_log(parser, kq, id, child, false, &sampled,
+ &dropped);
+
+ (void)kill(child, SIGKILL);
+ (void)waitpid(child, &status, 0);
+ if (id != PMC_ID_INVALID) {
+ (void)pmc_stop(id);
+ amd_test_release_pmc(id);
+ }
+ if (configured) {
+ ATF_CHECK(pmc_flush_logfile() == 0);
+ if (pmc_close_logfile() != 0)
+ ATF_CHECK_MSG(false, "pmc_close_logfile() failed: %s",
+ strerror(errno));
+ else if (error == 0)
+ error = wait_log(parser, kq, id, child, true,
+ &sampled, &dropped);
+ ATF_CHECK(pmc_configure_logfile(-1) == 0);
+ }
+ pmclog_close(parser);
+ close(kq);
+ close(reader);
+ close(fd);
+
+ ATF_REQUIRE_MSG(what == NULL, "%s failed: %s", what, strerror(error));
+ ATF_REQUIRE_MSG(error == 0, "reading the log failed: %s",
+ strerror(error));
+ ATF_CHECK_MSG(sampled && !dropped, "IBS-FETCH log: sampled %d, "
+ "dropped %d", sampled, dropped);
+}
+
+ATF_TC(getmsr_rejected);
+ATF_TC_HEAD(getmsr_rejected, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "pmc_get_msr() fails with ENOSYS or EINVAL for an IBS-FETCH PMC");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(getmsr_rejected, tc)
+{
+ pmc_id_t id;
+ uint32_t msr;
+ int error, rc;
+
+ skip_unless_ibs();
+ (void)pin_cpu();
+ id = alloc_attach_fetch(getpid(), IBS_RATE);
+ errno = 0;
+ rc = pmc_get_msr(id, &msr);
+ error = errno;
+ ATF_CHECK(pmc_detach(id, getpid()) == 0);
+ amd_test_release_pmc(id);
+ /* IBS has no GETMSR method; the sampling-mode check may come first. */
+ ATF_CHECK_MSG(rc == -1 && (error == ENOSYS || error == EINVAL),
+ "pmc_get_msr() returned %d, errno %d (%s)", rc, error,
+ strerror(error));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, class_info);
+ ATF_TP_ADD_TC(tp, class_and_event_names);
+ ATF_TP_ADD_TC(tp, pmcinfo_rows);
+ ATF_TP_ADD_TC(tp, alloc_system_sampling);
+ ATF_TP_ADD_TC(tp, alloc_refusals);
+ ATF_TP_ADD_TC(tp, fetch_attach_detach);
+ ATF_TP_ADD_TC(tp, fetch_self_count);
+ ATF_TP_ADD_TC(tp, fetch_child_samples);
+ ATF_TP_ADD_TC(tp, getmsr_rejected);
+
+ return (atf_no_error());
+}
diff --git a/tests/sys/pmc/amd/pmc_rowalloc_test.c b/tests/sys/pmc/amd/pmc_rowalloc_test.c
new file mode 100644
index 000000000000..c578046e8128
--- /dev/null
+++ b/tests/sys/pmc/amd/pmc_rowalloc_test.c
@@ -0,0 +1,286 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Author: Davi C.A <davi.chavesazevedo@amd.com>
+ * Author: Osvaldo Janeri Filho <ojanerif@amd.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
*** 809 LINES SKIPPED ***