From 18174400b238d21ac262ade28a7461f385c200be Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sat, 1 Mar 2025 18:04:34 -0800 Subject: [PATCH] fix stats_add condition --- src/stats.c | 23 +++++++++++------------ test/test-stress.c | 8 +++++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/stats.c b/src/stats.c index 1f3b928c..1edd89b1 100644 --- a/src/stats.c +++ b/src/stats.c @@ -38,10 +38,8 @@ static void mi_stat_update(mi_stat_count_t* stat, int64_t amount) { else { // add thread local stat->current += amount; - if (stat->current > stat->peak) stat->peak = stat->current; - if (amount > 0) { - stat->total += amount; - } + if (stat->current > stat->peak) { stat->peak = stat->current; } + if (amount > 0) { stat->total += amount; } } } @@ -67,16 +65,17 @@ void _mi_stat_decrease(mi_stat_count_t* stat, size_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) { if (stat==src) return; - if (src->total==0) return; - mi_atomic_addi64_relaxed( &stat->total, src->total); - mi_atomic_addi64_relaxed( &stat->current, src->current); + if (src->total!=0) { mi_atomic_addi64_relaxed(&stat->total, src->total); } + if (src->current!=0) { mi_atomic_addi64_relaxed(&stat->current, src->current); } // peak scores do really not work across threads ... we use conservative max - mi_atomic_maxi64_relaxed( &stat->peak, src->peak); // or: mi_atomic_addi64_relaxed( &stat->peak, src->peak); + if (src->peak > stat->peak) { + mi_atomic_maxi64_relaxed(&stat->peak, src->peak); // or: mi_atomic_addi64_relaxed( &stat->peak, src->peak); + } } static void mi_stat_counter_add(mi_stat_counter_t* stat, const mi_stat_counter_t* src) { if (stat==src) return; - mi_atomic_addi64_relaxed( &stat->total, src->total); + if (src->total!=0) { mi_atomic_addi64_relaxed(&stat->total, src->total); } } // must be thread safe as it is called from stats_merge @@ -113,9 +112,9 @@ static void mi_stats_add(mi_stats_t* stats, const mi_stats_t* src) { mi_stat_counter_add(&stats->guarded_alloc_count, &src->guarded_alloc_count); #if MI_STAT>1 for (size_t i = 0; i <= MI_BIN_HUGE; i++) { - if (src->normal_bins[i].total > 0) { - mi_stat_add(&stats->normal_bins[i], &src->normal_bins[i]); - } + // if (src->normal_bins[i].total != 0 && src->normal_bins[i].current != 0) { + mi_stat_add(&stats->normal_bins[i], &src->normal_bins[i]); + //} } #endif } diff --git a/test/test-stress.c b/test/test-stress.c index d23c7461..41f2162b 100644 --- a/test/test-stress.c +++ b/test/test-stress.c @@ -285,9 +285,6 @@ int main(int argc, char** argv) { #if !defined(NDEBUG) && !defined(USE_STD_MALLOC) mi_option_set(mi_option_arena_reserve, 32 * 1024 /* in kib = 32MiB */); #endif - #ifndef USE_STD_MALLOC - mi_stats_reset(); - #endif // > mimalloc-test-stress [THREADS] [SCALE] [ITER] if (argc >= 2) { @@ -309,6 +306,11 @@ int main(int argc, char** argv) { allow_large_objects = true; } printf("Using %d threads with a %d%% load-per-thread and %d iterations %s\n", THREADS, SCALE, ITER, (allow_large_objects ? "(allow large objects)" : "")); + + #if !defined(NDEBUG) && !defined(USE_STD_MALLOC) + mi_stats_reset(); + #endif + //mi_reserve_os_memory(1024*1024*1024ULL, false, true); //int res = mi_reserve_huge_os_pages(4,1); //printf("(reserve huge: %i\n)", res);