From 283a1bba417ed2173ae24d9b33a57478c86fd0b6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 23:41:31 -0800 Subject: [PATCH] bench(net): backport session_fanout_threads to release Ports main's session_fanout_threads case (moq-dev/moq#7493) into release's single-file session bench, under main's case IDs, so the pre-cut pairing compares the fanout contention fix. Adds a process CPU clock and a multi-threaded runtime to the bench only; no shipped code changes. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + rs/moq-net/Cargo.toml | 9 ++- rs/moq-net/benches/session.rs | 119 +++++++++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 07de8709ed3..dcb66284189 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4508,6 +4508,7 @@ dependencies = [ "futures", "getrandom 0.4.3", "kio 0.6.4", + "libc", "loom", "moq-pattern", "num_enum", diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index ca5dafdeda5..5d9b72ae450 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -39,8 +39,9 @@ web-transport-trait = { workspace = true } criterion = { workspace = true } serde_json = { workspace = true } # test-util (tokio::time::pause/advance) is test-only and is NOT supported on -# wasm, so it must not leak into the normal dependency feature set. -tokio = { workspace = true, features = ["macros", "io-util", "sync", "test-util", "time", "rt"] } +# wasm, so it must not leak into the normal dependency feature set. The session +# fanout bench runs across worker threads. +tokio = { workspace = true, features = ["macros", "io-util", "sync", "test-util", "time", "rt", "rt-multi-thread"] } tracing-test = { workspace = true, features = ["no-env-filter"] } # Model checks for the kio-backed model layer (tests/loom.rs). `cfg(loom)` is only @@ -56,6 +57,10 @@ getrandom = { workspace = true } # Keep this in lockstep with rs/moq-wasm's pinned wasm-bindgen CLI schema. wasm-bindgen-test = "=0.3.77" +# Process CPU timing in the session benchmark. +[target.'cfg(unix)'.dev-dependencies] +libc = { workspace = true } + [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)'] } diff --git a/rs/moq-net/benches/session.rs b/rs/moq-net/benches/session.rs index a3240c1d537..159e1df2bf7 100644 --- a/rs/moq-net/benches/session.rs +++ b/rs/moq-net/benches/session.rs @@ -35,7 +35,13 @@ //! moq-transport 14 every close is credited back through the MAX_REQUEST_ID window, //! so a slope here is that bookkeeping growing with the sessions or the requests. //! -//! Everything runs on one current-thread runtime, so the only work measured is +//! `session_fanout_threads` serves one broadcast's one-frame groups through one relay to 64 +//! viewers on multi-threaded runtimes, timed in process CPU. Each group wakes every +//! subscription's serve task at once, so a read path that makes them take one shared lock +//! many times pays for it in lock waits and wakes across workers: the `workers=16` rows +//! climb above the `workers=1` ones. +//! +//! Everything else runs on one current-thread runtime, so the only work measured is //! the protocol and model code, never scheduling across threads. //! //! Run with `cargo bench -p moq-net --bench session`. @@ -225,6 +231,66 @@ fn runtime() -> tokio::runtime::Runtime { .unwrap() } +/// A multi-threaded runtime with `workers` threads, for the scenarios timed in process CPU. +fn threaded(workers: usize) -> tokio::runtime::Runtime { + tokio::runtime::Builder::new_multi_thread() + .worker_threads(workers) + .enable_time() + .build() + .unwrap() +} + +/// What a round is timed in. +#[derive(Clone, Copy)] +enum Clock { + Wall, + /// Every thread's CPU time, for a multi-threaded runtime: lock waits and wakes across + /// its workers count too. + Process, +} + +/// One round's clock. +enum MeasurementClock { + Wall(Instant), + #[cfg(unix)] + Cpu(libc::clockid_t, Duration), +} + +impl MeasurementClock { + fn start(clock: Clock) -> Self { + match clock { + Clock::Wall => Self::Wall(Instant::now()), + #[cfg(unix)] + Clock::Process => Self::Cpu( + libc::CLOCK_PROCESS_CPUTIME_ID, + Self::cpu(libc::CLOCK_PROCESS_CPUTIME_ID), + ), + #[cfg(not(unix))] + Clock::Process => panic!("CPU-timed rounds require Unix CPU clocks"), + } + } + + fn elapsed(self) -> Duration { + match self { + Self::Wall(start) => start.elapsed(), + #[cfg(unix)] + Self::Cpu(id, start) => Self::cpu(id).checked_sub(start).expect("CPU clock moved backwards"), + } + } + + #[cfg(unix)] + fn cpu(id: libc::clockid_t) -> Duration { + let mut time = libc::timespec { tv_sec: 0, tv_nsec: 0 }; + // SAFETY: `time` is a valid, initialized output pointer for this supported clock. + let result = unsafe { libc::clock_gettime(id, &mut time) }; + assert_eq!(result, 0, "CPU clock: {}", std::io::Error::last_os_error()); + Duration::new( + u64::try_from(time.tv_sec).unwrap(), + u32::try_from(time.tv_nsec).unwrap(), + ) + } +} + fn write_group(track: &track::Producer, payload: &Bytes, frames: usize) -> u64 { let mut group = track.append_group().unwrap(); for _ in 0..frames { @@ -394,6 +460,8 @@ struct Room { /// The group each viewer's subscriber is reading, when paced, flattened in viewer order. reading: Vec>, cache_now: moq_net::time::Instant, + /// What [`Self::measure`] times each round in. + clock: Clock, } impl Room { @@ -450,6 +518,7 @@ impl Room { writing: Vec::new(), reading: Vec::new(), cache_now: moq_net::time::Instant::now(), + clock: Clock::Wall, }; room.writing.resize_with(room.watched.len(), || None); room.reading.resize_with(shape.viewers * shape.watch, || None); @@ -567,7 +636,7 @@ impl Room { let mut next_report = Duration::from_secs(10); for _ in 0..iters { self.tick(); - let start = Instant::now(); + let start = MeasurementClock::start(self.clock); self.round().await; elapsed += start.elapsed(); if report && began.elapsed() >= next_report { @@ -1006,6 +1075,42 @@ impl IdleRoom { } } +/// One relay serving one broadcast's one-frame groups to 64 viewers, as the relay load +/// harness's `fanout` workload does, on multi-threaded runtimes timed in process CPU; see +/// the module docs. +fn fanout_threads(c: &mut Criterion) { + // Process CPU is read through a Unix clock. + if !cfg!(unix) || std::env::var_os("SESSION_ALLOCS").is_some() || std::env::var_os("SESSION_EXPIRY_ONLY").is_some() + { + return; + } + let shape = Shape { + publishers: 1, + viewers: 64, + frame: 400, + group_frames: 1, + ..Shape::BASE + }; + let mut group = c.benchmark_group("session_fanout_threads"); + // Groups delivered per round: one to each viewer. + group.throughput(Throughput::Elements(shape.viewers as u64)); + for workers in [1, 16] { + let rt = threaded(workers); + for version in ["moq-lite-06", "moq-lite-07-wip"] { + let mut room = None; + group.bench_function(format!("{version}/workers={workers}"), |b| { + let room = room.get_or_insert_with(|| { + let mut room = rt.block_on(Room::new(version, shape)); + room.clock = Clock::Process; + room + }); + b.iter_custom(|iters| rt.block_on(room.measure(iters))) + }); + } + } + group.finish(); +} + fn idle(c: &mut Criterion) { let mut group = c.benchmark_group("session_idle"); for version in ["moq-lite-06", "moq-transport-16", "moq-transport-22"] { @@ -1242,5 +1347,13 @@ fn session(c: &mut Criterion) { ); } -criterion_group!(benches, session, group_cost, fetch_frames, withdrawal, idle); +criterion_group!( + benches, + session, + group_cost, + fetch_frames, + withdrawal, + fanout_threads, + idle +); criterion_main!(benches);