Skip to contentSkip to conversation

util/qemu-timer: Honor sub-ms poll timeouts on Darwin

RFC PATCH In review Philippe Mathieu-Daudé 1 participant 4 messages Nov 19 – Nov 20 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover util/qemu-timer: Honor sub-ms poll timeouts on Darwin

This series corrects a long-standing platform limitation:
qemu_poll_ns() promises nanosecond timeouts, but Darwin’s g_poll() fallback rounds up to milliseconds, making sub-ms virtual timers fire late. Fixes the observable behavior.

Add a Darwin-only microbenchmark used to compare wait primitives and justify the chosen approach.

During the final sub-ms sleep, descriptors are not polled, so fd readiness can be delayed by up to <1 ms. This is still much better than whole-ms timer overshoot and avoids busy-waiting.

Philippe Mathieu-Daudé (2):
tests/bench: Add macos-pollbench util/qemu-timer: Honor sub-ms poll timeouts on Darwin

 tests/bench/macos-pollbench.c | 290 ++++++++++++++++++++++++++++++++++
 util/qemu-timer.c             |  27 ++++
 tests/bench/meson.build       |   6 +
 3 files changed, 323 insertions(+)
 create mode 100644 tests/bench/macos-pollbench.c
Signature · 1 line
2.55.0
#1 lore raw

RFC PATCH 1/2 tests/bench: Add macos-pollbench

Benchmark macOS wait primitives on an idle descriptor, modeling qemu_poll_ns() on a host without ppoll(2). It reports interval percentiles, CPU time per wait via getrusage(), and wakeup rate for poll with g_poll()'s rounded-up timeout, truncated poll, pselect, kevent, one-shot EVFILT_TIMER, nanosleep, a poll+nanosleep hybrid, and, with --all, poll+spin, a mach_wait_until() kicker thread, and THREAD_TIME_CONSTRAINT_POLICY variants.

Built only on Darwin with build_by_default: false. Run as ./tests/bench/macos-pollbench [period_ns] [iters] [--all]; the default period is 1.047 ms, one 9600 8N1 serial character.

Results on Apple Silicon M1 at 1.047 ms (medians of 3 runs): g_poll 2.264 ms / 11 us, hybrid 1.139 ms / 11 us, kevent 1.187 ms / 7 us, poll+spin exact but 1038 us.

AI-used-for: microbenchmark

---
 tests/bench/macos-pollbench.c | 290 ++++++++++++++++++++++++++++++++++
 tests/bench/meson.build       |   6 +
 2 files changed, 296 insertions(+)
 create mode 100644 tests/bench/macos-pollbench.c
tests/bench/macos-pollbench.c new file +290 -0
new file mode 100644
index 000000000000..a0639d1ddfb8
@@ -0,0 +1,290 @@
+/*
+ * macOS poll/sleep wait-primitive benchmark.
+ *
+ * Models qemu_poll_ns() on a host without ppoll(2): wait on an idle pipe
+ * read end for one period, repeatedly, with the timer re-armed at fire
+ * time. The poll, pselect and kevent variants watch the pipe; nanosleep,
+ * the one-shot timer and the Mach variants only sleep. Reports the
+ * interval distribution (p10/median/p90/max), the CPU time per wait
+ * (getrusage) and the wakeup rate.
+ *
+ * Variants: poll with the timeout rounded up to ms (what g_poll() does),
+ * poll truncated to ms, pselect/kevent/nanosleep with ns resolution, a
+ * one-shot EVFILT_TIMER kevent, the poll+nanosleep hybrid used by the
+ * Darwin branch of qemu_poll_ns(), plus --all extras (poll+spin,
+ * mach_wait_until() kicker thread, THREAD_TIME_CONSTRAINT_POLICY).
+ *
+ * Usage: macos-pollbench [period_ns] [iters] [--all]
+ * Default: 1047000 ns (1.047 ms, one serial character at 9600 8N1), 3000
+ * iterations.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <sys/event.h>
+#include <sys/resource.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <mach/mach.h>
+#include <mach/mach_time.h>
+#include <mach/thread_policy.h>
+#include <pthread.h>
+#include <poll.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+static int64_t now_ns(void)
+{
+    struct timespec ts;
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    return ts.tv_sec * 1000000000LL + ts.tv_nsec;
+}
+
+static int cmp(const void *a, const void *b)
+{
+    int64_t x = *(const int64_t *)a, y = *(const int64_t *)b;
+    return x < y ? -1 : x > y;
+}
+
+static int pfd[2], kq;
+
+/* poll with timeout rounded up to whole ms (g_poll / qemu_timeout_ns_to_ms) */
+static void w_poll_ms_up(int64_t ns)
+{
+    struct pollfd p = { pfd[0], POLLIN, 0 };
+    poll(&p, 1, (int)((ns + 999999) / 1000000));
+}
+
+/* poll with timeout truncated to whole ms: busy-waits below 1 ms */
+static void w_poll_ms_down(int64_t ns)
+{
+    struct pollfd p = { pfd[0], POLLIN, 0 };
+    poll(&p, 1, (int)(ns / 1000000));
+}
+
+static void w_pselect(int64_t ns)
+{
+    fd_set r;
+    FD_ZERO(&r);
+    FD_SET(pfd[0], &r);
+    struct timespec ts = { ns / 1000000000LL, ns % 1000000000LL };
+    pselect(pfd[0] + 1, &r, NULL, NULL, &ts, NULL);
+}
+
+static void w_kevent(int64_t ns)
+{
+    struct kevent ev;
+    struct timespec ts = { ns / 1000000000LL, ns % 1000000000LL };
+    kevent(kq, NULL, 0, &ev, 1, &ts);
+}
+
+static void w_nanosleep(int64_t ns)
+{
+    struct timespec ts = { ns / 1000000000LL, ns % 1000000000LL };
+    nanosleep(&ts, NULL);
+}
+
+/*
+ * poll whole ms (floor), then nanosleep the sub-ms remainder (Darwin
+ * qemu_poll_ns())
+ */
+static void w_hybrid(int64_t ns)
+{
+    int64_t start = now_ns();
+    struct pollfd p = { pfd[0], POLLIN, 0 };
+    int r = poll(&p, 1, (int)(ns / 1000000));
+    if (r == 0) {
+        int64_t rem = ns - (now_ns() - start);
+        if (rem > 0) {
+            struct timespec ts = { rem / 1000000000LL, rem % 1000000000LL };
+            nanosleep(&ts, NULL);
+        }
+    }
+}
+
+/* poll whole ms, then spin the sub-ms remainder */
+static void w_poll_spin(int64_t ns)
+{
+    int64_t end = now_ns() + ns;
+    struct pollfd p = { pfd[0], POLLIN, 0 };
+    if (ns >= 1000000) {
+        poll(&p, 1, (int)(ns / 1000000) - 1);
+    }
+    while (now_ns() < end) {
+        /* spin */
+    }
+}
+
+/* one-shot EVFILT_TIMER with ns resolution, re-armed each call */
+static void w_kevent_timer(int64_t ns)
+{
+    struct kevent ev, out;
+    EV_SET(&ev, 1 /*ident*/, EVFILT_TIMER, EV_ADD | EV_ONESHOT,
+           NOTE_NSECONDS, ns, NULL);
+    kevent(kq, &ev, 1, &out, 1, NULL);
+}
+
+/*
+ * kicker thread with mach_wait_until(); the main thread blocks on a Mach
+ * semaphore. Deadline computed in the helper each round.
+ */
+static semaphore_t sem;
+static mach_timebase_info_data_t tbi;
+static int64_t kick_period;
+/* written by main, polled by the kicker; no locking needed in a benchmark */
+static volatile int kick_run = 1;
+static pthread_t kick_thread;
+
+static void *kicker(void *arg)
+{
+    (void)arg;
+    while (kick_run) {
+        uint64_t dl = mach_absolute_time() +
+            (uint64_t)((double)kick_period * tbi.denom / tbi.numer);
+        mach_wait_until(dl);
+        semaphore_signal(sem);
+    }
+    return NULL;
+}
+
+static int mach_thread_setup(int64_t ns)
+{
+    kick_period = ns; /* set before the thread starts: kick_period=0 spins */
+    kick_run = 1;
+    return pthread_create(&kick_thread, NULL, kicker, NULL);
+}
+
+/* stop the kicker so it cannot perturb the following variants */
+static void mach_thread_teardown(void)
+{
+    kick_run = 0;
+    pthread_join(kick_thread, NULL);
+}
+
+static void w_mach_thread(int64_t ns)
+{
+    semaphore_wait(sem); /* blocking; the helper always signals */
+}
+
+/* run under THREAD_TIME_CONSTRAINT_POLICY */
+static int set_time_constraint(int64_t ns)
+{
+    thread_time_constraint_policy_data_t pol;
+    thread_port_t self = pthread_mach_thread_np(pthread_self());
+    uint64_t per = (uint64_t)((double)ns * tbi.denom / tbi.numer);
+
+    pol.period = per;
+    pol.computation = per / 2;
+    pol.constraint = per;
+    pol.preemptible = 0;
+    return thread_policy_set(self, THREAD_TIME_CONSTRAINT_POLICY,
+                             (thread_policy_t)&pol,
+                             THREAD_TIME_CONSTRAINT_POLICY_COUNT);
+}
+
+struct variant {
+    const char *name;
+    void (*wait)(int64_t);
+    int (*setup)(int64_t);      /* returns 0 on success, else variant skipped */
+    void (*teardown)(void);
+    bool extra;                 /* only run with --all */
+};
+
+static void run(struct variant *v, int64_t period, int n)
+{
+    struct rusage r0, r1;
+    int64_t *iv = malloc(n * sizeof(*iv));
+    int64_t last;
+    int64_t total = 0;
+    double cpu;
+
+    if (!iv) {
+        perror("malloc");
+        exit(1);
+    }
+
+    if (v->setup && v->setup(period)) {
+        printf("%-14s skipped: setup failed\n", v->name);
+        free(iv);
+        return;
+    }
+    getrusage(RUSAGE_SELF, &r0);
+    last = now_ns();
+    for (int i = 0; i < n; i++) {
+        v->wait(period);
+        int64_t t = now_ns();
+        iv[i] = t - last;
+        last = t;
+    }
+    getrusage(RUSAGE_SELF, &r1);
+    if (v->teardown) {
+        v->teardown();
+    }
+    cpu = (r1.ru_utime.tv_sec - r0.ru_utime.tv_sec) +
+          (r1.ru_stime.tv_sec - r0.ru_stime.tv_sec) +
+          (r1.ru_utime.tv_usec - r0.ru_utime.tv_usec) * 1e-6 +
+          (r1.ru_stime.tv_usec - r0.ru_stime.tv_usec) * 1e-6;
+    for (int i = 0; i < n; i++) {
+        total += iv[i];
+    }
+    qsort(iv, n, sizeof(*iv), cmp);
+    printf("%-14s p10 %.3f  median %.3f  p90 %.3f  max %.3f ms"
+           "  cpu %6.1f us/wait  wakes/s %8.1f\n",
+           v->name, iv[n / 10] / 1e6, iv[n / 2] / 1e6, iv[n * 9 / 10] / 1e6,
+           iv[n - 1] / 1e6, cpu / n * 1e6, n / (total / 1e9));
+    free(iv);
+}
+
+int main(int argc, char **argv)
+{
+    int64_t period = argc > 1 ? atoll(argv[1]) : 1047000;
+    int n = argc > 2 ? atoi(argv[2]) : 3000;
+    int all = argc > 3 && !strcmp(argv[3], "--all");
+    struct kevent kev;
+
+    if (argc > 4 || (argc > 3 && !all) || period <= 0 || n <= 0) {
+        fprintf(stderr, "usage: %s [period_ns>0] [iters>0] [--all]\n", argv[0]);
+        return 1;
+    }
+    /* tcon poll changes the thread policy for good: keep it last */
+    struct variant vs[] = {
+        { "poll ms-up",   w_poll_ms_up },
+        { "poll ms-down", w_poll_ms_down },
+        { "pselect ns",   w_pselect },
+        { "kevent ns",    w_kevent },
+        { "kevent-timer", w_kevent_timer },
+        { "nanosleep",    w_nanosleep },
+        { "hybrid",       w_hybrid },
+        { "poll+spin",    w_poll_spin, NULL, NULL, true },
+        { "mach-thread",  w_mach_thread, mach_thread_setup,
+          mach_thread_teardown, true },
+        { "tcon poll",    w_poll_ms_down, set_time_constraint, NULL, true },
+    };
+
+    if (pipe(pfd) < 0) {
+        return 1;
+    }
+    kq = kqueue();
+    if (kq < 0) {
+        perror("kqueue");
+        return 1;
+    }
+    EV_SET(&kev, pfd[0], EVFILT_READ, EV_ADD, 0, 0, NULL);
+    if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0) {
+        perror("kevent register");
+        return 1;
+    }
+    mach_timebase_info(&tbi);
+    semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, 0);
+    printf("period %.3f ms, %d iterations\n", period / 1e6, n);
+    for (size_t i = 0; i < sizeof(vs) / sizeof(vs[0]); i++) {
+        if (all || !vs[i].extra) {
+            run(&vs[i], period, n);
+        }
+    }
+    return 0;
+}
tests/bench/meson.build +6 -0
index 4cd7a2f6b526..740b1ba7ac3f 100644
@@ -17,6 +17,12 @@ executable('atomic64-bench',
            dependencies: [qemuutil],
            build_by_default: false)
 
+if host_os == 'darwin'
+  executable('macos-pollbench',
+             sources: files('macos-pollbench.c'),
+             build_by_default: false)
+endif
+
 benchs = {}
 
 if have_block
Signature · 1 line
2.55.0
#2 lore raw

RFC PATCH 2/2 util/qemu-timer: Honor sub-ms poll timeouts on Darwin

In reply to Philippe Mathieu-Daudé

Darwin has no ppoll(2), so qemu_poll_ns() falls back to g_poll(), which rounds timeouts up to whole milliseconds. Sub-millisecond timers on QEMU_CLOCK_VIRTUAL can then fire up to 2x late:
a 1.047 ms periodic timer was measured firing every 2.52 ms on the host clock.

Poll the floored millisecond timeout, then nanosleep() the remainder when no descriptor is ready. This avoids busy-waiting and the whole-ms rounding overshoot, at the cost of up to one millisecond of extra fd readiness latency during the final sleep. Clamp the millisecond timeout to INT32_MAX.

Microbenchmark on Apple Silicon M1 (period 1.047 ms, medians of 3 runs; median ms / CPU us per wait):

  poll ms-up (old)   2.264 / 11
  poll ms-down       1.138 / 10  (busy-waits below 1 ms)
  pselect ns         1.318 / 8
  kevent ns          1.187 / 7
  nanosleep          1.319 / 7
  hybrid (this)      1.139 / 11
  poll+spin          1.047 / 1038
  mach-thread        1.317 / 9
  kevent-timer       1.351 / 9
---
 util/qemu-timer.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
util/qemu-timer.c +27 -0
index d93a020064fe..86a97a6403af 100644
@@ -342,6 +342,33 @@ int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
         ts.tv_nsec = timeout % 1000000000LL;
         return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
     }
+#elif defined(__APPLE__)
+    /*
+     * Darwin has no ppoll(2) and g_poll() rounds the timeout up to whole
+     * milliseconds, making sub-millisecond timers fire up to 2x late.
+     * Poll with a truncated (floored) millisecond timeout, then sleep the
+     * sub-millisecond remainder with nanosleep() when no descriptor is
+     * ready, so we never busy-wait and never overshoot a full ms.
+     */
+    if (timeout < 0) {
+        return g_poll(fds, nfds, -1);
+    }
+    if (!timeout) {
+        return g_poll(fds, nfds, 0);
+    } else {
+        int64_t ms = MIN(timeout / SCALE_MS, INT32_MAX);
+        int r = g_poll(fds, nfds, ms);
+        if (r != 0) {
+            return r;
+        }
+        if (timeout % SCALE_MS > 0) {
+            struct timespec ts = { 0, timeout % SCALE_MS };
+            while (nanosleep(&ts, &ts) < 0 && errno == EINTR) {
+                /* retry with the remaining time */
+            }
+        }
+        return 0;
+    }
 #else
     return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
 #endif
Signature · 1 line
2.55.0
#3 lore raw
In reply to Philippe Mathieu-Daudé

(forgot to Cc Akihiko)

Quoted 22 lines · Philippe Mathieu-Daudé

This series corrects a long-standing platform limitation:
qemu_poll_ns() promises nanosecond timeouts, but Darwin’s g_poll() fallback rounds up to milliseconds, making sub-ms virtual timers fire late. Fixes the observable behavior.

Add a Darwin-only microbenchmark used to compare wait primitives and justify the chosen approach.

During the final sub-ms sleep, descriptors are not polled, so fd readiness can be delayed by up to <1 ms. This is still much better than whole-ms timer overshoot and avoids busy-waiting.

Philippe Mathieu-Daudé (2):
tests/bench: Add macos-pollbench util/qemu-timer: Honor sub-ms poll timeouts on Darwin

  tests/bench/macos-pollbench.c | 290 ++++++++++++++++++++++++++++++++++
  util/qemu-timer.c             |  27 ++++
  tests/bench/meson.build       |   6 +
  3 files changed, 323 insertions(+)

create mode 100644 tests/bench/macos-pollbench.c