2021-04-08 09:16:02 +00:00
if ( SANITIZE OR NOT (
2023-06-22 13:54:34 +00:00
( ( O S _ L I N U X O R O S _ F R E E B S D ) AND ( ARCH_AMD64 OR ARCH_AARCH64 OR ARCH_PPC64LE OR ARCH_RISCV64 OR ARCH_S390X ) ) O R
2021-12-01 19:01:05 +00:00
( O S _ D A R W I N 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 }
2023-06-16 07:16:50 +00:00
" j e m a l l o c i s d i s a b l e d i m p l i c i t l y : i t d o e s n ' t w o r k w i t h s a n i t i z e r s a n d c a n o n l y b e u s e d w i t h x 8 6 _ 6 4 , a a r c h 6 4 , o r p p c 6 4 l e L i n u x o r F r e e B S D b u i l d s a n d R e l W i t h D e b I n f o m a c O S b u i l d s . U s e - D E N A B L E _ J E M A L L O C = 0 " )
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 )
2023-06-22 13:55:57 +00:00
message ( WARNING "jemalloc support on non-Linux is EXPERIMENTAL" )
2020-08-14 15:44:04 +00:00
endif ( )
2018-08-02 00:20:20 +00:00
2020-10-11 08:18:16 +00:00
if ( OS_LINUX )
2023-06-22 13:55:57 +00:00
# ThreadPool select job randomly, and there can be some threads that have been
# performed some memory-heavy tasks before and will be inactive for some time,
# but until it becomes active again, the memory will not be freed since, by
# default, each thread has its arena, but there should be no more than
2020-10-11 08:18:16 +00:00
# 4*CPU arenas (see opt.nareans description).
#
2023-06-22 13:55:57 +00:00
# By enabling percpu_arena number of arenas is limited to the number of CPUs, and hence
2020-10-11 08:18:16 +00:00
# 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 ( )
2023-06-22 13:55:57 +00:00
# CACHE variable is empty to allow changing defaults without the necessity
2020-10-11 08:18:16 +00:00
# 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
" $ { L I B R A R Y _ D I R } / s r c / a r e n a . c "
" $ { L I B R A R Y _ D I R } / s r c / b a c k g r o u n d _ t h r e a d . c "
" $ { L I B R A R Y _ D I R } / s r c / b a s e . c "
" $ { L I B R A R Y _ D I R } / s r c / b i n . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / b i n _ i n f o . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / b i t m a p . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / b u f _ w r i t e r . c "
" $ { L I B R A R Y _ D I R } / s r c / c a c h e _ b i n . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / c k h . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / c o u n t e r . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / c t l . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / d e c a y . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / d i v . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / e c a c h e . c "
" $ { L I B R A R Y _ D I R } / s r c / e d a t a . c "
" $ { L I B R A R Y _ D I R } / s r c / e d a t a _ c a c h e . c "
" $ { L I B R A R Y _ D I R } / s r c / e h o o k s . c "
" $ { L I B R A R Y _ D I R } / s r c / e m a p . c "
" $ { L I B R A R Y _ D I R } / s r c / e s e t . c "
" $ { L I B R A R Y _ D I R } / s r c / e x p _ g r o w . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / e x t e n t . c "
" $ { L I B R A R Y _ D I R } / s r c / e x t e n t _ d s s . c "
" $ { L I B R A R Y _ D I R } / s r c / e x t e n t _ m m a p . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / f x p . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / h o o k . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / h p a . c "
" $ { L I B R A R Y _ D I R } / s r c / h p a _ h o o k s . c "
" $ { L I B R A R Y _ D I R } / s r c / h p d a t a . c "
" $ { L I B R A R Y _ D I R } / s r c / i n s p e c t . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / j e m a l l o c . c "
" $ { L I B R A R Y _ D I R } / s r c / l a r g e . c "
" $ { L I B R A R Y _ D I R } / s r c / l o g . c "
" $ { L I B R A R Y _ D I R } / s r c / m a l l o c _ i o . c "
" $ { L I B R A R Y _ D I R } / s r c / m u t e x . c "
" $ { L I B R A R Y _ D I R } / s r c / n s t i m e . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / p a . c "
" $ { L I B R A R Y _ D I R } / s r c / p a c . c "
" $ { L I B R A R Y _ D I R } / s r c / p a _ e x t r a . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / p a g e s . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / p a i . c "
" $ { L I B R A R Y _ D I R } / s r c / p e a k _ e v e n t . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / p r o f . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / p r o f _ d a t a . c "
" $ { L I B R A R Y _ D I R } / s r c / p r o f _ l o g . c "
" $ { L I B R A R Y _ D I R } / s r c / p r o f _ r e c e n t . c "
" $ { L I B R A R Y _ D I R } / s r c / p r o f _ s t a t s . c "
" $ { L I B R A R Y _ D I R } / s r c / p r o f _ s y s . c "
" $ { L I B R A R Y _ D I R } / s r c / p s s e t . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / r t r e e . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / s a f e t y _ c h e c k . c "
" $ { L I B R A R Y _ D I R } / s r c / s a n _ b u m p . c "
" $ { L I B R A R Y _ D I R } / s r c / s a n . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / s c . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / s e c . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / s t a t s . c "
" $ { L I B R A R Y _ D I R } / s r c / s z . c "
" $ { L I B R A R Y _ D I R } / s r c / t c a c h e . c "
" $ { L I B R A R Y _ D I R } / s r c / t e s t _ h o o k s . c "
2022-02-09 08:57:44 +00:00
" $ { L I B R A R Y _ D I R } / s r c / t h r e a d _ e v e n t . c "
2021-04-24 19:47:52 +00:00
" $ { L I B R A R Y _ D I R } / s r c / t i c k e r . c "
" $ { L I B R A R Y _ D I R } / s r c / t s d . c "
" $ { L I B R A R Y _ D I R } / s r c / w i t n e s s . 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-07-03 17:32:46 +00:00
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)
2022-07-03 17:32:46 +00:00
target_include_directories ( _jemalloc SYSTEM 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 ( )
2022-05-10 15:28:46 +00:00
elseif ( ARCH_AARCH64 )
2020-10-11 08:18:16 +00:00
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" )
2023-06-18 02:03:14 +00:00
elseif ( ARCH_S390X )
set ( JEMALLOC_INCLUDE_PREFIX "${JEMALLOC_INCLUDE_PREFIX}_s390x" )
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
$ { J E M A L L O C _ I N C L U D E _ P R E F I X } / j e m a l l o c / i n t e r n a l / j e m a l l o c _ i n t e r n a l _ d e f s . h )
2022-01-20 14:18:24 +00:00
target_include_directories ( _jemalloc SYSTEM PRIVATE
2021-04-24 19:47:52 +00:00
" $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / $ { J E M A L L O C _ I N C L U D E _ P R E F I X } / j e m a l l o c / i n t e r n a l " )
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
- D J E M A L L O C _ D E B U G = 1
# Usage examples:
# - MALLOC_CONF=log:.
# - MALLOC_CONF='log:core.malloc.exit|core.sallocx.entry|core.sdallocx.entry'
- D J E M A L L O C _ L O G = 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
2023-05-03 08:09:11 +00:00
# jemalloc provides support for two different libunwind flavors: the original HP libunwind and the one coming with gcc / g++ / libstdc++.
# The latter is identified by `JEMALLOC_PROF_LIBGCC` and uses `_Unwind_Backtrace` method instead of `unw_backtrace`.
2023-06-22 13:55:57 +00:00
# At the time ClickHouse uses LLVM libunwind which follows libgcc's way of backtracking.
2023-05-03 08:09:11 +00:00
#
# ClickHouse has to provide `unw_backtrace` method by the means of [commit 8e2b31e](https://github.com/ClickHouse/libunwind/commit/8e2b31e766dd502f6df74909e04a7dbdf5182eb1).
target_compile_definitions ( _jemalloc PRIVATE -DJEMALLOC_PROF_LIBGCC=1 )
target_link_libraries ( _jemalloc PRIVATE unwind )
2020-08-14 15:44:04 +00:00
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 )