Skip to content

Commit

Permalink
feat(perf_buffer): perf_buffer relies on traits rather than specific …
Browse files Browse the repository at this point in the history
…types
  • Loading branch information
CoderPoet committed Sep 12, 2024
1 parent 5a64498 commit f4c6b42
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions libbpf-rs/src/perf_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ use std::ptr::NonNull;
use std::slice;
use std::time::Duration;

use crate::util;
use crate::util::validate_bpf_ret;
use crate::AsRawLibbpf;
use crate::Error;
use crate::ErrorExt as _;
use crate::Map;
use crate::MapCore as _;
use crate::MapType;
use crate::Result;
use crate::{util, MapCore};

// Workaround for `trait_alias`
// (https://doc.rust-lang.org/unstable-book/language-features/trait-alias.html)
Expand All @@ -44,16 +42,22 @@ impl Debug for CbStruct<'_> {
}

/// Builds [`PerfBuffer`] instances.
pub struct PerfBufferBuilder<'a, 'b> {
map: &'a Map<'a>,
pub struct PerfBufferBuilder<'a, 'b, M>
where
M: MapCore + AsFd,
{
map: &'a M,
pages: usize,
sample_cb: Option<Box<dyn SampleCb + 'b>>,
lost_cb: Option<Box<dyn LostCb + 'b>>,
}

impl<'a> PerfBufferBuilder<'a, '_> {
/// Create a new `PerfBufferBuilder` using the provided `Map`.
pub fn new(map: &'a Map<'a>) -> Self {
impl<'a, M> PerfBufferBuilder<'a, '_, M>
where
M: MapCore + AsFd,
{
/// Create a new `PerfBufferBuilder` using the provided `MapCore + AsFd` trait.
pub fn new(map: &'a M) -> Self {
Self {
map,
pages: 64,
Expand All @@ -63,14 +67,17 @@ impl<'a> PerfBufferBuilder<'a, '_> {
}
}

impl<'a, 'b> PerfBufferBuilder<'a, 'b> {
impl<'a, 'b, M> PerfBufferBuilder<'a, 'b, M>
where
M: MapCore + AsFd,
{
/// Callback to run when a sample is received.
///
/// This callback provides a raw byte slice. You may find libraries such as
/// [`plain`](https://crates.io/crates/plain) helpful.
///
/// Callback arguments are: `(cpu, data)`.
pub fn sample_cb<NewCb: SampleCb + 'b>(self, cb: NewCb) -> PerfBufferBuilder<'a, 'b> {
pub fn sample_cb<NewCb: SampleCb + 'b>(self, cb: NewCb) -> PerfBufferBuilder<'a, 'b, M> {
PerfBufferBuilder {
map: self.map,
pages: self.pages,
Expand All @@ -82,7 +89,7 @@ impl<'a, 'b> PerfBufferBuilder<'a, 'b> {
/// Callback to run when a sample is received.
///
/// Callback arguments are: `(cpu, lost_count)`.
pub fn lost_cb<NewCb: LostCb + 'b>(self, cb: NewCb) -> PerfBufferBuilder<'a, 'b> {
pub fn lost_cb<NewCb: LostCb + 'b>(self, cb: NewCb) -> PerfBufferBuilder<'a, 'b, M> {
PerfBufferBuilder {
map: self.map,
pages: self.pages,
Expand All @@ -92,7 +99,7 @@ impl<'a, 'b> PerfBufferBuilder<'a, 'b> {
}

/// The number of pages to size the ring buffer.
pub fn pages(self, pages: usize) -> PerfBufferBuilder<'a, 'b> {
pub fn pages(self, pages: usize) -> PerfBufferBuilder<'a, 'b, M> {
PerfBufferBuilder {
map: self.map,
pages,
Expand Down Expand Up @@ -164,7 +171,10 @@ impl<'a, 'b> PerfBufferBuilder<'a, 'b> {
}
}

impl Debug for PerfBufferBuilder<'_, '_> {
impl<M> Debug for PerfBufferBuilder<'_, '_, M>
where
M: MapCore + AsFd,
{
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let Self {
map,
Expand All @@ -181,7 +191,7 @@ impl Debug for PerfBufferBuilder<'_, '_> {
}
}

/// Represents a special kind of [`Map`]. Typically used to transfer data between
/// Represents a special kind of [`MapCore`]. Typically used to transfer data between
/// [`Program`][crate::Program]s and userspace.
#[derive(Debug)]
pub struct PerfBuffer<'b> {
Expand Down

0 comments on commit f4c6b42

Please sign in to comment.