DynamicBuilder

Struct DynamicBuilder 

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

Builder for a Dynamic event.

The linux kernel exposes dynamic perfomance monitoring units (PMUs) using a special filesystem under /sys/bus/event_source/devices. This builder reads the config files for a specified PMU and allows you to set their values by using the kernel-specified names instead of having to directly set the right bits in the config fields.

If you find yourself having to use Dynamic and DynamicBuilder for an event please consider submitting a PR to add a native Event impl for it to this crate.

§Fields and Parameters

Generally, kernel PMUs expose a few pieces of info:

  • A format, which defines fields and indicates which bits they correspond to within perf_event_attr.
  • A set of events, which contain values to assign to the the fields defined in the format.

Generally, all you need to do is to pick a PMU and an event within that PMU. Here’s how one would configure the Msr PMU using Dynamic:

use perf_event::events::Dynamic;

let event = Dynamic::builder("msr")?.event("tsc")?.build()?;

You can use perf list to get a list of which kernel PMU events are supported on the current machine. These will generally be listed in the format <pmu>/<event>/. Here is a sample of some of the events on my machine:

msr/aperf/                                         [Kernel PMU event]
msr/cpu_thermal_margin/                            [Kernel PMU event]
msr/mperf/                                         [Kernel PMU event]
msr/pperf/                                         [Kernel PMU event]
msr/smi/                                           [Kernel PMU event]
msr/tsc/                                           [Kernel PMU event]
power/energy-cores/                                [Kernel PMU event]
power/energy-gpu/                                  [Kernel PMU event]
power/energy-pkg/                                  [Kernel PMU event]
power/energy-psys/                                 [Kernel PMU event]
power/energy-ram/                                  [Kernel PMU event]

In most cases, the event file has values for all the fields contained in the format. However, for some events, the kernel leaves the field as an event parameter which must be set by the user. In that case, you must set the value for the field using the field method.

use perf_event::events::Dynamic;

let event = Dynamic::builder(&pmu)?
    .event("evt1")?
    .field("param", 32)?
    .build()?;

In cases where the kernel does not provide the desired event you can still set the fields directly. Whether this works properly will ultimately depend on the PMU itself. You will need to set all the fields or else build will return an error.

use perf_event::events::Dynamic;

let event = Dynamic::builder(&pmu)?
    .field("event", 77)?
    .field("param", 32)?
    .field("flag", 1)?
    .build()?;

Implementations§

Source§

impl DynamicBuilder

Source

pub fn new(pmu: impl AsRef<Path>) -> Result<Self>

Construct a new dynamic builder for the provided perf PMU.

pmu can be either

  • an absolute path to the PMU directory, or,
  • the name of a pmu under /sys/bus/event_source/devices.

This method will read the dynamic format configuration out of the PMU directory tree and configure the builder to use it.

§Errors

This method reads the contents of the format directory. Any IO errors from this will be returned directly. Any errors from parsing will have kind io::ErrorKind::Other with the inner error being a DynamicBuilderError.

Source

pub fn event(&mut self, event: impl AsRef<Path>) -> Result<&mut Self>

Initialize the builder for the specified event.

event can be either

  • an absolute path to the event config file, or,
  • the name of an event file under /sys/bus/event_source/devices/<pmu>/events.
§Errors

This method reads the contents of the event file. Any IO errors from this will be returned directly. Any errors from parsing will have kind io::ErrorKind::Other with the inner error being a DynamicBuilderError.

Source

pub fn field( &mut self, field: &str, value: u64, ) -> Result<&mut Self, DynamicBuilderError>

Set the value of a field.

This overwrites the previous value of the field, if there was one.

§Errors

An error will be returned if

  • field did not exist in the format description for the PMU, or,
  • value was larger than the availabel bits for field.
Source

pub fn build(&self) -> Result<Dynamic, MissingParameterError>

Build the Dynamic event type using the fields in this builder.

This will return an error if any event parameters have not been set to a value.

Source

pub fn params(&self) -> impl Iterator<Item = &str>

Iterate over all unset parameter fields.

By default, no fields are parameter fields. They only become parameter fields when the event file contains <field>=?.

Source

pub fn fields(&self) -> impl Iterator<Item = (&str, Option<u64>)>

Iterate over all fields in this builder.

Source

pub fn scale(&self) -> Result<Option<f64>>

Read the scale factor of the event.

This is a value to be multiplied by the event count emitted by the kernel in order to convert the count to the unit as returned by unit. Not all events have a scale.

If event has not been specified then this method will always return an error.

Source

pub fn unit(&self) -> Result<Option<String>>

Read the unit of the event.

This is a string describing the english unit that the event represents (once multiplied by scale). Not all events have a unit.

If event has not been specified then this method will always return an error.

Trait Implementations§

Source§

impl Clone for DynamicBuilder

Source§

fn clone(&self) -> DynamicBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DynamicBuilder

Source§

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

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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,