Skip to content

nvn/present: lift the GPU clock when a lowered clock holds 30 fps - #3586

Open
Kayyluhh wants to merge 1 commit into
seedds:mainfrom
Kayyluhh:nvn/gpu-clock-keeper
Open

Kayyluhh wants to merge 1 commit into
seedds:mainfrom
Kayyluhh:nvn/gpu-clock-keeper

Conversation

@Kayyluhh

@Kayyluhh Kayyluhh commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

What and why

On the Mac, Donkey Kong Bananza drops from 60 to 30 fps in a heavy moment and stays at 30 for tens of seconds, until a pause or a view of a wall lets it back. The game picks its own present interval: its frame-rate controller (main+0x204a18) returns to one vsync only once a frame fits 1.2 vsyncs (20 ms). At two vsyncs the GPU idles half of each frame, macOS lowers the GPU clock, and the same frame takes longer. On main at e693bb89 (M3 Max MacBook Pro, a window, MetalFX), about 5,000 draws took 12 ms of GPU at 60 fps and 20.4–22.4 ms at 30 fps, so the frames never fit again. On the Switch the GPU clock does not follow the load, so the game's rule is right there. The host already gives the CPU scheduler the shorter deadline for the same reason (rt_period_ns); nothing did it for the GPU.

This is the "focused PR with fresh main measurements and suspend/quit handling" asked for when #1847 was closed. Unlike #1847's keeper, it does not spin whenever frames are slow. It probes, and only while the game sits at a longer hold:

  • When: a present's period is more than 1.5 times the shortest one seen, and a frame's GPU time is above 0.6 of that shortest period. It never runs at the shortest hold, so a game at its full frame rate, a game whose only hold is the longer one, and a game that is light at the longer hold cost nothing.
  • What: one 32-thread threadgroup spins on a queue of its own, only while the game's queue has nothing in flight (rx_inflight), about 0.8 ms per spin, for at most 4 s.
  • Stops: as soon as the period shortens (won). Otherwise the probe is lost: 8 s before the next try, doubled after each further loss, and after 4 losses in a row it waits until the period shortens by itself.
  • Pause, suspend and quit: it does not run while the window is unfocused (dm_focus_now) or covered, while nothing is presented, while presents are discarded for a suspend, at thermal state serious or worse, or in Low Power Mode. At quit it is stopped and waited for (atexit). At the shortest hold its thread sleeps on a condition variable.
  • Mac only. DM_GPU_KEEP=0 turns it off. The log has a line for each probe (started, won, lost, stopped and why).

The decisions are pure, in dm/gfx/nvn/nvn_gpu_keep.h, with a test, t_gpu_keep. The thread is gpu_keep_main in nvn_mtl.m. Reference entry: A game's own 30 fps fallback held by the GPU clock its longer hold lowered. Bananza's note links it.

Checked

  • Tests: t_gpu_keep (30 checks, new, added to the CI list), t_link_latch_cb, t_present_rescue, t_gfx_suspend, check_t_present_snap, test_docs, test_env_switches. The full make -C dm/gfx/tests run on the branch rebased onto 1ca449bc9: five checks fail, all on main at 1ca449bc9 too, with the same messages (t_cpu_tex "late descriptor views retire at submission end" ×2, t_gl_imports abort, t_mdx_prescan indirect barrier after a shader write, t_pool_chunks line 176, t_transform_feedback capture after an inline update); the rest pass.
  • Games run: Donkey Kong Bananza 040074A01BF12000 v458752, played by hand. Eight sessions alternated with the keeper on and off (DM_GPU_KEEP=0), each starting from the same restored save. The last pair visited six layers and zoomed the map out and turned it.
    • With the keeper: 23 probes, all won, the game back at 60 fps 0.3–0.7 s after each. No 300-frame window at 30 fps in about 120 gameplay windows. At most 2.5 s of spinning in a session (18 probes in 2.8 minutes).
    • Without it: two windows locked at 30 fps (frame p50 33.3 ms, GPU p50 24.6–25.3 ms at 9,600–13,800 draws).
    • Two more sessions with the keeper, played over the same kind of route for a replay recording: 34 probes, all won, the game back at 60 fps 0.2–1.6 s after each. In all, 57 probes and 57 won. (A replay of the recordings with DM_KEYS did not stay on its path, since walking into the warp gongs does not land the same twice. DM_DETCLOCK would make it repeat, but it hands the game a fixed clock, and the controller measures that clock, so the 30 fps hold could not happen under it.)
    • Single-zone sessions showed no drops either way. Two sessions per side are not enough to compare: the first pair differed only by the clock macOS happened to pick.
  • Power (powermetrics, the same eight sessions): mean GPU power 10.4–12.8 W with the keeper, 11.9–12.5 W without.
  • Device and OS: M3 Max MacBook Pro (Mac15,10), macOS 27.2.1 (26B131).
  • scripts/regress.py check against 1dae211a9. regress.py impact names every NVN game (the present path); of the profiles with a usage manifest none is local, so these five cover the present path, two frame-rate controllers that switch intervals, and three games that never do:
game status fps frame p99 ms peak MB prepare s new RED keeper
Donkey Kong Bananza 040074A01BF12000 PASS 60.0 → 60.0 16.86 → 17.16 8162 → 8164 14.19 → 1.08 0 never probed (the route stayed at 60)
Star Fox 0400E88025B1A000 PASS 60.0 → 60.0 19.19 → 19.32 7941 → 7989 2.16 → 0.16 0 never probed
Mario Kart World 0400C3F00006E000 PASS 60.0 → 59.7 18.56 → 18.16 10065 → 11202 11.99 → 0.63 0 1 probe at a race end's load, back at interval 1 in 0.6 s
Metroid Prime Remastered 010012101468C000 PASS 60.0 → 60.0 17.48 → 17.34 1186 → 1190 0.12 → 0.01 0 never probed
The Legend of Zelda: Ocarina of Time 0400486024DEC000 REGRESSION (see below) 58.3 → 58.3 33.51 → 33.40 6504 → 6516 2.61 → 2.58 1 never probed
  • Ocarina of Time, first check: flagged for p99 (17.62 → 32.93 ms), prepare time (2.55 → 15.43 s) and screenshots. The prepare step runs before the first present, where the keeper does nothing. Across three runs of identical code it took 61.3, 2.55 and 15.4 s, depending on Metal's shader cache. The keeper run's first frame came 13 s later, so the fixed-time screenshots caught other moments of the title movie. The baseline's own first attempt, redone because its window was covered, had 36 frames over 20 ms in the measuring window against the keeper run's 43. This is the harness issue main has since fixed in cb58c93e5 (regress.py: a late first frame fails Ocarina of Time's screenshot check #3513: route seconds count from the first presented frame); the run used the harness before it.
  • Ocarina of Time, second check, both sides fresh (the row above): p99 33.51 vs 33.40 ms, screenshots match. It was flagged only for one slow residency commit (145 ms, 1,898 adds, 526 MB) at startup in the keeper run, where the keeper never probed. Its game note lists that line as its usual [RED].
  • Mario Kart World's peak memory: the baseline spent its first 12 s preparing, so at the same clock time the candidate was further into the race (frame 11,674 against 10,870), the same harness issue as above.
  • Not checked: other Macs, a fanless one above all (the reason Donkey Kong Bananza: render scale, acquire sync, and translation / GPU clock work for heavy NVN frames #1847's keeper was declined). Testers with an Air would help.
  • Rebased onto b1f95bdb9 after the regress run; the shader chain changed on main (e885e184f677 → da51882ee408), not here. The one conflict was the CI test list (t_gpu_keep added after t_link_latch). On the rebased branch the host builds, and t_gpu_keep, t_link_latch_cb, t_link_latch, t_link_drain, t_fstep, t_present_rescue, t_gfx_suspend, every mode of check_t_present_snap and test_docs pass. test_env_switches fails, with the same output on b1f95bdb9 itself (DM_YAZ0_NATIVE has no row, and some removed switches are still quoted); DM_GPU_KEEP has its row. The full graphics suite ran on 1ca449bc9, as above. The regress run is on 1dae211a9, before main's cb58c93e5.

Docs

  • The finding is in its docs/reference/frame-rate.md entry and Bananza's note, and python3 dm/tools/docs_symptoms.py was run
  • No chain tool changed
  • No game files, keys or data extracted from a game in the change or the attachments

macOS sets the GPU's clock from how busy it was. A game that picks its
own present interval and holds two vsyncs leaves the GPU idle half of
each frame, the clock falls, and the same frame takes longer: Donkey
Kong Bananza drew about 5,000 draws in 12 ms of GPU at 60 fps and in
20.4-22.4 ms at 30 fps (main at e693bb8). Its frame-rate controller
returns to one vsync only once a frame fits 1.2 vsyncs, so it stayed at
30 fps for tens of seconds, until a lighter view let it back. On the
Switch the GPU clock does not follow the load. The host already declares
the shorter deadline to the CPU scheduler for this reason
(rt_period_ns); nothing did it for the GPU.

The GPU clock keeper (Mac only) probes instead of spinning whenever
frames are slow, as PR seedds#1847's did. Its decisions are pure, in
nvn_gpu_keep.h: when a present's period is over 1.5 times the shortest
seen and a frame's GPU time is above 0.6 of that period, it keeps one
32-thread threadgroup spinning on a queue of its own for up to 4 s, only
while the game's queue has nothing in flight (each spin about 0.8 ms).
It stops as soon as the period shortens. A lost probe waits 8 s,
doubled after each further loss, and after 4 in a row it waits until
the period shortens by itself. It never runs at the shortest hold. The
thread (gpu_keep_main in nvn_mtl.m) sleeps on a condition variable at
the shortest hold, stops while the window is not focused or is covered,
while nothing is presented, while presents are discarded for a suspend,
at thermal state serious and in Low Power Mode, and is stopped and
waited for at quit. DM_GPU_KEEP=0 turns it off. The log has a line for
each probe, written before the thread sleeps.

Checked: t_gpu_keep (30 checks, new, in the CI list), t_link_latch_cb,
t_present_rescue, t_gfx_suspend, check_t_present_snap, test_docs and
test_env_switches; the host builds for Bananza. Played on an M3 Max
MacBook Pro, 8 sessions alternating on and off from one restored save:
with it, 23 probes, all won within 0.3-0.7 s, no 300-frame window at
30 fps; without it, two windows at 30 fps (GPU p50 24.6-25.3 ms).
powermetrics: GPU power 10.4-12.8 W with it, 11.9-12.5 W without.
regress.py check against 1dae211 on Bananza, Star Fox, Mario Kart
World, Metroid Prime Remastered and Ocarina of Time: PASS on the first
four; Ocarina of Time flagged for p99 on its first check (the baseline
run's own p99 is 33.5 ms on a second check, same as the keeper's 33.4)
and on its second for one slow residency commit at startup, which it
logs now and then on main too. Not checked: other Macs, a fanless one
above all.
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation ci Continuous integration and repository settings (.github) game: 040074A01BF12000 Donkey Kong Bananza labels Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continuous integration and repository settings (.github) docs Documentation game: 040074A01BF12000 Donkey Kong Bananza nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant