From e6e8213244a816511e95e14fb99176442922abac Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Wed, 28 Sep 2022 08:33:05 -0400 Subject: disable MADV_FREE usage in mallocng the entire intent of using madvise/MADV_FREE on freed slots is to improve system performance by avoiding evicting cache of useful data, or swapping useless data to disk, by marking any whole pages in the freed slot as discardable by the kernel. in particular, unlike unmapping the memory or replacing it with a PROT_NONE region, use of MADV_FREE does not make any difference to memory accounting for commit charge purposes, and so does not increase the memory available to other processes in a non-overcommitted environment. however, various measurements have shown that inordinate amounts of time are spent performing madvise syscalls in processes which frequently allocate and free medium sized objects in the size range roughly between PAGESIZE and MMAP_THRESHOLD, to the point that the net effect is almost surely significant performance degredation. so, turn it off. the code, which has some nontrivial logic for efficiently determining whether there is a whole-page range to apply madvise to, is left in place so that it can easily be re-enabled if desired, or later tuned to only apply to certain sizes or to use additional heuristics. --- src/malloc/mallocng/free.c | 2 +- src/malloc/mallocng/glue.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/malloc/mallocng/free.c b/src/malloc/mallocng/free.c index 418a085c..43f32aad 100644 --- a/src/malloc/mallocng/free.c +++ b/src/malloc/mallocng/free.c @@ -119,7 +119,7 @@ void free(void *p) if (((uintptr_t)(start-1) ^ (uintptr_t)end) >= 2*PGSZ && g->last_idx) { unsigned char *base = start + (-(uintptr_t)start & (PGSZ-1)); size_t len = (end-base) & -PGSZ; - if (len) { + if (len && USE_MADV_FREE) { int e = errno; madvise(base, len, MADV_FREE); errno = e; diff --git a/src/malloc/mallocng/glue.h b/src/malloc/mallocng/glue.h index 151c48b8..77f4c812 100644 --- a/src/malloc/mallocng/glue.h +++ b/src/malloc/mallocng/glue.h @@ -24,6 +24,8 @@ #define realloc __libc_realloc #define free __libc_free +#define USE_MADV_FREE 0 + #if USE_REAL_ASSERT #include #else -- cgit v1.2.1