#perf-events

criterion-perf-events

Measure perf events for criterion

8 unstable releases (3 breaking)

0.4.0 May 26, 2023
0.3.0 Dec 24, 2022
0.2.0 Mar 8, 2022
0.1.4 Jan 29, 2022
0.1.1 May 2, 2020

#5 in #perf-events

Download history 338/week @ 2024-06-28 374/week @ 2024-07-05 340/week @ 2024-07-12 460/week @ 2024-07-19 648/week @ 2024-07-26 683/week @ 2024-08-02 474/week @ 2024-08-09 604/week @ 2024-08-16 325/week @ 2024-08-23 285/week @ 2024-08-30 333/week @ 2024-09-06 340/week @ 2024-09-13 513/week @ 2024-09-20 522/week @ 2024-09-27 1880/week @ 2024-10-04 6218/week @ 2024-10-11

9,234 downloads per month
Used in 3 crates

MIT/Apache

15KB
133 lines

Criterion-perf-events

This is a measurement plugin for Criterion.rs to measure events of the Linux perf interface.

Supported Events

Criterion-perf-events uses the perfcnt crate and supports events provided by this crate. If you are interested in more details, please take a look at the events listed here:

Troubleshooting

If you get a "Permission denied" error, update perf_event_paranoid:

sudo sh -c 'echo 1 >/proc/sys/kernel/perf_event_paranoid'

For further details please take a look at the following link.

Example

The following code shows how to count retired instructions.

use criterion::{criterion_group, criterion_main, BenchmarkId, black_box, Criterion};
use criterion_perf_events::Perf;
use perfcnt::linux::HardwareEventType as Hardware;
use perfcnt::linux::PerfCounterBuilderLinux as Builder;

fn fibonacci_slow(n: usize) -> usize {
    match n {
        0 => 1,
        1 => 1,
        n => fibonacci_slow(n - 1) + fibonacci_slow(n - 2),
    }
}

fn bench(c: &mut Criterion<Perf>) {
    let mut group = c.benchmark_group("fibonacci");

    let fibo_arg = 30;
    group.bench_function(BenchmarkId::new("slow", fibo_arg), |b| {
        b.iter(|| fibonacci_slow(black_box(fibo_arg)))
    });

    group.finish()
}

criterion_group!(
    name = instructions_bench;
    config = Criterion::default().with_measurement(Perf::new(Builder::from_hardware_event(Hardware::Instructions)));
    targets = bench
);
criterion_main!(instructions_bench);

run with:

cargo criterion

Open target/criterion/reports/index.html to view detailed results with plots. For all event types (Hardware::Instructions, Hardware::CacheMisses...) criterion will always report cycles as the unit. Note that your event type is what is being shown, not CPU cycles.

Dependencies

~12–22MB
~299K SLoC