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: 0 additions & 1 deletion quest/m2/perf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ revisions on the same host and worker count, and profiles with
- [Ingest batch](/quest/m2/perf/ingest-batch.md) - relay ingest pays one lock, wake, and clock read per burst of whole frames instead of per frame
- [kio waiter costs](/quest/m2/perf/kio-waiter-costs.md) - long-lived waiters stop being rebuilt every poll, IETF fetch keeps one close watch per stream, and a short `kio::log` costs less
- [Shard by default](/quest/m2/perf/shard-default.md) - a saturating shard sweep decides whether an unset `runtime.workers` runs one worker per core on Linux
- [An idle export poll is cheap](/quest/m2/perf/export-idle-poll.md) - an export poll nothing woke skips the track fill and catalog work it repeats today
- [Mesh session cost](/quest/m2/perf/mesh-session-cost.md) - moq-net's per-session announce, route sync, and scope work drops, swept over sessions and announces
- [Cluster route memory](/quest/m2/perf/cluster-route-memory.md) - a full-mesh cluster's ~3.7 KB per route per broadcast shrinks, the in-memory hop is profiled, and `bench-profile lock` tolerates lost records
- [Lock-free single-delivery writes](/quest/m2/perf/single-delivery-write.md) - a frame with one delivery writes without a lock, recovering #6922's remaining FETCH cost
Expand Down
15 changes: 0 additions & 15 deletions quest/m2/perf/export-idle-poll.md

This file was deleted.

7 changes: 2 additions & 5 deletions quest/m2/perf/ts-idle-jitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ does, about 1.4k instructions per track. Today it is about 3k.
Planned 2026-11-01 (maintainer, follow-up planning after the 2026-11-01 /quest-complete run). Found by [moq-dev/moq#7504](https://github.com/moq-dev/moq/pull/7504). With
a delay, `jitter::Acquire::freshest` (18%) and `memcmp` (26%) lead the idle
poll (`rs/moq-mux`). Skip the freshest scan when nothing arrived, and find what
the memcmp compares on an idle poll. Measure with #7504's export idle bench.

## Required

- [An export poll nothing woke is cheap](/quest/m2/perf/export-idle-poll.md) - adds the export idle bench and the baseline this measures against, in [moq-dev/moq#7504](https://github.com/moq-dev/moq/pull/7504)
the memcmp compares on an idle poll. Measure with the `container-export-idle` bench
(`rs/moq-mux/benches/export.rs`).
31 changes: 24 additions & 7 deletions rs/kio/src/waiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ pub struct Waiter {
// unrecorded and a later registration there would stack a duplicate.
lost: AtomicBool,

// Made by `noop`, so a channel parks it nowhere: nothing would ever see its wake.
// Made by [`Self::noop`]: nothing would ever see its wake, so it registers nothing and a
// channel parks it nowhere.
noop: bool,
}

Expand Down Expand Up @@ -256,7 +257,8 @@ impl WaiterList {
}
}

/// Register a waiter, a no-op while this list still holds it.
/// Register a waiter, a no-op while this list still holds it, or for a
/// [`Waiter::noop`].
///
/// The list's tag changes on every drain and the waiter records the tag it
/// joined under, so a matching record proves the entry is still here and a
Expand All @@ -267,6 +269,10 @@ impl WaiterList {
/// window covers the list over time. A list about to grow sweeps every
/// dead slot first.
pub fn register(&mut self, waiter: &Waiter) {
// Registering would only allocate a handle nothing can wake, leaving a dead slot once it drops.
if waiter.noop {
return;
}
if self.tag == 0 {
self.tag = serial();
}
Expand Down Expand Up @@ -579,7 +585,7 @@ mod tests {
fn abandoned_waiters_do_not_grow_the_list() {
let mut list = WaiterList::new();
for _ in 0..1_000 {
Waiter::noop().register(&mut list);
Waiter::new(Waker::noop().clone()).register(&mut list);
}
assert!(list.entries.len() <= 2);
}
Expand All @@ -588,21 +594,32 @@ mod tests {
fn a_list_is_empty_once_its_waiters_drop() {
let mut list = WaiterList::new();
assert!(list.is_empty());
let waiter = Waiter::noop();
let waiter = Waiter::new(Waker::noop().clone());
waiter.register(&mut list);
assert!(!list.is_empty());
drop(waiter);
assert!(list.is_empty());
}

/// A peek through a no-op waiter allocates no handle and takes no slot, clone or not.
#[test]
fn a_noop_waiter_registers_nothing() {
let mut list = WaiterList::new();
let waiter = Waiter::noop();
waiter.register(&mut list);
assert!(waiter.shared.get().is_none(), "a no-op registration allocated a handle");
waiter.clone().register(&mut list);
assert!(list.entries.is_empty());
}

/// Tasks parked on one list are retired and re-register in whatever order they
/// wake, while the list itself is never woken (a rarely-changing value many tasks
/// watch). The probe window alone let such a list grow without bound.
#[test]
fn retired_live_waiters_do_not_grow_the_list() {
const LIVE: usize = 64;
let mut list = WaiterList::new();
let mut waiters: Vec<Waiter> = (0..LIVE).map(|_| Waiter::noop()).collect();
let mut waiters: Vec<Waiter> = (0..LIVE).map(|_| Waiter::new(Waker::noop().clone())).collect();
for waiter in &waiters {
waiter.register(&mut list);
}
Expand All @@ -614,7 +631,7 @@ mod tests {
for _ in 0..rounds {
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
let i = (seed >> 33) as usize % LIVE;
waiters[i] = Waiter::noop();
waiters[i] = Waiter::new(Waker::noop().clone());
waiters[i].register(&mut list);
}

Expand All @@ -639,7 +656,7 @@ mod tests {
/// after one of them drains, while an unrecorded list appends on every call.
#[test]
fn overflow_keeps_recorded_lists_idempotent() {
let waiter = Waiter::noop();
let waiter = Waiter::new(Waker::noop().clone());
let mut lists: Vec<_> = (0..=PARKED).map(|_| WaiterList::new()).collect();
for _ in 0..3 {
for list in &mut lists {
Expand Down
79 changes: 72 additions & 7 deletions rs/moq-mux/benches/export.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
//! Container export cost swept over independently varied tracks and queued frames.
//! Container export cost swept over independently varied tracks and queued frames, and the
//! cost of a live export's poll that nothing new woke.
#![expect(
clippy::disallowed_methods,
clippy::disallowed_types,
reason = "`iter_custom` timings measure the host clock on purpose"
)]
use std::task::Poll;
use std::time::Duration;

use bytes::Bytes;
Expand All @@ -22,8 +29,8 @@ impl Drop for Fixture {
}
}

/// A finished broadcast of `tracks` audio tracks with `frames` queued frames each.
async fn prepare(tracks: usize, frames: usize) -> (moq_mux::Source, moq_mux::catalog::Consumer, Fixture) {
/// A broadcast of `tracks` audio tracks with `frames` queued frames each, finished unless `live`.
async fn prepare(tracks: usize, frames: usize, live: bool) -> (moq_mux::Source, moq_mux::catalog::Consumer, Fixture) {
let (origin, driver) = moq_net::origin::Producer::new(Default::default());
let driver = tokio::spawn(moq_net::time::run(driver));
let broadcast = origin.create_broadcast("bench").unwrap();
Expand Down Expand Up @@ -52,9 +59,13 @@ async fn prepare(tracks: usize, frames: usize) -> (moq_mux::Source, moq_mux::cat
})
.unwrap();
}
track.finish().unwrap();
if !live {
track.finish().unwrap();
}
}
if !live {
catalog.finish().unwrap();
}
catalog.finish().unwrap();
broadcast.announce(Default::default()).unwrap();
let resolved = origin.consume().request("bench", None).await.unwrap();
let source = moq_mux::Source::new(origin.consume(), resolved);
Expand All @@ -73,6 +84,17 @@ enum Export {
Ts(Box<moq_mux::container::ts::Export<moq_mux::catalog::Consumer>>),
}

impl Export {
/// Poll for the next chunk, ready with whether one came out rather than the export ending.
fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll<bool> {
match self {
Export::Flv(export) => export.poll_next(waiter).map(|res| res.unwrap().is_some()),
Export::Mkv(export) => export.poll_next(waiter).map(|res| res.unwrap().is_some()),
Export::Ts(export) => export.poll_next(waiter).map(|res| res.unwrap().is_some()),
}
}
}

fn open(format: &str, source: moq_mux::Source, stream: moq_mux::catalog::Consumer) -> Export {
match format {
"flv" => Export::Flv(Box::new(
Expand Down Expand Up @@ -132,7 +154,7 @@ fn bench_export(c: &mut Criterion) {
// Publishing and opening are setup; only draining the export is measured.
b.iter_batched(
|| {
let (source, stream, fixture) = runtime.block_on(prepare(tracks, frames));
let (source, stream, fixture) = runtime.block_on(prepare(tracks, frames, false));
(open(format, source, stream), fixture)
},
|(opened, fixture)| {
Expand All @@ -153,7 +175,50 @@ fn bench_export(c: &mut Criterion) {
group.finish();
}

criterion_group!(benches, bench_export, video_reorder);
/// A live export polled again from the same task with nothing new: the poll a task pays for
/// whenever something else wakes it. Swept over tracks, a slope is work repeated per track.
fn export_idle(c: &mut Criterion) {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let mut group = c.benchmark_group("container-export-idle");
group.sample_size(10);
group.measurement_time(Duration::from_secs(1));
for format in ["flv", "mkv", "ts"] {
for tracks in [1, 4, 16] {
group.throughput(Throughput::Elements(tracks as u64));
group.bench_function(BenchmarkId::new(format, tracks), |b| {
let (mut opened, _fixture) = runtime.block_on(async {
let (source, stream, fixture) = prepare(tracks, 4, true).await;
let mut opened = open(format, source, stream);
// Take what the export lets go now, giving the origin's tasks a turn between polls.
while let Ok(chunk) =
kio::clock::timeout(Duration::from_millis(10), kio::wait(|waiter| opened.poll_next(waiter)))
.await
{
assert!(chunk, "a live export ended");
}
(opened, fixture)
});
let mut park = kio::Park::default();
b.iter_custom(|iterations| {
runtime.block_on(std::future::poll_fn(|cx| {
let start = std::time::Instant::now();
for _ in 0..iterations {
// Each poll holds its waiter as `kio::wait` does.
assert!(std::hint::black_box(opened.poll_next(park.hold(cx))).is_pending());
}
Poll::Ready(start.elapsed())
}))
});
});
}
}
group.finish();
}

criterion_group!(benches, bench_export, video_reorder, export_idle);
criterion_main!(benches);

/// A progressive AVC SPS declaring a reorder depth of two (25 fps), as the catalog does.
Expand Down
15 changes: 10 additions & 5 deletions rs/moq-net/src/model/expiry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,15 @@ impl Wakes {
mod test {
use super::*;

/// A reader's waiter that stays registered while it lives: `kio::Waiter::noop` registers nothing.
fn parked() -> kio::Waiter {
kio::Waiter::new(std::task::Waker::noop().clone())
}

#[test]
fn landing_hint_keeps_both_indices_armed() {
let wakes = Wakes::default();
let waiter = kio::Waiter::noop();
let waiter = parked();
assert!(!wakes.landing_waiters.load(Ordering::Relaxed));
wakes.watch_landing(0, &waiter);
wakes.watch_held(10, &waiter);
Expand Down Expand Up @@ -397,7 +402,7 @@ mod test {
for sequence in 0..100 {
// The last switch's reader gave its group up.
drop(reader.take());
let waiter = kio::Waiter::noop();
let waiter = parked();
wakes.watch_held(sequence, &waiter);
reader = Some(waiter);
}
Expand All @@ -416,7 +421,7 @@ mod test {
let mut next = 0u64;
let mut park = |count: usize| {
for _ in 0..count {
let waiter = kio::Waiter::noop();
let waiter = parked();
wakes.watch_deadline(Timestamp::from_micros(next).unwrap(), Duration::ZERO, &waiter);
next += 1;
}
Expand Down Expand Up @@ -446,7 +451,7 @@ mod test {
media_sweeps += usize::from(parked.deadlines.len() >= parked.deadlines_sweep);
wall_sweeps += usize::from(parked.wall.len() >= parked.wall_sweep);
}
let waiter = kio::Waiter::noop();
let waiter = parked();
let budget = Duration::from_secs(1) + Duration::from_micros(i);
wakes.watch_deadline(Timestamp::from_secs(10).unwrap(), budget, &waiter);
wakes.watch_wall(now + budget, &waiter);
Expand All @@ -465,7 +470,7 @@ mod test {
let wakes = Wakes::default();
let now = crate::time::Instant::ZERO;
for i in 0..1_000u64 {
let waiter = kio::Waiter::noop();
let waiter = parked();
let budget = Duration::from_secs(1) + Duration::from_micros(i);
wakes.watch_deadline(Timestamp::from_secs(10).unwrap(), budget, &waiter);
wakes.watch_wall(now + budget, &waiter);
Expand Down
Loading
Loading