Struct GroupData

Source
pub struct GroupData { /* private fields */ }
Expand description

A collection of counts from a group of counters.

This is the type returned by Counter::read_group and Group::read. You can index it with a reference to a specific Counter:

use perf_event::events::Hardware;
use perf_event::{Builder, Group};

let mut group = Group::new()?;
let cycles = group.add(&Builder::new(Hardware::CPU_CYCLES))?;
let insns = group.add(&Builder::new(Hardware::INSTRUCTIONS))?;
let counts = group.read()?;
println!(
    "cycles / instructions: {} / {} ({:.2} cpi)",
    counts[&cycles],
    counts[&insns],
    (counts[&cycles] as f64 / counts[&insns] as f64)
);

Or you can iterate over the results it contains:

for entry in &counts {
    println!("Counter id {} has value {}", entry.id(), entry.value());
}

The id values produced by this iteration are internal identifiers assigned by the kernel. You can use the Counter::id method to find a specific counter’s id.

For some kinds of events, the kernel may use timesharing to give all counters access to scarce hardware registers. You can see how long a group was actually running versus the entire time it was enabled using the time_enabled and time_running methods:

let scale =
    counts.time_enabled().unwrap().as_secs_f64() / counts.time_running().unwrap().as_secs_f64();
for entry in &counts {
    let value = entry.value() as f64 * scale;

    print!("Counter id {} has value {}", entry.id(), value as u64);
    if scale > 1.0 {
        print!(" (estimated)");
    }
    println!();
}

Implementations§

Source§

impl GroupData

Source

pub fn len(&self) -> usize

Return the number of counters this Counts holds results for.

Source

pub fn is_empty(&self) -> bool

Whether this GroupData is empty.

Source

pub fn time_enabled(&self) -> Option<Duration>

The duration for which the group was enabled.

This will only be present if TOTAL_TIME_ENABLED was passed to read_format.

Source

pub fn time_running(&self) -> Option<Duration>

The duration for which the group was scheduled on the CPU.

This will only be present if TOTAL_TIME_RUNNING was passed to read_format.

Source

pub fn get(&self, member: &Counter) -> Option<GroupEntry>

Get the entry for member in self, or None if member is not present.

member can be either a Counter or a Group.

If you know the counter is in the group then you can access the count via indexing.

use perf_event::events::Hardware;
use perf_event::{Builder, Group};

let mut group = Group::new()?;
let instrs = Builder::new(Hardware::INSTRUCTIONS).build_with_group(&mut group)?;
let cycles = Builder::new(Hardware::CPU_CYCLES).build_with_group(&mut group)?;
group.enable()?;
// ...
let counts = group.read()?;
let instrs = counts[&instrs];
let cycles = counts[&cycles];
Source

pub fn iter(&self) -> GroupIter<'_>

Return an iterator over all entries in self.

For compatibility reasons, if the Group this was

§Example
let data = group.read()?;
for entry in &data {
    println!("Counter with id {} has value {}", entry.id(), entry.value());
}

Trait Implementations§

Source§

impl Debug for GroupData

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Index<&Counter> for GroupData

Source§

type Output = u64

The returned type after indexing.
Source§

fn index(&self, ctr: &Counter) -> &u64

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a GroupData

Source§

type IntoIter = GroupIter<'a>

Which kind of iterator are we turning this into?
Source§

type Item = <GroupIter<'a> as Iterator>::Item

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

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> EventData for T
where T: Send + Sync,