diff --git a/CMakeLists.txt b/CMakeLists.txt index f396fa90c..b8838d425 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,23 @@ set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "${CMAKE_BINARY_DIR}/generators/") # Export compile commands for configuring language servers or debugging the build set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +# --------------------------------------------------------------------------- +# Allocator +# --------------------------------------------------------------------------- + +option(USE_MIMALLOC "Use mimalloc allocator" ON) + +# Only use mimalloc in Release mode, because of conflicts with ASAN +if(USE_MIMALLOC AND ${CMAKE_BUILD_TYPE} STREQUAL "Release") + message("Using mimalloc as allocator") + find_package(mimalloc REQUIRED) + add_compile_definitions(SILO_USE_MIMALLOC=1) + set(MIMALLOC_LIB mimalloc-static) +else() + set(MIMALLOC_LIB "") +endif() + + # --------------------------------------------------------------------------- # Compile definitions # --------------------------------------------------------------------------- @@ -113,6 +130,7 @@ add_library(silolib OBJECT ${SRC_SILO_WITHOUT_MAIN}) target_link_libraries( silolib PUBLIC + ${MIMALLOC_LIB} # this should be first Arrow::arrow_static ArrowAcero::arrow_acero_static ${Boost_LIBRARIES} diff --git a/conanfile.py b/conanfile.py index 1ead4a349..deaff17fe 100644 --- a/conanfile.py +++ b/conanfile.py @@ -8,9 +8,10 @@ class SiloRecipe(ConanFile): requires = [ "arrow/22.0.0", "boost/1.85.0", - "poco/1.13.3", - "nlohmann_json/3.12.0", "gtest/1.16.0", + "mimalloc/2.2.4", + "nlohmann_json/3.12.0", + "poco/1.13.3", "re2/20240702", "roaring/4.2.1", "simdjson/3.12.3", @@ -22,6 +23,7 @@ class SiloRecipe(ConanFile): default_options = { "abseil/*:shared": False, + "arrow/*:with_mimalloc": False, "arrow/*:compute": True, "arrow/*:acero": True, @@ -65,6 +67,9 @@ class SiloRecipe(ConanFile): "hwloc/*:shared": False, + # this statically overrides the `malloc` symbol to use mimalloc + "mimalloc/*:override": True, + "poco/*:shared": False, "poco/*:enable_json": True, "poco/*:enable_net": True, @@ -104,6 +109,7 @@ def generate(self): deps.set_property("boost", "cmake_find_mode", "both") deps.set_property("gtest", "cmake_find_mode", "both") deps.set_property("hwloc", "cmake_find_mode", "both") + deps.set_property("mimalloc", "cmake_find_mode", "both") deps.set_property("nlohmann_json", "cmake_find_mode", "both") deps.set_property("pcre2", "cmake_find_mode", "both") deps.set_property("poco", "cmake_find_mode", "both") diff --git a/src/silo/api/memory_monitor.cpp b/src/silo/api/memory_monitor.cpp index d413d7ce6..b0afa336e 100644 --- a/src/silo/api/memory_monitor.cpp +++ b/src/silo/api/memory_monitor.cpp @@ -2,8 +2,6 @@ #if defined(__linux__) -#include - #include #include #include @@ -12,6 +10,8 @@ #include +#include "silo/common/allocator.h" + namespace { std::optional parseVmRSSLine(const std::string& line) { @@ -73,8 +73,7 @@ void MemoryMonitor::checkRssAndLimit(Poco::Timer& /*timer*/) { SPDLOG_INFO("Current memory consumption: {} KB", rss.value()); if (soft_memory_limit_in_kb.has_value() && rss.value() > soft_memory_limit_in_kb.value()) { - SPDLOG_INFO("Manually invoking malloc_trim() to give back memory to OS."); - malloc_trim(0); + silo::common::Allocator::trim(); } } } diff --git a/src/silo/common/allocator.h b/src/silo/common/allocator.h new file mode 100644 index 000000000..edbd0abd5 --- /dev/null +++ b/src/silo/common/allocator.h @@ -0,0 +1,29 @@ +#ifdef SILO_USE_MIMALLOC +#include +#elif defined(__linux__) +#include +#endif + +#include + +namespace silo::common { + +class Allocator { + public: +#ifdef SILO_USE_MIMALLOC + static void trim() { + SPDLOG_INFO("Manually invoking mi_collect(true) to give back memory to OS."); + mi_collect(true); + mi_collect(true); // This should be invoked twice + } +#elif defined(__linux__) + static void trim() { + SPDLOG_INFO("Manually invoking malloc_trim() to give back memory to OS."); + malloc_trim(0); + } +#else + static void trim() { SPDLOG_INFO("Allocator::trim() is not implemented for this platform."); } +#endif +}; + +} // namespace silo::common \ No newline at end of file