// SPDX-License-Identifier: MIT //! A sampling profiler for the speed workloads, since `perf` is not allowed here: //! SIGPROF 1,000 times a second of CPU time, the interrupted instruction's address //! recorded, then `scripts/profile.py` maps the addresses to source lines (inlined frames //! included) with `addr2line`. (gameboy-rust-fast's profiler, adapted.) //! //! cargo build --release --features profile --bin sh2-profile //! target/release/sh2-profile [--steps N] [--cache] [--32x] [--out FILE] use std::sync::atomic::{AtomicUsize, Ordering}; const CAPACITY: usize = 1 << 20; static mut SAMPLES: [usize; CAPACITY] = [0; CAPACITY]; static COUNT: AtomicUsize = AtomicUsize::new(0); extern "C" fn on_prof(_: libc::c_int, _: *mut libc::siginfo_t, context: *mut libc::c_void) { // SAFETY: the kernel passes a ucontext_t; only the RIP is read, and each slot is // written once, by this handler, on this thread. unsafe { let ucontext = &*(context as *const libc::ucontext_t); let rip = ucontext.uc_mcontext.gregs[libc::REG_RIP as usize] as usize; let n = COUNT.fetch_add(1, Ordering::Relaxed); if n < CAPACITY { let samples = &raw mut SAMPLES; (*samples)[n] = rip; } } } fn timer(usec: libc::suseconds_t) { // SAFETY: setting this process's profiling interval timer. unsafe { let tick = libc::timeval { tv_sec: 0, tv_usec: usec }; let timer = libc::itimerval { it_interval: tick, it_value: tick }; libc::setitimer(libc::ITIMER_PROF, &timer, std::ptr::null_mut()); } } fn main() { let mut steps: u64 = 200_000_000; let (mut cache, mut bus32x) = (false, false); let mut out = "external/profile.txt".to_owned(); let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { match arg.as_str() { "--steps" => steps = args.next().expect("N").parse().expect("a number"), "--cache" => cache = true, "--32x" => bus32x = true, "--out" => out = args.next().expect("FILE"), other => panic!("unknown argument {other}"), } } let mut harness = if bus32x { sh2::bench::setup_32x(cache) } else { sh2::bench::setup(cache) }; // SAFETY: installing a handler for this process's SIGPROF. unsafe { let mut action: libc::sigaction = std::mem::zeroed(); action.sa_sigaction = on_prof as *const () as usize; action.sa_flags = libc::SA_SIGINFO | libc::SA_RESTART; libc::sigaction(libc::SIGPROF, &action, std::ptr::null_mut()); } timer(1000); let clock = sh2::bench::run(&mut harness, steps); timer(0); let maps = std::fs::read_to_string("/proc/self/maps").unwrap(); let exe = std::fs::read_link("/proc/self/exe").unwrap(); let base = maps .lines() .find(|line| line.ends_with(exe.to_str().unwrap())) .and_then(|line| usize::from_str_radix(line.split('-').next().unwrap(), 16).ok()) .expect("the executable's mapping"); let n = COUNT.load(Ordering::Relaxed).min(CAPACITY); let mut text = format!("{}\n", exe.display()); let samples = &raw const SAMPLES; for i in 0..n { // SAFETY: the timer is stopped, nothing writes SAMPLES any more. let rip = unsafe { (*samples)[i] }; text.push_str(&format!("{:x}\n", rip.wrapping_sub(base))); } std::fs::write(&out, text).unwrap(); eprintln!("{n} samples to {out} (clock {clock})"); }