Skip to content

Commit a077311

Browse files
committed
improve tracking of malloc_requested count
1 parent d48bafe commit a077311

File tree

6 files changed

+48
-29
lines changed

6 files changed

+48
-29
lines changed

include/mimalloc/types.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,22 +625,25 @@ void _mi_assert_fail(const char* assertion, const char* fname, unsigned int line
625625
// add to stat keeping track of the peak
626626
void _mi_stat_increase(mi_stat_count_t* stat, size_t amount);
627627
void _mi_stat_decrease(mi_stat_count_t* stat, size_t amount);
628+
void _mi_stat_adjust_decrease(mi_stat_count_t* stat, size_t amount);
628629
// counters can just be increased
629630
void _mi_stat_counter_increase(mi_stat_counter_t* stat, size_t amount);
630631

631632
#if (MI_STAT)
632633
#define mi_stat_increase(stat,amount) _mi_stat_increase( &(stat), amount)
633634
#define mi_stat_decrease(stat,amount) _mi_stat_decrease( &(stat), amount)
635+
#define mi_stat_adjust_decrease(stat,amount) _mi_stat_adjust_decrease( &(stat), amount)
634636
#define mi_stat_counter_increase(stat,amount) _mi_stat_counter_increase( &(stat), amount)
635637
#else
636638
#define mi_stat_increase(stat,amount) ((void)0)
637639
#define mi_stat_decrease(stat,amount) ((void)0)
640+
#define mi_stat_adjust_decrease(stat,amount) ((void)0)
638641
#define mi_stat_counter_increase(stat,amount) ((void)0)
639642
#endif
640643

641644
#define mi_heap_stat_counter_increase(heap,stat,amount) mi_stat_counter_increase( (heap)->tld->stats.stat, amount)
642645
#define mi_heap_stat_increase(heap,stat,amount) mi_stat_increase( (heap)->tld->stats.stat, amount)
643646
#define mi_heap_stat_decrease(heap,stat,amount) mi_stat_decrease( (heap)->tld->stats.stat, amount)
644-
647+
#define mi_heap_stat_adjust_decrease(heap,stat,amount) mi_stat_adjust_decrease( (heap)->tld->stats.stat, amount)
645648

646649
#endif

src/alloc-aligned.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,11 @@ static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t
191191
const bool is_aligned = (((uintptr_t)page->free + offset) & align_mask)==0;
192192
if mi_likely(is_aligned)
193193
{
194+
void* p = (zero ? _mi_page_malloc_zeroed(heap,page,padsize) : _mi_page_malloc(heap,page,padsize)); // call specific page malloc for better codegen
194195
#if MI_STAT>1
196+
mi_heap_stat_adjust_decrease(heap, malloc_requested, padsize);
195197
mi_heap_stat_increase(heap, malloc_requested, size);
196198
#endif
197-
void* p = (zero ? _mi_page_malloc_zeroed(heap,page,padsize) : _mi_page_malloc(heap,page,padsize)); // call specific page malloc for better codegen
198199
mi_assert_internal(p != NULL);
199200
mi_assert_internal(((uintptr_t)p + offset) % alignment == 0);
200201
mi_track_malloc(p,size,zero);

src/alloc.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ terms of the MIT license. A copy of the license can be found in the file
3030
// Note: in release mode the (inlined) routine is about 7 instructions with a single test.
3131
extern inline void* _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_t size, bool zero) mi_attr_noexcept
3232
{
33+
mi_assert_internal(size >= MI_PADDING_SIZE);
3334
mi_assert_internal(page->block_size == 0 /* empty heap */ || mi_page_block_size(page) >= size);
3435

3536
// check the free list
@@ -88,6 +89,7 @@ extern inline void* _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_
8889
#if (MI_STAT>1)
8990
const size_t bin = _mi_bin(bsize);
9091
mi_heap_stat_increase(heap, malloc_bins[bin], 1);
92+
mi_heap_stat_increase(heap, malloc_requested, size - MI_PADDING_SIZE);
9193
#endif
9294
}
9395
#endif
@@ -146,12 +148,6 @@ static inline mi_decl_restrict void* mi_heap_malloc_small_zero(mi_heap_t* heap,
146148
void* const p = _mi_page_malloc_zero(heap, page, size + MI_PADDING_SIZE, zero);
147149
mi_track_malloc(p,size,zero);
148150

149-
#if MI_STAT>1
150-
if (p != NULL) {
151-
if (!mi_heap_is_initialized(heap)) { heap = mi_prim_get_default_heap(); }
152-
mi_heap_stat_increase(heap, malloc_requested, mi_usable_size(p));
153-
}
154-
#endif
155151
#if MI_DEBUG>3
156152
if (p != NULL && zero) {
157153
mi_assert_expensive(mi_mem_is_zero(p, size));
@@ -188,12 +184,6 @@ extern inline void* _mi_heap_malloc_zero_ex(mi_heap_t* heap, size_t size, bool z
188184
void* const p = _mi_malloc_generic(heap, size + MI_PADDING_SIZE, zero, huge_alignment); // note: size can overflow but it is detected in malloc_generic
189185
mi_track_malloc(p,size,zero);
190186

191-
#if MI_STAT>1
192-
if (p != NULL) {
193-
if (!mi_heap_is_initialized(heap)) { heap = mi_prim_get_default_heap(); }
194-
mi_heap_stat_increase(heap, malloc_requested, mi_usable_size(p));
195-
}
196-
#endif
197187
#if MI_DEBUG>3
198188
if (p != NULL && zero) {
199189
mi_assert_expensive(mi_mem_is_zero(p, size));
@@ -666,7 +656,8 @@ mi_decl_restrict void* _mi_heap_malloc_guarded(mi_heap_t* heap, size_t size, boo
666656
if (p != NULL) {
667657
if (!mi_heap_is_initialized(heap)) { heap = mi_prim_get_default_heap(); }
668658
#if MI_STAT>1
669-
mi_heap_stat_increase(heap, malloc_requested, mi_usable_size(p));
659+
mi_heap_stat_adjust_decrease(heap, malloc_requested, req_size);
660+
mi_heap_stat_increase(heap, malloc_requested, size);
670661
#endif
671662
_mi_stat_counter_increase(&heap->tld->stats.malloc_guarded_count, 1);
672663
}

src/free.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -514,20 +514,18 @@ static void mi_check_padding(const mi_page_t* page, const mi_block_t* block) {
514514
// only maintain stats for smaller objects if requested
515515
#if (MI_STAT>0)
516516
static void mi_stat_free(const mi_page_t* page, const mi_block_t* block) {
517-
#if (MI_STAT < 2)
518517
MI_UNUSED(block);
519-
#endif
520518
mi_heap_t* const heap = mi_heap_get_default();
521519
const size_t bsize = mi_page_usable_block_size(page);
522-
#if (MI_STAT>1)
523-
const size_t usize = mi_page_usable_size_of(page, block);
524-
mi_heap_stat_decrease(heap, malloc_requested, usize);
525-
#endif
520+
// #if (MI_STAT>1)
521+
// const size_t usize = mi_page_usable_size_of(page, block);
522+
// mi_heap_stat_decrease(heap, malloc_requested, usize);
523+
// #endif
526524
if (bsize <= MI_LARGE_OBJ_SIZE_MAX) {
527525
mi_heap_stat_decrease(heap, malloc_normal, bsize);
528-
#if (MI_STAT > 1)
526+
#if (MI_STAT > 1)
529527
mi_heap_stat_decrease(heap, malloc_bins[_mi_bin(bsize)], 1);
530-
#endif
528+
#endif
531529
}
532530
else {
533531
const size_t bpsize = mi_page_block_size(page); // match stat in page.c:mi_huge_page_alloc

src/heap.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,17 @@ static bool _mi_heap_page_destroy(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_
331331
if (bsize > MI_LARGE_OBJ_SIZE_MAX) {
332332
mi_heap_stat_decrease(heap, malloc_huge, bsize);
333333
}
334-
#if (MI_STAT)
334+
#if (MI_STAT>0)
335335
_mi_page_free_collect(page, false); // update used count
336336
const size_t inuse = page->used;
337337
if (bsize <= MI_LARGE_OBJ_SIZE_MAX) {
338338
mi_heap_stat_decrease(heap, malloc_normal, bsize * inuse);
339-
#if (MI_STAT>1)
339+
#if (MI_STAT>1)
340340
mi_heap_stat_decrease(heap, malloc_bins[_mi_bin(bsize)], inuse);
341-
#endif
341+
#endif
342342
}
343-
mi_heap_stat_decrease(heap, malloc_requested, bsize * inuse); // todo: off for aligned blocks...
344-
#endif
343+
// mi_heap_stat_decrease(heap, malloc_requested, bsize * inuse); // todo: off for aligned blocks...
344+
#endif
345345

346346
/// pretend it is all free now
347347
mi_assert_internal(mi_page_thread_free(page) == NULL);

src/stats.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ void _mi_stat_decrease(mi_stat_count_t* stat, size_t amount) {
6262
}
6363

6464

65+
static void mi_stat_adjust(mi_stat_count_t* stat, int64_t amount) {
66+
if (amount == 0) return;
67+
if mi_unlikely(mi_is_in_main(stat))
68+
{
69+
// adjust atomically
70+
mi_atomic_addi64_relaxed(&stat->current, amount);
71+
mi_atomic_addi64_relaxed(&stat->total,amount);
72+
}
73+
else {
74+
// adjust local
75+
stat->current += amount;
76+
stat->total += amount;
77+
}
78+
}
79+
80+
void _mi_stat_adjust_decrease(mi_stat_count_t* stat, size_t amount) {
81+
mi_stat_adjust(stat, -((int64_t)amount));
82+
}
83+
6584

6685
// must be thread safe as it is called from stats_merge
6786
static void mi_stat_count_add_mt(mi_stat_count_t* stat, const mi_stat_count_t* src) {
@@ -199,6 +218,13 @@ static void mi_stat_peak_print(const mi_stat_count_t* stat, const char* msg, int
199218
_mi_fprintf(out, arg, "\n");
200219
}
201220

221+
static void mi_stat_total_print(const mi_stat_count_t* stat, const char* msg, int64_t unit, mi_output_fun* out, void* arg) {
222+
_mi_fprintf(out, arg, "%10s:", msg);
223+
_mi_fprintf(out, arg, "%12s", " "); // no peak
224+
mi_print_amount(stat->total, unit, out, arg);
225+
_mi_fprintf(out, arg, "\n");
226+
}
227+
202228
static void mi_stat_counter_print(const mi_stat_counter_t* stat, const char* msg, mi_output_fun* out, void* arg ) {
203229
_mi_fprintf(out, arg, "%10s:", msg);
204230
mi_print_amount(stat->total, -1, out, arg);
@@ -295,7 +321,7 @@ static void _mi_stats_print(mi_stats_t* stats, mi_output_fun* out0, void* arg0)
295321
mi_stat_print_ex(&total, "total", 1, out, arg, "");
296322
#endif
297323
#if MI_STAT>1
298-
mi_stat_peak_print(&stats->malloc_requested, "malloc req", 1, out, arg);
324+
mi_stat_total_print(&stats->malloc_requested, "malloc req", 1, out, arg);
299325
_mi_fprintf(out, arg, "\n");
300326
#endif
301327
mi_stat_print_ex(&stats->reserved, "reserved", 1, out, arg, "");

0 commit comments

Comments
 (0)