Files
psxrecomp/tools/_options_session/_thread_audit.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

45 lines
1.6 KiB
Python

"""Inspect thread_trace and look for fiber switches near the freeze."""
import json, sys, collections
dump = sys.argv[1] if len(sys.argv) > 1 else \
'F:/Projects/TombaRecomp/psx_freeze_dump_psx-runtime_1778822828.json'
with open(dump) as f:
d = json.load(f)
tt = d.get('thread_trace', [])
print(f"thread_trace: {len(tt)} entries")
if tt:
print(f" first: {tt[0]}")
print(f" last : {tt[-1]}")
kinds = collections.Counter(e.get('kind') for e in tt)
print(f" kind distribution: {kinds}")
print()
print(f"Last 30 thread_trace entries:")
for e in tt[-30:]:
print(f" {e}")
# Count 0x650 dispatches (SwitchThread)
db = d.get('dirty_block', [])
print()
print(f"=== 0x650 (SwitchThread) calls in dirty_block ring ===")
sw = [e for e in db if e['target'] == '0x00000650']
print(f" count: {len(sw)}")
for e in sw[-15:]:
print(f" seq={e['seq']} fr={e['frame']} target=0x650 ra={e['ra']} "
f"a0={e['a0']} a1={e['a1']} sp={e['sp']}")
# Look in fn_entry for 0x650 to count and see all params
fe = d.get('fn_entry', [])
print()
sw2 = [e for e in fe if e['func'] == '0x00000650']
print(f"=== 0x650 calls in fn_entry: {len(sw2)} ===")
for e in sw2[-15:]:
print(f" seq={e['seq']} fr={e['frame']} ra={e['ra']} a0={e['a0']} a1={e['a1']}")
# Look at any function in fn_entry with a0=3 (likely ChangeThread syscalls)
a0_3 = [e for e in fe if e.get('a0') == '0x00000003']
print()
print(f"=== fn_entry with a0=0x3 (ChangeThread param) ===")
print(f" count: {len(a0_3)}")
for e in a0_3[-15:]:
print(f" seq={e['seq']} fr={e['frame']} func={e['func']} ra={e['ra']} a1={e['a1']}")