Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
#define JEMALLOC_PAGES_C_
|
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
|
|
|
|
2016-05-06 08:45:02 +08:00
|
|
|
#ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
# define PAGES_PROT_COMMIT (PROT_READ | PROT_WRITE)
|
|
|
|
# define PAGES_PROT_DECOMMIT (PROT_NONE)
|
|
|
|
static int mmap_flags;
|
|
|
|
#endif
|
|
|
|
static bool os_overcommits;
|
|
|
|
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
void *
|
2016-05-06 08:45:02 +08:00
|
|
|
pages_map(void *addr, size_t size, bool *commit)
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
assert(size != 0);
|
|
|
|
|
2016-05-06 08:45:02 +08:00
|
|
|
if (os_overcommits)
|
|
|
|
*commit = true;
|
|
|
|
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
/*
|
|
|
|
* If VirtualAlloc can't allocate at the given address when one is
|
|
|
|
* given, it fails and returns NULL.
|
|
|
|
*/
|
2016-05-06 08:45:02 +08:00
|
|
|
ret = VirtualAlloc(addr, size, MEM_RESERVE | (*commit ? MEM_COMMIT : 0),
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
PAGE_READWRITE);
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* We don't use MAP_FIXED here, because it can cause the *replacement*
|
|
|
|
* of existing mappings, and we only want to create new mappings.
|
|
|
|
*/
|
2016-05-06 08:45:02 +08:00
|
|
|
{
|
|
|
|
int prot = *commit ? PAGES_PROT_COMMIT : PAGES_PROT_DECOMMIT;
|
|
|
|
|
|
|
|
ret = mmap(addr, size, prot, mmap_flags, -1, 0);
|
|
|
|
}
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
assert(ret != NULL);
|
|
|
|
|
|
|
|
if (ret == MAP_FAILED)
|
|
|
|
ret = NULL;
|
|
|
|
else if (addr != NULL && ret != addr) {
|
|
|
|
/*
|
|
|
|
* We succeeded in mapping memory, but not in the right place.
|
|
|
|
*/
|
|
|
|
pages_unmap(ret, size);
|
|
|
|
ret = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
assert(ret == NULL || (addr == NULL && ret != addr)
|
|
|
|
|| (addr != NULL && ret == addr));
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pages_unmap(void *addr, size_t size)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (VirtualFree(addr, 0, MEM_RELEASE) == 0)
|
|
|
|
#else
|
|
|
|
if (munmap(addr, size) == -1)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
char buf[BUFERROR_BUF];
|
|
|
|
|
|
|
|
buferror(get_errno(), buf, sizeof(buf));
|
|
|
|
malloc_printf("<jemalloc>: Error in "
|
|
|
|
#ifdef _WIN32
|
|
|
|
"VirtualFree"
|
|
|
|
#else
|
|
|
|
"munmap"
|
|
|
|
#endif
|
|
|
|
"(): %s\n", buf);
|
|
|
|
if (opt_abort)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2016-05-06 08:45:02 +08:00
|
|
|
pages_trim(void *addr, size_t alloc_size, size_t leadsize, size_t size,
|
|
|
|
bool *commit)
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
{
|
|
|
|
void *ret = (void *)((uintptr_t)addr + leadsize);
|
|
|
|
|
|
|
|
assert(alloc_size >= leadsize + size);
|
|
|
|
#ifdef _WIN32
|
|
|
|
{
|
|
|
|
void *new_addr;
|
|
|
|
|
|
|
|
pages_unmap(addr, alloc_size);
|
2016-05-06 08:45:02 +08:00
|
|
|
new_addr = pages_map(ret, size, commit);
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
if (new_addr == ret)
|
|
|
|
return (ret);
|
|
|
|
if (new_addr)
|
|
|
|
pages_unmap(new_addr, size);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
size_t trailsize = alloc_size - leadsize - size;
|
|
|
|
|
|
|
|
if (leadsize != 0)
|
|
|
|
pages_unmap(addr, leadsize);
|
|
|
|
if (trailsize != 0)
|
|
|
|
pages_unmap((void *)((uintptr_t)ret + size), trailsize);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
pages_commit_impl(void *addr, size_t size, bool commit)
|
|
|
|
{
|
|
|
|
|
2016-05-06 08:45:02 +08:00
|
|
|
if (os_overcommits)
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
return (commit ? (addr != VirtualAlloc(addr, size, MEM_COMMIT,
|
|
|
|
PAGE_READWRITE)) : (!VirtualFree(addr, size, MEM_DECOMMIT)));
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
int prot = commit ? PAGES_PROT_COMMIT : PAGES_PROT_DECOMMIT;
|
|
|
|
void *result = mmap(addr, size, prot, mmap_flags | MAP_FIXED,
|
|
|
|
-1, 0);
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
if (result == MAP_FAILED)
|
|
|
|
return (true);
|
|
|
|
if (result != addr) {
|
|
|
|
/*
|
|
|
|
* We succeeded in mapping memory, but not in the right
|
|
|
|
* place.
|
|
|
|
*/
|
|
|
|
pages_unmap(result, size);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
pages_commit(void *addr, size_t size)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (pages_commit_impl(addr, size, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
pages_decommit(void *addr, size_t size)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (pages_commit_impl(addr, size, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
pages_purge(void *addr, size_t size)
|
|
|
|
{
|
|
|
|
bool unzeroed;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
VirtualAlloc(addr, size, MEM_RESET, PAGE_READWRITE);
|
|
|
|
unzeroed = true;
|
|
|
|
#elif defined(JEMALLOC_HAVE_MADVISE)
|
2016-11-18 02:24:51 +08:00
|
|
|
# if defined(JEMALLOC_PURGE_MADVISE_FREE)
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
# define JEMALLOC_MADV_PURGE MADV_FREE
|
|
|
|
# define JEMALLOC_MADV_ZEROS false
|
2016-11-18 02:24:51 +08:00
|
|
|
# elif defined(JEMALLOC_PURGE_MADVISE_DONTNEED)
|
|
|
|
# define JEMALLOC_MADV_PURGE MADV_DONTNEED
|
|
|
|
# define JEMALLOC_MADV_ZEROS true
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
# else
|
2016-11-18 02:24:51 +08:00
|
|
|
# error No madvise(2) flag defined for purging unused dirty pages
|
Generalize chunk management hooks.
Add the "arena.<i>.chunk_hooks" mallctl, which replaces and expands on
the "arena.<i>.chunk.{alloc,dalloc,purge}" mallctls. The chunk hooks
allow control over chunk allocation/deallocation, decommit/commit,
purging, and splitting/merging, such that the application can rely on
jemalloc's internal chunk caching and retaining functionality, yet
implement a variety of chunk management mechanisms and policies.
Merge the chunks_[sz]ad_{mmap,dss} red-black trees into
chunks_[sz]ad_retained. This slightly reduces how hard jemalloc tries
to honor the dss precedence setting; prior to this change the precedence
setting was also consulted when recycling chunks.
Fix chunk purging. Don't purge chunks in arena_purge_stashed(); instead
deallocate them in arena_unstash_purged(), so that the dirty memory
linkage remains valid until after the last time it is used.
This resolves #176 and #201.
2015-07-28 23:28:19 +08:00
|
|
|
# endif
|
|
|
|
int err = madvise(addr, size, JEMALLOC_MADV_PURGE);
|
|
|
|
unzeroed = (!JEMALLOC_MADV_ZEROS || err != 0);
|
|
|
|
# undef JEMALLOC_MADV_PURGE
|
|
|
|
# undef JEMALLOC_MADV_ZEROS
|
|
|
|
#else
|
|
|
|
/* Last resort no-op. */
|
|
|
|
unzeroed = true;
|
|
|
|
#endif
|
|
|
|
return (unzeroed);
|
|
|
|
}
|
|
|
|
|
2016-05-06 08:45:02 +08:00
|
|
|
#ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT
|
|
|
|
static bool
|
|
|
|
os_overcommits_sysctl(void)
|
|
|
|
{
|
|
|
|
int vm_overcommit;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
sz = sizeof(vm_overcommit);
|
|
|
|
if (sysctlbyname("vm.overcommit", &vm_overcommit, &sz, NULL, 0) != 0)
|
|
|
|
return (false); /* Error. */
|
|
|
|
|
|
|
|
return ((vm_overcommit & 0x3) == 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY
|
2016-10-30 13:41:04 +08:00
|
|
|
/*
|
|
|
|
* Use syscall(2) rather than {open,read,close}(2) when possible to avoid
|
|
|
|
* reentry during bootstrapping if another library has interposed system call
|
|
|
|
* wrappers.
|
|
|
|
*/
|
2016-05-06 08:45:02 +08:00
|
|
|
static bool
|
|
|
|
os_overcommits_proc(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char buf[1];
|
|
|
|
ssize_t nread;
|
|
|
|
|
2016-11-03 10:18:33 +08:00
|
|
|
#if defined(JEMALLOC_HAVE_SYSCALL) && defined(SYS_open)
|
2016-10-30 13:41:04 +08:00
|
|
|
fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY);
|
|
|
|
#else
|
2016-05-06 08:45:02 +08:00
|
|
|
fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY);
|
2016-10-30 13:41:04 +08:00
|
|
|
#endif
|
2016-05-06 08:45:02 +08:00
|
|
|
if (fd == -1)
|
|
|
|
return (false); /* Error. */
|
|
|
|
|
2016-11-03 10:18:33 +08:00
|
|
|
#if defined(JEMALLOC_HAVE_SYSCALL) && defined(SYS_read)
|
2016-10-30 13:41:04 +08:00
|
|
|
nread = (ssize_t)syscall(SYS_read, fd, &buf, sizeof(buf));
|
|
|
|
#else
|
2016-05-06 08:45:02 +08:00
|
|
|
nread = read(fd, &buf, sizeof(buf));
|
2016-10-30 13:41:04 +08:00
|
|
|
#endif
|
|
|
|
|
2016-11-03 10:18:33 +08:00
|
|
|
#if defined(JEMALLOC_HAVE_SYSCALL) && defined(SYS_close)
|
2016-10-30 13:41:04 +08:00
|
|
|
syscall(SYS_close, fd);
|
|
|
|
#else
|
2016-09-27 06:55:40 +08:00
|
|
|
close(fd);
|
2016-10-30 13:41:04 +08:00
|
|
|
#endif
|
|
|
|
|
2016-05-06 08:45:02 +08:00
|
|
|
if (nread < 1)
|
|
|
|
return (false); /* Error. */
|
|
|
|
/*
|
|
|
|
* /proc/sys/vm/overcommit_memory meanings:
|
|
|
|
* 0: Heuristic overcommit.
|
|
|
|
* 1: Always overcommit.
|
|
|
|
* 2: Never overcommit.
|
|
|
|
*/
|
|
|
|
return (buf[0] == '0' || buf[0] == '1');
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
pages_boot(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
mmap_flags = MAP_PRIVATE | MAP_ANON;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT
|
|
|
|
os_overcommits = os_overcommits_sysctl();
|
|
|
|
#elif defined(JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY)
|
|
|
|
os_overcommits = os_overcommits_proc();
|
|
|
|
# ifdef MAP_NORESERVE
|
|
|
|
if (os_overcommits)
|
|
|
|
mmap_flags |= MAP_NORESERVE;
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
os_overcommits = false;
|
|
|
|
#endif
|
|
|
|
}
|