Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Raw Events #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions perf-event/examples/println-cpi-raw.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<_>>();

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(())
}
55 changes: 55 additions & 0 deletions perf-event/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub enum Event {

#[allow(missing_docs)]
Breakpoint(Breakpoint),

#[allow(missing_docs)]
Raw(Raw),
}

impl Event {
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also include config1 and config2? I'm not sure where config2 is used but config1 is used for some offcore x86 events.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added support for setting config1 and config2.

/// 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<Raw> 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
Expand Down