2021-04-08 09:16:02 +00:00
|
|
|
if (SANITIZE OR NOT (
|
2023-06-22 13:54:34 +00:00
|
|
|
((OS_LINUX OR OS_FREEBSD) AND (ARCH_AMD64 OR ARCH_AARCH64 OR ARCH_PPC64LE OR ARCH_RISCV64 OR ARCH_S390X)) 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}
|
2023-06-16 07:16:50 +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. Use -DENABLE_JEMALLOC=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.
|
2024-07-12 13:01:40 +00:00
|
|
|
set (JEMALLOC_CONFIG_MALLOC_CONF "percpu_arena:percpu,oversize_threshold:0,muzzy_decay_ms:0,dirty_decay_ms:5000,prof:true,prof_active:false,background_thread:true")
|
2020-10-11 08:18:16 +00:00
|
|
|
else()
|
2024-06-28 14:04:45 +00:00
|
|
|
set (JEMALLOC_CONFIG_MALLOC_CONF "oversize_threshold:0,muzzy_decay_ms:0,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
|
|
|
"${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-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
|
|
|
|
${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
|
|
|
|
2024-01-14 07:05:57 +00:00
|
|
|
# Because our coverage callbacks call malloc, and recursive call of malloc could not work.
|
|
|
|
target_compile_options(_jemalloc PRIVATE ${WITHOUT_COVERAGE_FLAGS_LIST})
|
|
|
|
|
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
|
|
|
|
Fix possible deadlock for jemalloc with enabled profiler
ClickHouse uses JEMALLOC_PROF_LIBGCC - _Unwind_Backtrace() for jemalloc,
which calls _Unwind_Backtrace() during bootstrap of jemalloc, and if,
dlsym() has allocations it will lead to deadlock:
0 __lll_lock_wait (futex=futex@entry=0x16516410 <init_lock+64>, private=0) at lowlevellock.c:52
1 0x00007ffff7f9d0a3 in __GI___pthread_mutex_lock (mutex=0x16516410 <init_lock+64>) at ../nptl/pthread_mutex_lock.c:80
2 0x000000000ba6ec23 in pthread_mutex_lock (arg=arg@entry=0x16516410 <init_lock+64>) at ./build/RelWithDebInfo/./src/Common/ThreadFuzzer.cpp:445
3 0x0000000014215fd4 in malloc_mutex_lock_final (mutex=0x165163d0 <init_lock>) at ./contrib/jemalloc/include/jemalloc/internal/mutex.h:151
4 malloc_mutex_lock_slow (mutex=0x165163d0 <init_lock>) at ./build/RelWithDebInfo/./contrib/jemalloc/src/mutex.c:90
5 0x00000000141defc6 in malloc_mutex_lock (tsdn=0x0, mutex=<optimized out>) at ./contrib/jemalloc/include/jemalloc/internal/mutex.h:217
6 malloc_init_hard () at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2118
7 0x00000000141d05db in malloc_init () at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:298
8 imalloc_init_check (sopts=<optimized out>, dopts=<optimized out>) at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2658
9 imalloc (sopts=<optimized out>, dopts=<optimized out>) at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2689
10 calloc (num=num@entry=1, size=size@entry=32) at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2852
11 0x00007ffff7c42c05 in _dlerror_run (operate=operate@entry=0x7ffff7c42490 <dlsym_doit>, args=args@entry=0x7fffffffda40) at dlerror.c:148
12 0x00007ffff7c42525 in __dlsym (handle=<optimized out>, name=0xe7f568 "dl_iterate_phdr") at dlsym.c:70
13 0x000000001408e1a0 in (anonymous namespace)::getOriginalDLIteratePHDR () at ./build/RelWithDebInfo/./base/base/phdr_cache.cpp:44
14 dl_iterate_phdr (callback=0x16287a60 <libunwind::findUnwindSectionsByPhdr(dl_phdr_info*, unsigned long, void*)>, data=0x7fffffffdb58) at ./build/RelWithDebInfo/./base/base/phdr_cache.cpp:64
15 0x0000000016288468 in libunwind::LocalAddressSpace::findUnwindSections (this=<optimized out>, targetAddr=371765377, info=...) at ./contrib/libunwind/src/AddressSpace.hpp:605
16 libunwind::UnwindCursor<libunwind::LocalAddressSpace, libunwind::Registers_x86_64>::setInfoBasedOnIPRegister (this=this@entry=0x7fffffffde70, isReturnAddress=false) at ./contrib/libunwind/src/UnwindCursor.hpp:2553
17 0x0000000016287617 in __unw_init_local (cursor=cursor@entry=0x7fffffffde70, context=context@entry=0x7fffffffddc8) at ./build/RelWithDebInfo/./contrib/libunwind/src/libunwind.cpp:91
18 0x000000001628b094 in _Unwind_Backtrace (callback=0x14226f40 <prof_unwind_init_callback>, ref=0x0) at ./build/RelWithDebInfo/./contrib/libunwind/src/UnwindLevel1-gcc-ext.c:106
19 0x000000001421a7e0 in prof_boot2 (tsd=tsd@entry=0x7ffff7c357c0, base=<optimized out>) at ./build/RelWithDebInfo/./contrib/jemalloc/src/prof.c:693
20 0x00000000141df3e7 in malloc_init_hard () at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2151
21 0x00000000141d96f1 in malloc_init () at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:298
22 nallocx (size=32, flags=0) at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:4011
23 0x000000000b9fdd9b in _ZN6Memory23getActualAllocationSizeITpTkNSt3__17same_asISt11align_val_tEEJEQsr2DBE16OptionalArgumentIDpT_EEEmmS5_ (size=32) at ./src/Common/memory.h:197
24 _ZN6Memory11trackMemoryITpTkNSt3__17same_asISt11align_val_tEEJEQsr2DBE16OptionalArgumentIDpT_EEEmmR15AllocationTraceS5_ (size=32, trace=...) at ./src/Common/memory.h:208
25 operator new (size=size@entry=32) at ./build/RelWithDebInfo/./src/Common/new_delete.cpp:53
26 0x000000001540e0a6 in google::protobuf::internal::ShutdownData::get () at ./build/RelWithDebInfo/./contrib/google-protobuf/src/google/protobuf/message_lite.cc:679
27 google::protobuf::internal::OnShutdownRun (f=0x15407420 <google::protobuf::internal::DestroyString(void const*)>, arg=0x167a6368 <google::protobuf::internal::fixed_address_empty_string>) at ./build/RelWithDebInfo/./contrib/google-protobuf/src/google/protobuf/message_lite.cc:697
28 0x0000000015407560 in global constructors keyed to 000101 () at ./contrib/google-protobuf/src/google/protobuf/generated_message_util.h:180
29 0x000000001629101d in __libc_csu_init ()
30 0x00007ffff7dba010 in __libc_start_main (main=0x66e3b00 <main(int, char**)>, argc=4, argv=0x7fffffffe258, init=0x16290fd0 <__libc_csu_init>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe248) at ../csu/libc-start.c:264
31 0x00000000066e302e in _start ()
And this is indeed what happens for glibc prio 2.34, since only in [2]
the allocation had been removed
[2]: https://sourceware.org/git/?p=glibc.git;a=commit;h=fada9018199c21c469ff0e731ef75c6020074ac9
But, jemalloc provides also JEMALLOC_PROF_LIBUNWIND, yes it is not llvm
libunwind, but gnu one, but ClickHouse version of llvm libunwind
provides unw_backtrace(), so we can use it, and it will not be called
during jemalloc bootstrap, so deadlock should not be possible.
Funny thing that for this deadlock the workaround is to enable
background_thread, because a thread created for it will initialize TLS for
__dlerror().
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
2024-07-10 13:58:42 +00:00
|
|
|
# jemalloc provides support two unwind flavors:
|
|
|
|
# - JEMALLOC_PROF_LIBUNWIND - unw_backtrace() - gnu libunwind (compatible with llvm libunwind)
|
|
|
|
# - JEMALLOC_PROF_LIBGCC - _Unwind_Backtrace() - the original HP libunwind and the one coming with gcc / g++ / libstdc++.
|
2023-05-03 08:09:11 +00:00
|
|
|
#
|
Fix possible deadlock for jemalloc with enabled profiler
ClickHouse uses JEMALLOC_PROF_LIBGCC - _Unwind_Backtrace() for jemalloc,
which calls _Unwind_Backtrace() during bootstrap of jemalloc, and if,
dlsym() has allocations it will lead to deadlock:
0 __lll_lock_wait (futex=futex@entry=0x16516410 <init_lock+64>, private=0) at lowlevellock.c:52
1 0x00007ffff7f9d0a3 in __GI___pthread_mutex_lock (mutex=0x16516410 <init_lock+64>) at ../nptl/pthread_mutex_lock.c:80
2 0x000000000ba6ec23 in pthread_mutex_lock (arg=arg@entry=0x16516410 <init_lock+64>) at ./build/RelWithDebInfo/./src/Common/ThreadFuzzer.cpp:445
3 0x0000000014215fd4 in malloc_mutex_lock_final (mutex=0x165163d0 <init_lock>) at ./contrib/jemalloc/include/jemalloc/internal/mutex.h:151
4 malloc_mutex_lock_slow (mutex=0x165163d0 <init_lock>) at ./build/RelWithDebInfo/./contrib/jemalloc/src/mutex.c:90
5 0x00000000141defc6 in malloc_mutex_lock (tsdn=0x0, mutex=<optimized out>) at ./contrib/jemalloc/include/jemalloc/internal/mutex.h:217
6 malloc_init_hard () at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2118
7 0x00000000141d05db in malloc_init () at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:298
8 imalloc_init_check (sopts=<optimized out>, dopts=<optimized out>) at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2658
9 imalloc (sopts=<optimized out>, dopts=<optimized out>) at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2689
10 calloc (num=num@entry=1, size=size@entry=32) at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2852
11 0x00007ffff7c42c05 in _dlerror_run (operate=operate@entry=0x7ffff7c42490 <dlsym_doit>, args=args@entry=0x7fffffffda40) at dlerror.c:148
12 0x00007ffff7c42525 in __dlsym (handle=<optimized out>, name=0xe7f568 "dl_iterate_phdr") at dlsym.c:70
13 0x000000001408e1a0 in (anonymous namespace)::getOriginalDLIteratePHDR () at ./build/RelWithDebInfo/./base/base/phdr_cache.cpp:44
14 dl_iterate_phdr (callback=0x16287a60 <libunwind::findUnwindSectionsByPhdr(dl_phdr_info*, unsigned long, void*)>, data=0x7fffffffdb58) at ./build/RelWithDebInfo/./base/base/phdr_cache.cpp:64
15 0x0000000016288468 in libunwind::LocalAddressSpace::findUnwindSections (this=<optimized out>, targetAddr=371765377, info=...) at ./contrib/libunwind/src/AddressSpace.hpp:605
16 libunwind::UnwindCursor<libunwind::LocalAddressSpace, libunwind::Registers_x86_64>::setInfoBasedOnIPRegister (this=this@entry=0x7fffffffde70, isReturnAddress=false) at ./contrib/libunwind/src/UnwindCursor.hpp:2553
17 0x0000000016287617 in __unw_init_local (cursor=cursor@entry=0x7fffffffde70, context=context@entry=0x7fffffffddc8) at ./build/RelWithDebInfo/./contrib/libunwind/src/libunwind.cpp:91
18 0x000000001628b094 in _Unwind_Backtrace (callback=0x14226f40 <prof_unwind_init_callback>, ref=0x0) at ./build/RelWithDebInfo/./contrib/libunwind/src/UnwindLevel1-gcc-ext.c:106
19 0x000000001421a7e0 in prof_boot2 (tsd=tsd@entry=0x7ffff7c357c0, base=<optimized out>) at ./build/RelWithDebInfo/./contrib/jemalloc/src/prof.c:693
20 0x00000000141df3e7 in malloc_init_hard () at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:2151
21 0x00000000141d96f1 in malloc_init () at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:298
22 nallocx (size=32, flags=0) at ./build/RelWithDebInfo/./contrib/jemalloc/src/jemalloc.c:4011
23 0x000000000b9fdd9b in _ZN6Memory23getActualAllocationSizeITpTkNSt3__17same_asISt11align_val_tEEJEQsr2DBE16OptionalArgumentIDpT_EEEmmS5_ (size=32) at ./src/Common/memory.h:197
24 _ZN6Memory11trackMemoryITpTkNSt3__17same_asISt11align_val_tEEJEQsr2DBE16OptionalArgumentIDpT_EEEmmR15AllocationTraceS5_ (size=32, trace=...) at ./src/Common/memory.h:208
25 operator new (size=size@entry=32) at ./build/RelWithDebInfo/./src/Common/new_delete.cpp:53
26 0x000000001540e0a6 in google::protobuf::internal::ShutdownData::get () at ./build/RelWithDebInfo/./contrib/google-protobuf/src/google/protobuf/message_lite.cc:679
27 google::protobuf::internal::OnShutdownRun (f=0x15407420 <google::protobuf::internal::DestroyString(void const*)>, arg=0x167a6368 <google::protobuf::internal::fixed_address_empty_string>) at ./build/RelWithDebInfo/./contrib/google-protobuf/src/google/protobuf/message_lite.cc:697
28 0x0000000015407560 in global constructors keyed to 000101 () at ./contrib/google-protobuf/src/google/protobuf/generated_message_util.h:180
29 0x000000001629101d in __libc_csu_init ()
30 0x00007ffff7dba010 in __libc_start_main (main=0x66e3b00 <main(int, char**)>, argc=4, argv=0x7fffffffe258, init=0x16290fd0 <__libc_csu_init>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe248) at ../csu/libc-start.c:264
31 0x00000000066e302e in _start ()
And this is indeed what happens for glibc prio 2.34, since only in [2]
the allocation had been removed
[2]: https://sourceware.org/git/?p=glibc.git;a=commit;h=fada9018199c21c469ff0e731ef75c6020074ac9
But, jemalloc provides also JEMALLOC_PROF_LIBUNWIND, yes it is not llvm
libunwind, but gnu one, but ClickHouse version of llvm libunwind
provides unw_backtrace(), so we can use it, and it will not be called
during jemalloc bootstrap, so deadlock should not be possible.
Funny thing that for this deadlock the workaround is to enable
background_thread, because a thread created for it will initialize TLS for
__dlerror().
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
2024-07-10 13:58:42 +00:00
|
|
|
# But for JEMALLOC_PROF_LIBGCC it also calls _Unwind_Backtrace() during
|
|
|
|
# bootstraping of jemalloc, which may lead to deadlock, if the dlsym will do
|
|
|
|
# allocations somewhere (like glibc does prio 2.34, see [1]).
|
|
|
|
#
|
|
|
|
# [1]: https://sourceware.org/git/?p=glibc.git;a=commit;h=fada9018199c21c469ff0e731ef75c6020074ac9
|
|
|
|
#
|
|
|
|
# And since ClickHouse unwind already supports unw_backtrace() we can safely
|
|
|
|
# switch to it to avoid this deadlock.
|
|
|
|
target_compile_definitions (_jemalloc PRIVATE -DJEMALLOC_PROF_LIBUNWIND=1)
|
2023-05-03 08:09:11 +00:00
|
|
|
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)
|