From 4737b45e2a7c51cb75bed2fd40973e7c3bdc1c90 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:22:09 -0700 Subject: [PATCH 1/7] chore(quest): claim quest/m2/performance-survey/audio-batches.md Quest-Claim: 3ad22b0ad0b087b4 From 46ce27ae7e3f2fa73ebbe172d40a578edd07cba4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:31:14 -0700 Subject: [PATCH 2/7] bench(moq-audio): measure PCM batching through encode::Producer Sweep one constant duration of audio across batch sizes, channels, input rates and independent producers, with PCM isolating buffering from codec CPU. Print instructions, allocations and the slowest write, then time each case with Criterion. Run it nightly in the rust-extra leg, and check that batching leaves the published track unchanged. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + rs/moq-audio/Cargo.toml | 8 + rs/moq-audio/benches/batches.rs | 364 ++++++++++++++++++++++++++++ rs/moq-audio/src/encode/producer.rs | 66 +++++ sh/gh/nightly.sh | 4 +- 5 files changed, 441 insertions(+), 2 deletions(-) create mode 100644 rs/moq-audio/benches/batches.rs diff --git a/Cargo.lock b/Cargo.lock index df17b147ca5..992633b3722 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5353,6 +5353,7 @@ dependencies = [ "dispatch2", "hang", "kio 0.6.2", + "libc", "moq-mux", "moq-net", "objc2 0.6.5", diff --git a/rs/moq-audio/Cargo.toml b/rs/moq-audio/Cargo.toml index 61504477c24..ea9f934d090 100644 --- a/rs/moq-audio/Cargo.toml +++ b/rs/moq-audio/Cargo.toml @@ -140,6 +140,14 @@ serde = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "test-util"] } +# The batching bench reads its instruction counter through perf_event_open. +[target.'cfg(target_os = "linux")'.dev-dependencies] +libc = { workspace = true } + +[[bench]] +name = "batches" +harness = false + [[bench]] name = "bus" harness = false diff --git a/rs/moq-audio/benches/batches.rs b/rs/moq-audio/benches/batches.rs new file mode 100644 index 00000000000..4e71858aee4 --- /dev/null +++ b/rs/moq-audio/benches/batches.rs @@ -0,0 +1,364 @@ +//! PCM batching cost through [`encode::Producer`]: one constant duration of audio, +//! delivered in batches from tiny steady writes to a single burst, swept over channels, +//! input rates (with and without resampling) and independent producers. +//! +//! PCM is the codec, so buffering and resampling show instead of codec CPU; two Opus +//! cases put that cost beside a real codec's. Each case builds fresh producers, writes +//! every batch and finishes the track, so the partial tail and resampler flush count too. +//! +//! Run with `cargo bench -p moq-audio --bench batches`. A table comes first: user-space +//! instructions per input sample (Linux `perf_event_open`, `-` where unavailable), +//! allocations, allocated bytes and the slowest write of one counted pass. Criterion +//! then times each case. + +use std::alloc::{GlobalAlloc, Layout as AllocLayout, System}; +use std::hint::black_box; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::time::{Duration, Instant}; + +use bytes::Bytes; +use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group}; +use moq_audio::encode::{Codec, Config, Input, Options, Producer}; +use moq_audio::{Frame, Layout}; +use moq_net::Timestamp; + +/// Every case delivers this much audio, however it is batched. +const SECONDS: usize = 4; +/// The codec rate every case publishes at. +const CODEC_RATE: u32 = 48_000; + +struct Counter; + +static COUNTING: AtomicBool = AtomicBool::new(false); +static ALLOCATIONS: AtomicU64 = AtomicU64::new(0); +static ALLOCATED: AtomicU64 = AtomicU64::new(0); + +#[global_allocator] +static ALLOCATOR: Counter = Counter; + +impl Counter { + fn count(size: usize) { + if COUNTING.load(Ordering::Relaxed) { + ALLOCATIONS.fetch_add(1, Ordering::Relaxed); + ALLOCATED.fetch_add(size as u64, Ordering::Relaxed); + } + } +} + +unsafe impl GlobalAlloc for Counter { + unsafe fn alloc(&self, layout: AllocLayout) -> *mut u8 { + Self::count(layout.size()); + unsafe { System.alloc(layout) } + } + + unsafe fn alloc_zeroed(&self, layout: AllocLayout) -> *mut u8 { + Self::count(layout.size()); + unsafe { System.alloc_zeroed(layout) } + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: AllocLayout, new_size: usize) -> *mut u8 { + Self::count(new_size); + unsafe { System.realloc(ptr, layout, new_size) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: AllocLayout) { + unsafe { System.dealloc(ptr, layout) } + } +} + +/// User-space instructions retired by this thread, for a count that repeats run to run. +#[cfg(target_os = "linux")] +mod instructions { + /// `struct perf_event_attr` up to `PERF_ATTR_SIZE_VER0`. + #[repr(C)] + struct Attr { + kind: u32, + size: u32, + config: u64, + sample_period: u64, + sample_type: u64, + read_format: u64, + flags: u64, + wakeup_events: u32, + bp_type: u32, + config1: u64, + } + + const PERF_TYPE_HARDWARE: u32 = 0; + const PERF_COUNT_HW_INSTRUCTIONS: u64 = 1; + /// `disabled`, `exclude_kernel` and `exclude_hv`. + const FLAGS: u64 = 1 | (1 << 5) | (1 << 6); + const IOC_ENABLE: libc::c_ulong = 0x2400; + const IOC_DISABLE: libc::c_ulong = 0x2401; + const IOC_RESET: libc::c_ulong = 0x2403; + + pub struct Counter(libc::c_int); + + impl Counter { + pub fn new() -> Option { + // A hybrid CPU counts a generic event on one core type only, so a thread migrating + // to the other type would stop counting. Pin to a performance core and name its PMU. + let mut pmu = 0; + if let Ok(kind) = std::fs::read_to_string("/sys/devices/cpu_core/type") { + let cpus = std::fs::read_to_string("/sys/devices/cpu_core/cpus").ok()?; + let cpu: usize = cpus.split(['-', ',', '\n']).next()?.parse().ok()?; + pmu = kind.trim().parse::().ok()?; + // SAFETY: the set is initialized before use and outlives the call. + unsafe { + let mut set: libc::cpu_set_t = std::mem::zeroed(); + libc::CPU_SET(cpu, &mut set); + if libc::sched_setaffinity(0, size_of::(), &set) != 0 { + return None; + } + } + } + let attr = Attr { + kind: PERF_TYPE_HARDWARE, + size: size_of::() as u32, + // The PMU type goes in the high bits, as `PERF_PMU_TYPE_SHIFT` describes. + config: (pmu << 32) | PERF_COUNT_HW_INSTRUCTIONS, + sample_period: 0, + sample_type: 0, + read_format: 0, + flags: FLAGS, + wakeup_events: 0, + bp_type: 0, + config1: 0, + }; + // SAFETY: the attribute outlives the call, and its size field names its layout. + let fd = unsafe { libc::syscall(libc::SYS_perf_event_open, &attr, 0, -1, -1, 0) }; + (fd >= 0).then(|| Self(fd as libc::c_int)) + } + + pub fn start(&self) { + // SAFETY: plain ioctls on a perf event descriptor this counter owns. + unsafe { + libc::ioctl(self.0, IOC_RESET, 0); + libc::ioctl(self.0, IOC_ENABLE, 0); + } + } + + pub fn stop(&self) -> u64 { + let mut count = 0u64; + // SAFETY: the descriptor reads one u64 into a buffer of that size. + let read = unsafe { + libc::ioctl(self.0, IOC_DISABLE, 0); + libc::read(self.0, (&raw mut count).cast(), size_of::()) + }; + assert_eq!(read, size_of::() as isize, "perf counter read"); + count + } + } + + impl Drop for Counter { + fn drop(&mut self) { + // SAFETY: the descriptor is owned and closed once. + unsafe { libc::close(self.0) }; + } + } +} + +#[cfg(not(target_os = "linux"))] +mod instructions { + pub struct Counter; + + impl Counter { + pub fn new() -> Option { + None + } + pub fn start(&self) {} + pub fn stop(&self) -> u64 { + 0 + } + } +} + +#[derive(Clone, Copy)] +struct Case { + codec: Codec, + input_rate: u32, + layout: Layout, + /// Input frames (per channel) in each write; the last write may be shorter. + batch: usize, + producers: usize, +} + +impl Case { + fn frames(&self) -> usize { + SECONDS * self.input_rate as usize + } + + fn samples(&self) -> u64 { + (self.frames() * self.layout.channels() as usize * self.producers) as u64 + } + + fn name(&self) -> String { + let batch = if self.batch >= self.frames() { + "burst".to_string() + } else { + self.batch.to_string() + }; + format!( + "{}/{}hz/{}ch/batch_{batch}/producers_{}", + self.codec, + self.input_rate, + self.layout.channels(), + self.producers + ) + } + + /// The batches every producer writes, built outside the measured section. + fn input(&self) -> Vec { + let channels = self.layout.channels() as usize; + let samples: Vec = (0..self.frames() * channels) + .map(|index| (index as f32 * 0.01).sin() * 0.25) + .collect(); + samples + .chunks(self.batch * channels) + .enumerate() + .map(|(index, chunk)| { + let frames = (index * self.batch) as u64; + let timestamp = Timestamp::from_micros(frames * 1_000_000 / self.input_rate as u64).unwrap(); + let data: Vec = chunk.iter().flat_map(|sample| sample.to_le_bytes()).collect(); + Frame::new(Bytes::from(data), timestamp) + }) + .collect() + } + + /// Fresh producers on one broadcast, kept alive so teardown stays outside the measurement. + fn producers(&self) -> (moq_net::broadcast::Producer, Vec) { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); + let input = Input::new(self.input_rate, self.layout); + let mut options = Options::default(); + options.config = Config::from_input(self.codec, &input); + options.config.sample_rate = CODEC_RATE; + let producers = (0..self.producers) + .map(|_| Producer::new(&mut broadcast, catalog.clone(), input.clone(), &options).unwrap()) + .collect(); + (broadcast, producers) + } +} + +/// Write every batch round-robin across the producers, then finish them, returning the slowest write. +fn run(producers: &mut [Producer], input: &[Frame]) -> Duration { + let mut slowest = Duration::ZERO; + for frame in input { + for producer in producers.iter_mut() { + let start = Instant::now(); + producer.write(black_box(frame)).unwrap(); + slowest = slowest.max(start.elapsed()); + } + } + for producer in producers.iter_mut() { + producer.finish().unwrap(); + } + slowest +} + +fn cases() -> Vec { + let case = |codec, input_rate: u32, layout, batch, producers| Case { + codec, + input_rate, + layout, + batch, + producers, + }; + let rates = [48_000, 44_100, 96_000]; + // 32 frames, 10 ms, 100 ms, one second, then all of it in one write. + let batches = |rate: u32| { + let rate = rate as usize; + [32, rate / 100, rate / 10, rate, SECONDS * rate] + }; + + let mut cases = Vec::new(); + for rate in rates { + for batch in batches(rate) { + cases.push(case(Codec::Pcm, rate, Layout::Stereo, batch, 1)); + } + } + for layout in [Layout::Mono, Layout::FivePointOne] { + for rate in rates { + let [_, steady, _, _, burst] = batches(rate); + for batch in [steady, burst] { + cases.push(case(Codec::Pcm, rate, layout, batch, 1)); + } + } + } + for rate in [48_000, 44_100] { + let [_, steady, _, _, burst] = batches(rate); + for batch in [steady, burst] { + cases.push(case(Codec::Pcm, rate, Layout::Stereo, batch, 8)); + cases.push(case(Codec::Opus, rate, Layout::Stereo, batch, 1)); + } + } + cases +} + +fn counts() { + let counter = instructions::Counter::new(); + println!( + "{:<44} {:>12} {:>10} {:>12} {:>12}", + "case", "insn/sample", "allocs", "alloc_bytes", "slowest_us" + ); + for case in cases() { + let input = case.input(); + let (broadcast, mut producers) = case.producers(); + ALLOCATIONS.store(0, Ordering::Relaxed); + ALLOCATED.store(0, Ordering::Relaxed); + COUNTING.store(true, Ordering::Relaxed); + if let Some(counter) = &counter { + counter.start(); + } + let slowest = run(&mut producers, &input); + let instructions = counter.as_ref().map(instructions::Counter::stop); + COUNTING.store(false, Ordering::Relaxed); + drop((producers, broadcast)); + + let per_sample = match instructions { + Some(instructions) => format!("{:.1}", instructions as f64 / case.samples() as f64), + None => "-".to_string(), + }; + println!( + "{:<44} {per_sample:>12} {:>10} {:>12} {:>12.1}", + case.name(), + ALLOCATIONS.load(Ordering::Relaxed), + ALLOCATED.load(Ordering::Relaxed), + slowest.as_secs_f64() * 1e6, + ); + } +} + +fn bench_batches(c: &mut Criterion) { + let mut group = c.benchmark_group("pcm_batches"); + group.sample_size(10); + group.warm_up_time(Duration::from_millis(500)); + group.measurement_time(Duration::from_secs(2)); + for case in cases() { + let input = case.input(); + group.throughput(Throughput::Elements(case.samples())); + group.bench_function(BenchmarkId::from_parameter(case.name()), |b| { + b.iter_batched( + || case.producers(), + |(broadcast, mut producers)| { + run(&mut producers, &input); + (broadcast, producers) + }, + BatchSize::PerIteration, + ); + }); + } + group.finish(); +} + +criterion_group!(benches, bench_batches); + +fn main() { + // Nextest lists all targets as potential test binaries. + if std::env::args().any(|arg| arg == "--list") { + return; + } + counts(); + benches(); + Criterion::default().configure_from_args().final_summary(); +} diff --git a/rs/moq-audio/src/encode/producer.rs b/rs/moq-audio/src/encode/producer.rs index b7de46d10a2..8f71c13a9e2 100644 --- a/rs/moq-audio/src/encode/producer.rs +++ b/rs/moq-audio/src/encode/producer.rs @@ -760,6 +760,72 @@ mod tests { } } + /// Batching cannot change what is published: every packet, timestamp, group boundary + /// and the flushed tail match whether PCM arrives a frame at a time or in one burst, + /// with or without resampling. + #[tokio::test] + async fn batch_size_does_not_change_the_published_track() { + for input_rate in [48_000u32, 44_100] { + let input = Input::new(input_rate, Layout::Stereo); + // Not a whole number of codec frames, so finish flushes a partial tail. + let frames = input_rate as usize + 123; + let samples: Vec = (0..frames * 2).map(|index| (index as f32 * 0.01).sin() * 0.25).collect(); + let mut reference = None; + for batch in [1, 7, 959, 961, 1921, 4410, frames] { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = + moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); + let consumer = broadcast.consume(); + let mut options = Options { + track: Some("audio".to_string()), + config: Config::from_input(super::super::Codec::Pcm, &input), + ..Options::default() + }; + options.config.sample_rate = 48_000; + let mut producer = Producer::new(&mut broadcast, catalog, input.clone(), &options).unwrap(); + let track = consumer + .track("audio") + .unwrap() + .subscribe(moq_net::track::Subscription::default().with_max_delay(Duration::from_secs(60))) + .await + .unwrap(); + let mut reader = moq_mux::container::Consumer::new( + track, + moq_mux::container::legacy::Wire(moq_mux::container::Kind::Audio), + ); + + for (index, chunk) in samples.chunks(batch * 2).enumerate() { + let at = (index * batch) as u64 * 1_000_000 / input_rate as u64; + producer.write(&pcm_frame(chunk, 1_000_000 + at)).unwrap(); + } + producer.finish().unwrap(); + + let mut published = Vec::new(); + while let Some(read) = reader.read().await.unwrap() { + published.push(match read { + moq_mux::container::Read::Frame { group, frame } => { + (format!("{group} {:?} {}", frame.timestamp, frame.keyframe), frame.payload) + } + other => (format!("{other:?}"), Bytes::new()), + }); + } + if input_rate == 48_000 { + // PCM at the codec rate publishes exactly the input, its final frame zero-padded. + let payload: Vec = published.iter().flat_map(|(_, payload)| payload.to_vec()).collect(); + let expected: Vec = samples.iter().flat_map(|sample| sample.to_le_bytes()).collect(); + assert_eq!(payload.len(), expected.len().next_multiple_of(1920 * 4), "batch={batch}"); + assert_eq!(payload[..expected.len()], expected[..], "batch={batch}"); + assert!(payload[expected.len()..].iter().all(|&byte| byte == 0), "batch={batch}"); + } + assert_eq!( + &published, + reference.get_or_insert_with(|| published.clone()), + "{input_rate} Hz, batch={batch}" + ); + } + } + } + /// A resampled publisher used to end its track early: `finish` flushed the /// encoder's own buffer but left the resampler holding its last partial chunk, /// plus the audio its filter runs behind on. diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index ae8312bc9dd..79792d3d8d1 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -120,13 +120,13 @@ case "$1" in ;; # Keep added coverage out of the rust-bench and rust-shared legs. rust-extra) - # The allocation/window/play-buffer tables keep their stdout in the run; + # The allocation/window/play-buffer/batch tables keep their stdout in the run; # only Criterion estimates enter the trend report. criterion extra --no-fail-fast -p moq-e2ee -p moq-sign -p moq-json -p moq-archive -p moq-cli -p moq-mux -p moq-audio \ -p moq-quic --features moq-quic/fuzz \ --bench protect --bench sign --bench codec --bench table --bench allocations --bench window \ --bench producer --bench play_buffer --bench jitter --bench decode_clock --bench ts_programs --bench ts_video \ - --bench bus --bench parameter_sets --bench sent_packets + --bench bus --bench batches --bench parameter_sets --bench sent_packets ;; rust-uring) # One run per target makes an unsupported/skipped ring fail report From 4fa62dc8de0ca0376b54af89bf9144150cb0f905 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:06:34 -0800 Subject: [PATCH 3/7] bench(moq-audio): count batch instructions with perf-event2 on every core type A hybrid CPU counts a generic event on one core type only, so sum one counter per core PMU instead of hand-rolling perf_event_open and pinning. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 43 +++++++++++- rs/moq-audio/Cargo.toml | 4 +- rs/moq-audio/benches/batches.rs | 119 +++++++++++--------------------- 3 files changed, 83 insertions(+), 83 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 992633b3722..c32762e0c55 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1136,6 +1136,12 @@ dependencies = [ "bytes", ] +[[package]] +name = "c-enum" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd17eb909a8c6a894926bfcc3400a4bb0e732f5a57d37b1f14e8b29e329bace8" + [[package]] name = "calloop" version = "0.13.0" @@ -5353,7 +5359,6 @@ dependencies = [ "dispatch2", "hang", "kio 0.6.2", - "libc", "moq-mux", "moq-net", "objc2 0.6.5", @@ -5362,6 +5367,7 @@ dependencies = [ "objc2-core-media", "objc2-foundation 0.3.2", "objc2-screen-capture-kit", + "perf-event2", "rand 0.10.3", "ringbuf", "rubato", @@ -7967,6 +7973,41 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "perf-event-data" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "575828d9d7d205188048eb1508560607a03d21eafdbba47b8cade1736c1c28e1" +dependencies = [ + "bitflags 2.13.2", + "c-enum", + "perf-event-open-sys2", +] + +[[package]] +name = "perf-event-open-sys2" +version = "5.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c25955321465255e437600b54296983fab1feac2cd0c38958adeb26dbae49e" +dependencies = [ + "libc", + "memoffset", +] + +[[package]] +name = "perf-event2" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0939b8fad77dfaeb29ebbd35faaeaadbf833167f30975f1b8993bbba09ea0a0f" +dependencies = [ + "bitflags 2.13.2", + "c-enum", + "libc", + "memmap2", + "perf-event-data", + "perf-event-open-sys2", +] + [[package]] name = "pest" version = "2.9.2" diff --git a/rs/moq-audio/Cargo.toml b/rs/moq-audio/Cargo.toml index ea9f934d090..369acc985f1 100644 --- a/rs/moq-audio/Cargo.toml +++ b/rs/moq-audio/Cargo.toml @@ -140,9 +140,9 @@ serde = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "test-util"] } -# The batching bench reads its instruction counter through perf_event_open. +# The batching bench counts the instructions each case retires. [target.'cfg(target_os = "linux")'.dev-dependencies] -libc = { workspace = true } +perf-event2 = "0.7.4" [[bench]] name = "batches" diff --git a/rs/moq-audio/benches/batches.rs b/rs/moq-audio/benches/batches.rs index 4e71858aee4..0cbab627f6e 100644 --- a/rs/moq-audio/benches/batches.rs +++ b/rs/moq-audio/benches/batches.rs @@ -69,91 +69,50 @@ unsafe impl GlobalAlloc for Counter { /// User-space instructions retired by this thread, for a count that repeats run to run. #[cfg(target_os = "linux")] mod instructions { - /// `struct perf_event_attr` up to `PERF_ATTR_SIZE_VER0`. - #[repr(C)] - struct Attr { - kind: u32, - size: u32, - config: u64, - sample_period: u64, - sample_type: u64, - read_format: u64, - flags: u64, - wakeup_events: u32, - bp_type: u32, - config1: u64, - } - - const PERF_TYPE_HARDWARE: u32 = 0; - const PERF_COUNT_HW_INSTRUCTIONS: u64 = 1; - /// `disabled`, `exclude_kernel` and `exclude_hv`. - const FLAGS: u64 = 1 | (1 << 5) | (1 << 6); - const IOC_ENABLE: libc::c_ulong = 0x2400; - const IOC_DISABLE: libc::c_ulong = 0x2401; - const IOC_RESET: libc::c_ulong = 0x2403; + use perf_event::events::Hardware; - pub struct Counter(libc::c_int); + /// One counter per core PMU, because a hybrid CPU counts a generic event on one core + /// type only. Retired instructions are architectural, so the sum is exact wherever the + /// thread runs. + pub struct Counter(Vec); impl Counter { pub fn new() -> Option { - // A hybrid CPU counts a generic event on one core type only, so a thread migrating - // to the other type would stop counting. Pin to a performance core and name its PMU. - let mut pmu = 0; - if let Ok(kind) = std::fs::read_to_string("/sys/devices/cpu_core/type") { - let cpus = std::fs::read_to_string("/sys/devices/cpu_core/cpus").ok()?; - let cpu: usize = cpus.split(['-', ',', '\n']).next()?.parse().ok()?; - pmu = kind.trim().parse::().ok()?; - // SAFETY: the set is initialized before use and outlives the call. - unsafe { - let mut set: libc::cpu_set_t = std::mem::zeroed(); - libc::CPU_SET(cpu, &mut set); - if libc::sched_setaffinity(0, size_of::(), &set) != 0 { - return None; - } - } - } - let attr = Attr { - kind: PERF_TYPE_HARDWARE, - size: size_of::() as u32, - // The PMU type goes in the high bits, as `PERF_PMU_TYPE_SHIFT` describes. - config: (pmu << 32) | PERF_COUNT_HW_INSTRUCTIONS, - sample_period: 0, - sample_type: 0, - read_format: 0, - flags: FLAGS, - wakeup_events: 0, - bp_type: 0, - config1: 0, + let hybrid: Vec = ["cpu_core", "cpu_atom"] + .iter() + .filter_map(|pmu| std::fs::read_to_string(format!("/sys/devices/{pmu}/type")).ok()) + .map(|kind| kind.trim().parse().ok()) + .collect::>()?; + // The PMU type goes in the high bits, as `PERF_PMU_TYPE_SHIFT` describes. + let events = match hybrid.is_empty() { + true => vec![Hardware::INSTRUCTIONS], + false => hybrid + .into_iter() + .map(|pmu| Hardware(pmu << 32 | Hardware::INSTRUCTIONS.0)) + .collect(), }; - // SAFETY: the attribute outlives the call, and its size field names its layout. - let fd = unsafe { libc::syscall(libc::SYS_perf_event_open, &attr, 0, -1, -1, 0) }; - (fd >= 0).then(|| Self(fd as libc::c_int)) + let counters = events + .into_iter() + .map(|event| perf_event::Builder::new(event).build().ok()) + .collect::>()?; + Some(Self(counters)) } - pub fn start(&self) { - // SAFETY: plain ioctls on a perf event descriptor this counter owns. - unsafe { - libc::ioctl(self.0, IOC_RESET, 0); - libc::ioctl(self.0, IOC_ENABLE, 0); + pub fn start(&mut self) { + for counter in &mut self.0 { + counter.reset().unwrap(); + counter.enable().unwrap(); } } - pub fn stop(&self) -> u64 { - let mut count = 0u64; - // SAFETY: the descriptor reads one u64 into a buffer of that size. - let read = unsafe { - libc::ioctl(self.0, IOC_DISABLE, 0); - libc::read(self.0, (&raw mut count).cast(), size_of::()) - }; - assert_eq!(read, size_of::() as isize, "perf counter read"); - count - } - } - - impl Drop for Counter { - fn drop(&mut self) { - // SAFETY: the descriptor is owned and closed once. - unsafe { libc::close(self.0) }; + pub fn stop(&mut self) -> u64 { + self.0 + .iter_mut() + .map(|counter| { + counter.disable().unwrap(); + counter.read().unwrap() + }) + .sum() } } } @@ -166,8 +125,8 @@ mod instructions { pub fn new() -> Option { None } - pub fn start(&self) {} - pub fn stop(&self) -> u64 { + pub fn start(&mut self) {} + pub fn stop(&mut self) -> u64 { 0 } } @@ -296,7 +255,7 @@ fn cases() -> Vec { } fn counts() { - let counter = instructions::Counter::new(); + let mut counter = instructions::Counter::new(); println!( "{:<44} {:>12} {:>10} {:>12} {:>12}", "case", "insn/sample", "allocs", "alloc_bytes", "slowest_us" @@ -307,11 +266,11 @@ fn counts() { ALLOCATIONS.store(0, Ordering::Relaxed); ALLOCATED.store(0, Ordering::Relaxed); COUNTING.store(true, Ordering::Relaxed); - if let Some(counter) = &counter { + if let Some(counter) = &mut counter { counter.start(); } let slowest = run(&mut producers, &input); - let instructions = counter.as_ref().map(instructions::Counter::stop); + let instructions = counter.as_mut().map(instructions::Counter::stop); COUNTING.store(false, Ordering::Relaxed); drop((producers, broadcast)); From 2cc2f434723e626255ac31d4b2ab9457c65f1e72 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:06:37 -0800 Subject: [PATCH 4/7] perf(moq-audio): move the PCM remainder once per batch Producer::publish_full_frames and Resampler::convert drained the front of their buffer for every codec frame or filter chunk, moving the rest each time, so one large batch copied a triangular number of samples. Read each frame in place and compact only the remainder at the end. The producer no longer allocates a Vec per packet either. Co-Authored-By: Claude Opus 5.5 --- rs/moq-audio/src/encode/producer.rs | 62 ++++++++++++++++++++--------- rs/moq-audio/src/resample.rs | 59 +++++++++++++++++---------- 2 files changed, 80 insertions(+), 41 deletions(-) diff --git a/rs/moq-audio/src/encode/producer.rs b/rs/moq-audio/src/encode/producer.rs index 8f71c13a9e2..e9abfa71285 100644 --- a/rs/moq-audio/src/encode/producer.rs +++ b/rs/moq-audio/src/encode/producer.rs @@ -393,24 +393,41 @@ impl Producer { /// Encode and publish every full frame in `pending`, keeping any partial /// trailing frame for the next call. + /// + /// Frames are encoded in place and the remainder moves to the front once, so a + /// large batch costs linear rather than quadratic copying. fn publish_full_frames(&mut self, anchor_us: u64) -> Result<(), Error> { let frame_samples = self.encoder()?.frame_size() * self.encoder()?.codec_channels() as usize; - while self.pending.len() >= frame_samples { - let chunk: Vec = self.pending.drain(..frame_samples).collect(); - let packet = self.encoder.as_mut().ok_or(moq_net::Error::Closed)?.encode(&chunk)?; - - let timestamp = Self::timestamp( - anchor_us, - self.frames_produced, - self.encoder()?.folded_delay(), - self.encoder()?.codec_rate(), - )?; - self.frames_produced += self.encoder()?.frame_size() as u64; - self.activity = packet.activity; - self.publish(packet, timestamp)?; - self.decoder_boundary = false; + let mut consumed = 0; + let mut result = Ok(()); + while result.is_ok() && self.pending.len() - consumed >= frame_samples { + let frame = consumed..consumed + frame_samples; + // A frame that fails is consumed too, so it is never encoded twice. + consumed = frame.end; + result = self.publish_frame(anchor_us, frame); } + self.pending.drain(..consumed); + result + } + + /// Encode and publish the frame at `samples` in `pending`. + fn publish_frame(&mut self, anchor_us: u64, samples: std::ops::Range) -> Result<(), Error> { + let packet = self + .encoder + .as_mut() + .ok_or(moq_net::Error::Closed)? + .encode(&self.pending[samples])?; + let timestamp = Self::timestamp( + anchor_us, + self.frames_produced, + self.encoder()?.folded_delay(), + self.encoder()?.codec_rate(), + )?; + self.frames_produced += self.encoder()?.frame_size() as u64; + self.activity = packet.activity; + self.publish(packet, timestamp)?; + self.decoder_boundary = false; Ok(()) } @@ -769,7 +786,9 @@ mod tests { let input = Input::new(input_rate, Layout::Stereo); // Not a whole number of codec frames, so finish flushes a partial tail. let frames = input_rate as usize + 123; - let samples: Vec = (0..frames * 2).map(|index| (index as f32 * 0.01).sin() * 0.25).collect(); + let samples: Vec = (0..frames * 2) + .map(|index| (index as f32 * 0.01).sin() * 0.25) + .collect(); let mut reference = None; for batch in [1, 7, 959, 961, 1921, 4410, frames] { let mut broadcast = moq_net::broadcast::Info::new().produce(); @@ -803,9 +822,10 @@ mod tests { let mut published = Vec::new(); while let Some(read) = reader.read().await.unwrap() { published.push(match read { - moq_mux::container::Read::Frame { group, frame } => { - (format!("{group} {:?} {}", frame.timestamp, frame.keyframe), frame.payload) - } + moq_mux::container::Read::Frame { group, frame } => ( + format!("{group} {:?} {}", frame.timestamp, frame.keyframe), + frame.payload, + ), other => (format!("{other:?}"), Bytes::new()), }); } @@ -813,7 +833,11 @@ mod tests { // PCM at the codec rate publishes exactly the input, its final frame zero-padded. let payload: Vec = published.iter().flat_map(|(_, payload)| payload.to_vec()).collect(); let expected: Vec = samples.iter().flat_map(|sample| sample.to_le_bytes()).collect(); - assert_eq!(payload.len(), expected.len().next_multiple_of(1920 * 4), "batch={batch}"); + assert_eq!( + payload.len(), + expected.len().next_multiple_of(1920 * 4), + "batch={batch}" + ); assert_eq!(payload[..expected.len()], expected[..], "batch={batch}"); assert!(payload[expected.len()..].iter().all(|&byte| byte == 0), "batch={batch}"); } diff --git a/rs/moq-audio/src/resample.rs b/rs/moq-audio/src/resample.rs index 04f6eabf4a2..e40384c8fe6 100644 --- a/rs/moq-audio/src/resample.rs +++ b/rs/moq-audio/src/resample.rs @@ -275,33 +275,23 @@ impl Resampler { } /// Convert every whole chunk that is buffered, keeping the remainder. + /// + /// Chunks are read in place and the remainder moves to the front once, so a + /// large batch costs linear rather than quadratic copying. fn convert(&mut self) -> Result, BackendError> { let chunk_samples = self.chunk_frames * self.channels; let mut out = Vec::new(); - while self.pending.len() >= chunk_samples { - for (frame_idx, frame) in self.pending[..chunk_samples].chunks_exact(self.channels).enumerate() { - for (ch, &sample) in frame.iter().enumerate() { - self.input_planar[ch][frame_idx] = sample; - } + let mut consumed = 0; + let mut result = Ok(()); + while result.is_ok() && self.pending.len() - consumed >= chunk_samples { + result = self.convert_chunk(consumed, &mut out); + // A chunk the filter refused stays buffered. + if result.is_ok() { + consumed += chunk_samples; } - - let input = SequentialSliceOfVecs::new(&self.input_planar, self.channels, self.chunk_frames) - .expect("resampler input buffer dimensions"); - let mut output = - SequentialSliceOfVecs::new_mut(&mut self.output_planar, self.channels, self.output_frames_max) - .expect("resampler output buffer dimensions"); - let (_, produced) = self.resampler.process_into_buffer(&input, &mut output, None)?; - - let prev_len = out.len(); - out.resize(prev_len + produced * self.channels, 0.0); - for frame_idx in 0..produced { - for ch in 0..self.channels { - out[prev_len + frame_idx * self.channels + ch] = self.output_planar[ch][frame_idx]; - } - } - - self.pending.drain(..chunk_samples); } + self.pending.drain(..consumed); + result?; // Drop the filter's startup silence rather than passing it on as audio. What // it costs is paid back by `flush`, which drains the same amount at the end, @@ -314,6 +304,31 @@ impl Resampler { Ok(out) } + + /// Convert the chunk starting at `start` in `pending`, appending its output to `out`. + fn convert_chunk(&mut self, start: usize, out: &mut Vec) -> Result<(), BackendError> { + let chunk = &self.pending[start..start + self.chunk_frames * self.channels]; + for (frame_idx, frame) in chunk.chunks_exact(self.channels).enumerate() { + for (ch, &sample) in frame.iter().enumerate() { + self.input_planar[ch][frame_idx] = sample; + } + } + + let input = SequentialSliceOfVecs::new(&self.input_planar, self.channels, self.chunk_frames) + .expect("resampler input buffer dimensions"); + let mut output = SequentialSliceOfVecs::new_mut(&mut self.output_planar, self.channels, self.output_frames_max) + .expect("resampler output buffer dimensions"); + let (_, produced) = self.resampler.process_into_buffer(&input, &mut output, None)?; + + let prev_len = out.len(); + out.resize(prev_len + produced * self.channels, 0.0); + for frame_idx in 0..produced { + for ch in 0..self.channels { + out[prev_len + frame_idx * self.channels + ch] = self.output_planar[ch][frame_idx]; + } + } + Ok(()) + } } /// A channel mix from one layout to another, each output channel a weighted sum From 01050c5323ccc11f97ce6b9b5c64539186d583fd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:39:26 -0800 Subject: [PATCH 5/7] quest: finish PCM batch processing Co-Authored-By: Claude Opus 5.5 --- quest/m2/performance-survey/README.md | 1 - quest/m2/performance-survey/audio-batches.md | 32 -------------------- 2 files changed, 33 deletions(-) delete mode 100644 quest/m2/performance-survey/audio-batches.md diff --git a/quest/m2/performance-survey/README.md b/quest/m2/performance-survey/README.md index c25cce3d3b9..025bb810214 100644 --- a/quest/m2/performance-survey/README.md +++ b/quest/m2/performance-survey/README.md @@ -179,7 +179,6 @@ No duplicate quest or workflow edit belongs here. - [Cache backing amplification](/quest/m2/performance-survey/cache-backing.md) - small retained samples pinning large shared fragments - [Bandwidth grants](/quest/m2/performance-survey/bandwidth-grants.md) - repeated full allocation in Rust and JS - [Cached JS group joins](/quest/m2/performance-survey/js-cached-groups.md) - repeated sorting while replaying retained groups -- [PCM batch processing](/quest/m2/performance-survey/audio-batches.md) - repeated tail movement during encoding and resampling - [Fragmented MKV import](/quest/m2/performance-survey/mkv-fragments.md) - accumulated-buffer copies and parser replay - [QUIC scheduling churn](/quest/m2/performance-survey/quic-schedule-churn.md) - priority and retirement work across unrelated groups - [HLS request fan-out](/quest/m2/performance-survey/hls-serving.md) - repeated fetch, remux and playlist construction per viewer diff --git a/quest/m2/performance-survey/audio-batches.md b/quest/m2/performance-survey/audio-batches.md deleted file mode 100644 index 0dd7c00361e..00000000000 --- a/quest/m2/performance-survey/audio-batches.md +++ /dev/null @@ -1,32 +0,0 @@ -# [M] PCM batching avoids repeated tail movement - -## Goal - -Encoding and resampling larger PCM batches does not repeatedly move the -same remaining samples, while preserving audio and timestamp behavior. - -## Plan - -[Producer::publish_full_frames](/rs/moq-audio/src/encode/producer.rs) -front-drains into a new Vec for each packet after appending the entire input. -[Resampler::convert](/rs/moq-audio/src/resample.rs) likewise -front-drains after every chunk. N chunks in a call can move a -triangular number of trailing samples. Capture and binding producers use these -paths; small normal capture periods may hide the cost. - -Benchmark a constant duration of audio delivered in different batch sizes, -channels and input/output rate pairs, also sweeping independent producers. -Separate buffering/resampling from codec CPU with a cheap supported codec -control. Measure instructions/sample, allocations, copied bytes, throughput -and worst call duration, including tiny steady batches and large bursts. -Verify PCM or decoded output as appropriate, sample counts, packet boundaries, -timestamps, partial tails, flush and discontinuities. - -Processing slices and compacting only the remainder is a candidate; do not -prescribe a queue type before measuring. This does not change capture period -policy or the [Opus grant](/quest/m2/2848-follow-the-bandwidth-grant-in-moq-audio-instead-of.md). - -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. From c31c0deff812fda882ba0e8d5e93854a5c82163f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 08:30:43 -0800 Subject: [PATCH 6/7] bench(audio): measure write latency outside instruction counting Co-Authored-By: GPT-6.1 Sol --- rs/moq-audio/benches/batches.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/rs/moq-audio/benches/batches.rs b/rs/moq-audio/benches/batches.rs index f7dbadc7102..10f36902de2 100644 --- a/rs/moq-audio/benches/batches.rs +++ b/rs/moq-audio/benches/batches.rs @@ -2,14 +2,14 @@ //! delivered in batches from tiny steady writes to a single burst, swept over channels, //! input rates (with and without resampling) and independent producers. //! -//! PCM is the codec, so buffering and resampling show instead of codec CPU; two Opus +//! PCM is the codec, so buffering and resampling show instead of codec CPU; four Opus //! cases put that cost beside a real codec's. Each case builds fresh producers, writes //! every batch and finishes the track, so the partial tail and resampler flush count too. //! //! Run with `cargo bench -p moq-audio --bench batches`. A table comes first: user-space //! instructions per input sample (Linux `perf_event_open`, `-` where unavailable), -//! allocations, allocated bytes and the slowest write of one counted pass. Criterion -//! then times each case. +//! allocations and allocated bytes from an untimed pass, plus the slowest write from a +//! separate latency pass. Criterion then times each case without per-write timers. #![expect( clippy::disallowed_methods, clippy::disallowed_types, @@ -204,14 +204,16 @@ impl Case { } } -/// Write every batch round-robin across the producers, then finish them, returning the slowest write. -fn run(producers: &mut [Producer], input: &[Frame]) -> Duration { +/// Time individual writes only in the separate latency pass, outside instruction and throughput measurements. +fn run(producers: &mut [Producer], input: &[Frame]) -> Duration { let mut slowest = Duration::ZERO; for frame in input { for producer in producers.iter_mut() { - let start = Instant::now(); + let start = TIMED.then(Instant::now); producer.write(black_box(frame)).unwrap(); - slowest = slowest.max(start.elapsed()); + if let Some(start) = start { + slowest = slowest.max(start.elapsed()); + } } } for producer in producers.iter_mut() { @@ -274,10 +276,13 @@ fn counts() { if let Some(counter) = &mut counter { counter.start(); } - let slowest = run(&mut producers, &input); + run::(&mut producers, &input); let instructions = counter.as_mut().map(instructions::Counter::stop); COUNTING.store(false, Ordering::Relaxed); drop((producers, broadcast)); + let (broadcast, mut producers) = case.producers(); + let slowest = run::(&mut producers, &input); + drop((producers, broadcast)); let per_sample = match instructions { Some(instructions) => format!("{:.1}", instructions as f64 / case.samples() as f64), @@ -305,7 +310,7 @@ fn bench_batches(c: &mut Criterion) { b.iter_batched( || case.producers(), |(broadcast, mut producers)| { - run(&mut producers, &input); + run::(&mut producers, &input); (broadcast, producers) }, BatchSize::PerIteration, From 07f26cc68fc2cd2609003d753bfbb41afd25a86f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 09:14:36 -0800 Subject: [PATCH 7/7] test(moq-audio): port the batch-size test to Subscription::with_delay main replaced Subscription::with_max_delay with a Delay { min, max } in #7597. Co-Authored-By: Claude Opus 5.5 --- rs/moq-audio/src/encode/producer.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rs/moq-audio/src/encode/producer.rs b/rs/moq-audio/src/encode/producer.rs index d43f9f6253e..816065c33e5 100644 --- a/rs/moq-audio/src/encode/producer.rs +++ b/rs/moq-audio/src/encode/producer.rs @@ -1243,7 +1243,10 @@ mod tests { let track = consumer .track("audio") .unwrap() - .subscribe(moq_net::track::Subscription::default().with_max_delay(Duration::from_secs(60))) + .subscribe( + moq_net::track::Subscription::default() + .with_delay(moq_net::track::Delay::default().with_max(Duration::from_secs(60))), + ) .await .unwrap(); let mut reader = moq_mux::container::Consumer::new(