From ea0ce694570f1e00f620fd83a29fadbd474d913f Mon Sep 17 00:00:00 2001 From: Wenxuan Date: Sun, 29 Sep 2024 22:03:15 +0900 Subject: [PATCH] *: Fix jemalloc in MacOS (#304) Signed-off-by: Wish --- contrib/jemalloc-cmake/CMakeLists.txt | 14 ++++++++- .../jemalloc/internal/jemalloc_preamble.h | 2 +- .../include/jemalloc/jemalloc.h | 2 +- .../include/jemalloc/jemalloc_rename.h | 31 ------------------- .../include/jemalloc/jemalloc_rename.h.in | 31 +++++++++++++++++++ .../internal/jemalloc_internal_defs.h.in | 4 +-- .../internal/jemalloc_internal_defs.h.in | 4 +-- .../internal/jemalloc_internal_defs.h.in | 4 +-- .../internal/jemalloc_internal_defs.h.in | 4 +-- .../internal/jemalloc_internal_defs.h.in | 4 +-- .../internal/jemalloc_internal_defs.h.in | 4 +-- .../internal/jemalloc_internal_defs.h.in | 4 +-- .../internal/jemalloc_internal_defs.h.in | 4 +-- .../internal/jemalloc_internal_defs.h.in | 4 +-- dbms/src/Interpreters/AsynchronousMetrics.cpp | 14 ++++----- 15 files changed, 71 insertions(+), 59 deletions(-) delete mode 100644 contrib/jemalloc-cmake/include/jemalloc/jemalloc_rename.h create mode 100644 contrib/jemalloc-cmake/include/jemalloc/jemalloc_rename.h.in diff --git a/contrib/jemalloc-cmake/CMakeLists.txt b/contrib/jemalloc-cmake/CMakeLists.txt index 2cc4a732f0c..8a38b1846ed 100644 --- a/contrib/jemalloc-cmake/CMakeLists.txt +++ b/contrib/jemalloc-cmake/CMakeLists.txt @@ -112,6 +112,12 @@ elseif (OS_FREEBSD) set (JEMALLOC_INCLUDE_PREFIX "include_freebsd") elseif (APPLE) set (JEMALLOC_INCLUDE_PREFIX "include_darwin") + + # Jemalloc is not supposed to replace `malloc`/`free`/etc. on MacOS. + # So we should explicitly prefix the jemalloc symbols. + # See https://github.com/jemalloc/jemalloc/blob/1978e5cdac731dca43b62e4b03612c0758f7cece/INSTALL.md?plain=1#L92-L94 + set (JEMALLOC_PREFIX "je_") + set (JEMALLOC_CPREFIX "JE_") else () message (FATAL_ERROR "internal jemalloc: This OS is not supported") endif () @@ -134,10 +140,16 @@ endif () configure_file(${JEMALLOC_INCLUDE_PREFIX}/jemalloc/internal/jemalloc_internal_defs.h.in ${JEMALLOC_INCLUDE_PREFIX}/jemalloc/internal/jemalloc_internal_defs.h) +configure_file(include/jemalloc/jemalloc_rename.h.in include/jemalloc/jemalloc_rename.h) + target_include_directories(jemalloc SYSTEM PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/${JEMALLOC_INCLUDE_PREFIX}/jemalloc/internal") -target_include_directories(jemalloc PUBLIC ${JEMALLOC_SOURCE_DIR}/include ${TiFlash_SOURCE_DIR}/contrib/jemalloc-cmake/include) +target_include_directories(jemalloc PUBLIC + ${CMAKE_CURRENT_BINARY_DIR}/include + ${CMAKE_CURRENT_BINARY_DIR}/include/jemalloc + ${JEMALLOC_SOURCE_DIR}/include + ${TiFlash_SOURCE_DIR}/contrib/jemalloc-cmake/include) target_compile_definitions(jemalloc PRIVATE -DJEMALLOC_NO_PRIVATE_NAMESPACE) diff --git a/contrib/jemalloc-cmake/include/jemalloc/internal/jemalloc_preamble.h b/contrib/jemalloc-cmake/include/jemalloc/internal/jemalloc_preamble.h index 45f43a6cd02..afc08f3a19b 100644 --- a/contrib/jemalloc-cmake/include/jemalloc/internal/jemalloc_preamble.h +++ b/contrib/jemalloc-cmake/include/jemalloc/internal/jemalloc_preamble.h @@ -1,8 +1,8 @@ #ifndef JEMALLOC_PREAMBLE_H #define JEMALLOC_PREAMBLE_H -#include "jemalloc/internal/jemalloc_internal_decls.h" #include "jemalloc_internal_defs.h" +#include "jemalloc/internal/jemalloc_internal_decls.h" #if defined(JEMALLOC_UTRACE) || defined(JEMALLOC_UTRACE_LABEL) #include diff --git a/contrib/jemalloc-cmake/include/jemalloc/jemalloc.h b/contrib/jemalloc-cmake/include/jemalloc/jemalloc.h index e90fa892100..7303340c3c0 100644 --- a/contrib/jemalloc-cmake/include/jemalloc/jemalloc.h +++ b/contrib/jemalloc-cmake/include/jemalloc/jemalloc.h @@ -10,9 +10,9 @@ extern "C" { #endif #include +#include #include #include -#include #include #if !defined(__clang__) diff --git a/contrib/jemalloc-cmake/include/jemalloc/jemalloc_rename.h b/contrib/jemalloc-cmake/include/jemalloc/jemalloc_rename.h deleted file mode 100644 index d032d46752d..00000000000 --- a/contrib/jemalloc-cmake/include/jemalloc/jemalloc_rename.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Name mangling for public symbols is controlled by --with-mangling and - * --with-jemalloc-prefix. With default settings the je_ prefix is stripped by - * these macro definitions. - */ -#ifndef JEMALLOC_NO_RENAME -#define je_aligned_alloc aligned_alloc -#define je_calloc calloc -#define je_dallocx dallocx -#define je_free free -#define je_mallctl mallctl -#define je_mallctlbymib mallctlbymib -#define je_mallctlnametomib mallctlnametomib -#define je_malloc malloc -#define je_malloc_conf malloc_conf -#define je_malloc_conf_2_conf_harder malloc_conf_2_conf_harder -#define je_malloc_message malloc_message -#define je_malloc_stats_print malloc_stats_print -#define je_malloc_usable_size malloc_usable_size -#define je_mallocx mallocx -#define je_smallocx_ca709c3139f77f4c00a903cdee46d71e9028f6c6 smallocx_ca709c3139f77f4c00a903cdee46d71e9028f6c6 -#define je_nallocx nallocx -#define je_posix_memalign posix_memalign -#define je_rallocx rallocx -#define je_realloc realloc -#define je_sallocx sallocx -#define je_sdallocx sdallocx -#define je_xallocx xallocx -#define je_memalign memalign -#define je_valloc valloc -#endif diff --git a/contrib/jemalloc-cmake/include/jemalloc/jemalloc_rename.h.in b/contrib/jemalloc-cmake/include/jemalloc/jemalloc_rename.h.in new file mode 100644 index 00000000000..6762c99a0ad --- /dev/null +++ b/contrib/jemalloc-cmake/include/jemalloc/jemalloc_rename.h.in @@ -0,0 +1,31 @@ +/* + * Name mangling for public symbols is controlled by --with-mangling and + * --with-jemalloc-prefix. With default settings the je_ prefix is stripped by + * these macro definitions. + */ +#ifndef JEMALLOC_NO_RENAME +#define je_aligned_alloc @JEMALLOC_PREFIX@aligned_alloc +#define je_calloc @JEMALLOC_PREFIX@calloc +#define je_dallocx @JEMALLOC_PREFIX@dallocx +#define je_free @JEMALLOC_PREFIX@free +#define je_mallctl @JEMALLOC_PREFIX@mallctl +#define je_mallctlbymib @JEMALLOC_PREFIX@mallctlbymib +#define je_mallctlnametomib @JEMALLOC_PREFIX@mallctlnametomib +#define je_malloc @JEMALLOC_PREFIX@malloc +#define je_malloc_conf @JEMALLOC_PREFIX@malloc_conf +#define je_malloc_conf_2_conf_harder @JEMALLOC_PREFIX@malloc_conf_2_conf_harder +#define je_malloc_message @JEMALLOC_PREFIX@malloc_message +#define je_malloc_stats_print @JEMALLOC_PREFIX@malloc_stats_print +#define je_malloc_usable_size @JEMALLOC_PREFIX@malloc_usable_size +#define je_mallocx @JEMALLOC_PREFIX@mallocx +#define je_smallocx_ca709c3139f77f4c00a903cdee46d71e9028f6c6 @JEMALLOC_PREFIX@smallocx_ca709c3139f77f4c00a903cdee46d71e9028f6c6 +#define je_nallocx @JEMALLOC_PREFIX@nallocx +#define je_posix_memalign @JEMALLOC_PREFIX@posix_memalign +#define je_rallocx @JEMALLOC_PREFIX@rallocx +#define je_realloc @JEMALLOC_PREFIX@realloc +#define je_sallocx @JEMALLOC_PREFIX@sallocx +#define je_sdallocx @JEMALLOC_PREFIX@sdallocx +#define je_xallocx @JEMALLOC_PREFIX@xallocx +#define je_memalign @JEMALLOC_PREFIX@memalign +#define je_valloc @JEMALLOC_PREFIX@valloc +#endif diff --git a/contrib/jemalloc-cmake/include_darwin_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in b/contrib/jemalloc-cmake/include_darwin_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in index 8ad95c51560..3995b31e7a4 100644 --- a/contrib/jemalloc-cmake/include_darwin_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/contrib/jemalloc-cmake/include_darwin_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in @@ -6,8 +6,8 @@ * public APIs to be prefixed. This makes it possible, with some care, to use * multiple allocators simultaneously. */ -#define JEMALLOC_PREFIX "je_" -#define JEMALLOC_CPREFIX "JE_" +#define JEMALLOC_PREFIX "@JEMALLOC_PREFIX@" +#define JEMALLOC_CPREFIX "@JEMALLOC_CPREFIX@" /* * Define overrides for non-standard allocator-related functions if they are diff --git a/contrib/jemalloc-cmake/include_darwin_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in b/contrib/jemalloc-cmake/include_darwin_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in index 8671da5db69..a141b8c97dc 100644 --- a/contrib/jemalloc-cmake/include_darwin_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/contrib/jemalloc-cmake/include_darwin_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in @@ -6,8 +6,8 @@ * public APIs to be prefixed. This makes it possible, with some care, to use * multiple allocators simultaneously. */ -#define JEMALLOC_PREFIX "je_" -#define JEMALLOC_CPREFIX "JE_" +#define JEMALLOC_PREFIX "@JEMALLOC_PREFIX@" +#define JEMALLOC_CPREFIX "@JEMALLOC_CPREFIX@" /* * Define overrides for non-standard allocator-related functions if they are diff --git a/contrib/jemalloc-cmake/include_freebsd_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in b/contrib/jemalloc-cmake/include_freebsd_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in index 0f61417d65f..3a47ff9997a 100644 --- a/contrib/jemalloc-cmake/include_freebsd_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/contrib/jemalloc-cmake/include_freebsd_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in @@ -6,8 +6,8 @@ * public APIs to be prefixed. This makes it possible, with some care, to use * multiple allocators simultaneously. */ -/* #undef JEMALLOC_PREFIX */ -/* #undef JEMALLOC_CPREFIX */ +#define JEMALLOC_PREFIX "@JEMALLOC_PREFIX@" +#define JEMALLOC_CPREFIX "@JEMALLOC_CPREFIX@" /* * Define overrides for non-standard allocator-related functions if they are diff --git a/contrib/jemalloc-cmake/include_freebsd_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in b/contrib/jemalloc-cmake/include_freebsd_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in index 32cad025f5f..420f2486a22 100644 --- a/contrib/jemalloc-cmake/include_freebsd_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/contrib/jemalloc-cmake/include_freebsd_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in @@ -6,8 +6,8 @@ * public APIs to be prefixed. This makes it possible, with some care, to use * multiple allocators simultaneously. */ -/* #undef JEMALLOC_PREFIX */ -/* #undef JEMALLOC_CPREFIX */ +#define JEMALLOC_PREFIX "@JEMALLOC_PREFIX@" +#define JEMALLOC_CPREFIX "@JEMALLOC_CPREFIX@" /* * Define overrides for non-standard allocator-related functions if they are diff --git a/contrib/jemalloc-cmake/include_linux_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in b/contrib/jemalloc-cmake/include_linux_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in index ad535e6d773..cd2714c10aa 100644 --- a/contrib/jemalloc-cmake/include_linux_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/contrib/jemalloc-cmake/include_linux_aarch64/jemalloc/internal/jemalloc_internal_defs.h.in @@ -6,8 +6,8 @@ * public APIs to be prefixed. This makes it possible, with some care, to use * multiple allocators simultaneously. */ -/* #undef JEMALLOC_PREFIX */ -/* #undef JEMALLOC_CPREFIX */ +#define JEMALLOC_PREFIX "@JEMALLOC_PREFIX@" +#define JEMALLOC_CPREFIX "@JEMALLOC_CPREFIX@" /* * Define overrides for non-standard allocator-related functions if they are diff --git a/contrib/jemalloc-cmake/include_linux_ppc64le/jemalloc/internal/jemalloc_internal_defs.h.in b/contrib/jemalloc-cmake/include_linux_ppc64le/jemalloc/internal/jemalloc_internal_defs.h.in index 12890f80ef1..5fb4d208f28 100644 --- a/contrib/jemalloc-cmake/include_linux_ppc64le/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/contrib/jemalloc-cmake/include_linux_ppc64le/jemalloc/internal/jemalloc_internal_defs.h.in @@ -6,8 +6,8 @@ * public APIs to be prefixed. This makes it possible, with some care, to use * multiple allocators simultaneously. */ -/* #undef JEMALLOC_PREFIX */ -/* #undef JEMALLOC_CPREFIX */ +#define JEMALLOC_PREFIX "@JEMALLOC_PREFIX@" +#define JEMALLOC_CPREFIX "@JEMALLOC_CPREFIX@" /* * Define overrides for non-standard allocator-related functions if they are diff --git a/contrib/jemalloc-cmake/include_linux_riscv64/jemalloc/internal/jemalloc_internal_defs.h.in b/contrib/jemalloc-cmake/include_linux_riscv64/jemalloc/internal/jemalloc_internal_defs.h.in index ad535e6d773..cd2714c10aa 100644 --- a/contrib/jemalloc-cmake/include_linux_riscv64/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/contrib/jemalloc-cmake/include_linux_riscv64/jemalloc/internal/jemalloc_internal_defs.h.in @@ -6,8 +6,8 @@ * public APIs to be prefixed. This makes it possible, with some care, to use * multiple allocators simultaneously. */ -/* #undef JEMALLOC_PREFIX */ -/* #undef JEMALLOC_CPREFIX */ +#define JEMALLOC_PREFIX "@JEMALLOC_PREFIX@" +#define JEMALLOC_CPREFIX "@JEMALLOC_CPREFIX@" /* * Define overrides for non-standard allocator-related functions if they are diff --git a/contrib/jemalloc-cmake/include_linux_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in b/contrib/jemalloc-cmake/include_linux_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in index 99ab2d53ca9..bce428c44a7 100644 --- a/contrib/jemalloc-cmake/include_linux_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/contrib/jemalloc-cmake/include_linux_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in @@ -6,8 +6,8 @@ * public APIs to be prefixed. This makes it possible, with some care, to use * multiple allocators simultaneously. */ -/* #undef JEMALLOC_PREFIX */ -/* #undef JEMALLOC_CPREFIX */ +#define JEMALLOC_PREFIX "@JEMALLOC_PREFIX@" +#define JEMALLOC_CPREFIX "@JEMALLOC_CPREFIX@" /* * Define overrides for non-standard allocator-related functions if they are diff --git a/contrib/jemalloc-cmake/include_linux_x86_64_musl/jemalloc/internal/jemalloc_internal_defs.h.in b/contrib/jemalloc-cmake/include_linux_x86_64_musl/jemalloc/internal/jemalloc_internal_defs.h.in index 684d4debb14..6b2bfbfbeda 100644 --- a/contrib/jemalloc-cmake/include_linux_x86_64_musl/jemalloc/internal/jemalloc_internal_defs.h.in +++ b/contrib/jemalloc-cmake/include_linux_x86_64_musl/jemalloc/internal/jemalloc_internal_defs.h.in @@ -6,8 +6,8 @@ * public APIs to be prefixed. This makes it possible, with some care, to use * multiple allocators simultaneously. */ -/* #undef JEMALLOC_PREFIX */ -/* #undef JEMALLOC_CPREFIX */ +#define JEMALLOC_PREFIX "@JEMALLOC_PREFIX@" +#define JEMALLOC_CPREFIX "@JEMALLOC_CPREFIX@" /* * Define overrides for non-standard allocator-related functions if they are diff --git a/dbms/src/Interpreters/AsynchronousMetrics.cpp b/dbms/src/Interpreters/AsynchronousMetrics.cpp index 48d2171446b..4a5142b2559 100644 --- a/dbms/src/Interpreters/AsynchronousMetrics.cpp +++ b/dbms/src/Interpreters/AsynchronousMetrics.cpp @@ -370,13 +370,13 @@ void AsynchronousMetrics::update() M("background_thread.num_runs", uint64_t) \ M("background_thread.run_interval", uint64_t) -#define GET_JEMALLOC_METRIC(NAME, TYPE) \ - do \ - { \ - TYPE value{}; \ - size_t size = sizeof(value); \ - mallctl("stats." NAME, &value, &size, nullptr, 0); \ - set("jemalloc." NAME, value); \ +#define GET_JEMALLOC_METRIC(NAME, TYPE) \ + do \ + { \ + TYPE value{}; \ + size_t size = sizeof(value); \ + je_mallctl("stats." NAME, &value, &size, nullptr, 0); \ + set("jemalloc." NAME, value); \ } while (0); FOR_EACH_METRIC(GET_JEMALLOC_METRIC);