Skip to content

Residency changes are queued and committed under a lock of their own, so res_add and res_remove no longer wait for another thread's residency commit - #2379

Closed
bymaxmarc wants to merge 1 commit into
seedds:mainfrom
bymaxmarc:pr/residency-commit

Conversation

@bymaxmarc

@bymaxmarc bymaxmarc commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What and why

On Nintendo Switch Sports Resort (04008E001F098000, a Switch 2 game; no profile on main yet), the court's first frame makes 855 new textures (908 MB). In that frame, res_add on the submit thread took 42-56 ms in all, a few milliseconds at a time, though each call is a hash-table insert.

res_commit held g_res_mu, the lock every res_add and res_remove takes, for the whole [MTLResidencySet commit]. A commit that adds memory takes about 0.04 ms per MB while the GPU is busy. The upload batches' completion handlers commit their staging's removal on their own threads, so the submit thread, adding the next textures, waited for those commits.

What nvn_mtl.m does now:

  • res_add and res_remove only queue the change under g_res_mu, holding a reference to its allocation.
  • res_commit takes the queue, then applies and commits it under g_res_commit_mu, which is never taken inside g_res_mu.
  • A res_commit returns once every change queued before it is committed, including a change another thread's commit took: that commit holds g_res_commit_mu until it is done.
  • A removed allocation stays alive until the commit that takes it out of the set.
  • defer_idle_commit asks res_pending whether anything is queued.
  • mtl_res_stats reads the set's counts under g_res_commit_mu.
  • The present snapshot's queue commit held g_res_mu to stay apart from the set's commit; it now holds g_res_commit_mu.

Reference: Residency changes waited for another thread's commit (new); symptom index regenerated.

This PR is independent of the staged-upload source buffer PR, but the game measurements below were taken with that change already in.

Checked

  • Tests, on this branch (main ccd834e + 1 commit), Apple M2 Max, macOS 27.2:

    • make -k -C dm/gfx/tests GAME=010015100B514000 -j8 run: exit 0, every test in the run list passed, as on main. t_res_commit: 8 checks, 0 failed (200 changes while a commit holds its lock: 0.72 ms in all).
    • t_res_commit runs under Metal validation. It checks that:
      • 200 res_add / res_remove calls, made while another thread holds a commit for 100 ms, take under a millisecond;
      • a res_commit then waits for that commit and commits its own changes;
      • a change another thread's commit took is in the set when res_commit returns (50 racing rounds);
      • a removed buffer stays alive until its removal is committed, then goes.
    • t_res_commit built with -fsanitize=thread (a one-off rule, as t_perf_order_tsan is built): 8 checks, 0 failed, no ThreadSanitizer warnings.
    • python -m unittest dm.tools.tests.test_docs dm.tools.tests.test_env_switches dm.tools.tests.test_pr_areas dm.tools.tests.test_lint_c: 30 tests OK.
  • Game: Nintendo Switch Sports Resort 04008E001F098000 v65536 on the user's Mac (Apple M2 Max, macOS 27.2), on our work branch. Two 75 s runs each, before and after, from the same state:

    Court's first frame Before After
    Submit thread's res_add calls, in all 42-56 ms 1.5-1.7 ms
    Longest single res_add 8.8-10.5 ms 0.08-0.13 ms
    The frame 242-263 ms 224-232 ms

    The user's last session (switch-sports-resort b6ddedf) reached frame 66,000.

  • Not checked:

    • scripts/regress.py: not run; every game's residency changes take this path.
    • lint_c.py tidy (no clang-tidy on this Mac) and iOS.

Docs

  • The finding is in its docs/reference/<subsystem>.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • No game files, keys or data extracted from a game in the change or the attachments

… so res_add and res_remove no longer wait for another thread's residency commit

Symptom: in a frame that makes many new textures (a scene's first), res_add on the submit thread took tens of milliseconds in all, a few milliseconds at a time, though each is a hash-table insert. Found on Nintendo Switch Sports Resort (04008E001F098000, Switch 2, v65536), the court's first frame (855 textures, 908 MB).

Cause: res_commit held g_res_mu, the lock every res_add and res_remove takes, for the whole [MTLResidencySet commit]. A commit that adds memory takes about 0.04 ms per MB while the GPU is busy, and the upload batches' completion handlers commit their staging's removal on their own threads, so the submit thread, adding the next textures, waited for those commits.

Fix (nvn_mtl.m): res_add and res_remove only queue the change, holding a reference to its allocation, under g_res_mu; res_commit takes the queue and applies and commits it under g_res_commit_mu, which nothing takes inside g_res_mu. A res_commit returns once every change queued before it is committed, also one another thread's commit took (that commit holds g_res_commit_mu until it is done), and a removed allocation stays alive until the commit that takes it out of the set. defer_idle_commit asks res_pending; mtl_res_stats reads the set's counts under g_res_commit_mu; the present snapshot's queue commit, which held g_res_mu to stay apart from the set's commit, holds g_res_commit_mu. Reference: memory.md, a new entry (Residency changes waited for another thread's commit); symptom index regenerated.

Checked: t_res_commit under Metal validation (200 res_add / res_remove while another thread holds a commit for 100 ms take under a millisecond, and a res_commit then waits for that commit and commits its own changes; a change another thread's commit took is in the set when res_commit returns, 50 racing rounds; a removed buffer stays alive until its removal is committed, then goes), also built with -fsanitize=thread (no warnings), the rest of the graphics tests (Apple M2 Max, macOS 27.2), test_docs. In the game, two 75 s runs each from the same state on the work branch (M2 Max), measured with the staged-upload source buffer fix (a separate PR) already in: the submit thread's res_add calls in the court's first frame 42-56 ms in all (the longest 8.8-10.5 ms) → 1.5-1.7 ms (0.08-0.13 ms), the frame 242-263 → 224-232 ms; the last session played to frame 66,000. Not run: regress.py (every game's residency changes take this path), lint_c.py tidy (no clang-tidy here), iOS.
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation labels Nov 12, 2026
seedds added a commit that referenced this pull request Nov 12, 2026
…d a lock of their own (PR #2379)

res_commit held g_res_mu, the lock every res_add and res_remove takes,
for the whole [MTLResidencySet commit], which takes 10-25 ms when it
adds hundreds of MB while the GPU runs. The upload batches' completion
handlers commit their staging's removal on their own threads, so the
submit thread, adding a scene's new textures, waited for those commits.

res_add and res_remove now only queue the change under g_res_mu, each
holding its allocation; res_commit takes the queue and applies and
commits it under g_res_commit_mu (taken before g_res_mu, never inside
it). A res_commit returns once every change queued before it is
committed, also one another thread's commit took, and a removed
allocation stays alive until the commit that takes it out of the set.
The queue commits that held g_res_mu to stay apart from the set's
commit, the present snapshot's and the encode thread's (nvn_rec.h, for
every command buffer), hold g_res_commit_mu.

- Switch Sports Resort's first court frame (855 textures, 908 MB, M2
  Max, PR #2379): the submit thread's res_add calls 42-56 -> 1.5-1.7 ms
  in all, the frame 242-263 -> 224-232 ms.
- t_res_commit (new, Metal validation): res_add / res_remove do not wait
  for a commit under way, res_commit waits for it, racing commits leave
  nothing out of the set, a removed buffer lives until its commit.
- regress.py check (M4 Pro): OCTOPATH TRAVELER II with the encode thread
  (DM_NVN_ENC_THREAD=1) and Mario Kart World on the direct path pass;
  fps and frame p99 unchanged.

Co-authored-by: bymaxmarc <354601562+bymaxmarc@users.noreply.github.com>
@seedds

seedds commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Landed on main as 0c07813, with you as co-author. Thank you for the diagnosis, the queue design and the test.

Changed on the way in:

Checked on an M4 Pro, macOS 27.2: the full dm/gfx/tests run list (exit 0, t_res_commit 8 checks, 0 failed); test_docs, test_pr_areas, test_lint_c; regress.py check against main: OCTOPATH TRAVELER II with DM_NVN_ENC_THREAD=1 (59.8 → 59.8 fps, p99 17.57 → 17.71 ms) and Mario Kart World on the direct path (59.9 → 60.0 fps, p99 20.40 → 20.41 ms), both PASS with no new RED. Not run: iOS, Switch Sports Resort.

@seedds seedds closed this Nov 12, 2026
iDams pushed a commit to iDams/nxnative that referenced this pull request Nov 13, 2026
…d a lock of their own (PR seedds#2379)

res_commit held g_res_mu, the lock every res_add and res_remove takes,
for the whole [MTLResidencySet commit], which takes 10-25 ms when it
adds hundreds of MB while the GPU runs. The upload batches' completion
handlers commit their staging's removal on their own threads, so the
submit thread, adding a scene's new textures, waited for those commits.

res_add and res_remove now only queue the change under g_res_mu, each
holding its allocation; res_commit takes the queue and applies and
commits it under g_res_commit_mu (taken before g_res_mu, never inside
it). A res_commit returns once every change queued before it is
committed, also one another thread's commit took, and a removed
allocation stays alive until the commit that takes it out of the set.
The queue commits that held g_res_mu to stay apart from the set's
commit, the present snapshot's and the encode thread's (nvn_rec.h, for
every command buffer), hold g_res_commit_mu.

- Switch Sports Resort's first court frame (855 textures, 908 MB, M2
  Max, PR seedds#2379): the submit thread's res_add calls 42-56 -> 1.5-1.7 ms
  in all, the frame 242-263 -> 224-232 ms.
- t_res_commit (new, Metal validation): res_add / res_remove do not wait
  for a commit under way, res_commit waits for it, racing commits leave
  nothing out of the set, a removed buffer lives until its commit.
- regress.py check (M4 Pro): OCTOPATH TRAVELER II with the encode thread
  (DM_NVN_ENC_THREAD=1) and Mario Kart World on the direct path pass;
  fps and frame p99 unchanged.

Co-authored-by: bymaxmarc <354601562+bymaxmarc@users.noreply.github.com>
Kayyluhh added a commit to Kayyluhh/nxnative that referenced this pull request Nov 14, 2026
…dds#2389 (034e91a) replaces the batch's port

PR seedds#2389 closed: main keeps finalized textures in the ghost table by use (a least-recently-used list, own_ghost_trim). The batch's port (the queue, own_ghost_trim_idle, its tests and its reference entry) goes: nvn_owner.m, nvn_owner.h, t_serial_owner.m and t_cpu_tex.m are main's, nvn_mtl.m takes main's trim and POOL_IDS_MAX comment, the reference entry and the game note's line are main's. nvn_rec.h: seedds#2341's held commit takes main's g_res_commit_mu (seedds#2379) in place of g_res_mu. DMSH_CHAIN regenerated. Checked: t_serial_owner 176, t_cpu_tex 105, t_replay_ahead, t_view_cache 44, t_dispatch_barrier (four modes), t_srgb_alias 15, all dm/gfx/tests programs build, test_docs, test_env_switches, test_game_profile, test_nsp_chain, the Mac host for 0400486024DEC000.
oliverames pushed a commit to oliverames/nxnative that referenced this pull request Nov 16, 2026
…the residency set; slow residency and queue commits say so

Symptom: NX18's Ocarina of Time test 3 on an iPhone 18 Pro Max froze once, about 13 s after a save load: presents stopped while audio went on, with no [RED] line and no crash. Its
audits found no lock cycle, but one path they could not rule out is this one.

Cause: every Metal 4 commit feedback handler runs on the one serial queue g_feedback_q, and the upload batch's handler called res_commit() (g_res_commit_mu) every time, to take its
staging out of the residency set. So one long [g_res commit], or a long [g_q4 commit:] holding the lock on the encode thread, held back every completion behind it (g_done, the
presents, the game's SyncWaits) with nothing in the log: nothing timed either kind of commit, and the residency set line only sums them every 300 frames.

Fix: the handler now only queues the staging's removal (res_remove, a moment under g_res_mu) and hands the commit to a residency commit worker, a serial queue of its own at
user-interactive QoS (res_commit_async). A removal is still committed only once the GPU is done with what it removes: the handler queues it after its command buffer has completed,
and any commit after that takes it, which is the rule defer_drain's removals already follow. A batch that retires staging stays in g_up_pending until the worker has committed, so
nvnQueueFinish and mtl_drain still return with the staging out of the set and freed; a batch with nothing to retire (its slot's own blocks, the usual case) is done in the handler as
before, with no commit. res_commit and the queue commits (the encode thread's main and upload commits, rec_commit and rec_commit_upload without it, snap_take) are timed
(CLOCK_UPTIME_RAW, two reads); one of 100 ms or more logs "[RED] slow residency commit: N ms (adds A, B MB; removes R, S MB) on thread T[, on the feedback queue] (#k)" or "[RED]
slow queue commit: N ms at <site> on thread T[, on the feedback queue] (#k)", each kind its first 20 and then every 100th. g_feedback_q carries a queue-specific key so the lines can
say they ran there. docs/reference/memory.md has the entry "A long residency commit held back every GPU completion".

Ported from NX18 (kencalx/NX18) ec481764188b717be1f43d1da263c31067b1f797:
kencalx/NX18@ec48176
Upstream already has the residency queueing of 0c07813 (PR seedds#2379), which this builds on: that change took the lock off res_add and res_remove, and this one takes the commit off the
feedback queue. Adapted: NX18's version also moved the one-off command buffer's and the whole-window screenshot's commits to the worker (main has neither once_commit nor that
screenshot path), and added a "slow wait for the residency commit lock" line built on NX18's lock notes (res_commit_lock, g_res_commit_note), which main does not have, and moved
pool_res's commit out of g_mu (pool_res_commit, g_pool_res_open). Those three are left out of this change; the lock-note line belongs with a freeze detector, and the pool_res move
is a separate change. t_res_commit is NX18's cases for the parts ported, rewritten for main's internals. NX18's test counts (28 checks, 5 runs) are theirs; they were not measured here.

Checked on a Mac (M2 Pro, macOS 27.3, Xcode 27.3 beta, GAME=0400486024DEC000): the dmhost build and check_t_res_commit (18 checks). Not checked: check_t_upload_budget, check_t_upload_slots, check_t_readback, check_t_present_snap, check_t_nvn, any game, any device, and nothing shows this was the cause of that freeze.

Co-authored-by: kencalx <105067885+kencalx@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RcRaRTbNVNsBJcfGqFidW1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation 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.

2 participants