Skip to content

libobs: os_sleepto_ns spins 1-2 ms per frame on the Windows video thread (measure, consider high-resolution timer) #1278

Closed

Description

@zackees

Part of #1273. A, intentional, cost to measure.

On Windows os_sleepto_ns sleeps ms-1, then spins on QPC with _mm_pause until the deadline, about 1–2 ms of spinning per call:

  • Rust: rust/obs-util/src/platform/windows/host.rs:653-683
  • C: libobs/util/platform-windows.c.h:321-345

Callers:

  • the video thread, libobs/obs-video.c.h:797, once per frame (at 60 fps that's up to ~6-12% of a core spent spinning);
  • the RTMP droptest, rtmp-stream.c.h:324 (debug only).

This matches upstream and buys frame-timing precision, so it isn't a bug as such. This issue is to measure it and decide.

Options: a high-resolution waitable timer (CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, Windows 10 1803+) for the whole wait, falling back to today's behaviour; or cap the spin tail.

Test / evidence: CPU time of the video thread at 60 fps idle, before and after, plus frame-time jitter (no regression in missed frames).

Activity

  1. MCPVOT commented on Oct 21, 2026

    @MCPVOT
    Contributor

    Claiming: measure the Windows spin first, then a high-resolution waitable timer with today's behaviour as the fallback.

    1. Make the spin a number. A pure wait-plan function (how long to sleep, which wait, how long to spin) tested host-neutrally in the style of sleep_duration.rs (libobs: os_sleep_ms(1) becomes Sleep(0) on Windows 8+, turning 1 ms retry loops into yield-spins #1274), plus a Windows-only measurement of process CPU time per os_sleepto_ns call at the video thread's cadence (60 fps), so the cost is recorded the way meta: Polling and busy-spin audit (CPU burn, UI stalls) #1273 asks.
    2. Baseline, then the change. Numbers on current master first, then CreateWaitableTimerExW with CREATE_WAITABLE_TIMER_HIGH_RESOLUTION on a per-thread timer handle, keeping the QPC spin for the last sub-millisecond so deadline precision does not regress. Sleep(ms-1) stays the fallback when the timer cannot be created (before Windows 10 1803).
    3. Keep the oracle honest. The same change in platform-windows.c.h so C and Rust stay identical, and the rust/README.md row plus the parity header updated if anything diverges.

    One PR. I will post the baseline measurement before the code so the decision rests on numbers, and if the measurement says the spin is not worth changing I will say that and close instead.

  2. added
    claimedAn agent or person has claimed this; check the claim comment before starting
    on Oct 21, 2026
  3. MCPVOT commented on Oct 21, 2026

    @MCPVOT
    Contributor

    Baseline on current master, Windows 11 laptop, Rust port (platform::windows::host::sleepto_ns), process CPU from GetProcessTimes, 3 x 90 calls per cadence:

    cadence CPU per call share of a core lateness p50 lateness max
    16.67 ms (60 fps) 1.273 ms 7.6 % 0.001 ms 0.230 ms
    33.33 ms (30 fps) 0.521 ms 1.6 % 0.001 ms 2.574 ms

    At 60 fps the spin is real and matches the estimate in the issue: ~1.3 ms per frame, 7.6 % of a core. Precision today is excellent (p50 lateness 1 us), so the change has to keep that.

    One methodological note: the 1 ms timer period matters. libobs sets it in DllMain; without it the test process sleeps on the default 15.6 ms tick and the spin varies run to run (0.17-0.52 ms per call). That is also why I will not put a CPU-time assertion in CI: the test will pin the plan (timer or fallback, and the spin tail length), and the CPU measurement stays as an opt-in measurement test.

    Proceeding with CreateWaitableTimerExW(CREATE_WAITABLE_TIMER_HIGH_RESOLUTION) and a 0.5 ms spin tail, Sleep(ms-1) unchanged as the fallback where the timer cannot be created.

  4. MCPVOT commented on Oct 21, 2026

    @MCPVOT
    Contributor

    I built the high-resolution timer path and measured it end to end. It makes frame pacing worse, so I am not opening a PR for it. Releasing the claim with the numbers, because the useful outcome here is the measurement.

    What I tried

    A per-thread CreateWaitableTimerExW(CREATE_WAITABLE_TIMER_MANUAL_RESET | CREATE_WAITABLE_TIMER_HIGH_RESOLUTION) timer, SetWaitableTimerEx with a relative due time, then the existing QPC spin for the tail. Sleep(ms - 1) stays the fallback where the timer cannot be created. Same algorithm, 30-300 calls at a fixed 16.67 ms deadline grid (what the video thread does), thread CPU from GetThreadTimes, 1 ms timer period set as DllMain does.

    implementation CPU per call lateness p50 p90 max
    today (Sleep(ms-1) + spin) 1.273 ms (7.6 %) 0.001 ms 0.002 ms 3.6-10.8 ms
    timer, 0.5 ms tail 0.116 ms (0.7 %) 0.56 ms 46 ms 195 ms
    timer, 1.0 ms tail ~0.6 ms 0.25-3.4 ms 8-22 ms 80 ms

    Why it loses

    The timer on its own is not accurate enough to hand the last stretch to a spin. Setting a 16.167 ms wait and timing the wake, same process:

    p50 p90 p99 max
    high-resolution timer wake +0.33 ms +0.76 ms +2.0 ms +3.0 ms
    normal timer (control) 25.8 ms for a 16.2 ms wait

    The overshoot is 0.33 ms at the median and up to 3 ms in the tail. To leave no lateness the spin tail has to cover that tail, and a 3 ms spin costs more than the 1.3 ms it replaces. Any tail small enough to win on CPU is shorter than the overshoot, so calls land late, and on a fixed deadline grid the miss accumulates. That is the whole effect: the timer does not remove the spin, it just moves it earlier and adds jitter.

    Recommendation

    Keep C's Sleep(ms - 1) + spin. It is well tuned for an absolute deadline: the sleep gets close, and the spin is the only thing that can hit the deadline exactly.

    The CPU can only come back by giving up precision, which is a product decision rather than a port fix: cap the spin (say 0.3 ms) and return up to ~0.7 ms late, or drop the spin and accept ~1 ms jitter. At 60 fps that buys back about 5 % of a core on the video thread. If that trade is acceptable for the video thread, I am happy to implement it as a separate, explicit change with the same measurement. If not, I would close this as measured and leave it as is.

    Nothing is pushed; the tree is back to C's behaviour.

  5. removed
    claimedAn agent or person has claimed this; check the claim comment before starting
    on Oct 23, 2026
  6. zackees commented on Oct 25, 2026

    @zackees
    MemberAuthor

    Decided: keep os_sleepto_ns as C has it (sleep, then spin to the deadline) and close this as measured.
    Reasons:

    • The issue's own framing: this matches upstream and buys frame-timing precision, so it isn't a bug.
    • @MCPVOT's measurement above: the high-resolution waitable timer made frame pacing worse. The other options (cap the spin, or drop it) trade away the frame-timing precision that libobs's video thread depends on, for about 5% of a core.
    • The port's rule is behavioural parity with the C, checked by differential tests. A deliberate precision change would be a product change of its own, not a port fix.
      If someone wants the capped-spin trade-off, open it as a new proposal with frame-pacing measurements at 60 fps and higher. Thanks for measuring instead of guessing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions