Skip to content

ringbuf: add zero-copy read with deferred consumer advancement#1

Open
orishuss wants to merge 1 commit intomainfrom
ringbuf-zero-copy-commit
Open

ringbuf: add zero-copy read with deferred consumer advancement#1
orishuss wants to merge 1 commit intomainfrom
ringbuf-zero-copy-commit

Conversation

@orishuss
Copy link
Copy Markdown
Owner

Summary

Adds ReadZeroCopy and Commit to ringbuf.Reader, enabling zero-copy reads from the mmap'd ring buffer with batched consumer position advancement.

Motivation

ReadInto copies every record from mmap into a user buffer and advances the consumer position per-record via atomic store. In high-throughput scenarios (100K+ events/sec), the memmove and per-record atomics become a measurable CPU cost.

This change allows callers to:

  1. Read records directly from the mmap'd region (no copy)
  2. Process multiple records
  3. Advance the consumer position once via Commit

Changes

  • Reader.ReadZeroCopy(rec *Record) error — sets rec.RawSample to a slice of the mmap'd ring, does not advance consumer position
  • Reader.Commit() — single atomic store to release all consumed space
  • readRecord refactored to delegate to readRecordZeroCopy + copy + advance (no duplication)
  • ReadInto and ReadZeroCopy share poll logic via readWait helper
  • Windows fallback: readRecordZeroCopy copies, advance is a no-op
  • Tests: single read, multi read with discards, commit releases space, deadline, no-op commit, benchmark

API

// Zero-copy read — slice valid until Commit.
err := reader.ReadZeroCopy(&rec)

// Batch commit after processing N records.
reader.Commit()

Existing Read/ReadInto semantics are unchanged.

Related

This PR differs by deferring consumer advancement, eliminating per-record atomic stores in addition to the copy.

orishuss pushed a commit that referenced this pull request Mar 19, 2026
The ubuntu 24.04 kernel used by CI seems to have a bug which causes it to
crash when executing TestKfunc in our testsuite.

    === RUN   TestKfunc
    [   15.728492] BUG: unable to handle page fault for address: ffffffffea91fa00
    [   15.728724] #PF: supervisor read access in kernel mode
    [   15.728940] #PF: error_code(0x0000) - not-present page
    [   15.729302] PGD 18e3f067 P4D 18e3f067 PUD 18e41067 PMD 0
    [   15.729693] Oops: Oops: 0000 [#1] SMP NOPTI
    [   15.729973] CPU: 1 UID: 0 PID: 1947 Comm: ebpf.test Not tainted 6.11.0-1015-azure cilium#15~24.04.1-Ubuntu
    [   15.730377] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
    [   15.730620] RIP: 0010:bpf_test_run+0x183/0x3d0

This requires the nf_conntrack module to be loaded since we invoke
bpf_skb_ct_lookup in the test.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
@orishuss orishuss force-pushed the ringbuf-zero-copy-commit branch 5 times, most recently from 2c99f76 to da08000 Compare March 23, 2026 10:46
Signed-off-by: Ori Shussman <orishuss@gmail.com>
@orishuss orishuss force-pushed the ringbuf-zero-copy-commit branch from da08000 to 609e92e Compare March 23, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant