From nobody Mon Nov 16 20:26:49 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jSzt20fl8z70qxZ for ; Mon, 16 Nov 2026 20:26:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jSzt15LV0z3Hrv for ; Mon, 16 Nov 2026 20:26:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794860809; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XOZr5Y1aKP53hP8DvVCaWD53jLvLvgG/F9QRHkQtNrg=; b=h4nyxV+aLfBAlB0+Taz42GZGZ70tgBdMsio2AfdODl5nkzd3E2ZooFDMppbXq3q8W7EWes wxnfOf73VLJQsBhBc3VgLEuXHaBRGhsPeTImmdW7WCoSUfGPel+qRGATyQoYn91/V8Elzo +sBuUlY34LYcFiRkIb2Hf3F9SPotmA7ih5LAeJy5G41t3VU6qrS+hvC/z9poGlutUHzr1Q jp8F5T6Yc8ooM4DwUXlCu05nWCBAZnVlV7QhSSnP50hWeJzbbH94dcjdg5iOnyyU/C1sI2 U5itAs5EnW4t//r35MOoL2Xkq53hvIG6i0PpvR/OT8nf5n9R4lJ/ECw/CrjbZg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794860809; b=WPtyBDhVpM06iq7mQg1uWvM/KpqmfobBsVib7LNxqUxb9CY964GJSqVOSwsv7ryqYMU6lR ucgX9hP5rS7F/nrJrWumK9lI3tlA5Cp7uR709dzoNlk2yRyrxnsv6ykUfljuyAHAB39VmA mz7qgqzGgI5uLBcwjxMvZ4zbGZq2DEnkPRAaLZbhrHxIoEvA/q7s5n4lOL69kgtSwXLb9j c4Xsgr2oMlCxwPabt58/lj+HwkQgEQoUJKgLV9iqEAZo4DgiPyq0OEy8IZkLrX2/YG+1Wx w8c0V16ghRAyUXSbvbV+qFEGjoyybTX+sXMXq6CWoQTDNyExFSiQYI0EBWWEpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794860809; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XOZr5Y1aKP53hP8DvVCaWD53jLvLvgG/F9QRHkQtNrg=; b=ler9q1p06d2gg4OnU3qnaCGyfOtq+nZjljEFRazN2bKCn69ilZcH2VppR9NhICJVfi8PrU roP21SwDQERau/3X+3ud/RbxWMWgXnZmPHwFhtZx+T2jqdP7gILJVJfTcOSyvk1OvWt8d7 i7ivBtdGN/KE5TB8fEYeMIOMKo7suusToCgvURp0Np7ACMz7AVwSYf0TVCLgstPh+u5XRi bDtfs6PbiLCp2vipEoy8fz02bWMx/YADMz3b5qEeRa+ZGP//32BLnBuLRQWjQ4OH+sJF5v 6FhcgCOufcWRGRS9h7IVJUhMBXLfhrnQzbOln7s6FDcK8CcmK7f94v5DyL2XNg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jSzt14QpHz18HG for ; Mon, 16 Nov 2026 20:26:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 4ac3 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 16 Nov 2026 20:26:49 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: d546356523f3 - main - tests/sys/pmc: add PERF, RDPMC, row, sampling and unload tests List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d546356523f3e4856611fe25e0d9c8cf34ba1941 Auto-Submitted: auto-generated Date: Mon, 16 Nov 2026 20:26:49 +0000 Message-Id: <6afb6709.4ac3.678dd71c@gitrepo.freebsd.org> The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=d546356523f3e4856611fe25e0d9c8cf34ba1941 commit d546356523f3e4856611fe25e0d9c8cf34ba1941 Author: Mitchell Horne AuthorDate: 2026-11-16 20:15:56 +0000 Commit: Mitchell Horne 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 Signed-off-by: Davi C.A 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 + * Author: Osvaldo Janeri Filho + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#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 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include + +#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 + * Author: Osvaldo Janeri Filho + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +#include +#include +#include +#include + +#include + +/* 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 + * Author: Osvaldo Janeri Filho + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 ***