Repository navigation
host: the guest's memory in files (DM_GUEST_FILE, DM_GUEST_FILE_CHUNK) - #1315
Closed
clabeuhtegrite wants to merge 1 commit into
Closed
clabeuhtegrite wants to merge 1 commit into
clabeuhtegrite wants to merge 1 commit into
Conversation
3 tasks done
On an iPhone the system ends the app once its footprint reaches the memory limit: about 6 GB on a 12 GB iPhone with the increased memory limit, 3.3 GB without it. Every guest page the game has touched is anonymous memory, which phys_footprint charges and which iOS cannot write out as a Mac swaps it. DM_GUEST_FILE=1 (off by default) puts the guest block in a sparse file, unlinked at once, mapped shared over the reservation at the same addresses, in DM_GUEST_FILE_DIR (the iOS app sets NSTemporaryDirectory). File pages take the same memory while they stay in it, but they are out of the footprint and the kernel writes them out when it needs the room. DM_GUEST_FILE_CHUNK=<size> uses several files instead: the stall at the system's periodic flush grows with the file (on a 16 GB M5 Mac with 1 GB touched, up to 148 ms with one 20 GB file, 4 ms with 20 files of 1 GB). The guest stays anonymous, with a [RED] line, when the files cannot be made, unlinked, sized or mapped, on iOS when their protection class cannot be C, and on the Mac when the disk has less than DM_GUEST_FILE_MIN_FREE (default 5 GB) plus 8 GB free. A thread stops the game (exit code 87) when the disk falls under that minimum, and logs the files' blocks, the disk I/O and the footprint every 10 s. guest_release made a range the game gives back a fresh anonymous mapping; over the files that would put it back into the footprint as soon as the game maps it again, so there the range only goes PROT_NONE and keeps its bytes. alias_initialize then zeroes them when they are mapped again, as before, and SetHeapSize zeroes a heap that grows back over them; with the files both leave the 4 KB pages that already read 0 alone, so that zeroing does not dirty pages the kernel would write back. crash.c: a fault the fault hooks claim 256 times in a row with nothing changed, at a page whose protection allows the access but which cannot be read (a file page that does not come in), takes the crash path with a [RED] line instead of faulting forever (DM_FAULT_RETRY_CAP, on by default only with DM_GUEST_FILE). Measured with Mario Kart World 1.0.0 (0400C3F00006E000) in handheld mode: - On this host (975bb4c with this change, DM_GUEST_FILE_CHUNK=1G, a larger texture view cache and the profile's map limit), on a 16 GB M5 Mac with the iPhone's settings: an 18-minute session (title, menus, a race) and a 30-minute one (a cup and free roam) peaked at 4,365 and 4,792 MiB of footprint, with no drift. The same build plays on a 12 GB iPhone in LiveContainer, whose limit is 6,143 MiB. - In seedds#1184, two people merged the switch2ios fork's version of this change by hand (onto 975bb4c and ed3c93e). On an iPhone Duo (8,191 MB limit), where the game was ended at course load, it finished a race with 4,539 MB left at the least. On an iPhone 18 Pro, where it was ended 30 s in at 8.1 GB, it played 13 minutes and two races with 512 MB files, at most 4.41 GB (4.55 GB by the peak counter). - On the switch2ios fork itself (host from nxnative 8c0b808), on the 12 GB iPhone: ended at 90 s with the guest anonymous; 7 min 33 s with one file, peak 5.56 GiB; 14 minutes with 1 GB files, peak 5,663 MB. Checked: on a 16 GB M5 Mac (macOS 27.0.1, Xcode 27.2), as CI runs them, make -C dm/host/tests run (with GAME=0400C3F00006E000 and with CI's 010015100B514000: 2,194 checks each, the memory SVC tests with the guest in one file and in 6 files), asan, tsan memory-tsan and tsan-threads pass, with no sanitizer report. The memory SVC tests (t_svc_memory, with the new heap_regrow) also pass on Linux with stand-ins for the Darwin calls, the guest anonymous, in one file and in 64 MB files, with and without the map limit (ownership() skipped there: it fails on Linux before and after this change); heap_regrow fails in files without the SetHeapSize zeroing. clang-tidy 18 on hle_svc.c reports nothing new. make -C dm/host/tests memory-file, part of run, runs the tests with the guest in files. With DM_GUEST_FILE unset the host takes the paths it took before: guest_release, alias_initialize and SetHeapSize test for the files, and DM_FAULT_RETRY_CAP is off; the "[mem] alias region mapped" line only gains what was given back and zeroed so far. Docs: docs/reference/memory.md (iOS ends the app once the guest's touched pages reach its memory limit), docs/env_switches.tsv, the symptom index and Mario Kart World's note. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzPJouRkJtwacjUv5vhZN8
clabeuhtegrite
force-pushed
the
guest-file
branch
from
November 5, 2026 14:00
5c7be99 to
0f8247e
Compare
fpedrolucas95
pushed a commit
to fpedrolucas95/nxnative
that referenced
this pull request
Nov 5, 2026
…: large-pool chunks retire, pools from 128 MB are chunked, the pool walk checks the type first, and DM_NVN_TEX_MIP_SKIP drops the finest level of compressed CPU-written textures PRAGMATA (0400C76024710000) on an 8 GB M1 MacBook Air: its opening load took the residency set to 5.2 GB of the 5,461 MB working set. Command buffers failed with kIOGPUCommandBufferCallbackErrorOutOfMemory, the submit thread waited in the kernel, and WindowServer hit its watchdog. - big_lookup: a chunk that a longer one replaces is retired once the submissions that may use it complete, instead of staying resident until the pool goes. Every cache of pool addresses checks g_pool_gen, which big_new bumps. Before, a 48 MB and a 96 MB chunk at one base both counted. - Pools of 128 MB and more (was 512) are wrapped in chunks, so only what lookups reach is wired. BIG_MAX goes from 32 to 64. - pool_gpu skips IDs whose descriptor cannot be of the walked type (ttype_apart). It used to make a Metal texture for every registered ID before comparing types, so a cube-texture walk uploaded every texture. - DM_NVN_TEX_MIP_SKIP=<px> (1 = 512; off by default): CPU-written block-compressed 2D, array and cube textures with a mip chain are made without their finest level. Views, copies, readbacks and a sparse texture's level clamp take the guest's level less the skip (tex_mip_skipped, lv_host). - DM_NVN_GMEM_RES=0: pools reached only through the raw global memory table stay out of the residency set, and large pools stay in chunks. - DM_NVN_DESWZ_GPU=0: textures are deswizzled on the CPU. - Diagnostics: the out-of-memory report gives the distinct pages under the shared buffers and texture memory by pixel format. DM_NVN_RES_LIMIT_MB=<MB> stops the game gracefully before the set reaches the limit. Measured on that Mac, with seedds#1315's guest file: at the title the set fell from 3,985 MB to 3,241 MB. Through the opening load it peaked at 4,284 MB, with no GPU errors, and the game went on to its opening scene. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DeVTtdCXD7t64Ek6e1py6A
seedds
added a commit
that referenced
this pull request
Nov 5, 2026
… its guest memory in files, out of the footprint iOS ends an app at (PR #1315, issue #1184) Every guest page the game has touched is anonymous memory, which phys_footprint charges and an iPhone cannot write out. Mario Kart World was ended that way at course load on 8 GiB iPhones. The guest block can instead lie in sparse files, unlinked at once and mapped shared over the reservation at the same addresses: file pages take the same memory while they stay in it, but they are out of the footprint, and the kernel writes them out when it needs the room. This goes around a platform limit: it does not reduce the memory used, and it costs disk. So a game that fits stays anonymous. With DM_GUEST_FILE unset, iOS uses the files when the game's memory pool is larger than os_proc_available_memory() at start, and the log's [mem] guest memory line gives both; the Mac stays anonymous, as it can swap. DM_GUEST_FILE=1 forces them on and 0 off. Of the profiles, the 9 with a 9 GB pool switch on under limits of 3,376, 6,143 and 8,191 MiB, none at 12,287 MiB, and the 136 with a pool of 3.21 GB or less under none of these. - The files are 1 GB each (DM_GUEST_FILE_CHUNK), since the stall at the system's periodic flush grows with the file. Any failure to make, unlink, protect (class C on iOS), size or map them leaves the guest anonymous, with a [RED] line, as does a disk under DM_GUEST_FILE_MIN_FREE (default 5 GB; on the Mac that plus 8 GB). A thread stops the game (exit code 87) when the disk falls under the minimum later. - guest_release only makes the files' ranges PROT_NONE, since a fresh anonymous mapping would bring them back into the footprint. alias_initialize and SetHeapSize zero them when they are opened again, leaving alone the 4 KB pages that already read 0. Not helped: memory outside the block, such as Unreal's heap on a host malloc zone, so OCTOPATH TRAVELER II (#1226, #925) gets no change from this rule. Measured: Mario Kart World 1.0.0 in handheld mode on a 16 GB M5 Mac, with the files forced on, on 5ae0d9a with this hle_svc.c (main's shader libraries are not on disk): footprint 6.87 GB in the race against 12 GB anonymous, p50 16.7 ms, longest frame near the end of the run 201 ms against 93 ms, 6.0 GB on disk and 9.15 GB of logical writes in 5.5 minutes. Three runs with files reached frames 11,700, 14,100 and 12,300 in the time the one anonymous run reached 14,100, so slower loading is not ruled out. In #1184 two people merged the switch2ios fork's version by hand, and Mario Kart World finished races on an iPhone Duo and an iPhone 18 Pro (4.41 GB at most over 13 minutes and two races). Those runs also had a larger texture view cache, which is not on main, and their own guest_release and mem_any changes. Not run on a phone: this code and the automatic rule. Checked: make -C dm/host/tests run with GAME=0400C3F00006E000 and 010015100B514000, with the memory tests anonymous, in one file and in 6 files, and a new test of the rule; asan and tsan memory-tsan report nothing; hle_svc.c compiles for the iPhone SDK; a low disk and a bad folder leave the guest anonymous; test_docs and the symptom index pass. regress.py not run: on the Mac the default path differs from main only in tests of g_gf_n. Based on clabeuhtegrite's design and code in PR #1315, without its fault retry cap, stale-file cleanup and one-file path. Front-ends: new: an iOS front end with its own launcher sets DM_GUEST_FILE_DIR to its temporary folder, as dm/ios/Sources/main.m does; a game whose memory pool is larger than the room to the memory limit now writes guest memory there, up to what it has touched, and exits with code 87 when the disk falls under DM_GUEST_FILE_MIN_FREE. Co-authored-by: clabeuhtegrite <82087430+clabeuhtegrite@users.noreply.github.com>
Owner
|
Landed on main as 865cc35, with you as co-author, thank you. The mechanism and much of the code are yours. It was rewritten before landing, so here is what differs:
On a 16 GB M5 Mac, Mario Kart World with the files forced on had a footprint of 6.87 GB in the race against 12 GB anonymous, at the same p50. That ran on The view cache from #1184 would be its own change. If you want to propose it, its numbers on current main with this switch would be the place to start. |
stan69b
pushed a commit
to stan69b/nxnative
that referenced
this pull request
Nov 5, 2026
The compile cache as one file (7249ee9), Unreal's dynamic buffers with 4 copies (6d269f9), the Arceus capture demo (dd6ce5b), guest memory in files on iPhone (865cc35, PR seedds#1315). dmsh_chain.h kept as ours; it is regenerated in A-9 with the rebuilt translator.
Know-It-All-Brother
pushed a commit
to Know-It-All-Brother/nxnative
that referenced
this pull request
Nov 9, 2026
… its guest memory in files, out of the footprint iOS ends an app at (PR seedds#1315, issue seedds#1184) Every guest page the game has touched is anonymous memory, which phys_footprint charges and an iPhone cannot write out. Mario Kart World was ended that way at course load on 8 GiB iPhones. The guest block can instead lie in sparse files, unlinked at once and mapped shared over the reservation at the same addresses: file pages take the same memory while they stay in it, but they are out of the footprint, and the kernel writes them out when it needs the room. This goes around a platform limit: it does not reduce the memory used, and it costs disk. So a game that fits stays anonymous. With DM_GUEST_FILE unset, iOS uses the files when the game's memory pool is larger than os_proc_available_memory() at start, and the log's [mem] guest memory line gives both; the Mac stays anonymous, as it can swap. DM_GUEST_FILE=1 forces them on and 0 off. Of the profiles, the 9 with a 9 GB pool switch on under limits of 3,376, 6,143 and 8,191 MiB, none at 12,287 MiB, and the 136 with a pool of 3.21 GB or less under none of these. - The files are 1 GB each (DM_GUEST_FILE_CHUNK), since the stall at the system's periodic flush grows with the file. Any failure to make, unlink, protect (class C on iOS), size or map them leaves the guest anonymous, with a [RED] line, as does a disk under DM_GUEST_FILE_MIN_FREE (default 5 GB; on the Mac that plus 8 GB). A thread stops the game (exit code 87) when the disk falls under the minimum later. - guest_release only makes the files' ranges PROT_NONE, since a fresh anonymous mapping would bring them back into the footprint. alias_initialize and SetHeapSize zero them when they are opened again, leaving alone the 4 KB pages that already read 0. Not helped: memory outside the block, such as Unreal's heap on a host malloc zone, so OCTOPATH TRAVELER II (seedds#1226, seedds#925) gets no change from this rule. Measured: Mario Kart World 1.0.0 in handheld mode on a 16 GB M5 Mac, with the files forced on, on 7b0b09a with this hle_svc.c (main's shader libraries are not on disk): footprint 6.87 GB in the race against 12 GB anonymous, p50 16.7 ms, longest frame near the end of the run 201 ms against 93 ms, 6.0 GB on disk and 9.15 GB of logical writes in 5.5 minutes. Three runs with files reached frames 11,700, 14,100 and 12,300 in the time the one anonymous run reached 14,100, so slower loading is not ruled out. In seedds#1184 two people merged the switch2ios fork's version by hand, and Mario Kart World finished races on an iPhone Duo and an iPhone 18 Pro (4.41 GB at most over 13 minutes and two races). Those runs also had a larger texture view cache, which is not on main, and their own guest_release and mem_any changes. Not run on a phone: this code and the automatic rule. Checked: make -C dm/host/tests run with GAME=0400C3F00006E000 and 010015100B514000, with the memory tests anonymous, in one file and in 6 files, and a new test of the rule; asan and tsan memory-tsan report nothing; hle_svc.c compiles for the iPhone SDK; a low disk and a bad folder leave the guest anonymous; test_docs and the symptom index pass. regress.py not run: on the Mac the default path differs from main only in tests of g_gf_n. Based on clabeuhtegrite's design and code in PR seedds#1315, without its fault retry cap, stale-file cleanup and one-file path. Front-ends: new: an iOS front end with its own launcher sets DM_GUEST_FILE_DIR to its temporary folder, as dm/ios/Sources/main.m does; a game whose memory pool is larger than the room to the memory limit now writes guest memory there, up to what it has touched, and exits with code 87 when the disk falls under DM_GUEST_FILE_MIN_FREE. Co-authored-by: clabeuhtegrite <82087430+clabeuhtegrite@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
On an iPhone, iOS ends the app once its footprint reaches the per-process limit, and every guest page the game has touched is anonymous memory, which
phys_footprintcharges and which iOS cannot write out as a Mac swaps it. Mario Kart World is ended that way on 8 GiB iPhones, at course load or about 30 s in (#1184; #1237 looks like the same limit), and at 90 s on a 12 GB iPhone in LiveContainer, whose limit is 6,143 MiB.DM_GUEST_FILE=1(off by default) puts the guest block in sparse files, unlinked at once and mapped shared over the reservation at the same addresses, inDM_GUEST_FILE_DIR(the iOS app setsNSTemporaryDirectory()). File pages take the same memory while they stay in it, but they are out of the footprint, and the kernel writes them out when it needs the room.DM_GUEST_FILE_CHUNK=<size>uses several files, because the stall at the system's periodic flush grows with the file.[RED]line, when the files cannot be made, unlinked, sized or mapped; on iOS when their protection class cannot be C; and on the Mac when the disk has less thanDM_GUEST_FILE_MIN_FREE(5 GB by default) plus 8 GB free.guest_releaseonly makes the files' rangesPROT_NONE, since a fresh anonymous mapping would bring them back into the footprint.alias_initializeandSetHeapSizezero them when they are opened again, leaving alone the 4 KB pages that already read 0.DM_FAULT_RETRY_CAPturns an endless fault loop, on a page that does not come in, into a crash with a[RED]line.The commit message and the new
docs/reference/memory.mdentry give the details. In #1184, Th0m45k and hayupadhyaya merged this change by hand from the switch2ios fork. This PR writes it on currentmain, as one commit on 056e41f that applies without conflict.For #1184.
Checked
Tests: on a MacBook Air M5 (macOS 27.0.1, Xcode 27.2), as CI runs them.
make -C dm/host/tests GAME=0400C3F00006E000 -j4 run, and the same withGAME=010015100B514000: 2,194 checks each, 0 failures. The memory SVC tests ran with the guest in one file and in 6 files.asan: 0 failures, and no ASan, UBSan or LSan report. It ran from a path without a space, becausedm/sanitizers/sanitizers.mkreads a path unquoted.tsan memory-tsanandtsan-threads: 0 failures and no TSan report.memory-tsanruns with the guest anonymous, so TSan does not cover the files.dm/gfx/tex/testsasan, coverage, fuzz.On Linux, with stand-ins for the Darwin calls,
t_svc_memorypasses (with the newheap_regrow) with the guest anonymous, in one file and in 64 MB files, with and without the map limit.make -C dm/host/tests memory-file, which is part ofrun, runs the memory tests with the guest in files.docs_symptoms.py --checkreports the index as current.Games run: Mario Kart World 1.0.0 (0400C3F00006E000), in handheld mode.
DM_GUEST_FILE=1and 1 GB files, plus a larger texture view cache and the profile's map limit (separate changes). On the Mac, with the iPhone's settings, an 18-minute session (title, menus, a race) and a 30-minute one (a cup and free roam) peaked at 4,365 and 4,792 MiB of footprint, with no drift. The same build plays on the 12 GB iPhone.Devices and OS: MacBook Air M5, 16 GB, macOS 27.0.1; a 12 GB iPhone (A20 Pro), iOS 27.3, in LiveContainer.
regress.py: none run. The switch is off by default, and with it unset the host takes the paths it took before:
guest_release,alias_initializeandSetHeapSizetest for the files, andDM_FAULT_RETRY_CAPis off. The only difference is that the[mem] alias region mappedline gains what was given back and zeroed so far.Not checked: a game run on this exact commit, on 056e41f. The switch stays off by default: turning it on for iOS, or offering it in Mario Kart World's settings, is your call.
Docs
docs/reference/memory.md("iOS ends the app once the guest's touched pages reach its memory limit"), with Mario Kart World's note and the symptom index (docs_symptoms.pyrun).docs/env_switches.tsvlists the five new switches.🤖 Generated with Claude Code
https://claude.ai/code/session_01MzPJouRkJtwacjUv5vhZN8