Browse Source

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.
Rich Felker 1 year ago
parent
commit
e6e8213244
2 changed files with 3 additions and 1 deletions
  1. 1 1
      src/malloc/mallocng/free.c
  2. 2 0
      src/malloc/mallocng/glue.h

+ 1 - 1
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;

+ 2 - 0
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 <assert.h>
 #else