Skip to content

Commit 62f2d84

Browse files
committed
Refactor madvise(2) configuration.
Add feature tests for the MADV_FREE and MADV_DONTNEED flags to madvise(2), so that MADV_FREE is detected and used for Linux kernel versions 4.5 and newer. Refactor pages_purge() so that on systems which support both flags, MADV_FREE is preferred over MADV_DONTNEED. This resolves jemalloc#387.
1 parent e7ca53b commit 62f2d84

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

configure.ac

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ maps_coalesce="1"
354354
case "${host}" in
355355
*-*-darwin* | *-*-ios*)
356356
abi="macho"
357-
AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])
358357
RPATH=""
359358
LD_PRELOAD_VAR="DYLD_INSERT_LIBRARIES"
360359
so="dylib"
@@ -367,29 +366,24 @@ case "${host}" in
367366
*-*-freebsd*)
368367
abi="elf"
369368
AC_DEFINE([JEMALLOC_SYSCTL_VM_OVERCOMMIT], [ ])
370-
AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])
371369
force_lazy_lock="1"
372370
;;
373371
*-*-dragonfly*)
374372
abi="elf"
375-
AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])
376373
;;
377374
*-*-openbsd*)
378375
abi="elf"
379-
AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])
380376
force_tls="0"
381377
;;
382378
*-*-bitrig*)
383379
abi="elf"
384-
AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])
385380
;;
386381
*-*-linux-android)
387382
dnl syscall(2) and secure_getenv(3) are exposed by _GNU_SOURCE.
388383
CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
389384
abi="elf"
390385
AC_DEFINE([JEMALLOC_HAS_ALLOCA_H])
391386
AC_DEFINE([JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY], [ ])
392-
AC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED], [ ])
393387
AC_DEFINE([JEMALLOC_THREADED_INIT], [ ])
394388
AC_DEFINE([JEMALLOC_C11ATOMICS])
395389
force_tls="0"
@@ -401,7 +395,6 @@ case "${host}" in
401395
abi="elf"
402396
AC_DEFINE([JEMALLOC_HAS_ALLOCA_H])
403397
AC_DEFINE([JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY], [ ])
404-
AC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED], [ ])
405398
AC_DEFINE([JEMALLOC_THREADED_INIT], [ ])
406399
AC_DEFINE([JEMALLOC_USE_CXX_THROW], [ ])
407400
default_munmap="0"
@@ -418,11 +411,9 @@ case "${host}" in
418411
[abi="elf"],
419412
[abi="aout"])
420413
AC_MSG_RESULT([$abi])
421-
AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])
422414
;;
423415
*-*-solaris2*)
424416
abi="elf"
425-
AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])
426417
RPATH='-Wl,-R,$(1)'
427418
dnl Solaris needs this for sigwait().
428419
CPPFLAGS="$CPPFLAGS -D_POSIX_PTHREAD_SEMANTICS"
@@ -1610,12 +1601,33 @@ dnl Check for madvise(2).
16101601
JE_COMPILABLE([madvise(2)], [
16111602
#include <sys/mman.h>
16121603
], [
1613-
{
1614-
madvise((void *)0, 0, 0);
1615-
}
1604+
madvise((void *)0, 0, 0);
16161605
], [je_cv_madvise])
16171606
if test "x${je_cv_madvise}" = "xyes" ; then
1618-
AC_DEFINE([JEMALLOC_HAVE_MADVISE], [ ])
1607+
dnl Check for madvise(..., MADV_FREE).
1608+
JE_COMPILABLE([madvise(..., MADV_FREE)], [
1609+
#include <sys/mman.h>
1610+
], [
1611+
madvise((void *)0, 0, MADV_FREE);
1612+
], [je_cv_madv_free])
1613+
if test "x${je_cv_madv_free}" = "xyes" ; then
1614+
AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])
1615+
fi
1616+
1617+
dnl Check for madvise(..., MADV_DONTNEED).
1618+
JE_COMPILABLE([madvise(..., MADV_DONTNEED)], [
1619+
#include <sys/mman.h>
1620+
], [
1621+
madvise((void *)0, 0, MADV_DONTNEED);
1622+
], [je_cv_madv_dontneed])
1623+
if test "x${je_cv_madv_dontneed}" = "xyes" ; then
1624+
AC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED], [ ])
1625+
fi
1626+
1627+
if test "x${je_cv_madv_free}" = "xyes" \
1628+
-o "x${je_cv_madv_dontneed}" = "xyes" ; then
1629+
AC_DEFINE([JEMALLOC_HAVE_MADVISE], [ ])
1630+
fi
16191631
fi
16201632

16211633
dnl ============================================================================

include/jemalloc/internal/jemalloc_internal_defs.h.in

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@
5555
*/
5656
#undef JEMALLOC_HAVE_BUILTIN_CLZ
5757

58-
/*
59-
* Defined if madvise(2) is available.
60-
*/
61-
#undef JEMALLOC_HAVE_MADVISE
62-
6358
/*
6459
* Defined if os_unfair_lock_*() functions are available, as provided by Darwin.
6560
*/
@@ -252,18 +247,20 @@
252247
#undef JEMALLOC_SYSCTL_VM_OVERCOMMIT
253248
#undef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY
254249

250+
/* Defined if madvise(2) is available. */
251+
#undef JEMALLOC_HAVE_MADVISE
252+
255253
/*
256254
* Methods for purging unused pages differ between operating systems.
257255
*
258-
* madvise(..., MADV_DONTNEED) : On Linux, this immediately discards pages,
259-
* such that new pages will be demand-zeroed if
260-
* the address region is later touched.
261-
* madvise(..., MADV_FREE) : On FreeBSD and Darwin, this marks pages as being
262-
* unused, such that they will be discarded rather
263-
* than swapped out.
256+
* madvise(..., MADV_FREE) : This marks pages as being unused, such that they
257+
* will be discarded rather than swapped out.
258+
* madvise(..., MADV_DONTNEED) : This immediately discards pages, such that
259+
* new pages will be demand-zeroed if the
260+
* address region is later touched.
264261
*/
265-
#undef JEMALLOC_PURGE_MADVISE_DONTNEED
266262
#undef JEMALLOC_PURGE_MADVISE_FREE
263+
#undef JEMALLOC_PURGE_MADVISE_DONTNEED
267264

268265
/* Define if operating system has alloca.h header. */
269266
#undef JEMALLOC_HAS_ALLOCA_H

src/pages.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ pages_purge(void *addr, size_t size)
171171
VirtualAlloc(addr, size, MEM_RESET, PAGE_READWRITE);
172172
unzeroed = true;
173173
#elif defined(JEMALLOC_HAVE_MADVISE)
174-
# ifdef JEMALLOC_PURGE_MADVISE_DONTNEED
175-
# define JEMALLOC_MADV_PURGE MADV_DONTNEED
176-
# define JEMALLOC_MADV_ZEROS true
177-
# elif defined(JEMALLOC_PURGE_MADVISE_FREE)
174+
# if defined(JEMALLOC_PURGE_MADVISE_FREE)
178175
# define JEMALLOC_MADV_PURGE MADV_FREE
179176
# define JEMALLOC_MADV_ZEROS false
177+
# elif defined(JEMALLOC_PURGE_MADVISE_DONTNEED)
178+
# define JEMALLOC_MADV_PURGE MADV_DONTNEED
179+
# define JEMALLOC_MADV_ZEROS true
180180
# else
181-
# error "No madvise(2) flag defined for purging unused dirty pages."
181+
# error No madvise(2) flag defined for purging unused dirty pages
182182
# endif
183183
int err = madvise(addr, size, JEMALLOC_MADV_PURGE);
184184
unzeroed = (!JEMALLOC_MADV_ZEROS || err != 0);

0 commit comments

Comments
 (0)