Skip to content

RingBuffer: Introduce consume_raw_n() #1173

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libbpf-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Unreleased
- Added `ProgramInput::repeat` field to run a test multiple times
- Added `ProgramOutput::duration` field which represent the average
duration per repetition
- Added `RingBuffer::consume_raw_n` method to consume up to N items


0.25.0-beta.1
Expand Down
9 changes: 9 additions & 0 deletions libbpf-rs/src/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ impl RingBuffer<'_> {
unsafe { libbpf_sys::ring_buffer__consume(self.ptr.as_ptr()) }
}

/// Greedily consume from all open ring buffers, calling the registered
/// callback for each one. Continues until `len` items have been consumed,
/// no more events are available, or a callback returns a non-zero value.
///
/// Return the amount of events consumed, or a negative value in case of error.
pub fn consume_raw_n(&self, len: usize) -> i32 {
unsafe { libbpf_sys::ring_buffer__consume_n(self.ptr.as_ptr(), len as libbpf_sys::size_t) }
}

/// Greedily consume from all open ring buffers, calling the registered
/// callback for each one. Consumes continually until we run out of events
/// to consume or one of the callbacks returns a non-zero integer.
Expand Down
17 changes: 17 additions & 0 deletions libbpf-rs/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,23 @@ fn test_object_ringbuf_raw() {
// Consume from a (potentially) empty ring buffer using poll()
let ret = mgr.poll_raw(Duration::from_millis(100));
assert!(ret >= 0);

// Call getpid multiple times, to refill the ring buffer.
for _ in 1..=10 {
unsafe { libc::getpid() };
}

// Consume exactly one item
let ret = mgr.consume_raw_n(1);
assert!(ret == 1);
Comment on lines +995 to +997
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add a test making sure that with two events (or more) events available we also consume & report two? 1 really could mean so many things, so I'd think there is some value add.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea, I'll add a test for this case as well, thanks.


// Consume two items
let ret = mgr.consume_raw_n(2);
assert!(ret == 2);

// Consume all the remaining items, but no more than 10
let ret = mgr.consume_raw_n(10);
assert!((7..=10).contains(&ret));
}

#[tag(root)]
Expand Down