2010-01-24 18:53:40 +08:00
|
|
|
#define JEMALLOC_CHUNK_DSS_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
2012-10-12 04:53:15 +08:00
|
|
|
const char *dss_prec_names[] = {
|
|
|
|
"disabled",
|
|
|
|
"primary",
|
|
|
|
"secondary",
|
|
|
|
"N/A"
|
|
|
|
};
|
|
|
|
|
2012-03-14 07:31:41 +08:00
|
|
|
/*
|
2016-10-14 03:18:38 +08:00
|
|
|
* Current dss precedence default, used when creating new arenas. NB: This is
|
|
|
|
* stored as unsigned rather than dss_prec_t because in principle there's no
|
|
|
|
* guarantee that sizeof(dss_prec_t) is the same as sizeof(unsigned), and we use
|
|
|
|
* atomic operations to synchronize the setting.
|
2012-03-14 07:31:41 +08:00
|
|
|
*/
|
2016-10-14 03:18:38 +08:00
|
|
|
static unsigned dss_prec_default = (unsigned)DSS_PREC_DEFAULT;
|
2010-01-24 18:53:40 +08:00
|
|
|
|
|
|
|
/* Base address of the DSS. */
|
2012-03-14 07:31:41 +08:00
|
|
|
static void *dss_base;
|
2016-10-14 03:18:38 +08:00
|
|
|
/* Atomic boolean indicating whether the DSS is exhausted. */
|
|
|
|
static unsigned dss_exhausted;
|
|
|
|
/* Atomic current upper limit on DSS addresses. */
|
2012-03-14 07:31:41 +08:00
|
|
|
static void *dss_max;
|
2010-01-24 18:53:40 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static void *
|
2013-12-04 13:49:36 +08:00
|
|
|
chunk_dss_sbrk(intptr_t increment)
|
2010-01-24 18:53:40 +08:00
|
|
|
{
|
|
|
|
|
2014-04-16 03:09:48 +08:00
|
|
|
#ifdef JEMALLOC_DSS
|
2013-12-04 13:49:36 +08:00
|
|
|
return (sbrk(increment));
|
|
|
|
#else
|
2012-04-13 11:20:58 +08:00
|
|
|
not_implemented();
|
|
|
|
return (NULL);
|
|
|
|
#endif
|
2013-12-04 13:49:36 +08:00
|
|
|
}
|
2010-01-24 18:53:40 +08:00
|
|
|
|
2012-10-12 04:53:15 +08:00
|
|
|
dss_prec_t
|
2016-10-14 03:18:38 +08:00
|
|
|
chunk_dss_prec_get(void)
|
2012-10-12 04:53:15 +08:00
|
|
|
{
|
|
|
|
dss_prec_t ret;
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!have_dss)
|
2012-10-12 04:53:15 +08:00
|
|
|
return (dss_prec_disabled);
|
2016-10-14 03:18:38 +08:00
|
|
|
ret = (dss_prec_t)atomic_read_u(&dss_prec_default);
|
2012-10-12 04:53:15 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-10-14 03:18:38 +08:00
|
|
|
chunk_dss_prec_set(dss_prec_t dss_prec)
|
2012-10-12 04:53:15 +08:00
|
|
|
{
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!have_dss)
|
2014-04-16 03:09:48 +08:00
|
|
|
return (dss_prec != dss_prec_disabled);
|
2016-10-14 03:18:38 +08:00
|
|
|
atomic_write_u(&dss_prec_default, (unsigned)dss_prec);
|
2012-10-12 04:53:15 +08:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
static void *
|
|
|
|
chunk_dss_max_update(void *new_addr)
|
|
|
|
{
|
|
|
|
void *max_cur;
|
|
|
|
spin_t spinner;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the current end of the DSS as max_cur and assure that dss_max is
|
|
|
|
* up to date.
|
|
|
|
*/
|
|
|
|
spin_init(&spinner);
|
|
|
|
while (true) {
|
|
|
|
void *max_prev = atomic_read_p(&dss_max);
|
|
|
|
|
|
|
|
max_cur = chunk_dss_sbrk(0);
|
|
|
|
if ((uintptr_t)max_prev > (uintptr_t)max_cur) {
|
|
|
|
/*
|
|
|
|
* Another thread optimistically updated dss_max. Wait
|
|
|
|
* for it to finish.
|
|
|
|
*/
|
|
|
|
spin_adaptive(&spinner);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!atomic_cas_p(&dss_max, max_prev, max_cur))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Fixed new_addr can only be supported if it is at the edge of DSS. */
|
|
|
|
if (new_addr != NULL && max_cur != new_addr)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
return (max_cur);
|
|
|
|
}
|
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
void *
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_alloc_dss(tsdn_t *tsdn, arena_t *arena, void *new_addr, size_t size,
|
2016-04-14 14:36:15 +08:00
|
|
|
size_t alignment, bool *zero, bool *commit)
|
2010-01-24 18:53:40 +08:00
|
|
|
{
|
2014-04-16 03:09:48 +08:00
|
|
|
cassert(have_dss);
|
2012-04-11 01:50:33 +08:00
|
|
|
assert(size > 0 && (size & chunksize_mask) == 0);
|
|
|
|
assert(alignment > 0 && (alignment & chunksize_mask) == 0);
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/*
|
|
|
|
* sbrk() uses a signed increment argument, so take care not to
|
|
|
|
* interpret a huge allocation request as a negative increment.
|
|
|
|
*/
|
|
|
|
if ((intptr_t)size < 0)
|
|
|
|
return (NULL);
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
if (!atomic_read_u(&dss_exhausted)) {
|
2010-01-24 18:53:40 +08:00
|
|
|
/*
|
|
|
|
* The loop is necessary to recover from races with other
|
|
|
|
* threads that are using the DSS for something other than
|
|
|
|
* malloc.
|
|
|
|
*/
|
2016-10-14 03:18:38 +08:00
|
|
|
while (true) {
|
|
|
|
void *ret, *cpad, *max_cur, *dss_next, *dss_prev;
|
2015-09-08 20:09:20 +08:00
|
|
|
size_t gap_size, cpad_size;
|
|
|
|
intptr_t incr;
|
2014-11-04 03:02:52 +08:00
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
max_cur = chunk_dss_max_update(new_addr);
|
|
|
|
if (max_cur == NULL)
|
|
|
|
goto label_oom;
|
2014-11-04 03:02:52 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/*
|
|
|
|
* Calculate how much padding is necessary to
|
|
|
|
* chunk-align the end of the DSS.
|
|
|
|
*/
|
2012-04-11 01:50:33 +08:00
|
|
|
gap_size = (chunksize - CHUNK_ADDR2OFFSET(dss_max)) &
|
|
|
|
chunksize_mask;
|
|
|
|
/*
|
|
|
|
* Compute how much chunk-aligned pad space (if any) is
|
|
|
|
* necessary to satisfy alignment. This space can be
|
|
|
|
* recycled for later use.
|
|
|
|
*/
|
|
|
|
cpad = (void *)((uintptr_t)dss_max + gap_size);
|
2012-04-12 09:13:45 +08:00
|
|
|
ret = (void *)ALIGNMENT_CEILING((uintptr_t)dss_max,
|
|
|
|
alignment);
|
2012-04-11 01:50:33 +08:00
|
|
|
cpad_size = (uintptr_t)ret - (uintptr_t)cpad;
|
|
|
|
dss_next = (void *)((uintptr_t)ret + size);
|
|
|
|
if ((uintptr_t)ret < (uintptr_t)dss_max ||
|
2016-10-14 03:18:38 +08:00
|
|
|
(uintptr_t)dss_next < (uintptr_t)dss_max)
|
|
|
|
goto label_oom; /* Wrap-around. */
|
2012-04-11 01:50:33 +08:00
|
|
|
incr = gap_size + cpad_size + size;
|
2016-10-14 03:18:38 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Optimistically update dss_max, and roll back below if
|
|
|
|
* sbrk() fails. No other thread will try to extend the
|
|
|
|
* DSS while dss_max is greater than the current DSS
|
|
|
|
* max reported by sbrk(0).
|
|
|
|
*/
|
|
|
|
if (atomic_cas_p(&dss_max, max_cur, dss_next))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Try to allocate. */
|
2013-12-04 13:49:36 +08:00
|
|
|
dss_prev = chunk_dss_sbrk(incr);
|
2016-10-14 03:18:38 +08:00
|
|
|
if (dss_prev == max_cur) {
|
2010-01-24 18:53:40 +08:00
|
|
|
/* Success. */
|
2015-02-16 10:04:46 +08:00
|
|
|
if (cpad_size != 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
|
|
|
chunk_hooks_t chunk_hooks =
|
|
|
|
CHUNK_HOOKS_INITIALIZER;
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_dalloc_wrapper(tsdn, arena,
|
2015-08-05 01:49:46 +08:00
|
|
|
&chunk_hooks, cpad, cpad_size,
|
2016-03-31 09:36:04 +08:00
|
|
|
false, true);
|
2015-02-16 10:04:46 +08:00
|
|
|
}
|
2012-04-22 07:04:51 +08:00
|
|
|
if (*zero) {
|
2014-04-16 07:35:08 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(
|
|
|
|
ret, size);
|
2012-04-22 07:04:51 +08:00
|
|
|
memset(ret, 0, size);
|
|
|
|
}
|
2015-08-13 01:26:54 +08:00
|
|
|
if (!*commit)
|
|
|
|
*commit = pages_decommit(ret, size);
|
2010-01-24 18:53:40 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
/*
|
|
|
|
* Failure, whether due to OOM or a race with a raw
|
|
|
|
* sbrk() call from outside the allocator. Try to roll
|
|
|
|
* back optimistic dss_max update; if rollback fails,
|
|
|
|
* it's due to another caller of this function having
|
|
|
|
* succeeded since this invocation started, in which
|
|
|
|
* case rollback is not necessary.
|
|
|
|
*/
|
|
|
|
atomic_cas_p(&dss_max, dss_next, max_cur);
|
|
|
|
if (dss_prev == (void *)-1) {
|
|
|
|
/* OOM. */
|
|
|
|
atomic_write_u(&dss_exhausted, (unsigned)true);
|
|
|
|
goto label_oom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
label_oom:
|
2010-01-24 18:53:40 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
static bool
|
|
|
|
chunk_in_dss_helper(void *chunk, void *max)
|
2010-12-01 08:50:58 +08:00
|
|
|
{
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
return ((uintptr_t)chunk >= (uintptr_t)dss_base && (uintptr_t)chunk <
|
|
|
|
(uintptr_t)max);
|
2010-12-01 08:50:58 +08:00
|
|
|
}
|
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
bool
|
2016-10-14 03:18:38 +08:00
|
|
|
chunk_in_dss(void *chunk)
|
2010-01-24 18:53:40 +08:00
|
|
|
{
|
|
|
|
|
2014-04-16 03:09:48 +08:00
|
|
|
cassert(have_dss);
|
2012-02-11 12:22:09 +08:00
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
return (chunk_in_dss_helper(chunk, atomic_read_p(&dss_max)));
|
2010-01-24 18:53:40 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
bool
|
|
|
|
chunk_dss_mergeable(void *chunk_a, void *chunk_b)
|
2012-03-14 07:31:41 +08:00
|
|
|
{
|
2016-10-14 03:18:38 +08:00
|
|
|
void *max;
|
2012-03-14 07:31:41 +08:00
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
cassert(have_dss);
|
2012-03-14 07:31:41 +08:00
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
max = atomic_read_p(&dss_max);
|
|
|
|
return (chunk_in_dss_helper(chunk_a, max) ==
|
|
|
|
chunk_in_dss_helper(chunk_b, max));
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-14 03:18:38 +08:00
|
|
|
chunk_dss_boot(void)
|
2012-03-14 07:31:41 +08:00
|
|
|
{
|
|
|
|
|
2016-10-14 03:18:38 +08:00
|
|
|
cassert(have_dss);
|
|
|
|
|
|
|
|
dss_base = chunk_dss_sbrk(0);
|
|
|
|
dss_exhausted = (unsigned)(dss_base == (void *)-1);
|
|
|
|
dss_max = dss_base;
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|