Skip to content

util: Port os_nstime_to_timespec to obs-util - #1507

Merged
zackees merged 3 commits into
obs-rust:masterfrom
rdoupe:rust/port-platform-time
Oct 20, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
rdoupe:rust/port-platform-time

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Refs #262 (slice 6, time).

Stacked: builds on #1506 (the verbatim move into platform-time.c). Review only the top two commits (RED, GREEN). I'll rebase onto master once #1506 merges.

What and why

os_nstime_to_timespec turns an os_gettime_ns timestamp into wall-clock time. It uses a base pair (the wall clock, then os_gettime_ns()) that it records on its first call. This PR ports it to obs-util::platform_time, and libobs-with-rust drops util/platform-time.c.

  • Safe core (rust/obs-util/src/platform_time.rs): nstime_to_timespec(base, timestamp) follows the C step for step. At or above the base it adds the offset's seconds and nanoseconds to the base's wall time, wrapping as the C's uint64_t sums do. Below the base it borrows a second when the offset's nanoseconds are more than the base's, then subtracts. The base sits in a BaseOnce (a OnceLock, the C's pthread_once). Its static is set by the first conversion, not when the library loads. The wall clock is SystemTime::now; the monotonic clock is the ported os_gettime_ns, re-exported from the platform boundary as platform::gettime_ns (the only platform/ change).
  • Shim (src/ffi/platform_time.rs): #[repr(C)] struct timespec with the host time_t and a C long tv_nsec (32-bit on Windows). The shim records the base first, so a NULL-storage first call sets the base too, as in C. Export in rust-exports.d/obs-util-platform_time.txt, and util/platform-time.c moves into libobs.ported-platform.
  • Kept, characterized, not endorsed: the C carry is nsecs > nsecs_per_sec. So above the base, a result that lands on a whole second is tv_nsec == 1000000000 with tv_sec one second short. The port keeps it, so test_platform_time stays unchanged and passes. The fix is bug: util: os_nstime_to_timespec can return tv_nsec == 1000000000 #1505, a separate PR that changes C and Rust together.
  • Intentional difference: the C returns NULL when timespec_get fails. SystemTime::now cannot fail, so the shim returns NULL only for a NULL storage. It is listed in the parity test header and the new rust/README.md row.

RED → GREEN

  • RED commit: 66f1eae util: Test os_nstime_to_timespec against a Rust stub (RED). All 14 Tier 1 cases fail on todo!("os_nstime_to_timespec port"). The layout test passes. The parity cases abort when the panic crosses the extern "C" shim.
    • Tier 1 (tests/platform/platform_time.rs): a 1:1 port of test_platform_time.c, each case on a fresh BaseOnce, so the "first call takes the base" bounds hold even though other tests share the process. New cases use injected bases: the timestamp equal to the base, a base on a whole second (which the C test can't rule out), carry and borrow at fixed values, a wall clock before 1970, and a stable process base.

    • Tier 2 layout (platform_time_layout.rs): struct timespec size, alignment and both offsets against the C header.

    • Tier 3 (platform_time_parity.rs): oracle/platform.c gets oracle_platform_time_set_base, which runs the C's first-call setup and then replaces the base it recorded. The test then compares the real platform-time.c with:

      • the core, on an injected base: fixed bases, the whole-second edge, and a proptest (512 cases) over random bases (real-looking and up to ±2^62 s) and timestamps (anywhere, within ±3 s of the base, or on the whole-second edge);
      • the shim with the process-wide Rust base, with the oracle given the same base: fixed and random u64 timestamps, and NULL storage.

      The oracle base is one static, so the test serializes it.

  • GREEN: 99634b8 util: Port os_nstime_to_timespec to obs-util (GREEN). tests::platform::platform_time* 21/21. obs-util lib harness 609/609.
  • Mutation check, each caught by Tier 1 and Tier 3:
    • carrying with >= (the "fix"): 7 failures, among them all three parity tests on the core;
    • dropping the borrow below the base: 5;
    • not adding the offset's seconds above the base: 9.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON), each running the cmocka tests against libobs and against libobs-with-rust:
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 319/319 319/319 IDENTICAL (1766)
Rust (ENABLE_RUST_LIBOBS=ON) 319/319 319/319 IDENTICAL (1766)
  • 7 frontend tests (test_config_migration_*, test_virtual_cam_shutdown) were not built in these trees: I built only the cmocka-tests target.
  • One PipeWire test, test_pulse_monitor_reset_shared_failed_context_reset_recovers, skipped once under -j6 in the C lane against libobs-with-rust. It passes 3/3 on its own in all four runs.
  • test_platform* (5 tests): 20 repeated runs in each of the four, all passing.
  • In libobs-with-rust, init_timespec_offset is gone and os_nstime_to_timespec is the Rust one. libobs keeps the C.
  • ci/local_gate.py --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, test-binary cap, ifdef ratchet): passed. --lane cross (clippy and Dylints for x86_64-pc-windows-msvc and aarch64-apple-darwin, plus a link of every test binary): passed.
  • Real app: not applicable (no behavior change).

The rust/obs-util/src/platform/ change starts the Windows and macOS Rust and Tier 2 lanes here, which also covers #1506's move on those hosts.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

Behavior-neutral extraction before the Rust port: os_nstime_to_timespec,
its static init_timespec_offset and the timespec_offset static move
verbatim from libobs/util/platform.c.h into libobs/util/platform-time.c.h,
in the same libobs.platform unity group and precompiled header. The
oracle compiles both files. No host conditionals move.

test_platform_time characterizes os_nstime_to_timespec, which no test
called. The base pair is taken on the first call and cannot be read
back, but its wall-minus-monotonic difference can: the group setup
converts a timestamp read just before the first call, which is at or
below the base, so every case predicts its result exactly. It covers
the first call taking the base, NULL storage, timestamps around, above
and below the base, the nanosecond carry and borrow, and timestamps up
to UINT64_MAX. One case is characterized, not endorsed: above the base,
a result on a whole second comes back as tv_nsec == 1000000000 with
tv_sec one short (the carry is `>` where `>=` was meant).

Refs obs-rust#262
rust/obs-util/src/platform_time.rs holds the safe core with a todo!()
conversion, and ffi/platform_time.rs the C ABI shim with its
#[repr(C)] struct timespec. The base pair is recorded once, by the
first conversion (BaseOnce over a OnceLock, the C's pthread_once), from
SystemTime and the ported os_gettime_ns, now re-exported from the
platform boundary as platform::gettime_ns.

- Tier 1 (tests/platform/platform_time.rs): 1:1 port of
  test_platform_time.c, each case on a fresh BaseOnce, plus injected
  bases: equal to the base, a base on a whole second, the carry and
  borrow at fixed values, a wall clock before 1970, and a stable
  process base.
- Tier 2 layout (platform_time_layout.rs): struct timespec against the
  C header.
- Tier 3 (platform_time_parity.rs): oracle/platform.c gains a hook that
  replaces the C's recorded base, so the core with an injected base and
  the shim with the process base are compared with the real
  platform-time.c on fixed and random bases and timestamps.
- ENABLE_RUST_LIBOBS=ON drops util/platform-time.c (moved into
  libobs.ported-platform); export in
  rust-exports.d/obs-util-platform_time.txt.

All 14 Tier 1 cases fail on todo!("os_nstime_to_timespec port"); the
layout test passes.

Refs obs-rust#262
The core follows platform-time.c step for step. At or above the base
it adds the offset's seconds and nanoseconds to the base's wall time in
wrapping arithmetic, as the C's uint64_t sums do. Below it, it borrows
a second when the offset's nanoseconds pass the base's, then
subtracts. The carry keeps the C's `>`, so a result on a whole second
above the base is still tv_nsec == 1000000000 with tv_sec one short
(characterized, not endorsed; the fix is its own change).

The base is recorded by the first call, a NULL storage included, not
when the library loads. The wall clock is SystemTime::now, which cannot
fail, so the C's NULL return when timespec_get fails does not happen.

rust/README.md gets the row for the port.

Refs obs-rust#262
@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 06:31
@zackees
zackees enabled auto-merge October 20, 2026 06:42
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into obs-rust:master with commit 847d4db Oct 20, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants