1use std::fs::File;
18use std::io::Read;
19use std::time::{Duration, Instant};
20
21use criterion::measurement::{ValueFormatter, WallTime};
22pub use criterion::*;
23
24pub type Criterion = criterion::Criterion<Measurement>;
25pub type Bencher<'a> = criterion::Bencher<'a, Measurement>;
26
27#[macro_export]
28macro_rules! criterion_group {
29 ($name:ident, $($target:path),+ $(,)?) => {
30 $crate::criterion::criterion_group! {
31 name = $name;
32 config = $crate::criterion::Criterion::default()
33 .with_measurement($crate::Measurement::from_env());
34 targets = $($target),+
35 }
36 };
37}
38
39#[doc(hidden)]
40pub use criterion;
41
42static WALL: WallTime = WallTime;
43
44pub struct Measurement(Option<File>);
47
48impl Measurement {
49 pub fn from_env() -> Self {
52 Measurement(match std::env::var("MZ_BENCH_MEASUREMENT").as_deref() {
53 Err(_) | Ok("wall") => None,
54 Ok("instructions") => Some(open_instruction_counter()),
55 Ok(other) => panic!("MZ_BENCH_MEASUREMENT must be wall or instructions, not {other}"),
56 })
57 }
58}
59
60impl criterion::measurement::Measurement for Measurement {
61 type Intermediate = (Instant, u64);
62 type Value = Duration;
63
64 fn start(&self) -> (Instant, u64) {
65 (Instant::now(), self.0.as_ref().map_or(0, read))
66 }
67
68 fn end(&self, (start, count): (Instant, u64)) -> Duration {
69 match &self.0 {
70 None => start.elapsed(),
71 Some(counter) => Duration::from_nanos(read(counter) - count),
72 }
73 }
74
75 fn add(&self, v1: &Duration, v2: &Duration) -> Duration {
76 *v1 + *v2
77 }
78
79 fn zero(&self) -> Duration {
80 Duration::ZERO
81 }
82
83 fn to_f64(&self, value: &Duration) -> f64 {
84 WALL.to_f64(value)
85 }
86
87 fn formatter(&self) -> &dyn ValueFormatter {
88 match self.0 {
89 None => WALL.formatter(),
90 Some(_) => &Instructions,
91 }
92 }
93}
94
95struct Instructions;
96
97impl ValueFormatter for Instructions {
98 fn scale_values(&self, _: f64, _: &mut [f64]) -> &'static str {
99 "instructions"
100 }
101
102 fn scale_throughputs(&self, _: f64, _: &Throughput, _: &mut [f64]) -> &'static str {
103 "instructions"
104 }
105
106 fn scale_for_machines(&self, _: &mut [f64]) -> &'static str {
107 "instructions"
108 }
109}
110
111#[cfg(target_os = "linux")]
112fn open_instruction_counter() -> File {
113 use std::os::fd::FromRawFd;
114
115 let mut attr = [0u64; 16];
118 attr[0] = 128 << 32;
119 attr[1] = 1; attr[5] = (1 << 5) | (1 << 6); let fd = unsafe { libc::syscall(libc::SYS_perf_event_open, attr.as_ptr(), 0, -1, -1, 0) };
124 assert!(
125 fd >= 0,
126 "perf_event_open: {}. It needs kernel.perf_event_paranoid <= 2, and CAP_PERFMON in Docker.",
127 std::io::Error::last_os_error()
128 );
129 unsafe { File::from_raw_fd(i32::try_from(fd).unwrap()) }
131}
132
133#[cfg(not(target_os = "linux"))]
134fn open_instruction_counter() -> File {
135 panic!("counting instructions needs Linux perf events")
136}
137
138fn read(mut counter: &File) -> u64 {
139 let mut buf = [0; 8];
140 counter.read_exact(&mut buf).expect("reading the counter");
141 u64::from_ne_bytes(buf)
142}