Skip to content

Commit

Permalink
Link statically with custom jemalloc with disabled initial TLS
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ldorau committed Dec 3, 2024
1 parent b021cc7 commit 5298b5e
Show file tree
Hide file tree
Showing 27 changed files with 148 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .github/docker/ubuntu-20.04.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ ARG BASE_DEPS="\

# UMF's dependencies
ARG UMF_DEPS="\
libjemalloc-dev \
libhwloc-dev \
libtbb-dev"

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

# Miscellaneous for our builds/CI (optional)
ARG MISC_DEPS="\
automake \
clang \
g++-7 \
python3-pip \
Expand Down
2 changes: 1 addition & 1 deletion .github/docker/ubuntu-22.04.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ ARG BASE_DEPS="\

# UMF's dependencies
ARG UMF_DEPS="\
libjemalloc-dev \
libhwloc-dev \
libtbb-dev"

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

# Miscellaneous for our builds/CI (optional)
ARG MISC_DEPS="\
automake \
clang \
python3-pip \
sudo \
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y cmake hwloc libhwloc-dev libjemalloc-dev libnuma-dev libtbb-dev
sudo apt-get install -y cmake hwloc libhwloc-dev libnuma-dev libtbb-dev
- name: Download Coverity
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y cmake hwloc libhwloc-dev libjemalloc-dev libnuma-dev libtbb-dev valgrind
sudo apt-get install -y cmake hwloc libhwloc-dev libnuma-dev libtbb-dev valgrind
- name: Configure CMake
run: >
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/reusable_basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y clang cmake libnuma-dev libjemalloc-dev lcov
sudo apt-get install -y clang cmake libnuma-dev lcov
- name: Install TBB apt package
if: matrix.install_tbb == 'ON'
Expand Down Expand Up @@ -469,7 +469,7 @@ jobs:
python3 -m pip install -r third_party/requirements.txt
- name: Install hwloc
run: brew install hwloc jemalloc tbb
run: brew install hwloc tbb automake

- name: Configure build
run: >
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y cmake libhwloc-dev libnuma-dev libjemalloc-dev libtbb-dev
sudo apt-get install -y cmake libhwloc-dev libnuma-dev libtbb-dev
- name: Initialize vcpkg
if: matrix.os == 'windows-latest'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y cmake clang libhwloc-dev libnuma-dev libjemalloc-dev libtbb-dev
sudo apt-get install -y cmake clang libhwloc-dev libnuma-dev libtbb-dev
# Latest distros do not allow global pip installation
- name: "[Lin] Install Python requirements in venv"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/reusable_fast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ jobs:
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y cmake libjemalloc-dev libhwloc-dev libnuma-dev libtbb-dev
sudo apt-get install -y cmake libhwloc-dev libnuma-dev libtbb-dev
- name: Install dependencies (ubuntu-20.04)
if: matrix.os == 'ubuntu-20.04'
run: |
sudo apt-get update
sudo apt-get install -y cmake libjemalloc-dev libnuma-dev libtbb-dev
sudo apt-get install -y cmake libnuma-dev libtbb-dev
.github/scripts/install_hwloc.sh # install hwloc-2.3.0 instead of hwloc-2.1.0 present in the OS package
- name: Set ptrace value for IPC test (on Linux only)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_proxy_lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y cmake libhwloc-dev libjemalloc-dev libtbb-dev lcov
sudo apt-get install -y cmake libhwloc-dev libtbb-dev lcov
- name: Set ptrace value for IPC test
run: sudo bash -c "echo 0 > /proc/sys/kernel/yama/ptrace_scope"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_sanitizers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y clang cmake libhwloc-dev libnuma-dev libjemalloc-dev libtbb-dev
sudo apt-get install -y clang cmake libhwloc-dev libnuma-dev libtbb-dev
- name: Install oneAPI basekit
if: matrix.compiler.cxx == 'icpx'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_valgrind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y cmake hwloc libhwloc-dev libjemalloc-dev libnuma-dev libtbb-dev valgrind
sudo apt-get install -y cmake hwloc libhwloc-dev libnuma-dev libtbb-dev valgrind
- name: Configure CMake
run: >
Expand Down
94 changes: 81 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,87 @@ else()
message(FATAL_ERROR "Unknown OS type")
endif()

if(NOT UMF_BUILD_LIBUMF_POOL_JEMALLOC)
set(UMF_POOL_JEMALLOC_ENABLED FALSE)
elseif(WINDOWS)
pkg_check_modules(JEMALLOC jemalloc)
if(NOT JEMALLOC_FOUND)
find_package(JEMALLOC REQUIRED jemalloc)
endif()
else()
if(NOT DEFINED UMF_JEMALLOC_REPO)
set(UMF_JEMALLOC_REPO "https://github.com/jemalloc/jemalloc.git")
endif()

if(NOT DEFINED UMF_JEMALLOC_TAG)
set(UMF_JEMALLOC_TAG 5.3.0)
endif()

include(FetchContent)
message(
STATUS
"Will fetch jemalloc from ${UMF_JEMALLOC_REPO} (tag: ${UMF_JEMALLOC_TAG})"
)

FetchContent_Declare(
jemalloc_targ
GIT_REPOSITORY ${UMF_JEMALLOC_REPO}
GIT_TAG ${UMF_JEMALLOC_TAG})
FetchContent_MakeAvailable(jemalloc_targ)

add_custom_command(
COMMAND ./autogen.sh
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
OUTPUT ${jemalloc_targ_SOURCE_DIR}/configure)
add_custom_command(
# Custom jemalloc build. Non-default options used:
# --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).
COMMAND
./configure --prefix=${jemalloc_targ_BINARY_DIR}
--with-jemalloc-prefix=je_ --disable-cxx --disable-initial-exec-tls
CFLAGS=-fPIC CXXFLAGS=-fPIC
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
OUTPUT ${jemalloc_targ_SOURCE_DIR}/Makefile
DEPENDS ${jemalloc_targ_SOURCE_DIR}/configure)
add_custom_command(
COMMAND make
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
OUTPUT ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.la
DEPENDS ${jemalloc_targ_SOURCE_DIR}/Makefile)
add_custom_command(
COMMAND make install
WORKING_DIRECTORY ${jemalloc_targ_SOURCE_DIR}
OUTPUT ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a
DEPENDS ${jemalloc_targ_SOURCE_DIR}/lib/libjemalloc.la)

add_custom_target(jemalloc_prod
DEPENDS ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
add_library(jemalloc INTERFACE)
target_link_libraries(
jemalloc INTERFACE ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
add_dependencies(jemalloc jemalloc_prod)

set(JEMALLOC_LIBRARY_DIRS ${jemalloc_targ_BINARY_DIR}/lib)
set(JEMALLOC_INCLUDE_DIRS ${jemalloc_targ_BINARY_DIR}/include)
set(JEMALLOC_LIBRARIES ${jemalloc_targ_BINARY_DIR}/lib/libjemalloc.a)
endif()

if(JEMALLOC_FOUND OR JEMALLOC_LIBRARIES)
set(UMF_POOL_JEMALLOC_ENABLED TRUE)
# add PATH to DLL on Windows
set(DLL_PATH_LIST
"${DLL_PATH_LIST};PATH=path_list_append:${JEMALLOC_DLL_DIRS}")
message(STATUS " JEMALLOC_LIBRARIES = ${JEMALLOC_LIBRARIES}")
message(STATUS " JEMALLOC_INCLUDE_DIRS = ${JEMALLOC_INCLUDE_DIRS}")
message(STATUS " JEMALLOC_LIBRARY_DIRS = ${JEMALLOC_LIBRARY_DIRS}")
endif()

if(UMF_DISABLE_HWLOC)
message(STATUS "hwloc is disabled, hence OS provider, memtargets, "
"topology discovery, examples won't be available!")
Expand Down Expand Up @@ -402,19 +483,6 @@ else()
set(UMF_POOL_SCALABLE_ENABLED FALSE)
endif()

if(UMF_BUILD_LIBUMF_POOL_JEMALLOC)
pkg_check_modules(JEMALLOC jemalloc)
if(NOT JEMALLOC_FOUND)
find_package(JEMALLOC REQUIRED jemalloc)
endif()
if(JEMALLOC_FOUND OR JEMALLOC_LIBRARIES)
set(UMF_POOL_JEMALLOC_ENABLED TRUE)
# add PATH to DLL on Windows
set(DLL_PATH_LIST
"${DLL_PATH_LIST};PATH=path_list_append:${JEMALLOC_DLL_DIRS}")
endif()
endif()

if(WINDOWS)
# TODO: enable the proxy library in the Debug build on Windows
#
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,25 @@ pool manager built as a separate static library: libjemalloc_pool.a on Linux and
jemalloc_pool.lib on Windows.
The `UMF_BUILD_LIBUMF_POOL_JEMALLOC` option has to be turned `ON` to build this library.

[jemalloc](https://github.com/jemalloc/jemalloc) is required to build the jemalloc pool.

In case of Linux OS jemalloc is built from the (fetched) sources with the following
non-default options enabled:
- `--with-jemalloc-prefix=je_` - adds the `je_` prefix to all public APIs,
- `--disable-cxx` - disables C++ integration, it will cause the `new` and the `delete`
operators implementations to be omitted.
- `--disable-initial-exec-tls` - disables the initial-exec TLS model for jemalloc's
internal thread-local storage (on those platforms that support
explicit settings), it can allow jemalloc to be dynamically
loaded after program startup (e.g. using `dlopen()`).

The default jemalloc package is required on Windows.

##### Requirements

1) The `UMF_BUILD_LIBUMF_POOL_JEMALLOC` option turned `ON`
2) Required packages:
- libjemalloc-dev (Linux) or jemalloc (Windows)
- jemalloc (Windows only)

#### Scalable Pool (part of libumf)

Expand Down
4 changes: 2 additions & 2 deletions benchmark/ubench.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ static void do_ipc_get_put_benchmark(alloc_t *allocs, size_t num_allocs,
}
}

int create_level_zero_params(ze_context_handle_t *context,
ze_device_handle_t *device) {
static int create_level_zero_params(ze_context_handle_t *context,
ze_device_handle_t *device) {
uint32_t driver_idx = 0;
ze_driver_handle_t driver = NULL;

Expand Down
7 changes: 5 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,11 @@ if(LINUX)
SRCS dram_and_fsdax/dram_and_fsdax.c
LIBS umf jemalloc_pool)

target_link_directories(${EXAMPLE_NAME} PRIVATE
${LIBHWLOC_LIBRARY_DIRS})
target_link_options(${EXAMPLE_NAME} PRIVATE "-Wl,--no-as-needed,-ldl")

target_link_directories(
${EXAMPLE_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS}
${JEMALLOC_LIBRARY_DIRS})

add_test(
NAME ${EXAMPLE_NAME}
Expand Down
6 changes: 4 additions & 2 deletions examples/cmake/FindJEMALLOC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

message(STATUS "Checking for module 'jemalloc' using find_library()")
message(
STATUS "Looking for the static 'libjemalloc.a' library using find_library()"
)

find_library(JEMALLOC_LIBRARY NAMES libjemalloc jemalloc)
find_library(JEMALLOC_LIBRARY NAMES libjemalloc.a jemalloc.a)
set(JEMALLOC_LIBRARIES ${JEMALLOC_LIBRARY})

get_filename_component(JEMALLOC_LIB_DIR ${JEMALLOC_LIBRARIES} DIRECTORY)
Expand Down
6 changes: 2 additions & 4 deletions examples/dram_and_fsdax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ if(NOT LIBHWLOC_FOUND)
find_package(LIBHWLOC 2.3.0 REQUIRED hwloc)
endif()

pkg_check_modules(JEMALLOC jemalloc)
if(NOT JEMALLOC_FOUND)
find_package(JEMALLOC REQUIRED jemalloc)
endif()
# find the custom jemalloc pointed by CMAKE_PREFIX_PATH
find_package(JEMALLOC REQUIRED jemalloc)

# build the example
set(EXAMPLE_NAME umf_example_dram_and_fsdax)
Expand Down
2 changes: 1 addition & 1 deletion scripts/qemu/run-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pwd

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

mkdir build
cd build
Expand Down
5 changes: 4 additions & 1 deletion src/pool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ if(UMF_BUILD_LIBUMF_POOL_JEMALLOC)
NAME jemalloc_pool
TYPE STATIC
SRCS pool_jemalloc.c ${POOL_EXTRA_SRCS}
LIBS jemalloc ${POOL_EXTRA_LIBS})
LIBS ${JEMALLOC_LIBRARIES} ${POOL_EXTRA_LIBS})
target_include_directories(jemalloc_pool PRIVATE ${JEMALLOC_INCLUDE_DIRS})
target_compile_definitions(jemalloc_pool
PRIVATE ${POOL_COMPILE_DEFINITIONS})
add_library(${PROJECT_NAME}::jemalloc_pool ALIAS jemalloc_pool)
if(NOT WINDOWS)
add_dependencies(jemalloc_pool jemalloc)
endif()
install(TARGETS jemalloc_pool EXPORT ${PROJECT_NAME}-targets)
endif()
10 changes: 0 additions & 10 deletions src/pool/pool_jemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@

#include <jemalloc/jemalloc.h>

// The Windows version of jemalloc uses API with je_ prefix,
// while the Linux one does not.
#ifndef _WIN32
#define je_mallocx mallocx
#define je_dallocx dallocx
#define je_rallocx rallocx
#define je_mallctl mallctl
#define je_malloc_usable_size malloc_usable_size
#endif

#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)

typedef struct jemalloc_memory_pool_t {
Expand Down
7 changes: 7 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,13 @@ if(LINUX
set(STANDALONE_CMAKE_OPTIONS
"-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
)
if(JEMALLOC_INCLUDE_DIRS)
# add custom jemalloc installation
set(STANDALONE_CMAKE_OPTIONS
"${STANDALONE_CMAKE_OPTIONS} -DCMAKE_PREFIX_PATH=${JEMALLOC_INCLUDE_DIRS}/../"
)
endif()

add_test(
NAME umf-standalone_examples
COMMAND
Expand Down
Loading

0 comments on commit 5298b5e

Please sign in to comment.