Repository navigation
Title heap hands back memory it frees: the SDK's heap fixes as patches/sdk (#75) - #110
Conversation
…trim after each request The title heap is the payload SDK's (dlmalloc in eight arenas over direct memory). At the pin (b83202be) it maps segments back to back, dlmalloc joins them, and a joined segment can only go back whole, so one block left near its end keeps everything below it. patches/sdk holds four later commits of the SDK by its author, path-filtered to the heap: 8278ca5e (calloc clears recycled memory), 3752be7e (arena locks block instead of spinning), 9d048784 (2 MiB segments) and 55a2de04 (a 64 KiB gap so segments never join, ps5_heap_trim, the heap's in-use figure). build-app.sh exports the pin, applies them with patch --fuzz=0, builds heap.o and mspace.o with the SDK's Makefile into libps5lm_heap.a, links it ahead of libps5platform.a and checks --why-extract that the stock members stayed out. The pinned checkout and the SDK cache stay as pinned. test-native-build.sh runs the SDK's host tests on the patched copy and records the patches' sha256s in result.json. The app (heap_info.cpp, the only includer of ps5platform/heap.h): - trims 0.25 s after each POST request ends (activity hook) and every 10 s; a fleet worker every 10 s - request files: trim (now, on, off), memory each / memory off - the memory line is built as one string and ends with exact MiB: largest block, heap mapped and segments, in use and the walk's time, trim time, requests ended and in flight - the stats thread no longer walks the heap once a second
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b084150fea
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } else if (!after_request && ps5lm::heap_can_trim()) { | ||
| ps5lm::heap_figures(); // the dashboard's heap figure (stats.cpp), at most 10 s old | ||
| } | ||
| last_trim = now; |
There was a problem hiding this comment.
Refresh cached heap figures independently of request trims
With the default memory_each == false, if requests keep completing less than 10 seconds apart, each request-triggered trim skips heap_figures() but still resets last_trim. This prevents the periodic refresh from running, leaving the cached heap figures stale indefinitely rather than at most 10 seconds old. StatsCollector::run() now reads only heap_cheap(), and write_report() consumes its cached heap_gib, so diagnostic reports can retain the ready-time heap size throughout a busy run and hide subsequent growth. Use a separate snapshot-refresh deadline, or refresh the figures after request-triggered trims too.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed in 564b769: the heap read now has its own 10 s deadline, separate from the trim's, so requests ending under 10 s apart no longer leave the dashboard and report figure stale.
Requests ending under 10 s apart each reset the periodic trim's clock, and the trim after a request did not read the heap, so the dashboard's and the report's heap figure stayed at the last periodic read for as long as the run kept going. The read now has its own 10 s deadline.
# Conflicts: # CHANGELOG.md
cobanov
left a comment
There was a problem hiding this comment.
Thanks @Adon1s, this is a careful port. I checked the four patches against mihawk-99's commits. 0002–0004 match them line for line on the heap paths, and 0001 leaves out only the shm and vrange checks, as its message says. The licensing is fine: GPL-3.0-or-later SDK code in a GPL-3.0 repo, with the authorship kept. Exporting and patching instead of touching the pin, the --why-extract check and the #error guard on the larger stats struct are all the right calls, and CI shows the SDK's host tests passing on the patched copy. 564b769 fixes Codex's stale-figure point correctly.
I merged main into the branch (a CHANGELOG conflict only, both entries kept) and will merge once CI is green on it.
Two small things for a follow-up:
patches/sdk/README.md: please name the licence and the author (GPL-3.0-or-later, Mihawk), aspatches/mesa/README.mddoes. Please also say thatc32d2beis left out on purpose: it's payload ForApp direct memory, the only other heap commit between the pin and55a2de04, and a title doesn't need it.docs/BUILDING.md:110: addpatches/sdk(the title heap) to thepatches/row of the layout table.
@MCsumz's long-chat run on main this weekend will cover the text-only side. A quick fleet-worker check before the next release would close the last gap.
# Conflicts: # CHANGELOG.md
…stream docs/UPSTREAM-TESTS.md: the 27 November runs, main 5e96235 on the PS5 Pro and main + cobanov#114 + cobanov#117 on the PS5 Fat. The README's fix table now says where each fix is upstream: the heap (cobanov#110), the keep-alive fix (with the llama.cpp move, cobanov#103), the Qwen3.5 cache (cobanov#88) and the media marker (cobanov#112) are on main; the scratch fix is next. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Long-chat run on
Every request was answered. The heap stays bounded on both: in use grows ~30 MiB over the whole chat and not at all during the reprocesses, and the Pro ran it with 0.43 GiB left. On the Fat, most of the 0.3 GiB the pool loses is outside the heap, where BR Marcell |
With the title heap (cobanov#110) upstream main writes "memory after a request" and "memory after the trim" once "memory each" is on, with the flexible pool and "in use" in the line, so the script printed n/a there. It now takes those lines as well (memfix's still work), waits for the trim and the log write, and prints the pool's largest free block when the line has it. The docstring says to turn the lines on after the model is ready, since a load restarts the app. Used for the cobanov#110 long-chat run on 27 November. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@cobanov fleet-worker check with #110, the last gap from your review. PS5 Fat as an RPC worker on main 5e96235 (+ #114, #117),
Every request was answered; a full 49k reprocess took 176 s over RPC. The worker's heap stops growing by turn 10 (56 MiB in use from there to the end), and the pool stays level across the reprocesses after a one-time 0.2 GiB step at the first. Worker mode in, out ( BR Marcell |
What happens
Over a run of photo requests the CPU heap grows and the pool's largest free block shrinks, and it does not come back
between requests. On upstream main (
fa0fc59), 25 full-size photos took the largest block from 1,669 to 1,055 MiB(−614) while the heap grew from 983 to 1,321 MiB (+338). MCsumz sees the same thing with long chats (#75).
Cause
The title heap is the payload SDK's: dlmalloc in eight arenas over direct memory. At the pinned SDK (
b83202be) itmaps its segments back to back, and dlmalloc joins neighbours into one segment. A joined segment can only go back to
the pool whole, so one block left near its end keeps everything below it. Memory a request frees stays in the heap,
where the GPU cannot use it.
Change
As asked in #75, the SDK author's fixes are carried as patches instead of moving the pin:
patches/sdk/: four later commits ofmihawk-99/PS5_PayloadSDKby its author, path-filtered to the heap and itshost tests.
8278ca5e(calloc clears reused memory),3752be7e(arena locks wait instead of spinning),9d048784(2 MiB segments instead of 16),55a2de04(a 64 KiB gap so segments never join,ps5_heap_trim(), theheap's in-use figure). Each patch says which commit it comes from and what of it is left out.
BASEnames the pinthey apply to.
scripts/build-app.sh: exports the pin withgit archive, applies the patches withpatch --fuzz=0, buildsheap.oandmspace.owith the SDK's own Makefile intolibps5lm_heap.a, and links that ahead oflibps5platform.a. lld's--why-extractreport is checked, so the build stops if the stock heap gets in. Thepinned checkout and the shared SDK cache are never modified, so
fetch_pinned's checks still hold.scripts/test-native-build.sh: runs the SDK's own host tests (make -C platform test) on a patched export first(198 of 198 pass), and records the patches' sha256s in
result.json.ps5/app/heap_info.cpp(the only file that includesps5platform/heap.hnow): the app trims the heap 0.25 s aftereach llama-server request ends (from the activity hook) and every 10 s; a fleet worker every 10 s (not tested in
worker mode). The trim does not wait for a gap between requests, because a run of photos never leaves one.
loop's figure, at most 10 s old.
[MiB: largest 1316, heap 1100 in 63 segments, in use 939, walk 6.0 ms, requests 25, in flight 0]. Its start isunchanged for
scripts/bench-ubatch.py.memory each/memory off(a memory line after every request and after its trim),trim(trim now, between two memory lines),trim on/trim off. Inscripts/ps5lm-app.shanddocs/CONSOLE.md.When the pin moves to
55a2de04or later,patches/sdk/can be deleted and the rest still works.Tested
On a PS5 Slim, stock title PPSA99575 built from each commit, Qwen 3.8 27B UD-IQ2_S with its F16 projector, 2k
context,
--image-max-tokens 1280, screen on. The same 25 full-size photos, one at a time, with a memory lineafter each (MiB):
fa0fc59So 261 MiB more of the pool is free in one piece after 25 photos, and the heap holds 221 MiB less. With the patches
the heap's in-use figure shows what is left: 854 MiB in use when ready, 939 after the run.
What this does not fix:
did not grow there (1,088 → 1,086): that is the vision encoder's GPU buffers growing, which the vision warm-up PR
from Photo runs lose ~0.6 GiB in their first 25 images: the title heap and the vision warm-up (measured, with fixes) #75 is for.
space it keeps sits in segments that still hold a block, and in each arena's last segment, which dlmalloc never
gives back.
In my fork the same patches have run since 2026-11-11 on the console, through 100-photo runs, Q2_K_XL with vision
and long text loads, with no errors.
How to test
build-app.shprintsbuild-app: title heap from patches/sdk (4 patches on SDK b83202be...),and
app.loghasheap: patches/sdk, empty segments go back to the pool after each request and every 10 s.scripts/ps5lm-app.sh memory each, then run requests: each one logsmemory after a requestandmemory after the trim.scripts/ps5lm-app.sh trim offturns the trim off to compare;trim onturns it back on.