Skip to content

Commit

Permalink
chore: add mimalloc allocator (#364)
Browse files Browse the repository at this point in the history
* mimalloc depdence

* reslove comment

* check asan enabled or tsan enabled

* comment

* comment

* add static and shared opt

* comment

* change add to target

* reslove commit and put mimalloc link first

* add necessary annotation

* reslove comment

* reslove comment
  • Loading branch information
wtr0504 authored Nov 19, 2024
1 parent 32ba417 commit 9daef4b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ option(CRANE_ADDRESS_SANITIZER "Enable address sanitizer" OFF)
option(CRANE_THREAD_SANITIZER "Enable thread sanitizer" OFF)

option(CRANE_MIN_LOG_LEVEL "Set the minimal log level (INFO/DEBUG/TRACE)" OFF)

option(CRANE_USE_MIMALLOC "Override malloc using mimalloc" OFF)
# Options end here -------------------------------------------------------------------------------



set(CMAKE_CXX_STANDARD 20)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand Down Expand Up @@ -168,6 +172,10 @@ endif ()
# In CentOS 7, the following packages are required to enable sanitizers:
# 1. devtoolset-11-libasan-devel.x86_64
# 2. devtoolset-11-libtsan-devel.x86_64
if (CRANE_ADDRESS_SANITIZER AND CRANE_THREAD_SANITIZER)
message(FATAL_ERROR "CRANE_ADDRESS_SANITIZER and CRANE_THREAD_SANITIZER cannot be enabled at the same time.")
endif()

if (${CRANE_ADDRESS_SANITIZER})
message(STATUS "address_sanitizer is enabled")
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
Expand Down
4 changes: 3 additions & 1 deletion dependencies/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ if (${CRANE_FULL_DYNAMIC})
else ()
set(BUILD_SHARED_LIBS OFF)
endif ()

if(CRANE_USE_MIMALLOC)
add_subdirectory(mimalloc)
endif()
add_subdirectory(BSThreadPool)
add_subdirectory(nlohmann_json)
add_subdirectory(yaml-cpp)
Expand Down
31 changes: 31 additions & 0 deletions dependencies/cmake/mimalloc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
include(FetchContent)
FetchContent_Declare(
mimalloc
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
GIT_TAG v2.1.7
GIT_SHALLOW TRUE
)
# If MI_OVERRIDE is ON then operator new and delete will be override not only malloc and free
# ref: https://github.com/microsoft/mimalloc/issues/535
set(MI_OVERRIDE ON)
set(MI_BUILD_TESTS OFF)
if (BUILD_SHARED_LIBS)
set(MI_BUILD_SHARED ON)
else ()
set(MI_BUILD_SHARED OFF)
endif()

if(CRANE_ADDRESS_SANITIZER)
set(MI_TRACK_ASAN ON)
message(STATUS "Set ASAN enable in mimalloc")
endif()

FetchContent_MakeAvailable(mimalloc)

if (BUILD_SHARED_LIBS)
add_library(dev_mimalloc ALIAS mimalloc)
else ()
# When building with static library, make sure that link it as the first object file.
# ref: https://github.com/microsoft/mimalloc?tab=readme-ov-file#static-override
add_library(dev_mimalloc ALIAS mimalloc-static)
endif()
2 changes: 2 additions & 0 deletions src/CraneCtld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ add_executable(cranectld
target_precompile_headers(cranectld PRIVATE CtldPreCompiledHeader.h)

target_link_libraries(cranectld PRIVATE
$<$<BOOL:${CRANE_USE_MIMALLOC}>:dev_mimalloc>

spdlog::spdlog
concurrentqueue

Expand Down
3 changes: 3 additions & 0 deletions src/Craned/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ if(ENABLE_BPF)
add_dependencies(craned cgroup_dev_bpf_object)
target_compile_definitions(craned PRIVATE CRANE_ENABLE_BPF)
endif()

target_link_libraries(craned
$<$<BOOL:${CRANE_USE_MIMALLOC}>:dev_mimalloc>
concurrentqueue

${LIBCGROUP_BUILD_PRODUCTS}
Expand Down Expand Up @@ -58,6 +60,7 @@ target_link_libraries(craned
Backward::Interface

$<$<BOOL:${ENABLE_BPF}>:bpf>

)

# Linker flag for c++ 17 filesystem library
Expand Down
3 changes: 3 additions & 0 deletions src/Misc/Pam/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ set_target_properties(pam_crane PROPERTIES PREFIX "")

target_include_directories(pam_crane PUBLIC ${PAM_INCLUDE_DIR})



target_link_libraries(pam_crane PUBLIC
$<$<BOOL:${CRANE_USE_MIMALLOC}>:dev_mimalloc>
${PAM_LIBRARIES}
Utility_PublicHeaderNoLogger
crane_proto_lib
Expand Down

0 comments on commit 9daef4b

Please sign in to comment.