Repository navigation
Conversation
… each guest thread names its pthread '<handle> <name>' (0x1008 Worker) once, right before it enters guest code (the SDK names a thread before StartThread), and the main thread is '0x1001 main thread' from its registration. sample <pid> and Instruments' Time Profiler (xctrace record --attach, no root needed) then show which guest thread a sample belongs to; before, every guest thread was an unnamed pthread of the host, told apart only by its thread ID. Breath of the Wild's CPU hitches were found this way: the main thread and the two Workers in one guest loop. Checked: the host unit tests pass, Breath of the Wild on the Mac shows the names in Time Profiler traces
…d): the CPU hitches of 30-210 ms in steady play were the main thread and two workers spinning in one job queue poll (main+0xef62dc). The first item of some of its jobs is for the thread of core 0; the others wait until it is taken, taking and releasing the queue's lock (an exclusive test-and-set, then a compare-and-clear) and writing a status word on every pass, so the spin wait search (a store or an exclusive store ends a path) did not take it. Polled at full speed by the two workers, the lock went back and forth between their cores, and the main thread, whose item it was, lost nearly every exclusive store: in a 40 s Time Profiler trace it never held the lock while each worker held it in about 750 samples (2,140 samples each of the three in the loop), and every frame with more than 30 ms of CPU work was the three spinning there (DM_HITCH_DUMP showed the main thread's item index 0 and the workers' 1 and 2 in x0, the queue's cursor still 0). patch_nso.py "Spin waits": a lock taken and released on the same address on every path back to the head (spin_lock 'acq' / 'rel', counted like the exclusive adds), a test loop on a word inside the region ('tst', whose branch back goes to the original loop and its own gate), and up to four stores whose value and address registers the region never writes ('stc', only in a region that loads something). Such stores write fixed values to fixed addresses, so a loop can change its own state for a few hundred passes at most, well inside the gate's 2 ms of polling, before its passes repeat. A store of a value a pass changes ends its path and the search runs again without it (the path that takes an item stores the new cursor). Every loop the search took before is still taken, over the same back-edges or a wider region that covers them: when the region with stores does not cover the old one's back-edges, the old one is used. Through the gate the workers leave the lock a moment between passes and the main thread takes its item at once. Zora's Domain on an M2 Max at 120 Hz (one host, 42 s of steady play per run, before -> after, three runs each, load average 2-3.7): standing still, frames with more than 30 ms of work 2.8 / 6.3 / 3.5 -> 0 per 30 s, the frame's work p99 20-21 -> 8.3-8.4 ms, frames late on screen 2.8 / 5.6 / 2.8 -> 2.1 / 0.7 / 2.1 per 30 s (120 Hz refresh slips only); turning the camera, CPU hitches 11.9 / 13.3 / 6.3 -> 0, p99 21-25 -> 9-15 ms, late frames 12.6 / 13.3 / 7.7 -> 1.4 each; process CPU per frame unchanged (about 20 ms). Every module of the games on this Mac rewritten with the old and the new search (109 modules): Breath of the Wild's main 308 -> 310 loops (this one and a state setter at main+0xf82230), its subsdk0 one region wider (0x27804, the movie library's thread loop, 6 -> 23 instructions, over its back-edge and two more), Mario Kart 8 Deluxe's main 25 -> 26 (a state setter at main+0x715df8) and its subsdk0 one region wider (0x24118, the same movie library loop); Hades (main, sdk), Super Smash Bros. Ultimate (main, sdk, subsdk0 and its 98 NROs) and both games' sdk rewrite byte for byte as before. Each new or wider region was read in its disassembly: the state setters store their state and leave on the second pass unless another thread changed it, so they never reach the gate's sleep; the movie loop clears and sets its request flag with constants and calls nothing on the gated paths. Breath of the Wild's main takes 26 s of CPU to rewrite instead of 19. Diagnostic that found it: DM_HITCH_DUMP=<ms>[:<s>] logs every guest thread (its wait, last SVC, where it is, x0-x2 and x19-x25 in guest code) when no frame was presented for that long. New test dm/tools/tests/test_spin_waits.py (the lock poll found and its item path left out; a store of a changing value, a lock not released and stores with no load refused; a plain test loop still found). Checked: the host builds, the host unit tests and the 46 tool tests pass; on the Mac with the new rewrite (hosts of the shader chain each game has on disk: Breath of the Wild b7b9aa970b37, Mario Kart 8 Deluxe 3f475989527f), Breath of the Wild into Zora's Domain and Mario Kart 8 Deluxe for 85 s from boot with A presses, the same red lines as with the old rewrite and no hang. Not checked: games whose files are not on this Mac (Paper Mario: The Thousand-Year Door among them), the iPhone. docs/development.md (Spin loops, Breath of the Wild) and docs/agents/05-debugging.md say so
hle_nvn.c defines dm_present_ns weakly (hle_thread.c holds it), so the graphics tests, which link hle_nvn.c without hle_thread.c, still link.
3e8283f to
0c5324e
Compare
…ath of the Wild; fixes #129). In steady play on the Mac, Breath of the Wild had frames with 30-210 ms of CPU work every 1-10 s: the main thread and the two workers spinning in one job queue poll (main+0xef62dc). The first item of some of its jobs is for the thread of core 0; the others wait until it is taken, taking and releasing the queue's lock (an exclusive test-and-set, then a compare-and-clear) and writing a status word on every pass, so the spin wait search (a store or an exclusive store ends a path) did not take it. Polled at full speed by the two workers, the lock went back and forth between their cores, and the main thread, whose item it was, lost nearly every exclusive store: in a 40 s Time Profiler trace it never held the lock while each worker held it in about 750 samples. patch_nso.py "Spin waits": a lock taken and released on the same address on every path back to the head (spin_lock 'acq' / 'rel', counted like the exclusive adds), a test loop on a word inside the region ('tst', whose branch back goes to the original loop and its own gate), and up to four stores whose value and address registers the region never writes ('stc', only in a region that loads something). Such stores write fixed values to fixed addresses, so a loop can change its own state for a few hundred passes at most, well inside the gate's 2 ms of polling, before its passes repeat. A store of a value a pass changes ends its path and the search runs again without it (the path that takes an item stores the new cursor); when the region with stores does not cover the old one's back-edges, the old one is used. Through the gate the workers leave the lock a moment between passes and the main thread takes its item at once. Zora's Domain on an M2 Max at 120 Hz (one host, 42 s of steady play per run, three runs each, before -> after): standing still, frames with more than 30 ms of work 2.8 / 6.3 / 3.5 -> 0 per 30 s, the frame's work p99 20-21 -> 8.3-8.4 ms; turning the camera, CPU hitches 11.9 / 13.3 / 6.3 -> 0, p99 21-25 -> 9-15 ms; process CPU per frame unchanged. Every module on the game drives rewritten with the old and the new search (206: 108 NSOs and Super Smash Bros. Ultimate's 98 NROs): 175 byte for byte as before (Hades, Super Smash Bros. Ultimate, Super Mario Odyssey among them), 7 fail alike under both (every rtld, New Super Mario Bros. U Deluxe), and 24 take new or wider loops, all but Breath of the Wild's queue through the store rule alone: Monster Hunter Rise's main 90 -> 166 (72 of them one inlined countdown whose reset makes the next pass leave), BALL x PIT 87 -> 94, Animal Crossing: New Horizons 162 -> 166, Balatro 9 -> 12, Octopath Traveler 0 75 -> 78, Pikmin 4 80 -> 83, Bayonetta 3 35 -> 37, Breath of the Wild 308 -> 310, one more each in Big Brain Academy, Mario Kart 8 Deluxe, Metroid Dread, Pokemon Legends: Z-A, Stardew Valley, Super Mario Bros. Wonder (both builds) and Tears of the Kingdom; the movie library's thread loop is wider in the subsdk0 of Breath of the Wild, Mario Kart 8 Deluxe, Animal Crossing, BALL x PIT, Monster Train 2 and Octopath Traveler 0, and one loop is wider in the main of AI: THE SOMNIUM FILES and Advance Wars 1+2. No loop of the old search is lost or loses a back-edge. Each distinct shape was read in its disassembly: one-pass resets and state setters (the gate is reached only when another thread changes the word), parser states that leave on the next pass, and real flag waits. Rewriting all 206 takes about 4,100 s of CPU instead of 3,250. Diagnostics: DM_HITCH_DUMP=<ms>[:<s>] logs every guest thread (its wait, last SVC, where it is, x0-x2 and x19-x25 in guest code) when no NVN frame was presented for that long, and each guest thread names its pthread '<handle> <name>' (0x1008 Worker) before it enters guest code, the main thread '0x1001 main thread', so sample and Instruments' Time Profiler show which guest thread a sample belongs to. The graphics tests that link hle_nvn.c without the host define dm_present_ns (t_host_stubs.c weak, t_nvn.c). New test dm/tools/tests/test_spin_waits.py. Checked: the host builds, the host unit tests, the 46 tool tests and the gfx run list pass; Breath of the Wild into Zora's Domain and Mario Kart 8 Deluxe for 85 s from boot with the new rewrite (the PR's author), and Monster Hunter Rise from a fresh save for 180 s headless with each rewrite on one host (e72a065's, the chain of its Mac libraries): 60 fps, frame p95 about 17.5 ms, the same red lines, no hang. Not checked: Paper Mario: The Thousand-Year Door, DARK SOULS: REMASTERED, FINAL FANTASY TACTICS and FINAL FANTASY RESONANCE (no files on the drives), the iPhone. docs/development.md (Spin loops, Breath of the Wild) and docs/agents/05-debugging.md say so
|
Taken as 6d34984 on main, squashed with two additions:
Checked here: the host unit tests, the tool tests and the gfx run list pass. Monster Hunter Rise, the game with the most new loops, ran alike with both rewrites on one host, 180 s headless from a fresh save each: 60 fps, frame p95 about 17.5 ms, the same red lines, no hang. Not checked: TTYD, DARK SOULS, FFT and FINAL FANTASY RESONANCE (not on my drives), and the iPhone. Two small things about
Thanks for the profiler work, the hitch dump and the thread names! They made the cause easy to follow. |
…s and the dyld lock (follow-up to #134's review). The present time dm_present_ns was written only in the NVN present (hle_nvn.c), so the dump never fired for the Vulkan and GL games; it is now written in present_enqueue (nvn_mtl.m), which mtl_present (NVN), mtl_present_ext (Vulkan: hle_vk.m; GL: hle_gl.m) all reach, and the NVN-only write is gone. hitch_main took thr_t pointers under G, released G, and read the threads afterwards, so a thread that exits and is freed during a dump was a use-after-free; and it called dladdr while the target thread was suspended, which deadlocks if that thread holds the dyld lock. Now hitch_grab copies what a line needs (handle, name, wait state, svc, registers, the raw pc / lr / frame-pointer chain) into a hitch_t while holding G (thread exit and thr_free need G, so no listed thread is freed or leaves), and between suspend and resume only mach calls and memory reads run; hitch_log then looks the symbols up (module table, dladdr) and logs after every thread runs again. The tests' weak dm_present_ns definitions (t_host_stubs.c, t_nvn.c) stay as they are. Checked: the host unit tests and the gfx run list pass (GAME=01007EF00011E000); the Mac host builds for Breath of the Wild (NVN) and Hades (Vulkan). From PR #137 (v0lumehi). Also checked on current main: the host unit tests and the gfx run list pass; on the Mac with DM_HITCH_DUMP=20:2, Mario vs. Donkey Kong (NVN) logged two dumps in its opening movie and Balatro (GL, which never dumped before: no present time was written outside NVN) one, every guest thread with its guest frames and host symbols, and both games ran on to the end of the run. Not checked: a Vulkan game (none on the drives; Vulkan presents reach present_enqueue through mtl_present_ext as GL's do)
…on Breath of the Wild; fixes seedds#129). In steady play on the Mac, Breath of the Wild had frames with 30-210 ms of CPU work every 1-10 s: the main thread and the two workers spinning in one job queue poll (main+0xef62dc). The first item of some of its jobs is for the thread of core 0; the others wait until it is taken, taking and releasing the queue's lock (an exclusive test-and-set, then a compare-and-clear) and writing a status word on every pass, so the spin wait search (a store or an exclusive store ends a path) did not take it. Polled at full speed by the two workers, the lock went back and forth between their cores, and the main thread, whose item it was, lost nearly every exclusive store: in a 40 s Time Profiler trace it never held the lock while each worker held it in about 750 samples. patch_nso.py "Spin waits": a lock taken and released on the same address on every path back to the head (spin_lock 'acq' / 'rel', counted like the exclusive adds), a test loop on a word inside the region ('tst', whose branch back goes to the original loop and its own gate), and up to four stores whose value and address registers the region never writes ('stc', only in a region that loads something). Such stores write fixed values to fixed addresses, so a loop can change its own state for a few hundred passes at most, well inside the gate's 2 ms of polling, before its passes repeat. A store of a value a pass changes ends its path and the search runs again without it (the path that takes an item stores the new cursor); when the region with stores does not cover the old one's back-edges, the old one is used. Through the gate the workers leave the lock a moment between passes and the main thread takes its item at once. Zora's Domain on an M2 Max at 120 Hz (one host, 42 s of steady play per run, three runs each, before -> after): standing still, frames with more than 30 ms of work 2.8 / 6.3 / 3.5 -> 0 per 30 s, the frame's work p99 20-21 -> 8.3-8.4 ms; turning the camera, CPU hitches 11.9 / 13.3 / 6.3 -> 0, p99 21-25 -> 9-15 ms; process CPU per frame unchanged. Every module on the game drives rewritten with the old and the new search (206: 108 NSOs and Super Smash Bros. Ultimate's 98 NROs): 175 byte for byte as before (Hades, Super Smash Bros. Ultimate, Super Mario Odyssey among them), 7 fail alike under both (every rtld, New Super Mario Bros. U Deluxe), and 24 take new or wider loops, all but Breath of the Wild's queue through the store rule alone: Monster Hunter Rise's main 90 -> 166 (72 of them one inlined countdown whose reset makes the next pass leave), BALL x PIT 87 -> 94, Animal Crossing: New Horizons 162 -> 166, Balatro 9 -> 12, Octopath Traveler 0 75 -> 78, Pikmin 4 80 -> 83, Bayonetta 3 35 -> 37, Breath of the Wild 308 -> 310, one more each in Big Brain Academy, Mario Kart 8 Deluxe, Metroid Dread, Pokemon Legends: Z-A, Stardew Valley, Super Mario Bros. Wonder (both builds) and Tears of the Kingdom; the movie library's thread loop is wider in the subsdk0 of Breath of the Wild, Mario Kart 8 Deluxe, Animal Crossing, BALL x PIT, Monster Train 2 and Octopath Traveler 0, and one loop is wider in the main of AI: THE SOMNIUM FILES and Advance Wars 1+2. No loop of the old search is lost or loses a back-edge. Each distinct shape was read in its disassembly: one-pass resets and state setters (the gate is reached only when another thread changes the word), parser states that leave on the next pass, and real flag waits. Rewriting all 206 takes about 4,100 s of CPU instead of 3,250. Diagnostics: DM_HITCH_DUMP=<ms>[:<s>] logs every guest thread (its wait, last SVC, where it is, x0-x2 and x19-x25 in guest code) when no NVN frame was presented for that long, and each guest thread names its pthread '<handle> <name>' (0x1008 Worker) before it enters guest code, the main thread '0x1001 main thread', so sample and Instruments' Time Profiler show which guest thread a sample belongs to. The graphics tests that link hle_nvn.c without the host define dm_present_ns (t_host_stubs.c weak, t_nvn.c). New test dm/tools/tests/test_spin_waits.py. Checked: the host builds, the host unit tests, the 46 tool tests and the gfx run list pass; Breath of the Wild into Zora's Domain and Mario Kart 8 Deluxe for 85 s from boot with the new rewrite (the PR's author), and Monster Hunter Rise from a fresh save for 180 s headless with each rewrite on one host (903b236's, the chain of its Mac libraries): 60 fps, frame p95 about 17.5 ms, the same red lines, no hang. Not checked: Paper Mario: The Thousand-Year Door, DARK SOULS: REMASTERED, FINAL FANTASY TACTICS and FINAL FANTASY RESONANCE (no files on the drives), the iPhone. docs/development.md (Spin loops, Breath of the Wild) and docs/agents/05-debugging.md say so
…s and the dyld lock (follow-up to seedds#134's review). The present time dm_present_ns was written only in the NVN present (hle_nvn.c), so the dump never fired for the Vulkan and GL games; it is now written in present_enqueue (nvn_mtl.m), which mtl_present (NVN), mtl_present_ext (Vulkan: hle_vk.m; GL: hle_gl.m) all reach, and the NVN-only write is gone. hitch_main took thr_t pointers under G, released G, and read the threads afterwards, so a thread that exits and is freed during a dump was a use-after-free; and it called dladdr while the target thread was suspended, which deadlocks if that thread holds the dyld lock. Now hitch_grab copies what a line needs (handle, name, wait state, svc, registers, the raw pc / lr / frame-pointer chain) into a hitch_t while holding G (thread exit and thr_free need G, so no listed thread is freed or leaves), and between suspend and resume only mach calls and memory reads run; hitch_log then looks the symbols up (module table, dladdr) and logs after every thread runs again. The tests' weak dm_present_ns definitions (t_host_stubs.c, t_nvn.c) stay as they are. Checked: the host unit tests and the gfx run list pass (GAME=01007EF00011E000); the Mac host builds for Breath of the Wild (NVN) and Hades (Vulkan). From PR seedds#137 (v0lumehi). Also checked on current main: the host unit tests and the gfx run list pass; on the Mac with DM_HITCH_DUMP=20:2, Mario vs. Donkey Kong (NVN) logged two dumps in its opening movie and Balatro (GL, which never dumped before: no present time was written outside NVN) one, every guest thread with its guest frames and host symbols, and both games ran on to the end of the run. Not checked: a Vulkan game (none on the drives; Vulkan presents reach present_enqueue through mtl_present_ext as GL's do)
Fixes #129.
What
patch_nso.py"Spin waits": the search now also takes polls that look at their state under a spin lock. A region may contain:A store of a value that a pass changes ends its path, and the search runs again without that path (the path that takes an item writes the new cursor). Every loop the search took before is still taken, over the same back-edges or a wider region that covers them.
DM_HITCH_DUMP=<ms>[:<s>](diagnostic): when no frame has been presented for<ms>, it logs every guest thread once per stall. For each thread: what it waits on, its last SVC, its pc / lr and frame-pointer chain, and x0–x2 / x19–x25 when it runs guest code.Guest threads are named on the host (separate commit) as
<handle> <guest name>, sosampleand Instruments' Time Profiler show which guest thread each sample belongs to.New test
dm/tools/tests/test_spin_waits.py.docs/development.md(Spin loops, the Breath of the Wild notes) anddocs/agents/05-debugging.mdare updated.Why
In steady play, Breath of the Wild on the Mac had frames with 30–210 ms of CPU work every 1–10 s. These were the visible stutter, since present snapshots already absorb its GPU spikes.
Workerthreads sat in one job-queue function (main+0xef62dc), each using most of a core; all other threads were idle.On the Switch the same code waits only until the main thread's core gets to the item. On the host the lock's cache line went to the workers almost every time.
Why pausing these loops is safe: each allowed store writes a fixed value to a fixed address, and nothing live at the head changes. So a loop can change its own state only for a bounded number of passes (a few hundred at most, a few microseconds). After that its passes repeat, and only another thread can end it. The gate polls without sleeping for its first 2 ms, so a loop that ends by itself never sleeps there.
Numbers
Zora's Domain save, M2 Max at 120 Hz, the same host for both, about 42 s of steady play per run, three runs each, load average 2–3.7:
Process CPU per frame is unchanged, at about 20 ms.
Effect on other games
All 109 modules of the games on this Mac were rewritten with the old and the new search:
main0xef6354, and a state setter0xf82230subsdk00x27804(the movie library's thread loop) widened from 6 to 23 instructions: its old back-edge plus two moresdkmain0x715df8subsdk00x24118widened (the same movie library loop)sdkmain,sdkmain,sdk,subsdk0, 98 NROsI read the disassembly of each new or wider region:
main+0xf82230in Breath of the Wild,main+0x715df8in Mario Kart 8 Deluxe): they read a state word, store the new state, and leave on the second pass unless another thread changed the word in between. They never reach the gate's sleep. They are not waits, but gating them is harmless.Rewrite time: Breath of the Wild's
maintakes 26 s of CPU instead of 19, because paths through stores are now searched.Checked
DM_METAL_HUD=1, each with a host of the shader chain that game has on disk:gpu-spikesbranch, chainb7b9aa970b37. The A/B runs above, and a boot into Zora's Domain with the rewrite from this branch'spatch_nso.py. Same red lines as with the old rewrite (the stalec1_full.tsvonly), no hang. In the window measured, CPU hitches went from 14.6 to 0 per 30 s, and frame work p99 from 27.3 to 8.5 ms.dmhost-coop, chain3f475989527f. 85 s from boot, with A presses every 4 s from 20 s on. Old and new rewrite: the same two red lines, no hang, frame p50 16.67 ms and p99 about 19.5 ms in both.Not checked