microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
10.45k stars 843 forks source link

Use MADV_FREE in FreeBSD #826

Open davide-digennaro-nozomi opened 10 months ago

davide-digennaro-nozomi commented 10 months ago

Apparently mimalloc uses MADV_DONTNEED on unix-like platforms in prim.c:

    #if defined(MADV_DONTNEED) && MI_DEBUG == 0 && MI_SECURE == 0
    // decommit: use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE)
    // (on the other hand, MADV_FREE would be good enough.. it is just not reflected in the stats :-( )
    err = unix_madvise(start, size, MADV_DONTNEED);

however the semantics of MADV_DONTNEED is not identical on every platform. According to man pages:

So it would make sense to prefer MADV_FREE (at least) in Freeebsd. Likely, the same may be true also in MacOS, but we didn't test.

What we propose is something like:

#ifndef MADV_DECOMMIT_BEHAVIOR

#if (defined(__FreeBSD__) || defined(__DragonFly__)) && defined(MADV_FREE)
#define MADV_DECOMMIT_BEHAVIOR MADV_FREE
#elif defined(MADV_DONTNEED)
#define MADV_DECOMMIT_BEHAVIOR MADV_DONTNEED
#endif

#endif
#if defined(MADV_DECOMMIT_BEHAVIOR) && MI_DEBUG == 0 && MI_SECURE == 0
    err = unix_madvise(start, size, MADV_DECOMMIT_BEHAVIOR);
devnexen commented 10 months ago

Your proposal makes sense, would be nice to come with tests before and after the change, what do you think ?

devnexen commented 10 months ago

Anyhow, snmalloc already uses MADV_FREE for FreeBSD.

davide-digennaro-nozomi commented 10 months ago
  1. Is there any other line in the code we should change, in addition to the above _mi_prim_commit? (I don't think so, but...)
  2. how precisely can we induce the allocator to call madvise i.e. force a decommit from c++ code? I'm not sure a plain delete is sufficient.

thanks

davide-digennaro-nozomi commented 3 months ago

Hello, sorry to revive an old thread, but the issue is still open. I noticed that the code in v2.17 is slightly different.

1) I think I only need to change the constant in _mi_prim_decommit. can you confirm? 2) do I also need to change the value returned in *needs_recommit?

thanks Davide