Struct CowIter

Source
pub struct CowIter<'a> { /* private fields */ }
Expand description

COW (copy-on-write) record iterator.

This type allows you to access the raw bytes of record in the underlying ring-buffer directly without copy it to the outside.

Implementations§

Source§

impl<'a> CowIter<'a>

Source

pub fn next<F, R>(&mut self, f: F) -> Option<R>
where F: FnOnce(CowChunk<'_>, &Parser) -> R,

Advances the iterator and returns the next value.

If sampling is in happening, operations in the closure should be quick and cheap. Slow iteration of raw bytes may throttle kernel threads from outputting new data to the ring-buffer, and heavy operations may affect the performance of the target process.

§Examples
use perf_event_open::config::{Cpu, Opts, Proc, SampleOn, Size};
use perf_event_open::count::Counter;
use perf_event_open::event::sw::Software;

let event = Software::TaskClock;
let target = (Proc::ALL, Cpu(0));

let mut opts = Opts::default();
opts.sample_on = SampleOn::Count(50_000); // 50us
opts.sample_format.user_stack = Some(Size(8)); // Dump 8-bytes user stack in sample.

let counter = Counter::new(event, target, &opts).unwrap();
let sampler = counter.sampler(5).unwrap();
let mut iter = sampler.iter().into_cow();

counter.enable().unwrap();

let mut skipped = 0;
let it = loop {
    let it = iter
        .next(|cc, p| {
            // ABI layout:
            // u32 type
            // u16 misc
            // u16 size
            // u64 len
            // [u8; len] bytes

            let ptr = cc.as_bytes().as_ptr();
            let ty = ptr as *const u32;

            // Only parse sample record with stack dumped.
            if unsafe { *ty } == 9 {
                let len = unsafe { ptr.offset(8) } as *const u64;
                if unsafe { *len } > 0 {
                    return Some(p.parse(cc));
                }
            }

            skipped += 1;
            None
        })
        .flatten();

    if let Some(it) = it {
        break it;
    }
};

println!("skipped: {}", skipped);
println!("{:-?}", it);
Source

pub fn into_async(self) -> Result<AsyncCowIter<'a>>

Creates an asynchronous iterator.

Auto Trait Implementations§

§

impl<'a> Freeze for CowIter<'a>

§

impl<'a> RefUnwindSafe for CowIter<'a>

§

impl<'a> Send for CowIter<'a>

§

impl<'a> Sync for CowIter<'a>

§

impl<'a> Unpin for CowIter<'a>

§

impl<'a> UnwindSafe for CowIter<'a>

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.