Skip to content

fix(shm): fault a pre-sized table in at creation instead of over the run - #24

Merged
lisachenko merged 1 commit into
mainfrom
claude/issue-24-parent-rss-climb
Aug 16, 2026
Merged

lisachenko merged 1 commit into
mainfrom
claude/issue-24-parent-rss-climb

Conversation

@lisachenko

@lisachenko lisachenko commented Aug 16, 2026 •

Copy link
Copy Markdown
Owner

The substrate half of lisachenko/native-php-coroutines#24 (parent-side RSS climb the arena metrics do not see).

The mechanism

The arena is MAP_ANONYMOUS, so a page exists only once something stores to it. ResultSlotTable is pre-sized before the fork, but slots are handed out one per spawn — so the table's pages fault in 64 bytes at a time over the entire run. Nothing is allocated (the bump cursor never moves, no watermark or allocator counter can see it), yet the family's RSS climbs with the workload: ~0.5 KiB per 8-spawn round, measured directly off the arena mapping's Rss in /proc/self/smaps at 62.7 B per consumed slot ≈ the 64-byte slot record. It plateaus only at full capacity (4096 slots / 256 KiB), which no run reaches — indistinguishable from a leak to any memory gate, and reported as one.

The fix

Arena::prefault($address, $length) — one zero store per page across a freshly bumped block, called from ResultSlotTable::create(), which is a pre-fork moment. The block is new and already zero, so the store changes nothing; it only decides whether residency is charged at creation or spread across the run. Afterwards, a climb really is a climb — the honest fix, not a suppression.

Details that earn their tests:

  • the first touch is at $address, not at its page boundary — the boundary may belong to the block allocated before this one, and a store there would silently clobber it (pinned by its own test);
  • the range is containment-asserted before any store;
  • no LAYOUT_VERSION bump — no record shape, table meaning, or globals[0] target changes.

The consumer-side component of the climb (~77% of it: the runtime's own unpruned per-spawn slot views) is fixed separately in lisachenko/native-php-coroutines#29.

Verification

PHP 8.5.9 (this tree's z-engine line), -d ffi.enable=1 -d opcache.jit=off:

  • vendor/bin/phpunit — OK (172 tests, 14479 assertions), exit 0; also --order-by=random and MALLOC_CHECK_=3 MALLOC_PERTURB_=85, both OK.
  • tools/soak.php 5000 — SOAK OK; tools/soak-drop.php 5000 — SOAK-DROP OK.
  • The same source also ran under PHP 8.4 through the consumer repo's vendored copy (full coroutines suite green); CI is the authority for the matrix.

With this plus the consumer fix, tools/soak-arena-watermark.php over there goes from rss CLIMBING +24.0 KiB / FAIL to rss plateau +0.0 KiB / PASS at 40, 120 and 400 rounds with the tolerance untouched, and its --self-test still fails as it must.

🤖 Generated with Claude Code

https://claude.ai/code/session_01U5QRWPfmkZME7hDjU6i5EA

The arena is MAP_ANONYMOUS, so a block's pages arrive on first write. A
structure that hands out one record at a time - ResultSlotTable, one slot
per spawn - therefore pays for its pages a fault at a time for the whole
life of a run, even though the table was pre-sized before the fork and
nothing is being allocated. No arena counter can see it: the watermark and
the allocator cursor are byte-flat throughout, and the only symptom is the
family's RSS climbing with the workload, which is exactly what a memory gate
calls a leak. native-php-coroutines#24 reported it as one: ~64 bytes of RSS
per slot id consumed - the slot record - with both arena counters at +0.0.

Arena::prefault() stores one zero per page across a block. The block is
freshly bumped and therefore already zero, so it changes nothing; it only
decides whether the residency is charged at creation or spread across the
run. ResultSlotTable::create() calls it, which is a pre-fork moment, so the
pages are materialized once and shared by the whole family.

Measured on the reporting soak (8 slots/round): arena mapping RSS went from
60 KiB climbing to 84 KiB across 392 slots, to a flat 84 KiB from round one.

No record shape, table meaning or globals[0] target changes, so
LAYOUT_VERSION stays at 5.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U5QRWPfmkZME7hDjU6i5EA
@lisachenko
lisachenko marked this pull request as ready for review August 16, 2026 09:24
@lisachenko
lisachenko merged commit 7af43a0 into main Aug 16, 2026
8 checks passed
@lisachenko
lisachenko deleted the claude/issue-24-parent-rss-climb branch August 16, 2026 09:24
lisachenko pushed a commit that referenced this pull request Aug 16, 2026
Brings in #24's prefault fix, which lands in the same two places this branch
rewrites.

`ResultSlotTable::create()` merged cleanly and correctly: the prefault call is
kept and its `$size` is computed from THIS branch's geometry, so the whole of the
larger table (8 header words, 12 words per slot, up from 4 and 8) is made resident
at creation rather than only the part main's arithmetic would have covered. The
inline comment's "one per eight slots" no longer matched either geometry and is
now stated without the arithmetic.

AGENTS.md §6 conflicted - both sides append a bullet after the shared-string rule.
Both are kept, prefault first, and the recycling bullet now says why one does not
replace the other: a steady-state pool reuses a handful of records and would look
page-flat by accident, while a workload whose concurrency really grows still
reaches records nothing has touched. The prefault is what makes that second case
flat too.

Suite on PHP 8.5: 182 tests, 15565 assertions, OK - including both of #24's
prefault tests against the new geometry, and green under --order-by=random with
MALLOC_CHECK_=3. Both soaks pass unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U5QRWPfmkZME7hDjU6i5EA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants