Skip to content

Commit

Permalink
Use mimalloc (netdata#19080)
Browse files Browse the repository at this point in the history
* Remove NEED_PROTOBUF

* Use mimalloc

* Disable on Windows and when cmake version < 3.16.

* Exclude mimalloc from all

* Fix cmake version check

* Check minor version to make Ubuntu 22.04 happy.

* Print used allocator in build info.
  • Loading branch information
vkalintiris authored Nov 27, 2024
1 parent 7ccd72d commit b0dd0f9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
53 changes: 37 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,37 @@ mark_as_advanced(BUILD_FOR_PACKAGING)
cmake_dependent_option(FORCE_LEGACY_LIBBPF "Force usage of libbpf 0.0.9 instead of the latest version." False "ENABLE_PLUGIN_EBPF" False)
mark_as_advanced(FORCE_LEGACY_LIBBPF)

set(NEED_PROTOBUF True)
cmake_dependent_option(ENABLE_MIMALLOC "Enable mimalloc allocator" ON
"CMAKE_MINOR_VERSION GREATER_EQUAL 18; CMAKE_SIZEOF_VOID_P EQUAL 8; NOT OS_FREEBSD; NOT OS_MACOS; NOT OS_WINDOWS; NOT ENABLE_ADDRESS_SANITIZER" OFF)

if(ENABLE_MIMALLOC)
function(netdata_add_mimalloc)
set(MI_BUILD_STATIC ON CACHE INTERNAL "")
set(MI_BUILD_SHARED OFF CACHE INTERNAL "")
set(MI_BUILD_OBJECT OFF CACHE INTERNAL "")
set(MI_BUILD_TESTS OFF CACHE INTERNAL "")

include(FetchContent)
include(NetdataFetchContentExtra)

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
FetchContent_Declare(mimalloc
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
GIT_TAG 8c532c32c3c96e5ba1f2283e032f69ead8add00f
EXCLUDE_FROM_ALL
)
else()
FetchContent_Declare(mimalloc
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
GIT_TAG 8c532c32c3c96e5ba1f2283e032f69ead8add00f
)
endif()

FetchContent_MakeAvailable_NoInstall(mimalloc)
endfunction()

netdata_add_mimalloc()
endif()

if(ENABLE_PLUGIN_GO)
include(NetdataGoTools)
Expand Down Expand Up @@ -245,12 +275,10 @@ if(ENABLE_WEBRTC)
FetchContent_MakeAvailable(libdatachannel)
endif()

if(NEED_PROTOBUF)
include(NetdataProtobuf)
include(NetdataProtobuf)

if(ENABLE_BUNDLED_PROTOBUF)
netdata_bundle_protobuf()
endif()
if(ENABLE_BUNDLED_PROTOBUF)
netdata_bundle_protobuf()
endif()

set(PKG_FILES_PATH "${CMAKE_SOURCE_DIR}/packaging/cmake/pkg-files")
Expand Down Expand Up @@ -590,13 +618,7 @@ else()
pkg_check_modules(CRYPTO IMPORTED_TARGET REQUIRED libcrypto)
endif()

#
# figure out if we need protoc/protobuf
#

if(NEED_PROTOBUF)
netdata_detect_protobuf()
endif()
netdata_detect_protobuf()

#
# source files
Expand Down Expand Up @@ -1822,6 +1844,7 @@ target_include_directories(libnetdata BEFORE PUBLIC ${CONFIG_H_DIR} ${CMAKE_SOUR
# target_link_libraries(test Threads::Threads)

target_link_libraries(libnetdata PUBLIC
"$<$<BOOL:${ENABLE_MIMALLOC}>:mimalloc-static>"
"$<$<NOT:$<BOOL:${HAVE_BUILTIN_ATOMICS}>>:atomic>"
"$<$<OR:$<BOOL:${OS_LINUX}>,$<BOOL:${OS_FREEBSD}>>:pthread;rt>"
"$<$<BOOL:${OS_WINDOWS}>:kernel32;advapi32;winmm;rpcrt4;wevtapi;ole32;oleaut32;wbemuuid>"
Expand Down Expand Up @@ -2553,9 +2576,7 @@ target_link_libraries(netdata PRIVATE
"$<$<BOOL:${CURL_FOUND}>:PkgConfig::CURL>"
)

if(NEED_PROTOBUF)
netdata_add_protobuf(netdata)
endif()
netdata_add_protobuf(netdata)

#
# build systemd-cat-native
Expand Down
1 change: 1 addition & 0 deletions packaging/cmake/config.cmake.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
#cmakedefine ENABLE_BUNDLED_JSONC
#cmakedefine ENABLE_BUNDLED_YAML
#cmakedefine ENABLE_BUNDLED_PROTOBUF
#cmakedefine ENABLE_MIMALLOC

// directory paths

Expand Down
18 changes: 18 additions & 0 deletions src/daemon/buildinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef enum __attribute__((packed)) {
BIB_FEATURE_CONTEXTS,
BIB_FEATURE_TIERING,
BIB_FEATURE_ML,
BIB_FEATURE_ALLOCATOR,
BIB_DB_DBENGINE,
BIB_DB_ALLOC,
BIB_DB_RAM,
Expand Down Expand Up @@ -530,6 +531,14 @@ static struct {
.json = "ml",
.value = NULL,
},
[BIB_FEATURE_ALLOCATOR] = {
.category = BIC_FEATURE,
.type = BIT_STRING,
.analytics = "allocator",
.print = "Memory Allocator",
.json = "allocator",
.value = NULL,
},
[BIB_DB_DBENGINE] = {
.category = BIC_DATABASE,
.type = BIT_BOOLEAN,
Expand Down Expand Up @@ -1096,6 +1105,15 @@ __attribute__((constructor)) void initialize_build_info(void) {
build_info_set_status(BIB_FEATURE_ML, true);
#endif

#if defined(ENABLE_MIMALLOC)
build_info_set_status(BIB_FEATURE_ALLOCATOR, true);
build_info_set_value(BIB_FEATURE_ALLOCATOR, "mimalloc");
#else
build_info_set_status(BIB_FEATURE_ALLOCATOR, true);
build_info_set_value(BIB_FEATURE_ALLOCATOR, "system");
#endif


#ifdef ENABLE_DBENGINE
build_info_set_status(BIB_DB_DBENGINE, true);
#ifdef ENABLE_ZSTD
Expand Down

0 comments on commit b0dd0f9

Please sign in to comment.