From bd8c80c647842277f65d0035f356810efa3a268d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 26 Nov 2026 11:11:44 -0800 Subject: [PATCH] quest: move sendmmsg batching to m4 on quic-sansio-driver (#9705) --- quest/m4/quic-packet-count-metrics.md | 2 +- quest/m4/tokio-sendmmsg-batch.md | 42 +++++++++++++++++++++++++++ quest/m9/perf/tokio-sendmmsg-batch.md | 28 ------------------ 3 files changed, 43 insertions(+), 29 deletions(-) create mode 100644 quest/m4/tokio-sendmmsg-batch.md delete mode 100644 quest/m9/perf/tokio-sendmmsg-batch.md diff --git a/quest/m4/quic-packet-count-metrics.md b/quest/m4/quic-packet-count-metrics.md index e02f5dab65..d6919dff9b 100644 --- a/quest/m4/quic-packet-count-metrics.md +++ b/quest/m4/quic-packet-count-metrics.md @@ -23,5 +23,5 @@ Public API: none (`moq-bench` JSONL adds new additive fields). Wire: none. ## Related - [Fewer ACKs, tail-room piggybacking, and PTO STREAM duplication](/quest/m1/quic-ack-packing-and-pto-duplication.md) - directly lowers ACK and total packet counts -- [Cross-connection `sendmmsg` batching in `moq-tokio`](/quest/m9/perf/tokio-sendmmsg-batch.md) - directly raises `datagrams / ios` +- [Cross-connection `sendmmsg` batching in `moq-tokio`](/quest/m4/tokio-sendmmsg-batch.md) - directly raises `datagrams / ios` - [Audience-scale simulation](/quest/m9/scale-sim/README.md) - consumes per-viewer packet counts diff --git a/quest/m4/tokio-sendmmsg-batch.md b/quest/m4/tokio-sendmmsg-batch.md new file mode 100644 index 0000000000..cc1bf9a2a0 --- /dev/null +++ b/quest/m4/tokio-sendmmsg-batch.md @@ -0,0 +1,42 @@ +# [M] Batch cross-connection UDP sends via sendmmsg in moq-sock and moq-tokio + +## Goal + +`moq_sock::udp::UdpSocketState` exposes a zero-allocation batched send API (`try_send_many` via `sendmmsg` on Linux and Android) with `moq-tokio`'s single-loop I/O driver (built on the runtime-free QUIC core) as its first in-tree caller, flushing each turn's outgoing transmits across connections at the end of the same pass with no extra flush task or wake latency. `moq-uring` is unchanged (its SQE ring already batches sends across connections into one `io_uring_enter`). + +## Plan + +Decided 2026-11-26 (maintainer, `/quest-plan` follow-up on [moq-dev/moq#9705](https://github.com/moq-dev/moq/issues/9705)): + +- ✅ Hold `try_send_many` until [the runtime-free QUIC driver](/quest/m4/quic-sansio-driver.md) lands so `moq-tokio`'s single-loop driver is the in-tree caller under [moq-dev/moq#8957](https://github.com/moq-dev/moq/pull/8957)'s rule ("publish only what something calls"), and design the best long-term batched send API rather than bolting a flush queue onto today's per-connection-task driver (rejected: publishing `try_send_many` in `moq-sock` now with no in-tree caller; adding an opt-in flush-task queue to today's `moq-tokio` driver). +- Moved from `m9/perf` to `m4`: adds a public API to `moq-sock` and requires `quic-sansio-driver` in `m4`. + +Evidence and prior art from [#9705](https://github.com/moq-dev/moq/issues/9705): + +- In `zshenker/moq@040df9948bd110a83463f35cd4bd86245db03373` (measured in [#7660](https://github.com/moq-dev/moq/pull/7660) and [#8184](https://github.com/moq-dev/moq/pull/8184)), batching today's per-connection-task `moq-tokio` driver through a per-socket queue and flush task raised p50 latency by ~0.2 to 0.3 ms from the flush yield. +- In a single-loop driver (2,000 connections, 4 endpoints, 1,200 B datagrams every 100 ms, `sendmmsg` batches up to 32), flushing each pass directly cut send syscalls from 4,052 to 363 per 50 ms (-3.5% server CPU with stock ACKs), and from 2,189 to 117 per 50 ms (-14% server CPU, 23.1 ms to 19.9 ms) once ACK-only packets stopped dominating, with zero extra flush wait. + +Guidance: + +- Design the long-term batched send API on `moq_sock::udp::UdpSocketState` (e.g. `try_send_many(&self, socket: UdpSockRef<'_>, transmits: &[Transmit<'_>]) -> io::Result` on Linux/Android, or the cleanest shape that fits `quic-sansio-driver`'s drain loop and future `quic-io-boundary` buffers): + - One `sendmmsg` of up to `BATCH_SIZE` (e.g. 32) transmits, running each through `prepare_msg` so every transmit keeps its own destination, GSO `segment_size`, ECN, and `src_ip`. + - Zero per-call heap allocations. + - Return the count sent; since `sendmmsg` returns an error only when 0 transmits were sent, errors map cleanly to `transmits[0]`, and `EIO`/`EINVAL` falls back through `send` so the GSO halt and `sendmsg_einval` fallback stay intact. +- Wire it into `moq-tokio`'s single-loop I/O pass once `quic-sansio-driver` lands, draining ready transmits across connections into one batch per turn. +- Verify with a loopback test (many peers, GSO train, ECN, mid-batch oversized transmit pinning which call reports the error), a micro-bench vs `try_send`, and `just bench BASE` on chat/fanout workloads confirming lower CPU/syscalls with no p50/p99 latency regression. + +Public API: additive (`moq_sock::udp::UdpSocketState` batched send method). Wire: none. + +## Required + +- [A runtime-free QUIC driver](/quest/m4/quic-sansio-driver.md) - turns `moq-tokio`'s QUIC driver into a single-loop pass that batches outgoing transmits across connections without a flush task + +## Closes + +- [#9705](https://github.com/moq-dev/moq/issues/9705) - close once the batched send API and its `moq-tokio` caller land + +## Related + +- [Fewer ACKs, tail-room piggybacking, and PTO STREAM duplication](/quest/m1/quic-ack-packing-and-pto-duplication.md) - cutting standalone ACKs unlocks the larger -14% CPU win from `sendmmsg` +- [QUIC I/O boundary](/quest/m5/quic-io-boundary.md) - buffer ownership contract for transmit APIs +- [Packet, ACK, and syscall count tracking](/quest/m4/quic-packet-count-metrics.md) - tracks `datagrams / ios` batch ratios diff --git a/quest/m9/perf/tokio-sendmmsg-batch.md b/quest/m9/perf/tokio-sendmmsg-batch.md deleted file mode 100644 index cc9377393a..0000000000 --- a/quest/m9/perf/tokio-sendmmsg-batch.md +++ /dev/null @@ -1,28 +0,0 @@ -# [M] Measure and build cross-connection sendmmsg batching in moq-tokio - -## Goal - -On Linux high-fanout and chat workloads where `moq-tokio` sends 1–2 packets per connection per tick, measure whether batching outgoing UDP transmits across connections via `sendmmsg` (e.g., up to 32 transmits per call) reduces relay CPU and syscall overhead enough to beat the cross-task TX queue hop, and land it in `moq-sock` + `moq-tokio` only if it wins. `moq-uring` is unchanged (its SQE ring already batches sends across connections into one `io_uring_enter`). - -## Plan - -Decided 2026-11-26 (maintainer, `/quest-plan` of the `moq-quic` audit + user optimization note): ✅ confirm as a measure-first perf quest in `m9/perf` (rejected: dropping it in favor of `moq-uring` alone). Joins `m9/perf` because it touches no published API, wire format, or CLI surface and must prove its cost against the TX channel hop before landing. - -Context: - -- Today every `moq-tokio` connection task calls `socket.try_send()` -> `libc::sendmsg` independently (`rs/moq-sock/src/udp/unix.rs:294`, `rs/moq-tokio/src/quic/driver/runtime/tokio.rs:51`). Per-connection GSO helps a single bulk stream, not 1,000+ viewers each receiving 1–2 packets per frame tick. -- A downstream user reported `sendmmsg` (32 per call) cutting send syscalls from ~4,050 to 117 per tick (-3.2 ms/tick) in their tick-driven server. -- Unlike a single-threaded tick loop, `moq-tokio` runs one Tokio task per connection, so cross-connection `sendmmsg` needs a shared TX batcher (bounded queue / ring or endpoint TX drain). Keep latency bounded (flush immediately when the batch fills or the queue drains, never add an artificial coalescing timer). - -Guidance: - -- Add a `sendmmsg` batch send path in `rs/moq-sock/src/udp/unix.rs` (Linux, preserving per-message ECN, `src_ip`, and GSO `segment_size` cmsgs and `EINVAL`/`EIO` fallback). -- Prototype the `moq-tokio` TX batcher and sweep `connections × bitrate` (chat and fanout workloads via `just bench BASE` and `just bench-profile`) measuring syscalls/s, CPU per Gbps, and delivery p99 latency. -- Land if it improves CPU without hurting p99 latency; if the cross-task queue/lock overhead outweighs `sendmmsg` (similar to the channel findings in [quic-lock-contention](/quest/m9/perf/quic-lock-contention.md)), record the numbers and close as a measured no-win. - -Public API: none. Wire: none. - -## Related - -- [QUIC connection lock and packet dispatch profile](/quest/m9/perf/quic-lock-contention.md) - measures cross-task hop costs in `moq-tokio` -- [Packet, ACK, and syscall count tracking](/quest/m4/quic-packet-count-metrics.md) - tracks `datagrams / ios` batch ratios