Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions src/prim/unix/prim.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,29 @@ 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
// if this memory was not marked as MADV_FREE_REUSABLE, this call is noop
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On non-Apple platforms, err is still 0 here, right? Or am I misreading?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you're right! I made a mistake, sorry!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, could you please look again?

{
// 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
Expand All @@ -454,23 +470,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;
}

Expand Down