Skip to content

disk tooling: opt-in cold store parks idle build trees on an external disk instead of deleting them - #1340

Merged
GLinnik21 merged 9 commits into
mainfrom
tools/cold-store
Nov 11, 2026
Merged

GLinnik21 merged 9 commits into
mainfrom
tools/cold-store

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

What

This adds an opt-in cold store. When the internal SSD runs low, idle cargo build trees are copied to a slow external disk instead of the disk-budget ladder deleting them. A later PR copies a parked tree back when a build that needs it starts.

Off by default

  • The cold store is off unless a per-user file cold-store (with a root= line) exists in the same config dir as tv-host (~/.config/plxnative/). PLX_COLD_STORE=off|<root> and PLX_COLD_STORE_CONFIG=<file> override it.
  • Without that file, every entry point does nothing and creates no file. A golden test pins that build-gc.sh --auto prints and logs exactly what it did before, above and below the threshold.
  • It also turns itself off, with a stated reason, when:
    • ditto is missing;
    • the root is not mounted;
    • the root is on the build's own volume;
    • the cold volume is below min_free_gib;
    • a probe write fails.

Parking

  • When: a new --auto stage, after --orphans. It runs only between the comfort line (20 GiB free) and park_below_gib (40 GiB). Below the comfort line it parks nothing and the ladder deletes as before, because a copy to a spinning disk takes minutes and a waiting build gives up after 60 s.
  • Which trees: the trees taken are the ladder's own whole-tree candidates (idle /tmp trees, idle lane and fleet trees), oldest first, up to park_max_gib_per_run. Each tree is re-probed for liveness, and its lane must have been idle for park_idle_min.
  • Steps for one tree:
    1. rename the tree to SSD staging;
    2. ditto it to <root>/trees/<id>.partial;
    3. verify file count and bytes;
    4. write the cold-side record and rename the copy into place;
    5. flush the cold volume once (sync and F_FULLFSYNC);
    6. write the SSD index entry;
    7. remove the staging copy.
  • Failures: a failure at any step puts the tree back and reports FAILED. A crashed parker's leftovers are settled by recover.
  • Lock: the --auto lock is released and re-taken between trees. The pass yields, and so do recover and prune, when a build preflight is waiting.
  • Pruning: a parked copy is dropped when its origin was rebuilt, its lane is gone or merged, it is older than max_days, or the cold volume is short of space.
  • By hand: build-gc.sh --park [-n] runs the pass, and tools/cold-store.py status|list|park|unpark|prune|recover gives direct control. make disk shows whether the cold store is on (or why not), what is parked, and any leftovers.

Why

The internal 228 GiB SSD keeps running out when several worktrees build at once. The external disk has terabytes free but is a spinning USB disk (12 MB/s reads under load), so nothing a running build touches goes there.

Measured on the owner's Mac: copying a tree back takes 0.18-0.59x a cold rebuild (4-12 s against 16-29 s for 212-804 MiB trees). After a ditto round trip cargo sees every unit as Fresh (114/114, 0 Dirty), and all 2160 hard links survive.

Verification

All on the committed tree:

  • ci/test_cold_store.py: 63 tests OK. They cover:
    • the unconfigured case creating nothing;
    • each reason it turns itself off;
    • a round trip that keeps ns mtimes and hard links;
    • crash recovery;
    • every post-copy failure point;
    • flush ordering;
    • a repeated park and unpark of the same tree;
    • pruning.
  • ci/test_disk_budget.py: 142 tests OK.
  • ci/test_build_gc.py: 81 tests OK, including the unconfigured golden runs and the park stage above and below the comfort line.
  • ci/test_disk_preflight.py: 32 tests OK.
  • ci/test_ci_split.py: 16 tests OK.
  • No Makefile change.

One Opus review of this diff found three issues, and all were fixed:

  • a park could hold the lock past a build's 60 s wait;
  • nothing flushed the cold volume before the SSD copy was deleted;
  • the steps after the copy had no error handling.

A real-disk run (W4's) found two more, also fixed:

  • a .consumed name collision when the same tree was parked twice;
  • no log line before a long copy back.

🤖 Generated with Claude Code

GLinnik21 and others added 9 commits November 11, 2026 20:43
The build-entry preflight stalled 20-35 minutes in IO wait on a Mac whose 21 lane
worktrees sit on a slow USB disk: collect() sized, mtime-walked, git-probed and
lock-probed every tree, and only afterwards did never() throw out the ones on
another volume than the build. Removing such a lane frees nothing on the volume
that is short, so none of that read was ever useful.

- disk-budget.py collect(): ask the one device lookup (device_of, which tests
  stub) first; a tree on another device is listed with its dev, kind, checkout
  and owner and nothing else (no measure, newest_mtime, tracked_under, lock
  probes, or lane git facts). never() keeps rejecting it; the report prints it
  as "other volume (not sized)". size_foreign is unrelated (it sizes a /tmp tree
  with no PlxNative marker) and is documented as such.
- build-gc.sh: --auto's --worktrees pass skips a lane on another device than
  ROOT before worktree_reason / idle_lane read it; by hand it is judged as
  before. New path_dev / on_other_volume helpers (GNU stat first: on Linux
  `stat -f %d` is a free-inode count).
- make disk: on macOS one line says whether the hourly launchd agent
  com.plxnative.build-gc is loaded, and that `make disk-watch` loads it (it sat
  unloaded for ~19 h with 19 GiB unreclaimed, silently).
- docs/disk-budget.md and the --worktrees help text say so.
- Tests: raising readers/probes for a foreign-dev lane and fleet tree
  (ci/test_disk_budget.py), a foreign-device lane skipped by --auto without a
  find over it, and the agent line (ci/test_build_gc.py).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The first pass skipped a tree on another volume, but a fleet tree
(~/plx-fleet/<lane>, the lane's CARGO_TARGET_DIR) is on the build's volume
while its lane's worktree can sit on the slow USB disk, and three readers
still walked that worktree:

- disk-budget.py collect(): is_merged / is_clean (a squash check and a
  `git status` over the whole worktree) now run only for a checkout on the
  build's volume; a foreign one reads neither merged nor clean (the
  conservative side). The `make check` lock is still probed: it is a flock
  on a local file named by a hash of the checkout's path, reads nothing on
  the slow disk, and a check running there builds into this very tree.
- build-gc.sh --evict: ev_lane_active counts a checkout on another volume
  as active instead of `find -mmin` over it, so its fleet tree is spared,
  and the skip line says why.
- build-gc.sh report (`make disk`): a worktree on another volume prints `-`
  columns and "other volume, not sized" instead of running du over its
  checkout, its target trees and its incremental dirs.

Tests: OtherVolume in ci/test_disk_budget.py (only the worktree foreign,
fleet tree local; every git reader raises), ci/test_build_gc.py (a logging
find and du shim behind a fake stat). docs/disk-budget.md says so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Park an idle derived build tree on a second volume (a big external disk) and
bring it back when its owner builds again. Off unless a per-user `cold-store`
config names a root: unconfigured, every entry point returns before creating a
file anywhere.

- config (key=value in the tv-config.sh directory, PLX_COLD_STORE{,_CONFIG}),
  available() with its six ordered reasons, an SSD index plus a cold-side
  record, park (rename into a same-volume parking dir, ditto to .partial,
  verify count + apparent bytes, record, rename), unpark (absent origin only,
  cost rule, sibling copy, verify, rename), prune and residue recovery.
- the one copy primitive is `ditto --norsrc`; tests swap in a hard-link
  preserving shutil walk, and one darwin-only test drives the real ditto
  (regular-file and directory ns mtimes, modes and hard links survive; a
  symlink's mtime does not, which nothing reads).
- no caller yet: the --auto park-ahead stage and the preflight unpark land
  with the next workstreams.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`build-gc.sh --auto` gains one stage between --orphans and --worktrees: with a
cold store configured and free space under park_below_gib (40 GiB, above the
20 GiB comfort line), `disk-budget.py park --auto` moves the planner's idle
whole-tree candidates (rungs 3 and 4 of ladder() --no-build, oldest first, never
garbage, the main checkout, the seed, live or young trees, merged lanes or
anything on another volume) to the cold volume, up to park_max_gib_per_run, then
recovers parking residue and prunes copies that no longer earn their space.
Each tree is asked once more, right before the rename, with probe_tree (the
executor's checks) plus a lane-idle rule (a source edit counts, .git does not).

The pass releases and re-takes build-gc's auto lock between trees and stops after
the current tree when a preflight is waiting: wait_on_holder writes a
<lock>.waiting.<pid> flag the pass reads. `--park [-n]` is the same pass by hand.
Unconfigured, `--auto` prints and logs exactly what it did before (golden test).

Also: ColdStore.rate_for takes the slower of the measured rate and the floor,
and a dry-run park creates nothing on either volume (available(write_probe=False)).
`make disk` shows the cold store state, parked count/bytes and parking residue.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…re the SSD copy goes, guard the post-copy steps

Review fixes on the park pass.

- A park holds build-gc's auto lock for a whole ditto (minutes) while a preflight waits 60 s and then
  judges on free space with no eviction. `--auto` now parks only from the comfort line
  (PLX_GC_MIN_FREE_GIB) up to park_below_gib; below it the stage prints one line and the ladder deletes
  as before, and the pass re-reads free space before each tree and stops when it fell under the line.
  recover(), prune() and the reap take a `stop` callback (the waiting flag) and end between items; the
  pass hands the lock back instead of sweeping the slow disk with a build waiting.
- After the copy verifies, one flush of the cold volume (os.sync, then F_FULLFSYNC through the cold
  record file; fsync where there is none) runs before the index is written and the SSD copy deleted.
- The steps after the copy (set-aside, record, rename into place, flush, index) are guarded: any
  OSError puts the tree back and reports FAILED; a copy already renamed in becomes .dead debris so
  recover reaps it rather than adopting it. run_park_pass turns any other exception into a logged
  FAILED that ends the pass.
- unpark set-asides use unique `.consumed.<pid>.<ns>` names (the second unpark of a tree used to fail
  its rename and leak a stale copy), the reap matches debris names as a whole suffix, and a park
  sets an unrecorded stale copy aside under a unique name too.
- unpark logs one line before its copy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@GLinnik21
GLinnik21 merged commit bd38620 into main Nov 11, 2026
15 checks passed
@GLinnik21
GLinnik21 deleted the tools/cold-store branch November 11, 2026 19:19
GLinnik21 added a commit that referenced this pull request Nov 11, 2026
GLinnik21 added a commit that referenced this pull request Nov 11, 2026
When a build's own cargo tree is absent and the opt-in cold store (#1340) indexes a parked copy of it, the disk preflight copies it back before cargo runs. The check is first in `disk-budget.py preflight`, ahead of the PLX_DISK_BUDGET_DONE and PLX_MIN_FREE_MB=0 exits, so make check/install/build-bench children are covered; the fast path stays import-free (one lstat, ~0.5 ms).

- Off or unmounted cold store: one line, no lock wait.
- Under build-gc's lock the preflight only claims the copy (atomic rename of the index entry to .unparking.<pid>); the copy runs unlocked, so two trees restore in parallel; a second build of the same tree is told it is being restored and rebuilds; a dead claimer's entry is put back by the next claim or recover.
- Restores only when size / the slower of measured and floor rate beats the tree's rebuild time and free space after the copy, counting copies in flight, stays above the comfort line.
- One deadline covers the unpark wait and the ordinary lock wait; any failure prints one line and the build goes on as if the cold store were off.

Measured on the owner's Mac: a parked 208.7 MiB target came back in 0.3 s and cargo reported Fresh 114, Dirty 0.

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