Skip to content

Commit 222ba88

Browse files
authored
Merge pull request #22 from chdb-io/enable-jemalloc
Enable jemalloc in linux x86_64 build
2 parents 8e8e727 + df76dc6 commit 222ba88

File tree

20 files changed

+313
-52
lines changed

20 files changed

+313
-52
lines changed

.github/workflows/build_wheels.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ jobs:
7474
with:
7575
key: ${{ matrix.os }}
7676
max-size: 5G
77-
append-timestamp: false
77+
append-timestamp: true
7878
- name: remove old clang and link clang-15 to clang
7979
if: matrix.os == 'ubuntu-20.04'
8080
run: |
@@ -199,7 +199,7 @@ jobs:
199199
with:
200200
key: ${{ matrix.os }}
201201
max-size: 5G
202-
append-timestamp: false
202+
append-timestamp: true
203203
- name: Run chdb/build.sh
204204
timeout-minutes: 300
205205
run: |
@@ -314,7 +314,7 @@ jobs:
314314
with:
315315
key: ${{ matrix.os }}
316316
max-size: 5G
317-
append-timestamp: false
317+
append-timestamp: true
318318
- name: Prepare chdb/build.sh
319319
run: |
320320
python3 -m pip install pybind11

.gitmodules

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
[submodule "contrib/mariadb-connector-c"]
3434
path = contrib/mariadb-connector-c
3535
url = https://github.com/ClickHouse/mariadb-connector-c.git
36-
[submodule "contrib/jemalloc"]
37-
path = contrib/jemalloc
38-
url = https://github.com/jemalloc/jemalloc.git
3936
[submodule "contrib/unixodbc"]
4037
path = contrib/unixodbc
4138
url = https://github.com/ClickHouse/UnixODBC.git
@@ -294,3 +291,6 @@
294291
[submodule "contrib/google-benchmark"]
295292
path = contrib/google-benchmark
296293
url = https://github.com/google/benchmark.git
294+
[submodule "contrib/jemalloc"]
295+
path = contrib/jemalloc
296+
url = https://github.com/auxten/jemalloc.git

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,9 @@ add_subdirectory (contrib EXCLUDE_FROM_ALL)
521521

522522
if (NOT ENABLE_JEMALLOC)
523523
message (WARNING "Non default allocator is disabled. This is not recommended for production builds.")
524+
else ()
525+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_JEMALLOC=1 -DJEMALLOC_NO_RENAME=1")
526+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUSE_JEMALLOC=1 -DJEMALLOC_NO_RENAME=1")
524527
endif ()
525528

526529
macro (clickhouse_add_executable target)

chdb/build.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ BUILD_DIR=${PROJ_DIR}/buildlib
1212
if [ "$(uname)" == "Darwin" ]; then
1313
GLIBC_COMPATIBILITY="-DGLIBC_COMPATIBILITY=0"
1414
UNWIND="-DUSE_UNWIND=0"
15+
JEMALLOC="-DENABLE_JEMALLOC=0"
1516
PYINIT_ENTRY="-Wl,-exported_symbol,_PyInit_${CHDB_PY_MOD}"
1617
# if Darwin ARM64 (M1, M2), disable AVX
1718
if [ "$(uname -m)" == "arm64" ]; then
@@ -42,6 +43,7 @@ if [ "$(uname)" == "Darwin" ]; then
4243
elif [ "$(uname)" == "Linux" ]; then
4344
GLIBC_COMPATIBILITY="-DGLIBC_COMPATIBILITY=1"
4445
UNWIND="-DUSE_UNWIND=1"
46+
JEMALLOC="-DENABLE_JEMALLOC=1"
4547
PYINIT_ENTRY="-Wl,-ePyInit_${CHDB_PY_MOD}"
4648
AVX_SUPPORT="-DENABLE_AVX=1 -DENABLE_AVX2=0"
4749
EMBEDDED_COMPILER="-DENABLE_EMBEDDED_COMPILER=1"
@@ -67,7 +69,7 @@ cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_THINLTO=1 -DENABLE_TESTS=0 -DENABLE_CL
6769
${GLIBC_COMPATIBILITY} \
6870
-DCLICKHOUSE_ONE_SHARED=0 \
6971
-DENABLE_UTILS=0 ${EMBEDDED_COMPILER} ${UNWIND} \
70-
-DENABLE_ICU=0 -DENABLE_JEMALLOC=0 \
72+
-DENABLE_ICU=0 ${JEMALLOC} \
7173
-DENABLE_PARQUET=1 -DENABLE_ROCKSDB=1 -DENABLE_SQLITE=1 -DENABLE_VECTORSCAN=1 \
7274
-DENABLE_PROTOBUF=1 -DENABLE_THRIFT=1 \
7375
-DENABLE_CLICKHOUSE_ALL=0 -DUSE_STATIC_LIBRARIES=1 -DSPLIT_SHARED_LIBRARIES=0 \
@@ -98,6 +100,13 @@ LIBCHDB_CMD=$(grep 'clang++.*-o programs/clickhouse .*' build.log \
98100
| sed 's/ -Xlinker --no-undefined//g' \
99101
)
100102

103+
if [ "$(uname)" == "Linux" ]; then
104+
# remove src/CMakeFiles/clickhouse_malloc.dir/Common/stubFree.c.o
105+
LIBCHDB_CMD=$(echo ${LIBCHDB_CMD} | sed 's/ src\/CMakeFiles\/clickhouse_malloc.dir\/Common\/stubFree.c.o//g')
106+
# put -Wl,-wrap,malloc ... after -DUSE_JEMALLOC=1
107+
LIBCHDB_CMD=$(echo ${LIBCHDB_CMD} | sed 's/ -DUSE_JEMALLOC=1/ -DUSE_JEMALLOC=1 -Wl,-wrap,malloc -Wl,-wrap,valloc -Wl,-wrap,pvalloc -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,memalign -Wl,-wrap,aligned_alloc -Wl,-wrap,posix_memalign -Wl,-wrap,free/g')
108+
fi
109+
101110
# save the command to a file for debug
102111
echo ${LIBCHDB_CMD} > libchdb_cmd.sh
103112

contrib/jemalloc

Submodule jemalloc updated 77 files

contrib/jemalloc-cmake/include/jemalloc/jemalloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern "C" {
1010
#endif
1111

1212
#include <jemalloc/jemalloc_defs.h>
13-
#include <jemalloc/jemalloc_rename.h>
13+
// #include <jemalloc/jemalloc_rename.h>
1414
#include <jemalloc/jemalloc_macros.h>
1515
#include <jemalloc/jemalloc_protos.h>
1616
#include <jemalloc/jemalloc_typedefs.h>

contrib/jemalloc-cmake/include_linux_x86_64/jemalloc/internal/jemalloc_internal_defs.h.in

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@
66
* public APIs to be prefixed. This makes it possible, with some care, to use
77
* multiple allocators simultaneously.
88
*/
9-
/* #undef JEMALLOC_PREFIX */
10-
/* #undef JEMALLOC_CPREFIX */
9+
#define JEMALLOC_PREFIX "je_"
10+
#define JEMALLOC_CPREFIX "JE_"
1111

1212
/*
1313
* Define overrides for non-standard allocator-related functions if they are
1414
* present on the system.
1515
*/
16-
#if !defined(USE_MUSL)
17-
#define JEMALLOC_OVERRIDE___LIBC_CALLOC
18-
#define JEMALLOC_OVERRIDE___LIBC_FREE
19-
#define JEMALLOC_OVERRIDE___LIBC_MALLOC
20-
#define JEMALLOC_OVERRIDE___LIBC_MEMALIGN
21-
#define JEMALLOC_OVERRIDE___LIBC_REALLOC
22-
#define JEMALLOC_OVERRIDE___LIBC_VALLOC
23-
#endif
16+
/* #undef JEMALLOC_OVERRIDE___LIBC_CALLOC */
17+
/* #undef JEMALLOC_OVERRIDE___LIBC_FREE */
18+
/* #undef JEMALLOC_OVERRIDE___LIBC_MALLOC */
19+
/* #undef JEMALLOC_OVERRIDE___LIBC_MEMALIGN */
20+
/* #undef JEMALLOC_OVERRIDE___LIBC_REALLOC */
21+
/* #undef JEMALLOC_OVERRIDE___LIBC_VALLOC */
2422
/* #undef JEMALLOC_OVERRIDE___POSIX_MEMALIGN */
2523

2624
/*
@@ -129,7 +127,7 @@
129127
* Among other things, it must be possible to initialize a mutex without
130128
* triggering allocation in order for threaded allocation to be safe.
131129
*/
132-
#define JEMALLOC_THREADED_INIT
130+
/* #undef JEMALLOC_THREADED_INIT */
133131

134132
/*
135133
* Defined if the pthreads implementation defines
@@ -139,7 +137,8 @@
139137
/* #undef JEMALLOC_MUTEX_INIT_CB */
140138

141139
/* Non-empty if the tls_model attribute is supported. */
142-
#define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec")))
140+
// #define JEMALLOC_TLS_MODEL __attribute__((tls_model("initial-exec")))
141+
#define JEMALLOC_TLS_MODEL
143142

144143
/*
145144
* JEMALLOC_DEBUG enables assertions and other sanity checks, and disables
@@ -374,10 +373,10 @@
374373
#define LG_SIZEOF_INTMAX_T 3
375374

376375
/* glibc malloc hooks (__malloc_hook, __realloc_hook, __free_hook). */
377-
#define JEMALLOC_GLIBC_MALLOC_HOOK
376+
/* #undef JEMALLOC_GLIBC_MALLOC_HOOK */
378377

379378
/* glibc memalign hook. */
380-
#define JEMALLOC_GLIBC_MEMALIGN_HOOK
379+
/* #undef JEMALLOC_GLIBC_MEMALIGN_HOOK */
381380

382381
/* pthread support */
383382
#define JEMALLOC_HAVE_PTHREAD
@@ -397,7 +396,7 @@
397396
/*
398397
* If defined, all the features necessary for background threads are present.
399398
*/
400-
#define JEMALLOC_BACKGROUND_THREAD
399+
/* #undef JEMALLOC_BACKGROUND_THREAD */
401400

402401
/*
403402
* If defined, jemalloc symbols are not exported (doesn't work when
@@ -409,7 +408,7 @@
409408
#define JEMALLOC_CONFIG_MALLOC_CONF "@JEMALLOC_CONFIG_MALLOC_CONF@"
410409

411410
/* If defined, jemalloc takes the malloc/free/etc. symbol names. */
412-
#define JEMALLOC_IS_MALLOC
411+
/* #undef JEMALLOC_IS_MALLOC */
413412

414413
/*
415414
* Defined if strerror_r returns char * if _GNU_SOURCE is defined.

programs/local/LocalChdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace py = pybind11;
1010

11-
// extern bool inside_main = true;
11+
extern bool inside_main = true;
1212

1313
class __attribute__((visibility("default"))) query_result
1414
{

programs/main.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,23 @@ struct Checker
344344
#endif
345345
;
346346

347+
// // if ENABLE_JEMALLOC is 1, we need to initialize it before any other memory allocation
348+
// // #if ENABLE_JEMALLOC
349+
// extern "C" {
350+
// extern bool malloc_init(void);
351+
// }
352+
// struct JEMallocInitializer
353+
// {
354+
// JEMallocInitializer()
355+
// {
356+
// malloc_init();
357+
// if (void * ptr = malloc(0))
358+
// {
359+
// free(ptr);
360+
// }
361+
// }
362+
// } jemalloc_initializer __attribute__((init_priority(101)));
363+
// // #endif
347364

348365
#if !defined(DISABLE_HARMFUL_ENV_VAR_CHECK) && !defined(USE_MUSL)
349366
/// NOTE: We will migrate to full static linking or our own dynamic loader to make this code obsolete.
@@ -439,17 +456,17 @@ void checkHarmfulEnvironmentVariables(char ** argv)
439456
///
440457
/// extern bool inside_main;
441458
/// class C { C() { assert(inside_main); } };
442-
#ifndef FUZZING_MODE
443-
bool inside_main = false;
444-
#else
445-
bool inside_main = true;
446-
#endif
459+
// #ifndef FUZZING_MODE
460+
// bool inside_main = false;
461+
// #else
462+
// bool inside_main = true;
463+
// #endif
447464

448465
#if !defined(FUZZING_MODE)
449466
int main(int argc_, char ** argv_)
450467
{
451-
inside_main = true;
452-
SCOPE_EXIT({ inside_main = false; });
468+
// inside_main = true;
469+
// SCOPE_EXIT({ inside_main = false; });
453470

454471
/// PHDR cache is required for query profiler to work reliably
455472
/// It also speed up exception handling, but exceptions from dynamically loaded libraries (dlopen)

programs/server/Server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static bool jemallocOptionEnabled(const char *name)
148148
bool value;
149149
size_t size = sizeof(value);
150150

151-
if (mallctl(name, reinterpret_cast<void *>(&value), &size, /* newp= */ nullptr, /* newlen= */ 0))
151+
if (je_mallctl(name, reinterpret_cast<void *>(&value), &size, /* newp= */ nullptr, /* newlen= */ 0))
152152
throw Poco::SystemException("mallctl() failed");
153153

154154
return value;

0 commit comments

Comments
 (0)