Skip to content

Commit

Permalink
Introduce Modern Allocator: Mimalloc (#2037)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Jun 4, 2021
1 parent 890c955 commit dc4d437
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@
[submodule "contrib/jemalloc"]
path = contrib/jemalloc
url = https://github.com/jemalloc/jemalloc.git
[submodule "contrib/mimalloc"]
path = contrib/mimalloc
url = https://github.com/microsoft/mimalloc
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ find_contrib_lib(double-conversion)
# Need to process before "contrib" dir:
include (libs/libcommon/cmake/find_gperftools.cmake)
include (libs/libcommon/cmake/find_jemalloc.cmake)
include (libs/libcommon/cmake/find_mimalloc.cmake)
include (libs/libcommon/cmake/find_cctz.cmake)
if (ENABLE_MYSQL_STORAGE)
include (libs/libmysqlxx/cmake/find_mysqlclient.cmake)
Expand Down
8 changes: 8 additions & 0 deletions contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ if (ENABLE_JEMALLOC AND USE_INTERNAL_JEMALLOC_LIBRARY)
add_subdirectory (jemalloc-cmake)
endif ()

if (ENABLE_MIMALLOC)
# we use object library on default
set(MI_BUILD_SHARED OFF CACHE BOOL "disable mimalloc shared library on default" FORCE)
set(MI_BUILD_TESTS OFF CACHE BOOL "disable mimalloc tests on default" FORCE)
set(MI_BUILD_STATIC OFF CACHE BOOL "disable mimalloc static library on default" FORCE)
add_subdirectory (mimalloc)
endif()

if (NOT ARCH_ARM)
add_subdirectory (libcpuid)
endif ()
Expand Down
1 change: 1 addition & 0 deletions contrib/mimalloc
Submodule mimalloc added at dc6bce
1 change: 1 addition & 0 deletions dbms/src/Common/config_build.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const char * auto_config_build[]
"USE_GLIBC_COMPATIBILITY", "@GLIBC_COMPATIBILITY@",
"USE_JEMALLOC", "@USE_JEMALLOC@",
"USE_TCMALLOC", "@USE_TCMALLOC@",
"USE_MIMALLOC", "@USE_MIMALLOC@",
"USE_UNWIND", "@USE_UNWIND@",
"USE_ICU", "@USE_ICU@",
"USE_RE2_ST", "@USE_RE2_ST@",
Expand Down
33 changes: 31 additions & 2 deletions dbms/src/Interpreters/AsynchronousMetrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <Interpreters/AsynchronousMetrics.h>
#include <Storages/MarkCache.h>
#include <Storages/StorageMergeTree.h>
#include <chrono>

#include <common/config_common.h>

#include <chrono>

#if USE_TCMALLOC
#include <gperftools/malloc_extension.h>

Expand All @@ -26,6 +26,10 @@ struct MallocExtensionInitializer
#include <jemalloc/jemalloc.h>
#endif

#if USE_MIMALLOC
#include <mimalloc.h>
#endif

namespace DB
{

Expand Down Expand Up @@ -197,6 +201,31 @@ void AsynchronousMetrics::update()
}
#endif

#if USE_MIMALLOC
#define MI_STATS_SET(X) set("mimalloc." #X, X)

{
size_t elapsed_msecs;
size_t user_msecs;
size_t system_msecs;
size_t current_rss;
size_t peak_rss;
size_t current_commit;
size_t peak_commit;
size_t page_faults;
mi_process_info(&elapsed_msecs, &user_msecs, &system_msecs, &current_rss, &peak_rss, &current_commit, &peak_commit, &page_faults);
MI_STATS_SET(elapsed_msecs);
MI_STATS_SET(user_msecs);
MI_STATS_SET(system_msecs);
MI_STATS_SET(current_rss);
MI_STATS_SET(peak_rss);
MI_STATS_SET(current_commit);
MI_STATS_SET(peak_commit);
MI_STATS_SET(page_faults);
};
#undef MI_STATS_SET
#endif

#if USE_JEMALLOC
{
#define FOR_EACH_METRIC(M) \
Expand Down
74 changes: 74 additions & 0 deletions dbms/src/Server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,62 @@
#include <jemalloc/jemalloc.h>
#endif

#if USE_MIMALLOC
#include <Poco/JSON/Parser.h>
#include <mimalloc.h>

#include <fstream>
#endif

#ifndef NDEBUG
#ifdef FIU_ENABLE
#include <fiu.h>
#endif
#endif


#if USE_MIMALLOC
#define TRY_LOAD_CONF(NAME) \
{ \
try \
{ \
auto value = obj->getValue<long>(#NAME); \
mi_option_set(NAME, value); \
} \
catch (...) \
{ \
} \
}

void loadMiConfig(Logger * log)
{
auto config = getenv("MIMALLOC_CONF");
if (config)
{
LOG_INFO(log, "Got environment variable MIMALLOC_CONF: " << config);
Poco::JSON::Parser parser;
std::ifstream data{config};
Poco::Dynamic::Var result = parser.parse(data);
auto obj = result.extract<Poco::JSON::Object::Ptr>();
TRY_LOAD_CONF(mi_option_show_errors);
TRY_LOAD_CONF(mi_option_show_stats);
TRY_LOAD_CONF(mi_option_verbose);
TRY_LOAD_CONF(mi_option_eager_commit);
TRY_LOAD_CONF(mi_option_eager_region_commit);
TRY_LOAD_CONF(mi_option_large_os_pages);
TRY_LOAD_CONF(mi_option_reserve_huge_os_pages);
TRY_LOAD_CONF(mi_option_segment_cache);
TRY_LOAD_CONF(mi_option_page_reset);
TRY_LOAD_CONF(mi_option_segment_reset);
TRY_LOAD_CONF(mi_option_reset_delay);
TRY_LOAD_CONF(mi_option_use_numa_nodes);
TRY_LOAD_CONF(mi_option_reset_decommits);
TRY_LOAD_CONF(mi_option_eager_commit_delay);
TRY_LOAD_CONF(mi_option_os_tag);
}
}
#undef TRY_LOAD_CONF
#endif
namespace CurrentMetrics
{
extern const Metric Revision;
Expand Down Expand Up @@ -293,6 +343,30 @@ void UpdateMallocConfig([[maybe_unused]] Logger * log)
LOG_INFO(log, "Set jemalloc.background_thread " << new_b);
}
#endif

#if USE_MIMALLOC
#define MI_OPTION_SHOW(OPTION) LOG_INFO(log, "mimalloc." #OPTION ": " << mi_option_get(OPTION));

int version = mi_version();
LOG_INFO(log, "Got mimalloc version: " << (version / 100) << "." << ((version % 100) / 10) << "." << (version % 10));
loadMiConfig(log);
MI_OPTION_SHOW(mi_option_show_errors);
MI_OPTION_SHOW(mi_option_show_stats);
MI_OPTION_SHOW(mi_option_verbose);
MI_OPTION_SHOW(mi_option_eager_commit);
MI_OPTION_SHOW(mi_option_eager_region_commit);
MI_OPTION_SHOW(mi_option_large_os_pages);
MI_OPTION_SHOW(mi_option_reserve_huge_os_pages);
MI_OPTION_SHOW(mi_option_segment_cache);
MI_OPTION_SHOW(mi_option_page_reset);
MI_OPTION_SHOW(mi_option_segment_reset);
MI_OPTION_SHOW(mi_option_reset_delay);
MI_OPTION_SHOW(mi_option_use_numa_nodes);
MI_OPTION_SHOW(mi_option_reset_decommits);
MI_OPTION_SHOW(mi_option_eager_commit_delay);
MI_OPTION_SHOW(mi_option_os_tag);
#undef MI_OPTION_SHOW
#endif
#undef RUN_FAIL_RETURN
}

Expand Down
3 changes: 3 additions & 0 deletions libs/libcommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ elseif (USE_TCMALLOC)
message (STATUS "Link libtcmalloc_minimal: ${GPERFTOOLS_TCMALLOC_MINIMAL}")
set (MALLOC_LIBRARIES ${GPERFTOOLS_TCMALLOC_MINIMAL})
endif ()
elseif(USE_MIMALLOC)
message (STATUS "Link mimalloc: ${MIMALLOC_LIBRARIES}")
set (MALLOC_LIBRARIES ${MIMALLOC_LIBRARIES})
else ()
message (WARNING "Non default allocator is disabled. This is not recommended for production Linux builds.")
endif ()
Expand Down
12 changes: 12 additions & 0 deletions libs/libcommon/cmake/find_mimalloc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
option (ENABLE_MIMALLOC "Set to ON to use mimalloc" OFF)

if (ENABLE_MIMALLOC)
set (MIMALLOC_LIBRARIES "mimalloc-obj")
set (USE_MIMALLOC 1)
message (STATUS "Using mimalloc=${USE_MIMALLOC}: ${MIMALLOC_LIBRARIES}")

if (ENABLE_JEMALLOC OR ENABLE_TCMALLOC)
message(FATAL_ERROR "multiple global allocator detected!")
endif()
endif ()

1 change: 1 addition & 0 deletions libs/libcommon/include/common/config_common.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#cmakedefine01 APPLE_SIERRA_OR_NEWER
#cmakedefine01 USE_TCMALLOC
#cmakedefine01 USE_JEMALLOC
#cmakedefine01 USE_MIMALLOC
#cmakedefine01 USE_READLINE
#cmakedefine01 USE_LIBEDIT
#cmakedefine01 HAVE_READLINE_HISTORY
20 changes: 18 additions & 2 deletions metrics/grafana/tiflash_summary.json
Original file line number Diff line number Diff line change
Expand Up @@ -553,20 +553,36 @@
"legendFormat": "metadata",
"refId": "G"
},
{
"expr": "sum(tiflash_system_asynchronous_metric_mimalloc_current_rss{tidb_cluster=\"$tidb_cluster\", job=\"tiflash\"})",
"format": "time_series",
"hide": true,
"intervalFactor": 1,
"legendFormat": "mimalloc_rss",
"refId": "H"
},
{
"expr": "sum(tiflash_system_asynchronous_metric_mimalloc_current_commit{tidb_cluster=\"$tidb_cluster\", job=\"tiflash\"})",
"format": "time_series",
"hide": true,
"intervalFactor": 1,
"legendFormat": "mimalloc_commit",
"refId": "I"
},
{
"expr": "sum(tiflash_system_asynchronous_metric_mmap_alive{tidb_cluster=\"$tidb_cluster\", job=\"tiflash\"})",
"format": "time_series",
"hide": true,
"intervalFactor": 1,
"legendFormat": "mmap",
"refId": "H"
"refId": "J"
},
{
"expr": "tiflash_proxy_process_resident_memory_bytes{tidb_cluster=\"$tidb_cluster\", job=\"tiflash\"}",
"format": "time_series",
"intervalFactor": 1,
"legendFormat": "{{instance}}",
"refId": "I"
"refId": "K"
}
],
"thresholds": [],
Expand Down

0 comments on commit dc4d437

Please sign in to comment.