diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ad877acde9..72279548ac 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -64,7 +64,7 @@ jobs: make GAME="$GAME" -j3 tsan-dirty-init tsan-dirty-reuse 2>&1 | tee ../../../logs/t_dirty-tsan.log make GAME="$GAME" -j3 present-history-tsan 2>&1 | tee ../../../logs/t_present_history-tsan.log # Each name is a test target in dm/gfx/tests/Makefile that runs without a GPU, listed by hand: a test removed there is removed here too - tests="t_gfx_suspend t_capture t_dirty_slots t_dirty_race t_dirty_retire_race t_dirty_init t_dirty_reuse t_tex_layout t_pace t_fstep t_link_drain t_link_latch t_link_ticks t_present_history t_cpu_tex t_pkg_share t_hmap t_pso_skey t_refl t_present_rescue t_gpuprof t_gpu_timing t_sparse_layout t_timestamp t_counter_clock t_ub t_pso_pending t_vk_desc t_rec_draws t_ubc_wait t_thread_rt t_rt_comp t_rt_stall t_pend_index t_bind_stages t_prog_bind_identity t_missing_keys t_yaz0" + tests="t_gfx_suspend t_capture t_dirty_slots t_dirty_race t_dirty_retire_race t_dirty_init t_dirty_reuse t_tex_layout t_pace t_fstep t_link_drain t_link_latch t_gpu_keep t_link_ticks t_present_history t_cpu_tex t_pkg_share t_hmap t_pso_skey t_refl t_present_rescue t_gpuprof t_gpu_timing t_sparse_layout t_timestamp t_counter_clock t_ub t_pso_pending t_vk_desc t_rec_draws t_ubc_wait t_thread_rt t_rt_comp t_rt_stall t_pend_index t_bind_stages t_prog_bind_identity t_missing_keys t_yaz0" make GAME="$GAME" -j3 $tests 2>&1 | tee ../../../logs/gfx-build.log failed=0 for t in $tests; do diff --git a/dm/games/040074A01BF12000/README.md b/dm/games/040074A01BF12000/README.md index 42b2d64ac0..3b4115539f 100644 --- a/dm/games/040074A01BF12000/README.md +++ b/dm/games/040074A01BF12000/README.md @@ -98,7 +98,7 @@ Each step below stopped the boot before its fix: - `oe::GetCurrentGraphicsScale`: which tier the console reports is not measured; the host answers 0 in both modes ([The graphics scale does not follow the operation mode](../../../docs/reference/services.md#the-graphics-scale-does-not-follow-the-operation-mode)). The game takes it as its display manager's scale index: its frame buffer is the display's size times {1.0, 0.8, 2/3}[tier] (`main+0x30f840`), and a renderer parameter of 0, 9 or 13 that its dynamic resolution adds to its GPU time (`hle_oe.c`). Measured docked in the mining tunnel (M6, 16 GB, a window, fresh save, a host of chain `cfcc3a82c78b` with only the answer changed): with tier 1 the frame buffer was 2304x1296 and the main passes 1676x943, GPU p50 6.8-7.2 ms; with tier 0 2880x1620 and 2095x1179, GPU p50 8.3-8.9 ms; 60 fps with both (frame p50 16.6-16.7 ms). Not measured: the busy views below, where the larger picture leaves less room. - The 9 GB pool. - The terrain's stores through loaded pointers keep reaching memory pools the host learns only afterwards (12 in a 17,700-frame session); each such miss is checked against the uniform shadows that relied on it, and none so far covered a written page ([Global memory at an address loaded from memory](../../../docs/reference/shaders.md#global-memory-at-an-address-loaded-from-memory), Open). -- **Frame rate in busy scenes:** the game picks its own present interval: 3 (20 fps) in its cutscenes, and 1 ↔ 2 (60 ↔ 30 fps) by its frame-rate controller (`main+0x204a18`; settings at `0x703f72b200`: interval 1 to 3, margin -0.2, 2 late frames to go up, 6 to come back). It measures each frame from its start (`+0xf0`) to the later of its submission and the GPU's end (`+0x128`, ticks) and wants interval 2 from 1.2 vsyncs (20 ms). On the Mac (M4 Pro) that measure sits at 17 ms a frame (p90 24): the host's translation (CPU, 5-9 ms) before the GPU's 10-13 ms. The upscaler's HMMA through `simdgroup_matrix` ([Ampere HMMA through simdgroup_matrix](../../../docs/reference/shaders.md#ampere-hmma-through-simdgroup_matrix)) cut the switches by about a fifth; the geometry runs' dependency check, quadratic in the run's length ([Geometry dependency checks skip read-only pairs](../../../docs/reference/shaders.md#geometry-dependency-checks-skip-read-only-pairs)), was about 8 % of the submitting thread, and making it linear cut them from 30 to 12 on the same scripted route. Entering a level stalled for 1.3 s on GPU drains, one or two per geometry draw ([CPU reads after a drain wait only for later writes](../../../docs/reference/rendering.md#cpu-reads-after-a-drain-wait-only-for-later-writes)); one CPU-bound build frame of 195-280 ms remains. Measured without `sample` attached: it suspends the process and makes the submitting thread look stalled. Its geometry draws are mesh draws ([Mesh geometry groups sized to the vertex output](../../../docs/reference/shaders.md#mesh-geometry-groups-sized-to-the-vertex-output)): translation 9.9 → 7.2 ms a frame, GPU p50 11.8 → 9.0 ms on the scripted route. Open: the rest of the translation time in the busiest views (argument encoding, render pass setup), where translation and the GPU together still miss a vsync now and then. +- **Frame rate in busy scenes:** the game picks its own present interval: 3 (20 fps) in its cutscenes, and 1 ↔ 2 (60 ↔ 30 fps) by its frame-rate controller (`main+0x204a18`; settings at `0x703f72b200`: interval 1 to 3, margin -0.2, 2 late frames to go up, 6 to come back). It measures each frame from its start (`+0xf0`) to the later of its submission and the GPU's end (`+0x128`, ticks) and wants interval 2 from 1.2 vsyncs (20 ms). On the Mac (M4 Pro) that measure sits at 17 ms a frame (p90 24): the host's translation (CPU, 5-9 ms) before the GPU's 10-13 ms. The upscaler's HMMA through `simdgroup_matrix` ([Ampere HMMA through simdgroup_matrix](../../../docs/reference/shaders.md#ampere-hmma-through-simdgroup_matrix)) cut the switches by about a fifth; the geometry runs' dependency check, quadratic in the run's length ([Geometry dependency checks skip read-only pairs](../../../docs/reference/shaders.md#geometry-dependency-checks-skip-read-only-pairs)), was about 8 % of the submitting thread, and making it linear cut them from 30 to 12 on the same scripted route. Entering a level stalled for 1.3 s on GPU drains, one or two per geometry draw ([CPU reads after a drain wait only for later writes](../../../docs/reference/rendering.md#cpu-reads-after-a-drain-wait-only-for-later-writes)); one CPU-bound build frame of 195-280 ms remains. Measured without `sample` attached: it suspends the process and makes the submitting thread look stalled. Its geometry draws are mesh draws ([Mesh geometry groups sized to the vertex output](../../../docs/reference/shaders.md#mesh-geometry-groups-sized-to-the-vertex-output)): translation 9.9 → 7.2 ms a frame, GPU p50 11.8 → 9.0 ms on the scripted route. Open: the rest of the translation time in the busiest views (argument encoding, render pass setup), where translation and the GPU together still miss a vsync now and then. Once at interval 2 the game stayed there for tens of seconds on main (2026-11-30, `e693bb89`), because macOS lowers the GPU's clock at 30 fps: about 5,000 draws took 12 ms of GPU at 60 fps and 20.4-22.4 ms at 30, over the controller's 20 ms, until a lighter view let it back to 1 ([A game's own 30 fps fallback held by the GPU clock its longer hold lowered](../../../docs/reference/frame-rate.md#a-games-own-30-fps-fallback-held-by-the-gpu-clock-its-longer-hold-lowered); the GPU clock keeper: in 4 sessions with it on an M3 Max MacBook Pro, 23 probes, all won within 0.3-0.7 s, no window at 30 fps; without it, two windows at 30 fps with GPU p50 24.6-25.3 ms). - **Submission-copy qualification ([PR #2677](https://github.com/seedds/nxnative/pull/2677), 2026-11-17, open):** against main `d6585a2d`, on the M4 Pro (14 CPU cores, 20 GPU cores, 48 GB, macOS 27.2.1), the mining-entrance overlook stays capped: 59.6 fps, frame p99 16.88 ms, copy/replay CPU 7.75 ms a frame and GPU p50 7.59 ms, with no pipeline or library creation in the 100-130 s window. Headless, handheld mode and MetalFX, the packed v458752 build's derived shader profile, identical save and archive copies, no movement or stack sampling. This is not a CPU-limited route. The planned main/submission-copy-only ABBA comparison was stopped when the first candidate process created four pipeline objects in its window; a single repeat from the combined warmed archive, at 150-180 s, stopped on main creating one. Those comparisons are excluded: no valid submission-copy performance result was obtained for this main revision, and the change should remain held. The separate sampler was not applied. Baseline limitation: before the window, main reports 16 zero-GPU-address diagnostics for geometry shader `5d537f7630ec3671_bl3`, stage 2 argument block slots 2/3/4/7; the scene renders. Those four unused mesh scratch bindings are now explained by [the mesh address diagnostic correction](../../../docs/reference/rendering.md#mesh-geometry-reports-unused-compute-scratch-as-zero-gpu-addresses); the submission-copy performance result remains open. - **Frame drops in a 63-minute Mac session (M5 Pro, 2026-11-08):** 40 drops under 54 fps, 126 s in all, every one in scenes of more than 12,000 draws, with the GPU at its top clock, no thermal or droop limit and the hold at interval 1 (the controller cut of [PR #1765](https://github.com/seedds/nxnative/pull/1765)). - The worst (14-23 fps for 26 s, and around 1,400 s): 1,700-2,800 geometry mesh draws a frame, none in runs, refused because their fragment shader `7f7781d3621f0ce6` (`SilverStarMaterial`: two uniform buffers, the support buffer, a discard) has resources. Runs now take fragment shaders that write no memory ([Mesh geometry draws gathered into one dispatch](../../../docs/reference/shaders.md#mesh-geometry-draws-gathered-into-one-dispatch)). With it and the `b64d15d642f9d2d6` pair below, a 113,000-frame session held 59.4-60 fps in every 1,500-frame window (M5 Pro, 2026-11-09, [PR #2641](https://github.com/seedds/nxnative/pull/2641)), and panning the map of Sublayer 900 went from 16-26 to 43-48 fps (submission 30-45 → 11-12 ms) with the map's pair. Not measured here: on the M4 Pro (2026-11-15) the opening, the mining tunnel and the area after it draw no geometry draw whose fragment shader has resources (main's rule refused none), so both rules give the same frames there (submission p50 6.2-6.4 ms, GPU 11.8-12.2 ms, every mesh draw in one run a frame). Open: a session in those areas with the `geometry mesh draws` line (draws in runs, runs ended by `a fragment binding change`), to see that the draws join runs, and whether a pass with colour attached keeps some single. @@ -111,6 +111,7 @@ Each step below stopped the boot before its fix: ## Found on this game +- [A game's own 30 fps fallback held by the GPU clock its longer hold lowered](../../../docs/reference/frame-rate.md#a-games-own-30-fps-fallback-held-by-the-gpu-clock-its-longer-hold-lowered): its controller's interval 2 kept for tens of seconds, GPU p50 12 ms at 60 fps against 20.4-22.4 ms at 30 for the same draws; with the keeper every drop went back to 60 within 0.7 s (23 of 23, M3 Max MacBook Pro). - [The prebuild dropped a finished period behind one slow compile](../../../docs/reference/rendering.md#the-prebuild-dropped-a-finished-period-behind-one-slow-compile): resolved, a prebuild lost a finished period behind a slow compute compile (and with the timer fix in [PR #3491](https://github.com/seedds/nxnative/pull/3491), first-start prepare/first-frame dropped from 123.66 → 1.04 s / 145.8 → 5.4 s on M3 Max against `25c5566a`). - [A replay without the live run's Switch 2 state](../../../docs/reference/nvn-objects.md#a-replay-without-the-live-runs-switch-2-state) and [A texture the GPU's copies filled that the capture did not read](../../../docs/reference/nvn-objects.md#a-texture-the-gpus-copies-filled-that-the-capture-did-not-read): Switch 2 capture state (`sdk_ounce`, `nvn_timestamp_ns`) and ASTC readback ([PR #1236](https://github.com/seedds/nxnative/pull/1236)). - [HDR colors more saturated than Off, and Off after HDR](../../../docs/reference/rendering.md#hdr-colors-more-saturated-than-off-and-off-after-hdr): its mode menu's PQ window has colors outside BT.709 (77 % of its lit pixels moved by a ΔE_ITP above 10 when clipped at 0), which Game's HDR clipped. Resolved. diff --git a/dm/gfx/nvn/nvn_gpu_keep.h b/dm/gfx/nvn/nvn_gpu_keep.h new file mode 100644 index 0000000000..67c3392a6d --- /dev/null +++ b/dm/gfx/nvn/nvn_gpu_keep.h @@ -0,0 +1,71 @@ +// The GPU clock keeper's decisions (nvn_mtl.m gpu_keep_main runs them; t_gpu_keep tests them). Pure: no Metal, the caller passes the clock. +// +// macOS sets the GPU's clock from how busy the GPU was. When a game holds its frames for two vsyncs instead of one, the GPU idles half of each frame and the clock falls, so the +// same frame takes longer on the GPU: Donkey Kong Bananza at about 5,000 draws took 12 ms of GPU a frame at 60 fps and 20.4-22.4 ms at 30 fps (M-series Mac, main at e693bb89). +// A frame-rate controller that steps back to the shorter hold only once a frame fits it (Bananza's wants its frame under 1.2 vsyncs) then never does: at the longer hold the +// frames stay slow because the hold is longer. The Switch's GPU clock does not follow the load, so the game's rule is right there and only wrong here. +// +// The keeper breaks that loop with a probe: when the frames are held longer than the shortest hold seen and a frame's GPU time is a large part of that shortest period, it keeps +// the GPU busy for up to GPU_KEEP_PROBE_NS (the caller spins a small kernel while the game's queue is idle), so the clock rises and the game can see its frames fit. It stops as +// soon as the hold shortens (won), and gives up when it does not (lost): the scene really needs the longer hold, or something other than the GPU holds it. Each loss in a row +// doubles the wait before the next try, and after GPU_KEEP_TRIES losses the keeper waits until the hold shortens by itself. At the shortest hold it never runs, so a game at its +// full frame rate, a game whose only hold is the longer one, and a game whose GPU is light at the longer hold cost nothing. The caller also stops it (allowed = 0) when the window +// is not focused or is covered, while nothing is presented, while presents are discarded for a suspend, at a serious thermal state and in Low Power Mode. +#pragma once +#include + +#define GPU_KEEP_LONGER 1.5 // a present period above this many times the shortest one seen is a longer hold (2 vsyncs against 1, 3 against 2) +#define GPU_KEEP_FIT 0.6 // a frame's GPU time above this share of the shortest period: the GPU's clock may be what keeps the longer hold +#define GPU_KEEP_PROBE_NS 4000000000ull // macOS raises the clock over seconds; Bananza's controller then needs 6 frames that fit +#define GPU_KEEP_COOL_NS 8000000000ull // the wait after the first loss, doubled after each further one +#define GPU_KEEP_TRIES 4 // losses in a row before waiting for the hold to shorten by itself + +enum { GPU_KEEP_IDLE, GPU_KEEP_PROBE, GPU_KEEP_COOL, GPU_KEEP_DONE }; +enum { GPU_KEEP_EV_NONE, GPU_KEEP_EV_START, GPU_KEEP_EV_WON, GPU_KEEP_EV_LOST, GPU_KEEP_EV_STOPPED }; + +typedef struct { + double p_min; // the shortest present period seen, in seconds (0: none yet) + double period; // the latest present's period, in seconds + int state; // GPU_KEEP_* + int losses; // probes lost in a row in this stretch of longer holds + uint64_t t_state; // when state was entered (the caller's clock, ns) + uint64_t cool_ns; // the current wait in GPU_KEEP_COOL + uint32_t probes, won, lost; +} nvn_gpu_keep; + +// Each present's period (the minimum time it is shown; 0 = unlimited, not a hold) +static inline void nvn_gpu_keep_period(nvn_gpu_keep *k, double period) { + if (!(period > 0)) return; + k->period = period; + if (!(k->p_min > 0) || period < k->p_min) k->p_min = period; +} +static inline int nvn_gpu_keep_longer(const nvn_gpu_keep *k) { return k->p_min > 0 && k->period > k->p_min * GPU_KEEP_LONGER; } +static inline int nvn_gpu_keep_spin(const nvn_gpu_keep *k) { return k->state == GPU_KEEP_PROBE; } + +// One step: now (ns), gpu_ms a frame's recent GPU time (< 0: not known), allowed (see above). Returns the event, GPU_KEEP_EV_*. +static inline int nvn_gpu_keep_step(nvn_gpu_keep *k, uint64_t now, double gpu_ms, int allowed) { + int longer = nvn_gpu_keep_longer(k); + if (!longer) { // back at the shortest hold: a probe won, and the next stretch of longer holds starts with no losses + int ev = k->state == GPU_KEEP_PROBE ? GPU_KEEP_EV_WON : GPU_KEEP_EV_NONE; + if (ev) k->won++; + k->state = GPU_KEEP_IDLE; k->losses = 0; k->cool_ns = 0; + return ev; + } + switch (k->state) { + case GPU_KEEP_IDLE: + if (allowed && gpu_ms > GPU_KEEP_FIT * k->p_min * 1e3) { k->state = GPU_KEEP_PROBE; k->t_state = now; k->probes++; return GPU_KEEP_EV_START; } + return GPU_KEEP_EV_NONE; + case GPU_KEEP_PROBE: + if (!allowed) { k->state = GPU_KEEP_IDLE; return GPU_KEEP_EV_STOPPED; } // not a loss: the probe did not get its time + if (now - k->t_state < GPU_KEEP_PROBE_NS) return GPU_KEEP_EV_NONE; + k->lost++; k->losses++; k->t_state = now; + k->cool_ns = k->cool_ns ? k->cool_ns * 2 : GPU_KEEP_COOL_NS; + k->state = k->losses >= GPU_KEEP_TRIES ? GPU_KEEP_DONE : GPU_KEEP_COOL; + return GPU_KEEP_EV_LOST; + case GPU_KEEP_COOL: + if (now - k->t_state >= k->cool_ns) k->state = GPU_KEEP_IDLE; + return GPU_KEEP_EV_NONE; + default: // GPU_KEEP_DONE: until the hold shortens + return GPU_KEEP_EV_NONE; + } +} diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index bbb4444e45..cd09c0daa0 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -43,6 +43,7 @@ #include "nvn_primitive.h" #include "nvn_link_drain.h" #include "nvn_link_latch.h" +#include "nvn_gpu_keep.h" #include "nvn_present_history.h" #include "nvn_timestamp.h" #include "nvn_pso_ds.h" @@ -107,6 +108,7 @@ static inline uint64_t tex_slot_h(const uint64_t *tex, const uint64_t *img, unsi #endif static id g_dev; +static void gpu_keep_start(void), gpu_keep_note(double period); // the GPU clock keeper (after mtl_present_discard) static void ser_compile_begin(void), ser_compile_end(void), plain_compile_begin(void), plain_compile_end(void); static void arch_died(NSError *e); // Every compile reports service death before leaving the serialization gate, even when its caller ignores the error. Archive lookups do not compile. @@ -1345,6 +1347,7 @@ static void mtl_init_once(void) { MLOG("Metal device %s", g_dev.name.UTF8String); rop_f16_probe(); udc_selftest(); + gpu_keep_start(); } void mtl_init(void) { @@ -26993,6 +26996,7 @@ static int present_now(id src, uint64_t serial, double period, uint6 t_present_origin_ll = (flags & PRESENT_ORIGIN_LL) != 0; if (g_present_stall_ns) { struct timespec ts = {0, (long)g_present_stall_ns}; nanosleep(&ts, NULL); } g_present_n++; + gpu_keep_note(period); if (bootprof() && (g_present_n == 1 || g_present_n % 120 == 0)) { if (g_present_n == 1) g_bp_first_present = bp_now() - g_bp_t0; MLOG("[bootprof] t=%.3f s present #%llu: metallibs loaded %llu / %.1f ms; pipelines created %llu / %.1f ms (all synchronous on the submit thread); prewarmed and used: shaders %llu / render pipelines %llu / compute pipelines %llu; " @@ -27631,6 +27635,121 @@ static void present_suspend_wait(void) { // When turned on, also clear the frames already queued: the system first stops the display-link callback and only afterwards notifies about going to the background, and in that gap the game has already filled the swapchain and is stuck at "before acquiring the next texture, wait for its previous present to be processed" // (hle_nvn.c nvnWindowAcquireTexture -> mtl_present_wait). Without clearing, the game cannot advance a single tick during those 2.5 seconds of waiting for the save, and the save is not written (the same pattern three times in player logs: 3 frames queued, 0 submits) void mtl_present_discard(int on) { atomic_store(&g_present_discard, on != 0); if (on) present_drop_for_suspend(); } + +// ---------- GPU clock keeper (Mac) ---------- +// macOS lowers the GPU's clock when frames held for two vsyncs leave it idle half the time, and a game's frame-rate controller that waits for its frames to fit one vsync again +// then stays at 30 fps (nvn_gpu_keep.h has the case and the decisions; Donkey Kong Bananza). The host already declares the shorter deadline to the CPU scheduler for the same +// reason (hle_nvn.c rt_period_ns); this is the GPU's side. A thread of its own sleeps on g_gk_cv while the frames are at their shortest hold. While a probe runs it keeps one +// threadgroup of 32 threads spinning on a command queue of its own, only while the game's queue has nothing in flight (rx_inflight), each spin about 0.8 ms at the top clock, +// so the GPU never idles and its clock rises, and the spin never stands in front of a frame for long (PR #1847's first form spun 3-4 ms whatever was in flight). Every GK_TICK_NS +// it checks whether it may run (gk_allowed): the window focused (dm_focus_now 1: not out of focus, minimized or suspended) and not covered, a present in the last GK_PRESENT_NS, +// presents not discarded for a suspend, thermal state under serious, Low Power Mode off. At quit (atexit) it is stopped and waited for. DM_GPU_KEEP=0 turns it off +enum { GK_SPIN = 35000, GK_LINES = 40 }; +#define GK_TICK_NS 50000000ull +#define GK_PRESENT_NS 250000000ull +static nvn_gpu_keep g_gk; // under g_gk_mu +static pthread_mutex_t g_gk_mu = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t g_gk_cv = PTHREAD_COND_INITIALIZER; +static _Atomic int g_gk_on, g_gk_stop; +static _Atomic uint64_t g_gk_present_ns; +static dispatch_semaphore_t g_gk_done; +static void gpu_keep_note(double period) { // present_now, each present + if (!atomic_load_explicit(&g_gk_on, memory_order_relaxed)) return; + atomic_store_explicit(&g_gk_present_ns, now_ns(), memory_order_relaxed); + pthread_mutex_lock(&g_gk_mu); + int was = nvn_gpu_keep_longer(&g_gk); nvn_gpu_keep_period(&g_gk, period); int wake = !was && nvn_gpu_keep_longer(&g_gk); + pthread_mutex_unlock(&g_gk_mu); + if (wake) pthread_cond_signal(&g_gk_cv); +} +static const char *gk_allowed(uint64_t now) { // NULL when the keeper may run, else why not + if (dm_focus_now() != 1) return "the window is not focused"; + if (front_window_covered()) return "the window is covered"; + if (atomic_load(&g_present_discard)) return "the app is being suspended"; + uint64_t p = atomic_load_explicit(&g_gk_present_ns, memory_order_relaxed); // a present may stamp a time after now was read + if (p < now && now - p > GK_PRESENT_NS) return "nothing is presented"; + NSProcessInfo *pi = NSProcessInfo.processInfo; + if (pi.thermalState >= NSProcessInfoThermalStateSerious) return "the thermal state is serious"; + if (@available(macOS 12.0, iOS 15.0, *)) { if (pi.lowPowerModeEnabled) return "Low Power Mode is on"; } + return NULL; +} +static double gk_gpu_ms(void) { // a frame's GPU time over the last 30 frames, 8 frames back as the timing line takes it (< 0: not known) + uint64_t f = atomic_load(&g_cur_frame); double sum = 0; int n = 0; + for (uint64_t d = 8; d < 38 && f > d; d++) { double g = mtl_gpu_ms(f - d); if (g >= 0) { sum += g; n++; } } + return n >= 4 ? sum / n : -1; +} +static void gk_log(int ev, const nvn_gpu_keep *k, double gpu, const char *why, double spun_ms, double probe_s) { + static unsigned lines; + if (ev == GPU_KEEP_EV_NONE || (lines++ >= GK_LINES && (k->probes & (k->probes - 1)))) return; // the first GK_LINES events, then at probes 64, 128, ... + double f = k->period * 1e3, s = k->p_min * 1e3; + if (ev == GPU_KEEP_EV_START) MLOG("GPU clock keeper: probe %u: frames held %.1f ms against %.1f ms at the shortest, a frame's GPU time %.1f ms; keeping the GPU busy", k->probes, f, s, gpu); + else if (ev == GPU_KEEP_EV_WON) MLOG("GPU clock keeper: probe %u won: frames back to %.1f ms after %.1f s, %.0f ms of spinning (%u won, %u lost so far)", k->probes, f, probe_s, spun_ms, k->won, k->lost); + else if (ev == GPU_KEEP_EV_LOST) MLOG("GPU clock keeper: probe %u lost: frames still held %.1f ms, a frame's GPU time %.1f ms, %.0f ms of spinning; %s (%u won, %u lost so far)", k->probes, f, gpu, spun_ms, + k->state == GPU_KEEP_DONE ? "no more tries until the hold shortens" : "next try after a wait", k->won, k->lost); + else MLOG("GPU clock keeper: probe %u stopped: %s", k->probes, why ? why : "?"); +} +static void *gpu_keep_main(void *arg) { + (void)arg; pthread_setname_np("dm-gpu-keep"); + NSError *e = nil; + id lib = [g_dev newLibraryWithSource:@"#include \nusing namespace metal;\nkernel void dm_keep(device float *o [[buffer(0)]], constant uint &n [[buffer(1)]], uint t [[thread_position_in_grid]]) { float x = t; for (uint i = 0; i < n; i++) x = fma(x, 1.0000001f, 0.5f); if (x == 12345.f) o[t] = x; }\n" options:nil error:&e]; + id fn = lib ? [lib newFunctionWithName:@"dm_keep"] : nil; + id ps = fn ? [g_dev newComputePipelineStateWithFunction:fn error:&e] : nil; + id q = [g_dev newCommandQueue]; id o = [g_dev newBufferWithLength:4096 options:MTLResourceStorageModePrivate]; + if (!ps || !q || !o) { + atomic_store(&g_gk_on, 0); MLOG("[RED] GPU clock keeper: cannot make its kernel (%s); off", e ? e.localizedDescription.UTF8String : "no error"); + dispatch_semaphore_signal(g_gk_done); return NULL; + } + uint32_t n = GK_SPIN; uint64_t next = 0, probe_t0 = 0, spun_ns = 0; int spin = 0; + while (!atomic_load(&g_gk_stop)) { @autoreleasepool { + uint64_t now = now_ns(); + if (now >= next) { + const char *why = gk_allowed(now); double gpu = gk_gpu_ms(); + pthread_mutex_lock(&g_gk_mu); + int ev = nvn_gpu_keep_step(&g_gk, now, gpu, !why); nvn_gpu_keep k = g_gk; + spin = nvn_gpu_keep_spin(&g_gk); + pthread_mutex_unlock(&g_gk_mu); + if (ev == GPU_KEEP_EV_START) { probe_t0 = now; spun_ns = 0; } + gk_log(ev, &k, gpu, why, spun_ns / 1e6, (now - probe_t0) / 1e9); // before any sleep below: a won probe's line waited for the next longer hold, never written when none came + if (!spin) { // no probe: asleep at the shortest hold until a longer one, else until the next check (both looked at again under the lock: gpu_keep_note signals under it) + pthread_mutex_lock(&g_gk_mu); + if (atomic_load(&g_gk_stop) || nvn_gpu_keep_spin(&g_gk)) {} + else if (!nvn_gpu_keep_longer(&g_gk)) pthread_cond_wait(&g_gk_cv, &g_gk_mu); + else { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); uint64_t w = (uint64_t)ts.tv_nsec + GK_TICK_NS * 4; ts.tv_sec += (time_t)(w / 1000000000ull); ts.tv_nsec = (long)(w % 1000000000ull); pthread_cond_timedwait(&g_gk_cv, &g_gk_mu, &ts); } + pthread_mutex_unlock(&g_gk_mu); + } + next = spin ? now + GK_TICK_NS : 0; // after a sleep, check again at once + continue; + } + if (!spin) continue; + if (rx_inflight() > 0) { usleep(500); continue; } // the game's work keeps the GPU busy itself + id cb = [q commandBuffer]; id ce = [cb computeCommandEncoder]; + [ce setComputePipelineState:ps]; [ce setBuffer:o offset:0 atIndex:0]; [ce setBytes:&n length:4 atIndex:1]; + [ce dispatchThreadgroups:MTLSizeMake(1, 1, 1) threadsPerThreadgroup:MTLSizeMake(32, 1, 1)]; [ce endEncoding]; + uint64_t t0 = now_ns(); [cb commit]; [cb waitUntilCompleted]; spun_ns += now_ns() - t0; + } } + dispatch_semaphore_signal(g_gk_done); + return NULL; +} +static void gpu_keep_quit(void) { // atexit: no spin left running or committed as the process goes + if (!atomic_load(&g_gk_on)) return; + atomic_store(&g_gk_stop, 1); + pthread_mutex_lock(&g_gk_mu); pthread_cond_broadcast(&g_gk_cv); pthread_mutex_unlock(&g_gk_mu); + if (dispatch_semaphore_wait(g_gk_done, dispatch_time(DISPATCH_TIME_NOW, 200 * NSEC_PER_MSEC))) MLOG("GPU clock keeper: its thread did not stop within 200 ms at quit"); +} +static void gpu_keep_start(void) { // mtl_init_once, after the device +#if TARGET_OS_OSX + const char *e = getenv("DM_GPU_KEEP"); + if (e && !strcmp(e, "0")) { MLOG("GPU clock keeper: off (DM_GPU_KEEP=0)"); return; } + g_gk_done = dispatch_semaphore_create(0); + atomic_store(&g_gk_on, 1); + pthread_t th; + if (pthread_create(&th, NULL, gpu_keep_main, NULL)) { atomic_store(&g_gk_on, 0); MLOG("[RED] GPU clock keeper: cannot start its thread; off"); return; } + pthread_detach(th); + atexit(gpu_keep_quit); + MLOG("GPU clock keeper: on; it keeps the GPU busy for a few seconds when frames are held longer than the shortest hold and their GPU time may not fit it at the lowered clock (DM_GPU_KEEP=0 turns it off)"); +#else + (void)gpu_keep_main; (void)gpu_keep_quit; // the Mac's only: an iPhone's heat and battery were not measured with it +#endif +} static uint64_t present_enqueue(const tex_t *t, double period, const int crop[4], uint64_t ev_val, unsigned flags, uint64_t *copy); uint64_t mtl_present(const tex_t *t, double period, const int crop[4], unsigned flags, uint64_t *copy) { return present_enqueue(t, period, crop, 0, flags, copy); } void *mtl_device(void) { mtl_init(); return (__bridge void *)g_dev; } diff --git a/dm/gfx/tests/Makefile b/dm/gfx/tests/Makefile index 1ec0437099..1c949c655e 100644 --- a/dm/gfx/tests/Makefile +++ b/dm/gfx/tests/Makefile @@ -190,6 +190,8 @@ t_link_latch_cb: t_link_latch_cb.m $(MTL_T_DEP) clang $(CFLAGS) -fobjc-arc -o $@ t_link_latch_cb.m $(MTL_T_SRC) $(FW) t_link_latch: t_link_latch.c ../nvn/nvn_link_latch.h clang -O1 -Wall -o $@ t_link_latch.c +t_gpu_keep: t_gpu_keep.c ../nvn/nvn_gpu_keep.h + clang -O1 -Wall -o $@ t_gpu_keep.c t_present_history: t_present_history.c ../nvn/nvn_present_history.h clang -O2 -Wall -Wextra -Werror -o $@ t_present_history.c t_present_history_tsan: t_present_history.c ../nvn/nvn_present_history.h diff --git a/dm/gfx/tests/t_gpu_keep.c b/dm/gfx/tests/t_gpu_keep.c new file mode 100644 index 0000000000..b820290ed7 --- /dev/null +++ b/dm/gfx/tests/t_gpu_keep.c @@ -0,0 +1,78 @@ +// Unit test of the GPU clock keeper's decisions (../nvn/nvn_gpu_keep.h). Pure: no Metal, no clock. +#include "../nvn/nvn_gpu_keep.h" +#include + +static int checks, failed; +#define CHECK(c, label) do { checks++; if (!(c)) { failed++; printf("FAIL: %s\n", label); } } while (0) + +static const double T1 = 1.0 / 60, T2 = 2.0 / 60, T3 = 3.0 / 60; +static const uint64_t S = 1000000000ull; + +int main(void) { + nvn_gpu_keep k = {0}; + CHECK(!nvn_gpu_keep_longer(&k) && nvn_gpu_keep_step(&k, 0, 21, 1) == GPU_KEEP_EV_NONE, "no present yet: nothing"); + nvn_gpu_keep_period(&k, 0); + CHECK(k.p_min == 0, "period 0 (unlimited) is not a hold"); + + // A game that only ever holds two vsyncs (a 30 fps game): its longer hold is its shortest, never probed + nvn_gpu_keep_period(&k, T2); + CHECK(nvn_gpu_keep_step(&k, 1 * S, 30, 1) == GPU_KEEP_EV_NONE && !nvn_gpu_keep_spin(&k), "a game whose only hold is two vsyncs: never probed, however heavy"); + + // Bananza: starts at interval 2, sets 1, then its controller drops to 2 in a heavy scene + k = (nvn_gpu_keep){0}; + nvn_gpu_keep_period(&k, T2); nvn_gpu_keep_period(&k, T1); + CHECK(nvn_gpu_keep_step(&k, 1 * S, 12, 1) == GPU_KEEP_EV_NONE && !nvn_gpu_keep_spin(&k), "at the shortest hold: never spins (60 fps costs nothing)"); + nvn_gpu_keep_period(&k, T2); + CHECK(nvn_gpu_keep_longer(&k), "interval 2 after interval 1 is a longer hold"); + CHECK(nvn_gpu_keep_step(&k, 2 * S, 8, 1) == GPU_KEEP_EV_NONE, "a light GPU at the longer hold (8 ms, under 0.6 of 16.7): not the clock, no probe"); + CHECK(nvn_gpu_keep_step(&k, 2 * S, 21, 0) == GPU_KEEP_EV_NONE, "not allowed (covered, unfocused, hot): no probe"); + CHECK(nvn_gpu_keep_step(&k, 2 * S, -1, 1) == GPU_KEEP_EV_NONE, "GPU time not known: no probe"); + CHECK(nvn_gpu_keep_step(&k, 2 * S, 21, 1) == GPU_KEEP_EV_START && nvn_gpu_keep_spin(&k) && k.probes == 1, "21 ms of GPU at interval 2: a probe starts and spins"); + CHECK(nvn_gpu_keep_step(&k, 3 * S, 14, 1) == GPU_KEEP_EV_NONE && nvn_gpu_keep_spin(&k), "within the probe time: keeps spinning"); + nvn_gpu_keep_period(&k, T1); + CHECK(nvn_gpu_keep_step(&k, 3 * S + 1, 12, 1) == GPU_KEEP_EV_WON && !nvn_gpu_keep_spin(&k) && k.won == 1, "the game goes back to interval 1: won, stops at once"); + + // A probe stopped by the window going away is not a loss + nvn_gpu_keep_period(&k, T2); + CHECK(nvn_gpu_keep_step(&k, 10 * S, 21, 1) == GPU_KEEP_EV_START, "a second drop: probe"); + CHECK(nvn_gpu_keep_step(&k, 11 * S, 21, 0) == GPU_KEEP_EV_STOPPED && !nvn_gpu_keep_spin(&k) && k.lost == 0, "the window goes away: stopped, not lost"); + CHECK(nvn_gpu_keep_step(&k, 12 * S, 21, 1) == GPU_KEEP_EV_START, "and probes again once allowed"); + + // A scene that really needs the longer hold: losses back off, then the keeper waits for the hold to shorten by itself + uint64_t t = 12 * S; + t += GPU_KEEP_PROBE_NS; + CHECK(nvn_gpu_keep_step(&k, t, 21, 1) == GPU_KEEP_EV_LOST && !nvn_gpu_keep_spin(&k) && k.cool_ns == GPU_KEEP_COOL_NS, "no shorter hold after the probe time: lost, first wait"); + CHECK(nvn_gpu_keep_step(&k, t + GPU_KEEP_COOL_NS - 1, 21, 1) == GPU_KEEP_EV_NONE && !nvn_gpu_keep_spin(&k), "within the wait: no probe"); + t += GPU_KEEP_COOL_NS; + CHECK(nvn_gpu_keep_step(&k, t, 21, 1) == GPU_KEEP_EV_NONE && k.state == GPU_KEEP_IDLE, "the wait ends"); + int lost = 1; uint64_t cool = GPU_KEEP_COOL_NS; + for (int i = 1; i < GPU_KEEP_TRIES; i++) { + CHECK(nvn_gpu_keep_step(&k, t, 21, 1) == GPU_KEEP_EV_START, "the next try"); + t += GPU_KEEP_PROBE_NS; + lost += nvn_gpu_keep_step(&k, t, 21, 1) == GPU_KEEP_EV_LOST; + cool *= 2; + if (i < GPU_KEEP_TRIES - 1) { CHECK(k.cool_ns == cool, "each loss doubles the wait"); t += k.cool_ns; nvn_gpu_keep_step(&k, t, 21, 1); } + } + CHECK(lost == GPU_KEEP_TRIES && k.state == GPU_KEEP_DONE, "GPU_KEEP_TRIES losses in a row: done"); + CHECK(nvn_gpu_keep_step(&k, t + 1000 * S, 21, 1) == GPU_KEEP_EV_NONE && !nvn_gpu_keep_spin(&k), "done: no probe however long the longer hold lasts"); + nvn_gpu_keep_period(&k, T1); + CHECK(nvn_gpu_keep_step(&k, t + 1001 * S, 12, 1) == GPU_KEEP_EV_NONE && k.state == GPU_KEEP_IDLE && k.losses == 0 && k.cool_ns == 0, "the hold shortens by itself: a fresh start, not counted as won"); + nvn_gpu_keep_period(&k, T2); + CHECK(nvn_gpu_keep_step(&k, t + 1002 * S, 21, 1) == GPU_KEEP_EV_START, "the next drop probes again"); + + // Cutscenes at interval 3: a probe while held longer, still a probe at interval 2 (not the shortest) + k = (nvn_gpu_keep){0}; + nvn_gpu_keep_period(&k, T1); nvn_gpu_keep_period(&k, T3); + CHECK(nvn_gpu_keep_step(&k, 1 * S, 9, 1) == GPU_KEEP_EV_NONE, "a light cutscene at interval 3: no probe"); + CHECK(nvn_gpu_keep_step(&k, 1 * S, 20, 1) == GPU_KEEP_EV_START, "a heavy one: probe"); + nvn_gpu_keep_period(&k, T2); + CHECK(nvn_gpu_keep_step(&k, 2 * S, 20, 1) == GPU_KEEP_EV_NONE && nvn_gpu_keep_spin(&k), "3 -> 2 is still longer than the shortest: the probe goes on"); + + // 120 Hz: a game at 120 fps (period 1/120) dropped to 60 is a longer hold too + k = (nvn_gpu_keep){0}; + nvn_gpu_keep_period(&k, 1.0 / 120); nvn_gpu_keep_period(&k, T1); + CHECK(nvn_gpu_keep_longer(&k) && nvn_gpu_keep_step(&k, 1 * S, 7, 1) == GPU_KEEP_EV_START, "120 -> 60 fps with 7 ms of GPU (over 0.6 of 8.3 ms): probe"); + + printf("t_gpu_keep: %d checks, %d failed\n", checks, failed); + return failed != 0; +} diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 7de3330d78..64a8dccb29 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -831,6 +831,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - virtual ticks keep the average, but a frame still stays up two or three refreshes in turn (13.9 / 20.8 ms at 144 Hz), and Apple's HUD showed the frame interval jumping between them. → [Fullscreen switches the screen to a rate the frame rate divides](../reference/frame-rate.md#fullscreen-switches-the-screen-to-a-rate-the-frame-rate-divides) - in fullscreen on a variable-refresh screen, some starts ran cleanly while others had the display link go silent for 100–500 ms two or three times, enter rescue, and run at 20–30 fps for a minute depending on when the mode switch landed. → [The display link started before fullscreen's mode switch](../reference/frame-rate.md#the-display-link-started-before-fullscreens-mode-switch) - a Mac game in fullscreen holds 60 fps for 45 s to 3 minutes, then the same work takes about twice the GPU time (Tears of the Kingdom 10.5 → 19.7 ms) and the frame step falls to 30 fps. `powermetrics` shows the GPU at its lowest clock (338 MHz on an M4 Pro) while it is 72–98% busy, with thermal pressure nominal. Probes back to 60 fail, because the clock does not rise within a probe. `gamepolicyd` logs `Game mode enabled` and `Set DPS to 2`. → [macOS Game Mode holds the GPU at its lowest clock](../reference/frame-rate.md#macos-game-mode-holds-the-gpu-at-its-lowest-clock) +- on the Mac, in a window, a game that picks its own present interval drops from 60 to 30 fps in a heavy moment and stays there for tens of seconds, until a pause or a view of a wall, then snaps back to 60 and holds it. The `[nvn] timing` lines show the same draws a frame taking much longer on the GPU at 30 fps than at 60; no `[RED]` line, thermal state -1, `the game sets present interval 1 → 2`. → [A game's own 30 fps fallback held by the GPU clock its longer hold lowered](../reference/frame-rate.md#a-games-own-30-fps-fallback-held-by-the-gpu-clock-its-longer-hold-lowered) - a Mac game started with `DM_FULLSCREEN=1` from an app that is itself in fullscreen (the launcher's console mode) came up as a window. → [A game opened from a fullscreen app stays a window](../reference/frame-rate.md#a-game-opened-from-a-fullscreen-app-stays-a-window) - on a ProMotion built-in screen, only in fullscreen, the frame step went 1 → 2 → 3 vsyncs on frames costing 0.1–0.6 vsyncs (`3 late frames; cost 0.32 vsyncs`) and the game presented 15 frames a second; a window, or the screen set to a fixed 60 Hz, did not do it. The `DM_NVN_TSV` rows show presents shown one 120 Hz refresh late (`@pres_at_ms` − `@link_target_ms` = 8.3 ms) with a skipped tick after each (`@link_gap_ms` 33 ms), and `@link_lead_ms` about 16 ms against about 50 ms in a window. → [Frame latency 2 for the display link in fullscreen](../reference/frame-rate.md#frame-latency-2-for-the-display-link-in-fullscreen) - in fullscreen on a MacBook Pro's ProMotion screen, a 60 fps game held 40 fps with its GPU at 10 ms a frame: frame times alternated 16.7 and 33.3 ms, and each `on-screen` line counted half its frames as held too long (`dropped 150` of 300, `discarded 0`). The same route in a window held 60.0 fps, and the title screen showed the same 40 fps in fullscreen. → [The 60 Hz link at 40 fps in fullscreen on a ProMotion screen](../reference/frame-rate.md#the-60-hz-link-at-40-fps-in-fullscreen-on-a-promotion-screen) diff --git a/docs/env_switches.tsv b/docs/env_switches.tsv index d7227ade7c..132af2529f 100644 --- a/docs/env_switches.tsv +++ b/docs/env_switches.tsv @@ -85,6 +85,7 @@ DM_GL_STUBS diagnostic `=1` gives every GL function the layer lacks a stub that DM_GPU_CAPTURE diagnostic `=[:]` or `gpu:[:]` writes a Metal GPU trace of those frames for Xcode's Metal debugger; can be set while running through `DM_CTL` DM_GPU_CAPTURE_DIR path Folder for `DM_GPU_CAPTURE`'s traces (default `gpu_captures/` in the working directory) DM_GPU_CAPTURE_KEEP diagnostic Before each GPU capture, prunes the capture folder's `dm_*.gputrace` entries so that the newest `KEEP - 1` stay (default 2) +DM_GPU_KEEP switch Mac: `0` turns off the GPU clock keeper, which keeps the GPU busy for a few seconds when frames are held longer than the shortest hold seen and their GPU time may not fit it at the clock macOS lowered for the longer hold (`nvn_gpu_keep.h`); on by default DM_GUEST_FILE switch `1` puts the guest's memory in sparse, unlinked files mapped shared over the guest block, `0` keeps it anonymous (`hle_svc.c`): file pages are out of `phys_footprint`, the figure iOS ends an app at, and the kernel can write them out when it needs the room. Unset: on iOS when the game's memory pool and half of it again, for the host's own memory (`DM_GUEST_FILE_HOST`), are larger than the room to the memory limit at start (the log's `[mem] guest memory` line says which), off on the Mac. The guest stays anonymous, with a `[RED]` line, when the files cannot be made, or the disk has less than `DM_GUEST_FILE_MIN_FREE` free (on the Mac that plus 8 GB) DM_GUEST_FILE_CHUNK switch `=` (whole MB, K / M / G; default 1G, at least 64M, at most 1024 files): the size of each guest file; one file when it is not under the block's size. The stall at the system's periodic flush grows with the file (on a 16 GB M5 Mac up to 148 ms with one 20 GB file, 4 ms with 20 files of 1 GB) DM_GUEST_FILE_DIR path Folder for `DM_GUEST_FILE`'s files (default the user's temporary folder; the iOS app sets it to `NSTemporaryDirectory()`) diff --git a/docs/reference/frame-rate.md b/docs/reference/frame-rate.md index 457a16cddf..4e7a59b839 100644 --- a/docs/reference/frame-rate.md +++ b/docs/reference/frame-rate.md @@ -610,6 +610,14 @@ Frame pacing: the host's frame step, engine frame limiters, present intervals an - **Check:** `test_nsp_mac_app.py` checks the key. To see a game's state: `/usr/bin/log show --last 10m --predicate 'process == "gamepolicyd"' --style compact | grep -E "Found game|Game mode|DPS"`. - **Not checked:** a game packed with the key false (the reporter's runs turned Game Mode off with the Game Overlay switch and with the category); other Apple chips; iOS, whose project still declares the key true (the same collapse is only suspected there, PR #314). +### A game's own 30 fps fallback held by the GPU clock its longer hold lowered + +- **Symptom:** on the Mac, in a window, a game that picks its own present interval drops from 60 to 30 fps in a heavy moment and stays there for tens of seconds, until a pause or a view of a wall, then snaps back to 60 and holds it. The `[nvn] timing` lines show the same draws a frame taking much longer on the GPU at 30 fps than at 60; no `[RED]` line, thermal state -1, `the game sets present interval 1 → 2`. +- **Cause:** macOS sets the GPU's clock from how busy it was. At two vsyncs a frame the GPU idles about half of each frame, the clock falls, and the same frame takes longer. A frame-rate controller that goes back to one vsync only once its frames fit one then never does, because at two vsyncs they are slow because of the two vsyncs. On the Switch the GPU's clock does not follow the load, so the game's rule is right there. The host already declares the shorter deadline to the CPU scheduler for the same reason (`rt_period_ns` in `hle_nvn.c`); nothing did it for the GPU. PR #1847's GPU clock keeper did, as a thread that spun the GPU whenever frames were slow, whatever the hold; it was declined for its power and heat on fanless Macs with no pause, suspend or quit handling. +- **Fix:** a workaround for a platform limit (no public API sets the GPU's performance state): the GPU clock keeper on the Mac probes. Its decisions are in `nvn_gpu_keep.h`, its thread is `gpu_keep_main` in `nvn_mtl.m`. Each present gives it the frame's period. When the 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 keeps one 32-thread threadgroup spinning on a queue of its own, only while the game's queue has nothing in flight (`rx_inflight`), each spin about 0.8 ms, for up to 4 s. It stops as soon as the period shortens (won). If it does not, the probe is lost: the next try waits 8 s, doubled after each further loss, and after 4 losses in a row the keeper waits until the period shortens by itself. 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 whose GPU is light at the longer hold cost nothing. It 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 or worse and in Low Power Mode; at quit it is stopped and waited for. The log has a line for each probe (started, won, lost, stopped and why). `DM_GPU_KEEP=0` turns it off. +- **Found on:** [Donkey Kong Bananza](../../dm/games/040074A01BF12000/README.md#found-on-this-game) (v458752, main at `e693bb89`, a window on a 60 Hz 2560x1440 screen, MetalFX): at 4,500-5,450 draws a frame, GPU p50 12.0-12.7 ms at 60 fps (frames 31,200-31,800 and 33,600) and 20.4-22.4 ms at 30 fps (frames 32,100-33,000); 8,066 draws a frame took 21.0-21.7 ms at 30 fps for 25 s (frames 20,400-22,200). Its controller wants interval 2 from a 20 ms frame. With the keeper (2026-11-30, M3 Max MacBook Pro, 8 hand-played sessions alternating on and off, each from the same restored save, the last pair a route through six layers with the map zoomed out and turned): 23 probes in the 4 sessions with it, all won, the game back at interval 1 0.3-0.7 s after each, 2.5 s of spinning in the busiest session (18 probes in 2.8 minutes); no 300-frame window at 30 fps in about 120 gameplay windows. Without it, two windows at 30 fps (frame p50 33.3 ms, GPU p50 24.6-25.3 ms at 9,600-13,800 draws). `powermetrics` read the GPU at a mean 10.4-12.8 W with the keeper and 11.9-12.5 W without it, so its spinning cost no measurable GPU power. **Open:** other Macs, a fanless one above all. +- **Check:** `t_gpu_keep` (30 checks: never at the shortest hold or for a game whose only hold is the longer one, the probe's start, win, stop and losses, the back-off, interval 3 to 2, 120 to 60 fps). `regress.py check` against main `1dae211a9` (2026-11-30, M3 Max MacBook Pro) on Donkey Kong Bananza, Star Fox, Mario Kart World, Metroid Prime Remastered and Ocarina of Time: no crash, hang, failed shader or `[RED]` kind of the keeper's, frame times within run-to-run noise. Mario Kart World's run had one probe, at a race end's load, won in 0.6 s; Star Fox, Metroid Prime Remastered and Ocarina of Time never change their interval, and Bananza's route did not, so the keeper never ran there. + ### A game opened from a fullscreen app stays a window - **Symptom:** a Mac game started with `DM_FULLSCREEN=1` from an app that is itself in fullscreen (the launcher's console mode) came up as a window.