Skip to content

Share sccache entries between checkouts at different paths with SCCACHE_BASEDIRS - #134009

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/sccache-basedirs-server
Nov 30, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/sccache-basedirs-server

Conversation

@groeneai

@groeneai groeneai commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #127827
Related: #133753
Related: #130277

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Builds from checkouts at different paths now share sccache entries. With sccache 0.20.0 or newer, the build starts the sccache server with SCCACHE_BASEDIRS set to the source directory (option ENABLE_SCCACHE_BASEDIRS, used when ENABLE_BUILD_PATH_MAPPING is on), and CI sets it for its non-Debug builds.

Description

Option B of #127827 (#127827 (comment)). The first commit is #133753 (sccache 0.20.0 in CI); merge that first.

  • cmake/ccache.cmake, new option ENABLE_SCCACHE_BASEDIRS (on): the C/C++ and CUDA launcher becomes env SCCACHE_BASEDIRS=<source dir> SCCACHE_DIRECT=false sccache, so an auto-started server gets both. Only the source dir is a base directory, as only it is path-mapped; a build dir inside the checkout shares by its relative name. A server with other options is not restarted (--stop-server returns before it exits, so a concurrent build could start a second server on the same cache): the configure warns with the command. As chosen in Improve sccache to reuse S3 artifacts as cold read-only cache #127827, Debug and CUDA compiles on such a server can get another checkout's paths.
  • Preprocessor cache mode is off: with basedirs its hits restore the filling checkout's .d file.
  • Rust: SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS=CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATH, as protoc-bin-vendored bakes its manifest path into code. No Rust path mapping.
  • compiler-rt keeps the path-mapping flags.
  • CI exports SCCACHE_BASEDIRS=/ClickHouse for non-Debug builds.

Local validation, CI's amd_binary flags, disk cache: checkout A filled it and was moved away; checkout B at another path built clickhouse-bundle in 428 s (cold: 1,599 s) with 17,712 of 17,717 C/C++ hits and 99 of 603 Rust hits (host units; target crates hash the absolute --sysroot). Without the verbatim line B fails in perfetto_protos's build script (protoc not found <A>/...). 20 sampled hits are byte-identical to local compiles. Exception (clang, also on master): -ffile-prefix-map misses a lambda location inside a pointer-to-member template argument, so 2 grpc TUs keep the filling checkout's path in DWARF type names.

Where it loses: the local and CI caches are cold once (stripped arguments; locally also -P preprocessing without preprocessor cache mode); an all-hit rebuild at -j32 takes 258 s, not 254 s; two checkouts built within the idle timeout share nothing until the server restarts.


Workflow [PR]
Sync PR [sync-upstream/pr/134009]

Version info

  • Merged into: 26.12.1.1042-master (included in 26.12 and later)

groeneai and others added 3 commits November 23, 2026 21:08
0.18.0 and 0.19.0 made `-ftime-trace` uncacheable (mozilla/sccache#2834),
which 27 CI build types pass through -DENABLE_BUILD_PROFILING=1, so CI
stayed on 0.17.0. 0.20.0 hashes the flag again and writes no trace on a
cache hit (mozilla/sccache#2956). It also leaves CARGO_TARGET_DIR and
CARGO_BUILD_* out of Rust keys (ClickHouse#3050) and hashes the C/C++ input file
name (ClickHouse#2974).

Measured locally with CI's cmake flags and a local disk cache, second
build from a fresh build directory at the same path:
- amd_binary clickhouse-bundle: 1562 s cold, 147 s warm; C/C++ 17715 of
  17717 hits (the 2 misses are openldap's generated version files with
  __DATE__/__TIME__), Rust 603 of 603; no hit wrote a time trace.
- amd_asan_ubsan dbms: 1934 s cold, 95 s warm; C/C++ 16237 of 16239,
  Rust 529 of 529.
- PR-build environment (READ_ONLY, anonymous S3): the server starts, a
  miss compiles, the write is refused, warn_on_low_sccache_hit_rate
  parses the stats. 0.20.0 additionally counts an anonymous 403 on a
  missing key as a read error (ClickHouse#2967); it is still a miss.

0.20.0 treats `--embed-dir` as uncacheable, so the 4 _jieba sources
(6.9 s of compile) are always compiled. 0.20.0 does not read 0.17.0
entries: every build type is cold once after this lands.

Related: ClickHouse#127827

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sccache keys contain absolute paths, so a checkout or git worktree at
another path misses every entry of the cache that another checkout
filled. sccache 0.20.0 strips SCCACHE_BASEDIRS from the preprocessed
input and from the arguments, but it reads the variable only when the
server starts: a running server ignores the value a client passes.

Local builds (option ENABLE_SCCACHE_BASEDIRS, sccache >= 0.20.0, path
mapping on, SCCACHE_BASEDIRS not set in the environment): the C, C++ and
CUDA launchers become `env SCCACHE_BASEDIRS=<source dir>
SCCACHE_DIRECT=false sccache`, and the cargo environment carries the same
two variables, so whichever compile starts the server starts it with
these options. Preprocessor cache mode is off because a direct-mode hit
restores the dependency file of the compile that filled the entry, which
names the other checkout's headers.

CMake does not stop or restart a server that runs with other options:
`sccache --stop-server` returns while the server still serves the
requests in flight, so a build using it could start a second server on
the same local cache, which sccache does not support. The configure
prints a warning with the command to run when no build uses the server.
The check reads `sccache --show-stats --stats-format=json`, which does
not start a server.

Only the source directory is a base directory, because path mapping maps
only the source directory. The build-type default and the path-mapping
option move before include(cmake/ccache.cmake) so that the launcher is
set before the heavy-build check and the dummy launchers save it.

Rust: SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS=CARGO_MANIFEST_DIR,
CARGO_MANIFEST_PATH for every cargo run, so a crate that bakes its
manifest path into its code (protoc-bin-vendored, whose protoc the
perfetto_protos build script runs) is cached per checkout. OUT_DIR is not
listed: every use of it in the dependency graph is inside include!, which
sccache hashes as a source.

compiler-rt keeps -fdebug-compilation-dir= and -ffile-prefix-map=, so its
objects do not embed the checkout path.

CI exports SCCACHE_BASEDIRS=/ClickHouse before it starts the server, for
every build type except Debug ones (no path mapping), and in the fast
test; CMake leaves the launcher alone when the variable is set.

Related: ClickHouse#127827

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Without preprocessor cache mode a fully cached rebuild has to run the
preprocessor for every hit. Measured with CI's amd_binary flags, sccache
0.20.0, a local disk cache and a fresh build directory at the same path,
clickhouse-bundle with 17715 of 17717 C/C++ hits: 254 s with preprocessor
cache mode, 258 s without it.

Related: ClickHouse#127827

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 3 findings) plus my own review (1 nit); 1 full pass, no fix round needed.

# Sev Finding Verdict Evidence / action
1 ⚠️ The bugfix-validation job's merge-base tree (ci/tmp/before_src) still misses CI's entries (unit_tests_bugfix_validation_job.py:468) DISAGREE Not a regression: that tree builds cold on master too, as its NOTE (:473-479) accepts, and the PR does not claim sharing there. It would also need its build dir at ci/tmp/build inside the tree: a separate change.
2 ⚠️ Rust units are shared across checkouts without Rust path remapping (clickhouse_config_crate_env) DISAGREE ENABLE_BUILD_PATH_MAPPING never covered Rust (CMakeLists.txt:360-365), so Rust is in the class chosen with option B in #127827: compiles without path mapping on such a server can get another checkout's paths. The one data case, protoc-bin-vendored, is the verbatim line. With CI's flags only host units hit (99 of 603); target crates hash the absolute --sysroot, and B's binary has no Rust string with A's path.
3 ⚠️ No end-to-end two-checkout proof DISAGREE Done locally (no repo test for build infrastructure): B at another path, A moved away, 17,712 of 17,717 C/C++ and 99 of 603 Rust hits, stats with B's base directory and preprocessor cache mode off; 20 sampled hits, compiler-rt included, byte-identical to B's own compiles; B fails in perfetto_protos without the verbatim line; Debug, the option off, an exported SCCACHE_BASEDIRS and sccache 0.17.0 configure without the launcher env.
4 💡 The description did not say that Debug and CUDA compiles can get another checkout's paths AGREE, fixed One sentence in the PR description.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-10:20261124-012800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @thevar1able, could you review this? With sccache 0.20.0 the C/C++ and CUDA launcher becomes env SCCACHE_BASEDIRS=<source dir> SCCACHE_DIRECT=false sccache, so an auto-started server strips the checkout path from cache keys and builds from checkouts at different paths share entries (option B of #127827); CI exports SCCACHE_BASEDIRS for non-Debug builds, and corrosion keeps CARGO_MANIFEST_DIR verbatim for protoc-bin-vendored.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Without basedirs, a checkout at another path misses every entry, because the keys hold its absolute -I, --sysroot and -ffile-prefix-map arguments. With this PR, checkout B at another path (A moved away) gets 17,712 of 17,717 C/C++ hits, every run.
b Root cause explained? sccache keys contain absolute paths. SCCACHE_BASEDIRS strips them, but it is server config that is read only when the server starts, so a running server ignores a client's env. The launcher therefore carries it, so a compile that starts the server starts it with it, and CI exports it before it starts its own server.
c Fix matches root cause? Yes. It uses sccache's own basedirs and auto-start, gated on the existing ENABLE_BUILD_PATH_MAPPING. No guard and no new mechanism.
d Test intent preserved / new tests added? No test: this is a build/CI infrastructure change. It was validated locally (two full builds at different paths, sccache 0.20.0, CI's amd_binary flags).
e Both directions demonstrated? Sharing: no basedirs MISS, basedirs HIT. Rust verbatim line: B fails in perfetto_protos (protoc not found <A>/...) without it and builds with it. Preprocessor cache mode: the other checkout's .d with it on, the own .d with it off. Configure check: it warns or stays silent according to the server's state, and the server PID never changes.
f Fix is general across code paths? It covers the C/C++ and CUDA launchers, the heavy-build prlimit wrap and the launcher save/restore, the contrib re-wrap, the nested native configure, Rust and cc-rs through the cargo env, compiler-rt's flag keep-list, and the 3 CI sites that start the server.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked: Debug (inert), Debug with path mapping, path-mapped types; option on/off; SCCACHE_BASEDIRS already in the env; ccache; sccache 0.17.0 / 0.19.0 / 0.20.0 clients and servers; unreadable stats; a source path with " or \.
h Backward compatible? (maintainer-approved exception only) Yes. -DENABLE_SCCACHE_BASEDIRS=0 restores master's launcher. Debug, ccache and sccache < 0.20.0 builds are unchanged. The local and CI caches are cold once because the keys change.
i Invariants and contracts preserved? A hit equals the local compile, except 2 grpc TUs whose DWARF type names keep a lambda's path, which clang does not map; their stripped objects are identical. Dependency files name only the own checkout. The configure never stops, starts or fails on the server. A server with other options still gives correct results, only without sharing.

Session id: cron:clickhouse-impl-slot-40:20261124-011259

@clickhouse-gh clickhouse-gh Bot added can be tested Allows running workflows for external contributors manual approve Manual approve required to run CI labels Nov 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c9f3e8a]

Summary: ✅


AI Review

Summary

Enables sharing sccache entries across checkout paths with SCCACHE_BASEDIRS, carries the launcher environment through CMake and Cargo, preserves compiler-rt path mapping, and updates CI to sccache 0.20.0. Reviewed head c9f3e8a34b70095fb1706ad5b9c7ffb690b0e4fe, the surrounding code, and all prior discussion. No actionable findings. The documented Debug, CUDA, and Rust path tradeoffs match the accepted option B. The reported two-checkout build with the original checkout removed, dependency-file checks, sampled object comparisons, and rebuild timings provide focused validation for this build-system change.

Missing context / blind spots
  • ⚠️ Several broader build jobs were still running at review time. The Fast test passed with 15,332 tests passed and 4,560 skipped; no failures were reported in the available Praktika results. Completion of the remaining jobs will close this CI coverage gap. No local builds or tests were run in this review job.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-build Pull request with build/testing/packaging improvement submodule changed At least one submodule changed in this PR. labels Nov 24, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-build-system Build system (CMake, compilation, platform builds). label Nov 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c9f3e8a34 with master d31a6eff9 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: object file sizes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes ⚠️

97 object files changed (+920.24 KiB total), 0 added.

Object file Master PR Δ
src/Storages/System/CMakeFiles/clickhouse_storages_system.dir/StorageSystemChangelog.generated.cpp.o 3.10 MiB 4.00 MiB +921.19 KiB (+29.02%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

17336 translation units recompiled, 26079 s compile time in total, 17311 of them have a recent master baseline.

Translation unit Master PR Δ vs median
contrib/rocksdb-cmake/__/rocksdb/db/version_set.cc 8.3 s 13.4 s +5.2 s (+63%)
Symbol sizes
Binary Symbol Master PR Δ
programs/clickhouse DB::auto_changelog 292.03 KiB 371.81 KiB +79.78 KiB (+27.32%)

Job report

@Felixoid

Copy link
Copy Markdown
Member

Ok, now I get why Debug and CUDA are off for the base directories.

Let me drive the PR to sccache to introduce client-side reading of SCCACHE_BASEDIRS.

I'd still merge this part and not wait for the upstream PR. The remainings could be fixed in the following-up PRs

@groeneai

Copy link
Copy Markdown
Collaborator Author

Once a sccache release has mozilla/sccache#3065, I'll send the follow-up: CI moves to that release; the launchers pass SCCACHE_BASEDIRS=<source dir> with each path-mapped compile and an empty SCCACHE_BASEDIRS= with Debug and CUDA compiles, which have no path mapping, so a server started with basedirs no longer gives them another checkout's paths; the configure check of the running server with its sccache --stop-server warning, and CI's SCCACHE_BASEDIRS export with its Debug exception, are removed. Checkouts built at the same time then also share entries without a server restart.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 25, 2026
…se-across-checkouts

Resolve the conflict in contrib/compiler-rt-cmake/CMakeLists.txt: master
added -mbranch-protection= to the compiler-rt keep-list, this branch adds
-fdebug-compilation-dir= / -ffile-prefix-map=. Keep both. The list comment
now uses the same single line as ClickHouse#134009, which carries the same hunk, so
whichever of the two merges first leaves the other without a conflict.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov alexey-milovidov self-assigned this Nov 29, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 29, 2026
Merged via the queue into ClickHouse:master with commit 79dd230 Nov 30, 2026
205 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-build-system Build system (CMake, compilation, platform builds). manual approve Manual approve required to run CI pr-build Pull request with build/testing/packaging improvement pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants