Use mimalloc (#19080)
* 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.
vkalintiris committed
Nov 27, 2024 at 19:53 UTC
b0dd0f93ea33cfe33038d30aa947de32a15c4056
3 files changed
+56
-16
CMakeLists.txt
+37
-16
@@ -208,7 +208,37 @@ mark_as_advanced(BUILD_FOR_PACKAGING)
208
cmake_dependent_option(FORCE_LEGACY_LIBBPF "Force usage of libbpf 0.0.9 instead of the latest version." False "ENABLE_PLUGIN_EBPF" False)
209
mark_as_advanced(FORCE_LEGACY_LIBBPF)
210
211
-set(NEED_PROTOBUF True)
211
+cmake_dependent_option(ENABLE_MIMALLOC "Enable mimalloc allocator" ON
212
+ "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)
213
+
214
+if(ENABLE_MIMALLOC)
215
+ function(netdata_add_mimalloc)
216
+ set(MI_BUILD_STATIC ON CACHE INTERNAL "")
217
+ set(MI_BUILD_SHARED OFF CACHE INTERNAL "")
218
+ set(MI_BUILD_OBJECT OFF CACHE INTERNAL "")
219
+ set(MI_BUILD_TESTS OFF CACHE INTERNAL "")
220
+
221
+ include(FetchContent)
222
+ include(NetdataFetchContentExtra)
223
+
224
+ if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
225
+ FetchContent_Declare(mimalloc
226
+ GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
227
+ GIT_TAG 8c532c32c3c96e5ba1f2283e032f69ead8add00f
228
+ EXCLUDE_FROM_ALL
229
+ )
230
+ else()
231
+ FetchContent_Declare(mimalloc
232
+ GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
233
+ GIT_TAG 8c532c32c3c96e5ba1f2283e032f69ead8add00f
234
+ )
235
+ endif()
236
+
237
+ FetchContent_MakeAvailable_NoInstall(mimalloc)
238
+ endfunction()
239
+
240
+ netdata_add_mimalloc()
241
+endif()
242
243
if(ENABLE_PLUGIN_GO)
244
include(NetdataGoTools)
@@ -245,12 +275,10 @@ if(ENABLE_WEBRTC)
275
FetchContent_MakeAvailable(libdatachannel)
276
endif()
277
248
-if(NEED_PROTOBUF)
249
- include(NetdataProtobuf)
278
+include(NetdataProtobuf)
279
251
- if(ENABLE_BUNDLED_PROTOBUF)
252
- netdata_bundle_protobuf()
253
- endif()
280
+if(ENABLE_BUNDLED_PROTOBUF)
281
+ netdata_bundle_protobuf()
282
endif()
283
284
set(PKG_FILES_PATH "${CMAKE_SOURCE_DIR}/packaging/cmake/pkg-files")
@@ -590,13 +618,7 @@ else()
618
pkg_check_modules(CRYPTO IMPORTED_TARGET REQUIRED libcrypto)
619
endif()
620
593
-#
594
-# figure out if we need protoc/protobuf
595
-#
596
-
597
-if(NEED_PROTOBUF)
598
- netdata_detect_protobuf()
599
-endif()
621
+netdata_detect_protobuf()
622
623
#
624
# source files
@@ -1822,6 +1844,7 @@ target_include_directories(libnetdata BEFORE PUBLIC ${CONFIG_H_DIR} ${CMAKE_SOUR
1844
# target_link_libraries(test Threads::Threads)
1845
1846
target_link_libraries(libnetdata PUBLIC
1847
+ "$<$<BOOL:${ENABLE_MIMALLOC}>:mimalloc-static>"
1848
"$<$<NOT:$<BOOL:${HAVE_BUILTIN_ATOMICS}>>:atomic>"
1849
"$<$<OR:$<BOOL:${OS_LINUX}>,$<BOOL:${OS_FREEBSD}>>:pthread;rt>"
1850
"$<$<BOOL:${OS_WINDOWS}>:kernel32;advapi32;winmm;rpcrt4;wevtapi;ole32;oleaut32;wbemuuid>"
@@ -2553,9 +2576,7 @@ target_link_libraries(netdata PRIVATE
2576
"$<$<BOOL:${CURL_FOUND}>:PkgConfig::CURL>"
2577
)
2578
2556
-if(NEED_PROTOBUF)
2557
- netdata_add_protobuf(netdata)
2558
-endif()
2579
+netdata_add_protobuf(netdata)
2580
2581
#
2582
# build systemd-cat-native
packaging/cmake/config.cmake.h.in
+1
@@ -145,6 +145,7 @@
145
#cmakedefine ENABLE_BUNDLED_JSONC
146
#cmakedefine ENABLE_BUNDLED_YAML
147
#cmakedefine ENABLE_BUNDLED_PROTOBUF
148
+#cmakedefine ENABLE_MIMALLOC
149
150
// directory paths
151
src/daemon/buildinfo.c
+18
@@ -54,6 +54,7 @@ typedef enum __attribute__((packed)) {
54
BIB_FEATURE_CONTEXTS,
55
BIB_FEATURE_TIERING,
56
BIB_FEATURE_ML,
57
+ BIB_FEATURE_ALLOCATOR,
58
BIB_DB_DBENGINE,
59
BIB_DB_ALLOC,
60
BIB_DB_RAM,
@@ -530,6 +531,14 @@ static struct {
531
.json = "ml",
532
.value = NULL,
533
},
534
+ [BIB_FEATURE_ALLOCATOR] = {
535
+ .category = BIC_FEATURE,
536
+ .type = BIT_STRING,
537
+ .analytics = "allocator",
538
+ .print = "Memory Allocator",
539
+ .json = "allocator",
540
+ .value = NULL,
541
+ },
542
[BIB_DB_DBENGINE] = {
543
.category = BIC_DATABASE,
544
.type = BIT_BOOLEAN,
@@ -1096,6 +1105,15 @@ __attribute__((constructor)) void initialize_build_info(void) {
1105
build_info_set_status(BIB_FEATURE_ML, true);
1106
#endif
1107
1108
+#if defined(ENABLE_MIMALLOC)
1109
+ build_info_set_status(BIB_FEATURE_ALLOCATOR, true);
1110
+ build_info_set_value(BIB_FEATURE_ALLOCATOR, "mimalloc");
1111
+#else
1112
+ build_info_set_status(BIB_FEATURE_ALLOCATOR, true);
1113
+ build_info_set_value(BIB_FEATURE_ALLOCATOR, "system");
1114
+#endif
1115
+
1116
+
1117
#ifdef ENABLE_DBENGINE
1118
build_info_set_status(BIB_DB_DBENGINE, true);
1119
#ifdef ENABLE_ZSTD