From 0e2fd8ff446a46a2396a4f5dcae6bd6e8a182b94 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:13:26 -0700 Subject: [PATCH 1/6] chore(quest): claim quest/m2/performance-survey/datagram-delivery.md Quest-Claim: 9487e34f156ee78f From 727ce7c9f0af2070e551cfe4e98492cb5366450c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:31:59 -0700 Subject: [PATCH 2/6] bench(net): measure complete datagram delivery Add `session_delivery_datagram` to the model and relay session benches, sweeping publishers x viewers, payload size and cadence on lite-06, lite-07-wip and moq-transport-22, with every datagram checked for its sequence and payload. `SESSION_ALLOCS=1` now also reports allocated bytes and, through a thread perf counter, user instructions per delivered datagram or viewer-group. Add a nightly moq-tokio benchmark sending datagrams below and above a bufferbloated bottleneck to a loopback viewer and a throttled one, reporting offered, received, lost, queue-dropped and reordered datagrams and their latency, and failing if the loopback viewer loses any. No optimization lands: the dominant per-datagram cost is the subscription's per-wake polling shared with groups, and draining buffered datagrams per step regresses the one-per-wake case. Co-Authored-By: Claude Opus 5.5 --- .config/nextest.toml | 6 + bench/README.md | 28 ++ quest/m2/audit-perf.md | 28 +- quest/m2/performance-survey/README.md | 1 - .../performance-survey/datagram-delivery.md | 32 -- rs/moq-net/benches/session.rs | 1 + rs/moq-net/benches/session/mod.rs | 282 ++++++++++++-- rs/moq-relay/benches/session.rs | 1 + .../tests/integration/datagram_delivery.rs | 346 ++++++++++++++++++ rs/moq-tokio/tests/integration/main.rs | 1 + .../tests/integration/receive_credit.rs | 4 +- sh/gh/nightly.sh | 2 +- 12 files changed, 651 insertions(+), 81 deletions(-) delete mode 100644 quest/m2/performance-survey/datagram-delivery.md create mode 100644 rs/moq-tokio/tests/integration/datagram_delivery.rs diff --git a/.config/nextest.toml b/.config/nextest.toml index 1a5f8ed28b2..3613c5ef960 100644 --- a/.config/nextest.toml +++ b/.config/nextest.toml @@ -49,6 +49,12 @@ slow-timeout = { period = "60s" } filter = "package(moq-tokio) & test(=receive_credit::benchmark_receive_windows)" slow-timeout = { period = "60s", terminate-after = 5 } +# The datagram benchmark publishes in real time and drains the bottleneck's backlog, +# per version and offered load. +[[profile.default.overrides]] +filter = "package(moq-tokio) & test(=datagram_delivery::benchmark_datagram_delivery)" +slow-timeout = { period = "60s", terminate-after = 3 } + [test-groups] io-uring = { max-threads = 4 } v4l2 = { max-threads = 1 } diff --git a/bench/README.md b/bench/README.md index 82427ba0cb9..844c84d91d1 100644 --- a/bench/README.md +++ b/bench/README.md @@ -54,6 +54,34 @@ SUBSCRIBE use uni data streams. No sleep supplies backpressure. Every case checks complete payload delivery. Compare paired runs with identical settings and disclose host load; CPU timing still depends on hardware contention. +Measure datagram delivery the same way, over publishers and viewers, payload +size, and cadence (datagrams per track between reads): + +```bash +nix develop --command cargo bench --locked -p moq-net --features fuzz --bench session -- session_delivery_datagram +nix develop --command env SESSION_ALLOCS=1 cargo bench --locked -p moq-net --features fuzz --bench session -- session_delivery +``` + +Throughput counts delivered datagrams. Every one must arrive with its sequence +and payload; the memory transport loses none at these cadences, so a drop fails +instead of looking cheaper. `SESSION_ALLOCS=1` prints allocations, allocated +bytes and, where Linux perf counters are readable, user instructions per +delivered datagram or viewer-group. Every task runs on the bench's one thread, +whose counts repeat within a fraction of a percent. The relay target runs the same shapes as +`relay_session_delivery_datagram`. + +Real QUIC adds congestion control and the sender's datagram queue. This sends +datagrams below and above a bufferbloated bottleneck to a loopback viewer and a +throttled one: + +```bash +nix develop --command just rs test -p moq-tokio --release -E 'test(benchmark_datagram_delivery)' --run-ignored only --success-output immediate +``` + +It reports offered, received and lost datagrams, bottleneck queue drops, reordered +arrivals and append-to-receive latency per viewer, and fails if the loopback +viewer loses any. It runs nightly. + Measure 50 fps audio at per-packet, 100 and 200 ms grouping, plus 400 fps (2.5 ms packets) at per-packet and the 20 ms default. Frames are 200 bytes: diff --git a/quest/m2/audit-perf.md b/quest/m2/audit-perf.md index b0313979e76..7bed385a1b5 100644 --- a/quest/m2/audit-perf.md +++ b/quest/m2/audit-perf.md @@ -163,34 +163,32 @@ follow-up quest. The m1 run added none of these: 1. [HLS request fan-out](/quest/m2/performance-survey/hls-serving.md) owns repeated segment fetch/remux and playlist rendering, swept over viewers, renditions and window segments. -2. [Session datagrams](/quest/m2/performance-survey/datagram-delivery.md) owns - complete adapter delivery across publishers and viewers. -3. LOC and CMAF wire containers in moq-mux: `containers` and `export` only use +2. LOC and CMAF wire containers in moq-mux: `containers` and `export` only use `Container::Legacy`; sweep container x frame size x frames. -4. [Buffered browser groups](/quest/m2/performance-survey/hang-buffered.md) +3. [Buffered browser groups](/quest/m2/performance-survey/hang-buffered.md) owns per-frame scans and bulk skip work in the Hang consumer. -5. [Browser media copies](/quest/m2/performance-survey/browser-media-copies.md) +4. [Browser media copies](/quest/m2/performance-survey/browser-media-copies.md) owns CMAF sample copies and LOC encode allocation. Generic `js/msf` and `js/flate` benchmark coverage remains here. -6. moq-quic loss recovery and real congestion controllers: every bench pins +5. moq-quic loss recovery and real congestion controllers: every bench pins `Unlimited`; sweep controller x flight x loss x streams. -7. [Announcement snapshots](/quest/m2/performance-survey/announced-snapshots.md) +6. [Announcement snapshots](/quest/m2/performance-survey/announced-snapshots.md) owns HTTP snapshot/rendering costs across routes and pollers. -8. [JWT verification](/quest/m2/performance-survey/jwt-verification.md) owns +7. [JWT verification](/quest/m2/performance-survey/jwt-verification.md) owns key-source I/O, import, lookup, parsing and crypto attribution. -9. MPEG-TS import with real repeated media (the CLI bench mostly skips null +8. MPEG-TS import with real repeated media (the CLI bench mostly skips null padding), and MKV, FLV, H.265, AV1 importers. -10. JS watch audio ring buffer and publish `Fanout` (lower impact). Include - capture-to-encoder staging: `audio/framer.ts` copies planes before - `audio/encoder.ts` joins them. AudioData already uses a transfer list; - do not count that transfer as another mandatory copy. Sweep publishers - x renditions, channels and frame duration in a browser. +9. JS watch audio ring buffer and publish `Fanout` (lower impact). Include + capture-to-encoder staging: `audio/framer.ts` copies planes before + `audio/encoder.ts` joins them. AudioData already uses a transfer list; + do not count that transfer as another mandatory copy. Sweep publishers + x renditions, channels and frame duration in a browser. The [2026-10-22 source survey](/quest/m2/performance-survey/README.md) owns the extracted items above and additional algorithmic hypotheses, with paired benchmarks required before optimization. This audit still owns nightly regression attribution and the remaining coverage. Its fragmented-MKV child -specializes item 9's MKV work. +specializes item 8's MKV work. Single-axis fan-out benches to widen: `session_delivery_paced` (viewers only), `track_fanout_*` (subscribers only), `memory` (lite-06 only), and diff --git a/quest/m2/performance-survey/README.md b/quest/m2/performance-survey/README.md index c25cce3d3b9..8df844a17d0 100644 --- a/quest/m2/performance-survey/README.md +++ b/quest/m2/performance-survey/README.md @@ -188,7 +188,6 @@ No duplicate quest or workflow edit belongs here. - [Transport preference history](/quest/m2/performance-survey/transport-history.md) - full URL retention across retired sessions - [Resume identity history](/quest/m2/performance-survey/resume-history.md) - historical source graphs held by first-seen track metadata - [UDP socket queues](/quest/m2/performance-survey/socket-queues.md) - drop attribution and queue-size tradeoffs across workers and load -- [Session datagrams](/quest/m2/performance-survey/datagram-delivery.md) - complete per-object delivery across publishers and viewers - [JWT verification](/quest/m2/performance-survey/jwt-verification.md) - key loading, lookup, parsing and crypto attribution - [Announcement snapshots](/quest/m2/performance-survey/announced-snapshots.md) - HTTP polling cost versus result size and unrelated routes - [Selective session operations](/quest/m2/performance-survey/session-selection.md) - exact-ID lookups and registry lock hold time diff --git a/quest/m2/performance-survey/datagram-delivery.md b/quest/m2/performance-survey/datagram-delivery.md deleted file mode 100644 index 65794781fb3..00000000000 --- a/quest/m2/performance-survey/datagram-delivery.md +++ /dev/null @@ -1,32 +0,0 @@ -# [M] Benchmark complete datagram delivery fan-out - -## Goal - -Session datagram costs are measured across independent publishers and -viewers, and any demonstrated avoidable per-object work is removed. - -## Plan - -The [lite publisher](/rs/moq-net/src/lite/publisher.rs) serializes and -schedules each delivered datagram in `serve_datagram`; the -[IETF adapter](/rs/moq-net/src/ietf/datagram.rs) has its own framing path. -Model-only datagram benches cannot attribute complete adapter delivery. -No regression is claimed merely because framing happens per subscriber. - -Extend session benchmarks with publishers × viewers, payload sizes and -object cadence, covering supported lite and IETF datagram paths. Measure -CPU/delivered object, allocations/copies and queue residence, with complete -sequence/payload validation. Separate the in-memory protocol cost from real -QUIC scheduling and congestion. Under bufferbloat record offered, received, -dropped and expired objects and slow-viewer isolation; best-effort loss must -not make lower CPU look like a gain. - -Keep framing, priority, expiry and memory-accounting behavior intact. The -decoded memory-accounting defect is fixed -([moq-dev/moq#6413](https://github.com/moq-dev/moq/pull/6413)). This quest owns the recurring audit's missing -session-level datagram measurement and any independent measured optimization. - -Follow the [survey's benchmark contract](/quest/m2/performance-survey/README.md): -measure the baseline before optimizing, retain paired evidence, verify equivalent -work, schedule coverage nightly, and accept a measured no-win. Expected public -API and wire impact: none; report the actual impact in the implementation PR. diff --git a/rs/moq-net/benches/session.rs b/rs/moq-net/benches/session.rs index 304cc1a23d3..df101a40e6d 100644 --- a/rs/moq-net/benches/session.rs +++ b/rs/moq-net/benches/session.rs @@ -9,6 +9,7 @@ criterion_group!( benches, session::session::, session::group_cost::, + session::datagram::, session::fetch_frames::, session::fetch_range::, session::range_updates::, diff --git a/rs/moq-net/benches/session/mod.rs b/rs/moq-net/benches/session/mod.rs index 2032db72277..1abb2e78db0 100644 --- a/rs/moq-net/benches/session/mod.rs +++ b/rs/moq-net/benches/session/mod.rs @@ -10,7 +10,8 @@ //! as a slope. Unwatched broadcasts stay announced and silent: their cost is the //! route table they occupy, not a publisher writing into its own cache. //! -//! `SESSION_ALLOCS=1` prints allocations per viewer-group instead of timing. +//! `SESSION_ALLOCS=1` prints allocations, allocated bytes and, where Linux perf counters are +//! readable, user instructions per viewer-group or delivered datagram instead of timing. //! `SESSION_CPU=1` measures thread CPU time on Unix instead of elapsed time, excluding //! preemption while retaining all protocol work on this current-thread runtime. CPU-timed //! groups have a `_cpu` suffix. Cache-clock advancement remains outside both clocks. @@ -28,6 +29,9 @@ //! `session_delivery_prefixes` holds silent broadcasts open on one viewer session beside the //! one it watches, against a fixed route table. Each is an announce request the session //! keeps, so a per-group cost that grows with them shows as a slope over `follow`. +//! `session_delivery_datagram` writes datagrams instead of groups: one per track per round +//! across publisher and viewer axes, then larger payloads and cadences. Throughput counts +//! delivered datagrams, each checked for its sequence and payload, so a lost one fails. //! `SESSION_EXPIRY_ONLY=1` uses the standalone 30s-expiry pool and reports groups //! written plus charged cache bytes every 10s during long profiling runs. Each round //! advances the cache clock by a second, so the expiry window spans 30 rounds. @@ -94,6 +98,9 @@ const VERSIONS: [&str; 3] = ["moq-lite-06", "moq-lite-07-wip", "moq-transport-22 /// Frames per group, so per-frame and per-group costs both appear. const FRAMES: usize = 4; +/// Distinct datagram payloads a datagram round cycles through by sequence. +const DATAGRAM_PAYLOADS: usize = 16; + const TRACK: &str = "video"; /// Lite 06 is what the dash session and cluster peers negotiate today. @@ -144,6 +151,9 @@ struct Shape { compress: bool, /// Read each group via a fresh FETCH rather than a live subscription. fetch: bool, + /// Deliver each round as `burst` datagrams of `frame` bytes per watched track instead of + /// groups. + datagrams: bool, } impl Shape { @@ -165,6 +175,7 @@ impl Shape { credit: None, fetch: false, compress: false, + datagrams: false, }; /// What each viewer subscribes with: a burst is a backlog, which the default @@ -177,6 +188,18 @@ impl Shape { self.publishers * self.broadcasts } + /// Datagrams every viewer reads in one datagram round. + fn objects(&self) -> usize { + self.viewers * self.watch * self.burst + } + + /// The broadcasts viewer `viewer` watches: a contiguous window starting at + /// `viewer * watch`, so viewers spread over the broadcasts before any doubles up. + fn watching(&self, viewer: usize) -> impl Iterator { + let (watch, total) = (self.watch, self.total()); + (0..watch).map(move |k| (viewer * watch + k) % total) + } + /// Frames each group gets per round. fn frames(&self) -> usize { if self.paced { 1 } else { self.group_frames } @@ -196,6 +219,12 @@ impl Shape { if self.follow > 0 { return format!("{version}/broadcasts={}/follow={}", self.broadcasts, self.follow); } + if self.datagrams { + return format!( + "{version}/publishers={}/viewers={}/payload={}/cadence={}", + self.publishers, self.viewers, self.frame, self.burst + ); + } if self.fetch && self.burst > 1 { return format!( "{version}/publishers={}/viewers={}/burst={}/fetch", @@ -244,6 +273,15 @@ struct Counter; static COUNTING: AtomicBool = AtomicBool::new(false); static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0); +/// Bytes requested by those allocations: every encode or copy into a fresh buffer shows here. +static ALLOCATED: AtomicUsize = AtomicUsize::new(0); + +fn count_allocation(bytes: usize) { + if COUNTING.load(Ordering::Relaxed) { + ALLOCATIONS.fetch_add(1, Ordering::Relaxed); + ALLOCATED.fetch_add(bytes, Ordering::Relaxed); + } +} #[global_allocator] static ALLOCATOR: Counter = Counter; @@ -251,23 +289,17 @@ static ALLOCATOR: Counter = Counter; // Counting only. Every call forwards to the system allocator unchanged. unsafe impl GlobalAlloc for Counter { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - if COUNTING.load(Ordering::Relaxed) { - ALLOCATIONS.fetch_add(1, Ordering::Relaxed); - } + count_allocation(layout.size()); unsafe { System.alloc(layout) } } unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { - if COUNTING.load(Ordering::Relaxed) { - ALLOCATIONS.fetch_add(1, Ordering::Relaxed); - } + count_allocation(layout.size()); unsafe { System.alloc_zeroed(layout) } } unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { - if COUNTING.load(Ordering::Relaxed) { - ALLOCATIONS.fetch_add(1, Ordering::Relaxed); - } + count_allocation(new_size); unsafe { System.realloc(ptr, layout, new_size) } } @@ -276,6 +308,81 @@ unsafe impl GlobalAlloc for Counter { } } +/// User-mode instructions this thread retires, from a Linux perf counter, for the +/// `SESSION_ALLOCS` report. Every task runs on the bench's current-thread runtime, so the +/// thread's count is all of the protocol work. Pin the process (`taskset -c`) to repeat it. +struct Instructions { + #[cfg(all(target_os = "linux", target_endian = "little"))] + fd: std::os::fd::OwnedFd, +} + +impl Instructions { + /// `None` without perf access, as under `perf_event_paranoid` 3 or in most containers. + #[cfg(all(target_os = "linux", target_endian = "little"))] + fn open() -> Option { + use std::os::fd::FromRawFd; + + /// `perf_event_attr` as of its first published size, which every kernel accepts. + #[repr(C)] + struct Attr { + kind: u32, + size: u32, + config: u64, + sample_period: u64, + sample_type: u64, + read_format: u64, + /// The flag bitfield, least significant bit first on a little-endian target. + flags: u64, + wakeup_events: u32, + bp_type: u32, + config1: u64, + } + const PERF_TYPE_HARDWARE: u32 = 0; + const PERF_COUNT_HW_INSTRUCTIONS: u64 = 1; + const EXCLUDE_KERNEL: u64 = 1 << 5; + const EXCLUDE_HV: u64 = 1 << 6; + let attr = Attr { + kind: PERF_TYPE_HARDWARE, + size: size_of::() as u32, + config: PERF_COUNT_HW_INSTRUCTIONS, + sample_period: 0, + sample_type: 0, + read_format: 0, + flags: EXCLUDE_KERNEL | EXCLUDE_HV, + wakeup_events: 0, + bp_type: 0, + config1: 0, + }; + // SAFETY: `attr` is a valid `perf_event_attr` of the size it declares; this thread, any CPU. + let fd = unsafe { libc::syscall(libc::SYS_perf_event_open, &attr, 0, -1, -1, 0) }; + // SAFETY: a nonnegative result is a new descriptor this process owns. + (fd >= 0).then(|| Self { + fd: unsafe { std::os::fd::OwnedFd::from_raw_fd(fd as i32) }, + }) + } + + #[cfg(not(all(target_os = "linux", target_endian = "little")))] + fn open() -> Option { + None + } + + #[cfg(all(target_os = "linux", target_endian = "little"))] + fn read(&self) -> u64 { + use std::os::fd::AsRawFd; + + let mut value = 0u64; + // SAFETY: reads one u64 count into `value`, which outlives the call. + let read = unsafe { libc::read(self.fd.as_raw_fd(), (&raw mut value).cast(), size_of::()) }; + assert_eq!(read, size_of::() as isize, "perf read: {}", std::io::Error::last_os_error()); + value + } + + #[cfg(not(all(target_os = "linux", target_endian = "little")))] + fn read(&self) -> u64 { + unreachable!("never opened") + } +} + fn runtime() -> tokio::runtime::Runtime { tokio::runtime::Builder::new_current_thread() .enable_time() @@ -599,8 +706,15 @@ struct Room { writing: Vec>, /// The group each viewer's subscriber is reading, when paced, flattened in viewer order. reading: Vec>, + /// Distinct datagram payloads, picked by sequence so a crossed or stale delivery fails. + payloads: Vec, + /// The sequence each broadcast's next datagram takes. + next: Vec, cache_now: moq_net::time::Instant, cpu: bool, + /// Counts the timed rounds' instructions into `retired` for the allocation report. + instructions: Option, + retired: u64, } impl Room { @@ -613,14 +727,10 @@ impl Room { assert!(!shape.fetch || !shape.paced, "fetch rounds read finished groups"); let mut cluster = Cluster::::new(version, shape).await; - // Viewer v watches a contiguous window starting at v * watch, so viewers - // spread over the broadcasts before any doubles up. let mut viewers = Vec::new(); let mut watched = vec![false; shape.total()]; for viewer_index in 0..shape.viewers { - let broadcasts: Vec<_> = (0..shape.watch) - .map(|k| (viewer_index * shape.watch + k) % shape.total()) - .collect(); + let broadcasts: Vec<_> = shape.watching(viewer_index).collect(); for &broadcast in &broadcasts { watched[broadcast] = true; } @@ -666,8 +776,14 @@ impl Room { groups: 0, writing: Vec::new(), reading: Vec::new(), + payloads: (0..DATAGRAM_PAYLOADS) + .map(|index| Bytes::from(vec![index as u8; shape.frame])) + .collect(), + next: vec![0; shape.total()], cache_now: moq_net::time::Instant::now(), cpu: std::env::var_os("SESSION_CPU").is_some(), + instructions: None, + retired: 0, }; room.writing.resize_with(room.watched.len(), || None); room.reading.resize_with(shape.viewers * shape.watch, || None); @@ -683,6 +799,11 @@ impl Room { .iter() .map(|viewer| viewer._pair.server_transport.blocked_writes()) .sum(); + if shape.datagrams { + // A group first: moq-transport binds a subscription's alias with it and drops a + // datagram that lands before then. + room.group_round().await; + } // Warm every path so the timed rounds skip first-group setup. for _ in 0..shape.group_frames / shape.frames() { room.tick(); @@ -749,6 +870,13 @@ impl Room { if self.shape.paced { return self.paced_round().await; } + if self.shape.datagrams { + return self.datagram_round().await; + } + self.group_round().await; + } + + async fn group_round(&mut self) { let constrained = self.shape.window < self.shape.frame * self.shape.group_frames; if constrained { for viewer in &mut self.viewers { @@ -803,6 +931,43 @@ impl Room { } assert_eq!(bytes, self.shape.expected()); self.groups += self.shape.burst * self.watched.len(); + for &broadcast in &self.watched { + self.next[broadcast] = newest[broadcast] + 1; + } + } + + /// `burst` datagrams into every watched track, then every one out to each viewer + /// watching it. The memory transport loses none below its queue bound, so each must + /// arrive in order with its payload: a dropped datagram fails rather than looking + /// cheaper. + async fn datagram_round(&mut self) { + for &broadcast in &self.watched { + let track = &mut self.cluster.tracks[broadcast]; + for _ in 0..self.shape.burst { + let sequence = self.next[broadcast]; + let payload = self.payloads[sequence as usize % DATAGRAM_PAYLOADS].clone(); + assert_eq!(track.append_datagram(Timestamp::ZERO, payload).unwrap(), sequence); + self.next[broadcast] = sequence + 1; + } + } + let burst = self.shape.burst as u64; + let mut delivered = 0; + for (index, viewer) in self.viewers.iter_mut().enumerate() { + for (subscriber, broadcast) in viewer.subscribers.iter_mut().zip(self.shape.watching(index)) { + let first = self.next[broadcast] - burst; + for sequence in first..first + burst { + let datagram = subscriber.recv_datagram().await.unwrap().expect("track ended"); + assert_eq!(datagram.sequence, sequence, "datagram lost or reordered"); + assert_eq!( + datagram.payload, + self.payloads[sequence as usize % DATAGRAM_PAYLOADS], + "datagram payload" + ); + delivered += 1; + } + } + } + assert_eq!(delivered, self.shape.objects()); } /// One frame into every watched group and one frame out to every viewer, so each @@ -849,7 +1014,11 @@ impl Room { for _ in 0..iters { self.tick(); let start = MeasurementClock::start(self.cpu); + let before = self.instructions.as_ref().map(Instructions::read); self.round().await; + if let (Some(counter), Some(before)) = (&self.instructions, before) { + self.retired += counter.read() - before; + } elapsed += start.elapsed(); if report && began.elapsed() >= next_report { eprintln!( @@ -879,7 +1048,11 @@ fn delivery(c: &mut Criterion, name: &str, shapes: impl IntoIterator // Built on first call: Criterion only calls a routine its filter // selects, and calls it again for every sample. let mut room = None; - group.throughput(Throughput::Bytes(shape.expected() as u64)); + group.throughput(if shape.datagrams { + Throughput::Elements(shape.objects() as u64) + } else { + Throughput::Bytes(shape.expected() as u64) + }); group.bench_function(BenchmarkId::from_parameter(shape.id(version)), |b| { let room = room.get_or_insert_with(|| rt.block_on(Room::::new(version, *shape))); b.iter_custom(|iters| rt.block_on(room.measure(iters))) @@ -910,6 +1083,34 @@ pub(crate) fn group_cost(c: &mut Criterion) { delivery::(c, "group_cost", shapes); } +/// Datagram delivery over independent publisher and viewer axes at a small payload and one +/// datagram per track per round, then payload size and cadence at 16 publishers and 16 +/// viewers. 64 per round fills a track's datagram send buffer exactly. +fn datagram_shapes() -> impl Iterator { + let base = Shape { + datagrams: true, + ..Shape::BASE + }; + let cost = [1, 16].into_iter().flat_map(move |publishers| { + [1, 16, 64].map(move |viewers| Shape { + publishers, + viewers, + ..base + }) + }); + let payload = [1024].map(|frame| Shape { frame, ..base }); + let cadence = [16, 64].map(|burst| Shape { burst, ..base }); + cost.chain(payload).chain(cadence) +} + +/// Complete datagram delivery: per-object cost across publishers, viewers, payload and cadence. +pub(crate) fn datagram(c: &mut Criterion) { + if std::env::var_os("SESSION_ALLOCS").is_some() || std::env::var_os("SESSION_EXPIRY_ONLY").is_some() { + return; + } + delivery::(c, "datagram", datagram_shapes()); +} + /// Cached FETCH write cost across independent publishers, viewers, and group lengths. pub(crate) fn fetch_frames(c: &mut Criterion) { if std::env::var_os("SESSION_ALLOCS").is_some() || std::env::var_os("SESSION_EXPIRY_ONLY").is_some() { @@ -1224,7 +1425,8 @@ fn dash(c: &mut Criterion, name: &str, shapes: impl IntoIterator() { const ROUNDS: usize = 256; @@ -1234,20 +1436,40 @@ fn allocations() { viewers: 256, ..Shape::BASE }; + let groups = [Shape::BASE, fanout] + .into_iter() + .flat_map(|shape| [false, true].map(|paced| Shape { paced, ..shape })); + let shapes: Vec<_> = groups.chain(datagram_shapes()).collect(); for version in VERSIONS { - for shape in [Shape::BASE, fanout] { - for paced in [false, true] { - let shape = Shape { paced, ..shape }; - let mut room = rt.block_on(Room::::new(version, shape)); - ALLOCATIONS.store(0, Ordering::Relaxed); - COUNTING.store(true, Ordering::Relaxed); - rt.block_on(room.measure(ROUNDS as u64)); - COUNTING.store(false, Ordering::Relaxed); - - let groups = ROUNDS * shape.viewers * shape.watch * shape.frames() / shape.group_frames; - let allocations = ALLOCATIONS.load(Ordering::Relaxed) as f64 / groups as f64; - println!("{}: {allocations:.1} allocations per viewer-group", shape.id(version)); - } + for &shape in &shapes { + let mut room = rt.block_on(Room::::new(version, shape)); + room.instructions = Instructions::open(); + ALLOCATIONS.store(0, Ordering::Relaxed); + ALLOCATED.store(0, Ordering::Relaxed); + COUNTING.store(true, Ordering::Relaxed); + rt.block_on(room.measure(ROUNDS as u64)); + COUNTING.store(false, Ordering::Relaxed); + + let (units, unit) = if shape.datagrams { + (ROUNDS * shape.objects(), "delivered datagram") + } else { + ( + ROUNDS * shape.viewers * shape.watch * shape.frames() / shape.group_frames, + "viewer-group", + ) + }; + let per = |count: usize| count as f64 / units as f64; + let allocations = per(ALLOCATIONS.load(Ordering::Relaxed)); + let bytes = per(ALLOCATED.load(Ordering::Relaxed)); + let instructions = room + .instructions + .as_ref() + .map(|_| format!(", {:.0} instructions", room.retired as f64 / units as f64)) + .unwrap_or_default(); + println!( + "{}: {allocations:.2} allocations, {bytes:.0} allocated bytes{instructions} per {unit}", + shape.id(version) + ); } } } diff --git a/rs/moq-relay/benches/session.rs b/rs/moq-relay/benches/session.rs index 30065306411..cf56f7ac8de 100644 --- a/rs/moq-relay/benches/session.rs +++ b/rs/moq-relay/benches/session.rs @@ -127,6 +127,7 @@ criterion_group!( benches, session::session::, session::group_cost::, + session::datagram::, session::fetch_frames::, session::fetch_range::, session::withdrawal:: diff --git a/rs/moq-tokio/tests/integration/datagram_delivery.rs b/rs/moq-tokio/tests/integration/datagram_delivery.rs new file mode 100644 index 00000000000..71d92d92a59 --- /dev/null +++ b/rs/moq-tokio/tests/integration/datagram_delivery.rs @@ -0,0 +1,346 @@ +//! Datagram delivery over native QUIC through a bufferbloated bottleneck. +//! +//! One stock server publishes a datagram track to two viewers: one on loopback and one +//! behind a rate limit below the offered bitrate, with a deep queue that drops what overflows +//! it. Datagrams are best-effort, so this records what each viewer got rather than timing +//! the work: the in-memory `session_delivery_datagram` benchmark owns protocol CPU, which +//! here would mix with QUIC, the shaper and the kernel. + +#![cfg(feature = "quic")] + +use std::{sync::Arc, time::Duration}; + +use bytes::Bytes; +use moq_tokio::moq_net::{self, Timestamp}; +use moq_tokio::quic::{driver, web}; + +use crate::receive_credit::{AnyCert, rand_seed}; + +const TIMEOUT: Duration = Duration::from_secs(10); + +/// How long a viewer waits for another datagram once the publisher has stopped. +const QUIET: Duration = Duration::from_secs(1); + +/// The slow viewer's bottleneck, both ways: half the overloaded cell's offered rate. +const BOTTLENECK: u64 = 2_000_000; + +/// Bytes per datagram payload: a typical full datagram once headers are added. +const PAYLOAD: usize = 1000; + +/// How long each cell publishes. +const RUN: Duration = Duration::from_secs(8); + +/// A bottleneck below the offered bitrate, behind a deep but finite queue that drops what +/// overflows it. No random loss: every drop is congestion. +fn bufferbloat() -> moq_shaper::Profile { + moq_shaper::Profile { + delay: Duration::from_millis(10), + rate: Some(moq_shaper::Rate { + bits_per_second: BOTTLENECK, + // One datagram, so a flight is paced rather than let through whole. + burst: 1500, + queue: Duration::from_millis(250), + }), + ..Default::default() + } +} + +fn timed() -> moq_net::track::Info { + moq_net::track::Info::default().with_timescale(moq_net::Timescale::MILLI) +} + +/// One offered load. +#[derive(Clone, Copy)] +struct Cell { + name: &'static str, + /// Time between datagrams. + interval: Duration, +} + +/// A datagram's payload: its index, then a fill derived from it, so a crossed, truncated +/// or corrupted delivery fails. +fn payload(index: u64) -> Bytes { + let mut bytes = vec![index as u8; PAYLOAD]; + bytes[..8].copy_from_slice(&index.to_le_bytes()); + Bytes::from(bytes) +} + +/// The index `payload` encoded, checking the rest of it. +fn decode(payload: &[u8]) -> u64 { + assert_eq!(payload.len(), PAYLOAD, "datagram truncated"); + let index = u64::from_le_bytes(payload[..8].try_into().unwrap()); + assert!(payload[8..].iter().all(|byte| *byte == index as u8), "datagram corrupted"); + index +} + +/// A viewer's session to the server, `through` a shaper or direct. +struct Viewer { + name: &'static str, + track: moq_net::track::Subscriber, + conn: driver::connection::Connection, + _session: moq_net::Session, + _endpoint: driver::endpoint::Endpoint, +} + +impl Viewer { + async fn connect(name: &'static str, version: moq_net::Version, addr: std::net::SocketAddr) -> Self { + let mut tls = rustls::ClientConfig::builder() + .dangerous() + .with_custom_certificate_verifier(Arc::new(AnyCert)) + .with_no_client_auth(); + tls.alpn_protocols = vec![version.alpn().as_bytes().to_vec()]; + let quic = driver::crypto::rustls::QuicClientConfig::try_from(tls).expect("quic config"); + let client = driver::client::Config::new(Arc::new(quic)); + let endpoint = + driver::endpoint::Endpoint::new(Default::default(), std::net::UdpSocket::bind("127.0.0.1:0").unwrap()) + .expect("endpoint"); + let conn = endpoint + .connect(&client, addr, "localhost") + .expect("connect") + .await + .expect("handshake"); + + let origin = moq_tokio::origin::spawn(); + let transport = + moq_tokio::transport::Session::new(web::Session::raw(conn.clone()), moq_net::transport::Address::Setup); + let (session, driver) = moq_net::Client::new() + .with_versions(version.into()) + .with_subscriber(origin.clone()) + .connect(moq_net::time::Instant::now(), transport) + .await + .expect("client handshake"); + tokio::spawn(moq_net::time::run(driver)); + + let consumer = origin.consume(); + let broadcast = kio::clock::timeout(TIMEOUT, consumer.follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves"); + let track = kio::clock::timeout(TIMEOUT, broadcast.track("audio").unwrap().subscribe(None)) + .await + .expect("subscribe timeout") + .expect("subscribe"); + Self { + name, + track, + conn, + _session: session, + _endpoint: endpoint, + } + } + + /// Read the priming group, which binds a moq-transport subscription's alias before any + /// datagram needs it. + async fn primed(&mut self) { + let mut group = kio::clock::timeout(TIMEOUT, self.track.recv_group()) + .await + .expect("group timed out") + .expect("group") + .expect("track ended"); + kio::clock::timeout(TIMEOUT, group.read_frame()) + .await + .expect("frame timed out") + .expect("frame") + .expect("group ended"); + } + + /// Every datagram until [`QUIET`] passes without one after `done` is set: each one's + /// index and arrival time. + async fn receive(mut self, done: Arc) -> (Self, Vec<(u64, kio::clock::Instant)>) { + let mut arrivals = Vec::new(); + let finished = done.notified(); + tokio::pin!(finished); + let mut stopping = false; + loop { + let next = self.track.recv_datagram(); + let datagram = if stopping { + match kio::clock::timeout(QUIET, next).await { + Ok(datagram) => datagram, + Err(_) => break, + } + } else { + tokio::select! { + datagram = next => datagram, + _ = &mut finished => { + stopping = true; + continue; + } + } + }; + let datagram = datagram.expect("datagram").expect("track ended"); + arrivals.push((decode(&datagram.payload), kio::clock::now())); + } + (self, arrivals) + } +} + +/// What one viewer got of `offered` datagrams sent at `sent`. +struct Report { + received: usize, + /// Arrivals with a lower index than one already received. + reordered: usize, + /// QUIC DATAGRAM frames the viewer's connection received. + frames: u64, + /// From the publisher's append to the viewer's receive, sorted. + latency: Vec, +} + +impl Report { + fn new(viewer: &Viewer, arrivals: &[(u64, kio::clock::Instant)], sent: &[kio::clock::Instant]) -> Self { + let mut seen = vec![false; sent.len()]; + let mut newest = None; + let mut reordered = 0; + let mut latency = Vec::with_capacity(arrivals.len()); + for &(index, arrived) in arrivals { + let slot = seen.get_mut(index as usize).expect("an index never offered"); + assert!(!*slot, "{}: datagram {index} delivered twice", viewer.name); + *slot = true; + if newest.is_some_and(|newest| index < newest) { + reordered += 1; + } + newest = newest.max(Some(index)); + latency.push(arrived.saturating_duration_since(sent[index as usize])); + } + latency.sort(); + Self { + received: arrivals.len(), + reordered, + frames: viewer.conn.stats().frame_rx.datagram, + latency, + } + } + + fn percentile(&self, p: usize) -> f64 { + self.latency + .get(self.latency.len().saturating_sub(1) * p / 100) + .map_or(f64::NAN, |latency| latency.as_secs_f64() * 1e3) + } +} + +/// Publish datagrams at `cell`'s rate to a loopback viewer and a bufferbloated one. +async fn measure(version: &str, cell: Cell) { + let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); + let version: moq_net::Version = version.parse().expect("version"); + + let publisher = moq_tokio::origin::spawn(); + let broadcast = publisher.create_broadcast("room").expect("broadcast"); + let mut track = broadcast.create_track("audio", timed()).unwrap(); + broadcast.announce(Default::default()).expect("announce"); + + let mut config = moq_tokio::listen::Config::default(); + config.bind = Some("127.0.0.1:0".parse().unwrap()); + config.tls.generate = vec!["localhost".into()]; + config.version = vec![version]; + let mut server = config + .init(Default::default()) + .expect("init server") + .listen() + .await + .expect("listen"); + let server_addr: std::net::SocketAddr = ([127, 0, 0, 1], server.local_addr().expect("addr").port()).into(); + let shaper = moq_shaper::Shaper::bind(moq_shaper::Config { + bind: "127.0.0.1:0".parse().unwrap(), + target: server_addr, + seed: rand_seed(), + up: bufferbloat(), + down: bufferbloat(), + }) + .await + .expect("start the shaper"); + let accept = tokio::spawn(async move { + loop { + let request = server.accept().await.expect("accept"); + let session = request.with_publisher(&publisher).ok().await.expect("session"); + tokio::spawn(async move { + let _ = session.closed().await; + }); + } + }); + + let mut viewers = [ + Viewer::connect("loopback", version, server_addr).await, + Viewer::connect("bufferbloat", version, shaper.addr()).await, + ]; + track + .write_frame(Timestamp::ZERO, &b"prime"[..]) + .expect("priming group"); + for viewer in &mut viewers { + viewer.primed().await; + } + + let done = Arc::new(tokio::sync::Notify::new()); + let receivers: Vec<_> = viewers + .into_iter() + .map(|viewer| tokio::spawn(viewer.receive(done.clone()))) + .collect(); + let offered = (RUN.as_nanos() / cell.interval.as_nanos()) as u64; + let mut sent = Vec::with_capacity(offered as usize); + let mut ticks = kio::clock::interval(cell.interval); + ticks.set_missed_tick_behavior(kio::clock::MissedTickBehavior::Burst); + for index in 0..offered { + ticks.tick().await; + sent.push(kio::clock::now()); + track + .append_datagram(Timestamp::from_millis(index).unwrap(), payload(index)) + .expect("append datagram"); + } + done.notify_waiters(); + + let shaped = shaper.verify().unwrap_or_else(|err| panic!("shaper: {err:#}")); + for receiver in receivers { + let (viewer, arrivals) = receiver.await.unwrap(); + let report = Report::new(&viewer, &arrivals, &sent); + let overflowed = if viewer.name == "bufferbloat" { + shaped.down.overflowed + } else { + 0 + }; + println!( + "{version},{},{},{offered},{},{},{overflowed},{},{},{:.1},{:.1},{:.1}", + cell.name, + viewer.name, + report.received, + offered as usize - report.received, + report.frames, + report.reordered, + report.percentile(50), + report.percentile(99), + report.percentile(100), + ); + if viewer.name == "loopback" { + // The slow viewer's queue and drops must not reach the other session. + assert_eq!(report.received as u64, offered, "{version}: the loopback viewer lost datagrams"); + } + } + accept.abort(); +} + +/// Datagram delivery to a loopback viewer and a bufferbloated one, below and above the +/// bottleneck's rate. Reports per viewer the datagrams offered, received, lost, those the +/// bottleneck's full queue dropped (UDP packets, so approximate), QUIC DATAGRAM frames +/// received, reordered arrivals, and append-to-receive latency percentiles. Nothing on the +/// path expires a datagram by age: what the sender's QUIC datagram buffer holds waits +/// there until it is sent or displaced. Fails if a datagram arrives twice or corrupted, or +/// if the loopback viewer loses any. Run by nightly. +#[tokio::test(flavor = "multi_thread")] +#[ignore = "release benchmark, run by nightly"] +async fn benchmark_datagram_delivery() { + let cells = [ + // 1 Mbit/s of payload: under the bottleneck. + Cell { + name: "under", + interval: Duration::from_millis(8), + }, + // 4 Mbit/s of payload: twice the bottleneck. + Cell { + name: "over", + interval: Duration::from_millis(2), + }, + ]; + println!("version,cell,viewer,offered,received,lost,overflowed,quic_frames,reordered,p50_ms,p99_ms,max_ms"); + for version in ["moq-lite-06", "moq-lite-07-wip", "moq-transport-22"] { + for cell in cells { + measure(version, cell).await; + } + } +} diff --git a/rs/moq-tokio/tests/integration/main.rs b/rs/moq-tokio/tests/integration/main.rs index faeb6ba00bb..2b4799984ac 100644 --- a/rs/moq-tokio/tests/integration/main.rs +++ b/rs/moq-tokio/tests/integration/main.rs @@ -6,6 +6,7 @@ mod backend; mod broadcast; mod close_code; +mod datagram_delivery; mod keylog; mod max_age; mod migrate; diff --git a/rs/moq-tokio/tests/integration/receive_credit.rs b/rs/moq-tokio/tests/integration/receive_credit.rs index a573ef35294..1f088f85254 100644 --- a/rs/moq-tokio/tests/integration/receive_credit.rs +++ b/rs/moq-tokio/tests/integration/receive_credit.rs @@ -153,7 +153,7 @@ async fn setup_through( } /// The shaper's seed: `MOQ_SHAPER_SEED` to replay a run, or a fresh one, printed. -fn rand_seed() -> u64 { +pub(crate) fn rand_seed() -> u64 { let seed = match std::env::var("MOQ_SHAPER_SEED") { Ok(seed) => seed.parse().expect("MOQ_SHAPER_SEED is not a u64"), Err(_) => kio::clock::wall() @@ -937,7 +937,7 @@ async fn benchmark_receive_windows() { /// Accepts any server certificate: the test dials its own self-signed server. #[derive(Debug)] -struct AnyCert; +pub(crate) struct AnyCert; impl rustls::client::danger::ServerCertVerifier for AnyCert { fn verify_server_cert( diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index ae8312bc9dd..83e61ec2aa4 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -82,7 +82,7 @@ case "$1" in --run-ignored only --success-output immediate run just rs bench-feed-fetch run just rs test -p moq-quic --release -E 'test(benchmark_receive_credit)' --run-ignored only --success-output immediate - run just rs test -p moq-tokio --release -E 'test(benchmark_receive_windows)' --run-ignored only --success-output immediate + run just rs test -p moq-tokio --release -E 'test(benchmark_receive_windows) | test(benchmark_datagram_delivery)' --run-ignored only --success-output immediate run just rs test -p moq-relay -E 'test(benchmark_stun_sources)' --run-ignored only --success-output immediate run just rs test -p moq-c -E 'test(benchmark_handles)' --run-ignored only --success-output immediate run just rs test -p moq-archive -p moq-mux -p moq-hls --no-fail-fast \ From 5633ec505114c25797587180ab66ef5b04219e02 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:02:02 -0800 Subject: [PATCH 3/6] style: format the datagram benchmarks Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/benches/session/mod.rs | 7 ++++++- rs/moq-tokio/tests/integration/datagram_delivery.rs | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/benches/session/mod.rs b/rs/moq-net/benches/session/mod.rs index 1abb2e78db0..f82d14a1f9d 100644 --- a/rs/moq-net/benches/session/mod.rs +++ b/rs/moq-net/benches/session/mod.rs @@ -373,7 +373,12 @@ impl Instructions { let mut value = 0u64; // SAFETY: reads one u64 count into `value`, which outlives the call. let read = unsafe { libc::read(self.fd.as_raw_fd(), (&raw mut value).cast(), size_of::()) }; - assert_eq!(read, size_of::() as isize, "perf read: {}", std::io::Error::last_os_error()); + assert_eq!( + read, + size_of::() as isize, + "perf read: {}", + std::io::Error::last_os_error() + ); value } diff --git a/rs/moq-tokio/tests/integration/datagram_delivery.rs b/rs/moq-tokio/tests/integration/datagram_delivery.rs index 71d92d92a59..08f97500f05 100644 --- a/rs/moq-tokio/tests/integration/datagram_delivery.rs +++ b/rs/moq-tokio/tests/integration/datagram_delivery.rs @@ -69,7 +69,10 @@ fn payload(index: u64) -> Bytes { fn decode(payload: &[u8]) -> u64 { assert_eq!(payload.len(), PAYLOAD, "datagram truncated"); let index = u64::from_le_bytes(payload[..8].try_into().unwrap()); - assert!(payload[8..].iter().all(|byte| *byte == index as u8), "datagram corrupted"); + assert!( + payload[8..].iter().all(|byte| *byte == index as u8), + "datagram corrupted" + ); index } @@ -309,7 +312,10 @@ async fn measure(version: &str, cell: Cell) { ); if viewer.name == "loopback" { // The slow viewer's queue and drops must not reach the other session. - assert_eq!(report.received as u64, offered, "{version}: the loopback viewer lost datagrams"); + assert_eq!( + report.received as u64, offered, + "{version}: the loopback viewer lost datagrams" + ); } } accept.abort(); From 9234af7d8755d6aa6c779451c5743592590799b1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 09:30:52 -0800 Subject: [PATCH 4/6] test(net): snapshot the shaper after the datagram viewers drain The shaper's drops and forwarding check now cover the same window as the received and lost columns. Co-Authored-By: Claude Opus 5.5 --- rs/moq-tokio/tests/integration/datagram_delivery.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rs/moq-tokio/tests/integration/datagram_delivery.rs b/rs/moq-tokio/tests/integration/datagram_delivery.rs index 08f97500f05..433aed40544 100644 --- a/rs/moq-tokio/tests/integration/datagram_delivery.rs +++ b/rs/moq-tokio/tests/integration/datagram_delivery.rs @@ -289,9 +289,14 @@ async fn measure(version: &str, cell: Cell) { } done.notify_waiters(); - let shaped = shaper.verify().unwrap_or_else(|err| panic!("shaper: {err:#}")); + // Drain every viewer before the shaper's snapshot, so its drops and any forwarding + // failure cover the same window as `received` and `lost`. + let mut finished = Vec::with_capacity(receivers.len()); for receiver in receivers { - let (viewer, arrivals) = receiver.await.unwrap(); + finished.push(receiver.await.unwrap()); + } + let shaped = shaper.verify().unwrap_or_else(|err| panic!("shaper: {err:#}")); + for (viewer, arrivals) in finished { let report = Report::new(&viewer, &arrivals, &sent); let overflowed = if viewer.name == "bufferbloat" { shaped.down.overflowed From 9607e2fb7ec3ef87062548ab5cd35c1ce5478adb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 09:02:18 -0800 Subject: [PATCH 5/6] bench(net): align delivery counters and isolate real-time runs Co-Authored-By: GPT-6.1 Sol --- bench/README.md | 7 ++- rs/moq-net/benches/session/mod.rs | 30 ++++++++---- .../tests/integration/datagram_delivery.rs | 18 +------ .../tests/integration/receive_credit.rs | 49 ++++++++++++------- sh/gh/nightly.sh | 3 +- 5 files changed, 60 insertions(+), 47 deletions(-) diff --git a/bench/README.md b/bench/README.md index 6935efc6889..917bb5a73d2 100644 --- a/bench/README.md +++ b/bench/README.md @@ -66,8 +66,11 @@ Throughput counts delivered datagrams. Every one must arrive with its sequence and payload; the memory transport loses none at these cadences, so a drop fails instead of looking cheaper. `SESSION_ALLOCS=1` prints allocations, allocated bytes and, where Linux perf counters are readable, user instructions per -delivered datagram or viewer-group. Every task runs on the bench's one thread, -whose counts repeat within a fraction of a percent. The relay target runs the same shapes as +delivered datagram or viewer-group. All three exclude the cache tick. Bytes sum +requested allocation and reallocation sizes, including growth in place; they +measure neither live memory nor bytes copied. A multiplexed instruction counter +fails the run instead of reporting an undercount. Every task runs on the bench's +one thread. Pin it to one CPU to compare repeated instruction counts. The relay target runs the same shapes as `relay_session_delivery_datagram`. Real QUIC adds congestion control and the sender's datagram queue. This sends diff --git a/rs/moq-net/benches/session/mod.rs b/rs/moq-net/benches/session/mod.rs index 38d08773352..46ea092f78f 100644 --- a/rs/moq-net/benches/session/mod.rs +++ b/rs/moq-net/benches/session/mod.rs @@ -279,7 +279,7 @@ struct Counter; static COUNTING: AtomicBool = AtomicBool::new(false); static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0); -/// Bytes requested by those allocations: every encode or copy into a fresh buffer shows here. +/// Cumulative requested allocation and reallocation sizes, not live or copied bytes. static ALLOCATED: AtomicUsize = AtomicUsize::new(0); fn count_allocation(bytes: usize) { @@ -345,6 +345,8 @@ impl Instructions { } const PERF_TYPE_HARDWARE: u32 = 0; const PERF_COUNT_HW_INSTRUCTIONS: u64 = 1; + const PERF_FORMAT_TOTAL_TIME_ENABLED: u64 = 1; + const PERF_FORMAT_TOTAL_TIME_RUNNING: u64 = 1 << 1; const EXCLUDE_KERNEL: u64 = 1 << 5; const EXCLUDE_HV: u64 = 1 << 6; let attr = Attr { @@ -353,7 +355,7 @@ impl Instructions { config: PERF_COUNT_HW_INSTRUCTIONS, sample_period: 0, sample_type: 0, - read_format: 0, + read_format: PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING, flags: EXCLUDE_KERNEL | EXCLUDE_HV, wakeup_events: 0, bp_type: 0, @@ -376,16 +378,21 @@ impl Instructions { fn read(&self) -> u64 { use std::os::fd::AsRawFd; - let mut value = 0u64; - // SAFETY: reads one u64 count into `value`, which outlives the call. - let read = unsafe { libc::read(self.fd.as_raw_fd(), (&raw mut value).cast(), size_of::()) }; + // PERF_FORMAT_GROUP is unset: value, time_enabled, time_running. + let mut values = [0u64; 3]; + // SAFETY: `values` holds the complete read format and outlives the call. + let read = unsafe { libc::read(self.fd.as_raw_fd(), values.as_mut_ptr().cast(), size_of_val(&values)) }; assert_eq!( read, - size_of::() as isize, + size_of_val(&values) as isize, "perf read: {}", std::io::Error::last_os_error() ); - value + assert_eq!( + values[1], values[2], + "perf instructions were multiplexed and undercounted" + ); + values[0] } #[cfg(not(all(target_os = "linux", target_endian = "little")))] @@ -1046,6 +1053,7 @@ impl Room { /// number of pools, not with the delivery being measured. async fn measure(&mut self, iters: u64) -> Duration { let mut elapsed = Duration::ZERO; + let counting = std::env::var_os("SESSION_ALLOCS").is_some(); let report = std::env::var_os("SESSION_EXPIRY_ONLY").is_some(); let began = Instant::now(); let mut next_report = Duration::from_secs(10); @@ -1053,7 +1061,13 @@ impl Room { self.tick(); let start = MeasurementClock::start(self.clock); let before = self.instructions.as_ref().map(Instructions::read); + if counting { + COUNTING.store(true, Ordering::Relaxed); + } self.round().await; + if counting { + COUNTING.store(false, Ordering::Relaxed); + } if let (Some(counter), Some(before)) = (&self.instructions, before) { self.retired += counter.read() - before; } @@ -1484,9 +1498,7 @@ fn allocations() { room.instructions = Instructions::open(); ALLOCATIONS.store(0, Ordering::Relaxed); ALLOCATED.store(0, Ordering::Relaxed); - COUNTING.store(true, Ordering::Relaxed); rt.block_on(room.measure(ROUNDS as u64)); - COUNTING.store(false, Ordering::Relaxed); let (units, unit) = if shape.datagrams { (ROUNDS * shape.objects(), "delivered datagram") diff --git a/rs/moq-tokio/tests/integration/datagram_delivery.rs b/rs/moq-tokio/tests/integration/datagram_delivery.rs index 433aed40544..164aa1d1aee 100644 --- a/rs/moq-tokio/tests/integration/datagram_delivery.rs +++ b/rs/moq-tokio/tests/integration/datagram_delivery.rs @@ -14,7 +14,7 @@ use bytes::Bytes; use moq_tokio::moq_net::{self, Timestamp}; use moq_tokio::quic::{driver, web}; -use crate::receive_credit::{AnyCert, rand_seed}; +use crate::receive_credit::{dial, rand_seed}; const TIMEOUT: Duration = Duration::from_secs(10); @@ -87,21 +87,7 @@ struct Viewer { impl Viewer { async fn connect(name: &'static str, version: moq_net::Version, addr: std::net::SocketAddr) -> Self { - let mut tls = rustls::ClientConfig::builder() - .dangerous() - .with_custom_certificate_verifier(Arc::new(AnyCert)) - .with_no_client_auth(); - tls.alpn_protocols = vec![version.alpn().as_bytes().to_vec()]; - let quic = driver::crypto::rustls::QuicClientConfig::try_from(tls).expect("quic config"); - let client = driver::client::Config::new(Arc::new(quic)); - let endpoint = - driver::endpoint::Endpoint::new(Default::default(), std::net::UdpSocket::bind("127.0.0.1:0").unwrap()) - .expect("endpoint"); - let conn = endpoint - .connect(&client, addr, "localhost") - .expect("connect") - .await - .expect("handshake"); + let (endpoint, conn) = dial(version, addr, Default::default()).await; let origin = moq_tokio::origin::spawn(); let transport = diff --git a/rs/moq-tokio/tests/integration/receive_credit.rs b/rs/moq-tokio/tests/integration/receive_credit.rs index 1f088f85254..2ac5eed67d8 100644 --- a/rs/moq-tokio/tests/integration/receive_credit.rs +++ b/rs/moq-tokio/tests/integration/receive_credit.rs @@ -106,30 +106,14 @@ async fn setup_through( ), None => None, }; - let dial = shaper.as_ref().map_or(server_addr, |shaper| shaper.addr()); + let addr = shaper.as_ref().map_or(server_addr, |shaper| shaper.addr()); tokio::spawn(async move { let request = server.accept().await.expect("accept"); let session = request.with_publisher(&publisher).ok().await.expect("session"); let _ = session.closed().await; }); - let mut tls = rustls::ClientConfig::builder() - .dangerous() - .with_custom_certificate_verifier(Arc::new(AnyCert)) - .with_no_client_auth(); - tls.alpn_protocols = vec![version.alpn().as_bytes().to_vec()]; - let quic = driver::crypto::rustls::QuicClientConfig::try_from(tls).expect("quic config"); - let mut client = driver::client::Config::new(Arc::new(quic)); - client.transport = transport; - - let endpoint = - driver::endpoint::Endpoint::new(Default::default(), std::net::UdpSocket::bind("127.0.0.1:0").unwrap()) - .expect("endpoint"); - let conn = endpoint - .connect(&client, dial, "localhost") - .expect("connect") - .await - .expect("handshake"); + let (endpoint, conn) = dial(version, addr, transport).await; let subscriber = moq_tokio::origin::spawn(); let session = @@ -152,6 +136,33 @@ async fn setup_through( } } +/// Dial native QUIC with the selected version and transport settings. +pub(crate) async fn dial( + version: moq_net::Version, + addr: std::net::SocketAddr, + transport: driver::transport::Config, +) -> (driver::endpoint::Endpoint, driver::connection::Connection) { + let mut tls = rustls::ClientConfig::builder() + .dangerous() + .with_custom_certificate_verifier(Arc::new(AnyCert)) + .with_no_client_auth(); + tls.alpn_protocols = vec![version.alpn().as_bytes().to_vec()]; + let quic = driver::crypto::rustls::QuicClientConfig::try_from(tls).expect("quic config"); + let mut client = driver::client::Config::new(Arc::new(quic)); + client.transport = transport; + + let endpoint = + driver::endpoint::Endpoint::new(Default::default(), std::net::UdpSocket::bind("127.0.0.1:0").unwrap()) + .expect("endpoint"); + let conn = endpoint + .connect(&client, addr, "localhost") + .expect("connect") + .await + .expect("handshake"); + + (endpoint, conn) +} + /// The shaper's seed: `MOQ_SHAPER_SEED` to replay a run, or a fresh one, printed. pub(crate) fn rand_seed() -> u64 { let seed = match std::env::var("MOQ_SHAPER_SEED") { @@ -937,7 +948,7 @@ async fn benchmark_receive_windows() { /// Accepts any server certificate: the test dials its own self-signed server. #[derive(Debug)] -pub(crate) struct AnyCert; +struct AnyCert; impl rustls::client::danger::ServerCertVerifier for AnyCert { fn verify_server_cert( diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index 277a7476fc9..f893829facf 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -82,7 +82,8 @@ case "$1" in --run-ignored only --success-output immediate run just rs bench-feed-fetch run just rs test -p moq-quic --release -E 'test(benchmark_receive_credit)' --run-ignored only --success-output immediate - run just rs test -p moq-tokio --release -E 'test(benchmark_receive_windows) | test(benchmark_datagram_delivery)' --run-ignored only --success-output immediate + run just rs test -p moq-tokio --release -E 'test(benchmark_receive_windows)' --run-ignored only --success-output immediate + run just rs test -p moq-tokio --release -E 'test(benchmark_datagram_delivery)' --run-ignored only --success-output immediate run just rs test -p moq-relay -E 'test(benchmark_stun_sources)' --run-ignored only --success-output immediate run just rs test -p moq-c -E 'test(benchmark_handles)' --run-ignored only --success-output immediate run just rs test -p moq-archive -p moq-mux -p moq-hls --no-fail-fast \ From 432ea3ba9549bb425c3e4355125de3e56c0a2e8a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 09:03:26 -0800 Subject: [PATCH 6/6] test(tokio): keep the shared certificate verifier visible Co-Authored-By: GPT-6.1 Sol --- rs/moq-tokio/tests/integration/receive_credit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-tokio/tests/integration/receive_credit.rs b/rs/moq-tokio/tests/integration/receive_credit.rs index 2ac5eed67d8..ddfa7205186 100644 --- a/rs/moq-tokio/tests/integration/receive_credit.rs +++ b/rs/moq-tokio/tests/integration/receive_credit.rs @@ -948,7 +948,7 @@ async fn benchmark_receive_windows() { /// Accepts any server certificate: the test dials its own self-signed server. #[derive(Debug)] -struct AnyCert; +pub(crate) struct AnyCert; impl rustls::client::danger::ServerCertVerifier for AnyCert { fn verify_server_cert(