Skip to content

Commit 1b749ea

Browse files
committed
clean up statistics
1 parent bc5f636 commit 1b749ea

File tree

10 files changed

+107
-88
lines changed

10 files changed

+107
-88
lines changed

include/mimalloc/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ bool _mi_page_is_valid(mi_page_t* page);
311311
#define MI_INIT64(x) MI_INIT32(x),MI_INIT32(x)
312312
#define MI_INIT128(x) MI_INIT64(x),MI_INIT64(x)
313313
#define MI_INIT256(x) MI_INIT128(x),MI_INIT128(x)
314-
314+
#define MI_INIT74(x) MI_INIT64(x),MI_INIT8(x),x(),x()
315315

316316
#include <string.h>
317317
// initialize a local variable to zero; use memset as compilers optimize constant sized memset's

include/mimalloc/types.h

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -573,37 +573,50 @@ typedef struct mi_stat_counter_s {
573573
} mi_stat_counter_t;
574574

575575
typedef struct mi_stats_s {
576-
mi_stat_count_t segments;
577-
mi_stat_count_t pages;
578-
mi_stat_count_t reserved;
579-
mi_stat_count_t committed;
580-
mi_stat_count_t reset;
581-
mi_stat_count_t purged;
582-
mi_stat_count_t page_committed;
583-
mi_stat_count_t segments_abandoned;
584-
mi_stat_count_t pages_abandoned;
585-
mi_stat_count_t threads;
586-
mi_stat_count_t normal;
587-
mi_stat_count_t huge;
588-
mi_stat_count_t giant;
589-
mi_stat_count_t malloc;
590-
mi_stat_count_t segments_cache;
591-
mi_stat_counter_t pages_extended;
576+
mi_stat_count_t pages; // count of mimalloc pages
577+
mi_stat_count_t reserved; // reserved memory bytes
578+
mi_stat_count_t committed; // committed bytes
579+
mi_stat_count_t reset; // reset bytes
580+
mi_stat_count_t purged; // purged bytes
581+
mi_stat_count_t page_committed; // committed memory inside pages
582+
mi_stat_count_t pages_abandoned; // abandonded pages count
583+
mi_stat_count_t threads; // number of threads
584+
mi_stat_count_t malloc_normal; // allocated bytes <= MI_LARGE_OBJ_SIZE_MAX
585+
mi_stat_count_t malloc_huge; // allocated bytes in huge pages
586+
mi_stat_count_t malloc_requested; // malloc requested bytes
587+
592588
mi_stat_counter_t mmap_calls;
593589
mi_stat_counter_t commit_calls;
594590
mi_stat_counter_t reset_calls;
595591
mi_stat_counter_t purge_calls;
596-
mi_stat_counter_t page_no_retire;
597-
mi_stat_counter_t searches;
598-
mi_stat_counter_t normal_count;
599-
mi_stat_counter_t huge_count;
600-
mi_stat_counter_t arena_count;
601-
mi_stat_counter_t arena_crossover_count;
592+
mi_stat_counter_t arena_count; // number of memory arena's
593+
mi_stat_counter_t malloc_normal_count; // number of blocks <= MI_LARGE_OBJ_SIZE_MAX
594+
mi_stat_counter_t malloc_huge_count; // number of huge bloks
595+
mi_stat_counter_t malloc_guarded_count; // number of allocations with guard pages
596+
597+
// internal statistics
602598
mi_stat_counter_t arena_rollback_count;
603-
mi_stat_counter_t guarded_alloc_count;
604-
#if MI_STAT>1
605-
mi_stat_count_t normal_bins[MI_BIN_HUGE+1];
606-
#endif
599+
mi_stat_counter_t pages_extended; // number of page extensions
600+
mi_stat_counter_t pages_retire; // number of pages that are retired
601+
mi_stat_counter_t page_searches; // searches for a fresh page
602+
// only on v1 and v2
603+
mi_stat_count_t segments;
604+
mi_stat_count_t segments_abandoned;
605+
mi_stat_count_t segments_cache;
606+
mi_stat_count_t _segments_reserved;
607+
// only on v3
608+
mi_stat_counter_t pages_reclaim_on_alloc;
609+
mi_stat_counter_t pages_reclaim_on_free;
610+
mi_stat_counter_t pages_reabandon_full;
611+
mi_stat_counter_t pages_unabandon_busy_wait;
612+
613+
// future extension
614+
mi_stat_count_t _stat_reserved[4];
615+
mi_stat_counter_t _stat_counter_reserved[4];
616+
617+
// size segregated statistics
618+
mi_stat_count_t malloc_bins[MI_BIN_HUGE+1]; // allocation per size bin
619+
mi_stat_count_t page_bins[MI_BIN_HUGE+1]; // pages allocated per size bin
607620
} mi_stats_t;
608621

609622

src/alloc-aligned.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t
192192
if mi_likely(is_aligned)
193193
{
194194
#if MI_STAT>1
195-
mi_heap_stat_increase(heap, malloc, size);
195+
mi_heap_stat_increase(heap, malloc_requested, size);
196196
#endif
197197
void* p = (zero ? _mi_page_malloc_zeroed(heap,page,padsize) : _mi_page_malloc(heap,page,padsize)); // call specific page malloc for better codegen
198198
mi_assert_internal(p != NULL);

src/alloc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ extern inline void* _mi_page_malloc_zero(mi_heap_t* heap, mi_page_t* page, size_
8383
#if (MI_STAT>0)
8484
const size_t bsize = mi_page_usable_block_size(page);
8585
if (bsize <= MI_LARGE_OBJ_SIZE_MAX) {
86-
mi_heap_stat_increase(heap, normal, bsize);
87-
mi_heap_stat_counter_increase(heap, normal_count, 1);
86+
mi_heap_stat_increase(heap, malloc_normal, bsize);
87+
mi_heap_stat_counter_increase(heap, malloc_normal_count, 1);
8888
#if (MI_STAT>1)
8989
const size_t bin = _mi_bin(bsize);
90-
mi_heap_stat_increase(heap, normal_bins[bin], 1);
90+
mi_heap_stat_increase(heap, malloc_bins[bin], 1);
9191
#endif
9292
}
9393
#endif
@@ -149,7 +149,7 @@ static inline mi_decl_restrict void* mi_heap_malloc_small_zero(mi_heap_t* heap,
149149
#if MI_STAT>1
150150
if (p != NULL) {
151151
if (!mi_heap_is_initialized(heap)) { heap = mi_prim_get_default_heap(); }
152-
mi_heap_stat_increase(heap, malloc, mi_usable_size(p));
152+
mi_heap_stat_increase(heap, malloc_requested, mi_usable_size(p));
153153
}
154154
#endif
155155
#if MI_DEBUG>3
@@ -191,7 +191,7 @@ extern inline void* _mi_heap_malloc_zero_ex(mi_heap_t* heap, size_t size, bool z
191191
#if MI_STAT>1
192192
if (p != NULL) {
193193
if (!mi_heap_is_initialized(heap)) { heap = mi_prim_get_default_heap(); }
194-
mi_heap_stat_increase(heap, malloc, mi_usable_size(p));
194+
mi_heap_stat_increase(heap, malloc_requested, mi_usable_size(p));
195195
}
196196
#endif
197197
#if MI_DEBUG>3

src/bitmap.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ static bool mi_bitmap_try_find_claim_field_across(mi_bitmap_t bitmap, size_t bit
242242
} while (!mi_atomic_cas_strong_acq_rel(field, &map, newmap));
243243

244244
// claimed!
245-
mi_stat_counter_increase(_mi_stats_main.arena_crossover_count,1);
246245
*bitmap_idx = mi_bitmap_index_create(idx, initial_idx);
247246
return true;
248247

src/free.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,17 +521,17 @@ static void mi_stat_free(const mi_page_t* page, const mi_block_t* block) {
521521
const size_t bsize = mi_page_usable_block_size(page);
522522
#if (MI_STAT>1)
523523
const size_t usize = mi_page_usable_size_of(page, block);
524-
mi_heap_stat_decrease(heap, malloc, usize);
524+
mi_heap_stat_decrease(heap, malloc_requested, usize);
525525
#endif
526526
if (bsize <= MI_LARGE_OBJ_SIZE_MAX) {
527-
mi_heap_stat_decrease(heap, normal, bsize);
527+
mi_heap_stat_decrease(heap, malloc_normal, bsize);
528528
#if (MI_STAT > 1)
529-
mi_heap_stat_decrease(heap, normal_bins[_mi_bin(bsize)], 1);
529+
mi_heap_stat_decrease(heap, malloc_bins[_mi_bin(bsize)], 1);
530530
#endif
531531
}
532532
else {
533533
const size_t bpsize = mi_page_block_size(page); // match stat in page.c:mi_huge_page_alloc
534-
mi_heap_stat_decrease(heap, huge, bpsize);
534+
mi_heap_stat_decrease(heap, malloc_huge, bpsize);
535535
}
536536
}
537537
#else

src/heap.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,18 @@ static bool _mi_heap_page_destroy(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_
324324
// stats
325325
const size_t bsize = mi_page_block_size(page);
326326
if (bsize > MI_LARGE_OBJ_SIZE_MAX) {
327-
mi_heap_stat_decrease(heap, huge, bsize);
327+
mi_heap_stat_decrease(heap, malloc_huge, bsize);
328328
}
329329
#if (MI_STAT)
330330
_mi_page_free_collect(page, false); // update used count
331331
const size_t inuse = page->used;
332332
if (bsize <= MI_LARGE_OBJ_SIZE_MAX) {
333-
mi_heap_stat_decrease(heap, normal, bsize * inuse);
333+
mi_heap_stat_decrease(heap, malloc_normal, bsize * inuse);
334334
#if (MI_STAT>1)
335-
mi_heap_stat_decrease(heap, normal_bins[_mi_bin(bsize)], inuse);
335+
mi_heap_stat_decrease(heap, malloc_bins[_mi_bin(bsize)], inuse);
336336
#endif
337337
}
338-
mi_heap_stat_decrease(heap, malloc, bsize * inuse); // todo: off for aligned blocks...
338+
mi_heap_stat_decrease(heap, malloc_requested, bsize * inuse); // todo: off for aligned blocks...
339339
#endif
340340

341341
/// pretend it is all free now

src/init.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,22 @@ const mi_page_t _mi_page_empty = {
6969
#define MI_STAT_COUNT_NULL() {0,0,0}
7070

7171
// Empty statistics
72-
#if MI_STAT>1
73-
#define MI_STAT_COUNT_END_NULL() , { MI_STAT_COUNT_NULL(), MI_INIT32(MI_STAT_COUNT_NULL) }
74-
#else
75-
#define MI_STAT_COUNT_END_NULL()
76-
#endif
77-
7872
#define MI_STATS_NULL \
79-
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
80-
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
81-
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
82-
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
83-
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
84-
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
85-
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
86-
MI_STAT_COUNT_NULL(), \
73+
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
74+
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
75+
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
76+
{ 0 }, { 0 }, { 0 }, { 0 }, \
8777
{ 0 }, { 0 }, { 0 }, { 0 }, \
78+
\
8879
{ 0 }, { 0 }, { 0 }, { 0 }, \
80+
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
8981
{ 0 }, { 0 }, { 0 }, { 0 }, \
90-
{ 0 } \
91-
MI_STAT_COUNT_END_NULL()
82+
\
83+
{ MI_INIT4(MI_STAT_COUNT_NULL) }, \
84+
{ { 0 }, { 0 }, { 0 }, { 0 } }, \
85+
\
86+
{ MI_INIT74(MI_STAT_COUNT_NULL) }, \
87+
{ MI_INIT74(MI_STAT_COUNT_NULL) }
9288

9389
// --------------------------------------------------------
9490
// Statically allocate an empty heap as the initial

src/page.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ void _mi_page_retire(mi_page_t* page) mi_attr_noexcept {
473473
const size_t bsize = mi_page_block_size(page);
474474
if mi_likely( /* bsize < MI_MAX_RETIRE_SIZE && */ !mi_page_queue_is_special(pq)) { // not full or huge queue?
475475
if (pq->last==page && pq->first==page) { // the only page in the queue?
476-
mi_stat_counter_increase(_mi_stats_main.page_no_retire,1);
476+
mi_stat_counter_increase(_mi_stats_main.pages_retire,1);
477477
page->retire_expire = (bsize <= MI_SMALL_OBJ_SIZE_MAX ? MI_RETIRE_CYCLES : MI_RETIRE_CYCLES/4);
478478
mi_heap_t* heap = mi_page_heap(page);
479479
mi_assert_internal(pq >= heap->pages);
@@ -809,7 +809,7 @@ static mi_page_t* mi_page_queue_find_free_ex(mi_heap_t* heap, mi_page_queue_t* p
809809
page = next;
810810
} // for each page
811811

812-
mi_heap_stat_counter_increase(heap, searches, count);
812+
mi_heap_stat_counter_increase(heap, page_searches, count);
813813

814814
// set the page to the best candidate
815815
if (page_candidate != NULL) {
@@ -922,8 +922,8 @@ static mi_page_t* mi_huge_page_alloc(mi_heap_t* heap, size_t size, size_t page_a
922922
mi_assert_internal(_mi_page_segment(page)->thread_id==0); // abandoned, not in the huge queue
923923
mi_page_set_heap(page, NULL);
924924
#endif
925-
mi_heap_stat_increase(heap, huge, mi_page_block_size(page));
926-
mi_heap_stat_counter_increase(heap, huge_count, 1);
925+
mi_heap_stat_increase(heap, malloc_huge, mi_page_block_size(page));
926+
mi_heap_stat_counter_increase(heap, malloc_huge_count, 1);
927927
}
928928
return page;
929929
}

src/stats.c

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,39 +81,50 @@ static void mi_stat_counter_add(mi_stat_counter_t* stat, const mi_stat_counter_t
8181
// must be thread safe as it is called from stats_merge
8282
static void mi_stats_add(mi_stats_t* stats, const mi_stats_t* src) {
8383
if (stats==src) return;
84-
mi_stat_add(&stats->segments, &src->segments);
8584
mi_stat_add(&stats->pages, &src->pages);
8685
mi_stat_add(&stats->reserved, &src->reserved);
8786
mi_stat_add(&stats->committed, &src->committed);
8887
mi_stat_add(&stats->reset, &src->reset);
8988
mi_stat_add(&stats->purged, &src->purged);
9089
mi_stat_add(&stats->page_committed, &src->page_committed);
91-
9290
mi_stat_add(&stats->pages_abandoned, &src->pages_abandoned);
93-
mi_stat_add(&stats->segments_abandoned, &src->segments_abandoned);
9491
mi_stat_add(&stats->threads, &src->threads);
92+
mi_stat_add(&stats->malloc_normal, &src->malloc_normal);
93+
mi_stat_add(&stats->malloc_huge, &src->malloc_huge);
94+
mi_stat_add(&stats->malloc_requested, &src->malloc_requested);
9595

96-
mi_stat_add(&stats->malloc, &src->malloc);
97-
mi_stat_add(&stats->segments_cache, &src->segments_cache);
98-
mi_stat_add(&stats->normal, &src->normal);
99-
mi_stat_add(&stats->huge, &src->huge);
100-
mi_stat_add(&stats->giant, &src->giant);
101-
102-
mi_stat_counter_add(&stats->pages_extended, &src->pages_extended);
10396
mi_stat_counter_add(&stats->mmap_calls, &src->mmap_calls);
10497
mi_stat_counter_add(&stats->commit_calls, &src->commit_calls);
10598
mi_stat_counter_add(&stats->reset_calls, &src->reset_calls);
10699
mi_stat_counter_add(&stats->purge_calls, &src->purge_calls);
100+
mi_stat_counter_add(&stats->arena_count, &src->arena_count);
101+
mi_stat_counter_add(&stats->malloc_normal_count, &src->malloc_normal_count);
102+
mi_stat_counter_add(&stats->malloc_huge_count, &src->malloc_huge_count);
103+
mi_stat_counter_add(&stats->malloc_guarded_count, &src->malloc_guarded_count);
104+
105+
mi_stat_counter_add(&stats->arena_rollback_count, &src->arena_rollback_count);
106+
mi_stat_counter_add(&stats->pages_extended, &src->pages_extended);
107+
mi_stat_counter_add(&stats->pages_retire, &src->pages_retire);
108+
mi_stat_counter_add(&stats->page_searches, &src->page_searches);
109+
110+
mi_stat_add(&stats->segments, &src->segments);
111+
mi_stat_add(&stats->segments_abandoned, &src->segments_abandoned);
112+
mi_stat_add(&stats->segments_cache, &src->segments_cache);
113+
114+
mi_stat_counter_add(&stats->pages_reclaim_on_alloc, &src->pages_reclaim_on_alloc);
115+
mi_stat_counter_add(&stats->pages_reclaim_on_free, &src->pages_reclaim_on_free);
116+
mi_stat_counter_add(&stats->pages_reabandon_full, &src->pages_reabandon_full);
117+
mi_stat_counter_add(&stats->pages_unabandon_busy_wait, &src->pages_unabandon_busy_wait);
107118

108-
mi_stat_counter_add(&stats->page_no_retire, &src->page_no_retire);
109-
mi_stat_counter_add(&stats->searches, &src->searches);
110-
mi_stat_counter_add(&stats->normal_count, &src->normal_count);
111-
mi_stat_counter_add(&stats->huge_count, &src->huge_count);
112-
mi_stat_counter_add(&stats->guarded_alloc_count, &src->guarded_alloc_count);
113119
#if MI_STAT>1
114120
for (size_t i = 0; i <= MI_BIN_HUGE; i++) {
115121
// if (src->normal_bins[i].total != 0 && src->normal_bins[i].current != 0) {
116-
mi_stat_add(&stats->normal_bins[i], &src->normal_bins[i]);
122+
mi_stat_add(&stats->malloc_bins[i], &src->malloc_bins[i]);
123+
//}
124+
}
125+
for (size_t i = 0; i <= MI_BIN_HUGE; i++) {
126+
// if (src->normal_bins[i].total != 0 && src->normal_bins[i].current != 0) {
127+
mi_stat_add(&stats->page_bins[i], &src->page_bins[i]);
117128
//}
118129
}
119130
#endif
@@ -301,18 +312,18 @@ static void _mi_stats_print(mi_stats_t* stats, mi_output_fun* out0, void* arg0)
301312
// and print using that
302313
mi_print_header(out,arg);
303314
#if MI_STAT>1
304-
mi_stats_print_bins(stats->normal_bins, MI_BIN_HUGE, "normal",out,arg);
315+
mi_stats_print_bins(stats->malloc_bins, MI_BIN_HUGE, "normal",out,arg);
305316
#endif
306317
#if MI_STAT
307-
mi_stat_print(&stats->normal, "normal", (stats->normal_count.total == 0 ? 1 : -1), out, arg);
308-
mi_stat_print(&stats->huge, "huge", (stats->huge_count.total == 0 ? 1 : -1), out, arg);
318+
mi_stat_print(&stats->malloc_normal, "normal", (stats->malloc_normal_count.total == 0 ? 1 : -1), out, arg);
319+
mi_stat_print(&stats->malloc_huge, "huge", (stats->malloc_huge_count.total == 0 ? 1 : -1), out, arg);
309320
mi_stat_count_t total = { 0,0,0 };
310-
mi_stat_add(&total, &stats->normal);
311-
mi_stat_add(&total, &stats->huge);
321+
mi_stat_add(&total, &stats->malloc_normal);
322+
mi_stat_add(&total, &stats->malloc_huge);
312323
mi_stat_print_ex(&total, "total", 1, out, arg, "");
313324
#endif
314325
#if MI_STAT>1
315-
mi_stat_print_ex(&stats->malloc, "malloc req", 1, out, arg, "");
326+
mi_stat_print_ex(&stats->malloc_requested, "malloc req", 1, out, arg, "");
316327
_mi_fprintf(out, arg, "\n");
317328
#endif
318329
mi_stat_print_ex(&stats->reserved, "reserved", 1, out, arg, "");
@@ -326,17 +337,17 @@ static void _mi_stats_print(mi_stats_t* stats, mi_output_fun* out0, void* arg0)
326337
mi_stat_print(&stats->pages, "pages", -1, out, arg);
327338
mi_stat_print(&stats->pages_abandoned, "-abandoned", -1, out, arg);
328339
mi_stat_counter_print(&stats->pages_extended, "-extended", out, arg);
329-
mi_stat_counter_print(&stats->page_no_retire, "-noretire", out, arg);
340+
mi_stat_counter_print(&stats->pages_retire, "-retire", out, arg);
330341
mi_stat_counter_print(&stats->arena_count, "arenas", out, arg);
331-
mi_stat_counter_print(&stats->arena_crossover_count, "-crossover", out, arg);
342+
// mi_stat_counter_print(&stats->arena_crossover_count, "-crossover", out, arg);
332343
mi_stat_counter_print(&stats->arena_rollback_count, "-rollback", out, arg);
333344
mi_stat_counter_print(&stats->mmap_calls, "mmaps", out, arg);
334345
mi_stat_counter_print(&stats->commit_calls, "commits", out, arg);
335346
mi_stat_counter_print(&stats->reset_calls, "resets", out, arg);
336347
mi_stat_counter_print(&stats->purge_calls, "purges", out, arg);
337-
mi_stat_counter_print(&stats->guarded_alloc_count, "guarded", out, arg);
348+
mi_stat_counter_print(&stats->malloc_guarded_count, "guarded", out, arg);
338349
mi_stat_print(&stats->threads, "threads", -1, out, arg);
339-
mi_stat_counter_print_avg(&stats->searches, "searches", out, arg);
350+
mi_stat_counter_print_avg(&stats->page_searches, "searches", out, arg);
340351
_mi_fprintf(out, arg, "%10s: %5zu\n", "numa nodes", _mi_os_numa_node_count());
341352

342353
size_t elapsed;

0 commit comments

Comments
 (0)