Repository navigation
feat(libs): let relinked macOS games opt into mimalloc - #11130
Conversation
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed the exact head d7a5770be493ed27017440cc2b2cf012ac766590. The constructor only restarts when APS5_MIMALLOC is present, mimalloc is not already loaded, the dylib is beside libkernel, and the restart marker is absent; it preserves argv and the prior DYLD_INSERT_LIBRARIES, and the marker prevents a restart loop if dyld ignores the insertion. The macOS tests cover disabled, enabled (malloc/new/new[] allocations resolve to mimalloc), and already-restarted cases. All hosted relinker jobs now pass on Linux, Windows, ARM macOS and x86_64 macOS; ASan passes, while product/library jobs are skipped by path policy. I could not run the macOS runtime tests on this host; the PR reports 85/86 focused libkernel tests passing on the same M4 Pro/Rosetta setup, with the lone unrelated app0.zar mmap failure. The performance traces compare the same dependency build with the allocator toggle off/on because native main cannot boot this title; the body states that limitation rather than presenting it as a pure main-versus-PR comparison.
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
@imellag #10809 changes the Windows direct-memory paragraph in AI-assisted: yes (Claude Code) |
jotitadev
left a comment
There was a problem hiding this comment.
Ran head d7a5770be on a second Apple Silicon machine. The review above couldn't run the macOS runtime tests.
Machine: Apple M2 Max (32 GB), macOS 27.2 (26B5112a), Apple clang 21.0.0, x86_64 Release under Rosetta 2 (-DCMAKE_OSX_ARCHITECTURES=x86_64, Ninja, BUILD_TESTING=ON, Python 3.9 from Xcode).
Base: main b986c11a34 does not build the libraries on macOS by itself, so the tree is that main plus the open macOS build PRs (#10366, #7631, #10659, #10660, #11249, #11358, #10669, #10552, #7804, #10676, #11447, #11437) and #11554, plus #11705 (applied locally). Only this PR is toggled, with git merge --no-commit on top.
without MimallocMacOs.cpp (test kept) |
with this PR | |
|---|---|---|
mimalloc_restart_off |
pass | pass, 3/3 |
mimalloc_restart_on |
fail: APS5_MIMALLOC did not load mimalloc |
pass, 3/3 |
mimalloc_restart_once |
fail (the restart marker stays in the environment) | pass, 3/3 |
libmimalloc.dylib(x86_64) is copied to bothlibs/andlibs/unpatched/.- Non-GPU suite (
ctest -E "^agc|gpu|vulkan|video_out", 1372 tests): 78 fail on the base and the same 78 fail with this PR. - The same suite with
APS5_MIMALLOC=1in the environment, so every test that loads libkernel restarts itself throughDYLD_INSERT_LIBRARIES(I confirmed withDYLD_PRINT_LIBRARIESthatlibmimalloc.dylibloads): the same 78 fail, nothing new, and nodyld did not loadline.
The merge with this tree is clean. #10809's ordering doesn't affect the build.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
|
@jotitadev Thanks for the runtime run on the M2 Max, and for running the whole non-GPU suite with |
d7a5770 to
4c75091
Compare
|
Rebased onto current Checked on the rebased head: the restart guard (opt-in variable, loaded check through |
What
Relinked macOS games can opt into mimalloc with
APS5_MIMALLOC, as Windows games can since #6067.libmimalloc.dylibbeside the libraries. It is built from the existing submodule with mimalloc's macOS defaults (interposed allocation functions andoperator new/delete, plus the default malloc zone) andMI_FREE_IS_CHECKED=SHARED, as on Windows.APS5_MIMALLOCis set and mimalloc is not loaded, a constructor in libkernel (MimallocMacOs.cpp) puts thelibmimalloc.dylibnext to libkernel first inDYLD_INSERT_LIBRARIES, setsAPS5_MIMALLOC_RESTARTEDand runs the executable again withexecv(same arguments, same process ID). If dyld ignores the variable (hardened runtime), the restarted process prints one line and keeps the system allocator instead of restarting again.Why: on macOS most of a frame is CPU translation in the AGC queue worker and the draw committer, and under Rosetta libmalloc's nano allocator keeps taking its slow path. In a 15 s Time Profiler capture of Planet of Lana on the worker,
operator newis 3,099 of 12,254 samples, 1,889 of them innanov2_allocate_outlined. Both driver threads allocate on every draw.Tested
macOS 27, M4 Pro, Rosetta 2, MoltenVK 1.4.2.
maindoes not boot a title on macOS yet, so the build ismain61fc572 plus the macOS changes needed to build and boot one: open macOS PRs (among them #10366, #10552, #7631, #7804, #10889, #10898, #11047, #11048 and #11049) and, for the AGC driver files and a few tests that no open PR covers yet, local patches taken from #8658's branch; plus this PR. Both sides are the same build; onlyAPS5_MIMALLOCchanges.Planet of Lana (PPSA19450), village gameplay from New Game with the same scripted pad input, 120 s per run, fps from 85 s, runs alternated:
APS5_MIMALLOCunsetAPS5_MIMALLOC=1Run 2 with mimalloc stalled in
VideoOut.Flip.presentfor 400 to 550 ms per frame from 50 s on, with GPU busy at about half its usual share. Runs without mimalloc dropped the same way on this bench earlier today (14 and 20 fps), while the Mac was in use. Run 2 had no mimalloc error, and run 4 repeated the pair.tools/trace_digest.pyofAPS5_TRACE_SECONDS=95:10 APS5_TRACE_EVENTS=1048576captures, same build and scene (traces, summaries and full digests linked below):APS5_MIMALLOC=1aps5-summary.jsonllinesunset:
{"commit":"61fc572ceef8","dirty":true,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_EVENTS":"1048576","APS5_TRACE_SECONDS":"95:10","APS5_VIDEOOUT_TELEMETRY_DIR":"<path>"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":12,"memory_mib":24576,"title_id":"PPSA19450","title":"Planet of Lana","content_version":"01.010.000","resolution":"3840x2160","window":{"spec":"seconds 95:10","first_present":4177,"ms":10025.605},"presents":290,"frame_ms":{"p50":34.383,"p90":36.233,"p99":44.063,"max":52.824},"gpu_busy_ms_per_present":18.488,"worker_busy_pct":{"queue worker 0x0":83.231},"top_phases_ms_per_frame":{"Driver.Packet":28.476,"Driver.Packet.draw":23.195,"Driver.Draw":23.051,"Graphics.Draw":19.788,"Driver.Draw.shader_compile_and_link":15.168,"Graphics.Draw.draw_and_resource_release":14.359,"Shader.DrawStage":8.796,"VideoOut.Flip":5.221},"trace_dropped":false,"trace":"aps5-trace-61fc572ceef8-85501.json.gz"}APS5_MIMALLOC=1:{"commit":"61fc572ceef8","dirty":true,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_MIMALLOC":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_EVENTS":"1048576","APS5_TRACE_SECONDS":"95:10","APS5_VIDEOOUT_TELEMETRY_DIR":"<path>"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":12,"memory_mib":24576,"title_id":"PPSA19450","title":"Planet of Lana","content_version":"01.010.000","resolution":"3840x2160","window":{"spec":"seconds 95:10","first_present":4305,"ms":10027.395},"presents":331,"frame_ms":{"p50":30.116,"p90":31.939,"p99":37.817,"max":48.366},"gpu_busy_ms_per_present":21.238,"worker_busy_pct":{"queue worker 0x0":74.751},"top_phases_ms_per_frame":{"Driver.Packet":22.229,"Driver.Packet.draw":16.748,"Driver.Draw":16.592,"Graphics.Draw":16.490,"Graphics.Draw.draw_and_resource_release":12.556,"Driver.Draw.shader_compile_and_link":9.470,"VideoOut.Flip":7.147,"Driver.Present":6.333},"trace_dropped":false,"trace":"aps5-trace-61fc572ceef8-89254.json.gz"}Driver.Packetgoes from 28.5 to 22.2 ms per frame andGraphics.Drawfrom 19.8 to 16.5. The queue worker's busy time goes from 22.7 to 13.4 ms per frame and the draw committer's from 14.0 to 9.7.Physical footprint (
footprint) near the end of a run: 10,096 and 11,015 MiB unset, 10,933 MiB with mimalloc.MIMALLOC_VERBOSE=1shows mimalloc in the restarted process, andvmmapshowslibmimalloc.dylibloaded fromlibs/. Nomimalloc: errorline in the runs.0x2f7...), outside the guest arena's range (0x2_0000_0000to0xFC_0000_0000).mimalloc_restart_off,mimalloc_restart_on(malloc,newandnew[]return mimalloc memory after the restart) andmimalloc_restart_once(no second restart) pass on macOS. Of the 86 libkernel-related ctest cases (mimalloc|kernel|guest_(system|process|posix|memory|apr|sleep|exit|tls|thread)) on the same build, 85 pass.guest_kernel_app_archive_mmapaborts withmmap on a file read from app0.zar, fd=4 not implemented; it does not setAPS5_MIMALLOC, so the new constructor returns before doing anything.__APPLE__and their CMake paths are unchanged; not built here.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)