diff --git a/perf-event/examples/println-cpi-raw.rs b/perf-event/examples/println-cpi-raw.rs new file mode 100644 index 0000000..a9a7ebb --- /dev/null +++ b/perf-event/examples/println-cpi-raw.rs @@ -0,0 +1,44 @@ +use perf_event::events::Raw; +use perf_event::{Builder, Group}; + +fn main() -> std::io::Result<()> { + /// Measure CPI on aarch64/x86_64 + /// + /// Raw events are different for each arch. + #[cfg(target_arch = "aarch64")] + let insns_retired: Raw = Raw::new().config(0x08); + #[cfg(target_arch = "aarch64")] + let cpu_cycles: Raw = Raw::new().config(0x11); + #[cfg(target_arch = "x86_64")] + let insns_retired: Raw = Raw::new().config(0x0c0); + #[cfg(target_arch = "x86_64")] + let cpu_cycles: Raw = Raw::new().config(0x3c); + + let mut group = Group::new()?; + let raw_insns_retired = Builder::new() + .group(&mut group) + .kind(insns_retired) + .include_kernel() + .build()?; + let raw_cpu_cycles = Builder::new() + .group(&mut group) + .kind(cpu_cycles) + .include_kernel() + .build()?; + + let vec = (0..=51).collect::>(); + + group.enable()?; + println!("{:?}", vec); + group.disable()?; + + let counts = group.read()?; + println!( + "cycles / instructions: {} / {} ({:.2} cpi)", + counts[&raw_cpu_cycles], + counts[&raw_insns_retired], + (counts[&raw_cpu_cycles] as f64 / counts[&raw_insns_retired] as f64) + ); + + Ok(()) +} diff --git a/perf-event/src/events.rs b/perf-event/src/events.rs index 85092ec..f1677c1 100644 --- a/perf-event/src/events.rs +++ b/perf-event/src/events.rs @@ -55,6 +55,9 @@ pub enum Event { #[allow(missing_docs)] Breakpoint(Breakpoint), + + #[allow(missing_docs)] + Raw(Raw), } impl Event { @@ -92,6 +95,12 @@ impl Event { } } } + Event::Raw(raw) => { + attr.type_ = bindings::PERF_TYPE_RAW; + attr.config = raw.config; + attr.__bindgen_anon_3.config1 = raw.config1; + attr.__bindgen_anon_4.config2 = raw.config2; + } } } } @@ -257,6 +266,52 @@ impl Cache { } } +/// A Raw event +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Raw { + /// Raw config of the event + pub config: u64, + /// Raw config1 of the event + pub config1: u64, + /// Raw config2 of the event + pub config2: u64, +} + +impl Raw { + /// Create a new Raw event + pub fn new() -> Self { + Raw { + config: 0, + config1: 0, + config2: 0, + } + } + + /// Set config + pub fn config(mut self, config: u64) -> Self { + self.config = config; + self + } + + /// Set config1 + pub fn config1(mut self, config1: u64) -> Self { + self.config1 = config1; + self + } + + /// Set config2 + pub fn config2(mut self, config2: u64) -> Self { + self.config2 = config2; + self + } +} + +impl From for Event { + fn from(raw: Raw) -> Event { + Event::Raw(raw) + } +} + /// A cache whose events we would like to count. /// /// This is used in the `Cache` type as part of the identification of a cache