Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Update RingBufs API - allow referring individual maps #318

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 23 additions & 6 deletions redbpf/src/load/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use futures::channel::mpsc;
use futures::prelude::*;
use std::collections::HashMap;
use std::convert::AsRef;
use std::fs;
use std::io;
Expand Down Expand Up @@ -44,6 +45,7 @@ impl Loader {

let online_cpus = cpus::get_online().unwrap();
let (sender, receiver) = mpsc::unbounded();
let mut ringbufs = HashMap::new();

// bpf_map_type_BPF_MAP_TYPE_PERF_EVENT_ARRAY = 4
for m in module.maps.iter_mut().filter(|m| m.kind == 4) {
Expand All @@ -65,17 +67,13 @@ impl Loader {
let name = m.name.clone();
let map = RingBufMap::bind(m).unwrap();
let stream = RingBufMessageStream::new(name.clone(), map);
let mut s = sender.clone();
let fut = stream.for_each(move |events| {
s.start_send((name.clone(), events)).unwrap();
future::ready(())
});
tokio::spawn(fut);
ringbufs.insert(name.clone(), stream);
}

Ok(Loaded {
module,
events: receiver,
ringbufs,
})
}

Expand Down Expand Up @@ -108,6 +106,25 @@ pub struct Loaded {
/// # };
/// ```
pub events: mpsc::UnboundedReceiver<(String, <PerfMessageStream as Stream>::Item)>,

/// Streams of events emitted by BPF ring buffer maps.
/// Each key in the map corresponds to a map name.
///
/// # Example
///
/// ```
/// use std::path::Path;
/// use futures::stream::StreamExt;
/// use redbpf::load::Loader;
/// # async {
/// let mut loader = Loader::load_file(&Path::new("probe.elf")).unwrap();
/// let mut ringbuf = loader.ringbufs.get("ring_buffer");
/// while let Some(event) = ringbuf.next().await {
/// // ...
/// }
/// # };
/// ```
pub ringbufs: HashMap<String, RingBufMessageStream>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have a reservation about how this is exposed in the API: there's a discrepancy in how ringbuf and perfmap events are handled.

I think it would make sense to use the same approach for PerfMaps too? This would be a breaking change though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

As per our agreement with @rhdxmr we are going to bump major version for the next release due to the change in default LLVM. The time to break APIs is now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! In this case, I'd advocate for adding a changelog - created an issue #319

}

impl Loaded {
Expand Down