From 76b631254d0b13e80dcee0a9b378f32d604fec2b Mon Sep 17 00:00:00 2001 From: iurii zakipnyi Date: Tue, 3 Jun 2025 09:23:42 -0700 Subject: [PATCH 1/3] Apple friendly way to madvice on commit/decommit --- src/prim/unix/prim.c | 60 +++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index a90fa659..ff4eb610 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -430,13 +430,28 @@ int _mi_prim_commit(void* start, size_t size, bool* is_zero) { err = errno; unix_mprotect_hint(err); } + + #if defined(__APPLE__) + // MADV_FREE_REUSABLE is paired with MADV_FREE_REUSE for accounting + madvise(start, size, MADV_FREE_REUSE); + #endif return err; } int _mi_prim_decommit(void* start, size_t size, bool* needs_recommit) { int err = 0; - // decommit: use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE) - err = unix_madvise(start, size, MADV_DONTNEED); + #if defined(__APPLE__) + // On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure. + err = unix_madvise(start, size, MADV_FREE_REUSABLE); + // if this fails - fallback to MADV_DONTNEED + #endif + + if (err) + { + // use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE) + err = unix_madvise(start, size, MADV_DONTNEED); + } + #if !MI_DEBUG && MI_SECURE<=2 *needs_recommit = false; #else @@ -454,23 +469,32 @@ int _mi_prim_decommit(void* start, size_t size, bool* needs_recommit) { } int _mi_prim_reset(void* start, size_t size) { - // We try to use `MADV_FREE` as that is the fastest. A drawback though is that it - // will not reduce the `rss` stats in tools like `top` even though the memory is available - // to other processes. With the default `MIMALLOC_PURGE_DECOMMITS=1` we ensure that by - // default `MADV_DONTNEED` is used though. - #if defined(MADV_FREE) - static _Atomic(size_t) advice = MI_ATOMIC_VAR_INIT(MADV_FREE); - int oadvice = (int)mi_atomic_load_relaxed(&advice); - int err; - while ((err = unix_madvise(start, size, oadvice)) != 0 && errno == EAGAIN) { errno = 0; }; - if (err != 0 && errno == EINVAL && oadvice == MADV_FREE) { - // if MADV_FREE is not supported, fall back to MADV_DONTNEED from now on - mi_atomic_store_release(&advice, (size_t)MADV_DONTNEED); - err = unix_madvise(start, size, MADV_DONTNEED); - } - #else - int err = unix_madvise(start, size, MADV_DONTNEED); + int err = 0; + #if defined(__APPLE__) + // On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure. + err = unix_madvise(start, size, MADV_FREE_REUSABLE); + // if this fails - fallback to MADV_FREE/MADV_DONTNEED #endif + + if (err) + { + // We try to use `MADV_FREE` as that is the fastest. A drawback though is that it + // will not reduce the `rss` stats in tools like `top` even though the memory is available + // to other processes. With the default `MIMALLOC_PURGE_DECOMMITS=1` we ensure that by + // default `MADV_DONTNEED` is used though. + #if defined(MADV_FREE) + static _Atomic(size_t) advice = MI_ATOMIC_VAR_INIT(MADV_FREE); + int oadvice = (int)mi_atomic_load_relaxed(&advice); + while ((err = unix_madvise(start, size, oadvice)) != 0 && errno == EAGAIN) { errno = 0; }; + if (err != 0 && errno == EINVAL && oadvice == MADV_FREE) { + // if MADV_FREE is not supported, fall back to MADV_DONTNEED from now on + mi_atomic_store_release(&advice, (size_t)MADV_DONTNEED); + err = unix_madvise(start, size, MADV_DONTNEED); + } + #else + err = unix_madvise(start, size, MADV_DONTNEED); + #endif + } return err; } From 157b201004fb262062b6b37dc2d248f57252ade0 Mon Sep 17 00:00:00 2001 From: iurii zakipnyi Date: Tue, 3 Jun 2025 09:39:05 -0700 Subject: [PATCH 2/3] minor comment --- src/prim/unix/prim.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index ff4eb610..5e265d17 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -433,6 +433,7 @@ int _mi_prim_commit(void* start, size_t size, bool* is_zero) { #if defined(__APPLE__) // MADV_FREE_REUSABLE is paired with MADV_FREE_REUSE for accounting + // if this memory was not marked as MADV_FREE_REUSABLE, this call is noop madvise(start, size, MADV_FREE_REUSE); #endif return err; From e784983b76d4575b5576fdbe90173119f7982f77 Mon Sep 17 00:00:00 2001 From: iurii zakipnyi Date: Wed, 4 Jun 2025 10:29:39 -0700 Subject: [PATCH 3/3] Made a mistake. Fixed for non-apple unix --- src/prim/unix/prim.c | 53 ++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index 5e265d17..ba062520 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -440,18 +440,18 @@ int _mi_prim_commit(void* start, size_t size, bool* is_zero) { } int _mi_prim_decommit(void* start, size_t size, bool* needs_recommit) { - int err = 0; #if defined(__APPLE__) // On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure. - err = unix_madvise(start, size, MADV_FREE_REUSABLE); - // if this fails - fallback to MADV_DONTNEED - #endif - - if (err) - { + int err = unix_madvise(start, size, MADV_FREE_REUSABLE); + if (err != 0) // if MADV_FREE_REUSABLE fails - fallback to MADV_DONTNEED + { + // use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE) + err = unix_madvise(start, size, MADV_DONTNEED); + } + #else // use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE) - err = unix_madvise(start, size, MADV_DONTNEED); - } + int err = unix_madvise(start, size, MADV_DONTNEED); + #endif #if !MI_DEBUG && MI_SECURE<=2 *needs_recommit = false; @@ -474,28 +474,27 @@ int _mi_prim_reset(void* start, size_t size) { #if defined(__APPLE__) // On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure. err = unix_madvise(start, size, MADV_FREE_REUSABLE); + if (err == 0) + return 0; // if this fails - fallback to MADV_FREE/MADV_DONTNEED #endif - if (err) - { - // We try to use `MADV_FREE` as that is the fastest. A drawback though is that it - // will not reduce the `rss` stats in tools like `top` even though the memory is available - // to other processes. With the default `MIMALLOC_PURGE_DECOMMITS=1` we ensure that by - // default `MADV_DONTNEED` is used though. - #if defined(MADV_FREE) - static _Atomic(size_t) advice = MI_ATOMIC_VAR_INIT(MADV_FREE); - int oadvice = (int)mi_atomic_load_relaxed(&advice); - while ((err = unix_madvise(start, size, oadvice)) != 0 && errno == EAGAIN) { errno = 0; }; - if (err != 0 && errno == EINVAL && oadvice == MADV_FREE) { - // if MADV_FREE is not supported, fall back to MADV_DONTNEED from now on - mi_atomic_store_release(&advice, (size_t)MADV_DONTNEED); - err = unix_madvise(start, size, MADV_DONTNEED); - } - #else + // We try to use `MADV_FREE` as that is the fastest. A drawback though is that it + // will not reduce the `rss` stats in tools like `top` even though the memory is available + // to other processes. With the default `MIMALLOC_PURGE_DECOMMITS=1` we ensure that by + // default `MADV_DONTNEED` is used though. + #if defined(MADV_FREE) + static _Atomic(size_t) advice = MI_ATOMIC_VAR_INIT(MADV_FREE); + int oadvice = (int)mi_atomic_load_relaxed(&advice); + while ((err = unix_madvise(start, size, oadvice)) != 0 && errno == EAGAIN) { errno = 0; }; + if (err != 0 && errno == EINVAL && oadvice == MADV_FREE) { + // if MADV_FREE is not supported, fall back to MADV_DONTNEED from now on + mi_atomic_store_release(&advice, (size_t)MADV_DONTNEED); err = unix_madvise(start, size, MADV_DONTNEED); - #endif - } + } + #else + err = unix_madvise(start, size, MADV_DONTNEED); + #endif return err; }