Skip to content

Studio: answer slow cache sizes at once and walk cache trees faster - #15627

Merged
danielhanchen merged 10 commits into
mainfrom
studio-settings-caches-fast
Oct 28, 2026
Merged

danielhanchen merged 10 commits into
mainfrom
studio-settings-caches-fast

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The Settings page's cache list (/api/settings/caches, studio/backend/utils/cache_inventory.py) sizes every cache by lstating every file under it: sizes and hard-link accounting need st_size, st_nlink and st_ino per file. A machine with a large uv or Triton cache (here 3.9M and 2.17M files) pays 30 to 40 s per full inventory warm, and over 200 s with a cold inode cache, so the request times out. The answer is kept for 60 s, so every visit after the first minute paid the whole walk again on the request thread.

What changes

studio/backend/utils/cache_inventory.py, response shape unchanged, frontend unchanged:

  • The walk keeps paths as strings and sums single-link file sizes locally instead of through the hard-link ledger per file. Symlinks, junctions, unreadable directories and hard-link accounting are handled exactly as before.
  • A cache whose last walk took 2 s or more answers with its previous size once the 60 s window has passed, and is re-walked in the background. Background walks run one at a time, start only after the request's own walks, and never store a result if the cache was purged meanwhile. A cache that walks in under 2 s behaves exactly as before, and the Recheck button still waits for a fresh walk.

Results

Per-root walk, warm, median of 3 (range), same roots on both sides:

Root (files) main user CPU this PR user CPU main wall this PR wall Bytes
Triton (2.17M) 5.37 s (5.09 to 5.71) 3.00 s (2.98 to 3.61) 11.85 s 8.53 s identical
uv (3.9M) 8.72 s (7.66 to 9.24) 5.55 s (5.35 to 6.22) 20.65 s 16.67 s identical

Kernel time is unchanged: one lstat per file is the floor, which is why the slow caches are refreshed off the request instead.

Full cache_inventory() request, wall time:

Read main this PR
First in a fresh process 31.4 to 43.6 s (median 37.1) 27.6 to 35.9 s (median 32.7)
After the 60 s window has passed 34.5 to 39.6 s, 208.3 s with a cold Triton cache 0.58 to 1.80 s

The remaining 0.6 to 1.8 s is the torch inductor cache (about 1 s, under the threshold) and the small caches; the background refresh of uv and Triton then finishes in 26 to 42 s off the request thread.

Tests

  • New studio/backend/tests/test_cache_inventory_walk.py (11): the new walk against the previous walk, kept as an oracle, on a tree with symlinks, hard links, unreadable and nested directories, a junction below the top level is still not walked, and the background refresh (a slow expired cache answers at once, a quick one is still walked in the request, Recheck waits for a fresh walk, a purge during a walk is not overwritten, background walks run one at a time and start after the request's own). 4 of them fail on main.
  • The 24 test files that touch this code: only those new tests differ between main and this branch; the same 6 unrelated tests fail on both (test_artifact_mirrors, test_cached_gguf_routes x2, test_mlx_bnb_substitution x3).

Not covered

  • The first visit after a restart still walks everything (about 30 s warm here).
  • Windows (junctions, long paths) and macOS were not run; the walk keeps the same os.scandir / DirEntry calls and the same junction handling as before.
  • Threads inside one walk were measured and rejected: 8 threads took 42.8 s against 16.9 s for one.

@danielhanchen
danielhanchen marked this pull request as ready for review October 28, 2026 05:54
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T06:03:35.563914Z ba66b1e Manual request
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e68d517db2

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if patterns is None:
# Sanity on the oracle itself: the outside-linked and unreadable bytes are not counted.
nested = sum(100 + a * 10 + d for a in range(4) for d in range(3))
assert expected == (11 + nested + 5000, 7)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Make the unreadable-directory assertion work under root

When this test runs as root, as in a root-based development or CI container, chmod(0) does not prevent the walker from reading these directories. Both implementations correctly return 8803 bytes, but this assertion requires 6403 bytes, so the new test fails despite identical walk behavior. Simulate permission failures deterministically or condition the unreadable-byte assertion on whether access is actually denied.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in ba66b1e: root bypasses permission checks (capabilities(7) CAP_DAC_OVERRIDE), so the oracle sanity check now expects the locked directories' bytes when geteuid() == 0; the new-vs-reference equality is unchanged.

danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Oct 28, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: ba66b1e51c

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# Conflicts:
#	studio/backend/hub/utils/inventory_scan.py
@danielhanchen
danielhanchen merged commit 78ef9b5 into main Oct 28, 2026
2 of 39 checks passed
@danielhanchen
danielhanchen deleted the studio-settings-caches-fast branch October 28, 2026 08:20
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Oct 28, 2026
…wing the old number (unslothai#15718)

Follow-up to unslothai#15627.

Before unslothai#15627, Settings measured a cache again in the request whenever
its last measurement was more than 60 seconds old, so the size shown was
always current. unslothai#15627 answers a slow cache from its last walk and walks
it again in the background. That keeps the page fast, but the page never
fetched the new number. A size changed by an install, a training run or
a tool outside Studio stayed old on screen until the next visit. This PR
closes that gap and a few related ones.

### Changes

- The inventory response has a `refreshing` flag. It is set when any
size came from an expired walk that is being redone.
- Settings asks again every 2 seconds, at most 60 times, until every
size is current. These follow-up reads only swap in the newer sizes:
there is no measuring state, and a failed one keeps the rows. They stop
when you leave the page, start a newer request or clear a cache.
- A background refresh queued behind another cache's walk no longer
holds its own cache's lock while it waits. A clear, download or Recheck
of that cache walks right away.
- The queued refresh skips itself if a newer size was stored in the
meantime.
- If the background thread cannot start, the read walks in the request
instead of serving the old size.
- Clearing one Hugging Face cache no longer forgets the sizes of the
other three. A download or delete still forgets all four.
- Recheck accepts any walk that began after the click, instead of only
one that began after that cache's turn in the request.
- The memo test fixture waits for background walk threads before
restoring the patched state.

### Tests

- Five new cases in `test_cache_inventory_walk.py` fail on main and pass
here:
  - a queued walk does not hold a read
  - a refresh that cannot start walks in the request
  - Recheck accepts a walk begun after the request
  - the `refreshing` flag
  - clearing one Hugging Face cache keeps the other sizes
- The new case in `cache-rows-model-folder.test.ts` fails on main and
passes here.
- Frontend: 13 of 13 tests pass across `cache-rows-model-folder.test.ts`
and `settings-cache-purge.test.ts`. The typecheck passes.
- Ten neighbouring cache, hub cache and route test files: main 934
passed, this branch 939 passed.
- Both show the same single failure,
`test_cached_gguf_routes.py::test_pipeline_class_guard_passes_every_shipped_family`.
It is unrelated and already fails on main.
- `py_compile` is clean on Python 3.9 and 3.10.


### Before and after

Settings > System > Cache files, with a Triton cache whose walk takes a
few seconds. The page read the cache once, the cache then grew by 5 GB
after the 60 second memo expired, and Settings was opened again. Both
shots are taken 25 seconds after opening. Both servers already held the
new 5 GB size by then. Before this PR the row kept showing 0 B; with it
the follow-up read swaps in 5.00 GB.

![Cache files before and
after](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/15718/pr15718_before_after-7ba691ceafa2.png)
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