Skip to content

disk preflight: restore a parked build tree from the cold store when a build needs it - #1341

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

GLinnik21 merged 14 commits into
mainfrom
tools/cold-store-unpark

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

What

When a build starts and its own cargo tree is absent, the disk preflight now copies the tree back from the opt-in cold store (#1340), if a parked copy is indexed for it.

  • When the check runs: first in tools/disk-budget.py preflight, before the PLX_DISK_BUDGET_DONE and PLX_MIN_FREE_MB=0 exits. That way the children of make check, install and build-bench (all run with DONE=1) also get their tree back. Only the entry's own DISK_TREE comes back. A direct cargo reaches no preflight and rebuilds cold.
  • Fast path: it imports nothing. It does one lstat of the tree, plus an index stat or listdir only when the tree is absent. That adds about 0.5 ms; the median was 23.2 → 23.7 ms over 20 runs. The import-set test still pins os/sys only.
  • Before taking the lock: if the cold store is off or its root is not mounted, the preflight prints one line and goes on, with no lock wait.
  • Claim, then copy:
    • Under build-gc's lock the preflight only claims the parked copy: an atomic rename of the index entry to .unparking.<pid>.
    • The copy then runs without the lock, so two trees in one make check restore in parallel.
    • A second build of the same tree gets "being restored by pid N" and rebuilds.
    • A dead claimer's entry is put back by the next claim or by recover. A failure, a decline or an exception gives the claim back without overwriting a newer record.
  • When it restores: only when the cost rule passes (size ÷ the slower of the measured and floor rate < the tree's rebuild time) and free space after the copy, counting other copies in flight, stays above the comfort line.
  • One deadline: cmd_preflight uses a single deadline for the unpark wait and the ordinary lock wait, so a build never waits 2 × --wait-secs.
  • Any failure prints one line, and the build proceeds as if the cold store were off.

Why

This is the last piece of the cold store. Parking an idle tree only pays off if it comes back when that lane builds again. Measured on the owner's Mac:

  • A 208.7 MiB target parked on the external disk came back in 0.3 s at make check-nodef.
  • After the copy back, cargo reported Fresh 114, Dirty 0, nothing compiled, Finished in 0.05s.

Verification

All on the committed tree:

  • ci/test_disk_preflight.py: 54 tests, OK. The tests cover:
    • the unpark path under DONE=1 and MIN_FREE_MB=0;
    • no cold-store.py load when the tree is present, when there is no index entry, or when the cold store is off;
    • decline, failure, exception and held-lock cases each printing their outcome line (a failed copy also prints its estimate line);
    • one deadline;
    • the no-go answers coming before any lock wait;
    • two trees restoring concurrently;
    • a tree claimed twice;
    • a dead claimer;
    • the comfort-line rule;
    • the escape rule pinned equal to cold-store.py's.
  • ci/test_cold_store.py: 74 tests, OK.
  • ci/test_disk_budget.py: 142 tests, OK.
  • ci/test_build_gc.py: 81 tests, OK.
  • ci/test_ci_split.py: 16 tests, OK.
  • No Makefile change.

One Opus review found four minor issues, and all four are fixed:

  • the preflight could wait twice its --wait-secs;
  • it took the lock before the cheap off/unmounted checks;
  • the global lock was held across the copy;
  • no test covered the comfort-line rule.

🤖 Generated with Claude Code

GLinnik21 and others added 14 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]>
…and zero-reserve exits

The check is the first thing fast_preflight does: two stat calls (the tree, then its
SSD index entry) and the PLX_COLD_STORE=off test, nothing imported. Only an absent tree
with an index entry takes the full command, which loads cold-store.py, takes build-gc's
auto lock (waiting up to --wait-secs, with the waiting flag), asks ColdStore.unpark with
a free-space-after-copy-stays-above-comfort rule, prints one line and goes on to the
ordinary decision. It sits before the exits because make check, install and build-bench
run their children with PLX_DISK_BUDGET_DONE=1. Any failure costs one line, never the build.

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]>
The failed-copy case now logs the estimate line before the copy as well
as the one outcome line, so assert one of each rather than one line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e wait deadline; no-gos before the wait

Review findings on the preflight unpark:

1. cmd_preflight takes ONE --wait-secs deadline at its start; the unpark's wait
   for build-gc's auto lock and wait_on_holder both spend what is left of it, so
   a build never waits twice the budget on a held lock.
2. The cheap no-gos come first: ColdStore.unusable() (the config, ditto, the
   cold volume mounted: available()'s first three steps) ends the unpark with
   one line before any lock is waited for.
3. The auto lock is no longer held across the copy. ColdStore.claim() decides
   (the cost rule and the caller's free-space rule, which now also counts what
   other live claims are still to copy) and atomically renames the index record
   to index/<id>.json.unparking.<pid>; unpark(claimed=...) copies without the
   lock and consumes the claim, or gives it back on any failure. A claimed
   entry is invisible to a second unparker (told "being restored by pid N"),
   to recover's adoption and to the park pass; a dead claimer's claim is put
   back by the next claim of that tree and by recover. The fast path also
   looks for a claim, so the build says who has the tree.
4. The free-space rule has its own test: a copy that would leave less than the
   comfort line free is declined and the entry stays.

make disk and cold-store.py status list a claim as "restoring".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@GLinnik21
GLinnik21 merged commit 19f66c6 into main Nov 11, 2026
15 checks passed
@GLinnik21
GLinnik21 deleted the tools/cold-store-unpark branch November 11, 2026 20:00
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