git: d546356523f3 - main - tests/sys/pmc: add PERF, RDPMC, row, sampling and unload tests

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 20:26:49 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=d546356523f3e4856611fe25e0d9c8cf34ba1941

commit d546356523f3e4856611fe25e0d9c8cf34ba1941
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2026-11-16 20:15:56 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-16 20:26:45 +0000

    tests/sys/pmc: add PERF, RDPMC, row, sampling and unload tests
    
    - pmc_perf_test: the pmc.perf(3) class (MPERF, APERF, IRPERF).
    - pmc_rdpmc_test: pmc_get_msr() and RDPMC.
    - pmc_row_test: row disposition per mode; system sampling needs a log.
    - pmc_ss_test: system sampling into a log, and its interrupt accounting.
    - pmc_unload_test: unloading hwpmc.ko leaks no memory.
    
    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/D61062
---
 tests/sys/pmc/Makefile          |  10 ++
 tests/sys/pmc/pmc_perf_test.c   | 333 ++++++++++++++++++++++++++++++++++++++++
 tests/sys/pmc/pmc_rdpmc_test.c  | 172 +++++++++++++++++++++
 tests/sys/pmc/pmc_row_test.c    | 123 +++++++++++++++
 tests/sys/pmc/pmc_ss_test.c     | 311 +++++++++++++++++++++++++++++++++++++
 tests/sys/pmc/pmc_unload_test.c | 129 ++++++++++++++++
 6 files changed, 1078 insertions(+)

diff --git a/tests/sys/pmc/Makefile b/tests/sys/pmc/Makefile
index 656e9055d11d..ba8f11b6dbd7 100644
--- a/tests/sys/pmc/Makefile
+++ b/tests/sys/pmc/Makefile
@@ -7,6 +7,9 @@ ATF_TESTS_C+=	pmc_exec_test
 ATF_TESTS_C+=	pmc_lifecycle_test
 ATF_TESTS_C+=	pmc_log_test
 ATF_TESTS_C+=	pmc_pidtrack_test
+ATF_TESTS_C+=	pmc_row_test
+ATF_TESTS_C+=	pmc_ss_test
+ATF_TESTS_C+=	pmc_unload_test
 ATF_TESTS_C+=	pmc_wrap_test
 
 ATF_TESTS_SH+=	pmc_callchain_ur_test
@@ -15,7 +18,9 @@ ATF_TESTS_SH+=	pmc_cli_test
 .if ${MACHINE_ARCH} == "amd64"
 ATF_TESTS_C+=	pmc_exterr_test
 ATF_TESTS_C+=	pmc_mapfile_test
+ATF_TESTS_C+=	pmc_perf_test
 ATF_TESTS_C+=	pmc_rapl_test
+ATF_TESTS_C+=	pmc_rdpmc_test
 
 ATF_TESTS_SH+=	pmcstat_tsc_test
 
@@ -26,11 +31,16 @@ TEST_METADATA.pmc_exterr_test+=		required_user="root"
 TEST_METADATA.pmc_callchain_ur_test+=	is_exclusive="true"
 TEST_METADATA.pmc_cli_test+=		is_exclusive="true"
 TEST_METADATA.pmc_pidtrack_test+=	is_exclusive="true"
+TEST_METADATA.pmc_perf_test+=		is_exclusive="true"
 TEST_METADATA.pmc_rapl_test+=		is_exclusive="true"
+TEST_METADATA.pmc_rdpmc_test+=		is_exclusive="true"
+TEST_METADATA.pmc_ss_test+=		is_exclusive="true"
+TEST_METADATA.pmc_unload_test+=		is_exclusive="true"
 TEST_METADATA.pmcstat_tsc_test+=	required_kmods="hwpmc"
 
 LIBADD+=			pmc
 LIBADD.pmc_detach_test+=	pthread
+LIBADD.pmc_unload_test+=	memstat
 
 CFLAGS.pmc_exterr_test.c+=	-I${SRCTOP}/tests
 
diff --git a/tests/sys/pmc/pmc_perf_test.c b/tests/sys/pmc/pmc_perf_test.c
new file mode 100644
index 000000000000..0fdf97b3a25c
--- /dev/null
+++ b/tests/sys/pmc/pmc_perf_test.c
@@ -0,0 +1,333 @@
+/*
+ * 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/pmc.h>
+
+#include <machine/cpufunc.h>
+#include <machine/specialreg.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <pmc.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+#define	WORK_ITERATIONS	1000000
+
+static const struct pmc_classinfo *
+perf_classinfo(void)
+{
+	const struct pmc_cpuinfo *ci;
+	uint32_t i;
+
+	ATF_REQUIRE_MSG(pmc_cpuinfo(&ci) == 0, "pmc_cpuinfo() failed: %s",
+	    strerror(errno));
+	for (i = 0; i < ci->pm_nclass; i++)
+		if (ci->pm_classes[i].pm_class == PMC_CLASS_PERF)
+			return (&ci->pm_classes[i]);
+	return (NULL);
+}
+
+static int
+perf_setup(void)
+{
+	cpuset_t mask;
+	int cpu;
+
+	if (pmc_init() != 0)
+		atf_tc_skip("hwpmc(4) is not loaded: %s", strerror(errno));
+	if (perf_classinfo() == NULL)
+		atf_tc_skip("hwpmc does not expose the PERF class");
+
+	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(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
+	    sizeof(mask), &mask) == 0);
+	return (cpu);
+}
+
+static bool
+perf_has_row(int cpu, const char *name)
+{
+	struct pmc_pmcinfo *info;
+	bool found;
+	int i, n;
+
+	n = pmc_npmc(cpu);
+	ATF_REQUIRE(n >= 0);
+	ATF_REQUIRE(pmc_pmcinfo(cpu, &info) == 0);
+	found = false;
+	for (i = 0; i < n; i++)
+		if (info->pm_pmcs[i].pm_class == PMC_CLASS_PERF &&
+		    strcmp(info->pm_pmcs[i].pm_name, name) == 0)
+			found = true;
+	free(info);
+	return (found);
+}
+
+static void
+work(void)
+{
+	volatile uint64_t x;
+	int i;
+
+	x = 1;
+	for (i = 0; i < WORK_ITERATIONS; i++)
+		x = x * 1664525U + 1013904223U;
+}
+
+/* PERF MSRs are free-running: reads advance even while stopped. */
+static void
+check_counter(const char *row, const char *name, const char *alias)
+{
+	const char *names[2] = { name, alias };
+	pmc_value_t before, previous, running, stopped;
+	pmc_id_t id;
+	int cpu, i;
+
+	cpu = perf_setup();
+	if (!perf_has_row(cpu, row))
+		atf_tc_skip("cpu%d has no %s row", cpu, row);
+
+	previous = 0;
+	for (i = 0; i < 2; i++) {
+		id = PMC_ID_INVALID;
+		ATF_REQUIRE_MSG(pmc_allocate(names[i], PMC_MODE_SC, 0, cpu,
+		    &id, 0) == 0, "pmc_allocate(%s) failed: %s", names[i],
+		    strerror(errno));
+		ATF_CHECK_EQ(PMC_ID_TO_CLASS(id), PMC_CLASS_PERF);
+		ATF_REQUIRE(pmc_read(id, &before) == 0);
+		ATF_CHECK_MSG(before > previous, "%s went back across the "
+		    "release: %ju, then %ju", row, (uintmax_t)previous,
+		    (uintmax_t)before);
+		ATF_REQUIRE(pmc_start(id) == 0);
+		work();
+		ATF_REQUIRE(pmc_read(id, &running) == 0);
+		ATF_REQUIRE(pmc_stop(id) == 0);
+		work();
+		ATF_REQUIRE(pmc_read(id, &stopped) == 0);
+		ATF_CHECK_MSG(running > before && stopped > running,
+		    "%s did not advance: %ju, %ju started, %ju stopped", row,
+		    (uintmax_t)before, (uintmax_t)running, (uintmax_t)stopped);
+		ATF_REQUIRE(pmc_release(id) == 0);
+		errno = 0;
+		ATF_CHECK_MSG(pmc_read(id, &before) == -1 && errno == ESRCH,
+		    "reading the released %s id: errno %d", row, errno);
+		previous = stopped;
+	}
+}
+
+ATF_TC(class_rows);
+ATF_TC_HEAD(class_rows, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "The PERF class is 64 bits wide and read-only; it has MPERF and "
+	    "APERF rows only if CPUID reports them, an IRPERF row only if "
+	    "CPUID reports it, and libpmc names every row");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(class_rows, tc)
+{
+	static const char *const rows[] = { "MPERF", "APERF", "IRPERF" };
+	const struct pmc_classinfo *ci;
+	const char **names;
+	u_int regs[4];
+	bool aperfmperf, irperf, listed;
+	size_t r;
+	int cpu, i, n, nrows;
+
+	cpu = perf_setup();
+	ci = perf_classinfo();
+	ATF_CHECK_EQ_MSG(ci->pm_width, 64, "PERF width is %u", ci->pm_width);
+	ATF_CHECK_EQ_MSG(ci->pm_caps, PMC_CAP_READ, "PERF caps are %#x",
+	    ci->pm_caps);
+
+	do_cpuid(0, regs);
+	aperfmperf = false;
+	if (regs[0] >= 6) {
+		do_cpuid(6, regs);
+		aperfmperf = (regs[2] & CPUID_PERF_STAT) != 0;
+	}
+	do_cpuid(0x80000000, regs);
+	irperf = false;
+	if (regs[0] >= 0x80000008) {
+		do_cpuid(0x80000008, regs);
+		irperf = (regs[1] & AMDFEID_IRPERF) != 0;
+	}
+
+	ATF_REQUIRE(pmc_event_names_of_class(PMC_CLASS_PERF, &names, &n) == 0);
+	nrows = 0;
+	for (r = 0; r < nitems(rows); r++) {
+		if (!perf_has_row(cpu, rows[r]))
+			continue;
+		nrows++;
+		ATF_CHECK_MSG(r == 2 ? irperf : aperfmperf,
+		    "cpu%d has a %s row that CPUID does not report", cpu,
+		    rows[r]);
+		listed = false;
+		for (i = 0; i < n; i++)
+			listed |= strcasecmp(names[i], rows[r]) == 0;
+		ATF_CHECK_MSG(listed, "libpmc does not name %s", rows[r]);
+	}
+	free(names);
+	ATF_CHECK_MSG(perf_has_row(cpu, "MPERF") == aperfmperf &&
+	    perf_has_row(cpu, "APERF") == aperfmperf,
+	    "MPERF and APERF rows do not match CPUID");
+	ATF_CHECK_EQ_MSG((uint32_t)nrows, ci->pm_num,
+	    "%d PERF rows on cpu%d, the class reports %u", nrows, cpu,
+	    ci->pm_num);
+}
+
+ATF_TC(mperf_counts);
+ATF_TC_HEAD(mperf_counts, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "MPERF advances under load when started and when stopped, and "
+	    "continues across a release");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(mperf_counts, tc)
+{
+	check_counter("MPERF", "perf-mperf", "mperf");
+}
+
+ATF_TC(aperf_counts);
+ATF_TC_HEAD(aperf_counts, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "APERF advances under load when started and when stopped, and "
+	    "continues across a release");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(aperf_counts, tc)
+{
+	check_counter("APERF", "perf-aperf", "aperf");
+}
+
+ATF_TC(irperf_counts);
+ATF_TC_HEAD(irperf_counts, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "IRPERF advances under load when started and when stopped, and "
+	    "continues across a release");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(irperf_counts, tc)
+{
+	check_counter("IRPERF", "perf-irperf", "irperf");
+}
+
+ATF_TC(irperf_matches_instructions);
+ATF_TC_HEAD(irperf_matches_instructions, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Over the same loop on one CPU, IRPERF and a system-mode "
+	    "instructions PMC advance by amounts within 10% of each other");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(irperf_matches_instructions, tc)
+{
+	pmc_value_t i0, i1, r0, r1;
+	pmc_id_t instr, irperf;
+	uint64_t di, dr;
+	int cpu;
+
+	cpu = perf_setup();
+	if (!perf_has_row(cpu, "IRPERF"))
+		atf_tc_skip("cpu%d has no IRPERF row", cpu);
+	irperf = instr = PMC_ID_INVALID;
+	ATF_REQUIRE(pmc_allocate("perf-irperf", PMC_MODE_SC, 0, cpu, &irperf,
+	    0) == 0);
+	if (pmc_allocate("instructions", PMC_MODE_SC, 0, cpu, &instr, 0) != 0)
+		atf_tc_skip("no instructions event: %s", strerror(errno));
+	ATF_REQUIRE(pmc_start(irperf) == 0);
+	ATF_REQUIRE(pmc_start(instr) == 0);
+
+	/* The IRPERF reads bracket the instructions reads. */
+	ATF_REQUIRE(pmc_read(irperf, &r0) == 0);
+	ATF_REQUIRE(pmc_read(instr, &i0) == 0);
+	work();
+	work();
+	ATF_REQUIRE(pmc_read(instr, &i1) == 0);
+	ATF_REQUIRE(pmc_read(irperf, &r1) == 0);
+	ATF_CHECK(pmc_stop(instr) == 0);
+	ATF_CHECK(pmc_release(instr) == 0);
+	ATF_CHECK(pmc_stop(irperf) == 0);
+	ATF_CHECK(pmc_release(irperf) == 0);
+
+	di = i1 - i0;
+	dr = r1 - r0;
+	ATF_CHECK_MSG(di > 0 && dr * 10 >= di * 9 && dr * 10 <= di * 11,
+	    "IRPERF advanced by %ju, instructions by %ju", (uintmax_t)dr,
+	    (uintmax_t)di);
+}
+
+ATF_TC(mode_refusals);
+ATF_TC_HEAD(mode_refusals, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "A PERF counter is refused in process counting mode (EINVAL), in "
+	    "either sampling mode (EOPNOTSUPP) and with a qualifier (EINVAL)");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(mode_refusals, tc)
+{
+	static const struct {
+		const char	*event;
+		enum pmc_mode	mode;
+		int		error;
+	} cases[] = {
+		{ "perf-mperf",		PMC_MODE_TC,	EINVAL },
+		{ "perf-mperf",		PMC_MODE_SS,	EOPNOTSUPP },
+		{ "perf-mperf",		PMC_MODE_TS,	EOPNOTSUPP },
+		{ "perf-mperf,usr",	PMC_MODE_SC,	EINVAL },
+	};
+	pmc_id_t id;
+	size_t i;
+	int cpu, rc;
+
+	cpu = perf_setup();
+	if (!perf_has_row(cpu, "MPERF"))
+		atf_tc_skip("cpu%d has no MPERF row", cpu);
+	for (i = 0; i < nitems(cases); i++) {
+		id = PMC_ID_INVALID;
+		errno = 0;
+		rc = pmc_allocate(cases[i].event, cases[i].mode, 0,
+		    PMC_IS_SYSTEM_MODE(cases[i].mode) ? cpu : PMC_CPU_ANY,
+		    &id, 0);
+		if (rc == 0)
+			ATF_CHECK(pmc_release(id) == 0);
+		ATF_CHECK_MSG(rc == -1 && errno == cases[i].error,
+		    "%s in mode %d: rc %d, errno %d, expected %d",
+		    cases[i].event, cases[i].mode, rc, errno, cases[i].error);
+	}
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, class_rows);
+	ATF_TP_ADD_TC(tp, mperf_counts);
+	ATF_TP_ADD_TC(tp, aperf_counts);
+	ATF_TP_ADD_TC(tp, irperf_counts);
+	ATF_TP_ADD_TC(tp, irperf_matches_instructions);
+	ATF_TP_ADD_TC(tp, mode_refusals);
+
+	return (atf_no_error());
+}
diff --git a/tests/sys/pmc/pmc_rdpmc_test.c b/tests/sys/pmc/pmc_rdpmc_test.c
new file mode 100644
index 000000000000..a79ef7b6e7bd
--- /dev/null
+++ b/tests/sys/pmc/pmc_rdpmc_test.c
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Author: Osvaldo Janeri Filho <ojanerif@amd.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/param.h>
+#include <sys/cpuset.h>
+#include <sys/pmc.h>
+#include <sys/wait.h>
+
+#include <machine/cpufunc.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <pmc.h>
+#include <signal.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+#define	EVENT		"instructions"
+#define	ITERATIONS	1000000
+
+static void
+setup(void)
+{
+	cpuset_t mask;
+	int cpu;
+
+	if (pmc_init() != 0)
+		atf_tc_skip("hwpmc(4) is not loaded: %s", strerror(errno));
+
+	/* RDPMC reads this CPU's counter; stay on one 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(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
+	    sizeof(mask), &mask) == 0);
+}
+
+static pmc_id_t
+alloc_tc(uint32_t flags)
+{
+	pmc_id_t id;
+
+	id = PMC_ID_INVALID;
+	if (pmc_allocate(EVENT, PMC_MODE_TC, flags, PMC_CPU_ANY, &id, 0) != 0)
+		atf_tc_skip("pmc_allocate(%s) failed: %s", EVENT,
+		    strerror(errno));
+	return (id);
+}
+
+static int
+getmsr_errno(pmc_id_t id)
+{
+	uint32_t msr;
+
+	errno = 0;
+	return (pmc_get_msr(id, &msr) == 0 ? 0 : errno);
+}
+
+static void
+work(void)
+{
+	volatile uint64_t x;
+	int i;
+
+	x = 1;
+	for (i = 0; i < ITERATIONS; i++)
+		x = x * 1664525U + 1013904223U;
+}
+
+ATF_TC(rdpmc_reads_own_counter);
+ATF_TC_HEAD(rdpmc_reads_own_counter, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "RDPMC of the index pmc_get_msr() returns for a counting PMC "
+	    "attached to its owner advances by at least one instruction per "
+	    "loop iteration");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(rdpmc_reads_own_counter, tc)
+{
+	uint64_t after, before;
+	uint32_t msr;
+	pmc_id_t id;
+
+	setup();
+	id = alloc_tc(0);
+	ATF_REQUIRE(pmc_attach(id, 0) == 0);
+	ATF_REQUIRE(pmc_start(id) == 0);
+	ATF_REQUIRE_MSG(pmc_get_msr(id, &msr) == 0, "pmc_get_msr() failed: %s",
+	    strerror(errno));
+	before = rdpmc(msr);
+	work();
+	after = rdpmc(msr);
+	ATF_CHECK(pmc_stop(id) == 0);
+	ATF_CHECK(pmc_release(id) == 0);
+	ATF_CHECK_MSG(after - before >= ITERATIONS,
+	    "RDPMC(%#x) advanced by %ju over %d iterations", msr,
+	    (uintmax_t)(after - before), ITERATIONS);
+}
+
+ATF_TC(getmsr_refusals);
+ATF_TC_HEAD(getmsr_refusals, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "pmc_get_msr() fails with EINVAL for a system-mode PMC, an "
+	    "unattached PMC, a PMC attached to another process and an "
+	    "inherited PMC, and with ENOSYS for the software class");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(getmsr_refusals, tc)
+{
+	pmc_id_t id;
+	pid_t child;
+	int error, status;
+
+	setup();
+
+	id = PMC_ID_INVALID;
+	ATF_REQUIRE(pmc_allocate(EVENT, PMC_MODE_SC, 0, 0, &id, 0) == 0);
+	error = getmsr_errno(id);
+	ATF_CHECK_MSG(error == EINVAL, "system mode: errno %d", error);
+	ATF_CHECK(pmc_release(id) == 0);
+
+	id = alloc_tc(0);
+	error = getmsr_errno(id);
+	ATF_CHECK_MSG(error == EINVAL, "unattached: errno %d", error);
+
+	child = fork();
+	ATF_REQUIRE(child >= 0);
+	if (child == 0) {
+		pause();
+		_exit(0);
+	}
+	ATF_REQUIRE(pmc_attach(id, child) == 0);
+	error = getmsr_errno(id);
+	ATF_CHECK_MSG(error == EINVAL, "attached to a child: errno %d", error);
+	(void)kill(child, SIGKILL);
+	(void)waitpid(child, &status, 0);
+	ATF_CHECK(pmc_release(id) == 0);
+
+	id = alloc_tc(PMC_F_DESCENDANTS);
+	ATF_REQUIRE(pmc_attach(id, 0) == 0);
+	error = getmsr_errno(id);
+	ATF_CHECK_MSG(error == EINVAL, "inherited: errno %d", error);
+	ATF_CHECK(pmc_release(id) == 0);
+
+	id = PMC_ID_INVALID;
+	ATF_REQUIRE(pmc_allocate("SOFT-CLOCK.HARD", PMC_MODE_TC, 0,
+	    PMC_CPU_ANY, &id, 0) == 0);
+	ATF_REQUIRE(pmc_attach(id, 0) == 0);
+	error = getmsr_errno(id);
+	ATF_CHECK_MSG(error == ENOSYS, "software class: errno %d", error);
+	ATF_CHECK(pmc_release(id) == 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, rdpmc_reads_own_counter);
+	ATF_TP_ADD_TC(tp, getmsr_refusals);
+
+	return (atf_no_error());
+}
diff --git a/tests/sys/pmc/pmc_row_test.c b/tests/sys/pmc/pmc_row_test.c
new file mode 100644
index 000000000000..772409468a1d
--- /dev/null
+++ b/tests/sys/pmc/pmc_row_test.c
@@ -0,0 +1,123 @@
+/*
+ * 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/types.h>
+#include <sys/pmc.h>
+
+#include <errno.h>
+#include <pmc.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <atf-c.h>
+
+/* The software class lets these run on any CPU. */
+#define	SOFT_EVENT	"SOFT-CLOCK.HARD"
+
+static void
+require_hwpmc(void)
+{
+	if (pmc_init() != 0)
+		atf_tc_skip("hwpmc(4) is not available: %s", strerror(errno));
+}
+
+static enum pmc_disp
+row_disposition(pmc_id_t pmcid)
+{
+	struct pmc_pmcinfo *pmcinfo;
+	enum pmc_disp disp;
+	unsigned int row;
+	int npmc;
+
+	row = PMC_ID_TO_ROWINDEX(pmcid);
+	npmc = pmc_npmc(0);
+	ATF_REQUIRE_MSG(npmc > 0, "pmc_npmc(0) failed: %s", strerror(errno));
+	ATF_REQUIRE_MSG(row < (unsigned int)npmc,
+	    "row %u is beyond the %d rows of cpu0", row, npmc);
+	ATF_REQUIRE_MSG(pmc_pmcinfo(0, &pmcinfo) == 0,
+	    "pmc_pmcinfo(0) failed: %s", strerror(errno));
+	disp = pmcinfo->pm_pmcs[row].pm_rowdisp;
+	free(pmcinfo);
+	return (disp);
+}
+
+ATF_TC(row_disposition_follows_mode);
+ATF_TC_HEAD(row_disposition_follows_mode, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "pmc_pmcinfo() reports the row of a process-mode PMC as THREAD "
+	    "and the row of a system-mode PMC as STANDALONE");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(row_disposition_follows_mode, tc)
+{
+	enum pmc_disp disp;
+	pmc_id_t id;
+
+	require_hwpmc();
+
+	ATF_REQUIRE_MSG(pmc_allocate(SOFT_EVENT, PMC_MODE_TC, 0, PMC_CPU_ANY,
+	    &id, 0) == 0, "pmc_allocate(%s, TC) failed: %s", SOFT_EVENT,
+	    strerror(errno));
+	disp = row_disposition(id);
+	ATF_CHECK_MSG(disp == PMC_DISP_THREAD,
+	    "process-mode row is %s, expected %s",
+	    pmc_name_of_disposition(disp),
+	    pmc_name_of_disposition(PMC_DISP_THREAD));
+	ATF_REQUIRE(pmc_release(id) == 0);
+
+	ATF_REQUIRE_MSG(pmc_allocate(SOFT_EVENT, PMC_MODE_SC, 0, 0, &id,
+	    0) == 0, "pmc_allocate(%s, SC) failed: %s", SOFT_EVENT,
+	    strerror(errno));
+	disp = row_disposition(id);
+	ATF_CHECK_MSG(disp == PMC_DISP_STANDALONE,
+	    "system-mode row is %s, expected %s",
+	    pmc_name_of_disposition(disp),
+	    pmc_name_of_disposition(PMC_DISP_STANDALONE));
+	ATF_REQUIRE(pmc_release(id) == 0);
+}
+
+ATF_TC(system_sampling_needs_logfile);
+ATF_TC_HEAD(system_sampling_needs_logfile, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Starting a system sampling PMC without a configured log file "
+	    "fails with EDOOFUS");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(system_sampling_needs_logfile, tc)
+{
+	pmc_id_t id;
+	int error;
+
+	require_hwpmc();
+
+	ATF_REQUIRE_MSG(pmc_allocate(SOFT_EVENT, PMC_MODE_SS, 0, 0, &id,
+	    10000) == 0, "pmc_allocate(%s, SS) failed: %s", SOFT_EVENT,
+	    strerror(errno));
+	errno = 0;
+	error = pmc_start(id);
+	if (error == 0) {
+		/* pmc_release() stops a running PMC itself. */
+		(void)pmc_release(id);
+		atf_tc_fail("pmc_start() succeeded without a log file");
+	}
+	ATF_CHECK_MSG(errno == EDOOFUS,
+	    "pmc_start() failed with %d (%s), expected EDOOFUS", errno,
+	    strerror(errno));
+	ATF_REQUIRE(pmc_release(id) == 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, row_disposition_follows_mode);
+	ATF_TP_ADD_TC(tp, system_sampling_needs_logfile);
+
+	return (atf_no_error());
+}
diff --git a/tests/sys/pmc/pmc_ss_test.c b/tests/sys/pmc/pmc_ss_test.c
new file mode 100644
index 000000000000..4716080903ab
--- /dev/null
+++ b/tests/sys/pmc/pmc_ss_test.c
@@ -0,0 +1,311 @@
+/*
+ * 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 <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>
+
+#define	EVENT		"instructions"
+#define	PERIOD		100000000
+#define	NPMC		2
+#define	WAIT_SECONDS	5
+
+struct result {
+	int		error;
+	char		what[64];
+	uint64_t	own[NPMC];	/* user-mode samples of the workload */
+	uint64_t	dropped;
+	uint64_t	total;		/* samples of either PMC */
+	uint64_t	processed;	/* kern.hwpmc.stats deltas */
+	uint64_t	ignored;
+	uint64_t	bad;		/* foreign PMC id or wrong CPU */
+	bool		closed;
+};
+
+static void
+fail_step(struct result *r, const char *what)
+{
+	if (r->error == 0) {
+		r->error = errno != 0 ? errno : EPROTO;
+		strlcpy(r->what, what, sizeof(r->what));
+	}
+}
+
+/* Returns true at CLOSELOG. */
+static bool
+consume(void *parser, const pmc_id_t *ids, int cpu, pid_t pid,
+    struct result *r)
+{
+	struct pmclog_ev ev;
+	const struct pmclog_ev_callchain *cc;
+	int k;
+
+	for (;;) {
+		memset(&ev, 0, sizeof(ev));
+		if (pmclog_read(parser, &ev) != 0) {
+			if (ev.pl_state != PMCLOG_EOF) {
+				errno = EPROTO;
+				fail_step(r, "pmclog_read");
+				return (true);
+			}
+			return (ev.pl_type == PMCLOG_TYPE_CLOSELOG);
+		}
+		if (ev.pl_type == PMCLOG_TYPE_DROPNOTIFY)
+			r->dropped++;
+		if (ev.pl_type != PMCLOG_TYPE_CALLCHAIN)
+			continue;
+		cc = &ev.pl_u.pl_cc;
+		for (k = 0; k < NPMC; k++)
+			if (cc->pl_pmcid == ids[k])
+				break;
+		if (k == NPMC ||
+		    PMC_CALLCHAIN_CPUFLAGS_TO_CPU(cc->pl_cpuflags) !=
+		    (unsigned int)cpu) {
+			r->bad++;
+			continue;
+		}
+		r->total++;
+		/* Other processes on the CPU are legitimate samples too. */
+		if (cc->pl_pid == (uint32_t)pid &&
+		    PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(cc->pl_cpuflags) != 0)
+			r->own[k]++;
+	}
+}
+
+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);
+}
+
+static bool
+all_sampled(const struct result *r)
+{
+	int k;
+
+	for (k = 0; k < NPMC; k++)
+		if (r->own[k] == 0)
+			return (false);
+	return (true);
+}
+
+/* A closed log leaves shutdown state on its owner: use a fresh process. */
+static void
+run_owner(int cpu, struct result *r)
+{
+	char path[] = "pmc-ss.XXXXXX";
+	struct kevent ke;
+	struct timespec deadline, left, now;
+	volatile uint64_t x;
+	cpuset_t set;
+	pmc_id_t ids[NPMC];
+	void *parser;
+	int fd, i, k, kq, reader;
+
+	memset(r, 0, sizeof(*r));
+	for (k = 0; k < NPMC; k++)
+		ids[k] = PMC_ID_INVALID;
+	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(r, "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(r, "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(r, "pmc_configure_logfile");
+		return;
+	}
+	r->processed = stat("kern.hwpmc.stats.intr_processed");
+	r->ignored = stat("kern.hwpmc.stats.intr_ignored");
+	for (k = 0; k < NPMC && r->error == 0; k++)
+		if (pmc_allocate(EVENT, PMC_MODE_SS, 0, cpu, &ids[k],
+		    PERIOD) != 0 || pmc_start(ids[k]) != 0)
+			fail_step(r, "allocating or starting a PMC");
+	if (r->error == 0 && ids[0] == ids[1]) {
+		errno = EPROTO;
+		fail_step(r, "two allocations returned one id");
+	}
+
+	clock_gettime(CLOCK_MONOTONIC, &deadline);
+	deadline.tv_sec += WAIT_SECONDS;
+	x = 1;
+	while (r->error == 0 && !all_sampled(r)) {
+		for (i = 0; i < 1000000; i++)
+			x = x * 1664525U + 1013904223U;
+		if (pmc_flush_logfile() != 0)
+			fail_step(r, "pmc_flush_logfile");
+		else if (consume(parser, ids, cpu, getpid(), r)) {
+			errno = EPROTO;
+			fail_step(r, "CLOSELOG while the PMCs ran");
+		}
+		clock_gettime(CLOCK_MONOTONIC, &now);
+		if (timespeccmp(&now, &deadline, >=))
+			break;
+	}
+
+	for (k = 0; k < NPMC; k++) {
+		if (ids[k] == PMC_ID_INVALID)
+			continue;
+		(void)pmc_stop(ids[k]);
+		if (pmc_release(ids[k]) != 0)
+			fail_step(r, "pmc_release");
+	}
+	if (pmc_flush_logfile() != 0 || pmc_close_logfile() != 0) {
+		fail_step(r, "closing the log");
+		return;
+	}
+	clock_gettime(CLOCK_MONOTONIC, &deadline);
+	deadline.tv_sec += WAIT_SECONDS;
+	while (!(r->closed = consume(parser, ids, cpu, getpid(), r))) {
+		clock_gettime(CLOCK_MONOTONIC, &now);
+		if (timespeccmp(&now, &deadline, >=))
+			break;
+		timespecsub(&deadline, &now, &left);
+		(void)kevent(kq, NULL, 0, &ke, 1, &left);
+	}
+	r->processed = stat("kern.hwpmc.stats.intr_processed") - r->processed;
+	r->ignored = stat("kern.hwpmc.stats.intr_ignored") - r->ignored;
+	(void)pmc_configure_logfile(-1);
+}
+
+static int
+sample(struct result *r)
+{
+	struct pmc_op_pmcallocate cfg;
+	cpuset_t mask;
+	pid_t child;
+	ssize_t n;
+	int cpu, fds[2], status;
+
+	if (pmc_init() != 0)
+		atf_tc_skip("hwpmc(4) is not loaded: %s", strerror(errno));
+	memset(&cfg, 0, sizeof(cfg));
+	if (pmc_pmu_pmcallocate(EVENT, &cfg) != 0)
+		atf_tc_skip("no %s event in the pmu-events tables", EVENT);
+
+	ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
+	    sizeof(mask), &mask) == 0);
+	cpu = CPU_FFS(&mask) - 1;
+	ATF_REQUIRE(cpu >= 0);
+
+	ATF_REQUIRE(pipe(fds) == 0);
+	child = fork();
+	ATF_REQUIRE(child >= 0);
+	if (child == 0) {
+		close(fds[0]);
+		run_owner(cpu, r);
+		n = write(fds[1], r, sizeof(*r));
+		_exit(n == (ssize_t)sizeof(*r) ? 0 : 1);
+	}
+	close(fds[1]);
+	n = read(fds[0], r, sizeof(*r));
+	close(fds[0]);
+	ATF_REQUIRE(waitpid(child, &status, 0) == child);
+	ATF_REQUIRE_MSG(n == (ssize_t)sizeof(*r) && WIFEXITED(status) &&
+	    WEXITSTATUS(status) == 0, "sampling owner failed: status %#x",
+	    status);
+	printf("cpu%d: samples %ju %ju of %ju, dropped %ju, bad %ju, "
+	    "interrupts %ju processed %ju ignored\n", cpu,
+	    (uintmax_t)r->own[0], (uintmax_t)r->own[1], (uintmax_t)r->total,
+	    (uintmax_t)r->dropped, (uintmax_t)r->bad,
+	    (uintmax_t)r->processed, (uintmax_t)r->ignored);
+	ATF_REQUIRE_MSG(r->error == 0, "%s failed: %s", r->what,
+	    strerror(r->error));
+	return (cpu);
+}
+
+ATF_TC(two_pmcs_deliver_samples);
+ATF_TC_HEAD(two_pmcs_deliver_samples, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
*** 184 LINES SKIPPED ***