Crates.io | perf-event2 |
lib.rs | perf-event2 |
version | 0.7.4 |
source | src |
created_at | 2023-04-20 07:17:12.316941 |
updated_at | 2024-05-30 08:57:30.446597 |
description | A Rust interface to Linux performance monitoring |
homepage | |
repository | https://github.com/Phantomical/perf-event.git |
max_upload_size | |
id | 844139 |
size | 233,406 |
This crate is a wrapper around the Linux perf_event_open
API. It
allows you to access a wide variety of the performance monitoring counters that
are available in Linux.
This crate is a fork of Jim Blandy's perf-event crate that has been updated to support more features of the
perf_event_open
API.
Add the following to your Cargo.toml
perf-event2 = "0.7"
Use Builder
to create a perf counter, then use enable
and disable
to stop
and start countinng. Call read
to get your count. If you need to use multiple
counters at once you can use Group
. If you want to sample events from the
kernel, check out Sampler
.
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;
use perf_event::events::Hardware;
fn main() -> std::io::Result<()> {
let mut counter = Builder::new(Hardware::INSTRUCTIONS).build()?;
let vec = (0..=51).collect::<Vec<_>>();
counter.enable()?;
println!("{:?}", vec);
counter.disable()?;
println!("{} instructions retired", counter.read()?);
Ok(())
}
The examples
directory includes programs that count other sorts of events.
perf-event2
supports all the same features that perf-event
does but it also
supports the following:
Group
to monitor anything other than all threads in the current
process.perf-event-data
crate.perf_event_attr
struct exposed by the
kernel.perf-event2
v0.4.8 is exactly the same as perf-event
v0.4.8. You should be
to just replace perf-event -> perf_event2
in your Cargo.toml
and continue
going with no changes. To get the new features, however, you will need to
upgrade to v0.5 or later.
The main change to be aware of is that Builder::new
now takes an event
directly. Where you would previously do this
let counter = Builder::new()
.kind(some_event)
// ...
.build()?
you will now need to do this instead
let counter = Builder::new(some_event)
// ...
.build()?;
Note that if you didn't call kind
the default event type was Hardware::INSTRUCTIONS
.
perf-event
crate is still perfectly workable
depending on your use case.linux-perf-event-reader
allows you to parse records
emitted by perf and should allow you to parse those emitted directly by the
kernel as well.perfcnt
crate provides similar coverage of the perf_event_open
API and
appears to support parsing samples emitted by the kernel as well.not-perf
project is a rewrite of perf
in Rust, and has a bunch of
code for dealing with the Linux perf API.