Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions rs/moq-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)'] }

Expand Down
119 changes: 116 additions & 3 deletions rs/moq-net/benches/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -394,6 +460,8 @@ struct Room {
/// The group each viewer's subscriber is reading, when paced, flattened in viewer order.
reading: Vec<Option<group::Consumer>>,
cache_now: moq_net::time::Instant,
/// What [`Self::measure`] times each round in.
clock: Clock,
}

impl Room {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"] {
Expand Down Expand Up @@ -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);
Loading