fix(libkernel): retain and reuse Linux guest thread stacks #7838
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7838"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T10:07:47Z.
What
Implements the Linux follow-up from #7700. Joined guest thread stacks and stacks reaped after detached threads remain mapped after completion and can be reused by later threads with the same mapping size. The cache is bounded to 1/1024 of detected physical RAM, clamped to 4–64 MiB; adding a mapping beyond that limit evicts the oldest cached mappings. Before a finished stack enters the cache, Linux discards its usable pages with
MADV_DONTNEEDwhile retaining the virtual mapping and guard; if page discard fails, that mapping is unmapped instead of cached. Reuse restores read/write access and reapplies the no-access guard; if a fresh mapping fails, cached finished stacks are released before retrying.Tested
Rebased onto upstream
mainb85d972f42at PR head76a9900450. On that exact head,guest_pthread_stackpassed 5/5 on Linux x86_64 (i7-13700K, Ubuntu 24.04, GCC 13.3, Release), and all 27^guest_pthreadtests passed 3/3 with-j4. Theguest_pthread_stack_teststarget also builds with GCC 12.2/Ninja on this Xeon E5-1620 v2, but its CTest run aborts before assertions at the project CPU guard (missing LZCNT, BMI1/2, MOVBE, AVX2 and FMA). GCC syntax-only,git diff --check, and conventions pass.4efaf651804f65b6e41f62266aa513a121b70abd(before the laterMADV_DONTNEEDand detached-stack test changes): the then-currentguest_pthread_stackpassed 3/3. Using the same temporary measurement fixture against main ancestore004d03ceb460c5b802b7905a8a88a2a3db26b46and this PR, 17 joined stack sizes from 4–20 MiB across three rounds measured peak RSS of 25.3–25.8 MiB on the ancestor and 82.0–82.6 MiB on this PR; after each round the PR remained around 48.5 MiB RSS. The cache cap on that host is 60.4 MiB. Evictions unmap cached mappings, the unmap observer confirms no host-only stack registration remains, and the eviction log appears once per process. With a temporary unlimited cap, the same workload retained about 223.6 MiB and failed the expected-unmap assertion. This cap bounds cached mappings, not total process RSS or concurrent live stacks. No title or GPU run was performed.a45b29fedafound the residency assertion used an unalignedmincorerange; commitc07bfab494rounded the queried range to page boundaries. On supported Linux,c07bfab494then passed the residency check but failed the marker assertions because cached pages are intentionally zeroed byMADV_DONTNEED. Commit49b47f9711now verifies the worker writes the marker while live, then verifies after exit that the mapping remains registered/readable/writable and the marker page is zero. The final detached-stack assertion change was later covered by the exact-head supported-host run above.guest_pthread_stack_teststarget builds, butguest_pthread_stackaborts before assertions because the CPU lacks LZCNT, BMI1/2, MOVBE, AVX2 and FMA. CurrentGuestPthreadStack.cpppasses GCC syntax-only;git diff --checkandpython3 tools/check_conventions.py --base upstream/mainpass.d472eda748, a supported-host rerun found detached stack pages still contained their marker immediately after the host thread exited. Detached stacks are retired first and reaped at the next guest-thread creation; commit76a9900450moves the page-discard assertions after the existing detached-reuse creation, which triggers that reap. The updated test is covered by the exact-head supported-host run above.ece11f830passed 3/3 each forguest_pthread_guard_size,guest_pthread_attr,guest_pthread_attr_stackandguest_pthread_stack; those are unrelated guard-size tests and do not validate this Linux cache change.python3 tools/pr_overlap.py --branch codex/linux-thread-stack-reuse-7700reported shared files it could not compare for #3986, #5021, #6044, #6879, #7149, #7201 and #7332. Manual patch review found distinct platform/features: signal masks, Windows stack handling, WebAssembly and macOS support, test organization, and AOT. No other PR implements the bounded Linux spare-stack cache; sharedThread.cppand test paths may need coordination if those PRs merge first.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)Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T11:10:05Z.
Review: COMMENTED
Ran this on Linux with the unmodified binary, since the container in Tested couldn't run it.
Exact head
916bdb06a6d3801da4700829d90fefff09b79d97, merged onmain674c25388. Linux x86_64, i7-8700 (AVX2/BMI2), Ubuntu 26.04, GCC 15.2, CMake 4.2.3, Ninja, Release,BUILD_TESTING=ON, full build.guest_pthread_stackpasses, 50 of 50 serial runs. The 42pthread|threadtests pass, 5 rounds at-j6.GuestPthreadStack.cppbuilt againstmainaborts, as expected, since the stacks are not retained there.main.guest_pthread_stack_tests× 25 rounds: 0/200 failures.GuestAllocationsLookupreports it read/write, then fills 4 KiB at the deep end, sleeps 50–250 µs and checks the fill is still there. 5 of 5 runs passed./proc/self/mapsstays at about 145–153 lines from round to round (about 90–97 onmain), and RSS is about 0.6 MB abovemain, so the spare list does not keep growing with this pattern.The PR drops the old test's unmap observer (
GuestThreadStackHostRangeOverlapsat unmap time). Unmapping now only happens inReleaseSpareStacksand on the failed-reprotect path, and the new test doesn't reach either one.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-19T12:28:40Z.
@jotitadev I exercised the two cleanup paths your review identifies, on exact head
916bdb06a6d3801da4700829d90fefff09b79d97(without merging another main revision).Ubuntu 24.04 x86_64, Ryzen 9 9950X, GCC 13.3, Release,
BUILD_TESTING=ON. The host CPU check stays intact. This is a native homebrew thread test, with no title or GPU run.Temporary fault injection was confined to the stack allocator's calls: return a failure before the selected
DoMapAnonor spare-stackDoMprotect, then let the existing cleanup/retry code run. Six joined 80 KiB guest threads first leave five cached mappings, covering both the spare list and its unfinished collection batch. I restored the unmap observer'sGuestThreadStackHostRangeOverlapscheck for every callback.Each case passed in 3/3 fresh processes:
scePthreadCreate: cannot map the guest thread stack; the caller's handle remains nullEvery unmap callback saw no overlapping host-only stack registration. The five-mapping releases produced 15 callbacks, and the one-mapping releases three: guard piece, writable piece, then whole range. After clearing injection, a further guest thread also ran and joined successfully in every process; its stack was still registered read/write and retained its low-end marker.
Two independent negative controls fail the temporary test: removing
ReleaseSpareStacks()from the mapping retry, and omittingDoMunmapfor the failed spare. Both lose the required unmap callbacks. These controls check that the failure-path assertions actually execute.Both edited files are restored byte-for-byte, the checkout is clean at the same head, and the rebuilt, unmodified
guest_pthread_stackpasses 3/3 again. This covers the joined-stack cleanup paths above; it does not simulate kernel memory pressure, detached-thread races or title behavior, and adds no production test hook or implementation change.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T16:52:05Z.
Thank you both for the independent Linux validation. The exact-head fault-injection runs cover the fresh-map and spare-stack cleanup paths, and the unmodified binary also passed 50 serial runs, five rounds of the thread tests, and the 776-test non-GPU suite. I understand these are native host tests, not title or GPU validation. I will retain that scope when describing the evidence.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T17:49:18Z.
Review: CHANGES_REQUESTED
Thanks for the work, and for the independent Linux runs. Two things block this:
core/libs/prx/libkernel/Pthread/src/Thread.cpp:119(spareStacks) has no bound. Stacks only leave it when a laterDoMapAnonfails (ReleaseSpareStacks), andTakeSpareStackmatches the exact mapping size, so a stack size that is not requested again stays mapped, with its touched pages resident, for the whole session. Please add a cap (entries or bytes, derived from the host, logging once when hit) and release the pages of cached stacks (e.g.madvise(MADV_DONTNEED)on the usable part) or trim on a size mismatch.Also, the test drops the
GuestThreadStackHostRangeOverlaps/ unmap-observer check that guarded the retired-stack host-only pages. Please keep a check that a stack that is really unmapped (cap hit or released) leaves no host-only pages.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T19:16:34Z.
I agree the current cache has no bound and can retain touched pages for the process lifetime when stack sizes are not reused. The previous stress result (about 0.6 MB RSS above main) is not a before/after peak-RSS measurement on the requested varied-size create/join workload, and the fault-injection run did not exercise cache eviction. I have not implemented a cap or
madvisepath yet, so the resource bound and the unmap-observer assertion remain unresolved; I am not claiming this review is addressed. The existing Linux stress results only cover reuse at the exact tested stack sizes.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T19:23:21Z.
I pushed two commits on current
main:ef7f7bd60capsspareStacksat 1/1024 of detected physical RAM, clamped to 4–64 MiB, evicts oldest mappings over the cap, and logs only on the first eviction. Evicted stacks go through the unmap path. The cache also retains the original contents while a mapping remains cached, so the existing post-exit preservation assertions still apply.4efaf6518restores an unmap observer that checksGuestThreadStackHostRangeOverlapson every callback and adds 17 joined workloads with distinct 4–20 MiB stacks that touch their stack pages, forcing eviction even at the 64 MiB cap.The focused
guest_pthread_stack_teststarget builds on Linux, andgit diff --checkpluspython3 tools/check_conventions.py --base upstream/mainpass. I ran the focused CTest; it aborts at startup in the project CPU feature check because this Xeon E5-1620 v2 lacks LZCNT, BMI1/2, MOVBE, AVX2, and FMA, before the stack workload runs. I therefore do not have a before/after peak-RSS result from this machine. The added varied-size workload and eviction/unmap assertions need a runtime run on a supported Linux CPU to close that evidence gap.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T19:26:47Z.
The normal PR workflow skips its Linux job, and the repository’s manual Linux build dispatch returned HTTP 403 (“Must have admin rights to Repository”) for this account. I will not bypass the project CPU guard locally. The latest pushed head builds here, but runtime validation and the requested before/after peak-RSS figures still need a supported Linux host. If someone with access to the project runner can run
guest_pthread_stackand measure the varied-size workload on both current main and this head, that would close the remaining evidence gap.Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-19T20:25:46Z.
Supported-CPU runtime and RSS results for exact head
4efaf651804f65b6e41f62266aa513a121b70abd, addressing the remaining Linux evidence request.Ubuntu 24.04, kernel 7.0.0-38, Ryzen 9 9950X, GCC 13.3, Release. The CPU feature guard is intact. The unmodified
guest_pthread_stackpasses, and passes 3/3 fresh executions again after the controls below are restored.For a like-for-like memory comparison I added a temporary measurement-only mode to the PR fixture, and compiled the same fixture source against:
e004d03ceb460c5b802b7905a8a88a2a3db26b46;4efaf651804f65b6e41f62266aa513a121b70abd.It invokes the existing
Workerfor 17 distinct requested stack sizes, 4–20 MiB in 1 MiB steps, creating and joining one thread at a time, for three rounds (51 guest threads/process).Workerdescends through the requested stack, touches its frames and checks their contents. This mode omits the PR-only post-exit retention assertions so main can run the identical workload. It retains the unmap observer and itsGuestThreadStackHostRangeOverlapsassertion. Three fresh processes per tree:Peaks were read with
getrusage(RUSAGE_SELF).ru_maxrssand/proc/self/statusVmHWM; they agree in all six normal measurements. On the PR, round-end RSS and the high-water mark stay flat after round 1. Every observed unmap has no host-only stack overlap. The eviction message appears once per process, despite further evictions in rounds 2 and 3.The host reports 15,839,079 physical pages × 4096 bytes, giving a cache limit of 63,356,316 bytes (60.4 MiB). That is the cached-mapping budget, not a process-RSS ceiling: live stacks and the rest of the process account for the higher transient peak.
Negative control: return
size_t::max()fromSpareStackLimit, leaving the rest of the PR unchanged. The same workload retains about 223.6 MiB (VmHWM229016 KiB), records zero unmap callbacks, and fails the measurement mode's required-unmap assertion after round 3. This run used line-buffered stdout to preserve the expected abort's measurements. It confirms the real cap/eviction path is exercised.All temporary fixture and production changes are restored byte-for-byte, the checkout is clean at the stated head, and the rebuilt stock test passes 3/3. This supplies the varied-size peak-RSS comparison and eviction/unmap evidence. It does not establish a general concurrent/live-thread RSS bound, detached-thread memory-pressure behavior, console batching semantics or NHL 26 behavior; no title was run. Logs and the identical measurement fixture are retained.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T20:26:33Z.
Thanks for the supported-CPU run and the controlled measurement. This fills the missing varied-size before/after evidence on exact head
4efaf651804f65b6e41f62266aa513a121b70abd. With 17 stack sizes from 4–20 MiB across three rounds, the main ancestor peaks at 25.3–25.8 MiB RSS, while this PR peaks at 82.0–82.6 MiB; after round 1 the PR remains around 48.5 MiB RSS. The cache cap on that host is 60.4 MiB, and over-cap entries are unmapped; the restored observer confirms no host-only stack overlap on every unmap. The unlimited-cap negative control retains about 223.6 MiB and triggers the expected assertion. I will make the PR description explicit that the cap bounds cached mappings, not total process RSS or concurrent live stacks, and include the measured memory cost. Could you reassess the remaining resource-bound request against this evidence and current head?Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T10:26:10Z.
I addressed the remaining cached-page concern in two commits on current head
2d0f8408fb:6d5c63a8e4appliesMADV_DONTNEEDto the usable pages before a finished Linux stack enters the cache (the guard page and virtual mapping remain), and unmaps the stack instead if discard fails;2d0f8408fbadds amincoreassertion that the 16 MiB joined stack is nonresident after completion. Both changed files pass GCC syntax-only checks with the existing Linux Release flags, and conventions plusgit diff --checkpass. I could not run the current-head binary here because this Xeon fails the required CPU feature guard. The prior RSS/runtime report is still for4efaf65180, so it does not validate these two commits. Could you review the page-discard behavior and rerun the focused test on a supported Linux host?Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T10:27:13Z.
Correction: I added a third focused test commit,
c608e48efb, so the current head isc608e48efb8baea481021c994d4ae5f8b588f7fc. The residency assertion now also acceptsmincorereturningENOMEMwhen the documentedmadvisefallback unmapped the stack; otherwise it requires every usable page to be nonresident. Both syntax-only checks, conventions andgit diff --checkpass on this head. Runtime validation is still pending on a supported Linux host.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T10:29:58Z.
I corrected the
mincoreloop ina45b29feda:scePthreadAttrGetstackreturns the usable range after the guard page, so the test now checks residency for every usable page, including index 0. GCC syntax-only checks for the test andThread.cpp, conventions, andgit diff --checkpass again. The runtime assertion still needs execution on a supported Linux CPU; hosted build jobs remain skipped.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T10:49:13Z.
@MarcZX10 a run of the current head on a supported CPU: Ubuntu 24.04 (Linux 6.8), i7-13700K, GCC 13.3, Release with
BUILD_TESTING=ON. Every targetctest -R '^guest_pthread'uses was built at the heada45b29fedaand at its merge basee004d03ceb, and each ran 3 times:guest_pthread_stackaborts in all three runs.The abort is the new residency check, at
Require(errno == ENOMEM).mincorefails withEINVAL, notENOMEM, because the address it gets isn't page-aligned. With a local print added:mincore errno 22 (Invalid argument) address 0x2006b0040 size 16777216 page 4096. So the usable rangescePthreadAttrGetstackreports for the 16 MiB stack starts 0x40 into a page.DiscardFinishedStackPagesitself is aligned, since it starts at the mapping base plusGUEST_STACK_GUARD. Only the test'smincorerange is off. RoundinglastGuestStackdown to the page, and the length up by the same amount, should make the call valid. I haven't changed or rerun it that way.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T17:51:37Z.
I fixed the
mincoretest failure in commitc07bfab4948592b9102a829d3ab6f65d9c63e5d7: the queried start address is now rounded down to a page boundary and the length is expanded to cover the full usable stack. This addresses the reportedEINVALfrom an unalignedscePthreadAttrGetstackaddress. GCC syntax-only, conventions, andgit diff --checkpass. I could not run the runtime test on this Xeon because it fails the project CPU feature guard, so the focusedguest_pthread_stackrun on a supported Linux CPU is still needed.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T18:05:11Z.
@MarcZX10 I ran c07bfab494 (merge base
e004d03ceb) on Ubuntu 24.04, i7-13700K, GCC 13.3, Release.guest_pthread_stackstill aborts, 3 of 3, but no longer in themincorecheck. The residency loop now runs and every page of the joined stack reads as not resident.The abort is now in
RequireRetainedfor the first joined probe, at the marker check (line 145). I added a print there locally:So the range is still registered and readable/writable, but the marker reads 0. Since 6d5c63a8e4, a finished stack's usable pages are discarded with
MADV_DONTNEEDbefore it is cached, and an anonymous private page reads back as zeros after that. So any marker the worker writes can only read 0 after the join or detach. When I changed that one expectation to 0 locally, the next abort was at line 211, the reuse probe's marker after its own join, for the same reason. The detached checks at lines 226 onward have the same pattern.The marker checks look like they predate the discard: the head measured in the description, 4efaf65180, comes before 6d5c63a8e4. One way to keep their meaning: after the join, expect the marker word to read 0 (that shows the pages were discarded but the mapping stayed), and keep
ReusesOneOfas the reuse check, since it compares stack addresses. I'm happy to rerun whichever way you go.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T18:10:51Z.
Thanks for the supported-host run and the marker trace. The page-aligned
mincorerange passes onc07bfab494, and the remaining failure is the expected zeroing fromMADV_DONTNEED. In49b47f9711, I now assert that the worker sees its marker before exit; after join/detach, the test checks that the mapping is still registered and readable/writable while the marker page reads as zero. Reuse is still checked by comparing the stack address with the earlier probes. GCC syntax-only, conventions andgit diff --checkpass here, but I could not run the emulator tests on this Xeon due to the CPU feature guard. Could you rerunguest_pthread_stackon the supported Linux host to verify this exact head?Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T18:11:30Z.
I rebased #7838 onto current
main8727ddb29eand force-pushed headd472eda748. The rebase was clean and retained only the intendedThread.cppand stack-test changes. On this head, GCC syntax-only, conventions, andgit diff --checkpass. The latest supported-host result is still forc07bfab494(the page-residency check passed, then marker assertions failed); commit49b47f9711changes those assertions, and this rebased head has not yet been run on a supported CPU. Could you rerunguest_pthread_stackond472eda748when convenient?Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T18:25:31Z.
@MarcZX10 I ran d472eda748 on the same host (i7-13700K, Ubuntu 24.04, GCC 13.3, Release).
guest_pthread_stackstill aborts, 3 of 3, but the joined half now passes. The abort is inRequireCachedStackDiscardedat the zero check (line 146), for the first detached probe. A local print in that function, the same on all three runs:So by the time
WaitForProbehas seen the detached thread's host task leave/proc/self/task, that thread's stack is still mapped, but its pages haven't been discarded yet. The description says detached stacks are reaped later, so the discard probably happens on the reap, not at exit. Either the test needs to trigger or wait for the reap before this check, or the detached path needs to discard at exit like the joined one. The joined probes all share stack 0x201838040, so joined reuse works.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T18:29:15Z.
Thanks for the supported-host trace on
d472eda748. It shows the detached stack is still inretiredStacksafter its host task exits;ReapRetiredStacks()runs at the next guest-thread creation. In76a9900450, I moved the detached page-discard assertions until after the existingdetachedReusethread is created and joined, which triggers that reap. The test now checks the detached mappings after the same cleanup path the implementation uses. I rebased onto currentmainb85d972f42; GCC syntax-only, conventions andgit diff --checkpass. I haven't run the updated test on a supported host; please rerun when convenient.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T18:31:56Z.
Validation update for exact head
76a9900450: theguest_pthread_stack_teststarget builds in the Linux GCC 12.2/Ninja build. CTest startsguest_pthread_stackbut this host's Xeon E5-1620 v2 aborts at the project's CPU guard before assertions (missing LZCNT, BMI1/2, MOVBE, AVX2 and FMA). I updated the description; the supported-host runtime result is still pending.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T18:45:15Z.
Update against the review: current head
76a9900450bounds the cache to 1/1024 of physical RAM, clamped to 4–64 MiB, evicts oldest mappings over the cap, and usesMADV_DONTNEEDon usable pages before caching (unmapping if discard fails). The test now cycles through 4–20 MiB stack sizes to force evictions and retains the unmap observer that checks no host-only stack range remains at unmap time. GCC syntax-only, conventions andgit diff --checkpass on this head.The available peak-RSS comparison is from
4efaf65180, before theMADV_DONTNEEDchange: 25.3–25.8 MiB on its main ancestor versus 82.0–82.6 MiB peak on that implementation, settling around 48.5 MiB with a 60.4 MiB cache cap. I am not treating that as exact-current-head evidence. The exact-head supported-CPU rerun is still pending; this Xeon stops at the project CPU guard before the test body.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T18:48:21Z.
@MarcZX10
76a9900450passes on the same host (i7-13700K, Ubuntu 24.04, GCC 13.3, Release).guest_pthread_stackpasses 5 of 5, and all 27^guest_pthreadtests pass 3 of 3 with-j4.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T19:19:28Z.
@milescoviello, thanks for validating exact head
76a9900450; the 5/5 stack test and 27 pthread tests across three rounds close the supported-host runtime gap. I updated the description to distinguish that exact-head result from the older RSS measurements. The earlier peak-RSS comparison predatesMADV_DONTNEED; if you have capacity, could you rerun the same varied-stack RSS fixture on76a9900450and its main ancestor? That would answer the remaining reviewer question about the final cache’s residency.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T19:20:03Z.
@adriadam10, the exact current head
76a9900450now has supported-host coverage:guest_pthread_stackpasses 5/5 and all 27^guest_pthreadtests pass 3/3 with-j4. That run includes the cache bound, page discard, eviction path and retained unmap observer. I updated the PR description with this result. It separately labels the existing before/after RSS numbers as pre-MADV_DONTNEED; I requested an exact-head RSS rerun to fill that remaining measurement gap.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T20:40:49Z.
The current PR head now addresses the bounded-cache and page-residency points: the cache is capped at 1/1024 physical RAM clamped to 4–64 MiB,
MADV_DONTNEEDdrops usable pages before caching, failed discards unmap instead, eviction/release unregisters mappings, and the description reports the earlier before/after RSS measurements and their limits. It also restores an unmap observer for evicted mappings; latest commits adjust detached-thread reaping before that assertion. The supported-host exact-head test report is in the PR description. Could you recheck the current head against the earlier requested cap/residency/unmap concerns?Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T21:01:09Z.
@adriadam10, could you reassess your
CHANGES_REQUESTEDreview against exact current head76a990045048c71e0effdaf107239061865506fa?The latest diff now has the requested cache cap (1/1024 physical RAM, clamped to 4–64 MiB),
MADV_DONTNEEDbefore caching with unmap-on-discard-failure, oldest-first eviction through the unmap path, and the unmap observer. The PR description reportsguest_pthread_stack5/5 and all 27 pthread tests 3/3 on a supported Linux host at this exact head. It keeps the older varied-size RSS comparison explicitly labeled as pre-MADV_DONTNEED, so that measurement is not presented as final-head evidence. The Xeon here cannot run past the CPU guard.Please let us know whether the current implementation and exact-head runtime report resolve the cap/residency/unmap concerns, or whether you still see a specific gap (including whether exact-head RSS is still required).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T22:39:58Z.
Thanks for the blocking review. Current head
76a990045048c71e0effdaf107239061865506fanow caps the spare-stack cache atclamp(physical memory / 1024, 4 MiB, 64 MiB), discards usable pages withMADV_DONTNEED, releases mappings when the cap is exceeded, and retains the host-only-range unmap observer in the test. The latest Linux report hasguest_pthread_stackpassing 5/5 and the 27^guest_pthreadtests passing 3/3 with-j4. I still don’t have a before/after peak-RSS measurement; does the explicit cache bound plus page-discard behavior and current test coverage resolve the requested memory concern, or do you still need those measurements?Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T23:04:33Z.
@MarcZX10 @adriadam10 here are before/after peak-RSS numbers for the exact head
76a9900450against its merge baseb85d972f42. The workload creates and joins many threads with varied stack sizes. It's a local test program linked likeguest_pthread_stack_tests, not part of the PR; the source is below. Setup: i7-13700K, 32 GB (so the cache limit is 32 MiB), Ubuntu 24.04 (Linux 6.8), GCC 13.3, Release, 3 runs per side.The phases run in this order:
/proc/self/statusb85d972f4276a9900450scePthread: spare stack cache reached its host-memory limit; releasing cached mappingsonce. No base run did.Not measured:
VmSizeincludes the guest address-space reservation (about 968 GiB), so 32 MiB doesn't show.Workload source (GuestPthreadStackRss.cpp), built with
add_test_executableand linked tolibkernel libcas inlibkernel/CMakeLists.txt, run asguest_pthread_stack_rss_bench 500 8 100AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T23:13:14Z.
Thanks, this closes the measurement gap I raised. The exact-head RSS data shows the bounded cache returns resident memory to baseline after joined workloads, handles 400 one-off sizes without retaining resident pages, and keeps the varied joined workload time within the base spread. The detached-phase peak is present on both sides and tracks unreaped thread stacks, so it does not point to cache growth. I consider the memory concern addressed on this host; the unmeasured virtual cache size and other hosts are useful limits to keep in the report.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T00:14:05Z.
Review: COMMENTED
Thanks for adding the exact-head RSS comparison. The bounded cache plus
MADV_DONTNEEDreturns resident memory to baseline after joined workloads and handles the 400 one-off sizes without retained resident pages; the detached-thread peak appears on both base and PR head. That closes the memory concern I raised on this host. The measurements remain specific to the reported Linux machine, and hosted runtime jobs are skipped here.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T01:43:44Z.
The exact-head RSS report above addresses the requested workload: joined varied-size and 400 one-off-size stacks return RSS to baseline on the capped cache head, and the measured peak is within the base run-to-run range. The detached-thread peak is also present on base and is attributed to unreaped stacks. The retained unmap-observer check is still in the test. Could you recheck the memory and unmap-test points against head
76a9900450and update the review if these resolve them? I understand the hosted runtime jobs are skipped, and this report is specific to the stated Linux host.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T00:19:00Z.
Review: COMMENTED
Re-validated on Linux at the new head
76a990045(I reviewed916bdb06aearlier; the force-push adds the spare-stack cache's host-memory cap and the discard of a finished stack's pages, with newguest_pthread_stackcases for both).Machine: Linux x86-64, i7-8700 (AVX2), Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON. Base:maina6dcea9279, this PR merged with--no-commiton top (onlyPthread/src/Thread.cppandtests/GuestPthreadStack.cppchange).ctest -R '^guest_pthread_stack$'passes with the PR (the reuse and cached-page-discard checks: a joined/detached stack is reused, andRequireCachedStackDiscardedsees the marker cleared and the pages non-resident viamincore).Thread.cpptomainand keeping the PR's test, the test aborts (SIGABRT, exit 134):mainmaps a fresh stack per thread and releases it on exit, so there is no retained cache for the new checks to find.So the added cache-cap and page-discard code builds and runs on Linux, and the new test fails without it. CI does not run this suite on real hardware and your host can't (no AVX2), so this is a first run of the updated test on Linux x86-64.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-22T00:38:14Z.
@jotitadev thanks for the re-run at
76a990045. The revert check (PR test withmain'sThread.cppaborting) is the part I couldn't show from the PR alone.One correction: my host does have AVX2 (i7-13700K), so the suite runs here as well. The before/after RSS numbers above are from that machine. An independent Linux run at the exact head is still useful.
AI-assisted: yes (Claude Code)