Skip to content

Commit e1bd905

Browse files
ldoraulplewa
authored andcommitted
Link statically with custom jemalloc with disabled initial TLS
Link statically with custom jemalloc built from sources with the following non-default options enabled: --with-jemalloc-prefix=je_ - add je_ prefix to all public APIs --disable-cxx - Disable C++ integration. This will cause new and delete operators implementations to be omitted. --disable-initial-exec-tls - Disable the initial-exec TLS model for jemalloc's internal thread-local storage (on those platforms that support explicit settings). This can allow jemalloc to be dynamically loaded after program startup (e.g. using dlopen). Fixes: oneapi-src#891 Fixes: oneapi-src#894 Fixes: oneapi-src#903 Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent 97ee384 commit e1bd905

27 files changed

+148
-137
lines changed

.github/docker/ubuntu-20.04.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ ARG BASE_DEPS="\
2424

2525
# UMF's dependencies
2626
ARG UMF_DEPS="\
27-
libjemalloc-dev \
2827
libhwloc-dev \
2928
libtbb-dev"
3029

@@ -34,6 +33,7 @@ ARG TEST_DEPS="\
3433

3534
# Miscellaneous for our builds/CI (optional)
3635
ARG MISC_DEPS="\
36+
automake \
3737
clang \
3838
g++-7 \
3939
python3-pip \

.github/docker/ubuntu-22.04.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ ARG BASE_DEPS="\
2424

2525
# UMF's dependencies
2626
ARG UMF_DEPS="\
27-
libjemalloc-dev \
2827
libhwloc-dev \
2928
libtbb-dev"
3029

@@ -34,6 +33,7 @@ ARG TEST_DEPS="\
3433

3534
# Miscellaneous for our builds/CI (optional)
3635
ARG MISC_DEPS="\
36+
automake \
3737
clang \
3838
python3-pip \
3939
sudo \

.github/workflows/coverity.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- name: Install apt packages
3232
run: |
3333
sudo apt-get update
34-
sudo apt-get install -y cmake hwloc libhwloc-dev libjemalloc-dev libnuma-dev libtbb-dev
34+
sudo apt-get install -y cmake hwloc libhwloc-dev libnuma-dev libtbb-dev
3535
3636
- name: Download Coverity
3737
run: |

.github/workflows/nightly.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
- name: Install apt packages
6868
run: |
6969
sudo apt-get update
70-
sudo apt-get install -y cmake hwloc libhwloc-dev libjemalloc-dev libnuma-dev libtbb-dev valgrind
70+
sudo apt-get install -y cmake hwloc libhwloc-dev libnuma-dev libtbb-dev valgrind
7171
7272
- name: Configure CMake
7373
run: >

.github/workflows/reusable_basic.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ jobs:
124124
- name: Install apt packages
125125
run: |
126126
sudo apt-get update
127-
sudo apt-get install -y clang cmake libnuma-dev libjemalloc-dev lcov
127+
sudo apt-get install -y clang cmake libnuma-dev lcov
128128
129129
- name: Install TBB apt package
130130
if: matrix.install_tbb == 'ON'
@@ -469,7 +469,7 @@ jobs:
469469
python3 -m pip install -r third_party/requirements.txt
470470
471471
- name: Install hwloc
472-
run: brew install hwloc jemalloc tbb
472+
run: brew install hwloc tbb automake
473473

474474
- name: Configure build
475475
run: >

.github/workflows/reusable_benchmarks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
if: matrix.os == 'ubuntu-latest'
3535
run: |
3636
sudo apt-get update
37-
sudo apt-get install -y cmake libhwloc-dev libnuma-dev libjemalloc-dev libtbb-dev
37+
sudo apt-get install -y cmake libhwloc-dev libnuma-dev libtbb-dev
3838
3939
- name: Initialize vcpkg
4040
if: matrix.os == 'windows-latest'

.github/workflows/reusable_codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
if: matrix.os == 'ubuntu-latest'
6363
run: |
6464
sudo apt-get update
65-
sudo apt-get install -y cmake clang libhwloc-dev libnuma-dev libjemalloc-dev libtbb-dev
65+
sudo apt-get install -y cmake clang libhwloc-dev libnuma-dev libtbb-dev
6666
6767
# Latest distros do not allow global pip installation
6868
- name: "[Lin] Install Python requirements in venv"

.github/workflows/reusable_fast.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ jobs:
7979
if: matrix.os == 'ubuntu-latest'
8080
run: |
8181
sudo apt-get update
82-
sudo apt-get install -y cmake libjemalloc-dev libhwloc-dev libnuma-dev libtbb-dev
82+
sudo apt-get install -y cmake libhwloc-dev libnuma-dev libtbb-dev
8383
8484
- name: Install dependencies (ubuntu-20.04)
8585
if: matrix.os == 'ubuntu-20.04'
8686
run: |
8787
sudo apt-get update
88-
sudo apt-get install -y cmake libjemalloc-dev libnuma-dev libtbb-dev
88+
sudo apt-get install -y cmake libnuma-dev libtbb-dev
8989
.github/scripts/install_hwloc.sh # install hwloc-2.3.0 instead of hwloc-2.1.0 present in the OS package
9090
9191
- name: Set ptrace value for IPC test (on Linux only)

.github/workflows/reusable_proxy_lib.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- name: Install apt packages
3333
run: |
3434
sudo apt-get update
35-
sudo apt-get install -y cmake libhwloc-dev libjemalloc-dev libtbb-dev lcov
35+
sudo apt-get install -y cmake libhwloc-dev libtbb-dev lcov
3636
3737
- name: Set ptrace value for IPC test
3838
run: sudo bash -c "echo 0 > /proc/sys/kernel/yama/ptrace_scope"

.github/workflows/reusable_sanitizers.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- name: Install apt packages
3030
run: |
3131
sudo apt-get update
32-
sudo apt-get install -y clang cmake libhwloc-dev libnuma-dev libjemalloc-dev libtbb-dev
32+
sudo apt-get install -y clang cmake libhwloc-dev libnuma-dev libtbb-dev
3333
3434
- name: Install oneAPI basekit
3535
if: matrix.compiler.cxx == 'icpx'

.github/workflows/reusable_valgrind.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Install apt packages
2121
run: |
2222
sudo apt-get update
23-
sudo apt-get install -y cmake hwloc libhwloc-dev libjemalloc-dev libnuma-dev libtbb-dev valgrind
23+
sudo apt-get install -y cmake hwloc libhwloc-dev libnuma-dev libtbb-dev valgrind
2424
2525
- name: Configure CMake
2626
run: >

CMakeLists.txt

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,87 @@ else()
111111
message(FATAL_ERROR "Unknown OS type")
112112
endif()
113113

114+
if(NOT UMF_BUILD_LIBUMF_POOL_JEMALLOC)
115+
set(UMF_POOL_JEMALLOC_ENABLED FALSE)
116+
elseif(WINDOWS)
117+
pkg_check_modules(JEMALLOC jemalloc)
118+
if(NOT JEMALLOC_FOUND)
119+
find_package(JEMALLOC REQUIRED jemalloc)
120+
endif()
121+
else()
122+
if(NOT DEFINED UMF_JEMALLOC_REPO)
123+
set(UMF_JEMALLOC_REPO "https://github.com/jemalloc/jemalloc.git")
124+
endif()
125+
126+
if(NOT DEFINED UMF_JEMALLOC_TAG)
127+
set(UMF_JEMALLOC_TAG 5.3.0)
128+
endif()
129+
130+
include(FetchContent)
131+
message(
132+
STATUS
133+
"Will fetch jemalloc from ${UMF_JEMALLOC_REPO} (tag: ${UMF_JEMALLOC_TAG})"
134+
)
135+
136+
FetchContent_Declare(
137+
jemalloc_targ
138+
GIT_REPOSITORY ${UMF_JEMALLOC_REPO}
139+
GIT_TAG ${UMF_JEMALLOC_TAG})
140+
FetchContent_MakeAvailable(jemalloc_targ)
141+
142+
add_custom_command(
143+
COMMAND ./autogen.sh
144+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
145+
OUTPUT ${jemalloc_targ_SOURCE_DIR}/configure)
146+
add_custom_command(
147+
# Custom jemalloc build. Non-default options used:
148+
# --with-jemalloc-prefix=je_ - add je_ prefix to all public APIs
149+
# --disable-cxx - Disable C++ integration. This will cause new and
150+
# delete operators implementations to be omitted.
151+
# --disable-initial-exec-tls - Disable the initial-exec TLS model for
152+
# jemalloc's internal thread-local storage (on those platforms that
153+
# support explicit settings). This can allow jemalloc to be dynamically
154+
# loaded after program startup (e.g. using dlopen).
155+
COMMAND
156+
./configure --prefix=${jemalloc_targ_BINARY_DIR}
157+
--with-jemalloc-prefix=je_ --disable-cxx --disable-initial-exec-tls
158+
CFLAGS=-fPIC
159+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
160+
OUTPUT ${jemalloc_targ_SOURCE_DIR}/Makefile
161+
DEPENDS ${jemalloc_targ_SOURCE_DIR}/configure)
162+
add_custom_command(
163+
COMMAND make
164+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
165+
OUTPUT ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.la
166+
DEPENDS ${jemalloc_targ_SOURCE_DIR}/Makefile)
167+
add_custom_command(
168+
COMMAND make install
169+
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
170+
OUTPUT ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a
171+
DEPENDS ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.la)
172+
173+
add_custom_target(jemalloc_prod
174+
DEPENDS ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
175+
add_library(jemalloc INTERFACE)
176+
target_link_libraries(
177+
jemalloc INTERFACE ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
178+
add_dependencies(jemalloc jemalloc_prod)
179+
180+
set(JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR}/lib)
181+
set(JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR}/include)
182+
set(JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
183+
endif()
184+
185+
if(JEMALLOC_FOUND OR JEMALLOC_LIBRARIES)
186+
set(UMF_POOL_JEMALLOC_ENABLED TRUE)
187+
# add PATH to DLL on Windows
188+
set(DLL_PATH_LIST
189+
"${DLL_PATH_LIST};PATH=path_list_append:${JEMALLOC_DLL_DIRS}")
190+
message(STATUS " JEMALLOC_LIBRARIES = ${JEMALLOC_LIBRARIES}")
191+
message(STATUS " JEMALLOC_INCLUDE_DIRS = ${JEMALLOC_INCLUDE_DIRS}")
192+
message(STATUS " JEMALLOC_LIBRARY_DIRS = ${JEMALLOC_LIBRARY_DIRS}")
193+
endif()
194+
114195
if(UMF_DISABLE_HWLOC)
115196
message(STATUS "hwloc is disabled, hence OS provider, memtargets, "
116197
"topology discovery, examples won't be available!")
@@ -402,19 +483,6 @@ else()
402483
set(UMF_POOL_SCALABLE_ENABLED FALSE)
403484
endif()
404485

405-
if(UMF_BUILD_LIBUMF_POOL_JEMALLOC)
406-
pkg_check_modules(JEMALLOC jemalloc)
407-
if(NOT JEMALLOC_FOUND)
408-
find_package(JEMALLOC REQUIRED jemalloc)
409-
endif()
410-
if(JEMALLOC_FOUND OR JEMALLOC_LIBRARIES)
411-
set(UMF_POOL_JEMALLOC_ENABLED TRUE)
412-
# add PATH to DLL on Windows
413-
set(DLL_PATH_LIST
414-
"${DLL_PATH_LIST};PATH=path_list_append:${JEMALLOC_DLL_DIRS}")
415-
endif()
416-
endif()
417-
418486
if(WINDOWS)
419487
# TODO: enable the proxy library in the Debug build on Windows
420488
#

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,25 @@ pool manager built as a separate static library: libjemalloc_pool.a on Linux and
281281
jemalloc_pool.lib on Windows.
282282
The `UMF_BUILD_LIBUMF_POOL_JEMALLOC` option has to be turned `ON` to build this library.
283283

284+
[jemalloc](https://github.com/jemalloc/jemalloc) is required to build the jemalloc pool.
285+
286+
In case of Linux OS jemalloc is built from the (fetched) sources with the following
287+
non-default options enabled:
288+
- `--with-jemalloc-prefix=je_` - adds the `je_` prefix to all public APIs,
289+
- `--disable-cxx` - disables C++ integration, it will cause the `new` and the `delete`
290+
operators implementations to be omitted.
291+
- `--disable-initial-exec-tls` - disables the initial-exec TLS model for jemalloc's
292+
internal thread-local storage (on those platforms that support
293+
explicit settings), it can allow jemalloc to be dynamically
294+
loaded after program startup (e.g. using `dlopen()`).
295+
296+
The default jemalloc package is required on Windows.
297+
284298
##### Requirements
285299

286300
1) The `UMF_BUILD_LIBUMF_POOL_JEMALLOC` option turned `ON`
287301
2) Required packages:
288-
- libjemalloc-dev (Linux) or jemalloc (Windows)
302+
- jemalloc (Windows only)
289303

290304
#### Scalable Pool (part of libumf)
291305

benchmark/ubench.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ static void do_ipc_get_put_benchmark(alloc_t *allocs, size_t num_allocs,
445445
}
446446
}
447447

448-
int create_level_zero_params(ze_context_handle_t *context,
449-
ze_device_handle_t *device) {
448+
static int create_level_zero_params(ze_context_handle_t *context,
449+
ze_device_handle_t *device) {
450450
uint32_t driver_idx = 0;
451451
ze_driver_handle_t driver = NULL;
452452

examples/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,11 @@ if(LINUX)
284284
SRCS dram_and_fsdax/dram_and_fsdax.c
285285
LIBS umf jemalloc_pool)
286286

287-
target_link_directories(${EXAMPLE_NAME} PRIVATE
288-
${LIBHWLOC_LIBRARY_DIRS})
287+
target_link_options(${EXAMPLE_NAME} PRIVATE "-Wl,--no-as-needed,-ldl")
288+
289+
target_link_directories(
290+
${EXAMPLE_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS}
291+
${JEMALLOC_LIBRARY_DIRS})
289292

290293
add_test(
291294
NAME ${EXAMPLE_NAME}

examples/cmake/FindJEMALLOC.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5-
message(STATUS "Checking for module 'jemalloc' using find_library()")
5+
message(
6+
STATUS "Looking for the static 'libjemalloc.a' library using find_library()"
7+
)
68

7-
find_library(JEMALLOC_LIBRARY NAMES libjemalloc jemalloc)
9+
find_library(JEMALLOC_LIBRARY NAMES libjemalloc.a jemalloc.a)
810
set(JEMALLOC_LIBRARIES ${JEMALLOC_LIBRARY})
911

1012
get_filename_component(JEMALLOC_LIB_DIR ${JEMALLOC_LIBRARIES} DIRECTORY)

examples/dram_and_fsdax/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ if(NOT LIBHWLOC_FOUND)
2121
find_package(LIBHWLOC 2.3.0 REQUIRED hwloc)
2222
endif()
2323

24-
pkg_check_modules(JEMALLOC jemalloc)
25-
if(NOT JEMALLOC_FOUND)
26-
find_package(JEMALLOC REQUIRED jemalloc)
27-
endif()
24+
# find the custom jemalloc pointed by CMAKE_PREFIX_PATH
25+
find_package(JEMALLOC REQUIRED jemalloc)
2826

2927
# build the example
3028
set(EXAMPLE_NAME umf_example_dram_and_fsdax)

scripts/qemu/run-build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pwd
1414

1515
echo password | sudo -Sk apt-get update
1616
echo password | sudo -Sk apt-get install -y git cmake gcc g++ pkg-config \
17-
numactl libnuma-dev hwloc libhwloc-dev libjemalloc-dev libtbb-dev valgrind lcov
17+
numactl libnuma-dev hwloc libhwloc-dev libtbb-dev valgrind lcov
1818

1919
mkdir build
2020
cd build

src/pool/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ if(UMF_BUILD_LIBUMF_POOL_JEMALLOC)
4545
NAME jemalloc_pool
4646
TYPE STATIC
4747
SRCS pool_jemalloc.c ${POOL_EXTRA_SRCS}
48-
LIBS jemalloc ${POOL_EXTRA_LIBS})
48+
LIBS ${JEMALLOC_LIBRARIES} ${POOL_EXTRA_LIBS})
4949
target_include_directories(jemalloc_pool PRIVATE ${JEMALLOC_INCLUDE_DIRS})
5050
target_compile_definitions(jemalloc_pool
5151
PRIVATE ${POOL_COMPILE_DEFINITIONS})
5252
add_library(${PROJECT_NAME}::jemalloc_pool ALIAS jemalloc_pool)
53+
if(NOT WINDOWS)
54+
add_dependencies(jemalloc_pool jemalloc)
55+
endif()
5356
install(TARGETS jemalloc_pool EXPORT ${PROJECT_NAME}-targets)
5457
endif()

src/pool/pool_jemalloc.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@
2222

2323
#include <jemalloc/jemalloc.h>
2424

25-
// The Windows version of jemalloc uses API with je_ prefix,
26-
// while the Linux one does not.
27-
#ifndef _WIN32
28-
#define je_mallocx mallocx
29-
#define je_dallocx dallocx
30-
#define je_rallocx rallocx
31-
#define je_mallctl mallctl
32-
#define je_malloc_usable_size malloc_usable_size
33-
#endif
34-
3525
#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)
3626

3727
typedef struct jemalloc_memory_pool_t {

test/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,13 @@ if(LINUX
747747
set(STANDALONE_CMAKE_OPTIONS
748748
"-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
749749
)
750+
if(JEMALLOC_INCLUDE_DIRS)
751+
# add custom jemalloc installation
752+
set(STANDALONE_CMAKE_OPTIONS
753+
"${STANDALONE_CMAKE_OPTIONS} -DCMAKE_PREFIX_PATH=${JEMALLOC_INCLUDE_DIRS}/../"
754+
)
755+
endif()
756+
750757
add_test(
751758
NAME umf-standalone_examples
752759
COMMAND

0 commit comments

Comments
 (0)