Skip to content

quic/api: SharedThreadedPacketWriter — cross-thread batched UDP writer#450

Open
afrind wants to merge 3 commits into
facebook:mainfrom
afrind:shared-threaded-packet-writer
Open

quic/api: SharedThreadedPacketWriter — cross-thread batched UDP writer#450
afrind wants to merge 3 commits into
facebook:mainfrom
afrind:shared-threaded-packet-writer

Conversation

@afrind
Copy link
Copy Markdown
Contributor

@afrind afrind commented May 15, 2026

Drains an EventFdQueue on a dedicated EventBase, batching
packets with writemGSO and UDP_SEGMENT (GSO) coalescing where supported.

Key design points:

  • GSO chains are restricted to same-connection packets; connIds_ is a flat
    per-slot vector (one entry per mmsg slot, the chain-opener's connId).
    segsInChain tracks the segment count per chain so the maxSegmentsPerMsg
    cap is applied to segments appended, not the mmsg slot index.
  • Drain-thread arrays (bufs_, addrs_, opts_, connIds_, segCounts_) are
    preallocated to maxMsgsPerCall_ at construction; no per-batch heap
    allocation.
  • bufs_ is preserved across EAGAIN so retryAndDrain resends the identical
    batch via EPOLLOUT without rebuilding.
  • maxMsgsBeforeYield (default 10) controls drain-thread fairness.

ConnectionPacketWriter is a per-connection adaptor that forwards write/flush
to the shared writer with the connection's connId attached.

QuicTransportBaseLite gains scheduleWrite() to let the drain thread signal
the connection EVB when the producer should resume after backpressure.

Six USDT tracepoints (shared_packet_writer_{queue_full,enqueue,batch_result,
partial_send,drain_done,yield}) instrument the enqueue and drain paths.

afrind and others added 3 commits May 14, 2026 17:48
Uses eventfd on Linux (EFD_NONBLOCK; counter semantics coalesce multiple
flush() calls) and a non-blocking self-pipe on macOS. Wraps a lock-free
folly::ProducerConsumerQueue for the data path.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Defines the write/flush/getResult contract for pluggable packet writers.
BufQuicBatchResult is moved from IoBufQuicBatch.h to break an include cycle
(IoBufQuicBatch → QuicBatchWriter → StateData). Adds conn.packetWriter
(unique_ptr<QuicPacketWriter>) to QuicConnectionStateBase; null on all
existing paths. Conditionalizes write/flush sites on packetWriter so the
inline path is unchanged when no writer is installed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Drains an EventFdQueue<PacketEntry> on a dedicated EventBase, batching
packets with writemGSO and UDP_SEGMENT (GSO) coalescing where supported.

Key design points:
- GSO chains are restricted to same-connection packets; connIds_ is a flat
  per-slot vector (one entry per mmsg slot, the chain-opener's connId).
  segsInChain tracks the segment count per chain so the maxSegmentsPerMsg
  cap is applied to segments appended, not the mmsg slot index.
- Drain-thread arrays (bufs_, addrs_, opts_, connIds_, segCounts_) are
  preallocated to maxMsgsPerCall_ at construction; no per-batch heap
  allocation.
- bufs_ is preserved across EAGAIN so retryAndDrain resends the identical
  batch via EPOLLOUT without rebuilding.
- maxMsgsBeforeYield (default 10) controls drain-thread fairness.

ConnectionPacketWriter is a per-connection adaptor that forwards write/flush
to the shared writer with the connection's connId attached.

QuicTransportBaseLite gains scheduleWrite() to let the drain thread signal
the connection EVB when the producer should resume after backpressure.

Six USDT tracepoints (shared_packet_writer_{queue_full,enqueue,batch_result,
partial_send,drain_done,yield}) instrument the enqueue and drain paths.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label May 15, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented May 15, 2026

@afrind has imported this pull request. If you are a Meta employee, you can view this in D105347846.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant