Skip to content

Commit a55c9f8

Browse files
committed
1 parent d681803 commit a55c9f8

File tree

6 files changed

+58
-23
lines changed

6 files changed

+58
-23
lines changed

include/mimalloc-stats.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ terms of the MIT license. A copy of the license can be found in the file
1111
#include <mimalloc.h>
1212
#include <stdint.h>
1313

14-
#define MI_STAT_VERSION 2 // increased on every backward incompatible change
14+
#define MI_STAT_VERSION 3 // increased on every backward incompatible change
1515

1616
// count allocation over time
1717
typedef struct mi_stat_count_s {
@@ -25,12 +25,18 @@ typedef struct mi_stat_counter_s {
2525
int64_t total; // total count
2626
} mi_stat_counter_t;
2727

28+
// average over time
29+
typedef struct mi_stat_average_s {
30+
int64_t total;
31+
int64_t count;
32+
} mi_stat_average_t;
33+
2834
#define MI_STAT_FIELDS() \
2935
MI_STAT_COUNT(pages) /* count of mimalloc pages */ \
3036
MI_STAT_COUNT(reserved) /* reserved memory bytes */ \
3137
MI_STAT_COUNT(committed) /* committed bytes */ \
32-
MI_STAT_COUNT(reset) /* reset bytes */ \
33-
MI_STAT_COUNT(purged) /* purged bytes */ \
38+
MI_STAT_COUNTER(reset) /* reset bytes */ \
39+
MI_STAT_COUNTER(purged) /* purged bytes */ \
3440
MI_STAT_COUNT(page_committed) /* committed memory inside pages */ \
3541
MI_STAT_COUNT(pages_abandoned) /* abandonded pages count */ \
3642
MI_STAT_COUNT(threads) /* number of threads */ \
@@ -52,7 +58,7 @@ typedef struct mi_stat_counter_s {
5258
MI_STAT_COUNTER(arena_purges) \
5359
MI_STAT_COUNTER(pages_extended) /* number of page extensions */ \
5460
MI_STAT_COUNTER(pages_retire) /* number of pages that are retired */ \
55-
MI_STAT_COUNTER(page_searches) /* searches for a fresh page */ \
61+
MI_STAT_AVERAGE(page_searches) /* searches for a fresh page */ \
5662
/* only on v1 and v2 */ \
5763
MI_STAT_COUNT(segments) \
5864
MI_STAT_COUNT(segments_abandoned) \
@@ -81,6 +87,7 @@ typedef enum mi_chunkbin_e {
8187
#define MI_BIN_HUGE (73U) // see types.h
8288
#define MI_STAT_COUNT(stat) mi_stat_count_t stat;
8389
#define MI_STAT_COUNTER(stat) mi_stat_counter_t stat;
90+
#define MI_STAT_AVERAGE(stat) mi_stat_average_t stat;
8491

8592
typedef struct mi_stats_s
8693
{
@@ -100,6 +107,7 @@ typedef struct mi_stats_s
100107

101108
#undef MI_STAT_COUNT
102109
#undef MI_STAT_COUNTER
110+
#undef MI_STAT_AVERAGE
103111

104112

105113
// Exported definitions

include/mimalloc/internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,18 @@ void __mi_stat_adjust_decrease_mt(mi_stat_count_t* stat, size_t amount);
350350
void __mi_stat_counter_increase(mi_stat_counter_t* stat, size_t amount);
351351
void __mi_stat_counter_increase_mt(mi_stat_counter_t* stat, size_t amount);
352352

353+
// averages can just be increased
354+
void __mi_stat_average_increase(mi_stat_average_t* stat, size_t amount);
355+
void __mi_stat_average_increase_mt(mi_stat_average_t* stat, size_t amount);
356+
353357
#define mi_subproc_stat_counter_increase(subproc,stat,amount) __mi_stat_counter_increase_mt( &(subproc)->stats.stat, amount)
354358
#define mi_subproc_stat_increase(subproc,stat,amount) __mi_stat_increase_mt( &(subproc)->stats.stat, amount)
355359
#define mi_subproc_stat_decrease(subproc,stat,amount) __mi_stat_decrease_mt( &(subproc)->stats.stat, amount)
356360
#define mi_subproc_stat_adjust_increase(subproc,stat,amnt) __mi_stat_adjust_increase_mt( &(subproc)->stats.stat, amnt)
357361
#define mi_subproc_stat_adjust_decrease(subproc,stat,amnt) __mi_stat_adjust_decrease_mt( &(subproc)->stats.stat, amnt)
358362

359363
#define mi_tld_stat_counter_increase(tld,stat,amount) __mi_stat_counter_increase( &(tld)->stats.stat, amount)
364+
#define mi_tld_stat_average_increase(tld,stat,amount) __mi_stat_average_increase( &(tld)->stats.stat, amount)
360365
#define mi_tld_stat_increase(tld,stat,amount) __mi_stat_increase( &(tld)->stats.stat, amount)
361366
#define mi_tld_stat_decrease(tld,stat,amount) __mi_stat_decrease( &(tld)->stats.stat, amount)
362367
#define mi_tld_stat_adjust_increase(tld,stat,amnt) __mi_stat_adjust_increase( &(tld)->stats.stat, amnt)
@@ -367,6 +372,7 @@ void __mi_stat_counter_increase_mt(mi_stat_counter_t* stat, size_t amount);
367372
#define mi_os_stat_decrease(stat,amount) mi_subproc_stat_decrease(_mi_subproc(),stat,amount)
368373

369374
#define mi_heap_stat_counter_increase(heap,stat,amount) mi_tld_stat_counter_increase(heap->tld, stat, amount)
375+
#define mi_heap_stat_average_increase(heap,stat,amount) mi_tld_stat_average_increase(heap->tld, stat, amount)
370376
#define mi_heap_stat_increase(heap,stat,amount) mi_tld_stat_increase( heap->tld, stat, amount)
371377
#define mi_heap_stat_decrease(heap,stat,amount) mi_tld_stat_decrease( heap->tld, stat, amount)
372378
#define mi_heap_stat_adjust_decrease(heap,stat,amount) mi_tld_stat_adjust_decrease( heap->tld, stat, amount)

src/init.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,19 @@ const mi_page_t _mi_page_empty = {
6565
QNULL(MI_LARGE_MAX_OBJ_WSIZE + 2) /* Full queue */ }
6666

6767
#define MI_STAT_COUNT_NULL() {0,0,0}
68+
#define MI_STAT_AVERAGE_NULL() {0,0}
6869

6970
// Empty statistics
7071
#define MI_STATS_NULL \
71-
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
72-
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
72+
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
73+
{ 0 }, { 0 }, \
74+
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
7375
MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), MI_STAT_COUNT_NULL(), \
7476
{ 0 }, { 0 }, { 0 }, { 0 }, \
7577
{ 0 }, { 0 }, { 0 }, { 0 }, \
7678
\
77-
{ 0 }, { 0 }, { 0 }, { 0 }, { 0 }, \
79+
{ 0 }, { 0 }, { 0 }, { 0 }, \
80+
MI_STAT_AVERAGE_NULL(), \
7881
MI_INIT4(MI_STAT_COUNT_NULL), \
7982
{ 0 }, { 0 }, { 0 }, { 0 }, \
8083
\

src/os.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ bool _mi_os_reset(void* addr, size_t size) {
551551
size_t csize;
552552
void* start = mi_os_page_align_area_conservative(addr, size, &csize);
553553
if (csize == 0) return true; // || _mi_os_is_huge_reserved(addr)
554-
mi_os_stat_increase(reset, csize);
554+
mi_os_stat_counter_increase(reset, csize);
555555
mi_os_stat_counter_increase(reset_calls, 1);
556556

557557
#if (MI_DEBUG>1) && !MI_SECURE && !MI_TRACK_ENABLED // && !MI_TSAN
@@ -583,7 +583,7 @@ bool _mi_os_purge_ex(void* p, size_t size, bool allow_reset, size_t stat_size, m
583583
{
584584
if (mi_option_get(mi_option_purge_delay) < 0) return false; // is purging allowed?
585585
mi_os_stat_counter_increase(purge_calls, 1);
586-
mi_os_stat_increase(purged, size);
586+
mi_os_stat_counter_increase(purged, size);
587587

588588
if (commit_fun != NULL) {
589589
bool decommitted = (*commit_fun)(false, p, size, NULL, commit_fun_arg);

src/page.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ static mi_decl_noinline mi_page_t* mi_page_queue_find_free_ex(mi_heap_t* heap, m
789789
page = next;
790790
} // for each page
791791

792-
mi_heap_stat_counter_increase(heap, page_searches, count);
792+
mi_heap_stat_average_increase(heap, page_searches, count);
793793

794794
// set the page to the best candidate
795795
if (page_candidate != NULL) {

src/stats.c

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ void __mi_stat_counter_increase(mi_stat_counter_t* stat, size_t amount) {
4646
stat->total += amount;
4747
}
4848

49+
void __mi_stat_average_increase_mt(mi_stat_average_t* stat, size_t amount) {
50+
mi_atomic_addi64_relaxed( &stat->count, (int64_t)1 );
51+
mi_atomic_addi64_relaxed( &stat->total, (int64_t)amount );
52+
}
53+
54+
void __mi_stat_average_increase(mi_stat_average_t* stat, size_t amount) {
55+
stat->count++;
56+
stat->total += amount;
57+
}
58+
4959
void __mi_stat_increase_mt(mi_stat_count_t* stat, size_t amount) {
5060
mi_stat_update_mt(stat, (int64_t)amount);
5161
}
@@ -111,8 +121,15 @@ static void mi_stat_counter_add_mt(mi_stat_counter_t* stat, const mi_stat_counte
111121
mi_atomic_void_addi64_relaxed(&stat->total, &src->total);
112122
}
113123

124+
static void mi_stat_average_add_mt(mi_stat_average_t* stat, const mi_stat_average_t* src) {
125+
if (stat==src) return;
126+
mi_atomic_void_addi64_relaxed(&stat->count, &src->count);
127+
mi_atomic_void_addi64_relaxed(&stat->total, &src->total);
128+
}
129+
114130
#define MI_STAT_COUNT(stat) mi_stat_count_add_mt(&stats->stat, &src->stat);
115131
#define MI_STAT_COUNTER(stat) mi_stat_counter_add_mt(&stats->stat, &src->stat);
132+
#define MI_STAT_AVERAGE(stat) mi_stat_average_add_mt(&stats->stat, &src->stat);
116133

117134
// must be thread safe as it is called from stats_merge
118135
static void mi_stats_add(mi_stats_t* stats, const mi_stats_t* src) {
@@ -133,6 +150,7 @@ static void mi_stats_add(mi_stats_t* stats, const mi_stats_t* src) {
133150

134151
#undef MI_STAT_COUNT
135152
#undef MI_STAT_COUNTER
153+
#undef MI_STAT_AVERAGE
136154

137155
/* -----------------------------------------------------------
138156
Display statistics
@@ -225,12 +243,6 @@ static void mi_stat_print(const mi_stat_count_t* stat, const char* msg, int64_t
225243
mi_stat_print_ex(stat, msg, unit, out, arg, NULL);
226244
}
227245

228-
static void mi_stat_peak_print(const mi_stat_count_t* stat, const char* msg, int64_t unit, mi_output_fun* out, void* arg) {
229-
_mi_fprintf(out, arg, "%10s:", msg);
230-
mi_print_amount(stat->peak, unit, out, arg);
231-
_mi_fprintf(out, arg, "\n");
232-
}
233-
234246
#if MI_STAT>1
235247
static void mi_stat_total_print(const mi_stat_count_t* stat, const char* msg, int64_t unit, mi_output_fun* out, void* arg) {
236248
_mi_fprintf(out, arg, "%10s:", msg);
@@ -246,15 +258,13 @@ static void mi_stat_counter_print(const mi_stat_counter_t* stat, const char* msg
246258
_mi_fprintf(out, arg, "\n");
247259
}
248260

249-
250-
static void mi_stat_counter_print_avg(const mi_stat_counter_t* stat, const char* msg, mi_output_fun* out, void* arg) {
251-
const int64_t avg_tens = (stat->total == 0 ? 0 : (stat->total*10 / stat->total));
261+
static void mi_stat_average_print(const mi_stat_average_t* stat, const char* msg, mi_output_fun* out, void* arg) {
262+
const int64_t avg_tens = (stat->count == 0 ? 0 : (stat->total*10 / stat->count));
252263
const long avg_whole = (long)(avg_tens/10);
253264
const long avg_frac1 = (long)(avg_tens%10);
254265
_mi_fprintf(out, arg, "%10s: %5ld.%ld avg\n", msg, avg_whole, avg_frac1);
255266
}
256267

257-
258268
static void mi_print_header(mi_output_fun* out, void* arg ) {
259269
_mi_fprintf(out, arg, "%10s: %11s %11s %11s %11s %11s\n", "heap stats", "peak ", "total ", "current ", "block ", "total# ");
260270
}
@@ -341,8 +351,8 @@ void _mi_stats_print(mi_stats_t* stats, mi_output_fun* out0, void* arg0) mi_attr
341351
#endif
342352
mi_stat_print_ex(&stats->reserved, "reserved", 1, out, arg, "");
343353
mi_stat_print_ex(&stats->committed, "committed", 1, out, arg, "");
344-
mi_stat_peak_print(&stats->reset, "reset", 1, out, arg );
345-
mi_stat_peak_print(&stats->purged, "purged", 1, out, arg );
354+
mi_stat_counter_print(&stats->reset, "reset", out, arg );
355+
mi_stat_counter_print(&stats->purged, "purged", out, arg );
346356
mi_stat_print_ex(&stats->page_committed, "touched", 1, out, arg, "");
347357
// mi_stat_print(&stats->segments, "segments", -1, out, arg);
348358
// mi_stat_print(&stats->segments_abandoned, "-abandoned", -1, out, arg);
@@ -365,7 +375,7 @@ void _mi_stats_print(mi_stats_t* stats, mi_output_fun* out0, void* arg0) mi_attr
365375
mi_stat_counter_print(&stats->purge_calls, "purges", out, arg);
366376
mi_stat_counter_print(&stats->malloc_guarded_count, "guarded", out, arg);
367377
mi_stat_print(&stats->threads, "threads", -1, out, arg);
368-
mi_stat_counter_print_avg(&stats->page_searches, "searches", out, arg);
378+
mi_stat_average_print(&stats->page_searches, "searches", out, arg);
369379
_mi_fprintf(out, arg, "%10s: %5i\n", "numa nodes", _mi_os_numa_node_count());
370380

371381
size_t elapsed;
@@ -619,8 +629,16 @@ static void mi_heap_buf_print_counter_value(mi_heap_buf_t* hbuf, const char* nam
619629
mi_heap_buf_print_value(hbuf, name, stat->total);
620630
}
621631

632+
static void mi_heap_buf_print_average_value(mi_heap_buf_t* hbuf, const char* name, mi_stat_average_t* stat) {
633+
char buf[128];
634+
_mi_snprintf(buf, 128, " \"%s\": { \"count\": %lld, \"total\": %lld },\n", name, stat->count, stat->total);
635+
buf[127] = 0;
636+
mi_heap_buf_print(hbuf, buf);
637+
}
638+
622639
#define MI_STAT_COUNT(stat) mi_heap_buf_print_count_value(&hbuf, #stat, &stats->stat);
623640
#define MI_STAT_COUNTER(stat) mi_heap_buf_print_counter_value(&hbuf, #stat, &stats->stat);
641+
#define MI_STAT_AVERAGE(stat) mi_heap_buf_print_average_value(&hbuf, #stat, &stats->stat);
624642

625643
char* mi_stats_get_json(size_t output_size, char* output_buf) mi_attr_noexcept {
626644
mi_stats_merge();

0 commit comments

Comments
 (0)