Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
The pool walk's statistics no longer make a chunk of a large pool for…
… every texture they ask about

The first pool walk of a scene (and every 500th after) asks pool_find(lo, 1) where each registered texture's storage lies, to log how much of it is in wired pools; the raw-memory
log's pool age asks the same. pool_find makes the chunk of a large pool's stride when it has none, and a length of 1 means BIG_UNKNOWN past the address: a zero-copy buffer of 80 MB or
more, not in the residency set, counted by Metal in full. In Ocarina of Time's area load (pool of 2,432 MB, 153 strides) the walk made 77 of them, 6.7 GB of currentAllocatedSize for
pages other buffers already map. pool_peek answers the same question without making anything. t_pool_chunks: 3 new checks. Reference entry and game note added.
  • Loading branch information
lunks committed Nov 9, 2026
commit 5da752cdcabf45f85fadf2b490ef48f8c55e2f1a
1 change: 1 addition & 0 deletions dm/games/0400486024DEC000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F
- [Frame-step stubs placed by their function](../../../docs/reference/rewriter.md#frame-step-stubs-placed-by-their-function)
- [Repository patches missing under bracketed paths](../../../docs/reference/rewriter.md#repository-patches-missing-under-bracketed-paths): resolved, a build in a folder named with brackets lost this game's frame-step stubs and cuts without an error.
- [Retired pool buffers pile up while a game submits nothing](../../../docs/reference/memory.md#retired-pool-buffers-pile-up-while-a-game-submits-nothing): out of focus GPU allocated went from 11 to 20 GB in 12 s with swap following (PR #1392); the likely cause of a session that stayed frozen after a window switch. Not reproduced on the village-deck route (2026-11-09, M6): 40 s out of focus, during the load or standing on the deck, made no pool deletions at all, so the scene that remakes a virtual pool's runs out of focus is still to be found.
- [The pool walk's statistics made chunks of a large pool](../../../docs/reference/memory.md#the-pool-walks-statistics-made-chunks-of-a-large-pool): resolved, GPU allocated 3.3 to 15.8 GB in the area load on a 16 GB Mac (issue #2068); 77 overlapping 80 MB chunks of the 2,432 MB pool, made by two log statistics.

- [Position invariance across shaders](../../../docs/reference/shaders.md#position-invariance-across-shaders): white shapes on plants and bushes for single frames in Kokiri Forest, from the foliage's mesh pre-pass and mesh colour pass rounding the position apart.
- [gl_FragCoord.z against the depth stored for its own surface](../../../docs/reference/shaders.md#gl_fragcoordz-against-the-depth-stored-for-its-own-surface): the dark specks flickering in the ground mist (long-standing). The mist's own depth test (`5ad65c0eefd09c7f`, against its depth-only pass `4757b5b373a5406e`) rejected its own surface where Apple's GPU stored the depth one ulp under the fragment's z. Bisected by dropping fragment shaders: after mesh draws took the drop too (`skip_fs_draw`), the specks went with the mist or its depth-only pass, and when that pass wrote `[[position]].z` as depth. Resolved by the rewrite: no specks in play with its 39 rewritten shaders as an overlay (the player's check, 2026-11-09); a full library build on the new chain has not been run.
Expand Down
16 changes: 14 additions & 2 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,18 @@ static int pool_find(uint64_t addr, uint64_t len) {
for (int i = 0; i < g_npool; i++) if (addr >= g_pools[i].lo && addr + len <= g_pools[i].hi) return g_plast = i;
return -1;
}
// The entry that holds addr, or -1, with no side effect: pool_find makes a large pool's chunk (a zero-copy buffer of 80 MB and more) for any stride it is asked about, which a statistic must not
// do (the pool walk's log asked about every texture's storage and made 77 of them, 6.7 GB counted by Metal, in Ocarina of Time's first area load). Caller holds g_mu
static int pool_peek(uint64_t addr) {
if (g_nbig) { int b = big_of(addr); if (b >= 0) { const big_pool *bp = &g_big[b]; return bp->full ? bp->full - 1 : bp->chunk[(addr - bp->a) >> 24] - 1; } }
if (g_plast >= 0 && g_plast < g_npool && addr >= g_pools[g_plast].lo && addr < g_pools[g_plast].hi) return g_plast;
pidx_sort();
int lo = 0, hi = g_npool - 1, hit = -1;
while (lo <= hi) { int m = (lo + hi) / 2; if (g_pools[g_pidx[m]].lo <= addr) { hit = m; lo = m + 1; } else hi = m - 1; }
if (hit >= 0 && addr < g_pools[g_pidx[hit]].hi) return g_pidx[hit];
for (int i = 0; i < g_npool; i++) if (addr >= g_pools[i].lo && addr < g_pools[i].hi) return i;
return -1;
}
// Whether [addr, addr + len) lies in a pool, with no side effect: mtl_addr makes the pool resident, for a GPU use to follow. A large pool is answered from its range, since its lookup
// (big_lookup) makes the chunk that holds the range. hle_nvn.c keys_hash asks this whether a shader's code is mapped: through mtl_addr every pool holding shader code went into the
// residency set, though the GPU never reads it (the shaders run translated): The Great Ace Attorney Chronicles' three chunks, about 5 MB
Expand Down Expand Up @@ -9751,7 +9763,7 @@ static uint64_t pool_gpu(int samp, int dtt, int dpf, int base, __unsafe_unretain
for (int i = 0; i < n && rs; i++) { int id_ = base + i; if (id_ > 0 && g_treg[id_].ok && !g_treg[id_].gone && g_treg[id_].t.mem) { rs[nr].lo = (uint64_t)(uintptr_t)g_treg[id_].t.mem; rs[nr].hi = rs[nr].lo + pool_tex_size(&g_treg[id_].t); nr++; } }
if (rs) { qsort(rs, (size_t)nr, sizeof *rs, pool_size_cmp); pthread_mutex_lock(&g_mu);
for (int k = 0; k < g_npool; k++) { uint64_t sz = g_pools[k].hi - g_pools[k].lo; pall += sz; if (g_pools[k].res) pres += sz; }
uint64_t end = 0; for (int i = 0; i < nr; i++) { uint64_t lo = MAX(rs[i].lo, end), hi = rs[i].hi; if (hi <= lo) continue; end = hi; tall += hi - lo; int q = pool_find(lo, 1); if (q >= 0 && g_pools[q].res) tres += hi - lo; }
uint64_t end = 0; for (int i = 0; i < nr; i++) { uint64_t lo = MAX(rs[i].lo, end), hi = rs[i].hi; if (hi <= lo) continue; end = hi; tall += hi - lo; int q = pool_peek(lo); if (q >= 0 && g_pools[q].res) tres += hi - lo; }
pthread_mutex_unlock(&g_mu); free(rs); }
MLOG("pool walk: tables made %llu, reused %llu; passes ended after a pool shader that may read depth %llu", (unsigned long long)g_pool_made, (unsigned long long)g_pool_reused, (unsigned long long)g_fb_split_pool);
MLOG("pool walk: %d IDs, %d left out, %u read by a shader (%llu MB), allocated %llu MB; memory pools %llu MB, wired %llu MB; texture storage %llu MB, in wired pools %llu MB", n, left, nseen, (unsigned long long)(bseen >> 20), (unsigned long long)(g_dev.currentAllocatedSize >> 20), (unsigned long long)(pall >> 20), (unsigned long long)(pres >> 20), (unsigned long long)(tall >> 20), (unsigned long long)(tres >> 20)); }
Expand Down Expand Up @@ -10003,7 +10015,7 @@ static void gml_read(id<MTLBuffer> b, id<MTLBuffer> map, int predicted, uint64_t
if (known) continue;
if (g_gml_n == GML_MAX) { g_gml_over = 1; break; }
if (predicted) for (int j = 0; j < nr; j++) if (e[0] < rhi[j] && rlo[j] < e[1]) { shadowed++; break; } // under a shadow? The whole pool counts: the log does not say where in it
pthread_mutex_lock(&g_mu); int p = pool_find(e[0], 1); double age = p >= 0 ? (double)(now - g_pools[p].born) / 1e6 : -1; pthread_mutex_unlock(&g_mu);
pthread_mutex_lock(&g_mu); int p = pool_peek(e[0]); double age = p >= 0 ? (double)(now - g_pools[p].born) / 1e6 : -1; pthread_mutex_unlock(&g_mu);
if (age >= 0) { if (age < age_lo) age_lo = age; if (age > age_hi) age_hi = age; }
g_gml_lo[g_gml_n] = e[0]; g_gml_hi[g_gml_n++] = e[1]; added++;
}
Expand Down
5 changes: 5 additions & 0 deletions dm/gfx/tests/t_pool_chunks.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ int main(void) { @autoreleasepool {
mtl_pool_add(mem, GB);
CHECK("no buffer until a lookup", g_npool == pools0 && g_nbig == 1);
CHECK("overlapping buffers: barriers carry alias visibility", resource_vis() & MTL4VisibilityOptionResourceAlias);
pthread_mutex_lock(&g_mu); int peeked = pool_peek(base + 100), peek_chunks = g_npool; pthread_mutex_unlock(&g_mu);
CHECK("asking where an address is (the pool walk's statistics) makes no chunk", peeked == -1 && peek_chunks == pools0);
id<MTLBuffer> a = look(base + 100, 4096);
CHECK("one chunk", g_npool == pools0 + 1);
pthread_mutex_lock(&g_mu); int in_stride = pool_peek(base + 9 * MB), other = pool_peek(base + 300 * MB), n_after = g_npool; pthread_mutex_unlock(&g_mu);
CHECK("peek finds the chunk of a stride that has one", in_stride >= 0 && g_pools[in_stride].buf == a);
CHECK("peek of a stride with none: -1, and still no new chunk", other == -1 && n_after == pools0 + 1);
CHECK("a chunk is far smaller than the pool", a.length <= 3 * BIG_S);
CHECK("the same stride: the same buffer", look(base + 200, 4096) == a);
CHECK("no more chunks", g_npool == pools0 + 1);
Expand Down
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- SIGBUS inside a host hook (`snprintf` under `mount_ex` / `h_fs_MountSave`), then the process hangs (Super Mario 3D World + Bowser's Fury); SIGBUS inside Metal (`AGCDeserializedReply`, an AGX compiler block) at an address in the guest stack region (Breath of the Wild). → [Host calls on small guest stacks](../reference/memory.md#host-calls-on-small-guest-stacks)
- the iPhone app is killed for memory; resident memory grows steadily (about 0.2 MB a frame) while the residency set and the texture counts stay flat. → [Deferred-release drain without an autorelease pool](../reference/memory.md#deferred-release-drain-without-an-autorelease-pool)
- out of focus, with the picture still and the sound on, GPU allocated climbs by hundreds of MB a second while the residency set stays flat, and swap follows it. → [Retired pool buffers pile up while a game submits nothing](../reference/memory.md#retired-pool-buffers-pile-up-while-a-game-submits-nothing)
- in an area load GPU allocated jumps by 6-10 GB between two stats windows (Ocarina of Time: 3.3 GB at frame 900, 15.8 GB at frame 1200 on a 16 GB M5, issue #2068; 3.5 to 13.7 GB inside the load on an M5 Pro) while the residency set grows by about 4 GB, and stays there. The memory census's "not counted" line is 8 GB, and the pool buffers made sum to twice the pools' size. → [The pool walk's statistics made chunks of a large pool](../reference/memory.md#the-pool-walks-statistics-made-chunks-of-a-large-pool)
- travelling produces a brief multi-GB footprint spike, then memory returns to its earlier level; periodic memory lines can miss the peak. Upload diagnostics show many swapped-out blocks still alive in one batch. → [Upload staging grows within one rendering submission](../reference/memory.md#upload-staging-grows-within-one-rendering-submission)
- GPU memory grows with repeated `nvnTextureReadTexels` calls on a long-lived game thread, even after each readback completes. → [GPU readback without an autorelease pool](../reference/memory.md#gpu-readback-without-an-autorelease-pool)
- process footprint and Metal allocation grow during stationary gameplay even after readback fences complete; the deferred-release queue and command allocator sizes remain bounded. → [Completion readbacks survive a guest waiter's pool](../reference/memory.md#completion-readbacks-survive-a-guest-waiters-pool)
Expand Down
8 changes: 8 additions & 0 deletions docs/reference/memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ The guest address space, heaps, pools, virtual address memory and the allocators
- **Check:** `make -C dm/gfx/tests GAME=<id> check_t_pool_retire`: a buffer retired between submissions goes with none after it, one retired with a submission open stays until that submission completes, one retired with a submission in flight waits for that fence. In a game, `DM_OE_SCRIPT="focus=2@60,focus=1@90"`: GPU allocated stays flat between the two.
- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md), measured and diagnosed by Pedro Nascimento in [PR #1392](https://github.com/seedds/nxnative/pull/1392).

### The pool walk's statistics made chunks of a large pool

- **Symptom:** in an area load GPU allocated jumps by 6-10 GB between two stats windows (Ocarina of Time: 3.3 GB at frame 900, 15.8 GB at frame 1200 on a 16 GB M5, issue #2068; 3.5 to 13.7 GB inside the load on an M5 Pro) while the residency set grows by about 4 GB, and stays there. The memory census's "not counted" line is 8 GB, and the pool buffers made sum to twice the pools' size.
- **Cause:** the pool walk's log (`pool walk: ... in wired pools`, the first pool-table build of a scene and every 500th after) and the raw-memory log's pool age asked `pool_find(addr, 1)` where each texture's storage lies. For a large pool (`BIG_MIN` and up, in 16 MB strides) `pool_find` makes the stride's chunk, a zero-copy buffer of the asked length plus a stride, and a length of 1 means `BIG_UNKNOWN` (64 MB) past the address: 80 MB or more, not in the residency set, overlapping its neighbours' chunks, none used by the GPU. Ocarina's 2,432 MB pool has 153 strides; the walk touched 77 of them: 6.7 GB that `currentAllocatedSize` counts in full (it counts a no-copy buffer at its whole length, in a residency set or not). The pages are shared, so resident memory did not grow by that much (10.9 GB against 14.1 GB allocated), but the 16 GB Macs that run this game reach 17-18 GB allocated.
- **Fix:** `pool_peek` (`nvn_mtl.m`) answers the same question without making a chunk: the stride's entry if it has one, else -1. Both statistics use it.
- **Check:** `make -C dm/gfx/tests GAME=<id> check_t_pool_chunks`: asking where an address is makes no chunk, and finds the chunk of a stride that has one. In a game, the memory census line ("GPU allocation census") at the same point before and after: Metal's total and the pool buffers made.
- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md), diagnosed from the allocation census of Pedro Nascimento's M5 Pro runs and the numbers in issue #2068.

### Upload staging grows within one rendering submission

- **Symptom:** travelling produces a brief multi-GB footprint spike, then memory returns to its earlier level; periodic memory lines can miss the peak. Upload diagnostics show many swapped-out blocks still alive in one batch.
Expand Down
Loading