From 5f6ebb70fa655714f1c811d63e99051f4289ecb0 Mon Sep 17 00:00:00 2001 From: daanx Date: Fri, 28 Feb 2025 17:30:41 -0800 Subject: [PATCH 1/6] add mi_options_print and mi_arenas_print --- include/mimalloc.h | 12 +++++++----- src/arena.c | 5 +++++ src/options.c | 27 +++++++++++++++++++++------ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/include/mimalloc.h b/include/mimalloc.h index dbb6e7bf..136647e9 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -153,6 +153,7 @@ mi_decl_export void mi_stats_reset(void) mi_attr_noexcept; mi_decl_export void mi_stats_merge(void) mi_attr_noexcept; mi_decl_export void mi_stats_print(void* out) mi_attr_noexcept; // backward compatibility: `out` is ignored and should be NULL mi_decl_export void mi_stats_print_out(mi_output_fun* out, void* arg) mi_attr_noexcept; +mi_decl_export void mi_options_print(void) mi_attr_noexcept; mi_decl_export void mi_process_init(void) mi_attr_noexcept; mi_decl_export void mi_thread_init(void) mi_attr_noexcept; @@ -269,13 +270,14 @@ mi_decl_export bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_block mi_decl_nodiscard mi_decl_export bool mi_is_in_heap_region(const void* p) mi_attr_noexcept; mi_decl_nodiscard mi_decl_export bool mi_is_redirected(void) mi_attr_noexcept; -mi_decl_export int mi_reserve_huge_os_pages_interleave(size_t pages, size_t numa_nodes, size_t timeout_msecs) mi_attr_noexcept; -mi_decl_export int mi_reserve_huge_os_pages_at(size_t pages, int numa_node, size_t timeout_msecs) mi_attr_noexcept; +mi_decl_export int mi_reserve_huge_os_pages_interleave(size_t pages, size_t numa_nodes, size_t timeout_msecs) mi_attr_noexcept; +mi_decl_export int mi_reserve_huge_os_pages_at(size_t pages, int numa_node, size_t timeout_msecs) mi_attr_noexcept; -mi_decl_export int mi_reserve_os_memory(size_t size, bool commit, bool allow_large) mi_attr_noexcept; -mi_decl_export bool mi_manage_os_memory(void* start, size_t size, bool is_committed, bool is_large, bool is_zero, int numa_node) mi_attr_noexcept; +mi_decl_export int mi_reserve_os_memory(size_t size, bool commit, bool allow_large) mi_attr_noexcept; +mi_decl_export bool mi_manage_os_memory(void* start, size_t size, bool is_committed, bool is_large, bool is_zero, int numa_node) mi_attr_noexcept; -mi_decl_export void mi_debug_show_arenas(void) mi_attr_noexcept; +mi_decl_export void mi_debug_show_arenas(void) mi_attr_noexcept; +mi_decl_export void mi_arenas_print(void) mi_attr_noexcept; // Experimental: heaps associated with specific memory arena's typedef int mi_arena_id_t; diff --git a/src/arena.c b/src/arena.c index 8dde86e7..d88aeaa3 100644 --- a/src/arena.c +++ b/src/arena.c @@ -952,6 +952,11 @@ void mi_debug_show_arenas(void) mi_attr_noexcept { } +void mi_arenas_print(void) mi_attr_noexcept { + mi_debug_show_arenas(); +} + + /* ----------------------------------------------------------- Reserve a huge page arena. ----------------------------------------------------------- */ diff --git a/src/options.c b/src/options.c index 712e3900..69247367 100644 --- a/src/options.c +++ b/src/options.c @@ -175,12 +175,7 @@ void _mi_options_init(void) { mi_add_stderr_output(); // now it safe to use stderr for output for(int i = 0; i < _mi_option_last; i++ ) { mi_option_t option = (mi_option_t)i; - long l = mi_option_get(option); MI_UNUSED(l); // initialize - // if (option != mi_option_verbose) - { - mi_option_desc_t* desc = &options[option]; - _mi_verbose_message("option '%s': %ld %s\n", desc->name, desc->value, (mi_option_has_size_in_kib(option) ? "KiB" : "")); - } + mi_option_get(option); // initialize } mi_max_error_count = mi_option_get(mi_option_max_errors); mi_max_warning_count = mi_option_get(mi_option_max_warnings); @@ -191,6 +186,26 @@ void _mi_options_init(void) { _mi_warning_message("option 'allow_large_os_pages' is disabled to allow for guarded objects\n"); } } + #endif + mi_options_print(); +} + +void mi_options_print(void) mi_attr_noexcept +{ + // show version + const int vermajor = MI_MALLOC_VERSION/100; + const int verminor = (MI_MALLOC_VERSION%100)/10; + const int verpatch = (MI_MALLOC_VERSION%10); + _mi_verbose_message("v%i.%i.%i (built on %s, %s)\n", vermajor, verminor, verpatch, __DATE__, __TIME__); + + // show options + for (int i = 0; i < _mi_option_last; i++) { + mi_option_t option = (mi_option_t)i; + mi_option_get(option); + mi_option_desc_t* desc = &options[option]; + _mi_verbose_message("option '%s': %ld %s\n", desc->name, desc->value, (mi_option_has_size_in_kib(option) ? "KiB" : "")); + } + #if MI_GUARDED _mi_verbose_message("guarded build: %s\n", mi_option_get(mi_option_guarded_sample_rate) != 0 ? "enabled" : "disabled"); #endif } From 527cd05fecd9749cb9d12dd2d5a8060a137cb2c0 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sat, 1 Mar 2025 14:29:23 -0800 Subject: [PATCH 2/6] add git hash to compile defines --- CMakeLists.txt | 14 ++++++++++++++ src/init.c | 8 -------- src/options.c | 33 ++++++++++++++++++++++++++++----- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cb0dad9f..52647130 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,18 @@ else() set(mi_defines "") endif() +# pass git revision as a define +if(EXISTS "${CMAKE_SOURCE_DIR}/.git/index") + find_package(Git) + if(GIT_FOUND) + execute_process(COMMAND ${GIT_EXECUTABLE} "describe" OUTPUT_VARIABLE mi_git_describe RESULT_VARIABLE mi_git_res ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) + if(mi_git_res EQUAL "0") + list(APPEND mi_defines "MI_GIT_DESCRIBE=${mi_git_describe}") + list(APPEND CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/index") # add to dependencies so we rebuild if the git rev changes + endif() + endif() +endif() + # ----------------------------------------------------------------------------- # Convenience: set default build type and compiler depending on the build directory # ----------------------------------------------------------------------------- @@ -492,6 +504,7 @@ else() endif() endif() + # ----------------------------------------------------------------------------- # Install and output names # ----------------------------------------------------------------------------- @@ -522,6 +535,7 @@ if(MI_TRACK_ASAN) set(mi_libname "${mi_libname}-asan") endif() string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC) +list(APPEND mi_defines "MI_CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE_LC}") #todo: multi-config project needs $ ? if(NOT(CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel|none)$")) set(mi_libname "${mi_libname}-${CMAKE_BUILD_TYPE_LC}") #append build type (e.g. -debug) if not a release version endif() diff --git a/src/init.c b/src/init.c index 03201707..dd3666fb 100644 --- a/src/init.c +++ b/src/init.c @@ -625,14 +625,6 @@ void mi_process_init(void) mi_attr_noexcept { mi_detect_cpu_features(); _mi_os_init(); mi_heap_main_init(); - #if MI_DEBUG - _mi_verbose_message("debug level : %d\n", MI_DEBUG); - #endif - _mi_verbose_message("secure level: %d\n", MI_SECURE); - _mi_verbose_message("mem tracking: %s\n", MI_TRACK_TOOL); - #if MI_TSAN - _mi_verbose_message("thread santizer enabled\n"); - #endif mi_thread_init(); #if defined(_WIN32) diff --git a/src/options.c b/src/options.c index 69247367..33e2bbce 100644 --- a/src/options.c +++ b/src/options.c @@ -175,7 +175,7 @@ void _mi_options_init(void) { mi_add_stderr_output(); // now it safe to use stderr for output for(int i = 0; i < _mi_option_last; i++ ) { mi_option_t option = (mi_option_t)i; - mi_option_get(option); // initialize + long l = mi_option_get(option); MI_UNUSED(l); // initialize } mi_max_error_count = mi_option_get(mi_option_max_errors); mi_max_warning_count = mi_option_get(mi_option_max_warnings); @@ -190,24 +190,47 @@ void _mi_options_init(void) { mi_options_print(); } -void mi_options_print(void) mi_attr_noexcept +#define mi_stringifyx(str) #str // and stringify +#define mi_stringify(str) mi_stringifyx(str) // expand + +void mi_options_print(void) mi_attr_noexcept { // show version const int vermajor = MI_MALLOC_VERSION/100; const int verminor = (MI_MALLOC_VERSION%100)/10; const int verpatch = (MI_MALLOC_VERSION%10); - _mi_verbose_message("v%i.%i.%i (built on %s, %s)\n", vermajor, verminor, verpatch, __DATE__, __TIME__); + _mi_verbose_message("v%i.%i.%i%s%s (built on %s, %s)\n", vermajor, verminor, verpatch, + #if defined(MI_CMAKE_BUILD_TYPE) + ", " mi_stringify(MI_CMAKE_BUILD_TYPE) + #else + "" + #endif + , + #if defined(MI_GIT_DESCRIBE) + ", git " mi_stringify(MI_GIT_DESCRIBE) + #else + "" + #endif + , __DATE__, __TIME__); // show options for (int i = 0; i < _mi_option_last; i++) { mi_option_t option = (mi_option_t)i; - mi_option_get(option); + long l = mi_option_get(option); MI_UNUSED(l); // possibly initialize mi_option_desc_t* desc = &options[option]; _mi_verbose_message("option '%s': %ld %s\n", desc->name, desc->value, (mi_option_has_size_in_kib(option) ? "KiB" : "")); - } + } + + // show build configuration + _mi_verbose_message("debug level : %d\n", MI_DEBUG ); + _mi_verbose_message("secure level: %d\n", MI_SECURE ); + _mi_verbose_message("mem tracking: %s\n", MI_TRACK_TOOL); #if MI_GUARDED _mi_verbose_message("guarded build: %s\n", mi_option_get(mi_option_guarded_sample_rate) != 0 ? "enabled" : "disabled"); #endif + #if MI_TSAN + _mi_verbose_message("thread santizer enabled\n"); + #endif } long _mi_option_get_fast(mi_option_t option) { From c2ae1c3539067d6e1d9d683fb0c93abec19c18a1 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sat, 1 Mar 2025 14:31:24 -0800 Subject: [PATCH 3/6] fix git rev dependency in cmake configure --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 52647130..2cf10569 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,7 +85,7 @@ if(EXISTS "${CMAKE_SOURCE_DIR}/.git/index") execute_process(COMMAND ${GIT_EXECUTABLE} "describe" OUTPUT_VARIABLE mi_git_describe RESULT_VARIABLE mi_git_res ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) if(mi_git_res EQUAL "0") list(APPEND mi_defines "MI_GIT_DESCRIBE=${mi_git_describe}") - list(APPEND CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/index") # add to dependencies so we rebuild if the git rev changes + set_property(GLOBAL APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/index") # add to dependencies so we rebuild if the git rev changes endif() endif() endif() From b0b9d9c22bf0070083933c4a9217309be9e5577c Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sat, 1 Mar 2025 14:51:27 -0800 Subject: [PATCH 4/6] add note that git rev dependency doesnt work --- CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2cf10569..900241b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,8 +84,11 @@ if(EXISTS "${CMAKE_SOURCE_DIR}/.git/index") if(GIT_FOUND) execute_process(COMMAND ${GIT_EXECUTABLE} "describe" OUTPUT_VARIABLE mi_git_describe RESULT_VARIABLE mi_git_res ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) if(mi_git_res EQUAL "0") + message(STATUS, "git describe: ${mi_git_describe}") list(APPEND mi_defines "MI_GIT_DESCRIBE=${mi_git_describe}") - set_property(GLOBAL APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/index") # add to dependencies so we rebuild if the git rev changes + # add to dependencies so we rebuild if the git head commit changes + # todo: this doesn't seem to work? + set_property(GLOBAL APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/index") endif() endif() endif() From 113800d1f052f3c0fa09b38d5e435730ed292fc3 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sat, 1 Mar 2025 14:52:37 -0800 Subject: [PATCH 5/6] remove status message --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 900241b5..dbd400c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,10 +84,8 @@ if(EXISTS "${CMAKE_SOURCE_DIR}/.git/index") if(GIT_FOUND) execute_process(COMMAND ${GIT_EXECUTABLE} "describe" OUTPUT_VARIABLE mi_git_describe RESULT_VARIABLE mi_git_res ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) if(mi_git_res EQUAL "0") - message(STATUS, "git describe: ${mi_git_describe}") list(APPEND mi_defines "MI_GIT_DESCRIBE=${mi_git_describe}") # add to dependencies so we rebuild if the git head commit changes - # todo: this doesn't seem to work? set_property(GLOBAL APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/index") endif() endif() From bdaeb1d469f9ee42774b72138eca3f3cc380bebb Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sat, 1 Mar 2025 16:29:28 -0800 Subject: [PATCH 6/6] remove stat_adjust --- include/mimalloc/types.h | 7 ------- src/stats.c | 27 --------------------------- 2 files changed, 34 deletions(-) diff --git a/include/mimalloc/types.h b/include/mimalloc/types.h index c8c10a2a..fdadcb35 100644 --- a/include/mimalloc/types.h +++ b/include/mimalloc/types.h @@ -612,9 +612,6 @@ typedef struct mi_stats_s { // add to stat keeping track of the peak void _mi_stat_increase(mi_stat_count_t* stat, size_t amount); void _mi_stat_decrease(mi_stat_count_t* stat, size_t amount); -// adjust stat in special cases to compensate for double counting -void _mi_stat_adjust_increase(mi_stat_count_t* stat, size_t amount); -void _mi_stat_adjust_decrease(mi_stat_count_t* stat, size_t amount); // counters can just be increased void _mi_stat_counter_increase(mi_stat_counter_t* stat, size_t amount); @@ -622,14 +619,10 @@ void _mi_stat_counter_increase(mi_stat_counter_t* stat, size_t amount); #define mi_stat_increase(stat,amount) _mi_stat_increase( &(stat), amount) #define mi_stat_decrease(stat,amount) _mi_stat_decrease( &(stat), amount) #define mi_stat_counter_increase(stat,amount) _mi_stat_counter_increase( &(stat), amount) -#define mi_stat_adjust_increase(stat,amount) _mi_stat_adjust_increase( &(stat), amount) -#define mi_stat_adjust_decrease(stat,amount) _mi_stat_adjust_decrease( &(stat), amount) #else #define mi_stat_increase(stat,amount) ((void)0) #define mi_stat_decrease(stat,amount) ((void)0) #define mi_stat_counter_increase(stat,amount) ((void)0) -#define mi_stat_adjuct_increase(stat,amount) ((void)0) -#define mi_stat_adjust_decrease(stat,amount) ((void)0) #endif #define mi_heap_stat_counter_increase(heap,stat,amount) mi_stat_counter_increase( (heap)->tld->stats.stat, amount) diff --git a/src/stats.c b/src/stats.c index 8566e8f2..e652c65f 100644 --- a/src/stats.c +++ b/src/stats.c @@ -51,27 +51,6 @@ static void mi_stat_update(mi_stat_count_t* stat, int64_t amount) { } } -// Adjust stats to compensate; for example before committing a range, -// first adjust downwards with parts that were already committed so -// we avoid double counting. -static void mi_stat_adjust(mi_stat_count_t* stat, int64_t amount) { - if (amount == 0) return; - if mi_unlikely(mi_is_in_main(stat)) - { - // adjust atomically - mi_atomic_addi64_relaxed(&stat->current, amount); - mi_atomic_addi64_relaxed(&stat->allocated, amount); - mi_atomic_addi64_relaxed(&stat->freed, amount); - } - else { - // don't affect the peak - stat->current += amount; - // add to both - stat->allocated += amount; - stat->freed += amount; - } -} - void _mi_stat_counter_increase(mi_stat_counter_t* stat, size_t amount) { if (mi_is_in_main(stat)) { mi_atomic_addi64_relaxed( &stat->count, 1 ); @@ -91,13 +70,7 @@ void _mi_stat_decrease(mi_stat_count_t* stat, size_t amount) { mi_stat_update(stat, -((int64_t)amount)); } -void _mi_stat_adjust_increase(mi_stat_count_t* stat, size_t amount) { - mi_stat_adjust(stat, (int64_t)amount); -} -void _mi_stat_adjust_decrease(mi_stat_count_t* stat, size_t amount) { - mi_stat_adjust(stat, -((int64_t)amount)); -} // must be thread safe as it is called from stats_merge static void mi_stat_add(mi_stat_count_t* stat, const mi_stat_count_t* src, int64_t unit) {