Skip to content

Commit 7d5a4eb

Browse files
committed
[CMake] Expand support for preinstalled jemalloc
Signed-off-by: Sarnie, Nick <[email protected]>
1 parent c765222 commit 7d5a4eb

File tree

3 files changed

+110
-67
lines changed

3 files changed

+110
-67
lines changed

CMakeLists.txt

Lines changed: 79 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -155,79 +155,92 @@ if(NOT UMF_BUILD_LIBUMF_POOL_JEMALLOC)
155155
set(UMF_POOL_JEMALLOC_ENABLED FALSE)
156156
set(JEMALLOC_FOUND FALSE)
157157
set(JEMALLOC_LIBRARIES FALSE)
158-
elseif(WINDOWS)
159-
pkg_check_modules(JEMALLOC jemalloc)
160-
if(NOT JEMALLOC_FOUND)
161-
find_package(JEMALLOC REQUIRED jemalloc)
162-
endif()
163158
else()
164-
if(NOT DEFINED UMF_JEMALLOC_REPO)
165-
set(UMF_JEMALLOC_REPO "https://github.com/jemalloc/jemalloc.git")
159+
set(JEMALLOC_VER 5.3.0)
160+
pkg_check_modules(JEMALLOC QUIET jemalloc)
161+
if(NOT JEMALLOC_FOUND)
162+
find_package(JEMALLOC ${JEMALLOC_VER} QUIET)
166163
endif()
164+
if(JEMALLOC_FOUND)
165+
# We may find a cached result from the in-tree built which uses
166+
# different symbol names, so compile a test profile to check if the
167+
# found version is a system install or not.
168+
set(JEMALLOC_TEST_PROGRAM "#include <jemalloc/jemalloc.h>
169+
int main(int, char*[]) { (void) je_mallocx(0, 0); return 0; }")
170+
include(CheckCSourceCompiles)
171+
set(CMAKE_REQUIRED_INCLUDES ${JEMALLOC_INCLUDE_DIRS})
172+
set(CMAKE_REQUIRED_LIBRARIES ${JEMALLOC_Library})
173+
check_c_source_compiles("${JEMALLOC_TEST_PROGRAM}"
174+
SYSTEM_JEMALLOC_FOUND)
175+
elseif(NOT WINDOWS) # Cannot build from source on Windows
176+
if(NOT DEFINED UMF_JEMALLOC_REPO)
177+
set(UMF_JEMALLOC_REPO "https://github.com/jemalloc/jemalloc.git")
178+
endif()
167179

168-
if(NOT DEFINED UMF_JEMALLOC_TAG)
169-
set(UMF_JEMALLOC_TAG 5.3.0)
170-
endif()
180+
if(NOT DEFINED UMF_JEMALLOC_TAG)
181+
set(UMF_JEMALLOC_TAG ${JEMALLOC_VER})
182+
endif()
171183

172-
message(
173-
STATUS
174-
"Will fetch jemalloc from ${UMF_JEMALLOC_REPO} (tag: ${UMF_JEMALLOC_TAG})"
175-
)
184+
message(
185+
STATUS
186+
"Will fetch jemalloc from ${UMF_JEMALLOC_REPO} (tag: ${UMF_JEMALLOC_TAG})"
187+
)
176188

177-
FetchContent_Declare(
178-
jemalloc_targ
179-
GIT_REPOSITORY ${UMF_JEMALLOC_REPO}
180-
GIT_TAG ${UMF_JEMALLOC_TAG})
181-
FetchContent_MakeAvailable(jemalloc_targ)
189+
FetchContent_Declare(
190+
jemalloc_targ
191+
GIT_REPOSITORY ${UMF_JEMALLOC_REPO}
192+
GIT_TAG ${UMF_JEMALLOC_TAG})
193+
FetchContent_MakeAvailable(jemalloc_targ)
182194

183-
add_custom_command(
184-
COMMAND ./autogen.sh
185-
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
186-
OUTPUT ${jemalloc_targ_SOURCE_DIR}/configure)
187-
add_custom_command(
188-
# Custom jemalloc build. Non-default options used:
189-
# --with-jemalloc-prefix=je_ - add je_ prefix to all public APIs
190-
# --disable-cxx - Disable C++ integration. This will cause new and
191-
# delete operators implementations to be omitted.
192-
# --disable-initial-exec-tls - Disable the initial-exec TLS model for
193-
# jemalloc's internal thread-local storage (on those platforms that
194-
# support explicit settings). This can allow jemalloc to be dynamically
195-
# loaded after program startup (e.g. using dlopen). --disable-doc -
196-
# Disable building and installing the documentation.
197-
COMMAND
198-
./configure --prefix=${jemalloc_targ_BINARY_DIR}
199-
--with-jemalloc-prefix=je_ --disable-cxx --disable-initial-exec-tls
200-
--disable-doc CFLAGS=-fPIC
201-
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
202-
OUTPUT ${jemalloc_targ_SOURCE_DIR}/Makefile
203-
DEPENDS ${jemalloc_targ_SOURCE_DIR}/configure)
204-
205-
if(NOT UMF_QEMU_BUILD)
206-
set(MAKE_ARGUMENTS "-j$(nproc)")
207-
endif()
195+
add_custom_command(
196+
COMMAND ./autogen.sh
197+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
198+
OUTPUT ${jemalloc_targ_SOURCE_DIR}/configure)
199+
add_custom_command(
200+
# Custom jemalloc build. Non-default options used:
201+
# --with-jemalloc-prefix=je_ - add je_ prefix to all public APIs
202+
# --disable-cxx - Disable C++ integration. This will cause new and
203+
# delete operators implementations to be omitted.
204+
# --disable-initial-exec-tls - Disable the initial-exec TLS model
205+
# for jemalloc's internal thread-local storage (on those platforms
206+
# that support explicit settings). This can allow jemalloc to be
207+
# dynamically loaded after program startup (e.g. using dlopen).
208+
# --disable-doc - Disable building and installing the documentation.
209+
COMMAND
210+
./configure --prefix=${jemalloc_targ_BINARY_DIR}
211+
--with-jemalloc-prefix=je_ --disable-cxx
212+
--disable-initial-exec-tls --disable-doc CFLAGS=-fPIC
213+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
214+
OUTPUT ${jemalloc_targ_SOURCE_DIR}/Makefile
215+
DEPENDS ${jemalloc_targ_SOURCE_DIR}/configure)
216+
217+
if(NOT UMF_QEMU_BUILD)
218+
set(MAKE_ARGUMENTS "-j$(nproc)")
219+
endif()
208220

209-
add_custom_command(
210-
COMMAND make ${MAKE_ARGUMENTS}
211-
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
212-
OUTPUT ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a
213-
DEPENDS ${jemalloc_targ_SOURCE_DIR}/Makefile)
214-
add_custom_command(
215-
COMMAND make install
216-
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
217-
OUTPUT ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a
218-
DEPENDS ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a)
219-
220-
add_custom_target(jemalloc_prod
221-
DEPENDS ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
222-
add_library(jemalloc INTERFACE)
223-
target_link_libraries(
224-
jemalloc INTERFACE ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
225-
add_dependencies(jemalloc jemalloc_prod)
226-
227-
set(JEMALLOC_FOUND TRUE)
228-
set(JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR}/lib)
229-
set(JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR}/include)
230-
set(JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
221+
add_custom_command(
222+
COMMAND make ${MAKE_ARGUMENTS}
223+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
224+
OUTPUT ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a
225+
DEPENDS ${jemalloc_targ_SOURCE_DIR}/Makefile)
226+
add_custom_command(
227+
COMMAND make install
228+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
229+
OUTPUT ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a
230+
DEPENDS ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.a)
231+
232+
add_custom_target(jemalloc_prod
233+
DEPENDS ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
234+
add_library(jemalloc INTERFACE)
235+
target_link_libraries(
236+
jemalloc INTERFACE ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
237+
add_dependencies(jemalloc jemalloc_prod)
238+
239+
set(JEMALLOC_FOUND TRUE)
240+
set(JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR}/lib)
241+
set(JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR}/include)
242+
set(JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
243+
endif()
231244
endif()
232245

233246
if(JEMALLOC_FOUND)

src/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ if(UMF_POOL_JEMALLOC_ENABLED)
7676
${JEMALLOC_INCLUDE_DIRS})
7777
set(UMF_COMMON_COMPILE_DEFINITIONS ${UMF_COMMON_COMPILE_DEFINITIONS}
7878
"UMF_POOL_JEMALLOC_ENABLED=1")
79+
if(SYSTEM_JEMALLOC_FOUND)
80+
set(UMF_COMMON_COMPILE_DEFINITIONS ${UMF_COMMON_COMPILE_DEFINITIONS}
81+
"UMF_USING_PREBUILT_JEMALLOC=1")
82+
endif()
7983
endif()
8084

8185
set(UMF_PRIVATE_LIBRARY_DIRS ${UMF_PRIVATE_LIBRARY_DIRS}
@@ -166,7 +170,7 @@ if(UMF_LINK_HWLOC_STATICALLY)
166170
endif()
167171
endif()
168172

169-
if(NOT WINDOWS AND UMF_POOL_JEMALLOC_ENABLED)
173+
if(UMF_POOL_JEMALLOC_ENABLED AND TARGET jemalloc)
170174
add_dependencies(umf jemalloc)
171175
endif()
172176

src/pool/pool_jemalloc.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ umfJemallocPoolParamsSetName(umf_jemalloc_pool_params_handle_t hParams,
5959
#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)
6060
#define DEFAULT_NAME "jemalloc"
6161

62+
#ifdef UMF_USING_PREBUILT_JEMALLOC
63+
64+
// When jemalloc is built in-tree, it is configured such that
65+
// all symbols are prepended with the je_ prefix. This is not
66+
// the case for system-installed jemalloc.
67+
68+
#define je_mallctl(...) mallctl(__VA_ARGS__)
69+
70+
#define je_mallocx(...) mallocx(__VA_ARGS__)
71+
72+
#define je_dallocx(...) dallocx(__VA_ARGS__)
73+
74+
#define je_rallocx(...) rallocx(__VA_ARGS__)
75+
76+
#define je_malloc_usable_size(...) malloc_usable_size(__VA_ARGS__)
77+
78+
#endif
79+
6280
typedef struct umf_jemalloc_pool_params_t {
6381
size_t n_arenas;
6482
char name[64];
@@ -678,4 +696,12 @@ umfJemallocPoolParamsSetName(umf_jemalloc_pool_params_handle_t hParams,
678696
return UMF_RESULT_SUCCESS;
679697
}
680698

699+
#ifdef UMF_USING_PREBUILT_JEMALLOC
700+
#undef je_mallctl
701+
#undef je_mallocx
702+
#undef je_dallocx
703+
#undef je_rallocx
704+
#undef je_malloc_usable_size
705+
#endif
706+
681707
#endif /* UMF_POOL_JEMALLOC_ENABLED */

0 commit comments

Comments
 (0)