about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2022-09-28 08:33:05 -0400
committerRich Felker <dalias@aerifal.cx>2022-10-19 14:01:31 -0400
commite6e8213244a816511e95e14fb99176442922abac (patch)
treeed212c679facb2a52855d3d6d423998f6870cb45
parent25e6fee27f4a293728dd15b659170e7b9c7db9bc (diff)
downloadmusl-e6e8213244a816511e95e14fb99176442922abac.tar.gz
musl-e6e8213244a816511e95e14fb99176442922abac.tar.xz
musl-e6e8213244a816511e95e14fb99176442922abac.zip
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.
-rw-r--r--src/malloc/mallocng/free.c2
-rw-r--r--src/malloc/mallocng/glue.h2
2 files changed, 3 insertions, 1 deletions
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 <assert.h>
 #else