Expand description
A Future
based map that can be used to receive events from eBPF programs using the linux
perf
API.
This is the async version of PerfEventArray
, which provides integration
with tokio and async-std and a nice Future
based API.
To receive events you need to:
- call
AsyncPerfEventArray::open
- call
AsyncPerfEventArrayBuffer::read_events
to read the events
Minimum kernel version
The minimum kernel version required to use this feature is 4.3.
Examples
use aya::maps::perf::{AsyncPerfEventArray, PerfBufferError};
use aya::util::online_cpus;
use std::convert::TryFrom;
use futures::future;
use bytes::BytesMut;
use tokio::task; // or async_std::task
// try to convert the PERF_ARRAY map to an AsyncPerfEventArray
let mut perf_array = AsyncPerfEventArray::try_from(bpf.map_mut("PERF_ARRAY")?)?;
for cpu_id in online_cpus()? {
// open a separate perf buffer for each cpu
let mut buf = perf_array.open(cpu_id, None)?;
// process each perf buffer in a separate task
task::spawn(async move {
let mut buffers = (0..10)
.map(|_| BytesMut::with_capacity(1024))
.collect::<Vec<_>>();
loop {
// wait for events
let events = buf.read_events(&mut buffers).await?;
// events.read contains the number of events that have been read,
// and is always <= buffers.len()
for i in 0..events.read {
let buf = &mut buffers[i];
// process buf
}
}
Ok::<_, PerfBufferError>(())
});
}
Implementations
sourceimpl<T: DerefMut<Target = Map>> AsyncPerfEventArray<T>
impl<T: DerefMut<Target = Map>> AsyncPerfEventArray<T>
sourcepub fn open(
&mut self,
index: u32,
page_count: Option<usize>
) -> Result<AsyncPerfEventArrayBuffer<T>, PerfBufferError>
pub fn open(
&mut self,
index: u32,
page_count: Option<usize>
) -> Result<AsyncPerfEventArrayBuffer<T>, PerfBufferError>
Opens the perf buffer at the given index.
The returned buffer will receive all the events eBPF programs send at the given index.
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for AsyncPerfEventArray<T> where
T: RefUnwindSafe,
impl<T> Send for AsyncPerfEventArray<T> where
T: Send + Sync,
impl<T> Sync for AsyncPerfEventArray<T> where
T: Send + Sync,
impl<T> Unpin for AsyncPerfEventArray<T>
impl<T> UnwindSafe for AsyncPerfEventArray<T> where
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more