Skip to content

Commit

Permalink
[FLASH-572] Use jemalloc instead of tcmalloc by default (#424)
Browse files Browse the repository at this point in the history
* Use jemalloc instead of tcmalloc by default

Signed-off-by: JaySon-Huang <[email protected]>

* Fix build with libjemalloc

* Use jemalloc in Linux and tcmalloc in MaxOS

Signed-off-by: JaySon-Huang <[email protected]>

* Fix bug: TMTContext should be free before background_pool

Signed-off-by: JaySon-Huang <[email protected]>

* Use of invalid iterator

* Fix use after free in client-c

Signed-off-by: JaySon-Huang <[email protected]>
  • Loading branch information
JaySon-Huang authored Feb 12, 2020
1 parent f98ab08 commit af17e0d
Show file tree
Hide file tree
Showing 23 changed files with 1,097 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@
[submodule "contrib/junction"]
path = contrib/junction
url = https://github.com/preshing/junction.git
[submodule "contrib/jemalloc"]
path = contrib/jemalloc
url = https://github.com/jemalloc/jemalloc.git
5 changes: 5 additions & 0 deletions contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ if (USE_INTERNAL_CCTZ_LIBRARY)
add_subdirectory (cctz-cmake)
endif ()

# TODO: remove tcmalloc
if (ENABLE_TCMALLOC AND USE_INTERNAL_GPERFTOOLS_LIBRARY)
add_subdirectory (libtcmalloc)
endif ()

if (ENABLE_JEMALLOC AND USE_INTERNAL_JEMALLOC_LIBRARY)
add_subdirectory (jemalloc-cmake)
endif ()

if (NOT ARCH_ARM)
add_subdirectory (libcpuid)
endif ()
Expand Down
2 changes: 1 addition & 1 deletion contrib/client-c
1 change: 1 addition & 0 deletions contrib/jemalloc
Submodule jemalloc added at cd2931
66 changes: 66 additions & 0 deletions contrib/jemalloc-cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
set(JEMALLOC_SOURCE_DIR ${ClickHouse_SOURCE_DIR}/contrib/jemalloc)

set(SRCS
${JEMALLOC_SOURCE_DIR}/src/arena.c
${JEMALLOC_SOURCE_DIR}/src/background_thread.c
${JEMALLOC_SOURCE_DIR}/src/base.c
${JEMALLOC_SOURCE_DIR}/src/bin.c
${JEMALLOC_SOURCE_DIR}/src/bitmap.c
${JEMALLOC_SOURCE_DIR}/src/ckh.c
${JEMALLOC_SOURCE_DIR}/src/ctl.c
${JEMALLOC_SOURCE_DIR}/src/div.c
${JEMALLOC_SOURCE_DIR}/src/extent.c
${JEMALLOC_SOURCE_DIR}/src/extent_dss.c
${JEMALLOC_SOURCE_DIR}/src/extent_mmap.c
${JEMALLOC_SOURCE_DIR}/src/hash.c
${JEMALLOC_SOURCE_DIR}/src/hook.c
${JEMALLOC_SOURCE_DIR}/src/jemalloc.c
${JEMALLOC_SOURCE_DIR}/src/jemalloc_cpp.cpp
${JEMALLOC_SOURCE_DIR}/src/large.c
${JEMALLOC_SOURCE_DIR}/src/log.c
${JEMALLOC_SOURCE_DIR}/src/malloc_io.c
${JEMALLOC_SOURCE_DIR}/src/mutex.c
${JEMALLOC_SOURCE_DIR}/src/mutex_pool.c
${JEMALLOC_SOURCE_DIR}/src/nstime.c
${JEMALLOC_SOURCE_DIR}/src/pages.c
${JEMALLOC_SOURCE_DIR}/src/prng.c
${JEMALLOC_SOURCE_DIR}/src/prof.c
${JEMALLOC_SOURCE_DIR}/src/rtree.c
${JEMALLOC_SOURCE_DIR}/src/sc.c
${JEMALLOC_SOURCE_DIR}/src/stats.c
${JEMALLOC_SOURCE_DIR}/src/sz.c
${JEMALLOC_SOURCE_DIR}/src/tcache.c
${JEMALLOC_SOURCE_DIR}/src/test_hooks.c
${JEMALLOC_SOURCE_DIR}/src/ticker.c
${JEMALLOC_SOURCE_DIR}/src/tsd.c
${JEMALLOC_SOURCE_DIR}/src/witness.c
)

if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
list(APPEND SRCS ${JEMALLOC_SOURCE_DIR}/src/zone.c)
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w")
endif ()

add_library(jemalloc STATIC ${SRCS})

target_include_directories(jemalloc PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include_linux_x86_64) # jemalloc.h

target_include_directories(jemalloc PRIVATE
${JEMALLOC_SOURCE_DIR}/include)

target_compile_definitions(jemalloc PRIVATE -DJEMALLOC_NO_PRIVATE_NAMESPACE)

if (CMAKE_BUILD_TYPE_UC STREQUAL "DEBUG")
target_compile_definitions(jemalloc PRIVATE -DJEMALLOC_DEBUG=1 -DJEMALLOC_PROF=1)

if (USE_UNWIND)
target_compile_definitions (jemalloc PRIVATE -DJEMALLOC_PROF_LIBUNWIND=1)
target_include_directories (jemalloc BEFORE PRIVATE ${UNWIND_INCLUDE_DIR})
target_link_libraries (jemalloc PRIVATE ${UNWIND_LIBRARY})
endif ()
endif ()
1 change: 1 addition & 0 deletions contrib/jemalloc-cmake/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It allows to integrate JEMalloc into CMake project.
16 changes: 16 additions & 0 deletions contrib/jemalloc-cmake/include/jemalloc/jemalloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include <jemalloc/jemalloc_defs.h>
#include <jemalloc/jemalloc_rename.h>
#include <jemalloc/jemalloc_macros.h>
#include <jemalloc/jemalloc_protos.h>
#include <jemalloc/jemalloc_typedefs.h>

#ifdef __cplusplus
}
#endif

29 changes: 29 additions & 0 deletions contrib/jemalloc-cmake/include/jemalloc/jemalloc_rename.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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_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_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
7 changes: 7 additions & 0 deletions contrib/jemalloc-cmake/include_linux_x86_64/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Here are pre-generated files from jemalloc on Linux x86_64.
You can obtain these files by running ./autogen.sh inside jemalloc source directory.

Added #define GNU_SOURCE
Added JEMALLOC_OVERRIDE___POSIX_MEMALIGN because why not.
Removed JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF because it's non standard.
Removed JEMALLOC_PURGE_MADVISE_FREE because it's available only from Linux 4.5.
Loading

0 comments on commit af17e0d

Please sign in to comment.