Files
psxrecomp/tools/_options_session/_capture_stuck.py
T
Matthew Stanley fdbd2b515f runtime: cross-fiber longjmp fix + decouple vblank-present from MMIO read
Two distinct bugs were causing OPTIONS-black hard freezes after 12K-65K
frames:

1. interrupts.c + traps.c: exception_jmpbuf is a single global. setjmp
   captures the running fiber, but longjmp may be requested from a
   different fiber via psx_exception_longjmp / psx_restore_state_escape
   during chain-handler dispatch. Cross-fiber longjmp restores RSP to
   the wrong stack while Win32 still tracks the calling fiber, corrupting
   fiber state until SwitchToFiber eventually hangs.

   Fix: track g_exception_owner_fiber at setjmp time. New
   deferred_exception_longjmp checks the current fiber; if it's not the
   owner, sets a pending flag and SwitchToFiber's back to the owner.
   psx_change_thread_fiber's post-SwitchToFiber path observes the flag
   and executes the longjmp on the correct stack.

2. gpu.c: gpu_read_gpustat fired gpu_vblank_tick() after 1000 polls,
   which invoked sdl_vblank_present (SDL_Delay -> Sleep) from inside
   an MMIO read code path. That meant recompiled MIPS spent wall time
   in Sleep deep inside the fiber call tree -- confusing to reason
   about and a contributor to fiber/longjmp interactions.

   Fix: synthetic VBLANK from a poll-storm only updates emulation state
   (LCF + I_STAT). The host-side render+sleep happens only at the proper
   cycle-paced VBLANK trigger in psx_check_interrupts.

Also adds: dirty_block ring slice to the freeze auto-dump, Win32 main-
thread stack capture for wedge_kind=1 hard freezes (SuspendThread +
StackWalk64 + SymFromAddr), and dbghelp link.

Validated: TombaRecomp psx-runtime ran 30+ minutes / 105K+ frames at
OPTIONS with no hard freeze. Previous freezes consistently hit at
11900, 20493, and 64596 frames.

Diagnostic tooling under tools/_options_session/ that produced this
diagnosis is included.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 12:49:13 -07:00

76 lines
3.1 KiB
Python

"""Capture the current divergence snapshot between runtime and beetle
when runtime is in a stuck/black-screen state.
Reads from the rings we just built — no arming, no probes:
- always-on wtrace_all on BOTH (last 200 writes)
- per-frame ring last 200 frames on BOTH (CPU/display/IRQ over time)
- freeze_check on runtime (handler/dispatch/dirty diagnostics)
Writes everything to /tmp/optsess/stuck_<timestamp>.json so we don't
keep re-querying as state churns.
"""
from __future__ import annotations
import sys, os, json, time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _dbg import call, RUNTIME_PORT, BEETLE_PORT
def safe(port, cmd, **kw):
try: return call(port, cmd, **kw, timeout=15)
except Exception as e: return {"error": str(e)}
def capture(port, label):
out = {"label": label, "port": port}
out["ping"] = safe(port, "ping")
out["history"] = safe(port, "history")
out["wtrace_all_stats"] = safe(port, "wtrace_all_stats")
out["wtrace_all_dump"] = safe(port, "wtrace_all_dump", count=512)
# Per-frame ring: pull last 200 frames
h = out["history"]
if h.get("ok"):
newest = h["newest"]
start = max(0, newest - 199)
out["frame_range_start"] = start
out["frame_range_end"] = newest
out["frame_timeseries"] = safe(port, "frame_timeseries", start=start, end=newest)
out["get_frame_newest"] = safe(port, "get_frame", frame=newest)
if label == "runtime":
out["freeze_check"] = safe(port, "freeze_check", window=512)
out["wtrace_ranges"] = safe(port, "wtrace_ranges")
return out
print("Capturing snapshot…")
snap = {
"ts": time.strftime("%Y-%m-%dT%H:%M:%S"),
"runtime": capture(RUNTIME_PORT, "runtime"),
"beetle": capture(BEETLE_PORT, "beetle"),
}
out_dir = os.path.join(os.environ.get("TEMP", "/tmp"), "optsess")
os.makedirs(out_dir, exist_ok=True)
out_path = os.path.join(out_dir, f"stuck_{int(time.time())}.json")
with open(out_path, "w") as f:
json.dump(snap, f, indent=2)
print(f"wrote {out_path} ({os.path.getsize(out_path)} bytes)")
# Print a one-screen summary
print(f"\nruntime: frame={snap['runtime']['ping']['frame']} disp_miss={snap['runtime']['ping']['dispatch_miss_total']}")
print(f"beetle : frame={snap['beetle']['ping']['frame']}")
fc = snap["runtime"]["freeze_check"]
if fc.get("ok"):
print(f"\nruntime freeze_check:")
for k in ("current_func", "last_store_pc", "i_stat", "i_mask",
"exception_reentry_blocks", "dirty_ram_blocks", "dispatch_count",
"frame_count", "in_exception", "sio_stat", "mc_max_state"):
if k in fc: print(f" {k:32s} = {fc[k]}")
# Last 5 writes on each backend's catch-all
print(f"\nruntime wtrace_all newest 3:")
for e in snap["runtime"]["wtrace_all_dump"]["entries"][-3:]:
print(f" seq={e['seq']} addr={e['addr']} new={e['new']} pc={e['pc']} ra={e['ra']} fr={e['frame']}")
print(f"beetle wtrace_all newest 3:")
for e in snap["beetle"]["wtrace_all_dump"]["entries"][-3:]:
print(f" seq={e['seq']} addr={e['addr']} new={e['new']} pc={e['pc']} ra={e['ra']} fr={e['frame']}")