Skip to content

Integrate jemalloc with je_ prefix and remove usage of linker's --wrap - #99342

Merged
azat merged 1 commit into
ClickHouse:masterfrom
azat:jemalloc-je-prefix
Mar 16, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
azat:jemalloc-je-prefix

Conversation

@azat

@azat azat commented Mar 12, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Integrate jemalloc with je_ prefix and remove usage of linker's --wrap

Simplify jemalloc integration by replacing the three-mechanism approach (JEMALLOC_IS_MALLOC + JEMALLOC_OVERRIDE___LIBC_* + linker --wrap) with a cleaner two-step design:

  1. Compile jemalloc with je_ prefix so it only exports je_malloc, je_free, etc. (matching what Darwin already does).

  2. Define our own malloc/free in malloc.cpp that perform memory tracking and delegate to je_malloc/je_free. Provide __libc_* aliases on glibc to intercept internal calls.

Changes:

  • Enable JEMALLOC_PREFIX "je_" in all non-Darwin jemalloc_internal_defs.h.in
  • Undef JEMALLOC_IS_MALLOC and all JEMALLOC_OVERRIDE___LIBC_* defines
  • Remove JEMALLOC_OVERRIDE_MEMALIGN/VALLOC/PVALLOC from jemalloc_defs.h
  • Add JEMALLOC_NO_RENAME interface define so consumer code sees je_* names
  • Prefix all jemalloc API calls (mallctl -> je_mallctl, etc.) in ClickHouse
  • Rewrite memory.h to call je_malloc/je_free directly instead of __real_*
  • Rewrite malloc.cpp with tracking wrappers calling je_* underneath
  • Rewrite AllocationInterceptors.h to map __real_* -> je_* or ::*
  • Remove __wrap_* functions from AllocationInterceptors.cpp
  • Remove --wrap linker flags from CMakeLists.txt
  • Add jemalloc + clickhouse_common_io deps to clickhouse_malloc OBJECT library
  • And also fix JEMALLOC_CPREFIX for Darwin, that was the only platform that uses JE_MALLOC_CONF (over MALLOC_CONF)

Alternative to #98915 (but not complete, it does not touches memory tracker)


Note

High Risk
High risk because it changes the process-wide allocator interposition and memory tracking path (malloc/free, realloc, alignment allocators), which can impact correctness, performance, and platform-specific linking behavior (glibc/musl/FreeBSD/Darwin).

Overview
Reworks allocator interposition to use je_-prefixed jemalloc symbols everywhere. jemalloc is now built/consumed with JEMALLOC_PREFIX="je_" and JEMALLOC_NO_RENAME, and ClickHouse call sites are updated from mallctl/mallocx/malloc_stats_print/malloc_message to the corresponding je_* APIs.

Removes linker --wrap allocation hooks and moves C allocation tracking into code. The --wrap=malloc/free/etc. link options and __wrap_* implementations are deleted; Common/malloc.cpp now defines interposed malloc/free/calloc/realloc/posix_memalign/aligned_alloc/valloc/memalign/pvalloc/reallocarray/malloc_usable_size that track memory and delegate to je_*, plus __libc_* aliases on glibc.

Build/link wiring is adjusted so clickhouse_malloc links jemalloc when available, and the parser memory-profiler scripts/docs are updated to use MALLOC_CONF consistently.

Written by Cursor Bugbot for commit c6e3ef6. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.3.1.716

@azat
azat requested a review from antonio2368 March 12, 2026 17:39
@clickhouse-gh

clickhouse-gh Bot commented Mar 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c6e3ef6]

Summary: ✅


AI Review

Summary

This PR migrates allocator integration from linker --wrap interposition to direct jemalloc je_ symbol usage, introduces explicit malloc/free wrappers in src/Common/malloc.cpp, and updates all jemalloc control/stat calls accordingly. I reviewed the touched runtime allocator paths and related build wiring, and did not find new high-confidence correctness, safety, or performance regressions in the current PR head.

Missing context

  • ⚠️ No CI run logs or benchmark artifacts were provided in this review context.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsHistory.cpp ➖
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements submodule changed At least one submodule changed in this PR. labels Mar 12, 2026
Comment thread src/Common/malloc.cpp Resolved
Comment thread src/Common/malloc.cpp Resolved
Comment thread src/Common/malloc.cpp Resolved
@antonio2368 antonio2368 self-assigned this Mar 12, 2026
@azat
azat force-pushed the jemalloc-je-prefix branch from 0e04c5d to 7715a30 Compare March 12, 2026 18:38
Comment thread src/Common/malloc.cpp Resolved
@azat
azat force-pushed the jemalloc-je-prefix branch from 7715a30 to a56aec5 Compare March 12, 2026 18:43

@cursor cursor 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.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

Comment thread src/Common/malloc.cpp Resolved
Comment thread src/Common/malloc.cpp Resolved
@azat
azat force-pushed the jemalloc-je-prefix branch 2 times, most recently from 50f1f21 to 96bc08b Compare March 12, 2026 23:58
Comment thread src/Common/malloc.cpp Resolved
@azat
azat force-pushed the jemalloc-je-prefix branch from 96bc08b to 60aa4a2 Compare March 13, 2026 09:38
Comment thread src/Common/malloc.cpp Resolved
Simplify jemalloc integration by replacing the three-mechanism approach
(`JEMALLOC_IS_MALLOC` + `JEMALLOC_OVERRIDE___LIBC_*` + linker `--wrap`)
with a cleaner two-step design:

1. Compile jemalloc with `je_` prefix so it only exports `je_malloc`,
   `je_free`, etc. (matching what Darwin already does).

2. Define our own `malloc`/`free` in `malloc.cpp` that perform memory
   tracking and delegate to `je_malloc`/`je_free`. Provide `__libc_*`
   aliases on glibc to intercept internal calls.

Changes:
- Enable `JEMALLOC_PREFIX "je_"` in all non-Darwin `jemalloc_internal_defs.h.in`
- Undef `JEMALLOC_IS_MALLOC` and all `JEMALLOC_OVERRIDE___LIBC_*` defines
- Remove `JEMALLOC_OVERRIDE_MEMALIGN`/`VALLOC`/`PVALLOC` from `jemalloc_defs.h`
- Add `JEMALLOC_NO_RENAME` interface define so consumer code sees `je_*` names
- Prefix all jemalloc API calls (`mallctl` -> `je_mallctl`, etc.) in ClickHouse
- Rewrite `memory.h` to call `je_malloc`/`je_free` directly instead of `__real_*`
- Rewrite `malloc.cpp` with tracking wrappers calling `je_*` underneath
- Rewrite `AllocationInterceptors.h` to map `__real_*` -> `je_*` or `::*`
- Remove `__wrap_*` functions from `AllocationInterceptors.cpp`
- Remove `--wrap` linker flags from `CMakeLists.txt`
- Add jemalloc + clickhouse_common_io deps to `clickhouse_malloc` OBJECT library
- And also fix JEMALLOC_CPREFIX for Darwin, that was the only platform that uses JE_MALLOC_CONF (over MALLOC_CONF)

Co-Authored-By: Claude <[email protected]>
@azat
azat force-pushed the jemalloc-je-prefix branch from 60aa4a2 to c6e3ef6 Compare March 13, 2026 09:51
@clickhouse-gh

clickhouse-gh Bot commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.70% 83.70% +0.00%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 57.81% (174/301, 0 noise lines excluded)
Diff coverage report
Uncovered code

@azat
azat enabled auto-merge March 16, 2026 13:28
@azat
azat added this pull request to the merge queue Mar 16, 2026
Merged via the queue into ClickHouse:master with commit be3032f Mar 16, 2026
160 checks passed
@azat
azat deleted the jemalloc-je-prefix branch March 16, 2026 13:54
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 16, 2026
azat added a commit to azat/ClickHouse that referenced this pull request Apr 9, 2026
…emalloc-submodule-url"

This reverts commit 4bb7431, reversing
changes made to aef8cc3.

New version likely has some issues, I have a test that reproduce a
crashes, but with this patch it happens in jemalloc, before (but after ClickHouse#99342)
in random places.

Next step will be to try jemalloc debug mode, since likely when the
crash happens inside jemalloc, it is less likely will help.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

3 participants