Page MenuHomeFreeBSD

D61653.1795040812.diff
No OneTemporary

Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None

D61653.1795040812.diff

diff --git a/tests/sys/pmc/pmc_log_test.c b/tests/sys/pmc/pmc_log_test.c
--- a/tests/sys/pmc/pmc_log_test.c
+++ b/tests/sys/pmc/pmc_log_test.c
@@ -49,13 +49,18 @@
*/
#include <sys/param.h>
+#include <sys/cpuset.h>
#include <sys/socket.h>
+#include <sys/sysctl.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <pmc.h>
+#include <pmclog.h>
+#include <sched.h>
#include <signal.h>
+#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@@ -75,6 +80,9 @@
#define SAMPLE_RATE 10000
+/* Fresh log buffers that log_keeps_records_across_buffer_switches starts. */
+#define SWITCH_BUFFERS 4
+
static void
require_hwpmc(void)
{
@@ -375,6 +383,161 @@
(void)close(fd);
}
+/**
+ * @internal
+ * The owner half of log_keeps_records_across_buffer_switches: write n
+ * numbered user records from one CPU into the log on logfd, then close the
+ * log. A failed step is reported through the exit status.
+ */
+static void
+buffer_switch_owner(int logfd, int holdfd, uint32_t n)
+{
+ cpuset_t mask;
+ uint32_t i;
+ char c;
+
+ /* Log buffers are per CPU, so the records must not change CPU. */
+ CPU_SETOF(sched_getcpu(), &mask);
+
+ if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
+ sizeof(mask), &mask) != 0)
+ _exit(10);
+
+ if (pmc_init() != 0)
+ _exit(11);
+
+ if (pmc_configure_logfile(logfd) != 0)
+ _exit(12);
+
+ for (i = 0; i < n; i++) {
+ if (pmc_writelog(i) != 0)
+ _exit(13);
+ }
+
+ if (pmc_close_logfile() != 0)
+ _exit(14);
+
+ /*
+ * Exiting deconfigures the log, and that discards every buffer the
+ * writer thread has not written yet. Stay until the parent has
+ * read the whole log and closes its end of the pipe.
+ */
+ (void)read(holdfd, &c, 1);
+ _exit(0);
+}
+
+/**
+ * @internal
+ * A record that does not fit in what is left of a CPU's log buffer starts
+ * a fresh buffer. pmclog_reserve() used to return the base of the fresh
+ * buffer without moving its write pointer past the record, so the next
+ * record on that CPU overwrote it, and no counter saw the loss.
+ *
+ * A user record is 24 bytes. A buffer of S bytes holds S / 24 of them,
+ * and unless S is a multiple of 24 the next one does not fit. With the
+ * default 256 KiB, 262144 = 10922 * 24 + 16, so record 10922 starts a
+ * fresh buffer. The owner writes SWITCH_BUFFERS * (S / 24 + 1) numbered
+ * records from one CPU, 4 * 10923 = 43692 by default. They start four
+ * fresh buffers, at records 10922, 21844, 32766 and 43688, and leave four
+ * records in the last one, so the CLOSELOG record fits behind them. The
+ * log must hold every number once and in order. A kernel with the
+ * defect lost records 10922, 21845, 32768 and 43691.
+ *
+ * When S is a multiple of 24 the records fill each buffer exactly. The
+ * buffer is then handed to the writer thread when its last record is
+ * released, the next record takes a buffer through the ordinary path, and
+ * no record ever starts a fresh buffer.
+ *
+ * The owner has no PMC, so hwpmc logs nothing else for it, and the
+ * INITIALIZE record is despatched synchronously, which hands its buffer
+ * over at once. The first user record therefore starts an empty buffer.
+ *
+ * The log is a pipe, read while the owner writes, so the writer thread
+ * never holds buffers back. pmc_close_logfile() queues CLOSELOG behind
+ * every record of the owner's CPU, so reading up to it reads the whole
+ * stream, and each read(2) blocks until the next part is there. libpmc
+ * does not return CLOSELOG as an event: pmclog_read() fails with
+ * PMCLOG_EOF and leaves the record type in the event.
+ */
+ATF_TC_WITHOUT_HEAD(log_keeps_records_across_buffer_switches);
+ATF_TC_BODY(log_keeps_records_across_buffer_switches, tc)
+{
+ struct pmclog_ev ev;
+ void *parser;
+ size_t len;
+ uint32_t bufsize, n, next, reclen, value;
+ pid_t child;
+ int hold[2], kib, logp[2], status;
+ bool closed;
+
+ require_hwpmc();
+
+ len = sizeof(kib);
+ ATF_REQUIRE_MSG(sysctlbyname("kern.hwpmc.logbuffersize", &kib, &len,
+ NULL, 0) == 0 && kib > 0, "kern.hwpmc.logbuffersize: %s",
+ strerror(errno));
+
+ bufsize = 1024 * (uint32_t)kib;
+ reclen = sizeof(struct pmclog_userdata);
+
+ if (bufsize % reclen == 0)
+ atf_tc_skip("%u-byte records fill a %u-byte log buffer "
+ "exactly, so none of them starts a fresh buffer", reclen,
+ bufsize);
+
+ n = SWITCH_BUFFERS * (bufsize / reclen + 1);
+
+ ATF_REQUIRE_MSG(pipe(logp) == 0, "pipe: %s", strerror(errno));
+ ATF_REQUIRE_MSG(pipe(hold) == 0, "pipe: %s", strerror(errno));
+
+ ATF_REQUIRE((child = fork()) >= 0);
+ if (child == 0) {
+ (void)close(logp[0]);
+ (void)close(hold[1]);
+ buffer_switch_owner(logp[1], hold[0], n);
+ }
+
+ ATF_REQUIRE(close(logp[1]) == 0);
+ ATF_REQUIRE(close(hold[0]) == 0);
+
+ parser = pmclog_open(logp[0]);
+ ATF_REQUIRE_MSG(parser != NULL, "pmclog_open: %s", strerror(errno));
+
+ next = 0;
+ for (;;) {
+ ev.pl_type = 0;
+ if (pmclog_read(parser, &ev) != 0)
+ break;
+
+ if (ev.pl_type != PMCLOG_TYPE_USERDATA)
+ continue;
+
+ value = ev.pl_u.pl_u.pl_userdata;
+ if (value != next)
+ atf_tc_fail_nonfatal("expected record %u, read "
+ "record %u", next, value);
+
+ next = value + 1;
+ }
+
+ closed = ev.pl_state == PMCLOG_EOF &&
+ ev.pl_type == PMCLOG_TYPE_CLOSELOG;
+
+ /* The log is read: let the owner exit. */
+ ATF_REQUIRE(close(hold[1]) == 0);
+ ATF_REQUIRE(waitpid(child, &status, 0) == child);
+
+ ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
+ "the owner could not write the log (exit %d)",
+ WIFEXITED(status) ? WEXITSTATUS(status) : -1);
+
+ ATF_CHECK_MSG(closed, "the log ended without a CLOSELOG record");
+ ATF_CHECK_MSG(next == n, "the log ends at record %u of %u", next, n);
+
+ pmclog_close(parser);
+ (void)close(logp[0]);
+}
+
/**
* @internal
* Process exit is a teardown path of its own: the owner leaves with the PMC
@@ -447,6 +610,7 @@
ATF_TP_ADD_TC(tp, log_ops_without_an_owner);
ATF_TP_ADD_TC(tp, log_survives_userland_closing_the_fd);
ATF_TP_ADD_TC(tp, log_write_error_is_reported);
+ ATF_TP_ADD_TC(tp, log_keeps_records_across_buffer_switches);
ATF_TP_ADD_TC(tp, owner_exit_with_log_and_running_pmc);
return (atf_no_error());

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 18, 10:26 PM (15 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41812379
Default Alt Text
D61653.1795040812.diff (6 KB)

Event Timeline