2021-04-08 09:16:02 +00:00
|
|
|
if (SANITIZE OR NOT (
|
2021-11-11 18:18:41 +00:00
|
|
|
((OS_LINUX OR OS_FREEBSD) AND (ARCH_AMD64 OR ARCH_ARM OR ARCH_PPC64LE OR ARCH_RISCV64)) OR
|
2021-12-01 19:01:05 +00:00
|
|
|
(OS_DARWIN AND (CMAKE_BUILD_TYPE_UC STREQUAL "RELWITHDEBINFO" OR CMAKE_BUILD_TYPE_UC STREQUAL "DEBUG"))
|
2021-08-18 06:47:59 +00:00
|
|
|
))
|
2020-08-14 15:33:17 +00:00
|
|
|
if (ENABLE_JEMALLOC)
|
|
|
|
message (${RECONFIGURE_MESSAGE_LEVEL}
|
2021-08-18 06:47:59 +00:00
|
|
|
"jemalloc is disabled implicitly: it doesn't work with sanitizers and can only be used with x86_64, aarch64, or ppc64le Linux or FreeBSD builds and RelWithDebInfo macOS builds.")
|
2021-04-08 09:16:02 +00:00
|
|
|
endif ()
|
2020-03-17 11:22:15 +00:00
|
|
|
set (ENABLE_JEMALLOC OFF)
|
2021-04-08 09:16:02 +00:00
|
|
|
else ()
|
2020-08-14 15:33:17 +00:00
|
|
|
option (ENABLE_JEMALLOC "Enable jemalloc allocator" ${ENABLE_LIBRARIES})
|
2019-05-31 08:55:01 +00:00
|
|
|
endif ()
|
|
|
|
|
2020-08-14 15:44:04 +00:00
|
|
|
if (NOT ENABLE_JEMALLOC)
|
|
|
|
message (STATUS "Not using jemalloc")
|
|
|
|
return()
|
|
|
|
endif ()
|
2018-08-02 00:20:20 +00:00
|
|
|
|
2020-08-14 15:44:04 +00:00
|
|
|
if (NOT OS_LINUX)
|
|
|
|
message (WARNING "jemalloc support on non-linux is EXPERIMENTAL")
|
|
|
|
endif()
|
2018-08-02 00:20:20 +00:00
|
|
|
|
2020-10-11 08:18:16 +00:00
|
|
|
if (OS_LINUX)
|
|
|
|
# ThreadPool select job randomly, and there can be some threads that had been
|
|
|
|
# performed some memory heavy task before and will be inactive for some time,
|
|
|
|
# but until it will became active again, the memory will not be freed since by
|
|
|
|
# default each thread has it's own arena, but there should be not more then
|
|
|
|
# 4*CPU arenas (see opt.nareans description).
|
|
|
|
#
|
|
|
|
# By enabling percpu_arena number of arenas limited to number of CPUs and hence
|
|
|
|
# this problem should go away.
|
|
|
|
#
|
|
|
|
# muzzy_decay_ms -- use MADV_FREE when available on newer Linuxes, to
|
|
|
|
# avoid spurious latencies and additional work associated with
|
|
|
|
# MADV_DONTNEED. See
|
|
|
|
# https://github.com/ClickHouse/ClickHouse/issues/11121 for motivation.
|
2021-04-16 07:54:11 +00:00
|
|
|
set (JEMALLOC_CONFIG_MALLOC_CONF "percpu_arena:percpu,oversize_threshold:0,muzzy_decay_ms:5000,dirty_decay_ms:5000")
|
2020-10-11 08:18:16 +00:00
|
|
|
else()
|
2021-04-16 07:54:11 +00:00
|
|
|
set (JEMALLOC_CONFIG_MALLOC_CONF "oversize_threshold:0,muzzy_decay_ms:5000,dirty_decay_ms:5000")
|
2020-10-11 08:18:16 +00:00
|
|
|
endif()
|
|
|
|
# CACHE variable is empty, to allow changing defaults without necessity
|
|
|
|
# to purge cache
|
|
|
|
set (JEMALLOC_CONFIG_MALLOC_CONF_OVERRIDE "" CACHE STRING "Change default configuration string of JEMalloc" )
|
|
|
|
if (JEMALLOC_CONFIG_MALLOC_CONF_OVERRIDE)
|
|
|
|
set (JEMALLOC_CONFIG_MALLOC_CONF "${JEMALLOC_CONFIG_MALLOC_CONF_OVERRIDE}")
|
|
|
|
endif()
|
|
|
|
message (STATUS "jemalloc malloc_conf: ${JEMALLOC_CONFIG_MALLOC_CONF}")
|
|
|
|
|
|
|
|
set (LIBRARY_DIR "${ClickHouse_SOURCE_DIR}/contrib/jemalloc")
|
|
|
|
|
|
|
|
set (SRCS
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/arena.c"
|
|
|
|
"${LIBRARY_DIR}/src/background_thread.c"
|
|
|
|
"${LIBRARY_DIR}/src/base.c"
|
|
|
|
"${LIBRARY_DIR}/src/bin.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/bin_info.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/bitmap.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/buf_writer.c"
|
|
|
|
"${LIBRARY_DIR}/src/cache_bin.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/ckh.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/counter.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/ctl.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/decay.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/div.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/ecache.c"
|
|
|
|
"${LIBRARY_DIR}/src/edata.c"
|
|
|
|
"${LIBRARY_DIR}/src/edata_cache.c"
|
|
|
|
"${LIBRARY_DIR}/src/ehooks.c"
|
|
|
|
"${LIBRARY_DIR}/src/emap.c"
|
|
|
|
"${LIBRARY_DIR}/src/eset.c"
|
|
|
|
"${LIBRARY_DIR}/src/exp_grow.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/extent.c"
|
|
|
|
"${LIBRARY_DIR}/src/extent_dss.c"
|
|
|
|
"${LIBRARY_DIR}/src/extent_mmap.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/fxp.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/hook.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/hpa.c"
|
|
|
|
"${LIBRARY_DIR}/src/hpa_hooks.c"
|
|
|
|
"${LIBRARY_DIR}/src/hpdata.c"
|
|
|
|
"${LIBRARY_DIR}/src/inspect.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/jemalloc.c"
|
|
|
|
"${LIBRARY_DIR}/src/large.c"
|
|
|
|
"${LIBRARY_DIR}/src/log.c"
|
|
|
|
"${LIBRARY_DIR}/src/malloc_io.c"
|
|
|
|
"${LIBRARY_DIR}/src/mutex.c"
|
|
|
|
"${LIBRARY_DIR}/src/nstime.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/pa.c"
|
|
|
|
"${LIBRARY_DIR}/src/pac.c"
|
|
|
|
"${LIBRARY_DIR}/src/pa_extra.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/pages.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/pai.c"
|
|
|
|
"${LIBRARY_DIR}/src/peak_event.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/prof.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/prof_data.c"
|
|
|
|
"${LIBRARY_DIR}/src/prof_log.c"
|
|
|
|
"${LIBRARY_DIR}/src/prof_recent.c"
|
|
|
|
"${LIBRARY_DIR}/src/prof_stats.c"
|
|
|
|
"${LIBRARY_DIR}/src/prof_sys.c"
|
|
|
|
"${LIBRARY_DIR}/src/psset.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/rtree.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/safety_check.c"
|
|
|
|
"${LIBRARY_DIR}/src/san_bump.c"
|
|
|
|
"${LIBRARY_DIR}/src/san.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/sc.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/sec.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/stats.c"
|
|
|
|
"${LIBRARY_DIR}/src/sz.c"
|
|
|
|
"${LIBRARY_DIR}/src/tcache.c"
|
|
|
|
"${LIBRARY_DIR}/src/test_hooks.c"
|
2022-02-09 08:57:44 +00:00
|
|
|
"${LIBRARY_DIR}/src/thread_event.c"
|
2021-04-24 19:47:52 +00:00
|
|
|
"${LIBRARY_DIR}/src/ticker.c"
|
|
|
|
"${LIBRARY_DIR}/src/tsd.c"
|
|
|
|
"${LIBRARY_DIR}/src/witness.c"
|
2020-10-11 08:18:16 +00:00
|
|
|
)
|
|
|
|
if (OS_DARWIN)
|
2021-04-24 19:47:52 +00:00
|
|
|
list(APPEND SRCS "${LIBRARY_DIR}/src/zone.c")
|
2020-08-14 15:44:04 +00:00
|
|
|
endif ()
|
2020-08-09 18:31:00 +00:00
|
|
|
|
2022-01-20 14:18:24 +00:00
|
|
|
add_library(_jemalloc ${SRCS})
|
2022-02-21 05:29:49 +00:00
|
|
|
# First include jemalloc-cmake files, to override anything that jemalloc has.
|
|
|
|
# (for example if you were trying to build jemalloc inside contrib/jemalloc you
|
|
|
|
# will have some files that may be out of date)
|
|
|
|
target_include_directories(_jemalloc PUBLIC include)
|
2022-01-20 14:18:24 +00:00
|
|
|
target_include_directories(_jemalloc PRIVATE "${LIBRARY_DIR}/include")
|
2020-10-11 08:18:16 +00:00
|
|
|
|
|
|
|
set (JEMALLOC_INCLUDE_PREFIX)
|
|
|
|
# OS_
|
|
|
|
if (OS_LINUX)
|
|
|
|
set (JEMALLOC_INCLUDE_PREFIX "include_linux")
|
|
|
|
elseif (OS_FREEBSD)
|
|
|
|
set (JEMALLOC_INCLUDE_PREFIX "include_freebsd")
|
|
|
|
elseif (OS_DARWIN)
|
|
|
|
set (JEMALLOC_INCLUDE_PREFIX "include_darwin")
|
|
|
|
else ()
|
|
|
|
message (FATAL_ERROR "internal jemalloc: This OS is not supported")
|
|
|
|
endif ()
|
|
|
|
# ARCH_
|
|
|
|
if (ARCH_AMD64)
|
2022-04-12 16:48:16 +00:00
|
|
|
if (USE_MUSL)
|
|
|
|
set(JEMALLOC_INCLUDE_PREFIX "${JEMALLOC_INCLUDE_PREFIX}_x86_64_musl")
|
|
|
|
else()
|
|
|
|
set(JEMALLOC_INCLUDE_PREFIX "${JEMALLOC_INCLUDE_PREFIX}_x86_64")
|
|
|
|
endif()
|
2020-10-11 08:18:16 +00:00
|
|
|
elseif (ARCH_ARM)
|
|
|
|
set(JEMALLOC_INCLUDE_PREFIX "${JEMALLOC_INCLUDE_PREFIX}_aarch64")
|
2021-04-01 15:25:42 +00:00
|
|
|
elseif (ARCH_PPC64LE)
|
|
|
|
set(JEMALLOC_INCLUDE_PREFIX "${JEMALLOC_INCLUDE_PREFIX}_ppc64le")
|
2021-11-11 18:18:41 +00:00
|
|
|
elseif (ARCH_RISCV64)
|
|
|
|
set(JEMALLOC_INCLUDE_PREFIX "${JEMALLOC_INCLUDE_PREFIX}_riscv64")
|
2020-10-11 08:18:16 +00:00
|
|
|
else ()
|
|
|
|
message (FATAL_ERROR "internal jemalloc: This arch is not supported")
|
|
|
|
endif ()
|
2020-08-14 15:44:04 +00:00
|
|
|
|
2020-10-11 08:18:16 +00:00
|
|
|
configure_file(${JEMALLOC_INCLUDE_PREFIX}/jemalloc/internal/jemalloc_internal_defs.h.in
|
|
|
|
${JEMALLOC_INCLUDE_PREFIX}/jemalloc/internal/jemalloc_internal_defs.h)
|
2022-01-20 14:18:24 +00:00
|
|
|
target_include_directories(_jemalloc SYSTEM PRIVATE
|
2021-04-24 19:47:52 +00:00
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/${JEMALLOC_INCLUDE_PREFIX}/jemalloc/internal")
|
2020-08-14 15:44:04 +00:00
|
|
|
|
2022-01-20 14:18:24 +00:00
|
|
|
target_compile_definitions(_jemalloc PRIVATE -DJEMALLOC_NO_PRIVATE_NAMESPACE)
|
2020-08-14 15:44:04 +00:00
|
|
|
|
2020-10-11 08:18:16 +00:00
|
|
|
if (CMAKE_BUILD_TYPE_UC STREQUAL "DEBUG")
|
2022-04-18 18:37:49 +00:00
|
|
|
target_compile_definitions(_jemalloc PRIVATE
|
|
|
|
-DJEMALLOC_DEBUG=1
|
|
|
|
# Usage examples:
|
|
|
|
# - MALLOC_CONF=log:.
|
|
|
|
# - MALLOC_CONF='log:core.malloc.exit|core.sallocx.entry|core.sdallocx.entry'
|
|
|
|
-DJEMALLOC_LOG=1)
|
2021-04-08 08:52:28 +00:00
|
|
|
endif ()
|
2020-08-14 15:44:04 +00:00
|
|
|
|
2022-01-20 14:18:24 +00:00
|
|
|
target_compile_definitions(_jemalloc PRIVATE -DJEMALLOC_PROF=1)
|
2020-08-14 15:44:04 +00:00
|
|
|
|
2021-04-08 08:52:28 +00:00
|
|
|
if (USE_UNWIND)
|
2022-01-20 14:18:24 +00:00
|
|
|
target_compile_definitions (_jemalloc PRIVATE -DJEMALLOC_PROF_LIBUNWIND=1)
|
|
|
|
target_link_libraries (_jemalloc PRIVATE unwind)
|
2018-08-29 15:54:00 +00:00
|
|
|
endif ()
|
2020-08-14 15:44:04 +00:00
|
|
|
|
2022-01-20 14:18:24 +00:00
|
|
|
target_compile_options(_jemalloc PRIVATE -Wno-redundant-decls)
|
2020-10-11 08:18:16 +00:00
|
|
|
# for RTLD_NEXT
|
2022-01-20 14:18:24 +00:00
|
|
|
target_compile_options(_jemalloc PRIVATE -D_GNU_SOURCE)
|
2020-10-11 08:18:16 +00:00
|
|
|
|
2022-01-20 14:18:24 +00:00
|
|
|
add_library(ch_contrib::jemalloc ALIAS _jemalloc)
|