Skip to content

fix: reduce Linux Gateway background memory-check overhead - #206746

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-memory-pressure-cgroup-view-rediscovery-20260919
Nov 27, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-memory-pressure-cgroup-view-rediscovery-20260919

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

On Linux, each check for whether the Gateway should pause optional work reads and parses the process’s mount table, then resolves the same cgroup paths again. Startup prewarm can make several checks per item and more from the load callbacks, adding synchronous reads and allocation on the Gateway thread.

User Impact

Startup and idle work spend less CPU and allocate less memory checking for headroom. The Gateway still reacts to fresh leaf and ancestor charges, controller ceilings, process limits and host free memory. Existing installations need no configuration change.

Why This Change Was Made

Host memory owns one mapped view for the current process placement. Normal checks reuse the visible mount, file masks and ancestor paths. The process can move within a delegated subtree via cgroup.procs, so a change in membership captures the new view. For a fixed membership the mount layout is assumed stable for the process lifetime. The periodic worker-pressure and cron capacity readers keep their live lookup policy. Linux cgroup delegation

This builds on Vincent Koc’s delegated sizing and hidden-submount work and his follow-up correction for delegated pressure. The quiet-path change adds eight runtime lines for the single slot and its membership transition. Regression tests add 33 net lines, with 100 lines of measurement tooling. #206230 · #206308

Evidence

Team, on 6312d8be at 06:22 PST on November 27 with about 120 RPC/min, sampled 179.7 MB in 30 s. The startup pressure call site’s two synchronous file-read stacks and path joins accounted for about 4.49 MB (2.5%). The combined profile entries include counter reads as well as discovery.

Real Linux arm64 cgroup v2, Node 26.12.1, delegated mount with a 2 GiB parent, 1.5 GiB leaf and a lower file mask hidden by a nonrecursive bind. The test mountinfo was 60 lines / 7,142 bytes, close to Team’s 60 lines / ~7.8 KB. Five interleaved A/A and A/B pairs per capture, with 10,000 calls and 2,000 warmup checks per sample. The owner takes no database or workspace input. Measurements cover the predicate itself.

Per check A/A median (two arms) Before median / p95 After median / p95
JS sync file reads 9 / 9 9 8
Mountinfo reads 1 / 1 1 0
Main-thread CPU µs 59.69 / 59.59 59.56 / 62.89 22.36 / 22.67
Wall µs 59.82 / 59.71 59.70 / 63.13 22.46 / 22.73
Sampled V8 allocation bytes 24,954 / 25,010 25,074 / 25,602 8,865 / 9,475
First call µs 555.09 / 586.79 550.84 / 593.59 544.46 / 580.17

Every timed check returned the same decision. The remaining JS reads include the membership probe and live policy/counter reads. Node’s native host memory reads occur in both arms. A separate kernel check admitted at 36.6 MB of leaf charge, deferred at 162.9 MB under a 180.0 MB cap, then admitted after a live move to an idle sibling. Both versions returned the same results. Replay with node scripts/perf/host-memory-pressure-measure.mjs <base host-memory.ts> <head host-memory.ts> in a disposable delegated mount, with normal module resolution for those files.

The regression failed on the original owner with two mountinfo reads where one was expected. Host memory, Gateway resource profile and worker memory budget passed 77 tests (34.03 s in the local wrapper). The 33 host-memory tests executed in approximately 1.2 s. Existing tests cover hidden submounts, restricted procfs, live limits, ancestor pressure and delegated moves.

Two additional macOS cron cases failed with the same SQLite offline-maintenance error on this branch and clean main. The fixture globally mocks Linux while a real database worker runs on macOS. An exact-main diagnostic scoping the mock to the memory probe passed both cases. Fixture change in #200180

Codex autoreview found zero actionable P0–P2 findings.

pnpm check:changed passed.

@clawsweeper

clawsweeper Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts size: S labels Nov 27, 2026
@steipete
steipete marked this pull request as ready for review November 27, 2026 14:39
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T14:40:39.124664Z 01cd514 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 27, 2026
@clawsweeper

clawsweeper Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Codex review: passed.

What this changes

This PR reuses the mapped Linux cgroup view until process membership changes while continuing to read live memory counters and limits.

Example: Repeated optional-work checks on the reported Linux ARM64 delegated-cgroup fixture.

  • Before: Each warm check reads mountinfo; reported median CPU cost is 59.56 microseconds.
  • After: Warm checks reuse discovery without reading mountinfo; reported median CPU cost is 22.36 microseconds.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) A focused, apparently correct optimization with relevant real-kernel measurements and green exact-head CI; performance evidence remains at the predicate boundary.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the contributor proof gate; its supplied Linux kernel measurements exercise the changed owner with real controller reads and pressure and membership-move controls.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Linux Gateway startup and idle work repeatedly spend synchronous CPU and allocation on unchanged cgroup discovery.
Reason: The measured predicate improvement supports a small, bounded change, and the maintainer-authored direction explicitly accepts its discovery-lifetime tradeoff.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR provides a focused, measured reduction in repeated Linux memory-check work. No actionable correctness or security defects were found, and the exact-head CI gate passed.

Reviewed head: 01cd51436450e89c4d1509148adf6c229968e0d5

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The supplied after-fix measurements exercise the production predicate directly; no isolated Gateway startup or idle-work profile is supplied.
Agent review details

How this fits together

The host-memory owner translates process membership, mount visibility, and live kernel memory readings into the pressure decision consumed by Gateway startup and background schedulers.

flowchart TD
  A[Process membership] --> B[Mapped cgroup view]
  C[Mount information] --> B
  B --> D[Live memory readings]
  E[Host and process limits] --> F[Memory pressure decision]
  D --> F
  F --> G[Optional Gateway work]
Loading

Technical review

Best possible solution:

Keep discovery reuse in the existing host-memory owner, invalidate it on membership changes, and retain live policy and counter reads.

Do we have a high-confidence way to reproduce the issue?

Current-main source repeats mount discovery for each Linux Node pressure check, corroborated by the supplied real-Linux measurements; this read-only review executed no target code.

Is this the best way to solve the issue?

The small owner-local change removes repeated discovery without introducing a parallel reader, and its explicit mount-lifetime assumption explains the intended reduction in freshness.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 7f65b8d78ada.

Provenance checked

  • Delegated pressure and ancestor accounting keeps the original intent (398c8db: Use the mapped service leaf for raw-charge pressure while independently checking visible ancestors' working sets.)
  • Mount-view freshness for optional-work checks changes intended behavior with a stated reason (2f3debd: Resolve visible mounts and masks on each lookup so readers address the service's actual controllers; this PR explicitly trades repeated layout discovery for lower overhead when membership is unchanged.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Vincent Koc: Raw commit 398c8db adds src/infra/host-memory.ts:80 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 398c8db23048; files: src/infra/host-memory.ts)
  • Josh Lehman: Raw commit 71d835b adds src/infra/host-memory.ts:56 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 71d835b63abc; files: src/infra/host-memory.ts)

PR surface

Source +8, Tests +33, Other +100. Total +141 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 11 3 +8
Tests 1 38 5 +33
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 100 0 +100
Total 3 149 8 +141

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Optionally capture an isolated Gateway startup or idle-work profile to quantify the improvement in the consuming flow.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 27, 2026, 9:55 AM ET / 14:55 UTC.

@steipete
steipete merged commit 97e3c87 into main Nov 27, 2026
208 of 210 checks passed
@steipete
steipete deleted the steipete/perf-micro-memory-pressure-cgroup-view-rediscovery-20260919 branch November 27, 2026 14:59
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 28, 2026
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
* fix(runtime): defer optional work under host memory pressure

* style(infra): format memory pressure admission

* style(gateway): format the deferred prewarm check

(cherry picked from commit 9f50802)

Companion audit: #207993 selected later for publication fixture isolation. #206779 required if the new Cron host-memory fixture spoofs platform: preserve native SQLite namespace. #206041/#206230/#206308/#206746 are separate cgroup detection and optimization changes; #206322 is service-aware cron run budget, #206483 forks chat and #206384 changes a main-only warmer.

Release adaptation: Extend the stable optional-memory owner and its existing callers only. Preserve cgroup/Bun/macOS behavior and upstream threshold cases; omit main-only prompt warmer, cgroup diagnosis, and Cron platform-spoof changes.

Co-authored-by: RomneyDa <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
* fix: pause optional Gateway work under parent memory pressure

* fix: resolve visible cgroup controllers from their mounted subtree

* fix: retain visible memory limits above covered controllers

* fix: honor controller masks on the visible mount only

* fix: keep existing cgroup budget readers independent of admission

* refactor: name parsed cgroup mount fields

(cherry picked from commit 589a70a)

Release adaptation: take the pressure-owner slices of #206230 (verified mount mapping), #206308 (delegated leaf), and #206746 (mount discovery reused per membership). Do not import resource sizing, diagnostics or #206322 cron scheduling APIs; none are needed by stable optional callers.

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. scripts Repository scripts size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant