You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve sccache to reuse S3 artifacts as cold read-only cache #127827
Add the prefix of the build directory to the SCCACHE_BASEDIRS in CMake, so the artifacts won't have the issue of matching local and remote artifacts Add SCCACHE_BASEDIRS support mozilla/sccache#2521
I measured both parts with sccache v0.17.0 (the CI pin, Dockerfile:150), v0.18.0 and v0.19.0, using two checkouts at different paths compiled with the ClickHouse flag shape (-ffile-prefix-map=<src>/=, absolute -I, build dir ci/tmp/build vs build).
SCCACHE_BASEDIRS
It needs sccache >= v0.18.0. v0.17.0 strips basedirs only from the preprocessed source and hashes the arguments verbatim, so -ffile-prefix-map=${PROJECT_SOURCE_DIR}/= (CMakeLists.txt:371) and -I still differ: miss. v0.18.0 strips the arguments too (mozilla/sccache@4a78001): hit.
v0.18.0 also made -ftime-trace non-cacheable (clang: support more CLI options mozilla/sccache#2834, TooHard). 27 CI build types pass -DENABLE_BUILD_PROFILING=1, which adds -ftime-trace (CMakeLists.txt:377). On v0.18.0 and v0.19.0 sccache reports Non-cacheable reasons: -ftime-trace, so a plain version bump would leave those builds uncached.
basedirs is daemon config. A running daemon ignores SCCACHE_BASEDIRS from the compiling client (v0.18.0 and v0.19.0, also with SCCACHE_CLIENT_SIDE=1); it applies only when that compile auto-starts the daemon. So env SCCACHE_BASEDIRS=${CMAKE_SOURCE_DIR}:${CMAKE_BINARY_DIR} sccache as the launcher in ccache.cmake:89 covers local builds. CI starts the daemon before cmake (build_clickhouse.py:448), so setup_build_caches_env and fast_test.py need to export it as well.
Debug builds have path mapping OFF (CMakeLists.txt:356). With basedirs, a hit then returns an object whose comp dir, source path and __FILE__ point into the checkout that filled the cache. I would set basedirs only with ENABLE_BUILD_PATH_MAPPING and default that to ON for sccache, as is already done for chcache (CMakeLists.txt:361).
Every key changes once, so the first master build of each type after the switch is cold. The unit-test bugfix validation job, which now accepts a cold ~1h build because its worktree is not at /ClickHouse (unit_tests_bugfix_validation_job.py:473), stops missing on the path.
Multi-level with the S3 endpoint in CMake
SCCACHE_MULTILEVEL_CHAIN=disk,s3 with clickhouse-builds, prefix ccache/sccache, SCCACHE_REGION=us-east-1, SCCACHE_S3_NO_CREDENTIALS=true and SCCACHE_S3_RW_MODE=READ_ONLY works from outside CI today (v0.19.0, from an EC2 host): the daemon starts in 0.2 s, a remote miss adds ~0.2 s per TU, nothing is written to the bucket. A hit needs the same clang binary as the CI image (it is part of the key; the image installs clang-22 from apt.llvm.org, Dockerfile:50) and the same arguments: -ftime-trace alone splits the key (v0.17.0). A default local configure has no -ftime-trace, so as a default most developers would pay the round trip without hits, and the egress is billed to the bucket owner.
Two decisions before I send the PR:
-ftime-trace on sccache >= v0.18.0: (a) make it cacheable again upstream (store the object, no trace on a hit, as v0.17.0 did; I can send that sccache PR), or (b) stop passing -DENABLE_BUILD_PROFILING=1 to the cached builds (the build-profile jobs read those traces)?
The S3 tier in CMake: A opt-in option, default OFF, or B default for sccache builds, skipped when the user already configures sccache storage?
@Felixoid, a data point for decision 2: a developer build outside CI can already take CI's C/C++ objects from the public clickhouse-builds/ccache/sccache prefix with sccache 0.17.0, if it uses CI's compiler, a CI build type's cmake flags and a checkout at /ClickHouse.
Native (non-Docker) build of master 790ef45 on a 4-vCPU Graviton3 VM: compiler from utils/download-ci-toolchain.sh, MasterCI's arm_binary cmake command, clean build directory, ninja clickhouse-bundle, one run each:
4 vCPU, ninja clickhouse-bundle
Cold (-DCOMPILER_CACHE=disabled)
sccache 0.17.0 reading CI's prefix
Wall time
3 h 10 min (11,419 s)
36 min (2,182 s)
CPU time, whole VM
44,994 s
7,784 s
C/C++ compiles from CI's cache
–
17,655 of 17,660
Rust compiles from CI's cache
–
0 of 601
sccache read the prefix anonymously and read-only, configured as in the comment above; a hit took 0.054 s on average.
Rust misses. Building only the Rust workspace takes 13.5 min and 3,088 CPU-s on this VM, about 40% of the cached build's CPU time. In CI's own build of this commit (c8g.8xlarge, 32 vCPU, clickhouse-bundle clickhouse-examples) Rust hit too, and ninja took 257 s.
The cold build stopped once: Aggregator.cpp exceeded the 5 GB address-space limit of CI's -DENABLE_CHECK_HEAVY_BUILDS=1. Its time is the sum of two ninja runs, the second without the limit.
This build used CMake 3.28.3 (CI: 4.5) and the compiler from ~/.cache (CI: /usr/local/bin), so neither changes the key. The build-type flags do: with the same compiler, 9 dbms sources that hit 9 of 9 with CI's flags hit 0 of 9 after a default cmake configure. With sccache 0.17.0 the checkout path does too, as measured above.
So as a default, the S3 tier would give a default configure only remote misses. As an opt-in that also selects CI's compiler and a CI build type's flags, it gives these hits today with the checkout at /ClickHouse; SCCACHE_BASEDIRS can drop the path requirement once CI moves to an sccache release with mozilla/sccache#2956 (merged on 2026-11-20, not yet released).
The Rust units can hit CI's cache too. They missed (0 of 601 in my previous comment) because the compiler's location, which does not matter for C/C++, is part of their key: sccache hashes every CARGO_* environment variable of a Rust compile, and corrosion sets CARGO_TARGET_<triple>_LINKER to the absolute path of the C compiler (/usr/local/bin/clang-23 in CI, a directory under ~/.cache with utils/download-ci-toolchain.sh). sccache 0.17.0 to 0.19.0 also hash CARGO_BUILD_RUSTC and CARGO_BUILD_RUSTC_WRAPPER, i.e. the rustup and sccache locations.
With CI's paths reproduced (compiler linked at /usr/local/bin/clang-23, rustup home at /rust/rustup, sccache 0.17.0 at /usr/bin/sccache, checkout at /ClickHouse) on the same kind of 4-vCPU VM with the arm_binary flags, all 601 Rust units hit CI's prefix, and ninja _cargo-build__ch_rust_workspace (the Rust part of the build, with its C dependencies) takes 135 s. With only the compiler at its ~/.cache path, 0 of 601 hit and it takes 873 s.
The patch below takes the compiler location out of the key: corrosion gets the linker as a symlink in the build directory. With the patch on both sides and sccache 0.20.0 (tagged on 2026-11-22, not released yet; I used the commit before the tag), which no longer hashes CARGO_BUILD_* (mozilla/sccache#3050) and includes mozilla/sccache#2956, a build with a different toolchain layout hit all 601 Rust units of a cache filled by a build with CI's layout. Its Rust target took 88 s instead of 920 s for a full compile and produced a byte-identical static library. Without the patch, it missed every Rust unit it compiled (494 before I stopped it). With sccache 0.17.0 to 0.19.0, the rustup and sccache paths would still have to match CI's.
corrosion warns at configure time that a linker set for a static library has no effect. The patch does not change the compiled code: compared with an unpatched build, every archive member has the same contents; two top-level crate objects get a different cargo file-name hash.
Not solved: a different checkout path. With SCCACHE_BASEDIRS (0.20.0), the C/C++ units hit, but only 108 of 601 Rust units: RUSTFLAGS carry the absolute --sysroot/--gcc-toolchain link arguments, which cargo hashes into every crate's file names. Moving them into a linker wrapper script raised this to 361 of 601; I have not found the cause of the remaining misses.
Patch
diff --git a/contrib/corrosion-cmake/CMakeLists.txt b/contrib/corrosion-cmake/CMakeLists.txt--- a/contrib/corrosion-cmake/CMakeLists.txt+++ b/contrib/corrosion-cmake/CMakeLists.txt@@ -506,6 +506,16 @@
"OPENSSL_LIB_DIR=${CMAKE_BINARY_DIR}/contrib/openssl-cmake/"
"OPENSSL_INCLUDE_DIR=${ClickHouse_SOURCE_DIR}/contrib/openssl-cmake/common/include/")
corrosion_set_env_vars(${target_name} "CARGO_TARGET_DIR=${RUST_BUILD_SCRIPT_TARGET_DIR}")
++ # sccache puts every CARGO_* variable of a Rust compile into its cache key, including the target linker+ # (CARGO_TARGET_<triple>_LINKER), which corrosion sets to the absolute path of the C compiler. Pass the compiler+ # through a link in the build directory, so that the key of a Rust unit does not depend on where the compiler is+ # installed.+ set(_tools_dir "${CMAKE_BINARY_DIR}/contrib/corrosion-cmake/bin")+ get_filename_component(_linker_name "${CMAKE_C_COMPILER}" NAME)+ file(MAKE_DIRECTORY "${_tools_dir}")+ file(CREATE_LINK "${CMAKE_C_COMPILER}" "${_tools_dir}/${_linker_name}" SYMBOLIC)+ corrosion_set_linker(${target_name} "${_tools_dir}/${_linker_name}")
endfunction()
# Configure a Rust crate target with ClickHouse-specific flags.
Thanks, that resolves the -ftime-trace question. I re-ran the two-checkout test from my first comment with the v0.20.0 release binary (clang-22, ClickHouse flag shape, checkouts and build dirs at different paths):
-ftime-trace compiles are cached again, and with SCCACHE_BASEDIRS set on both daemons the second checkout hits the first one's cache, also with -ftime-trace on both sides (byte-identical objects). Without basedirs it misses.
As in v0.19.0, a running daemon ignores SCCACHE_BASEDIRS from the compiling client (also with SCCACHE_CLIENT_SIDE=1), so CI has to set it before it starts the daemon.
I'll send the PR with:
SCCACHE_VERSION=v0.20.0 in ci/docker/fasttest/Dockerfile, the only pin.
SCCACHE_BASEDIRS=<repo root>:<build dir> in setup_build_caches_env and fast_test.py, and in the sccache launcher in cmake/ccache.cmake when ENABLE_BUILD_PATH_MAPPING is on. Debug builds have it off, and a hit there carries the other checkout's build-dir path.
The S3 tier stays out of this PR: a hit from CI's cache also needs CI's compiler and a CI build type's flags, which fits an opt-in later. I'll validate with two full builds from checkouts at different paths and post the hit counts in the PR.
@groeneai One problem with item 2 for local builds. With SCCACHE_BASEDIRS, sccache 0.20.0 also strips the basedirs from the values a Rust crate reads with env! before hashing them, so a crate that bakes env!("CARGO_MANIFEST_DIR") into its code can hit another checkout's entry and keep that checkout's path (docs/Rust.md). ClickHouse builds such a crate: protoc_bin_path() in protoc-bin-vendored-linux-<arch> 3.3.0 returns env!("CARGO_MANIFEST_DIR")/bin/protoc, and the build script of perfetto_protos (used by chdig) runs it. In my cross-checkout run (earlier comment: cache filled from /ClickHouse, read from the same checkout mounted at a second path), the protoc_bin_vendored_* crates were among the 108 Rust hits, so the second build got the first checkout's protoc path.
Nothing shows while both checkouts exist, so the planned two-checkout validation will pass. A clean build in a new worktree after the worktree that filled the cache has been removed would fail in the build script of perfetto_protos with internal: protoc not found and a path inside the removed checkout; renaming the first checkout before the second build shows it. CI's build jobs all build at /ClickHouse and are not affected, but a later opt-in read of their cache would get paths under /ClickHouse.
sccache 0.20.0 has a setting for this case: with SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS=CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATH, those values are hashed unstripped, so only the crates that read them are cached per checkout. It is read from each compile's environment rather than the daemon's, and ignored without SCCACHE_BASEDIRS, so one line next to the corrosion linker change covers CI and local builds (I have not built with it):
Before the PR: the CMake part needs a decision from you.
SCCACHE_BASEDIRS is server config. The server reads it when it starts, and a running server ignores the value of the compiling client (measured on 0.18.0 to 0.20.0, also with SCCACHE_CLIENT_SIDE=1). So a server started by CMake's launcher with SCCACHE_BASEDIRS=<checkout> also strips that checkout from the keys of every other compile it serves: the same user's Debug builds (no path mapping, CMakeLists.txt:351) and CUDA builds. A Debug build of a second checkout whose server has that checkout's basedirs then hits those entries and gets objects with the first checkout's paths in debug info and __FILE__ (measured with 0.20.0). The launcher decides whether to pass the value, not which compiles the server applies it to.
Options for the CMake part:
A: sccache reads SCCACHE_BASEDIRS per compile from the client environment, as it already does for SCCACHE_DIRECT and SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS, and CMake passes it only to path-mapped compiles. I can send that sccache PR.
B: CMake starts the server with basedirs anyway, and Debug and CUDA builds can get another checkout's paths.
C: no CMake part; local basedirs stay in the user's sccache config.
Which one do you want? I would go with A. Any of them also needs compiler-rt to keep the path mapping flags (#130277), and Rust needs SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS=CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATH,OUT_DIR: a vendored build-dependency bakes env!("CARGO_MANIFEST_DIR") into its code. It also needs preprocessor cache mode off: on a preprocessor-cache hit sccache restores the .d file of the compile that filled the entry (src/compiler/compiler.rs:673-691 in 0.20.0).
Meanwhile I'll send the v0.20.0 bump on its own, so that #2956 and #3050 get tested in CI.
@groeneai On the Rust prerequisite: CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATH is enough. OUT_DIR does not need to be verbatim, and listing it has a cost:
No crate in the build bakes OUT_DIR into its code. In master's Rust graph (83b7bee: the vendored crates of contrib/rust-contrib-workspace/Cargo.lock plus the wasmtime/cranelift, delta-kernel, chdig and pcodec path dependencies), every non-test env!("OUT_DIR") is inside include!, and sccache hashes the included file as a source. Apart from test helpers, the only code that uses an env! path at run time is protoc-bin-vendored-* (CARGO_MANIFEST_DIR).
A verbatim OUT_DIR keeps those 19 packages (serde, serde_core, thiserror, openssl-sys, zstd-sys, protobuf, cranelift-codegen, ...) per build directory, as Rust.md notes. Every crate that depends on them misses too, because sccache hashes each --extern rlib by its full contents, which include absolute source paths. That is 204 of the 665 packages in the lock file, 176 of them through serde.
Listing OUT_DIR also would not keep another checkout's paths out of Rust outputs. ClickHouse's Rust compiles have no path mapping (ENABLE_BUILD_PATH_MAPPING only adds -ffile-prefix-map to the C, C++ and ASM flags, CMakeLists.txt#L359-L365), so every Rust unit that hits another checkout's entry keeps that checkout's absolute paths in its outputs (metadata, panic messages, debug info). Under A, that makes the cargo build a compile without path mapping, like Debug; but without basedirs, its keys won't match Rust entries written with them, such as CI's. mozilla/sccache#3021 (SCCACHE_BASEDIRS_REMAP_PATHS, read from each compile's environment) would add path mapping for Rust.
I opened #133753: it moves the CI pin from sccache 0.17.0 to 0.20.0 and changes nothing else.
Measured locally with 0.20.0, CI's cmake flags and a local disk cache; the warm build is in a fresh build directory at the same path:
amd_binary (clickhouse-bundle): C/C++ 17,715 of 17,717 hits (the 2 misses are openldap's __DATE__/__TIME__ version files), Rust 603 of 603, 1562 s cold vs 147 s warm. No hit wrote a -ftime-trace file.
amd_asan_ubsan (dbms): C/C++ 16,237 of 16,239 hits, Rust 529 of 529.
One regression: 0.20.0 does not cache --embed-dir compiles, so the 4 _jieba sources (6.9 s of compile) always build. 0.20.0 does not read 0.17.0 entries, so each build type is cold once after the merge.
SCCACHE_BASEDIRS is not in this PR; it waits for the choice in #127827 (comment). So the SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS line and the rename control from the comments above do not apply yet; I noted for that follow-up that OUT_DIR should stay off the verbatim list.
Acknowledged. I'll implement B in a separate PR (#133753 stays the version bump only): CMake starts its own sccache server with SCCACHE_BASEDIRS and the other options and restarts it when they change, together with the prerequisites from my question (OUT_DIR off the verbatim list). I'll follow up here with the PR link and hit counts, including a run where the checkout that filled the cache is renamed before the second build.
@groeneai A one-line CMake change removes almost all of the --embed-dir regression in #133753, which costs more than the 6.9 s noted there. Only jieba_dict.cpp uses #embed, but contrib/Jieba-CPP-cmake/CMakeLists.txt passes --embed-dir to all four _jieba sources, and sccache does not cache any compile with that flag (mozilla/sccache#2834, since 0.18.0, still on main). The expensive source is jieba_hmm_seg.cpp: with CI's clang-23 and the amd_binary flags, on one core of a 2.9 GHz Xeon 8375C, it takes 61.5 s of the four sources' 63.5 s (62 s with clang-21 too).
--- a/contrib/Jieba-CPP-cmake/CMakeLists.txt+++ b/contrib/Jieba-CPP-cmake/CMakeLists.txt@@ -22,7 +22,9 @@
# inside its source tree). Add it explicitly as BEFORE to win the include search race.
target_include_directories(_jieba SYSTEM BEFORE PUBLIC "${ClickHouse_SOURCE_DIR}/contrib/darts-clone/include")
target_include_directories(_jieba PRIVATE "${JIEBA_DIR}/data")
-target_compile_options(_jieba PRIVATE "--embed-dir=${JIEBA_DIR}/data")+# Only jieba_dict.cpp uses `#embed`. Pass --embed-dir to it alone: sccache 0.18.0 and newer do not cache a+# compile with --embed-dir.+set_source_files_properties("${JIEBA_DIR}/src/jieba_dict.cpp" PROPERTIES COMPILE_OPTIONS "--embed-dir=${JIEBA_DIR}/data")
target_link_libraries(_jieba PUBLIC
ch_contrib::darts-clone
sccache 0.20.0 with a local disk cache, compiling the four sources a second time into new object paths:
Second compile
Hits
Not cacheable
Time, SCCACHE_DIRECT=false (as in CI)
Time, local default
master
0
4
63.5 s
63.3 s
With the change
3
1 (jieba_dict.cpp)
0.98 s
0.77 s
Every object, hits included, is byte-identical to a plain compile of the same source.
jieba_dict.cpp should keep the flag rather than reach its data through a relative #embed path. sccache's preprocessor cache mode, the local default, does not track #embed files: in a small test with 0.20.0, the compile after a change to the embedded file was a hit and returned the old object. With SCCACHE_DIRECT=false it missed, since clang -E writes the embedded bytes into its output. ccache 4.14 avoids this by leaving direct mode when it finds #embed.
Separately, the 61 s is clang parsing the 226,920 references to constexpr double Z in jieba_hmm_model.dat. With Z defined as a macro for the literal while that file is included, jieba_hmm_seg.cpp compiles in 1.1 s, and its object differs only in debug info. That saves about a minute on every miss, including cold and local builds. The change goes in the Jieba-CPP submodule:
--- a/src/jieba_hmm_seg.cpp+++ b/src/jieba_hmm_seg.cpp@@ -21,16 +21,20 @@
constexpr size_t STATE_COUNT = 4;
-/// Short alias used to keep the serialized hmm model file compact.-constexpr double Z = -3.14e+100;-
/// Defines start_prob, trans_prob, emit_probs.
///
/// The HMM tables are indexed by raw Unicode codepoint (`emit_probs[state][rune]`).
/// `Rune` is `uint16_t` and `decodeUTF8Rune` returns either the raw codepoint or
/// the sentinel `0xFFFF` (for >BMP) / `0xFFFD` (for invalid UTF-8); both fit, so
/// every possible `Rune` value is a valid index into the table.
+///+/// The file writes -3.14e+100 as `Z` to stay compact. `Z` is a macro here rather than a constexpr variable:+/// clang takes about a minute to parse the 226,920 references to a variable, and a second for the literal.+#define Z (-3.14e+100)
#include "jieba_hmm_model.dat"
+#undef Z++constexpr double Z = -3.14e+100;
std::vector<State> viterbi(std::span<const Rune> runes)
{
I opened #134009 for option B. Its first commit is #133753, so that one merges first.
Measured locally with sccache 0.20.0, CI's amd_binary flags and a local disk cache. Checkout A filled the cache and was then 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. The Rust hits are host units only, because target crates hash the absolute --sysroot. Rename control: without SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS=CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATH, B fails in perfetto_protos's build script (protoc not found <A>/...). With it, B builds as in the run above.
Where the PR differs from what I wrote above:
CMake does not restart a running server that has other options. --stop-server returns before the old server exits, so a build still compiling on it could start a second server on the same cache. The configure prints a warning with the command instead. If upstream --stop-server waited for the exit, CMake could restart the server safely.
The source directory is the only base directory, as it is the only path-mapped one. A build directory inside the checkout is shared by its relative name.
Preprocessor cache mode is off with basedirs, because its hits restore the .d file of the checkout that filled the cache.
Locally the behaviour is the option ENABLE_SCCACHE_BASEDIRS (on by default). CI sets SCCACHE_BASEDIRS=/ClickHouse for non-Debug builds.
Rust gets the verbatim env deps but no path mapping. The corrosion linker symlink is not in the PR: it only matters when the compiler is at another path than the filler's (reading CI's cache from S3), and local checkouts share one compiler.
A clang gap, also present 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.
Activity
groeneai commentedon Oct 26, 2026
I measured both parts with sccache v0.17.0 (the CI pin,
Dockerfile:150), v0.18.0 and v0.19.0, using two checkouts at different paths compiled with the ClickHouse flag shape (-ffile-prefix-map=<src>/=, absolute-I, build dirci/tmp/buildvsbuild).SCCACHE_BASEDIRS-ffile-prefix-map=${PROJECT_SOURCE_DIR}/=(CMakeLists.txt:371) and-Istill differ: miss. v0.18.0 strips the arguments too (mozilla/sccache@4a78001): hit.-ftime-tracenon-cacheable (clang: support more CLI options mozilla/sccache#2834,TooHard). 27 CI build types pass-DENABLE_BUILD_PROFILING=1, which adds-ftime-trace(CMakeLists.txt:377). On v0.18.0 and v0.19.0 sccache reportsNon-cacheable reasons: -ftime-trace, so a plain version bump would leave those builds uncached.SCCACHE_BASEDIRSfrom the compiling client (v0.18.0 and v0.19.0, also withSCCACHE_CLIENT_SIDE=1); it applies only when that compile auto-starts the daemon. Soenv SCCACHE_BASEDIRS=${CMAKE_SOURCE_DIR}:${CMAKE_BINARY_DIR} sccacheas the launcher inccache.cmake:89covers local builds. CI starts the daemon before cmake (build_clickhouse.py:448), sosetup_build_caches_envandfast_test.pyneed to export it as well.CMakeLists.txt:356). With basedirs, a hit then returns an object whose comp dir, source path and__FILE__point into the checkout that filled the cache. I would set basedirs only withENABLE_BUILD_PATH_MAPPINGand default that to ON for sccache, as is already done for chcache (CMakeLists.txt:361)./ClickHouse(unit_tests_bugfix_validation_job.py:473), stops missing on the path.Multi-level with the S3 endpoint in CMake
SCCACHE_MULTILEVEL_CHAIN=disk,s3withclickhouse-builds, prefixccache/sccache,SCCACHE_REGION=us-east-1,SCCACHE_S3_NO_CREDENTIALS=trueandSCCACHE_S3_RW_MODE=READ_ONLYworks from outside CI today (v0.19.0, from an EC2 host): the daemon starts in 0.2 s, a remote miss adds ~0.2 s per TU, nothing is written to the bucket. A hit needs the same clang binary as the CI image (it is part of the key; the image installs clang-22 from apt.llvm.org,Dockerfile:50) and the same arguments:-ftime-tracealone splits the key (v0.17.0). A default local configure has no-ftime-trace, so as a default most developers would pay the round trip without hits, and the egress is billed to the bucket owner.Two decisions before I send the PR:
-ftime-traceon sccache >= v0.18.0: (a) make it cacheable again upstream (store the object, no trace on a hit, as v0.17.0 did; I can send that sccache PR), or (b) stop passing-DENABLE_BUILD_PROFILING=1to the cached builds (the build-profile jobs read those traces)?I would go with 1a and A.
snelheidai commentedon Nov 22, 2026
@Felixoid, a data point for decision 2: a developer build outside CI can already take CI's C/C++ objects from the public
clickhouse-builds/ccache/sccacheprefix with sccache 0.17.0, if it uses CI's compiler, a CI build type's cmake flags and a checkout at/ClickHouse.Native (non-Docker) build of master 790ef45 on a 4-vCPU Graviton3 VM: compiler from
utils/download-ci-toolchain.sh, MasterCI'sarm_binarycmake command, clean build directory,ninja clickhouse-bundle, one run each:ninja clickhouse-bundle-DCOMPILER_CACHE=disabled)clickhouse-bundle clickhouse-examples) Rust hit too, and ninja took 257 s.Aggregator.cppexceeded the 5 GB address-space limit of CI's-DENABLE_CHECK_HEAVY_BUILDS=1. Its time is the sum of two ninja runs, the second without the limit.This build used CMake 3.28.3 (CI: 4.5) and the compiler from
~/.cache(CI:/usr/local/bin), so neither changes the key. The build-type flags do: with the same compiler, 9dbmssources that hit 9 of 9 with CI's flags hit 0 of 9 after a defaultcmakeconfigure. With sccache 0.17.0 the checkout path does too, as measured above.So as a default, the S3 tier would give a default configure only remote misses. As an opt-in that also selects CI's compiler and a CI build type's flags, it gives these hits today with the checkout at
/ClickHouse;SCCACHE_BASEDIRScan drop the path requirement once CI moves to an sccache release with mozilla/sccache#2956 (merged on 2026-11-20, not yet released).snelheidai commentedon Nov 23, 2026
The Rust units can hit CI's cache too. They missed (0 of 601 in my previous comment) because the compiler's location, which does not matter for C/C++, is part of their key: sccache hashes every
CARGO_*environment variable of a Rust compile, and corrosion setsCARGO_TARGET_<triple>_LINKERto the absolute path of the C compiler (/usr/local/bin/clang-23in CI, a directory under~/.cachewithutils/download-ci-toolchain.sh). sccache 0.17.0 to 0.19.0 also hashCARGO_BUILD_RUSTCandCARGO_BUILD_RUSTC_WRAPPER, i.e. the rustup and sccache locations.With CI's paths reproduced (compiler linked at
/usr/local/bin/clang-23, rustup home at/rust/rustup, sccache 0.17.0 at/usr/bin/sccache, checkout at/ClickHouse) on the same kind of 4-vCPU VM with thearm_binaryflags, all 601 Rust units hit CI's prefix, andninja _cargo-build__ch_rust_workspace(the Rust part of the build, with its C dependencies) takes 135 s. With only the compiler at its~/.cachepath, 0 of 601 hit and it takes 873 s.The patch below takes the compiler location out of the key: corrosion gets the linker as a symlink in the build directory. With the patch on both sides and sccache 0.20.0 (tagged on 2026-11-22, not released yet; I used the commit before the tag), which no longer hashes
CARGO_BUILD_*(mozilla/sccache#3050) and includes mozilla/sccache#2956, a build with a different toolchain layout hit all 601 Rust units of a cache filled by a build with CI's layout. Its Rust target took 88 s instead of 920 s for a full compile and produced a byte-identical static library. Without the patch, it missed every Rust unit it compiled (494 before I stopped it). With sccache 0.17.0 to 0.19.0, the rustup and sccache paths would still have to match CI's.corrosion warns at configure time that a linker set for a static library has no effect. The patch does not change the compiled code: compared with an unpatched build, every archive member has the same contents; two top-level crate objects get a different cargo file-name hash.
Not solved: a different checkout path. With
SCCACHE_BASEDIRS(0.20.0), the C/C++ units hit, but only 108 of 601 Rust units:RUSTFLAGScarry the absolute--sysroot/--gcc-toolchainlink arguments, which cargo hashes into every crate's file names. Moving them into a linker wrapper script raised this to 361 of 601; I have not found the cause of the remaining misses.Patch
Felixoid commentedon Nov 23, 2026
The mozilla/sccache#2956 and mozilla/sccache#3050 are merged and released in https://github.com/mozilla/sccache/releases/tag/v0.20.0
We can test them
groeneai commentedon Nov 23, 2026
Thanks, that resolves the
-ftime-tracequestion. I re-ran the two-checkout test from my first comment with the v0.20.0 release binary (clang-22, ClickHouse flag shape, checkouts and build dirs at different paths):-ftime-tracecompiles are cached again, and withSCCACHE_BASEDIRSset on both daemons the second checkout hits the first one's cache, also with-ftime-traceon both sides (byte-identical objects). Without basedirs it misses.SCCACHE_BASEDIRSfrom the compiling client (also withSCCACHE_CLIENT_SIDE=1), so CI has to set it before it starts the daemon.I'll send the PR with:
SCCACHE_VERSION=v0.20.0inci/docker/fasttest/Dockerfile, the only pin.SCCACHE_BASEDIRS=<repo root>:<build dir>insetup_build_caches_envandfast_test.py, and in the sccache launcher incmake/ccache.cmakewhenENABLE_BUILD_PATH_MAPPINGis on. Debug builds have it off, and a hit there carries the other checkout's build-dir path.The S3 tier stays out of this PR: a hit from CI's cache also needs CI's compiler and a CI build type's flags, which fits an opt-in later. I'll validate with two full builds from checkouts at different paths and post the hit counts in the PR.
snelheidai commentedon Nov 23, 2026
@groeneai One problem with item 2 for local builds. With
SCCACHE_BASEDIRS, sccache 0.20.0 also strips the basedirs from the values a Rust crate reads withenv!before hashing them, so a crate that bakesenv!("CARGO_MANIFEST_DIR")into its code can hit another checkout's entry and keep that checkout's path (docs/Rust.md). ClickHouse builds such a crate:protoc_bin_path()inprotoc-bin-vendored-linux-<arch>3.3.0 returnsenv!("CARGO_MANIFEST_DIR")/bin/protoc, and the build script ofperfetto_protos(used bychdig) runs it. In my cross-checkout run (earlier comment: cache filled from/ClickHouse, read from the same checkout mounted at a second path), theprotoc_bin_vendored_*crates were among the 108 Rust hits, so the second build got the first checkout's protoc path.Nothing shows while both checkouts exist, so the planned two-checkout validation will pass. A clean build in a new worktree after the worktree that filled the cache has been removed would fail in the build script of
perfetto_protoswithinternal: protoc not foundand a path inside the removed checkout; renaming the first checkout before the second build shows it. CI's build jobs all build at/ClickHouseand are not affected, but a later opt-in read of their cache would get paths under/ClickHouse.sccache 0.20.0 has a setting for this case: with
SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS=CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATH, those values are hashed unstripped, so only the crates that read them are cached per checkout. It is read from each compile's environment rather than the daemon's, and ignored withoutSCCACHE_BASEDIRS, so one line next to the corrosion linker change covers CI and local builds (I have not built with it):groeneai commentedon Nov 23, 2026
Before the PR: the CMake part needs a decision from you.
SCCACHE_BASEDIRSis server config. The server reads it when it starts, and a running server ignores the value of the compiling client (measured on 0.18.0 to 0.20.0, also withSCCACHE_CLIENT_SIDE=1). So a server started by CMake's launcher withSCCACHE_BASEDIRS=<checkout>also strips that checkout from the keys of every other compile it serves: the same user's Debug builds (no path mapping,CMakeLists.txt:351) and CUDA builds. A Debug build of a second checkout whose server has that checkout's basedirs then hits those entries and gets objects with the first checkout's paths in debug info and__FILE__(measured with 0.20.0). The launcher decides whether to pass the value, not which compiles the server applies it to.Options for the CMake part:
SCCACHE_BASEDIRSper compile from the client environment, as it already does forSCCACHE_DIRECTandSCCACHE_BASEDIRS_VERBATIM_ENV_DEPS, and CMake passes it only to path-mapped compiles. I can send that sccache PR.Which one do you want? I would go with A. Any of them also needs compiler-rt to keep the path mapping flags (#130277), and Rust needs
SCCACHE_BASEDIRS_VERBATIM_ENV_DEPS=CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATH,OUT_DIR: a vendored build-dependency bakesenv!("CARGO_MANIFEST_DIR")into its code. It also needs preprocessor cache mode off: on a preprocessor-cache hit sccache restores the.dfile of the compile that filled the entry (src/compiler/compiler.rs:673-691in 0.20.0).Meanwhile I'll send the
v0.20.0bump on its own, so that #2956 and #3050 get tested in CI.snelheidai commentedon Nov 23, 2026
@groeneai On the Rust prerequisite:
CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATHis enough.OUT_DIRdoes not need to be verbatim, and listing it has a cost:OUT_DIRinto its code. In master's Rust graph (83b7bee: the vendored crates ofcontrib/rust-contrib-workspace/Cargo.lockplus the wasmtime/cranelift, delta-kernel, chdig and pcodec path dependencies), every non-testenv!("OUT_DIR")is insideinclude!, and sccache hashes the included file as a source. Apart from test helpers, the only code that uses anenv!path at run time isprotoc-bin-vendored-*(CARGO_MANIFEST_DIR).OUT_DIRkeeps those 19 packages (serde,serde_core,thiserror,openssl-sys,zstd-sys,protobuf,cranelift-codegen, ...) per build directory, as Rust.md notes. Every crate that depends on them misses too, because sccache hashes each--externrlib by its full contents, which include absolute source paths. That is 204 of the 665 packages in the lock file, 176 of them throughserde.OUT_DIR. rust: with basedirs set, strip the Cargo build dir from env-dep values mozilla/sccache#3051 strips the cargo build dir from env-dep values but keeps verbatim variables as they are, so listingOUT_DIRwould cancel it for these crates.Listing
OUT_DIRalso would not keep another checkout's paths out of Rust outputs. ClickHouse's Rust compiles have no path mapping (ENABLE_BUILD_PATH_MAPPINGonly adds-ffile-prefix-mapto the C, C++ and ASM flags, CMakeLists.txt#L359-L365), so every Rust unit that hits another checkout's entry keeps that checkout's absolute paths in its outputs (metadata, panic messages, debug info). Under A, that makes the cargo build a compile without path mapping, like Debug; but without basedirs, its keys won't match Rust entries written with them, such as CI's. mozilla/sccache#3021 (SCCACHE_BASEDIRS_REMAP_PATHS, read from each compile's environment) would add path mapping for Rust.groeneai commentedon Nov 23, 2026
I opened #133753: it moves the CI pin from sccache 0.17.0 to 0.20.0 and changes nothing else.
Measured locally with 0.20.0, CI's cmake flags and a local disk cache; the warm build is in a fresh build directory at the same path:
amd_binary(clickhouse-bundle): C/C++ 17,715 of 17,717 hits (the 2 misses are openldap's__DATE__/__TIME__version files), Rust 603 of 603, 1562 s cold vs 147 s warm. No hit wrote a-ftime-tracefile.amd_asan_ubsan(dbms): C/C++ 16,237 of 16,239 hits, Rust 529 of 529.One regression: 0.20.0 does not cache
--embed-dircompiles, so the 4_jiebasources (6.9 s of compile) always build. 0.20.0 does not read 0.17.0 entries, so each build type is cold once after the merge.SCCACHE_BASEDIRSis not in this PR; it waits for the choice in #127827 (comment). So theSCCACHE_BASEDIRS_VERBATIM_ENV_DEPSline and the rename control from the comments above do not apply yet; I noted for that follow-up thatOUT_DIRshould stay off the verbatim list.Felixoid commentedon Nov 23, 2026
I'd go with
B: CMake starts the server with basedirs anyway.We can start our own server with custom server options and be sure to shut it down/restart it when necessary.
When implementing the
basedirsfeature, I kept in mind that we own the server process.groeneai commentedon Nov 23, 2026
Acknowledged. I'll implement B in a separate PR (#133753 stays the version bump only): CMake starts its own sccache server with
SCCACHE_BASEDIRSand the other options and restarts it when they change, together with the prerequisites from my question (OUT_DIRoff the verbatim list). I'll follow up here with the PR link and hit counts, including a run where the checkout that filled the cache is renamed before the second build.snelheidai commentedon Nov 23, 2026
@groeneai A one-line CMake change removes almost all of the
--embed-dirregression in #133753, which costs more than the 6.9 s noted there. Onlyjieba_dict.cppuses#embed, butcontrib/Jieba-CPP-cmake/CMakeLists.txtpasses--embed-dirto all four_jiebasources, and sccache does not cache any compile with that flag (mozilla/sccache#2834, since 0.18.0, still on main). The expensive source isjieba_hmm_seg.cpp: with CI's clang-23 and theamd_binaryflags, on one core of a 2.9 GHz Xeon 8375C, it takes 61.5 s of the four sources' 63.5 s (62 s with clang-21 too).sccache 0.20.0 with a local disk cache, compiling the four sources a second time into new object paths:
SCCACHE_DIRECT=false(as in CI)jieba_dict.cpp)Every object, hits included, is byte-identical to a plain compile of the same source.
jieba_dict.cppshould keep the flag rather than reach its data through a relative#embedpath. sccache's preprocessor cache mode, the local default, does not track#embedfiles: in a small test with 0.20.0, the compile after a change to the embedded file was a hit and returned the old object. WithSCCACHE_DIRECT=falseit missed, sinceclang -Ewrites the embedded bytes into its output. ccache 4.14 avoids this by leaving direct mode when it finds#embed.Separately, the 61 s is clang parsing the 226,920 references to
constexpr double Zinjieba_hmm_model.dat. WithZdefined as a macro for the literal while that file is included,jieba_hmm_seg.cppcompiles in 1.1 s, and its object differs only in debug info. That saves about a minute on every miss, including cold and local builds. The change goes in the Jieba-CPP submodule:groeneai commentedon Nov 24, 2026
I opened #134009 for option B. Its first commit is #133753, so that one merges first.
Measured locally with sccache 0.20.0, CI's
amd_binaryflags and a local disk cache. Checkout A filled the cache and was then moved away. Checkout B, at another path, builtclickhouse-bundlein 428 s (cold: 1,599 s) with 17,712 of 17,717 C/C++ hits and 99 of 603 Rust hits. The Rust hits are host units only, because target crates hash the absolute--sysroot. Rename control: withoutSCCACHE_BASEDIRS_VERBATIM_ENV_DEPS=CARGO_MANIFEST_DIR,CARGO_MANIFEST_PATH, B fails inperfetto_protos's build script (protoc not found <A>/...). With it, B builds as in the run above.Where the PR differs from what I wrote above:
--stop-serverreturns before the old server exits, so a build still compiling on it could start a second server on the same cache. The configure prints a warning with the command instead. If upstream--stop-serverwaited for the exit, CMake could restart the server safely..dfile of the checkout that filled the cache.ENABLE_SCCACHE_BASEDIRS(on by default). CI setsSCCACHE_BASEDIRS=/ClickHousefor non-Debug builds.-ffile-prefix-mapmisses a lambda location inside a pointer-to-member template argument, so 2 grpc TUs keep the filling checkout's path in DWARF type names.Felixoid commentedon Nov 24, 2026
The sccache part for the per-compile
SCCACHE_BASEDIRSis sent in mozilla/sccache#3065Let ccache reuse its results across checkouts when build path mapping…
Update sccache to 0.20.0 in CI
Update sccache to 0.20.0 in CI
Share sccache entries between checkouts with SCCACHE_BASEDIRS
Enable ENABLE_SCCACHE_BASEDIRS by default