diff --git a/Cargo.lock b/Cargo.lock index 4387ae5f067..bd9ddcf6819 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4430,6 +4430,7 @@ dependencies = [ "moq-archive", "moq-audio", "moq-auth 0.2.1", + "moq-clock", "moq-hls", "moq-json", "moq-msf", @@ -4505,6 +4506,7 @@ dependencies = [ "kio 0.6.2", "libc", "moq-audio", + "moq-clock", "moq-json", "moq-mux", "moq-net", @@ -4634,6 +4636,7 @@ dependencies = [ "hang", "kio 0.6.2", "memchr", + "moq-clock", "moq-flate", "moq-json", "moq-loc", @@ -4652,7 +4655,6 @@ dependencies = [ "tracing", "tracing-test", "url", - "web-async", "webm-iterable", ] @@ -4672,7 +4674,7 @@ dependencies = [ "kio 0.6.2", "libc", "loom", - "moq-net-sim", + "moq-clock", "moq-pattern 0.1.2", "num_enum", "rand 0.10.3", @@ -4685,7 +4687,6 @@ dependencies = [ "tracing", "uuid", "wasm-bindgen-test", - "web-async", ] [[package]] @@ -4696,19 +4697,6 @@ dependencies = [ "moq-net", ] -[[package]] -name = "moq-net-sim" -version = "0.0.0" -dependencies = [ - "futures", - "kio 0.6.2", - "moq-net-sim-macros", -] - -[[package]] -name = "moq-net-sim-macros" -version = "0.0.0" - [[package]] name = "moq-nvenc" version = "0.1.2" @@ -4835,12 +4823,12 @@ version = "0.2.11" dependencies = [ "kio 0.6.2", "moq-auth 0.2.1", + "moq-clock", "moq-json", "moq-net", "moq-tokio", "thiserror 2.0.21", "tokio", - "web-async", ] [[package]] @@ -4942,6 +4930,7 @@ version = "0.2.12" dependencies = [ "criterion", "futures", + "moq-clock", "moq-json", "moq-net", "serde", diff --git a/Cargo.toml b/Cargo.toml index e614cffd4bd..8da65c95389 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,8 +24,6 @@ members = [ "rs/moq-native", "rs/moq-net", "rs/moq-net/fuzz", - "rs/moq-net/sim", - "rs/moq-net/sim/macros", "rs/moq-nvenc", "rs/moq-pattern", "rs/moq-quic", diff --git a/quest/m1/spinning-loops.md b/quest/m1/spinning-loops.md index fb95d670386..31cdbb50461 100644 --- a/quest/m1/spinning-loops.md +++ b/quest/m1/spinning-loops.md @@ -34,10 +34,6 @@ Decided 2026-10-09 while reworking #5088: datagram backlog after `TrackServe` yields, and its drain arm would skip datagrams after a yield unless it returns `Poll`. -## Required - -- [moq-net on moq-clock](/quest/m1/time/net.md) - moq-net's tests reach the moq-clock sim, which hosts the poll counter - ## Related - [Session burst hang](https://github.com/moq-dev/moq/pull/5238) - the same bench shapes that hang diff --git a/quest/m1/time/README.md b/quest/m1/time/README.md index e0eccd34f4f..05d80766129 100644 --- a/quest/m1/time/README.md +++ b/quest/m1/time/README.md @@ -63,7 +63,7 @@ reasons, so later sessions don't reopen them: dev-dependency only, so production builds never compile it). It is one backend of the ambient clock, starts at zero, keeps deadlock detection, and seeds the later runtime trait. `#[moq_clock::test]` replaces - `#[moq_net_sim::test]`. + `#[moq_clock::test]`. - **Tests for migrated contracts use the sim wherever possible.** A test with no real I/O that cannot run on the sim yet uses paused tokio, whose clock `moq-clock` reads. A real-I/O test never uses paused tokio, which auto-advances to the @@ -95,7 +95,6 @@ cleanup and the global lint ratchet belong to ## Required -- [moq-net on moq-clock](/quest/m1/time/net.md) - moq-net, moq-mux, moq-stats, moq-room, and moq-ffi run on moq-clock and moq-net-sim is gone - [Every crate on moq-clock](/quest/m1/time/crates.md) - every remaining crate reads time only through moq-clock, with controlled tests for the production/public API migrations - [moq-uring on moq-clock](/quest/m1/time/uring.md) - one clock read per worker turn and moq-clock's deadline queue replace the private timer heap - [The relay seam](/quest/m1/time/relay-seam.md) - one in-memory session and a generic `moq_relay::Connection`, so relay tests run on the sim diff --git a/quest/m1/time/crates.md b/quest/m1/time/crates.md index f3d180c45b0..cf6a7e6327c 100644 --- a/quest/m1/time/crates.md +++ b/quest/m1/time/crates.md @@ -35,10 +35,6 @@ workspace-wide lint ratchet follow in m2. Public API: breaking wherever a crate exposes an instant or a time source. Wire: none. -## Required - -- [moq-net on moq-clock](/quest/m1/time/net.md) - these crates consume moq-net's instant - ## Related - [Time test sweep](/quest/m2/time-test-sweep.md) - unrelated test-only/private clock cleanup and global lint are deferred; migrate production/public APIs and their required regression tests here diff --git a/quest/m1/time/net.md b/quest/m1/time/net.md deleted file mode 100644 index 7cef980c8b1..00000000000 --- a/quest/m1/time/net.md +++ /dev/null @@ -1,61 +0,0 @@ -# [L] moq-net on moq-clock - -## Goal - -moq-net, moq-mux, moq-stats, moq-room, and moq-ffi read time and -arm timers only through `moq-clock`: - -- `moq_net::time::Instant` is `moq_clock::Instant`, and moq-net's private - `Clock`/`Deadline` are gone in favor of moq-clock's deadline queue. -- `model/clock.rs` reads wall time through moq-clock, so `Timestamp::now` - follows a test's `Manual` or sim clock. Its anchor (today a process-wide - `LazyLock` of instant and wall offset) moves into the ambient clock, so each - clock gets its own and the first test cannot fix it for later ones; a - `step_wall()` within one clock keeps it. Test sequential clocks that start - at different wall times. -- `rs/moq-net/sim` is deleted; moq-net's tests run on `#[moq_clock::test]`. -- `moq_mux::Clock` stays a media mapping (monotonic instant to PTS, plus the - wall time of PTS zero) and consumes moq-clock; `arrival()` and `placed()` - read the ambient clock instead of `SystemTime::now()`. -- These crates' `web_async::time` uses move to moq-clock; web-async remains - only for spawn. - -## Plan - -Breaking; it lands on `main` after the crate (see [the line's README](/quest/m1/time/README.md)). -The ambient functions sit behind moq-net's `async` feature; the core takes -explicit instants. The migration preserves that boundary rather than exposing -ambient time to the sans-IO core. -Coordinate clock origin, units, and controlled-time behavior with the -[browser backend](/quest/m2/rs2ts/README.md) if it has landed. Source-level -`Instant` mappings depend on the selected backend; WASM retains Rust time types. - -What moq-clock landed with, mapped onto moq-net: `time::Clock` becomes -`moq_clock::Deadlines` (built with its first `now`, so no `try_now`), and -`Clock::sim()` becomes `sim::attach(&deadlines)`, which keeps the queue in -step until it drops. `time::run` and `run_sim` both become `moq_clock::drive`. -`moq_net_sim::{now, sleep, sleep_until, timeout}` become the ambient -`moq_clock` functions; `spawn`, `advance`, and `yield_now` move to -`moq_clock::sim`. The sim's and `Manual`'s wall clock start at the fixed -`Manual::WALL_START` (2025-01-01 UTC), so moq-mux sees a set host clock -without any setup. `moq_clock::Wall` is wall time; `moq_net::Timestamp` -stays media time. - -moq-mux's paused-clock test (`paused_clock_preserves_native_boundaries`) moves -to the sim or `Manual`, and gains a `step_wall` case: a wall step changes the -ambient wall clock, but an existing `Clock`'s PTS-zero wall mapping and its -PTS stay put, as `wall_mapping_survives_a_system_clock_adjustment` requires -(archived records keep their wall times); only a newly created mapping -samples the corrected wall time. The anchor's random jitter stays as is; -[seeded sim](/quest/m2/sim-seed.md) owns randomness. - -Ban direct clock reads and timers in these crates (the ratchet). Update -`rs/moq-net/AGENTS.md`'s test rule and every doc that names `moq_net_sim`. -Report the API break in the PR. - -Public API: breaking. Every public signature in these crates that takes or -returns `std::time::Instant` moves to `moq_clock::Instant`, since a logical -instant has no general conversion back: `time::Instant`, `moq_mux::Clock`, -and moq-mux's `flush`/`update` family among them (`container::Producer`, -`catalog::Estimator`, `rate::Control`, the codec and import producers). The -PR enumerates each break and migrates its in-tree callers. Wire: none. diff --git a/quest/m1/time/relay-seam.md b/quest/m1/time/relay-seam.md index a0aec704b69..ac495d34462 100644 --- a/quest/m1/time/relay-seam.md +++ b/quest/m1/time/relay-seam.md @@ -21,7 +21,3 @@ types as the switch to `moq-quic` (#5664) left them. Simulated UDP belongs to QUIC on the sim. Public API: the remaining relay seam. Wire: none. - -## Required - -- [moq-net on moq-clock](/quest/m1/time/net.md) - the sim and instant the relay tests run on diff --git a/quest/m1/time/uring.md b/quest/m1/time/uring.md index 2e21264c777..b9017fef0a2 100644 --- a/quest/m1/time/uring.md +++ b/quest/m1/time/uring.md @@ -33,10 +33,6 @@ Public API: breaking. `moq_uring::Timer` is re-exported, and `Timer::set` takes `Option` instead of std's; no conversion shim. Wire: none. -## Required - -- [moq-net on moq-clock](/quest/m1/time/net.md) - the driver's instant becomes moq-clock's - ## Closes - [#3122](https://github.com/moq-dev/moq/issues/3122) - close this issue when the quest finishes diff --git a/quest/m2/sim-seed.md b/quest/m2/sim-seed.md index db3119b7ac9..12a09939ccd 100644 --- a/quest/m2/sim-seed.md +++ b/quest/m2/sim-seed.md @@ -12,7 +12,3 @@ replays bit-exact. Decided 2026-10-10: out of [controlled time](/quest/m1/time/README.md); with controlled time a test advances past the largest jitter, so seeding only adds exact replay. Production keeps its OS randomness. - -## Required - -- [moq-net on moq-clock](/quest/m1/time/net.md) - the sim and its first jitter consumer diff --git a/rs/AGENTS.md b/rs/AGENTS.md index e38f7042351..094eb379e0a 100644 --- a/rs/AGENTS.md +++ b/rs/AGENTS.md @@ -50,7 +50,7 @@ Prefer poll. New logic is a `poll_*` with an `async` helper, not the other way a # Testing -- Tests are inline `#[cfg(test)] mod tests`, except full Client/Server session tests over a crate's shared mock harness, which go in its `tests/integration/` module. Time-dependent async tests call `tokio::time::pause()` first (moq-net uses `#[moq_net_sim::test]` instead), unless they cross real networking that can't be mocked (sockets, smoke tests); those run on the wall clock and assert lower bounds. +- Tests are inline `#[cfg(test)] mod tests`, except full Client/Server session tests over a crate's shared mock harness, which go in its `tests/integration/` module. Time-dependent async tests call `tokio::time::pause()` first (moq-net uses `#[moq_clock::test]` instead), unless they cross real networking that can't be mocked (sockets, smoke tests); those run on the wall clock and assert lower bounds. - Run tests through `just` (nextest), not `cargo test`: nextest kills a wedged test as TIMEOUT, cargo hangs forever. A test flagged SLOW is a bug to fix, not a threshold to raise. - `just check` compiles default features only, like CI. `just rs features` (nightly) covers `--all-features` / `--no-default-features`. Keep a feature gate around the dependency, not the logic, so the logic's tests stay in the merge gate. - Local checks compile only the host platform; PR CI runs `just rs windows` / `macos` on those hosts, and `just rs wasm` covers `moq-wasm`. diff --git a/rs/justfile b/rs/justfile index f8c3094c79f..e741e7ccc6d 100644 --- a/rs/justfile +++ b/rs/justfile @@ -196,9 +196,11 @@ android *args: wasm *args: cargo clippy --locked -p moq-wasm -p moq-mux -p moq-ffi -p moq-clock --target wasm32-unknown-unknown --lib "$@" -- -D warnings -# Run the browser clock backend on the Playwright Chromium installed by wasm.yml. +# Run the browser clock backend on the Playwright Chromium installed by wasm.yml, +# with the runtime and with reads alone. wasm-test: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER="bun {{ justfile_directory() }}/test/wasm/rust.ts" cargo test --locked -p moq-clock --lib --target wasm32-unknown-unknown + CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER="bun {{ justfile_directory() }}/test/wasm/rust.ts" cargo test --locked -p moq-clock --lib --target wasm32-unknown-unknown --no-default-features # Auto-fix the browser/WASM bindings. [positional-arguments] diff --git a/rs/moq-audio/src/capture.rs b/rs/moq-audio/src/capture.rs index 764c2b24a1b..41ddc4a2752 100644 --- a/rs/moq-audio/src/capture.rs +++ b/rs/moq-audio/src/capture.rs @@ -18,9 +18,10 @@ //! in that same callback so the buffers leaving here are already clean. use std::task::Poll; -use std::time::{Duration, Instant}; +use std::time::Duration; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use moq_net::time::Instant; use ringbuf::traits::Producer; use crate::Error; diff --git a/rs/moq-audio/src/capture/buffer.rs b/rs/moq-audio/src/capture/buffer.rs index cdb05514bbb..d5a63ced111 100644 --- a/rs/moq-audio/src/capture/buffer.rs +++ b/rs/moq-audio/src/capture/buffer.rs @@ -2,8 +2,9 @@ use std::sync::Arc; use std::sync::atomic::{AtomicU64, Ordering}; -use std::time::{Duration, Instant}; +use std::time::Duration; +use moq_net::time::Instant; use ringbuf::traits::{Consumer, Observer, Producer, Split}; use ringbuf::{CachingCons, CachingProd, HeapCons, HeapProd, HeapRb}; use tokio::sync::mpsc; diff --git a/rs/moq-audio/src/encode/capture.rs b/rs/moq-audio/src/encode/capture.rs index a50580e258e..8a7e2e41df3 100644 --- a/rs/moq-audio/src/encode/capture.rs +++ b/rs/moq-audio/src/encode/capture.rs @@ -3,8 +3,9 @@ use std::fmt; use std::sync::Arc; use std::task::Poll; -use std::time::{Duration, Instant}; +use std::time::Duration; +use moq_net::time::Instant; use rand::RngExt; use moq_mux::catalog::hang::CatalogExt; @@ -2107,7 +2108,7 @@ mod tests { /// the broadcast clock's own correlation, so the published timestamp is exactly that /// instant however late the read happens. mod clock { - use std::time::{Duration, Instant, SystemTime}; + use std::time::{Duration, SystemTime}; use super::*; diff --git a/rs/moq-audio/src/encode/producer.rs b/rs/moq-audio/src/encode/producer.rs index 4f3650f31c3..9410af9f6ab 100644 --- a/rs/moq-audio/src/encode/producer.rs +++ b/rs/moq-audio/src/encode/producer.rs @@ -1,7 +1,5 @@ //! Encode raw PCM and publish it as a moq audio track. -use std::time::Instant; - use bytes::Bytes; use moq_mux::catalog::hang::CatalogExt; @@ -442,7 +440,7 @@ impl Producer { if self.grouped >= self.group_samples { self.track.cut(None)?; } - self.track.flush(timestamp, Instant::now())?; + self.track.flush(timestamp, moq_net::time::now())?; Ok(()) } @@ -468,7 +466,7 @@ impl Producer { keyframe: false, duration: None, })?; - track.flush(timestamp, Instant::now())?; + track.flush(timestamp, moq_net::time::now())?; } track.cut(Some(terminal.end))?; diff --git a/rs/moq-c/src/publish.rs b/rs/moq-c/src/publish.rs index 4cd8b2c1a60..03754bd3542 100644 --- a/rs/moq-c/src/publish.rs +++ b/rs/moq-c/src/publish.rs @@ -283,7 +283,7 @@ impl Publish { /// unless their caller explicitly identifies the frame as encoder output. pub fn media_flush(&mut self, media: Id, timestamp: hang::container::Timestamp) -> Result<(), Error> { let track = self.media.get_mut(media).ok_or(Error::MediaNotFound)?; - track.flush(timestamp, std::time::Instant::now())?; + track.flush(timestamp, moq_net::time::now())?; Ok(()) } diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index 56a1baf5f6e..7bbdacde9ea 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -15,7 +15,7 @@ use std::ffi::{c_char, c_void}; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, OnceLock}; -use std::time::{Duration, Instant}; +use std::time::Duration; use tokio::sync::oneshot; @@ -232,7 +232,7 @@ async fn follow_reservation( max = next; control = Control::new(Policy::new(max)); } - let Some(bitrate) = control.update(estimate, Instant::now()) else { + let Some(bitrate) = control.update(estimate, moq_net::time::now()) else { continue; }; diff --git a/rs/moq-cli/Cargo.toml b/rs/moq-cli/Cargo.toml index 14401ce2245..14bbd7016de 100644 --- a/rs/moq-cli/Cargo.toml +++ b/rs/moq-cli/Cargo.toml @@ -106,6 +106,7 @@ humantime = { workspace = true } moq-archive = { workspace = true } moq-audio = { workspace = true, optional = true, features = ["aac"] } moq-auth = { workspace = true, features = ["serve"] } +moq-clock = { workspace = true } # `server` enables the HTTP egress server for `moq export hls`; the importer is always available. moq-hls = { workspace = true, features = ["server"] } # Names the MSF catalog track `export archive` records. diff --git a/rs/moq-cli/src/play/fake.rs b/rs/moq-cli/src/play/fake.rs index 6c2f00feed2..9b88db5c0f8 100644 --- a/rs/moq-cli/src/play/fake.rs +++ b/rs/moq-cli/src/play/fake.rs @@ -1,12 +1,12 @@ //! A deviceless [`Output`] for tests: it records what the speaker would have -//! played and when, on tokio's clock, so a paused runtime measures playback +//! played and when, on the ambient clock, so a paused runtime measures playback //! without waiting on it. use std::sync::{Arc, Mutex}; use std::time::Duration; use moq_audio::playback::Input; -use tokio::time::Instant; +use moq_clock::Instant; use super::output::{Output, Sink, Speaker}; use super::window::Event; @@ -43,7 +43,7 @@ impl Recorder { pub fn present(&self, media: &super::media::Media) -> Option { let mut video = media.video.lock().unwrap(); let presentation = media.presentation.lock().unwrap(); - let now = Instant::now().into_std(); + let now = moq_clock::now(); let mut shown = None; while video .front() @@ -116,7 +116,7 @@ pub(super) struct FakeSink { impl Sink for FakeSink { async fn finish(self) { - tokio::time::sleep_until(self.end).await; + moq_clock::sleep_until(self.end).await; } fn write(&mut self, samples: &[u8]) -> anyhow::Result<()> { diff --git a/rs/moq-cli/src/play/media.rs b/rs/moq-cli/src/play/media.rs index d0a19b62c85..362fd430ee4 100644 --- a/rs/moq-cli/src/play/media.rs +++ b/rs/moq-cli/src/play/media.rs @@ -9,9 +9,9 @@ use anyhow::Context; use hang::moq_net; use moq_mux::catalog::{self, Stream}; use moq_net::announce::Event as AnnounceEvent; -// tokio's clock, which is the wall clock unless a test pauses it to drive the +// The ambient clock, which follows tokio's paused clock when a test drives the // playout clock itself. -use tokio::time::Instant; +use moq_clock::Instant; use super::args::Args; use super::output::{Output, Sink, Speaker}; @@ -581,7 +581,7 @@ async fn play_audio( .due(role.map(frame.timestamp)?) .and_then(|at| at.checked_sub(first)); if let Some(at) = at { - tokio::time::sleep_until(at.into()).await; + moq_clock::sleep_until(at).await; } if let Some((ended, end)) = deferred.take() && ended == discontinuity @@ -648,7 +648,7 @@ async fn play_audio( .due(mapped) .and_then(|at| at.checked_sub(lead(delay, &frame))); if let Some(at) = at { - tokio::time::sleep_until(at.into()).await; + moq_clock::sleep_until(at).await; } } @@ -685,7 +685,7 @@ async fn play_audio( let moved = presentation .lock() .unwrap() - .audio(timing.end, sink.buffered(), Instant::now().into_std()); + .audio(timing.end, sink.buffered(), Instant::now()); if moved { changed.notify_one(); output.send(Event::Wake); @@ -695,7 +695,7 @@ async fn play_audio( Ok::<_, anyhow::Error>(sink) }; let (_, sink) = tokio::try_join!(receive, play)?; - role.retire(Instant::now().into_std().checked_add(sink.buffered())); + role.retire(Instant::now().checked_add(sink.buffered())); Ok(sink) } @@ -1347,7 +1347,7 @@ mod tests { while index < 50 { old.write(packet(index, OLD)).unwrap(); index += 1; - tokio::time::sleep_until(start + PACKET_DURATION * index as u32).await; + moq_clock::sleep_until(start + PACKET_DURATION * index as u32).await; } // The replacement joins the catalog, then the old track finishes. @@ -1358,7 +1358,7 @@ mod tests { drop(old); while index < 100 { - tokio::time::sleep_until(start + PACKET_DURATION * index as u32).await; + moq_clock::sleep_until(start + PACKET_DURATION * index as u32).await; new.write(packet(index, NEW)).unwrap(); index += 1; } @@ -1794,7 +1794,7 @@ mod tests { loop { tokio::task::yield_now().await; if media.video.lock().unwrap().len() == 30 - || media.presentation.lock().unwrap().due(last) == Some((now + delay).into_std()) + || media.presentation.lock().unwrap().due(last) == Some(now + delay) { break; } @@ -1802,11 +1802,7 @@ mod tests { let due = media.presentation.lock().unwrap().due(last); task.abort(); let _ = task.await; - assert_eq!( - due, - Some((now + delay).into_std()), - "the decoder did not observe the live edge" - ); + assert_eq!(due, Some(now + delay), "the decoder did not observe the live edge"); assert!(recorder.present(&media).is_none(), "nothing is due before its delay"); } } diff --git a/rs/moq-cli/src/play/timeline.rs b/rs/moq-cli/src/play/timeline.rs index d4e8d330e55..744dae2e0de 100644 --- a/rs/moq-cli/src/play/timeline.rs +++ b/rs/moq-cli/src/play/timeline.rs @@ -1,7 +1,8 @@ //! Media time: the playout clock, and the audio track's own timeline. use anyhow::Context; -use std::time::{Duration, Instant}; +use moq_clock::Instant; +use std::time::Duration; use super::playback::Kind; use hang::moq_net::Timestamp; @@ -268,7 +269,7 @@ impl Role { /// Stop while a tail still plays until `drained`, holding a declared end until /// then. Dropping instead discards the tail with the end. pub(super) fn retire(self, drained: Option) { - let now = tokio::time::Instant::now().into_std(); + let now = moq_clock::now(); let mut presentation = self.presentation.lock().unwrap(); presentation.tails.retain(|tail| *tail > now); if presentation.ends[self.role as usize].take().is_some() @@ -289,7 +290,7 @@ impl Role { /// long as its replacement has not started playing; otherwise this one opens /// a transition of its own. pub(super) fn declare(&self) -> Transition { - let now = tokio::time::Instant::now().into_std(); + let now = moq_clock::now(); let own = self.role as usize; let mut presentation = self.presentation.lock().unwrap(); presentation.prune(now); @@ -366,7 +367,7 @@ impl Role { pending.members[own] = false; pending.plans[own] = None; } - presentation.prune(tokio::time::Instant::now().into_std()); + presentation.prune(moq_clock::now()); drop(presentation); self.timeline.notify_waiters(); } @@ -377,7 +378,7 @@ impl Role { if let Some(pending) = presentation.transition(transition) { pending.resumed[self.role as usize] = true; } - presentation.prune(tokio::time::Instant::now().into_std()); + presentation.prune(moq_clock::now()); drop(presentation); self.timeline.notify_waiters(); } @@ -396,7 +397,7 @@ impl Role { let changed = self.timeline.notified(); let mut changed = std::pin::pin!(changed); changed.as_mut().enable(); - let now = tokio::time::Instant::now().into_std(); + let now = moq_clock::now(); let end = { let mut presentation = self.presentation.lock().unwrap(); // The peer already resumed this transition after draining every end @@ -415,7 +416,7 @@ impl Role { }; if let Some(end) = end.filter(|end| *end > now) { tokio::select! { - _ = tokio::time::sleep_until(end.into()) => {}, + _ = moq_clock::sleep_until(end) => {}, _ = changed => {}, } continue; @@ -469,7 +470,7 @@ impl Drop for Role { pending.plans[own] = None; } presentation.declared[own] = Transition::default(); - presentation.prune(tokio::time::Instant::now().into_std()); + presentation.prune(moq_clock::now()); } self.timeline.notify_waiters(); } @@ -593,11 +594,11 @@ mod tests { #[tokio::test] async fn one_ended_role_maps_to_the_continuous_peers_clock() { tokio::time::pause(); - let start = tokio::time::Instant::now(); + let start = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); - presentation.lock().unwrap().video(ms(10_000), start.into_std()); + presentation.lock().unwrap().video(ms(10_000), start); let transition = audio.declare(); audio.end(ms(10_020)).unwrap(); tokio::time::advance(DELAY + Duration::from_millis(20)).await; @@ -611,12 +612,9 @@ mod tests { #[tokio::test] async fn a_late_paired_replacement_keeps_its_media_offset() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); let transition = audio.declare(); @@ -639,12 +637,9 @@ mod tests { #[tokio::test] async fn a_late_replacement_pairs_with_its_own_transition() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); let transition = audio.declare(); @@ -670,12 +665,9 @@ mod tests { #[tokio::test] async fn a_peer_two_restarts_ahead_pairs_with_the_first() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); let transition = audio.declare(); @@ -702,12 +694,9 @@ mod tests { #[tokio::test] async fn an_abandoned_replacement_leaves_the_next_transition_alone() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); let transition = audio.declare(); @@ -724,22 +713,19 @@ mod tests { audio.apply(transition, audio.resume(transition, ms(0), Some(DELAY)).await.unwrap()); video.apply(next, video.resume(next, ms(5_000), None).await.unwrap()); // The later replacement maps onto the running clock, not the audio restart's. - let resumed = tokio::time::Instant::now() + DELAY; + let resumed = moq_clock::now() + DELAY; assert_eq!( presentation.lock().unwrap().due(video.map(ms(5_000)).unwrap()), - Some(resumed.into_std()) + Some(resumed) ); } #[tokio::test] async fn an_abandoned_peer_plan_is_not_paired() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); let transition = audio.declare(); @@ -751,22 +737,19 @@ mod tests { video.plan(transition, ms(40), None); tokio::time::advance(DELAY + Duration::from_millis(20)).await; video.apply(transition, video.resume(transition, ms(40), None).await.unwrap()); - let resumed = tokio::time::Instant::now() + DELAY; + let resumed = moq_clock::now() + DELAY; assert_eq!( presentation.lock().unwrap().due(video.map(ms(40)).unwrap()), - Some(resumed.into_std()) + Some(resumed) ); } #[tokio::test] async fn a_resumed_transition_ignores_the_peers_later_end() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); let transition = audio.declare(); @@ -790,12 +773,9 @@ mod tests { #[tokio::test] async fn a_cancelled_role_releases_its_declared_end() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); audio.declare(); @@ -816,18 +796,15 @@ mod tests { #[tokio::test] async fn a_closed_role_still_holds_its_declared_end() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); audio.declare(); audio.end(ms(10_200)).unwrap(); // The track closed, but its returned sink still plays the tail up to the end. - audio.retire(Some((now + Duration::from_millis(300)).into_std())); + audio.retire(Some(now + Duration::from_millis(300))); let transition = video.declare(); video.end(ms(10_000)).unwrap(); video.plan(transition, ms(0), None); @@ -842,22 +819,19 @@ mod tests { #[tokio::test] async fn a_retired_end_ignores_a_later_re_pin() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); audio.declare(); audio.end(ms(10_200)).unwrap(); - audio.retire(Some((now + Duration::from_millis(300)).into_std())); + audio.retire(Some(now + Duration::from_millis(300))); // A replacement rendition starts over at zero and re-pins the anchor. { let mut presentation = presentation.lock().unwrap(); presentation.restarted(); - presentation.audio(Duration::from_millis(50), DELAY, now.into_std()); + presentation.audio(Duration::from_millis(50), DELAY, now); } let transition = video.declare(); video.plan(transition, ms(0), None); @@ -868,17 +842,14 @@ mod tests { #[tokio::test] async fn a_cancelled_successor_keeps_a_retired_tail() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); audio.declare(); audio.end(ms(10_200)).unwrap(); - audio.retire(Some((now + Duration::from_millis(300)).into_std())); + audio.retire(Some(now + Duration::from_millis(300))); // The next rendition fails while the retired sink still plays. let successor = Role::new(presentation.clone(), Kind::Audio); successor.declare(); @@ -892,12 +863,9 @@ mod tests { #[tokio::test] async fn paired_plans_keep_media_offsets_and_follow_the_speakers_delay() { tokio::time::pause(); - let now = tokio::time::Instant::now(); + let now = moq_clock::now(); let presentation = std::sync::Arc::new(std::sync::Mutex::new(Presentation::new(DELAY))); - presentation - .lock() - .unwrap() - .audio(Duration::from_secs(10), DELAY, now.into_std()); + presentation.lock().unwrap().audio(Duration::from_secs(10), DELAY, now); let audio = Role::new(presentation.clone(), Kind::Audio); let video = Role::new(presentation.clone(), Kind::Video); let transition = audio.declare(); @@ -921,7 +889,7 @@ mod tests { ); assert_eq!( presentation.lock().unwrap().due(audio.map(ms(0)).unwrap()), - Some((now + Duration::from_millis(200)).into_std()) + Some(now + Duration::from_millis(200)) ); } diff --git a/rs/moq-cli/src/play/video.rs b/rs/moq-cli/src/play/video.rs index 9d7d1794026..d278315e6de 100644 --- a/rs/moq-cli/src/play/video.rs +++ b/rs/moq-cli/src/play/video.rs @@ -4,8 +4,8 @@ use std::collections::{BTreeSet, VecDeque}; use std::sync::{Arc, Mutex}; use std::time::Duration; +use moq_clock::Instant; use moq_mux::container::{Frame, Read}; -use tokio::time::Instant; use super::buffer::Buffer; use super::output::Output; @@ -22,7 +22,7 @@ const DECODE_AHEAD: Duration = Duration::from_millis(100); /// What the decode loop does next. enum Step { /// Sleep until the instant, or until the buffer or the window changes. - Wait(Option), + Wait(Option), Decode, /// The track ended: drain what the codec still holds. Flush, @@ -36,7 +36,7 @@ enum Step { /// is skipped anyway once the next falls due. Making room then keeps a /// stalled window from stalling decode without dropping a picture a live one /// would show. -fn vacancy(queue: &VecDeque, presentation: &Presentation) -> Option { +fn vacancy(queue: &VecDeque, presentation: &Presentation) -> Option { if queue.len() < MAX_FRAMES { return None; } @@ -154,7 +154,7 @@ impl Video { .presentation .lock() .unwrap() - .video(role.map(frame.timestamp)?, Instant::now().into_std()) + .video(role.map(frame.timestamp)?, Instant::now()) { self.output.send(Event::Wake); } @@ -248,7 +248,7 @@ impl Video { let step = { let mut queue = self.frames.lock().unwrap(); let presentation = self.presentation.lock().unwrap(); - let now = Instant::now().into_std(); + let now = Instant::now(); let mut placed = false; while !pending.is_empty() && vacancy(&queue, &presentation).is_none_or(|at| at <= now) { if queue.len() >= MAX_FRAMES { @@ -302,7 +302,7 @@ impl Video { let frames = match step { Step::Wait(Some(at)) => { tokio::select! { - _ = tokio::time::sleep_until(at.into()) => {}, + _ = moq_clock::sleep_until(at) => {}, _ = self.changed.notified() => {}, } continue; @@ -368,7 +368,7 @@ impl Video { advancing.store(false, std::sync::atomic::Ordering::Relaxed); let mut presentation = self.presentation.lock().unwrap(); for frame in &buffers.front().unwrap().frames { - presentation.video(role.map(frame.timestamp)?, Instant::now().into_std()); + presentation.video(role.map(frame.timestamp)?, Instant::now()); } self.output.send(Event::Wake); continue; @@ -530,11 +530,7 @@ mod tests { let start = Instant::now(); let last = moq_net::Timestamp::from_millis(1_980).unwrap(); if speaker { - media - .presentation - .lock() - .unwrap() - .audio(Duration::ZERO, delay, start.into_std()); + media.presentation.lock().unwrap().audio(Duration::ZERO, delay, start); } let decoder = Buffered::default(); let decoded = decoder.decoded.clone(); @@ -548,7 +544,7 @@ mod tests { } else { Duration::ZERO }; - assert_eq!(media.presentation.lock().unwrap().due(last), Some(last_due.into_std())); + assert_eq!(media.presentation.lock().unwrap().due(last), Some(last_due)); assert!(media.video.lock().unwrap().len() <= MAX_FRAMES); if speaker { assert!( @@ -657,7 +653,7 @@ mod tests { .lock() .unwrap() .due(moq_net::Timestamp::from_millis(500).unwrap()), - Some((restarted + delay).into_std()) + Some(restarted + delay) ); assert!(media.output.present(&media).is_none()); tokio::time::advance(delay + Duration::from_millis(1)).await; @@ -808,14 +804,11 @@ mod tests { .unwrap() .due(moq_net::Timestamp::from_millis(66).unwrap()) .unwrap(); - assert!( - Instant::now().into_std() < last, - "the track closed after its tail was due" - ); + assert!(Instant::now() < last, "the track closed after its tail was due"); let audio = Role::new(media.presentation.clone(), super::super::playback::Kind::Audio); let transition = audio.declare(); audio.resume(transition, moq_net::Timestamp::ZERO, None).await.unwrap(); - assert!(Instant::now().into_std() >= last, "audio resumed over the video tail"); + assert!(Instant::now() >= last, "audio resumed over the video tail"); } #[tokio::test] @@ -1062,9 +1055,9 @@ mod tests { let last = moq_net::Timestamp::from_millis(order.iter().max().unwrap() * 33).unwrap(); let end = media.presentation.lock().unwrap().due(last).unwrap(); let mut shown = Vec::new(); - while Instant::now().into_std() <= end + Duration::from_millis(10) { + while Instant::now() <= end + Duration::from_millis(10) { if let Some(timestamp) = media.output.present(&media) { - shown.push((timestamp, Instant::now().into_std())); + shown.push((timestamp, Instant::now())); } tokio::time::advance(Duration::from_millis(1)).await; tokio::task::yield_now().await; diff --git a/rs/moq-cli/src/play/window.rs b/rs/moq-cli/src/play/window.rs index b516778123a..085de50a440 100644 --- a/rs/moq-cli/src/play/window.rs +++ b/rs/moq-cli/src/play/window.rs @@ -3,7 +3,9 @@ use std::collections::VecDeque; use std::sync::{Arc, Mutex}; -use std::time::{Duration, Instant}; +use std::time::Duration; + +use moq_clock::Instant; use anyhow::Context; use hang::moq_net; @@ -307,14 +309,18 @@ impl ApplicationHandler for App { return; } + let now = moq_clock::now(); match self.next_redraw { - Some(deadline) if deadline <= Instant::now() => { + Some(deadline) if deadline <= now => { self.next_redraw = None; if let Some(display) = &self.display { display.window.request_redraw(); } } - Some(deadline) => event_loop.set_control_flow(ControlFlow::WaitUntil(deadline)), + // winit waits on the host clock, so the deadline converts at this boundary. + Some(deadline) => event_loop.set_control_flow(ControlFlow::WaitUntil( + std::time::Instant::now() + deadline.saturating_duration_since(now), + )), None => event_loop.set_control_flow(ControlFlow::Wait), } } diff --git a/rs/moq-clock/Cargo.toml b/rs/moq-clock/Cargo.toml index eecaa2cd3b3..255fab8d587 100644 --- a/rs/moq-clock/Cargo.toml +++ b/rs/moq-clock/Cargo.toml @@ -21,23 +21,30 @@ features = ["sim"] ignored = ["moq-clock"] [features] +default = ["runtime"] # The deterministic single-threaded executor and `#[moq_clock::test]`. Meant for # dev-dependencies, so production builds never compile it. sim = ["dep:futures", "dep:moq-clock-macros"] +runtime = ["dep:tokio", "dep:web-async"] [dependencies] futures = { workspace = true, optional = true } kio = { workspace = true } moq-clock-macros = { workspace = true, optional = true } +web-async = { workspace = true, optional = true } + +# The browser's only clocks, read with or without the runtime's timers. +[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies] web-async = { workspace = true } # The runtime handle, to keep a paused runtime's clock on its own origin. [target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies] -tokio = { workspace = true, features = ["rt", "time"] } +tokio = { workspace = true, features = ["rt", "time"], optional = true } [dev-dependencies] -# Enable the simulator so its tests run in the merge gate. -moq-clock = { path = ".", features = ["sim"] } +# Enable the simulator so its tests run in the merge gate. Without default features, +# so `--no-default-features` still tests the build without the runtime. +moq-clock = { path = ".", default-features = false, features = ["sim"] } [target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dev-dependencies] # Match the wasm-bindgen CLI schema used by the browser runner. diff --git a/rs/moq-clock/src/deadline.rs b/rs/moq-clock/src/deadline.rs index 5fc9c676215..7641fa53d2d 100644 --- a/rs/moq-clock/src/deadline.rs +++ b/rs/moq-clock/src/deadline.rs @@ -208,6 +208,14 @@ impl Deadline { } } +impl kio::Task for Deadline { + type Output = (); + + fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { + self.poll(waiter) + } +} + impl Drop for Deadline { fn drop(&mut self) { self.set(None); diff --git a/rs/moq-clock/src/instant.rs b/rs/moq-clock/src/instant.rs index 36d39b9c935..97b9efde200 100644 --- a/rs/moq-clock/src/instant.rs +++ b/rs/moq-clock/src/instant.rs @@ -9,7 +9,8 @@ use std::{ /// The methods mirror [`std::time::Instant`]'s, so swapping one for the other is an /// import change. Instants are only comparable within one clock: the simulator /// starts at [`Instant::ZERO`], and the runtime backends count from an origin they -/// pick once, so an instant has no conversion back to the host clock. +/// pick once, reading far past zero there so earlier moments stay representable. +/// An instant has no conversion back to the host clock. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Instant(u64); diff --git a/rs/moq-clock/src/lib.rs b/rs/moq-clock/src/lib.rs index c32128e6ccb..70b73f87d16 100644 --- a/rs/moq-clock/src/lib.rs +++ b/rs/moq-clock/src/lib.rs @@ -63,3 +63,14 @@ pub fn wall() -> Wall { } runtime::wall() } + +/// The first monotonic and wall readings observed on the ambient clock. +/// +/// Unlike [`wall`], the wall half does not change after a clock correction. +pub fn anchor() -> (Instant, Wall) { + #[cfg(feature = "sim")] + if let Some(anchor) = sim::anchor() { + return anchor; + } + runtime::anchor() +} diff --git a/rs/moq-clock/src/runtime.rs b/rs/moq-clock/src/runtime.rs index 893d59fb039..46f90574fce 100644 --- a/rs/moq-clock/src/runtime.rs +++ b/rs/moq-clock/src/runtime.rs @@ -1,22 +1,36 @@ //! The runtime backend: tokio's clock on native (paused tokio included), and //! `performance.now()` with `setTimeout` in the browser, both through //! `web_async::time`. The only module, with the simulator, that reads a host clock. +//! +//! Without the `runtime` feature there are no timers, and reads fall back to std, +//! except in the browser, where std has no clock and `web_async::time` still reads. #![allow(clippy::disallowed_methods)] +#[cfg(feature = "runtime")] +use std::{future::Future, pin::Pin}; use std::{ - future::Future, - pin::Pin, sync::OnceLock, task::{Context, Poll}, }; +#[cfg(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi"))))] use web_async::time as host; use crate::{Instant, Wall}; +/// How far past [`Instant::ZERO`] a runtime clock reads at its origin. +/// +/// Like std's since-boot `Instant`, so `now() - d` names a moment before the +/// process started instead of underflowing during its first seconds. 2^32 s +/// (~136 years) outlasts any lookback, and leaves ~448 years of range ahead. +const LEAD: std::time::Duration = std::time::Duration::from_secs(1 << 32); + /// Where every unpaused clock counts from, so instants read on any thread, in any /// runtime or none, stay comparable. +#[cfg(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi"))))] static ORIGIN: OnceLock = OnceLock::new(); +#[cfg(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi"))))] +static ANCHOR: OnceLock<(Instant, Wall)> = OnceLock::new(); /// The origin this context counts from, and a reading of its clock. /// @@ -24,6 +38,7 @@ static ORIGIN: OnceLock = OnceLock::new(); /// never reads below it. A paused tokio runtime's clock starts at the runtime's /// build, which can precede the origin; that runtime counts from its own first /// reading instead (see [`paused`]). +#[cfg(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi"))))] fn read() -> (host::Instant, host::Instant) { #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] let origin = *ORIGIN.get_or_init(|| host::Instant::from_std(std::time::Instant::now())); @@ -37,11 +52,13 @@ fn read() -> (host::Instant, host::Instant) { (origin, reading) } +#[cfg(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi"))))] pub(crate) fn now() -> Instant { let (origin, reading) = read(); - Instant::from_origin(reading.duration_since(origin)) + Instant::from_origin(LEAD + reading.duration_since(origin)) } +#[cfg(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi"))))] pub(crate) fn wall() -> Wall { let since = host::SystemTime::now() .duration_since(host::UNIX_EPOCH) @@ -49,23 +66,50 @@ pub(crate) fn wall() -> Wall { Wall::UNIX_EPOCH + since } +#[cfg(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi"))))] +pub(crate) fn anchor() -> (Instant, Wall) { + #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] + if let Some(anchor) = paused::anchor() { + return anchor; + } + *ANCHOR.get_or_init(|| (real_now(), wall())) +} + +/// [`now`] from the host's real clock, so an advanced paused runtime sharing the +/// origin cannot skew the anchor every unpaused caller maps through. +#[cfg(all(feature = "runtime", any(not(target_arch = "wasm32"), target_os = "wasi")))] +fn real_now() -> Instant { + let origin = ORIGIN.get_or_init(|| host::Instant::from_std(std::time::Instant::now())); + Instant::from_origin(LEAD + std::time::Instant::now().saturating_duration_since(origin.into_std())) +} + +/// The browser has one clock and no paused runtimes. +#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] +fn real_now() -> Instant { + now() +} + /// A host timer for `at`, on the origin it was created with. +#[cfg(feature = "runtime")] pub(crate) struct Timer { origin: host::Instant, sleep: Pin>, } +#[cfg(feature = "runtime")] impl Timer { pub(crate) fn new(at: Instant) -> Self { let (origin, _) = read(); Self { origin, - sleep: Box::pin(host::sleep_until(origin + at.since_origin())), + sleep: Box::pin(host::sleep_until(origin + at.since_origin().saturating_sub(LEAD))), } } pub(crate) fn reset(&mut self, at: Instant) { - self.sleep.as_mut().reset(self.origin + at.since_origin()); + self.sleep + .as_mut() + .reset(self.origin + at.since_origin().saturating_sub(LEAD)); } pub(crate) fn poll(&mut self, cx: &mut Context<'_>) -> Poll<()> { @@ -78,7 +122,56 @@ impl Timer { } } -#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] +#[cfg(not(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi")))))] +static STD_ORIGIN: OnceLock<(std::time::Instant, Wall)> = OnceLock::new(); + +#[cfg(not(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi")))))] +fn std_origin() -> (std::time::Instant, Wall) { + *STD_ORIGIN.get_or_init(|| (std::time::Instant::now(), system_wall())) +} + +#[cfg(not(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi")))))] +fn system_wall() -> Wall { + let since = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("the host wall clock reads before 1970"); + Wall::UNIX_EPOCH + since +} + +#[cfg(not(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi")))))] +pub(crate) fn now() -> Instant { + Instant::from_origin(LEAD + std_origin().0.elapsed()) +} + +#[cfg(not(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi")))))] +pub(crate) fn wall() -> Wall { + system_wall() +} + +#[cfg(not(any(feature = "runtime", all(target_arch = "wasm32", not(target_os = "wasi")))))] +pub(crate) fn anchor() -> (Instant, Wall) { + (Instant::from_origin(LEAD), std_origin().1) +} + +#[cfg(not(feature = "runtime"))] +pub(crate) struct Timer; + +#[cfg(not(feature = "runtime"))] +impl Timer { + pub(crate) fn new(_: Instant) -> Self { + panic!("moq-clock runtime support is disabled") + } + + pub(crate) fn reset(&mut self, _: Instant) { + panic!("moq-clock runtime support is disabled") + } + + pub(crate) fn poll(&mut self, _: &mut Context<'_>) -> Poll<()> { + panic!("moq-clock runtime support is disabled") + } +} + +#[cfg(all(feature = "runtime", any(not(target_arch = "wasm32"), target_os = "wasi")))] mod paused { use std::{ collections::HashMap, @@ -89,11 +182,20 @@ mod paused { }; use super::host; + use crate::{Instant, Wall}; - /// Paused runtimes whose clock started before [`ORIGIN`](super::ORIGIN), with the - /// first reading each counts from. Only test runtimes land here: an unpaused - /// clock never reads below the origin. Never pruned; one entry per such runtime. - static OWN: LazyLock>> = LazyLock::new(Default::default); + /// A paused runtime counting from its own origin. + struct Own { + /// The first reading it counts from. + origin: host::Instant, + /// Its own [`anchor`], since its instants share no scale with [`ANCHOR`](super::ANCHOR). + anchor: Option<(Instant, Wall)>, + } + + /// Paused runtimes whose clock started before [`ORIGIN`](super::ORIGIN). Only test + /// runtimes land here: an unpaused clock never reads below the origin. Never + /// pruned; one entry per such runtime. + static OWN: LazyLock>> = LazyLock::new(Default::default); /// Whether [`OWN`] has any entry, so production reads skip its lock. static ANY: AtomicBool = AtomicBool::new(false); @@ -107,18 +209,37 @@ mod paused { }; let mut own = OWN.lock().unwrap(); if let Some(own) = own.get(&runtime.id()) { - return *own; + return own.origin; } if reading >= origin { return origin; } - own.insert(runtime.id(), reading); + own.insert( + runtime.id(), + Own { + origin: reading, + anchor: None, + }, + ); ANY.store(true, Ordering::Release); reading } + + /// The anchor of the calling runtime, if it counts from its own origin. + pub(super) fn anchor() -> Option<(Instant, Wall)> { + // Read first: it registers a runtime whose clock starts before the origin. + let now = super::now(); + if !ANY.load(Ordering::Acquire) { + return None; + } + let runtime = tokio::runtime::Handle::try_current().ok()?; + let mut own = OWN.lock().unwrap(); + let own = own.get_mut(&runtime.id())?; + Some(*own.anchor.get_or_insert_with(|| (now, super::wall()))) + } } -#[cfg(all(test, any(not(target_arch = "wasm32"), target_os = "wasi")))] +#[cfg(all(test, feature = "runtime", any(not(target_arch = "wasm32"), target_os = "wasi")))] mod tests { use std::time::Duration; @@ -147,6 +268,69 @@ mod tests { }); } + /// A paused runtime counting from its own origin anchors on its own clock, not + /// on one another runtime advanced. + #[test] + fn paused_runtimes_own_their_anchors() { + let paused = || { + tokio::runtime::Builder::new_current_thread() + .enable_time() + .start_paused(true) + .build() + .unwrap() + }; + // Built before anything reads the clock, so in a fresh process each counts + // from its own origin. + let first = paused(); + let second = paused(); + let _ = crate::now(); + first.block_on(async { + let _ = crate::now(); + tokio::time::advance(Duration::from_secs(60)).await; + let _ = crate::anchor(); + }); + second.block_on(async { + let (instant, _) = crate::anchor(); + assert_eq!(instant, crate::now()); + }); + } + + /// A paused runtime sharing the origin cannot skew the anchor of unpaused + /// callers by advancing its clock before the anchor is first taken. + #[test] + fn a_later_paused_runtime_does_not_skew_the_anchor() { + let _ = crate::now(); + // Built after the origin, so it counts from the shared origin. + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_time() + .start_paused(true) + .build() + .unwrap(); + runtime.block_on(async { + let _ = crate::now(); + tokio::time::advance(Duration::from_secs(3_600)).await; + let _ = crate::anchor(); + }); + let (instant, _) = crate::anchor(); + assert!(instant <= crate::now(), "{instant:?} > {:?}", crate::now()); + } + + /// A fresh clock names moments before the process started, as std's does, so + /// looking back from startup never underflows. + #[test] + fn startup_reads_can_look_back() { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_time() + .start_paused(true) + .build() + .unwrap(); + runtime.block_on(async { + let now = crate::now(); + assert!(now.checked_sub(Duration::from_secs(3_600)).is_some(), "{now:?}"); + }); + assert!(crate::now().checked_sub(Duration::from_secs(3_600)).is_some()); + } + /// Every worker of a multi-thread runtime, and a thread outside any runtime, /// count from one origin. #[test] @@ -172,7 +356,7 @@ mod tests { } } -#[cfg(all(test, target_arch = "wasm32", not(target_os = "wasi")))] +#[cfg(all(test, feature = "runtime", target_arch = "wasm32", not(target_os = "wasi")))] mod browser_tests { use std::{future::pending, time::Duration}; @@ -233,3 +417,20 @@ export function restoreWall() { Date.now = originalNow; } assert_eq!(wall, crate::Wall::UNIX_EPOCH + Duration::from_millis(1_700_000_000_123)); } } + +#[cfg(all(test, not(feature = "runtime"), target_arch = "wasm32", not(target_os = "wasi")))] +mod browser_read_tests { + use wasm_bindgen_test::*; + + wasm_bindgen_test_configure!(run_in_browser); + + /// Reads work without the runtime, since std has no clock in the browser. + #[wasm_bindgen_test] + fn reads_without_runtime() { + let start = crate::now(); + assert!(crate::now() >= start); + assert!(crate::wall() > crate::Wall::UNIX_EPOCH); + let (instant, _) = crate::anchor(); + assert!(instant <= crate::now()); + } +} diff --git a/rs/moq-clock/src/sim.rs b/rs/moq-clock/src/sim.rs index 926447a982a..7fbd7af3c8b 100644 --- a/rs/moq-clock/src/sim.rs +++ b/rs/moq-clock/src/sim.rs @@ -7,14 +7,15 @@ //! once every task has stalled, [`run`] jumps the clock to the earliest armed //! timer and wakes it, like tokio's paused clock, so a test never waits on the //! host clock. A run whose tasks are all parked with nothing armed can never -//! finish, so it panics rather than hang. +//! finish, so it panics rather than hang, unless a task awaits [`idle`], which +//! completes at exactly that point. //! //! Caller-driven state machines take time from their caller: //! [`drive`](crate::drive) polls one with the simulated instant, and [`attach`] //! keeps a [`Deadlines`] queue in step. use std::{ - cell::RefCell, + cell::{Cell, RefCell}, collections::BTreeMap, future::{Future, IntoFuture}, pin::{Pin, pin}, @@ -45,10 +46,13 @@ struct Runtime { time: RefCell