Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

self suspend the batch handler when AX is in mailbox wait. in-process profiler (PPC/DSP per-block heatmap CSVs and Windows IP-sampler)

Layle714d436c541d
diff --git a/Cargo.lock b/Cargo.lock
index 5e4002f..0e3406d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -24,6 +24,15 @@ version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf203f9d3bd8f29f98833d1fbef628df18f759248a547e7e01cfbf63cda36a99"
+[[package]]
+name = "addr2line"
+version = "0.25.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b"
+dependencies = [
+ "gimli 0.32.3",
+]
+
[[package]]
name = "adler2"
version = "2.0.1"
@@ -272,6 +281,21 @@ dependencies = [
"wgpu",
]
+[[package]]
+name = "backtrace"
+version = "0.3.76"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6"
+dependencies = [
+ "addr2line",
+ "cfg-if",
+ "libc",
+ "miniz_oxide",
+ "object 0.37.3",
+ "rustc-demangle",
+ "windows-link",
+]
+
[[package]]
name = "base64"
version = "0.22.1"
@@ -815,7 +839,7 @@ dependencies = [
"cranelift-control",
"cranelift-entity",
"cranelift-isle",
- "gimli",
+ "gimli 0.33.0",
"hashbrown 0.16.1",
"libm",
"log",
@@ -1534,6 +1558,7 @@ dependencies = [
name = "gecko"
version = "0.1.0"
dependencies = [
+ "backtrace",
"bytemuck",
"chapa",
"chipi-build",
@@ -1552,6 +1577,7 @@ dependencies = [
"rustc-hash 2.1.2",
"tracing",
"twox-hash",
+ "windows-sys 0.61.2",
"zerocopy",
]
@@ -1587,6 +1613,12 @@ dependencies = [
"wasip2",
]
+[[package]]
+name = "gimli"
+version = "0.32.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
+
[[package]]
name = "gimli"
version = "0.33.0"
@@ -1868,7 +1900,7 @@ name = "image"
version = "0.1.0"
dependencies = [
"idb-rs",
- "object",
+ "object 0.36.7",
"zerocopy",
"zip",
"zstd",
@@ -2910,6 +2942,15 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "object"
+version = "0.37.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe"
+dependencies = [
+ "memchr",
+]
+
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -3413,6 +3454,12 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "rustc-demangle"
+version = "0.1.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b50b8869d9fc858ce7266cce0194bd74df58b9d0e3f6df3a9fc8eb470d95c09d"
+
[[package]]
name = "rustc-hash"
version = "1.1.0"
diff --git a/crates/gecko/Cargo.toml b/crates/gecko/Cargo.toml
index 626ee38..49470c4 100644
--- a/crates/gecko/Cargo.toml
+++ b/crates/gecko/Cargo.toml
@@ -25,6 +25,7 @@ jit = [
]
jit-stats = ["jit"]
fps-counter = []
+profile = ["dep:backtrace", "dep:windows-sys"]
[build-dependencies]
chipi-build = "0.9.1"
@@ -48,3 +49,12 @@ cranelift-frontend = { version = "0.131.1", optional = true }
cranelift-jit = { version = "0.131.1", optional = true }
cranelift-module = { version = "0.131.1", optional = true }
cranelift-native = { version = "0.131.1", optional = true }
+backtrace = { version = "0.3", optional = true }
+
+[target.'cfg(windows)'.dependencies]
+windows-sys = { version = "0.61.2", features = [
+ "Win32_Foundation",
+ "Win32_System_Threading",
+ "Win32_System_Diagnostics_Debug",
+ "Win32_System_Kernel",
+], optional = true }
diff --git a/crates/gecko/src/flipper/dsp.rs b/crates/gecko/src/flipper/dsp.rs
index e40467f..e531fb2 100644
--- a/crates/gecko/src/flipper/dsp.rs
+++ b/crates/gecko/src/flipper/dsp.rs
@@ -26,48 +26,6 @@ use crate::system::{System, SystemId};
#[cfg(feature = "jit")]
pub const DSP_JIT_CHAIN_BUDGET: u32 = 16;
-#[cfg(feature = "jit")]
-pub trait DspJitHandle {
- fn run_block(&mut self, ctx_ptr: *mut ::core::ffi::c_void, iram: &[u8], irom: &[u8], start_pc: u16) -> u16;
- fn record_chain_depth(&mut self, depth: u32);
- fn flush(&mut self);
- fn dump_hot_blocks(&self, top_k: usize);
- fn dump_top_clif(&mut self, top_k: usize, iram: &[u8], irom: &[u8]);
-}
-
-#[cfg(feature = "jit")]
-impl<const SYSTEM: SystemId> DspJitHandle for jit::JitEngine<SYSTEM> {
- fn run_block(&mut self, ctx_ptr: *mut ::core::ffi::c_void, iram: &[u8], irom: &[u8], start_pc: u16) -> u16 {
- let entry = self.lookup_or_compile(iram, irom, start_pc);
- Self::run_block(self, ctx_ptr, entry)
- }
- fn record_chain_depth(&mut self, depth: u32) {
- Self::record_chain_depth(self, depth);
- }
- fn flush(&mut self) {
- Self::flush(self);
- }
- fn dump_hot_blocks(&self, _top_k: usize) {
- #[cfg(feature = "jit-stats")]
- Self::dump_hot_blocks(self, _top_k);
- #[cfg(not(feature = "jit-stats"))]
- tracing::warn!("feature `jit-stats` is not enabled. Rebuild with `--features jit-stats`.");
- }
- fn dump_top_clif(&mut self, _top_k: usize, _iram: &[u8], _irom: &[u8]) {
- #[cfg(feature = "jit-stats")]
- {
- let mut pcs: Vec<(u16, u64)> = self.hits.iter().map(|(&pc, &n)| (pc, n)).collect();
- pcs.sort_by(|a, b| b.1.cmp(&a.1));
- for (pc, hits) in pcs.into_iter().take(_top_k) {
- tracing::info!("hits={hits} pc={pc:04X}");
- self.dump_block_clif(pc, _iram, _irom);
- }
- }
- #[cfg(not(feature = "jit-stats"))]
- tracing::warn!("feature `jit-stats` is not enabled. Rebuild with `--features jit-stats`.");
- }
-}
-
pub struct Dsp {
pub registers: core::Registers,
@@ -112,6 +70,8 @@ pub struct Dsp {
pub instr_count: u32,
pub poll_cache: std::cell::Cell<Option<(u16, bool, bool)>>,
+
+ pub scheduler_suspended: bool,
}
impl Dsp {
@@ -158,6 +118,7 @@ impl Dsp {
#[cfg(feature = "jit")]
instr_count: 0,
poll_cache: std::cell::Cell::new(None),
+ scheduler_suspended: false,
}
}
@@ -173,6 +134,11 @@ impl Dsp {
dsp
}
+ #[inline]
+ pub fn mailbox_wait_state(&self) -> (bool, bool) {
+ self.poll_cache_for_pc(self.registers.pc)
+ }
+
fn poll_cache_for_pc(&self, pc: u16) -> (bool, bool) {
if let Some((cached_pc, cpu, dsp)) = self.poll_cache.get() {
if cached_pc == pc {
@@ -588,6 +554,35 @@ pub fn refresh_interrupts<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
} else {
sys.pi.clear_interrupt(InterruptFlag::Dsp);
}
+
+ if sys.dsp.csr.pi_interrupt() && sys.dsp.registers.status.external_interrupt_enable() {
+ self::wake_dsp_scheduler::<SYSTEM>(sys);
+ }
+}
+
+#[cfg(feature = "jit-stats")]
+pub static DSP_SUSPEND_COUNT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
+#[cfg(feature = "jit-stats")]
+pub static DSP_WAKE_COUNT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
+
+#[inline]
+pub fn wake_dsp_scheduler<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
+ if !sys.dsp.scheduler_suspended {
+ return;
+ }
+
+ if sys.dsp.csr.halt() || sys.dsp.csr.reset() {
+ return;
+ }
+
+ sys.dsp.scheduler_suspended = false;
+ sys.scheduler.schedule_in(
+ crate::scheduler::dsp_batch_interval(SYSTEM),
+ crate::scheduler::dsp_batch_handler::<SYSTEM>,
+ );
+
+ #[cfg(feature = "jit-stats")]
+ DSP_WAKE_COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
#[inline(always)]
@@ -763,3 +758,57 @@ pub fn dispatch_gc_dsp_ext<const SYSTEM: SystemId>(ctx: &mut System<SYSTEM>, ins
self::lut_wii::dispatch_gc_dsp_ext(ctx, instr);
}
}
+
+#[cfg(feature = "jit")]
+pub trait DspJitHandle {
+ fn run_block(&mut self, ctx_ptr: *mut ::core::ffi::c_void, iram: &[u8], irom: &[u8], start_pc: u16) -> u16;
+ fn record_chain_depth(&mut self, depth: u32);
+ fn flush(&mut self);
+ fn dump_hot_blocks(&self, top_k: usize);
+ fn dump_hot_blocks_csv(&self, top_k: usize, path: &std::path::Path) -> std::io::Result<()>;
+ fn dump_top_clif(&mut self, top_k: usize, iram: &[u8], irom: &[u8]);
+}
+
+#[cfg(feature = "jit")]
+impl<const SYSTEM: SystemId> DspJitHandle for jit::JitEngine<SYSTEM> {
+ fn run_block(&mut self, ctx_ptr: *mut ::core::ffi::c_void, iram: &[u8], irom: &[u8], start_pc: u16) -> u16 {
+ let entry = self.lookup_or_compile(iram, irom, start_pc);
+ Self::run_block(self, ctx_ptr, entry)
+ }
+
+ fn record_chain_depth(&mut self, depth: u32) {
+ Self::record_chain_depth(self, depth);
+ }
+
+ fn flush(&mut self) {
+ Self::flush(self);
+ }
+
+ fn dump_hot_blocks(&self, _top_k: usize) {
+ #[cfg(feature = "jit-stats")]
+ Self::dump_hot_blocks(self, _top_k);
+ #[cfg(not(feature = "jit-stats"))]
+ tracing::warn!("feature `jit-stats` is not enabled. Rebuild with `--features jit-stats`.");
+ }
+
+ fn dump_hot_blocks_csv(&self, _top_k: usize, _path: &std::path::Path) -> std::io::Result<()> {
+ #[cfg(feature = "jit-stats")]
+ return Self::dump_hot_blocks_csv(self, _top_k, _path);
+ #[cfg(not(feature = "jit-stats"))]
+ Ok(())
+ }
+
+ fn dump_top_clif(&mut self, _top_k: usize, _iram: &[u8], _irom: &[u8]) {
+ #[cfg(feature = "jit-stats")]
+ {
+ let mut pcs: Vec<(u16, u64)> = self.hits.iter().map(|(&pc, &n)| (pc, n)).collect();
+ pcs.sort_by(|a, b| b.1.cmp(&a.1));
+ for (pc, hits) in pcs.into_iter().take(_top_k) {
+ tracing::info!("hits={hits} pc={pc:04X}");
+ self.dump_block_clif(pc, _iram, _irom);
+ }
+ }
+ #[cfg(not(feature = "jit-stats"))]
+ tracing::warn!("feature `jit-stats` is not enabled. Rebuild with `--features jit-stats`.");
+ }
+}
diff --git a/crates/gecko/src/flipper/dsp/jit.rs b/crates/gecko/src/flipper/dsp/jit.rs
index 2742db8..542cdd5 100644
--- a/crates/gecko/src/flipper/dsp/jit.rs
+++ b/crates/gecko/src/flipper/dsp/jit.rs
@@ -501,6 +501,52 @@ impl<const SYSTEM: SystemId> JitEngine<SYSTEM> {
next_pc_u32 as u16
}
+ #[cfg(feature = "jit-stats")]
+ pub fn dump_hot_blocks_csv(&self, top_k: usize, path: &std::path::Path) -> std::io::Result<()> {
+ use std::io::Write;
+
+ let mut entries: Vec<(u16, u64, u64, usize, String, String)> = self
+ .block_entry_counter_ptrs
+ .iter()
+ .map(|(&pc, &addr)| {
+ let executions = unsafe { *(addr as *const u64) };
+ let dispatch_hits = self.hits.get(&pc).copied().unwrap_or(0);
+ let spec = self.block_specs.get(&pc);
+ let len = spec.map(|s| s.instrs.len()).unwrap_or(0);
+ let term = spec.map(|s| format!("{:?}", s.terminator)).unwrap_or_default();
+ let lead = spec
+ .and_then(|s| s.instrs.first())
+ .map(|e| disasm_mnemonic(e.raw))
+ .unwrap_or_default();
+ (pc, executions, dispatch_hits, len, term, lead)
+ })
+ .collect();
+
+ entries.sort_by(|a, b| {
+ let ca = a.1.saturating_mul(a.3 as u64);
+ let cb = b.1.saturating_mul(b.3 as u64);
+ cb.cmp(&ca).then(b.1.cmp(&a.1))
+ });
+
+ crate::profile::write_file_atomic(path, |file| {
+ writeln!(
+ file,
+ "rank,start_pc,executions,dispatch_hits,instr_count,cycles_estimated,terminator,lead_mnemonic"
+ )?;
+
+ for (rank, (pc, exec, hits, len, term, lead)) in entries.iter().take(top_k).enumerate() {
+ let cycles = exec.saturating_mul(*len as u64);
+ writeln!(
+ file,
+ "{},{:#06X},{},{},{},{},{},{}",
+ rank, pc, exec, hits, len, cycles, term, lead
+ )?;
+ }
+
+ Ok(())
+ })
+ }
+
#[cfg(feature = "jit-stats")]
pub fn dump_hot_blocks(&self, top_k: usize) {
let mut entries: Vec<(u16, u64)> = self.hits.iter().map(|(&pc, &n)| (pc, n)).collect();
diff --git a/crates/gecko/src/flipper/dsp/regs.rs b/crates/gecko/src/flipper/dsp/regs.rs
index c3549b5..766a1ac 100644
--- a/crates/gecko/src/flipper/dsp/regs.rs
+++ b/crates/gecko/src/flipper/dsp/regs.rs
@@ -110,11 +110,17 @@ impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for ControlStatus {
let now_active = !sys.dsp.csr.halt() && !sys.dsp.csr.reset();
match (was_active, now_active) {
- (false, true) => sys.scheduler.schedule_in(
- scheduler::dsp_batch_interval(SYSTEM),
- scheduler::dsp_batch_handler::<SYSTEM>,
- ),
- (true, false) => sys.scheduler.cancel(scheduler::dsp_batch_handler::<SYSTEM>),
+ (false, true) => {
+ sys.dsp.scheduler_suspended = false;
+ sys.scheduler.schedule_in(
+ scheduler::dsp_batch_interval(SYSTEM),
+ scheduler::dsp_batch_handler::<SYSTEM>,
+ );
+ }
+ (true, false) => {
+ sys.dsp.scheduler_suspended = false;
+ sys.scheduler.cancel(scheduler::dsp_batch_handler::<SYSTEM>);
+ }
_ => {}
}
@@ -177,6 +183,7 @@ impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for MailboxToDspLo {
// start of each render before the new contents land.
// Based off of FFCC, thanks SpinningCube, zayd, JustinCase
sys.drain_dsp_synchronous(64 * 1024);
+ crate::flipper::dsp::wake_dsp_scheduler::<SYSTEM>(sys);
}
}
@@ -216,6 +223,7 @@ impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for MailboxToCpuLo {
let val = sys.dsp.mailbox_to_cpu_lo;
// Reading DMBL clears DMBH.M (bit 15), signaling the CPU has consumed the mail
sys.dsp.mailbox_to_cpu_hi.set_busy(false);
+ crate::flipper::dsp::wake_dsp_scheduler::<SYSTEM>(sys);
val
}
diff --git a/crates/gecko/src/gekko/jit.rs b/crates/gecko/src/gekko/jit.rs
index 8a47fe2..9e28a6a 100644
--- a/crates/gecko/src/gekko/jit.rs
+++ b/crates/gecko/src/gekko/jit.rs
@@ -85,6 +85,10 @@ pub struct JitEngine<const SYSTEM: SystemId> {
pending_chain_slots: FxHashMap<u32, Vec<usize>>,
#[cfg(feature = "jit-stats")]
hits: FxHashMap<u32, u64>,
+ #[cfg(feature = "jit-stats")]
+ pub(crate) block_specs: FxHashMap<u32, block::BlockSpec>,
+ #[cfg(feature = "jit-stats")]
+ pub(crate) block_entry_counter_ptrs: FxHashMap<u32, usize>,
block_sig: Signature,
extern_funcs: ExternFuncs,
trampoline_fn: TrampolineFn,
@@ -476,6 +480,10 @@ impl<const SYSTEM: SystemId> JitEngine<SYSTEM> {
pending_chain_slots: FxHashMap::default(),
#[cfg(feature = "jit-stats")]
hits: FxHashMap::default(),
+ #[cfg(feature = "jit-stats")]
+ block_specs: FxHashMap::default(),
+ #[cfg(feature = "jit-stats")]
+ block_entry_counter_ptrs: FxHashMap::default(),
block_sig,
extern_funcs,
trampoline_fn,
@@ -524,10 +532,26 @@ impl<const SYSTEM: SystemId> JitEngine<SYSTEM> {
let entry = self.compile(&spec, &gprs_snapshot);
self.cache.insert(pc, entry);
+ #[cfg(feature = "jit-stats")]
+ {
+ self.block_specs.insert(pc, spec);
+ }
entry
}
+ #[cfg(feature = "jit-stats")]
+ fn block_entry_counter_slot(&mut self, pc: u32) -> usize {
+ if let Some(&addr) = self.block_entry_counter_ptrs.get(&pc) {
+ return addr;
+ }
+
+ let slot: &'static mut u64 = Box::leak(Box::new(0u64));
+ let addr = slot as *mut u64 as usize;
+ self.block_entry_counter_ptrs.insert(pc, addr);
+ addr
+ }
+
fn func_id_for(&mut self, pc: u32) -> FuncId {
if let Some(&id) = self.block_func_ids.get(&pc) {
return id;
@@ -552,6 +576,88 @@ impl<const SYSTEM: SystemId> JitEngine<SYSTEM> {
sys.gekko.pc = next_pc;
}
+ #[cfg(feature = "jit-stats")]
+ pub fn dump_hot_blocks_csv(&self, top_k: usize, path: &std::path::Path) -> std::io::Result<()> {
+ use std::io::Write;
+
+ let gprs_dummy = [0u32; 32];
+ let mut entries: Vec<(u32, u64, u64, usize, String, String)> = self
+ .block_entry_counter_ptrs
+ .iter()
+ .map(|(&pc, &addr)| {
+ let executions = unsafe { *(addr as *const u64) };
+ let dispatch_hits = self.hits.get(&pc).copied().unwrap_or(0);
+ let (len, term, idle) = self
+ .block_specs
+ .get(&pc)
+ .map(|s| {
+ (
+ s.instrs.len(),
+ format!("{:?}", s.terminator),
+ format!("{:?}", idle::classify::<SYSTEM>(s, &gprs_dummy)),
+ )
+ })
+ .unwrap_or((0, "?".to_string(), "?".to_string()));
+ (pc, executions, dispatch_hits, len, term, idle)
+ })
+ .collect();
+
+ entries.sort_by(|a, b| {
+ let ca = a.1.saturating_mul(a.3 as u64);
+ let cb = b.1.saturating_mul(b.3 as u64);
+ cb.cmp(&ca).then(b.1.cmp(&a.1))
+ });
+
+ crate::profile::write_file_atomic(path, |file| {
+ writeln!(
+ file,
+ "rank,start_pc,executions,dispatch_hits,instr_count,cycles_estimated,terminator,idle_class"
+ )?;
+
+ for (rank, (pc, exec, hits, len, term, idle)) in entries.iter().take(top_k).enumerate() {
+ let cycles = exec.saturating_mul(*len as u64);
+ writeln!(
+ file,
+ "{},{:#010X},{},{},{},{},{},{}",
+ rank, pc, exec, hits, len, cycles, term, idle
+ )?;
+ }
+
+ Ok(())
+ })?;
+
+ let disasm_path = path.with_file_name(format!(
+ "{}-disasm.txt",
+ path.file_stem().and_then(|s| s.to_str()).unwrap_or("ppc-heatmap")
+ ));
+
+ crate::profile::write_file_atomic(&disasm_path, |dfile| {
+ writeln!(
+ dfile,
+ "# top {} hot PPC blocks (instructions)",
+ top_k.min(entries.len())
+ )?;
+
+ for (rank, (pc, exec, hits, len, term, idle)) in entries.iter().take(top_k).enumerate() {
+ writeln!(
+ dfile,
+ "\n#{} pc={:#010X} executions={} dispatch_hits={} len={} term={} idle={}",
+ rank, pc, exec, hits, len, term, idle
+ )?;
+
+ if let Some(spec) = self.block_specs.get(pc) {
+ for (i, &raw) in spec.instrs.iter().enumerate() {
+ let ipc = spec.start_pc.wrapping_add((i as u32) * 4);
+ let text = decode_gekko_instr(raw);
+ writeln!(dfile, " {:08X} {:08X} {}", ipc, raw, text)?;
+ }
+ }
+ }
+
+ Ok(())
+ })
+ }
+
#[cfg(feature = "jit-stats")]
pub fn dump_idle_candidates(&self, sys: &System<SYSTEM>, top_k: usize) {
const SHAPE_BODY_CAP: usize = 12;
@@ -655,6 +761,11 @@ impl<const SYSTEM: SystemId> JitEngine<SYSTEM> {
let mut pending_for_this_block: Vec<(u32, usize)> = Vec::new();
+ #[cfg(feature = "jit-stats")]
+ let entry_counter_addr = Some(self.block_entry_counter_slot(spec.start_pc));
+ #[cfg(not(feature = "jit-stats"))]
+ let entry_counter_addr: Option<usize> = None;
+
let chain = translator::ChainContext {
self_pc: spec.start_pc,
self_func_id: func_id,
@@ -673,6 +784,7 @@ impl<const SYSTEM: SystemId> JitEngine<SYSTEM> {
spec,
gprs,
&chain,
+ entry_counter_addr,
);
drop(chain);
@@ -711,6 +823,15 @@ impl<const SYSTEM: SystemId> Default for JitEngine<SYSTEM> {
}
}
+#[cfg(feature = "jit-stats")]
+fn decode_gekko_instr(raw: u32) -> String {
+ let bytes = raw.to_be_bytes();
+ match disasm::gekko::GekkoInstruction::decode(&bytes) {
+ Some((ins, _)) => format!("{ins}"),
+ None => format!("<undecoded raw={:08X}>", raw),
+ }
+}
+
#[cfg(feature = "jit-stats")]
fn looks_like_polling_shape(spec: &block::BlockSpec, body_cap: usize) -> bool {
if spec.terminator != block::TermKind::BranchCond {
diff --git a/crates/gecko/src/gekko/jit/runtime.rs b/crates/gecko/src/gekko/jit/runtime.rs
index 636026a..63715ef 100644
--- a/crates/gecko/src/gekko/jit/runtime.rs
+++ b/crates/gecko/src/gekko/jit/runtime.rs
@@ -20,10 +20,24 @@ pub extern "C" fn cause_invalid_opcode_wii(sys: *mut core::ffi::c_void, raw_inst
cause_invalid_opcode::<WII>(sys.cast(), raw_instr, pc)
}
+#[cfg(feature = "jit-stats")]
+pub static IDLE_SKIP_CALLS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
+#[cfg(feature = "jit-stats")]
+pub static IDLE_SKIP_CYCLES_ADVANCED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
+
#[inline(always)]
fn advance_to_deadline<const SYSTEM: SystemId>(sys: *mut System<SYSTEM>) {
let sys = unsafe { &mut *sys };
let deadline = sys.scheduler.next_deadline();
+
+ #[cfg(feature = "jit-stats")]
+ {
+ IDLE_SKIP_CALLS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ if sys.scheduler.cycles < deadline {
+ IDLE_SKIP_CYCLES_ADVANCED.fetch_add(deadline - sys.scheduler.cycles, std::sync::atomic::Ordering::Relaxed);
+ }
+ }
+
if sys.scheduler.cycles < deadline {
sys.scheduler.cycles = deadline;
}
diff --git a/crates/gecko/src/gekko/jit/translator.rs b/crates/gecko/src/gekko/jit/translator.rs
index f5fea97..d167f38 100644
--- a/crates/gecko/src/gekko/jit/translator.rs
+++ b/crates/gecko/src/gekko/jit/translator.rs
@@ -101,6 +101,7 @@ pub fn translate<const SYSTEM: SystemId>(
spec: &BlockSpec,
gprs: &[u32; 32],
chain: &ChainContext<'_>,
+ entry_counter_addr: Option<usize>,
) {
let local = LocalFuncs {
cause_invalid_opcode: module.declare_func_in_func(extern_funcs.cause_invalid_opcode, &mut ctx.func),
@@ -157,6 +158,13 @@ pub fn translate<const SYSTEM: SystemId>(
let ctx_ptr: Value = builder.block_params(entry)[0];
+ if let Some(addr) = entry_counter_addr {
+ let slot_v = builder.ins().iconst(types::I64, addr as i64);
+ let cur = builder.ins().load(types::I64, MemFlags::trusted(), slot_v, 0);
+ let next = builder.ins().iadd_imm(cur, 1);
+ builder.ins().store(MemFlags::trusted(), next, slot_v, 0);
+ }
+
let exit_block = builder.create_block();
builder.append_block_param(exit_block, types::I32);
diff --git a/crates/gecko/src/lib.rs b/crates/gecko/src/lib.rs
index 0f7ab2c..90c212e 100644
--- a/crates/gecko/src/lib.rs
+++ b/crates/gecko/src/lib.rs
@@ -11,6 +11,8 @@ pub mod host;
pub mod input;
pub mod ipl;
pub mod mmio;
+#[cfg(any(feature = "jit-stats", feature = "profile"))]
+pub mod profile;
pub mod scheduler;
pub mod starlet;
pub mod system;
diff --git a/crates/gecko/src/profile.rs b/crates/gecko/src/profile.rs
new file mode 100644
index 0000000..d13d9a2
--- /dev/null
+++ b/crates/gecko/src/profile.rs
@@ -0,0 +1,289 @@
+use std::path::{Path, PathBuf};
+
+#[derive(Clone, Debug)]
+pub struct HeatmapConfig {
+ pub enabled: bool,
+ pub interval_frames: u32,
+ pub out_dir: PathBuf,
+ pub top_k: usize,
+}
+
+impl Default for HeatmapConfig {
+ fn default() -> Self {
+ Self {
+ enabled: false,
+ interval_frames: 60,
+ out_dir: PathBuf::from("./profile-dumps"),
+ top_k: 64,
+ }
+ }
+}
+
+impl HeatmapConfig {
+ pub fn ppc_csv_path(&self) -> PathBuf {
+ self.out_dir.join("ppc-heatmap.csv")
+ }
+
+ pub fn dsp_csv_path(&self) -> PathBuf {
+ self.out_dir.join("dsp-heatmap.csv")
+ }
+}
+
+pub fn ensure_dir(path: &Path) -> std::io::Result<()> {
+ if path.as_os_str().is_empty() {
+ return Ok(());
+ }
+ std::fs::create_dir_all(path)
+}
+
+pub fn write_file_atomic(
+ path: &Path,
+ write: impl FnOnce(&mut std::fs::File) -> std::io::Result<()>,
+) -> std::io::Result<()> {
+ if let Some(parent) = path.parent() {
+ ensure_dir(parent)?;
+ }
+
+ let tmp = path.with_extension("tmp");
+ {
+ let mut file = std::fs::File::create(&tmp)?;
+ write(&mut file)?;
+ file.sync_data().ok();
+ }
+ std::fs::rename(&tmp, path)
+}
+
+#[cfg(feature = "profile")]
+#[derive(Clone, Debug)]
+pub struct PprofConfig {
+ pub hz: u32,
+ pub secs: u32,
+ pub out: PathBuf,
+}
+
+#[cfg(all(feature = "profile", windows))]
+mod win_sampler {
+ use super::*;
+ use rustc_hash::FxHashMap;
+ use std::sync::atomic::{AtomicBool, Ordering};
+ use std::sync::{Arc, Mutex};
+ use std::thread;
+ use std::time::{Duration, Instant};
+ use windows_sys::Win32::Foundation::{CloseHandle, FALSE, HANDLE};
+ use windows_sys::Win32::System::Diagnostics::Debug::{CONTEXT, CONTEXT_FULL_AMD64, GetThreadContext};
+ use windows_sys::Win32::System::Threading::{
+ GetCurrentThreadId, OpenThread, ResumeThread, SuspendThread, THREAD_GET_CONTEXT, THREAD_QUERY_INFORMATION,
+ THREAD_SUSPEND_RESUME,
+ };
+
+ pub struct IpSampler {
+ stop: Arc<AtomicBool>,
+ joiner: Option<thread::JoinHandle<()>>,
+ counts: Arc<Mutex<FxHashMap<u64, u64>>>,
+ deadline: Instant,
+ out: PathBuf,
+ hz: u32,
+ sample_total: Arc<std::sync::atomic::AtomicU64>,
+ }
+
+ #[repr(C, align(16))]
+ struct AlignedContext(CONTEXT);
+
+ impl IpSampler {
+ pub fn start_for_current_thread(hz: u32, secs: u32, out: PathBuf) -> Result<Self, std::io::Error> {
+ let tid = unsafe { GetCurrentThreadId() };
+ let target: HANDLE = unsafe {
+ OpenThread(
+ THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION,
+ FALSE,
+ tid,
+ )
+ };
+
+ if target.is_null() {
+ return Err(std::io::Error::last_os_error());
+ }
+
+ let stop = Arc::new(AtomicBool::new(false));
+ let counts: Arc<Mutex<FxHashMap<u64, u64>>> = Arc::new(Mutex::new(FxHashMap::default()));
+ let sample_total = Arc::new(std::sync::atomic::AtomicU64::new(0));
+ let deadline = Instant::now() + Duration::from_secs(secs as u64);
+ let interval = Duration::from_nanos((1_000_000_000u64 / hz.max(1) as u64).max(1));
+
+ let stop_c = stop.clone();
+ let counts_c = counts.clone();
+ let total_c = sample_total.clone();
+ let target_addr = target as usize;
+
+ let joiner = thread::Builder::new()
+ .name("pprof-sampler".into())
+ .spawn(move || {
+ let target = target_addr as HANDLE;
+ let mut ctx_box: Box<AlignedContext> = Box::new(unsafe { std::mem::zeroed() });
+ let mut local: Vec<(u64, u64)> = Vec::new();
+
+ while !stop_c.load(Ordering::Relaxed) {
+ let next = Instant::now() + interval;
+
+ ctx_box.0 = unsafe { std::mem::zeroed() };
+ ctx_box.0.ContextFlags = CONTEXT_FULL_AMD64;
+
+ let suspended = unsafe { SuspendThread(target) };
+ if suspended == u32::MAX {
+ break;
+ }
+
+ let ok = unsafe { GetThreadContext(target, &mut ctx_box.0) };
+ let rip = if ok != 0 { ctx_box.0.Rip } else { 0 };
+ unsafe { ResumeThread(target) };
+
+ if rip != 0 {
+ local.push((rip, 1));
+ total_c.fetch_add(1, Ordering::Relaxed);
+ }
+
+ if local.len() >= 1024 {
+ let mut counts = counts_c.lock().unwrap();
+ for (ip, n) in local.drain(..) {
+ *counts.entry(ip).or_insert(0) += n;
+ }
+ }
+
+ let now = Instant::now();
+ if next > now {
+ thread::sleep(next - now);
+ }
+ }
+
+ if !local.is_empty() {
+ let mut counts = counts_c.lock().unwrap();
+ for (ip, n) in local.drain(..) {
+ *counts.entry(ip).or_insert(0) += n;
+ }
+ }
+
+ unsafe { CloseHandle(target) };
+ })
+ .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
+
+ Ok(Self {
+ stop,
+ joiner: Some(joiner),
+ counts,
+ deadline,
+ out,
+ hz,
+ sample_total,
+ })
+ }
+
+ pub fn expired(&self) -> bool {
+ Instant::now() >= self.deadline
+ }
+
+ pub fn finish(mut self) -> std::io::Result<PathBuf> {
+ self.stop.store(true, Ordering::Relaxed);
+ if let Some(j) = self.joiner.take() {
+ let _ = j.join();
+ }
+
+ if let Some(parent) = self.out.parent() {
+ ensure_dir(parent)?;
+ }
+
+ let counts: Vec<(u64, u64)> = {
+ let c = self.counts.lock().unwrap();
+ c.iter().map(|(&k, &v)| (k, v)).collect()
+ };
+
+ let mut resolved: FxHashMap<String, u64> = FxHashMap::default();
+ let mut unresolved: u64 = 0;
+ for (ip, n) in &counts {
+ let mut name: Option<String> = None;
+ backtrace::resolve(*ip as *mut std::ffi::c_void, |sym| {
+ if name.is_none() {
+ if let Some(n) = sym.name() {
+ name = Some(format!("{:#}", n));
+ }
+ }
+ });
+
+ match name {
+ Some(n_str) => *resolved.entry(n_str).or_insert(0) += n,
+ None => unresolved += n,
+ }
+ }
+ if unresolved > 0 {
+ resolved.insert("<unresolved (likely JIT'd code)>".to_string(), unresolved);
+ }
+
+ let mut entries: Vec<(String, u64)> = resolved.into_iter().collect();
+ entries.sort_by(|a, b| b.1.cmp(&a.1));
+
+ let total: u64 = entries.iter().map(|(_, n)| *n).sum();
+ let total_samples = self.sample_total.load(Ordering::Relaxed);
+
+ use std::io::Write;
+ let mut file = std::fs::File::create(&self.out)?;
+ writeln!(file, "# samples={}, hz={}", total_samples, self.hz)?;
+ writeln!(file, "rank,samples,pct,symbol")?;
+
+ for (rank, (sym, n)) in entries.iter().enumerate() {
+ let pct = (*n as f64) * 100.0 / (total.max(1) as f64);
+ writeln!(file, "{},{},{:.2},{}", rank, n, pct, escape_csv(sym))?;
+ }
+
+ file.flush()?;
+ Ok(self.out)
+ }
+ }
+
+ fn escape_csv(s: &str) -> String {
+ if s.contains(',') || s.contains('"') || s.contains('\n') {
+ let mut out = String::with_capacity(s.len() + 2);
+ out.push('"');
+
+ for c in s.chars() {
+ if c == '"' {
+ out.push('"');
+ }
+ out.push(c);
+ }
+
+ out.push('"');
+ out
+ } else {
+ s.to_string()
+ }
+ }
+}
+
+#[cfg(all(feature = "profile", windows))]
+pub use win_sampler::IpSampler;
+
+#[cfg(all(feature = "profile", not(windows)))]
+mod stub_sampler {
+ use super::*;
+
+ pub struct IpSampler;
+
+ impl IpSampler {
+ pub fn start_for_current_thread(_hz: u32, _secs: u32, _out: PathBuf) -> Result<Self, std::io::Error> {
+ Err(std::io::Error::new(
+ std::io::ErrorKind::Unsupported,
+ "in-process sampler is currently Windows-only",
+ ))
+ }
+
+ pub fn expired(&self) -> bool {
+ true
+ }
+
+ pub fn finish(self) -> std::io::Result<PathBuf> {
+ Ok(PathBuf::new())
+ }
+ }
+}
+
+#[cfg(all(feature = "profile", not(windows)))]
+pub use stub_sampler::IpSampler;
diff --git a/crates/gecko/src/scheduler.rs b/crates/gecko/src/scheduler.rs
index 425e5a4..5839c18 100644
--- a/crates/gecko/src/scheduler.rs
+++ b/crates/gecko/src/scheduler.rs
@@ -173,8 +173,23 @@ pub const fn dsp_batch_interval(system: SystemId) -> u64 {
pub fn dsp_batch_handler<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
sys.execute_dsp_batch();
- if !sys.dsp.csr.halt() && !sys.dsp.csr.reset() {
- sys.scheduler
- .schedule_in(self::dsp_batch_interval(SYSTEM), self::dsp_batch_handler::<SYSTEM>);
+ if sys.dsp.csr.halt() || sys.dsp.csr.reset() {
+ return;
}
+
+ let cpu_mail_quiet = !sys.dsp.mailbox_to_dsp_hi.busy();
+ let dsp_mail_full = sys.dsp.mailbox_to_cpu_hi.busy();
+ let (waits_cpu, waits_dsp) = sys.dsp.mailbox_wait_state();
+ let in_idle_wait = (cpu_mail_quiet && waits_cpu) || (dsp_mail_full && waits_dsp);
+ let pending_interrupt = sys.dsp.csr.pi_interrupt() && sys.dsp.registers.status.external_interrupt_enable();
+
+ if in_idle_wait && !pending_interrupt {
+ sys.dsp.scheduler_suspended = true;
+ #[cfg(feature = "jit-stats")]
+ crate::flipper::dsp::DSP_SUSPEND_COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ return;
+ }
+
+ sys.scheduler
+ .schedule_in(self::dsp_batch_interval(SYSTEM), self::dsp_batch_handler::<SYSTEM>);
}
diff --git a/crates/gecko/src/system.rs b/crates/gecko/src/system.rs
index df15d76..7342626 100644
--- a/crates/gecko/src/system.rs
+++ b/crates/gecko/src/system.rs
@@ -67,6 +67,18 @@ pub struct System<const SYSTEM: SystemId> {
#[cfg(feature = "fps-counter")]
pub fps_counter: FpsCounter,
+
+ #[cfg(any(feature = "jit-stats", feature = "profile"))]
+ pub heatmap: crate::profile::HeatmapConfig,
+
+ #[cfg(any(feature = "jit-stats", feature = "profile"))]
+ pub vsync_count: u64,
+
+ #[cfg(feature = "profile")]
+ pub pprof_config: Option<crate::profile::PprofConfig>,
+
+ #[cfg(feature = "profile")]
+ pub pprof_session: Option<crate::profile::IpSampler>,
}
impl<const SYSTEM: SystemId> System<SYSTEM> {
@@ -106,6 +118,18 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
#[cfg(feature = "fps-counter")]
fps_counter: FpsCounter::new(),
+
+ #[cfg(any(feature = "jit-stats", feature = "profile"))]
+ heatmap: crate::profile::HeatmapConfig::default(),
+
+ #[cfg(any(feature = "jit-stats", feature = "profile"))]
+ vsync_count: 0,
+
+ #[cfg(feature = "profile")]
+ pprof_config: None,
+
+ #[cfg(feature = "profile")]
+ pprof_session: None,
}
}
@@ -199,6 +223,100 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
// Drain all events that are now due
self.drain_events();
}
+
+ #[cfg(any(feature = "jit-stats", feature = "profile"))]
+ self.on_vsync_boundary();
+ }
+
+ #[cfg(any(feature = "jit-stats", feature = "profile"))]
+ fn on_vsync_boundary(&mut self) {
+ self.vsync_count = self.vsync_count.wrapping_add(1);
+
+ #[cfg(feature = "jit-stats")]
+ self.dump_heatmap_if_due();
+
+ #[cfg(feature = "profile")]
+ self.tick_pprof_session();
+ }
+
+ #[cfg(feature = "jit-stats")]
+ fn dump_heatmap_if_due(&mut self) {
+ if !self.heatmap.enabled || self.heatmap.interval_frames == 0 {
+ return;
+ }
+
+ if self.vsync_count % self.heatmap.interval_frames as u64 != 0 {
+ return;
+ }
+
+ #[cfg(feature = "jit")]
+ if let Some(jit) = self.jit.as_ref() {
+ if let Err(err) = jit.dump_hot_blocks_csv(self.heatmap.top_k, &self.heatmap.ppc_csv_path()) {
+ tracing::warn!(?err, "ppc heatmap dump failed");
+ }
+ }
+
+ if let Some(jit) = self.dsp.jit.as_ref() {
+ if let Err(err) = jit.dump_hot_blocks_csv(self.heatmap.top_k, &self.heatmap.dsp_csv_path()) {
+ tracing::warn!(?err, "dsp heatmap dump failed");
+ }
+ }
+
+ #[cfg(feature = "jit")]
+ self.dump_idle_skip_sidecar();
+ }
+
+ #[cfg(all(feature = "jit-stats", feature = "jit"))]
+ fn dump_idle_skip_sidecar(&self) {
+ use std::io::Write;
+ use std::sync::atomic::Ordering;
+
+ let calls = crate::gekko::jit::runtime::IDLE_SKIP_CALLS.load(Ordering::Relaxed);
+ let cycles = crate::gekko::jit::runtime::IDLE_SKIP_CYCLES_ADVANCED.load(Ordering::Relaxed);
+ let avg = if calls > 0 { cycles as f64 / calls as f64 } else { 0.0 };
+ let dsp_suspends = crate::flipper::dsp::DSP_SUSPEND_COUNT.load(Ordering::Relaxed);
+ let dsp_wakes = crate::flipper::dsp::DSP_WAKE_COUNT.load(Ordering::Relaxed);
+
+ let path = self.heatmap.out_dir.join("idle-skip.txt");
+ let result = crate::profile::write_file_atomic(&path, |f| {
+ writeln!(
+ f,
+ "vsync_count={}\nppc_idle_calls={}\nppc_cycles_advanced={}\nppc_avg_advance={:.1}\ndsp_suspends={}\ndsp_wakes={}",
+ self.vsync_count, calls, cycles, avg, dsp_suspends, dsp_wakes
+ )
+ });
+ if let Err(err) = result {
+ tracing::warn!(?err, "idle-skip sidecar write failed");
+ }
+ }
+
+ #[cfg(feature = "profile")]
+ fn tick_pprof_session(&mut self) {
+ if self.pprof_session.is_none() {
+ if let Some(cfg) = self.pprof_config.take() {
+ match crate::profile::IpSampler::start_for_current_thread(cfg.hz, cfg.secs, cfg.out.clone()) {
+ Ok(s) => {
+ tracing::info!(
+ "pprof: sampling at {} Hz for {} s, writing to {}",
+ cfg.hz,
+ cfg.secs,
+ cfg.out.display()
+ );
+ self.pprof_session = Some(s);
+ }
+ Err(err) => tracing::warn!(?err, "failed to start pprof sampler"),
+ }
+ }
+ }
+
+ let expired = self.pprof_session.as_ref().is_some_and(|s| s.expired());
+ if expired {
+ let session = self.pprof_session.take().unwrap();
+ match session.finish() {
+ Ok(path) => tracing::info!("pprof samples written to {}", path.display()),
+ Err(err) => tracing::warn!(?err, "pprof sample dump failed"),
+ }
+ }
}
/// JIT inner loop: runs compiled blocks back-to-back until
diff --git a/crates/tinyapp/Cargo.toml b/crates/tinyapp/Cargo.toml
index 5f4920d..a82f534 100644
--- a/crates/tinyapp/Cargo.toml
+++ b/crates/tinyapp/Cargo.toml
@@ -10,6 +10,8 @@ scripting-mut-traps = ["scripting", "gecko/hooks-mut-traps", "scripting/hooks-mu
efb-writeback = ["backend-wgpu/efb-writeback"]
audio-wav-dump = ["gecko/audio-wav-dump"]
fps-counter = ["gecko/fps-counter"]
+jit-stats = ["gecko/jit-stats"]
+profile = ["gecko/profile"]
[dependencies]
gecko = { path = "../gecko" }
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index b0ac142..b2d5bfa 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -74,6 +74,42 @@ struct Args {
/// Disable audio output
#[arg(long)]
no_audio: bool,
+
+ /// Enable per-block JIT heatmap CSV dumps (requires --features jit-stats)
+ #[cfg(feature = "jit-stats")]
+ #[arg(long)]
+ heatmap: bool,
+
+ /// Frames between heatmap CSV writes
+ #[cfg(feature = "jit-stats")]
+ #[arg(long, default_value_t = 60)]
+ heatmap_interval_frames: u32,
+
+ /// Output directory for heatmap CSV files
+ #[cfg(feature = "jit-stats")]
+ #[arg(long, default_value = "./profile-dumps")]
+ heatmap_out: String,
+
+ /// Top-K rows per heatmap CSV
+ #[cfg(feature = "jit-stats")]
+ #[arg(long, default_value_t = 64)]
+ heatmap_top_k: usize,
+
+ /// Run an in-process pprof sampling profile for N seconds, then write a
+ /// flamegraph SVG (requires --features profile)
+ #[cfg(feature = "profile")]
+ #[arg(long)]
+ pprof_secs: Option<u32>,
+
+ /// Sampling rate (Hz) for pprof
+ #[cfg(feature = "profile")]
+ #[arg(long, default_value_t = 997)]
+ pprof_hz: u32,
+
+ /// Output path for the pprof samples CSV (rank,samples,pct,symbol)
+ #[cfg(feature = "profile")]
+ #[arg(long, default_value = "./profile-dumps/pprof-samples.csv")]
+ pprof_out: String,
}
fn resolve_aspect(arg: &str, system: SystemId) -> TargetAspect {
@@ -190,6 +226,25 @@ fn configure<const SYSTEM: SystemId>(emulator: &mut System<SYSTEM>, args: &Args)
}
emulator.apply_host_input(&HostInput::neutral_for(SYSTEM));
+
+ #[cfg(feature = "jit-stats")]
+ {
+ emulator.heatmap = gecko::profile::HeatmapConfig {
+ enabled: args.heatmap,
+ interval_frames: args.heatmap_interval_frames,
+ out_dir: std::path::PathBuf::from(&args.heatmap_out),
+ top_k: args.heatmap_top_k,
+ };
+ }
+
+ #[cfg(feature = "profile")]
+ if let Some(secs) = args.pprof_secs {
+ emulator.pprof_config = Some(gecko::profile::PprofConfig {
+ hz: args.pprof_hz,
+ secs,
+ out: std::path::PathBuf::from(&args.pprof_out),
+ });
+ }
}
fn run<const SYSTEM: SystemId>(mut emulator: System<SYSTEM>, present_mode: wgpu::PresentMode, args: &Args) {