Repository navigation
util: Port os_nstime_to_timespec to obs-util - #1507
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #262 (slice 6, time).
What and why
os_nstime_to_timespecturns anos_gettime_nstimestamp into wall-clock time. It uses a base pair (the wall clock, thenos_gettime_ns()) that it records on its first call. This PR ports it toobs-util::platform_time, andlibobs-with-rustdropsutil/platform-time.c.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'suint64_tsums 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 aBaseOnce(aOnceLock, the C'spthread_once). Itsstaticis set by the first conversion, not when the library loads. The wall clock isSystemTime::now; the monotonic clock is the portedos_gettime_ns, re-exported from the platform boundary asplatform::gettime_ns(the onlyplatform/change).src/ffi/platform_time.rs):#[repr(C)] struct timespecwith the hosttime_tand a Clongtv_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 inrust-exports.d/obs-util-platform_time.txt, andutil/platform-time.cmoves intolibobs.ported-platform.nsecs > nsecs_per_sec. So above the base, a result that lands on a whole second istv_nsec == 1000000000withtv_secone second short. The port keeps it, sotest_platform_timestays 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.timespec_getfails.SystemTime::nowcannot fail, so the shim returns NULL only for a NULLstorage. It is listed in the parity test header and the newrust/README.mdrow.RED → GREEN
util: Test os_nstime_to_timespec against a Rust stub (RED). All 14 Tier 1 cases fail ontodo!("os_nstime_to_timespec port"). The layout test passes. The parity cases abort when the panic crosses theextern "C"shim.Tier 1 (
tests/platform/platform_time.rs): a 1:1 port oftest_platform_time.c, each case on a freshBaseOnce, 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 timespecsize, alignment and both offsets against the C header.Tier 3 (
platform_time_parity.rs):oracle/platform.cgetsoracle_platform_time_set_base, which runs the C's first-call setup and then replaces the base it recorded. The test then compares the realplatform-time.cwith:u64timestamps, and NULL storage.The oracle base is one static, so the test serializes it.
util: Port os_nstime_to_timespec to obs-util (GREEN).tests::platform::platform_time*21/21. obs-util lib harness 609/609.>=(the "fix"): 7 failures, among them all three parity tests on the core;ENABLE_RUST_LIBOBS=OFFandON), each running the cmocka tests againstlibobsand againstlibobs-with-rust:ENABLE_RUST_LIBOBS=OFF)ENABLE_RUST_LIBOBS=ON)test_config_migration_*,test_virtual_cam_shutdown) were not built in these trees: I built only thecmocka-teststarget.test_pulse_monitor_reset_shared_failed_context_reset_recovers, skipped once under-j6in the C lane againstlibobs-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.libobs-with-rust,init_timespec_offsetis gone andos_nstime_to_timespecis the Rust one.libobskeeps 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 forx86_64-pc-windows-msvcandaarch64-apple-darwin, plus a link of every test binary): passed.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
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): not applicable