#perf #event #events #monitoring #linux #performance

perf-event

A Rust interface to Linux performance monitoring

9 unstable releases (3 breaking)

0.4.4 Jul 30, 2020
0.4.3 Jul 8, 2020
0.4.2 Dec 22, 2019
0.3.0 Dec 18, 2019
0.1.1 Dec 17, 2019

#60 in Profiling

Download history 3/week @ 2020-04-19 8/week @ 2020-04-26 1/week @ 2020-05-03 29/week @ 2020-05-17 7/week @ 2020-05-24 21/week @ 2020-05-31 9/week @ 2020-06-14 3/week @ 2020-06-21 7/week @ 2020-06-28 23/week @ 2020-07-05 17/week @ 2020-07-12 11/week @ 2020-07-19 466/week @ 2020-07-26 562/week @ 2020-08-02

97 downloads per month

MIT/Apache

46KB
405 lines

perf-event: a Rust interface to Linux performance monitoring

This is a nascent project. Tests are lacking. The design may change.

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.

Dependencies

~135KB