Repository navigation
Build the CI toolchain with jemalloc - #131183
Conversation
clang and lld spend a measurable share of a ClickHouse build in glibc malloc (lld runs one ThinLTO backend per core). Link the toolchain's executables with Ubuntu's static jemalloc, the recipe LLVM itself uses for its BOLT image (bolt/utils/docker/Dockerfile): libjemalloc-dev is added to the binary-builder image, and stage 2 links libjemalloc_pic.a with --whole-archive, so malloc and operator new/delete of clang, lld and the other LLVM tools resolve to jemalloc inside the executable. The tarball stays self-contained: no runtime dependency on libjemalloc.so.2 is added. Why -lstdc++ -lm: CMake also links its compiler check at project() and every try_compile/check_* probe with CMAKE_EXE_LINKER_FLAGS (CMP0056 is NEW under LLVM's cmake_minimum_required), and those C links pull in the archive's C++ part (jemalloc_cpp.pic.o needs libstdc++) and, on amd64, the --enable-prof code (exp, log, round from libm). Without them the stage 2 configure fails with "The C compiler ... is not able to compile a simple test program". -lpthread and -ldl of the upstream line are not needed with glibc >= 2.34. Ubuntu builds the package with --with-lg-page=12 on amd64 and 16 on arm64, so the aarch64 toolchain also runs on 64 KiB-page kernels, and with --with-lg-hugepage=21. It is the same 5.3.0 build as libjemalloc2, which was used for the measurements. Shared libraries (libclang.so, libLTO.so, LLVMgold.so), stage 1 (the instrumented build) and the compiler-rt runtimes and builtins are unchanged: CMAKE_SHARED_LINKER_FLAGS is not touched, and CMAKE_EXE_LINKER_FLAGS is not passed to the runtimes' or bolt_rt's external projects. -znow, --emit-relocs, PIE and RELRO are kept. The memory columns of -ftime-report (glibc mallinfo2) no longer report the toolchain's heap. It takes effect once OptimizeToolchain runs on master and the toolchain pin in the binary-builder Dockerfile moves. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The toolchain's executables now contain jemalloc, which is BSD-2-Clause: a binary redistribution must reproduce its copyright notice, conditions and disclaimer, and the archive is published. The package stage installs the copyright file of the image's libjemalloc-dev package (/usr/share/doc/libjemalloc-dev/copyright) as share/doc/jemalloc/copyright, after the strip step; when the file is missing, the stage fails and no archive is written. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex) plus my own review, two rounds. Round 1: 2 findings + 1 nit. Round 2 (re-review of the fix): 0 findings.
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261113-021300 |
|
Workflow [PR], commit [e141ba8] Summary: ✅
AI ReviewSummaryPR #131183, reviewed at Findings
Tests
Final VerdictStatus: |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units6 translation units recompiled, 4 s compile time in total, 6 of them have a recent master baseline. |
| binutils-dev \ | ||
| build-essential \ | ||
| zlib1g-dev \ | ||
| libjemalloc-dev \ |
There was a problem hiding this comment.
No. Ubuntu 24.04 ships jemalloc 5.3.0 (5.3.0-2build1, the upstream release of 2022-05-06). Upstream has since released 5.3.1 (2026-04-13) and 5.4.0 (2026-09-17), and dev is 95 commits past 5.4.0. I'm measuring a from-source build against it now.
|
@groeneai, also test the fresh jemalloc built from the source, and test mimalloc the same way. |
|
Acknowledged. I'm measuring the toolchain linked with jemalloc built from source (5.4.0 and |
c65bdd8
|
@alexey-milovidov @groeneai, Graviton3 results for this comparison. Nearly all CI build jobs run on Graviton runners (the amd64 ones cross-compile); @groeneai is measuring x86. jemalloc 5.4.0 built from source compiles no faster than Ubuntu's 5.3.0; 5.4.0 and
The clean builds use the method of #130705 (comment): the allocator is preloaded into every With huge pages, system CPU time fell by two thirds and page faults by about 90% (1.5-1.6 million huge-page faults per build, no compaction stalls). Memory: during a clean build, the lowest available memory was 95-97 GiB of 123 with THP-jemalloc, against 98-99 GiB with the other allocators. In the link, So the 13% does not need mimalloc. jemalloc 5.4.0 configured with Builds and per-run numbers# jemalloc 5.4.0 release tarball; the THP variant adds --with-malloc-conf=thp:always,metadata_thp:always
./configure --with-lg-page=16 && make
# jemalloc dev at ecb4556becc8
./autogen.sh --with-lg-page=16 && make
# mimalloc v3.6.1, defaults (MI_OVERRIDE=ON, MI_ALLOW_THP=FULL)
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release && cmake --build buildClean builds as in the earlier comment, with Links, each allocator twice, alternating with 5.3.0 links: 5.4.0 526.6 / 526.9 s, |
|
The x86 results (Xeon 6975P-C; non-PGO clang/lld 22.1.8 from one LLVM tree, differing only in the linked allocator). Compile: a fixed sample of 1192 of ClickHouse's translation units (8.5% of the compile time) rebuilt 6 times per variant, interleaved, on one 32-core NUMA node. Link: 5 interleaved ThinLTO links of clang.
Two compile runs of the same 5.3.0 toolchain differ by 3.7% (median) on this shared machine, so smaller compile differences are not resolved. As on Graviton3, 5.4.0 and Requesting huge pages is what makes mimalloc slow here: 12x the system time, and +0.9% without them. This host (up 106 days, THP The only measured win is snelheidai's THP-configured jemalloc 5.4.0 above (Graviton3: -13% clean build, -4.7% link). Should I open the PR that builds jemalloc 5.4.0 from source in the toolchain job with |
|
@alexey-milovidov, about @groeneai's question above (build jemalloc 5.4.0 with huge pages on, or keep 5.3.0): there is a third option. Keep 5.3.0 with its default options (5.4.0 by itself only links about 1% faster), and let CI turn huge pages on at run time. jemalloc reads Clean builds of
Every build produced the same The variable also reaches compilations that go through sccache: sccache 0.17.0, which CI uses, passes Once Patch (CI build jobs only)--- a/ci/jobs/build_clickhouse.py
+++ b/ci/jobs/build_clickhouse.py
@@ -317,6 +317,17 @@
setup_build_caches_env(info)
+ # CI build jobs run on freshly started instances with plenty of free 2 MiB blocks: back the heaps of compilers,
+ # linkers and clang-tidy with transparent huge pages, through MALLOC_CONF for the jemalloc linked into the
+ # toolchain's clang and lld, and through GLIBC_TUNABLES for tools that use glibc malloc (such as apt's
+ # clang-tidy). Not for local builds: on a long-running machine with fragmented memory, huge-page faults stall
+ # in direct compaction and make builds slower.
+ if not info.is_local_run:
+ os.environ.setdefault("MALLOC_CONF", "thp:always,metadata_thp:always")
+ tunables = os.environ.get("GLIBC_TUNABLES", "")
+ if "glibc.malloc.hugetlb" not in tunables:
+ os.environ["GLIBC_TUNABLES"] = ":".join(t for t in (tunables, "glibc.malloc.hugetlb=1") if t)
+
is_private = (
"PRIVATE_BUILDS_TO_CMAKE" in vars() or "PRIVATE_BUILDS_TO_CMAKE" in globals()
) |
|
@groeneai, I choose 5.4.0 with the built-in options. Send a PR. |
|
Acknowledged. I'm preparing the PR that builds jemalloc 5.4.0 from source with |
|
The PR: #131745. It takes effect once |
The toolchain job now builds jemalloc 5.4.0 from the sha256-pinned release tarball in a new stage and links its libjemalloc_pic.a into the stage 2 executables instead of Ubuntu noble's 5.3.0 from libjemalloc-dev, which is removed from the binary-builder image (nothing else used it). The archive is configured with --with-malloc-conf=thp:always,metadata_thp:always, so clang and lld ask for transparent huge pages for the heap and jemalloc's metadata (madvise on hosts in THP mode `madvise`); MALLOC_CONF still overrides it at run time. On Graviton3 the same jemalloc preloaded into the toolchain made a clean `ninja clickhouse` 13.2% faster and the ThinLTO link 4.7% faster (ClickHouse#131183 (comment)). --with-lg-page=16 on aarch64 because jemalloc refuses to start when the kernel page is larger than its own; --with-lg-hugepage=21 so the value does not depend on the build runner's /proc/meminfo; -g0 because configure adds -g3 by default. -lm is no longer needed: jemalloc only calls libm from its profiling code, which this build does not enable. The license now comes from the tarball's COPYING. Related: ClickHouse#131183 Related: ClickHouse#130064 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #130064
Related: #130705
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The CI toolchain (
clang,lldand the other LLVM tools built by theOptimizeToolchainworkflow) is now linked with jemalloc, so building ClickHouse with it is about 4-6% faster. The built binaries are unchanged.Description
clang and lld spend a measurable share of a ClickHouse build in glibc
malloc(lld runs one ThinLTO backend per core). The toolchain's executables now link Ubuntu's staticlibjemalloc_pic.awith--whole-archive, the recipe LLVM uses for its own BOLT image (bolt/utils/docker/Dockerfile):clickhouse/binary-builderinstallslibjemalloc-dev, and stage 2 adds-ljemalloc_pic -lstdc++ -lmtoCMAKE_EXE_LINKER_FLAGS. CMake also links its compiler checks with these flags, and the archive's C++ part and amd64 profiling code need libstdc++ and libm. It is Ubuntu 24.04's jemalloc 5.3.0, the same build as thelibjemalloc2the reporter preloaded (built for 64 KiB pages on arm64). Shared libraries, stage 1, the compiler-rt runtimes and-znow/--emit-relocs/PIE/RELRO are unchanged, and the tarball gains no runtime dependency and ships jemalloc's license (share/doc/jemalloc/copyright).It takes effect once
OptimizeToolchainruns on master and the toolchain pin inci/docker/binary-builder/Dockerfilemoves. With theci-toolchainlabel, this PR's CI also runs the amd64 and aarch64 toolchain builds.Validation on x86 (Xeon 6975P): the modified job ran from stage 2 to the package in the image with
libjemalloc-dev(BOLT applied), stage 2's configure results equal master's, andclang/lldresolvemallocandoperator newto jemalloc. Two non-PGO toolchains from one LLVM tree, differing only in the allocator, on the 32 physical cores of a NUMA node, nodes swapped: from-scratchninja clickhouse1281.7 / 1282.2 s -> 1200.9 / 1242.8 s (-4.7%), lld ThinLTO link ofclangmedian 58.2 -> 55.7 s (-4.3%, peak RSS -20%); all outputs byte-identical. Cross-built for aarch64 against the arm64 image with these flags,clangandlldconfigure and link, and under qemu they run on jemalloc (64 KiB pages) with output identical to master's flags. On Graviton3 the reporter measured -5.9% with jemalloc preloaded into the CI toolchain: #130705 (comment).Workflow [PR]
Sync PR [sync-upstream/pr/131183]
Version info
26.11.1.2181-master(included in26.11and later)