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
impl DynamicBuilder
Sourcepub fn new(pmu: impl AsRef<Path>) -> Result<Self>
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.
Sourcepub fn event(&mut self, event: impl AsRef<Path>) -> Result<&mut Self>
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.
Sourcepub fn field(
&mut self,
field: &str,
value: u64,
) -> Result<&mut Self, DynamicBuilderError>
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
fielddid not exist in the format description for the PMU, or,valuewas larger than the availabel bits forfield.
Sourcepub fn build(&self) -> Result<Dynamic, MissingParameterError>
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.
Sourcepub fn params(&self) -> impl Iterator<Item = &str>
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>=?.
Sourcepub fn fields(&self) -> impl Iterator<Item = (&str, Option<u64>)>
pub fn fields(&self) -> impl Iterator<Item = (&str, Option<u64>)>
Iterate over all fields in this builder.
Trait Implementations§
Source§impl Clone for DynamicBuilder
impl Clone for DynamicBuilder
Source§fn clone(&self) -> DynamicBuilder
fn clone(&self) -> DynamicBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more