Struct AsyncPerfEventArray

Source
pub struct AsyncPerfEventArray<T> { /* private fields */ }
Available on crate features async_tokio or async_std only.
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:

§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 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.take_map("PERF_ARRAY").unwrap())?;

for cpu_id in online_cpus().map_err(|(_, error)| error)? {
    // 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 = std::iter::repeat_n(BytesMut::with_capacity(1024), 10)
            .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§

Source§

impl<T: BorrowMut<MapData>> AsyncPerfEventArray<T>

Source

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.

Source

pub fn pin<P: AsRef<Path>>(&self, path: P) -> Result<(), PinError>

Pins the map to a BPF filesystem.

When a map is pinned it will remain loaded until the corresponding file is deleted. All parent directories in the given path must already exist.

Trait Implementations§

Source§

impl<'a> TryFrom<&'a Map> for AsyncPerfEventArray<&'a MapData>

Source§

type Error = MapError

The type returned in the event of a conversion error.
Source§

fn try_from(map: &'a Map) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<&'a mut Map> for AsyncPerfEventArray<&'a mut MapData>

Source§

type Error = MapError

The type returned in the event of a conversion error.
Source§

fn try_from(map: &'a mut Map) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Map> for AsyncPerfEventArray<MapData>

Source§

type Error = MapError

The type returned in the event of a conversion error.
Source§

fn try_from(map: Map) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<T> Freeze for AsyncPerfEventArray<T>

§

impl<T> RefUnwindSafe for AsyncPerfEventArray<T>
where T: RefUnwindSafe,

§

impl<T> Send for AsyncPerfEventArray<T>
where T: Sync + Send,

§

impl<T> Sync for AsyncPerfEventArray<T>
where T: Sync + Send,

§

impl<T> Unpin for AsyncPerfEventArray<T>

§

impl<T> UnwindSafe for AsyncPerfEventArray<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.