Page MenuHomeFreeBSD

D62355.1796484264.diff
No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None

D62355.1796484264.diff

diff --git a/tests/sys/pmc/Makefile b/tests/sys/pmc/Makefile
--- a/tests/sys/pmc/Makefile
+++ b/tests/sys/pmc/Makefile
@@ -8,6 +8,7 @@
ATF_TESTS_C+= pmc_log_test
ATF_TESTS_C+= pmc_pidtrack_test
ATF_TESTS_C+= pmc_row_test
+ATF_TESTS_C+= pmc_soft_test
ATF_TESTS_C+= pmc_ss_test
ATF_TESTS_C+= pmc_unload_test
ATF_TESTS_C+= pmc_wrap_test
@@ -34,6 +35,7 @@
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_soft_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"
diff --git a/tests/sys/pmc/pmc_soft_test.c b/tests/sys/pmc/pmc_soft_test.c
new file mode 100644
--- /dev/null
+++ b/tests/sys/pmc/pmc_soft_test.c
@@ -0,0 +1,157 @@
+/*
+ * 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/mman.h>
+#include <sys/pmc.h>
+#include <sys/sysctl.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pmc.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+#define EVENT "PAGE_FAULT.ALL"
+#define NPAGES 8192
+#define ROUNDS 10
+#define MINCOUNT "kern.hwpmc.mincount"
+#define SAVED "mincount.saved"
+
+static uint64_t
+stat(const char *name)
+{
+ uint64_t v;
+ size_t len;
+
+ len = sizeof(v);
+ ATF_REQUIRE_MSG(sysctlbyname(name, &v, &len, NULL, 0) == 0,
+ "sysctl %s: %s", name, strerror(errno));
+ return (v);
+}
+
+/* Take NPAGES page faults in the kernel, copying out to fresh pages. */
+static void *
+fault(int zero)
+{
+ size_t len;
+ void *p;
+
+ len = NPAGES * getpagesize();
+ p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
+ -1, 0);
+ ATF_REQUIRE(p != MAP_FAILED);
+ ATF_REQUIRE(read(zero, p, len) == (ssize_t)len);
+ return (p);
+}
+
+static void
+unmap(void *p)
+{
+ ATF_REQUIRE(munmap(p, NPAGES * getpagesize()) == 0);
+}
+
+ATF_TC_WITH_CLEANUP(ss_stop_after_stall);
+ATF_TC_HEAD(ss_stop_after_stall, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Stopping a stalled system-mode soft PMC must not leave its "
+ "event enabled");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(ss_stop_after_stall, tc)
+{
+ cpuset_t mask;
+ pmc_id_t id;
+ uint64_t full, ignored;
+ u_int min, one;
+ size_t len;
+ void *p;
+ int cpu, fd, i, log, zero;
+ bool stalled;
+
+ if (pmc_init() != 0)
+ atf_tc_skip("hwpmc(4) is not loaded: %s", strerror(errno));
+
+ /* One sample per fault; the default minimum count is 1000. */
+ len = sizeof(min);
+ ATF_REQUIRE(sysctlbyname(MINCOUNT, &min, &len, NULL, 0) == 0);
+ ATF_REQUIRE((fd = open(SAVED, O_WRONLY | O_CREAT, 0644)) >= 0);
+ ATF_REQUIRE(write(fd, &min, sizeof(min)) == sizeof(min));
+ ATF_REQUIRE(close(fd) == 0);
+ one = 1;
+ ATF_REQUIRE(sysctlbyname(MINCOUNT, NULL, NULL, &one,
+ sizeof(one)) == 0);
+
+ 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_SETOF(cpu, &mask);
+ ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
+ sizeof(mask), &mask) == 0);
+
+ ATF_REQUIRE((zero = open("/dev/zero", O_RDONLY)) >= 0);
+ ATF_REQUIRE((log = open("/dev/null", O_WRONLY)) >= 0);
+ ATF_REQUIRE_MSG(pmc_configure_logfile(log) == 0,
+ "pmc_configure_logfile: %s", strerror(errno));
+
+ stalled = false;
+ for (i = 0; i < ROUNDS; i++) {
+ ATF_REQUIRE_MSG(pmc_allocate(EVENT, PMC_MODE_SS, 0, cpu, &id,
+ 1) == 0, "pmc_allocate(%s): %s", EVENT, strerror(errno));
+ ATF_REQUIRE(pmc_start(id) == 0);
+
+ /*
+ * Faulting faster than hardclock drains fills the buffer.
+ * Stop right away, while the PMC is likely still stalled.
+ */
+ full = stat("kern.hwpmc.stats.intr_bufferfull");
+ p = fault(zero);
+ ATF_REQUIRE(pmc_stop(id) == 0);
+ if (stat("kern.hwpmc.stats.intr_bufferfull") != full)
+ stalled = true;
+ unmap(p);
+ ATF_REQUIRE(pmc_release(id) == 0);
+
+ /* With no PMC left, the event must not reach hwpmc. */
+ ignored = stat("kern.hwpmc.stats.intr_ignored");
+ unmap(fault(zero));
+ ATF_REQUIRE_MSG(
+ stat("kern.hwpmc.stats.intr_ignored") - ignored < NPAGES / 2,
+ "%s still enabled after round %d", EVENT, i);
+ }
+ (void)pmc_configure_logfile(-1);
+
+ if (!stalled)
+ atf_tc_skip("the sample buffer never filled");
+}
+
+ATF_TC_CLEANUP(ss_stop_after_stall, tc)
+{
+ u_int min;
+ int fd;
+
+ if ((fd = open(SAVED, O_RDONLY)) < 0)
+ return;
+ if (read(fd, &min, sizeof(min)) == sizeof(min))
+ (void)sysctlbyname(MINCOUNT, NULL, NULL, &min, sizeof(min));
+ (void)close(fd);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, ss_stop_after_stall);
+
+ return (atf_no_error());
+}

File Metadata

Mime Type
text/plain
Expires
Sat, Dec 5, 3:24 PM (14 h, 44 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42210817
Default Alt Text
D62355.1796484264.diff (4 KB)

Event Timeline