Skip to content

Commit 587e08c

Browse files
committed
improve commit stats
1 parent cfe73e9 commit 587e08c

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

include/mimalloc/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ bool _mi_os_decommit(void* addr, size_t size);
121121
bool _mi_os_protect(void* addr, size_t size);
122122
bool _mi_os_unprotect(void* addr, size_t size);
123123
bool _mi_os_purge(void* p, size_t size);
124-
bool _mi_os_purge_ex(void* p, size_t size, bool allow_reset);
124+
bool _mi_os_purge_ex(void* p, size_t size, bool allow_reset, size_t stat_size);
125125

126126
void* _mi_os_alloc_aligned(size_t size, size_t alignment, bool commit, bool allow_large, mi_memid_t* memid);
127127
void* _mi_os_alloc_aligned_at_offset(size_t size, size_t alignment, size_t align_offset, bool commit, bool allow_large, mi_memid_t* memid);

src/arena.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,7 @@ static void mi_arena_purge(mi_arena_t* arena, size_t bitmap_idx, size_t blocks)
474474
// we need to ensure we do not try to reset (as that may be invalid for uncommitted memory),
475475
// and also undo the decommit stats (as it was already adjusted)
476476
mi_assert_internal(mi_option_is_enabled(mi_option_purge_decommits));
477-
needs_recommit = _mi_os_purge_ex(p, size, false /* allow reset? */);
478-
if (needs_recommit) { _mi_stat_increase(&_mi_stats_main.committed, size); }
477+
needs_recommit = _mi_os_purge_ex(p, size, false /* allow reset? */, 0);
479478
}
480479

481480
// clear the purged blocks

src/os.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ terms of the MIT license. A copy of the license can be found in the file
99
#include "mimalloc/atomic.h"
1010
#include "mimalloc/prim.h"
1111

12-
#define os_stats (&_mi_stats_main)
12+
#define mi_os_stat_increase(stat,amount) _mi_stat_increase(&_mi_stats_main.stat, amount)
13+
#define mi_os_stat_decrease(stat,amount) _mi_stat_decrease(&_mi_stats_main.stat, amount)
14+
#define mi_os_stat_counter_increase(stat,inc) _mi_stat_counter_increase(&_mi_stats_main.stat, inc)
1315

1416
/* -----------------------------------------------------------
1517
Initialization.
@@ -171,9 +173,9 @@ static void mi_os_prim_free(void* addr, size_t size, size_t commit_size) {
171173
_mi_warning_message("unable to free OS memory (error: %d (0x%x), size: 0x%zx bytes, address: %p)\n", err, err, size, addr);
172174
}
173175
if (commit_size > 0) {
174-
_mi_stat_decrease(&os_stats->committed, commit_size);
176+
mi_os_stat_decrease(committed, commit_size);
175177
}
176-
_mi_stat_decrease(&os_stats->reserved, size);
178+
mi_os_stat_decrease(reserved, size);
177179
}
178180

179181
void _mi_os_free_ex(void* addr, size_t size, bool still_committed, mi_memid_t memid) {
@@ -236,11 +238,11 @@ static void* mi_os_prim_alloc_at(void* hint_addr, size_t size, size_t try_alignm
236238

237239

238240

239-
mi_stat_counter_increase(os_stats->mmap_calls, 1);
241+
mi_os_stat_counter_increase(mmap_calls, 1);
240242
if (p != NULL) {
241-
_mi_stat_increase(&os_stats->reserved, size);
243+
mi_os_stat_increase(reserved, size);
242244
if (commit) {
243-
_mi_stat_increase(&os_stats->committed, size);
245+
mi_os_stat_increase(committed, size);
244246
// seems needed for asan (or `mimalloc-test-api` fails)
245247
#ifdef MI_TRACK_ASAN
246248
if (*is_zero) { mi_track_mem_defined(p,size); }
@@ -357,7 +359,8 @@ void* _mi_os_alloc_aligned(size_t size, size_t alignment, bool commit, bool allo
357359
if (p != NULL) {
358360
*memid = _mi_memid_create_os(commit, os_is_zero, os_is_large);
359361
memid->mem.os.base = os_base;
360-
memid->mem.os.alignment = alignment;
362+
// memid->mem.os.alignment = alignment;
363+
memid->mem.os.size += ((uint8_t*)p - (uint8_t*)os_base); // todo: return from prim_alloc_aligned
361364
}
362365
return p;
363366
}
@@ -425,10 +428,10 @@ static void* mi_os_page_align_area_conservative(void* addr, size_t size, size_t*
425428
return mi_os_page_align_areax(true, addr, size, newsize);
426429
}
427430

428-
bool _mi_os_commit(void* addr, size_t size, bool* is_zero) {
431+
bool _mi_os_commit_ex(void* addr, size_t size, bool* is_zero, size_t stat_size) {
429432
if (is_zero != NULL) { *is_zero = false; }
430-
_mi_stat_increase(&os_stats->committed, size); // use size for precise commit vs. decommit
431-
_mi_stat_counter_increase(&os_stats->commit_calls, 1);
433+
mi_os_stat_increase(committed, stat_size); // use size for precise commit vs. decommit
434+
mi_os_stat_counter_increase(commit_calls, 1);
432435

433436
// page align range
434437
size_t csize;
@@ -492,8 +495,8 @@ bool _mi_os_reset(void* addr, size_t size) {
492495
size_t csize;
493496
void* start = mi_os_page_align_area_conservative(addr, size, &csize);
494497
if (csize == 0) return true; // || _mi_os_is_huge_reserved(addr)
495-
_mi_stat_increase(&os_stats->reset, csize);
496-
_mi_stat_counter_increase(&os_stats->reset_calls, 1);
498+
mi_os_stat_increase(reset, csize);
499+
mi_os_stat_counter_increase(reset_calls, 1);
497500

498501
#if (MI_DEBUG>1) && !MI_SECURE && !MI_TRACK_ENABLED // && !MI_TSAN
499502
memset(start, 0, csize); // pretend it is eagerly reset
@@ -509,17 +512,17 @@ bool _mi_os_reset(void* addr, size_t size) {
509512

510513
// either resets or decommits memory, returns true if the memory needs
511514
// to be recommitted if it is to be re-used later on.
512-
bool _mi_os_purge_ex(void* p, size_t size, bool allow_reset)
515+
bool _mi_os_purge_ex(void* p, size_t size, bool allow_reset, size_t stat_size)
513516
{
514517
if (mi_option_get(mi_option_purge_delay) < 0) return false; // is purging allowed?
515-
_mi_stat_counter_increase(&os_stats->purge_calls, 1);
516-
_mi_stat_increase(&os_stats->purged, size);
518+
mi_os_stat_counter_increase(purge_calls, 1);
519+
mi_os_stat_increase(purged, size);
517520

518521
if (mi_option_is_enabled(mi_option_purge_decommits) && // should decommit?
519522
!_mi_preloading()) // don't decommit during preloading (unsafe)
520523
{
521524
bool needs_recommit = true;
522-
mi_os_decommit_ex(p, size, &needs_recommit);
525+
mi_os_decommit_ex(p, size, &needs_recommit, stat_size);
523526
return needs_recommit;
524527
}
525528
else {
@@ -533,7 +536,7 @@ bool _mi_os_purge_ex(void* p, size_t size, bool allow_reset)
533536
// either resets or decommits memory, returns true if the memory needs
534537
// to be recommitted if it is to be re-used later on.
535538
bool _mi_os_purge(void* p, size_t size) {
536-
return _mi_os_purge_ex(p, size, true);
539+
return _mi_os_purge_ex(p, size, true, size);
537540
}
538541

539542

@@ -648,8 +651,8 @@ void* _mi_os_alloc_huge_os_pages(size_t pages, int numa_node, mi_msecs_t max_mse
648651

649652
// success, record it
650653
page++; // increase before timeout check (see issue #711)
651-
_mi_stat_increase(&os_stats->committed, MI_HUGE_OS_PAGE_SIZE);
652-
_mi_stat_increase(&os_stats->reserved, MI_HUGE_OS_PAGE_SIZE);
654+
mi_os_stat_increase(committed, MI_HUGE_OS_PAGE_SIZE);
655+
mi_os_stat_increase(reserved, MI_HUGE_OS_PAGE_SIZE);
653656

654657
// check for timeout
655658
if (max_msecs > 0) {

test/test-stress.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static bool main_participates = false; // main thread participates as a
6464
#define custom_calloc(n,s) mi_calloc(n,s)
6565
#define custom_realloc(p,s) mi_realloc(p,s)
6666
#define custom_free(p) mi_free(p)
67+
6768
#ifndef NDEBUG
6869
#define HEAP_WALK // walk the heap objects?
6970
#endif
@@ -223,7 +224,7 @@ static void test_stress(void) {
223224
run_os_threads(THREADS, &stress);
224225
#if !defined(NDEBUG) && !defined(USE_STD_MALLOC)
225226
// switch between arena and OS allocation for testing
226-
mi_option_set_enabled(mi_option_disallow_arena_alloc, (n%2)==1);
227+
// mi_option_set_enabled(mi_option_disallow_arena_alloc, (n%2)==1);
227228
#endif
228229
#ifdef HEAP_WALK
229230
size_t total = 0;

0 commit comments

Comments
 (0)