// SPDX-License-Identifier: MIT //! Instructions per second on one core (not a judge: speed is the `-fast` tier's business; //! this records where the transcription stands). //! //! The workload is the ladder's 15 CPU test ROMs in the minimal harness, each run from a //! fresh harness to its branch to itself, the suite repeated until a trial has used about //! `--trial` seconds of CPU time; the best of `--trials` trials is reported (on a shared //! machine other work only slows a run down). CPU time is the process's //! (`CLOCK_PROCESS_CPUTIME_ID`). //! //! ```text //! arm7-bench [ROM...] [--trials N] [--trial SECONDS] //! ``` //! //! Without ROMs it runs the 15 under `$TEST_ROMS` (scripts/env.sh). use std::path::PathBuf; use arm7tdmi::harness::Harness; #[repr(C)] struct Timespec { tv_sec: i64, tv_nsec: i64, } extern "C" { fn clock_gettime(clock: i32, time: *mut Timespec) -> i32; } /// The process's CPU time in seconds (Linux: CLOCK_PROCESS_CPUTIME_ID = 2). fn cpu_seconds() -> f64 { let mut time = Timespec { tv_sec: 0, tv_nsec: 0, }; // SAFETY: clock_gettime writes one timespec through a valid pointer. unsafe { clock_gettime(2, &mut time) }; time.tv_sec as f64 + time.tv_nsec as f64 * 1e-9 } const ROMS: [&str; 15] = [ "jsmolka-gba-tests/arm/arm.gba", "jsmolka-gba-tests/thumb/thumb.gba", "alyosha-gba-tests/arm/arm.gba", "alyosha-gba-tests/thumb/blx.gba", "alyosha-gba-tests/thumb/cpy.gba", "alyosha-gba-tests/arm/Coprocessor_14.gba", "alyosha-gba-tests/thumb/Push_no_regs.gba", "alyosha-gba-tests/LDM/LDM_ALU.gba", "alyosha-gba-tests/LDM/LDM_ALU_IMM.gba", "alyosha-gba-tests/LDM/LDM_ALU_Store.gba", "alyosha-gba-tests/LDM/LDM_Bx.gba", "alyosha-gba-tests/LDM/LDM_LD.gba", "alyosha-gba-tests/LDM/LDM_MUL_UL_32.gba", "alyosha-gba-tests/LDM/LDM_MUL_UL_SL.gba", "alyosha-gba-tests/LDM/LDM_Swap.gba", ]; /// One pass over the suite: (instructions, bus cycles). fn suite(roms: &[Vec]) -> (u64, u64) { let (mut steps, mut cycles) = (0u64, 0u64); for rom in roms { let mut harness = Harness::new(rom, None); while !harness.at_branch_to_self() { harness.step().expect("a ROM entered the BIOS"); steps += 1; } cycles += harness.cycles(); } (steps, cycles) } fn main() { let mut paths: Vec = Vec::new(); let (mut trials, mut trial) = (7, 0.5f64); let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { match arg.as_str() { "--trials" => trials = args.next().and_then(|v| v.parse().ok()).unwrap_or(7), "--trial" => trial = args.next().and_then(|v| v.parse().ok()).unwrap_or(0.5), _ => paths.push(PathBuf::from(arg)), } } if paths.is_empty() { let base = PathBuf::from(std::env::var("TEST_ROMS").expect("ROMs, or TEST_ROMS (scripts/env.sh)")); paths = ROMS.iter().map(|r| base.join(r)).collect(); } let roms: Vec> = paths .iter() .map(|p| std::fs::read(p).unwrap_or_else(|e| panic!("{}: {e}", p.display()))) .collect(); let (per_steps, per_cycles) = suite(&roms); let mut best = f64::MAX; let mut repeats = 1u64; for _ in 0..trials { let start = cpu_seconds(); let mut passes = 0u64; while cpu_seconds() - start < trial || passes == 0 { suite(&roms); passes += 1; } let seconds = (cpu_seconds() - start) / passes as f64; if seconds < best { best = seconds; repeats = passes; } } println!( "{} ROMs, {} instructions and {} bus cycles a pass; best of {trials} trials ({repeats} passes): {:.3} ms a pass, {:.2} M instructions/s, {:.2} M bus cycles/s (CPU time, one core)", roms.len(), per_steps, per_cycles, best * 1e3, per_steps as f64 / best / 1e6, per_cycles as f64 / best / 1e6 ); }