Skip to main content

PerfEventArray

Struct PerfEventArray 

Source
pub struct PerfEventArray<T> { /* private fields */ }
Expand description

A map that can be used to receive events from eBPF programs using the linux perf API.

Each element of a PerfEventArray is a separate PerfEventArrayBuffer which can be used to receive events sent by eBPF programs that use bpf_perf_event_output().

To receive events you need to:

§Minimum kernel version

The minimum kernel version required to use this feature is 4.3.

§Examples

A common way to use a perf array is to have one perf buffer for each available CPU:

use aya::maps::PerfEventArray;
use aya::util::online_cpus;

let mut perf_array = PerfEventArray::try_from(bpf.map_mut("EVENTS").unwrap())?;

// eBPF programs are going to write to the EVENTS perf array, using the id of the CPU they're
// running on as the array index.
let mut perf_buffers = Vec::new();
for cpu_id in online_cpus().map_err(|(_, error)| error)? {
    // this perf buffer will receive events generated on the CPU with id cpu_id
    perf_buffers.push(perf_array.open(cpu_id, None)?);
}

// poll the buffers to know when they have queued events
let poll = poll_buffers(perf_buffers);
loop {
    for perf_buf in poll.poll_readable() {
        perf_buf.for_each(|event| match event {
            PerfEvent::Sample { head, tail } => {
                // process the sample bytes (`tail` is empty unless the sample wraps)
            }
            PerfEvent::Lost { count } => {
                // record the dropped-events counter
            }
        });
    }
}

§Polling and avoiding lost events

In the example above the implementation of poll_buffers() and poll.poll_readable() is not given. PerfEventArrayBuffer implements the AsRawFd trait, so you can implement polling using any crate that can poll file descriptors, like epoll, mio etc.

Perf buffers are internally implemented as ring buffers. If your eBPF programs produce large amounts of data, in order not to lose events you might want to process each PerfEventArrayBuffer on a different thread.

Implementations§

Source§

impl<T: Borrow<MapData>> PerfEventArray<T>

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.

Source§

impl<T: BorrowMut<MapData>> PerfEventArray<T>

Source

pub fn open( &mut self, index: u32, page_count: Option<usize>, ) -> Result<PerfEventArrayBuffer<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§

Source§

impl<'a> TryFrom<&'a Map> for PerfEventArray<&'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 PerfEventArray<&'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 PerfEventArray<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 PerfEventArray<T>

§

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

§

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

§

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

§

impl<T> Unpin for PerfEventArray<T>

§

impl<T> UnsafeUnpin for PerfEventArray<T>

§

impl<T> UnwindSafe for PerfEventArray<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.
Source§

impl<T> FromMapData for T
where T: FromMapData,

Source§

impl<T> InnerMap for T
where T: InnerMap,