Crates.io | perf-event |
lib.rs | perf-event |
version | 0.4.8 |
source | src |
created_at | 2019-12-17 05:36:04.208611 |
updated_at | 2022-11-07 03:15:43.291786 |
description | A Rust interface to Linux performance monitoring |
homepage | |
repository | https://github.com/jimblandy/perf-event.git |
max_upload_size | |
id | 189938 |
size | 73,066 |
This uses the Linux perf_event_open
API to access performance monitoring
hardware and software. Use Builder
to create a perf event counter, then use
enable
and disable
to start and stop counting. Call read
to get your
count.
For example, this counts the number of cycles used by the call to println!
.
Try adjusting the length of the vector to see the cycle count change.
use perf_event::Builder;
fn main() -> std::io::Result<()> {
let mut counter = Builder::new().build()?;
let vec = (0..=51).collect::<Vec<_>>();
counter.enable()?;
println!("{:?}", vec);
counter.disable()?;
println!("{} instructions retired", counter.read()?);
Ok(())
}
Since we don't specify what sort of event we want to count, Builder
defaults
to PERF_COUNT_HW_INSTRUCTIONS
events, whose documentation says:
Retired instructions. Be careful, these can be affected by various issues, most notably hardware interrupt counts.
The examples
directory includes programs that count other sorts of events.
The perfcnt
crate provides more extensive coverage of the Linux
perf_event_open
API than this crate.
Markus Stange's linux-perf-event-reader
supports events.
This crate only handles counters for now.
The not-perf
project is a rewrite of perf
in Rust, and has a
bunch of code for dealing with the Linux perf API.