Skip to content

3.x.x: return a landing's freed heap to the kernel with malloc_trim after each block landing (#600) - #627

Merged
djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b600-trim2
Oct 1, 2026
Merged

djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b600-trim2

Conversation

@djh58

@djh58 djh58 commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Refs #600

#600's investigation found that a 400k-window frontend's resident set ratchets up because glibc keeps memory, not because live data grows. Each block landing rebuilds the payout window and audit, about 1.4 GB of short-lived small allocations. glibc keeps what they free in its arenas. The landing frontend climbs to a 3.2–3.3 GB plateau while its live heap returns to about 250 MiB. This PR takes the investigation's zero-dependency fix, malloc_trim(0) after each landing. jemalloc, the better long-term option, is filed as a follow-up (#628).

What changes

  • Trim after each landing, on the frontend that landed. land_offered already hands the rebuilt window to OffRuntime, which drops it on a blocking thread. OffRuntime gains a .then follow-up that runs on that same thread after the drop, and the landing's follow-up is the trim. So the trim runs:
    • after the window is released;
    • never on a runtime worker;
    • never under a lock or a database transaction.
  • glibc only. On other targets (musl, macOS) the trim is a no-op and records nothing.
  • Setting. PRISM_LANDING_MALLOC_TRIM_ENABLED, default 1. run, check-config and candidates recover read it. Compose and .env.example pass it, and it is in the native settings inventory.
  • Metrics (unlabelled; the census grows by 2 startup series and 16 populated):
    • qbit_prism_landing_malloc_trim_seconds (histogram): duration of each trim.
    • qbit_prism_landing_malloc_trim_released_bytes_total (counter): resident set before minus after each trim.
    • qbit_prism_landing_malloc_trim_resident_bytes (gauge): resident set right after the latest trim; -1 before the first trim.
    • The resident set is read with the process collector's VmRSS parser, now a shared collectors::resident_bytes. There is no second procfs parser and no unsafe sysconf.
  • Docs. prism-storage-sizing.md gets a "Landing memory per frontend" section: the measurements below and the memory limit to set. That is 3 GiB per frontend at 400k with the trim (RSS warning 2.5 GiB) against 5 GiB without (warning 4 GiB). The capacity-readiness malloc_trim section, PRISM.md and the metrics inventory are updated too.

Measurements (400k soak shape, trim off and on)

Same release binary, trim off and on, two 30-minute runs of each, interleaved off/on/off/on. The shape is soak-weekly's: two frontends, 400 sessions, 400k window, six blocks per run. Each run was pinned to four cores under the shared build lock. The harness was patched locally to pass the setting through, since it clears the frontend environment. Every run passed its gates: 6 of 6 blocks accepted, exact reconciliation.

trim frontend landings 10-min cycle floors (MiB) peak (MiB)
off fe-1 4 420, 2,110, 2,540 3,166
off fe-1 5 419, 1,374, 2,026 2,635
off fe-0 1 354, 634, 1,349 1,436
off fe-0 0 356, 614, 632 709
on fe-0 6 426, 579, 593 1,367
on fe-0 3 412, 538, 551 745
on fe-1 3 387, 605, 685 1,642
on fe-1 0 447, 651, 674 766

Tests

  • Gated landing_malloc_trim (listed in test/prism-gated-tests.txt):

    • three landings over a 20k-share production-shaped window, through the real offer path;
    • the resident floor after each landing's trim must stay within 0.65 of the landings' resident burst;
    • exactly one trim per landing is recorded, and the other frontend records none.
  • Calibration. Debug build, whole test binary, on 2 and 18 cores:

    • with the trim (12 runs), the worst landing's floor was 0.24–0.46 of the burst;
    • trim off, no malloc_trim call, or no follow-up (10 runs): 0.82–1.0.
  • Unit tests.

  • Mutants, each run against the listed test:

    mutant test result
    set_enabled(false) gated fails 4/4 (floors 0.82–1.0)
    malloc_trim call removed gated fails 4/4
    .then(...) follow-up removed gated fails 2/2
    trim before the drop OffRuntime unit test fails 3/3

    The trim-before-drop mutant fails the gated test only marginally, because that trim still returns the rest of the landing's garbage, so the unit test carries it.

  • Also run (after rebasing onto the current 3.x.x): cargo test -p qbit-prism-server --lib (602 passed; rollups::tests::postgres_sql_cancellation_retains_success_and_rolls_back hit a lock timeout once under full-suite load and passed 3/3 alone), and landing_metrics, offer_lifecycle, observability, config_cli, self_check_cli, candidate_window_switch, lost_race_pin, all green against a private PostgreSQL 16 with PRISM_TEST_REQUIRE_INTEGRATION=1. cargo fmt --check and cargo clippy --workspace --all-targets -D warnings are clean, and so are generate_prism_metrics.py --check, check_prism_settings.py and the docs/settings Python tests.

  • Per-PR CI cost. One new test binary in a prism-native-postgres shard. Its run takes about 15 s pinned to 2 cores (the shard runners are 2-vCPU), well inside the fixture's 45 s timeout, plus one test-binary build and link.

Not in this PR

🤖 Generated with Claude Code

…fter each block landing (#600)

#600: every 400k-window block landing rebuilds the payout window and audit,
about 1.4 GB of short-lived allocations, and glibc keeps what they free in
its arenas. The landing frontend's resident set climbed with each landing
to a 3.2-3.3 GB plateau while its live heap returned to about 250 MiB.

- The frontend that landed calls malloc_trim(0) once the landing has
  released its rebuilt window, on the blocking thread that released it
  (OffRuntime gains a follow-up that runs after the drop): never on a
  runtime worker, never under a lock or a database transaction. glibc
  builds only; elsewhere it is a no-op that records nothing.
- PRISM_LANDING_MALLOC_TRIM_ENABLED (default 1) turns it off; run,
  check-config and `candidates recover` read it, compose and .env.example
  pass it.
- Metrics: qbit_prism_landing_malloc_trim_seconds (histogram),
  qbit_prism_landing_malloc_trim_released_bytes_total (resident set before
  minus after each trim) and qbit_prism_landing_malloc_trim_resident_bytes
  (the resident set after the latest trim). The resident set is read with
  the process collector's VmRSS parser, now shared.
- Gated test landing_malloc_trim: three landings over a 20k-share
  production-shaped window through the real offer path; the resident floor
  after each landing's trim stays within 0.65 of the landings' resident
  burst. Trim off, the malloc_trim call removed and the follow-up removed
  each fail it; OffRuntime's test now fails a trim run before the drop.
- Measured on the 400k two-frontend soak shape, two 30-minute runs each:
  landing frontends' cycle floors 0.55-0.69 GB with the trim against
  1.35-2.54 GB without, peaks 0.75-1.64 GB against 1.44-3.17 GB; each trim
  62-100 ms; no change in landing latency or share ACK tails.
- Docs: landing memory per frontend and the memory limit to set (3 GiB at
  400k with the trim, 5 GiB without), the capacity-readiness malloc_trim
  section, PRISM.md.

Refs #600

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@djh58

djh58 commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 6fa59e5. Configure here.

@djh58

djh58 commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

Review ledger

CI is green on 6fa59e5: 23 checks passed, 11 skipped. Bugbot reviewed that commit and found no issues; no other review bot runs on this repo. Before the PR, the change went through a local review battery: /code-review at medium effort, a code-quality review and an adversarial review. The Codex lane was not run, because this lane is limited to Claude. Every finding is listed below with what happened to it.

New gated test in CI. landing_malloc_trim ran in shard 1. Building and linking it took 4 s and the test itself 16 s. The resident floor after each landing was 0.16–0.20 of the landing burst; the bound is 0.65.

# source finding disposition
1 code-review, quality, adversarial TRIM_TABLE placeholder in docs/prism-storage-sizing.md fixed: filled from the four 400k soak runs
2 code-review, adversarial malloc_trim locks each arena, so a runtime worker that allocates from it waits; the "never on a runtime worker" wording implied no stall fixed: measured 62–100 ms per trim with no slow-ACK clustering near trims; the caveat is stated in memory.rs and the sizing doc, with the histogram as the bound
3 quality the requested/running flags coalesce overlapping trims, but landings are already serialized per frontend fixed: flags, loop and their unit test removed
4 quality a third resident-set reader, plus an unsafe sysconf call fixed: collectors::resident_bytes is shared by the process collector, memory.rs and the gated test
5 adversarial candidates recover ignored PRISM_LANDING_MALLOC_TRIM_ENABLED fixed: the tool reads and applies it
6 adversarial the gated test could approach the fixture's 45 s timeout on 2-vCPU runners; landing 1 detects a missing trim only weakly fixed: about 15 s when pinned to 2 cores (16 s in CI); the bound was recalibrated to 0.65 on 2- and 18-core runs. Mutants fail on landings 2–3 (0.82–1.0 of the burst)
7 quality redundant glibc check; the released-bytes counter is noisy fixed (the double check went with #3). The counter stays, because the task asked for bytes released; its help text states the caveat
8 quality the setting is parsed outside Config and applied after Coordinator::new rejected: this follows the rollups::settings_from_env / partitions::settings_from_env precedent and is validated in both run and check-config. Moving it into Config would touch the full Config literals in test files other changes are editing right now
9 quality the gated test duplicates landing_metrics' land_from/queue_block rejected: sharing them means editing landing_metrics.rs and the shared support module while other lanes change both; the new test stays self-contained
10 quality OffRuntime::then has a single caller rejected: it is explicit and tested; the alternative, relying on tuple drop order, is implicit
11 adversarial soak-weekly keeps rss_expected_failure: "#600" deferred to #629: the peak-based gates still see the roughly 1 GB landing transient with the trim; #611's "looks fixed" row says when to drop the key
12 adversarial a landing cancelled mid-way, or a rebuild that outlives its deadline, finishes after the trim deferred: the frontend's next landing trims that memory; noted in #628
13 test the ordering unit test (mine, inherited) did not catch a trim before the drop, a race in the test itself fixed: the follow-up reads the drop state itself; that mutant now fails 3/3

Also filed: #630, the offered candidate could not be processed … payout revision changed while observing chain state alert seen in every soak run whether the trim was on or off.

@djh58

djh58 commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

Handover

@kiwidream now owns this PR. State at 6fa59e5: CI is green, and the review ledger is posted above with no open items. Left to do: owner review and merge. The jemalloc follow-up is tracked separately in #628.

@djh58
djh58 merged commit fd96c15 into 3.x.x Oct 1, 2026
34 checks passed
djh58 added a commit that referenced this pull request Oct 1, 2026
…families

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 6, 2026
…ixed #600 (#714)

The weekly 5.5 h soak at c95febb (run 37403129136), which includes
#627's post-landing malloc_trim, passed every resident-memory row.
load-fe-0 peaked at 1,026.9 MiB against a warm-up peak of 880.3 MiB
(1.17x, window-peak slope +8.65 MiB/h), and load-fe-1 at 1,292.7
against 1,141.3 MiB (1.13x, -15.75 MiB/h). Every other gate passed
too. The job failed only on the expected-failure marker, which
reported that #600 looks fixed.

So soak-weekly's rss_expected_failure goes back to null. The test in
tests/soak_plan.rs now checks every preset with a soak block and every
soak-gates file, at least four configurations, so an expected
resident-memory failure can't come back in any of them unnoticed.
docs/prism-soak.md now describes the marker generically and records
#600's history with the first weekly run's margins.

This changes only the harness preset, the test and the docs, with no
server code. Refs #600.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 6, 2026
…rade's sidecar and FETCH_COUNT steps, and migrate-before-start

From the review of the rc.2 notes:
- Known issues now list what opened since rc.1 and isn't fixed here:
  #716 (a valid share can lose its credit on "commit gate closed" at a
  tip change; seen on the pair at rc.1), #711 (the order lock across
  about nine round trips), #712 (85-minute evidence export), #713
  (verify-ca checks the host name) and #704 (a drill flake).
- Upgrading from 2.0.x adds running the sidecar tool before
  import-audits and exporting the evidence with FETCH_COUNT. Upgrading
  from rc.1 says to run migrate first, because an rc.2 frontend refuses a
  database without 025.
- #708's bullet says #710 also changes the evidence summarizer, which must
  be this candidate's on both sides of a cutover, and puts #710 next to
  the fix, not the refusal.
- #600's fix (#627) was already in rc.1. The carried-over soak passed
  every gate, and its job failed only on the tripwire #714 removed.
  faults-long-real-node is not re-run here.
- docs/prism-e2e-load-ci.md's example tag reads v3.0.0-rc.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 6, 2026
…rade's sidecar and FETCH_COUNT steps, and migrate-before-start

From the review of the rc.2 notes:
- Known issues now list what opened since rc.1 and isn't fixed here:
  #716 (a valid share can lose its credit on "commit gate closed" at a
  tip change; seen on the pair at rc.1), #711 (the order lock across
  about nine round trips), #712 (85-minute evidence export), #713
  (verify-ca checks the host name) and #704 (a drill flake).
- Upgrading from 2.0.x adds running the sidecar tool before
  import-audits and exporting the evidence with FETCH_COUNT. Upgrading
  from rc.1 says to run migrate first, because an rc.2 frontend refuses a
  database without 025.
- #708's bullet says #710 also changes the evidence summarizer, which must
  be this candidate's on both sides of a cutover, and puts #710 next to
  the fix, not the refusal.
- #600's fix (#627) was already in rc.1. The carried-over soak passed
  every gate, and its job failed only on the tripwire #714 removed.
  faults-long-real-node is not re-run here.
- docs/prism-e2e-load-ci.md's example tag reads v3.0.0-rc.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant