// SPDX-License-Identifier: MIT //! SH-2 instructions per second on this machine: sh2-python's harness_speed workload. //! //! sh2-bench [--steps N] [--runs N] [--cache] //! //! One thread. Each run is a fresh harness; the report gives the best (and the median) //! of the runs, by thread CPU time where the platform has it (other work on a shared //! machine only slows a run down: methodology.md, "Measuring speed"), and by wall time. use std::time::Instant; fn cpu_seconds() -> f64 { #[cfg(target_os = "linux")] { // CLOCK_THREAD_CPUTIME_ID through /proc: utime + stime of this thread, in ticks. if let Ok(text) = std::fs::read_to_string("/proc/thread-self/stat") { let after = &text[text.rfind(')').unwrap() + 2..]; let fields: Vec<&str> = after.split_whitespace().collect(); let ticks: f64 = fields[11].parse::().unwrap() + fields[12].parse::().unwrap(); return ticks / 100.0; } } 0.0 } fn main() { let args: Vec = std::env::args().collect(); let mut steps: u64 = 20_000_000; let mut runs = 5; let mut cache = false; let mut bus32x = false; let mut i = 1; while i < args.len() { match args[i].as_str() { "--steps" => { steps = args[i + 1].parse().unwrap(); i += 2; } "--runs" => { runs = args[i + 1].parse().unwrap(); i += 2; } "--cache" => { cache = true; i += 1; } "--32x" => { bus32x = true; i += 1; } other => panic!("unknown argument {other}"), } } let mut wall = Vec::new(); let mut cpu = Vec::new(); let mut clock = 0; for _ in 0..runs { let mut harness = if bus32x { sh2::bench::setup_32x(cache) } else { sh2::bench::setup(cache) }; let (w0, c0) = (Instant::now(), cpu_seconds()); clock = sh2::bench::run(&mut harness, steps); wall.push(steps as f64 / w0.elapsed().as_secs_f64()); let c = cpu_seconds() - c0; if c > 0.0 { cpu.push(steps as f64 / c); } } wall.sort_by(|a, b| b.partial_cmp(a).unwrap()); cpu.sort_by(|a, b| b.partial_cmp(a).unwrap()); let load = std::fs::read_to_string("/proc/loadavg").unwrap_or_default(); println!( "sh2-bench: {} cache, {steps} steps x {runs} runs: best {:.0} instructions/s wall (median {:.0}); \ best {:.0} by CPU time; {:.3} SH-2 states per instruction; load average {}", match (bus32x, cache) { (true, true) => "32X bus (SDRAM, refresh), on", (true, false) => "32X bus (SDRAM, refresh), off", (false, true) => "on", (false, false) => "off", }, wall[0], wall[wall.len() / 2], cpu.first().copied().unwrap_or(0.0), clock as f64 / steps as f64, load.split_whitespace().take(3).collect::>().join(" ") ); }