13 releases

0.4.8 Nov 7, 2022
0.4.7 Apr 27, 2021
0.4.6 Dec 12, 2020
0.4.5 Aug 18, 2020
0.4.2 Dec 22, 2019

#18 in Profiling

Download history 12054/week @ 2024-08-02 12264/week @ 2024-08-09 10904/week @ 2024-08-16 11704/week @ 2024-08-23 11619/week @ 2024-08-30 12162/week @ 2024-09-06 12738/week @ 2024-09-13 13381/week @ 2024-09-20 12277/week @ 2024-09-27 13004/week @ 2024-10-04 13594/week @ 2024-10-11 13618/week @ 2024-10-18 12899/week @ 2024-10-25 13104/week @ 2024-11-01 11894/week @ 2024-11-08 10675/week @ 2024-11-15

50,834 downloads per month
Used in 50 crates (6 directly)

MIT/Apache

67KB
584 lines

perf-event: a Rust interface to Linux performance monitoring

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.

See also

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.

Dependencies

~290KB