2010-01-17 01:53:50 +08:00
|
|
|
#define JEMALLOC_ARENA_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
purge_mode_t opt_purge = PURGE_DEFAULT;
|
|
|
|
const char *purge_mode_names[] = {
|
|
|
|
"ratio",
|
|
|
|
"decay",
|
|
|
|
"N/A"
|
|
|
|
};
|
2010-01-17 01:53:50 +08:00
|
|
|
ssize_t opt_lg_dirty_mult = LG_DIRTY_MULT_DEFAULT;
|
2015-03-19 09:55:33 +08:00
|
|
|
static ssize_t lg_dirty_mult_default;
|
2016-02-20 12:09:31 +08:00
|
|
|
ssize_t opt_decay_time = DECAY_TIME_DEFAULT;
|
|
|
|
static ssize_t decay_time_default;
|
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
arena_bin_info_t arena_bin_info[NBINS];
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
size_t map_bias;
|
|
|
|
size_t map_misc_offset;
|
|
|
|
size_t arena_maxrun; /* Max run size for arenas. */
|
2015-09-12 11:50:20 +08:00
|
|
|
size_t large_maxclass; /* Max large size class. */
|
2016-02-23 08:20:56 +08:00
|
|
|
size_t run_quantize_max; /* Max run_quantize_*() input. */
|
|
|
|
static size_t small_maxrun; /* Max run size for small size classes. */
|
2015-05-05 00:58:36 +08:00
|
|
|
static bool *small_run_tab; /* Valid small run page multiples. */
|
2016-02-23 08:20:56 +08:00
|
|
|
static size_t *run_quantize_floor_tab; /* run_quantize_floor() memoization. */
|
|
|
|
static size_t *run_quantize_ceil_tab; /* run_quantize_ceil() memoization. */
|
2014-10-13 13:53:59 +08:00
|
|
|
unsigned nlclasses; /* Number of large size classes. */
|
|
|
|
unsigned nhclasses; /* Number of huge size classes. */
|
2016-02-24 04:06:21 +08:00
|
|
|
static szind_t runs_avail_bias; /* Size index for first runs_avail tree. */
|
|
|
|
static szind_t runs_avail_nclasses; /* Number of runs_avail trees. */
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-01-15 08:23:03 +08:00
|
|
|
/*
|
|
|
|
* Function prototypes for static functions that are referenced prior to
|
|
|
|
* definition.
|
|
|
|
*/
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
static void arena_purge_to_limit(tsdn_t *tsdn, arena_t *arena,
|
2016-04-14 14:36:15 +08:00
|
|
|
size_t ndirty_limit);
|
2016-05-11 13:21:10 +08:00
|
|
|
static void arena_run_dalloc(tsdn_t *tsdn, arena_t *arena, arena_run_t *run,
|
2016-04-14 14:36:15 +08:00
|
|
|
bool dirty, bool cleaned, bool decommitted);
|
2016-05-11 13:21:10 +08:00
|
|
|
static void arena_dalloc_bin_run(tsdn_t *tsdn, arena_t *arena,
|
2016-04-14 14:36:15 +08:00
|
|
|
arena_chunk_t *chunk, arena_run_t *run, arena_bin_t *bin);
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
static void arena_bin_lower_run(arena_t *arena, arena_chunk_t *chunk,
|
|
|
|
arena_run_t *run, arena_bin_t *bin);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
JEMALLOC_INLINE_C size_t
|
2015-09-19 04:58:17 +08:00
|
|
|
arena_miscelm_size_get(const arena_chunk_map_misc_t *miscelm)
|
2015-08-05 01:49:46 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
size_t pageind, mapbits;
|
|
|
|
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(miscelm);
|
|
|
|
pageind = arena_miscelm_to_pageind(miscelm);
|
|
|
|
mapbits = arena_mapbits_get(chunk, pageind);
|
2015-08-20 05:12:05 +08:00
|
|
|
return (arena_mapbits_size_decode(mapbits));
|
2014-04-07 04:24:16 +08:00
|
|
|
}
|
|
|
|
|
2016-03-27 08:30:37 +08:00
|
|
|
JEMALLOC_INLINE_C int
|
|
|
|
arena_run_addr_comp(const arena_chunk_map_misc_t *a,
|
|
|
|
const arena_chunk_map_misc_t *b)
|
|
|
|
{
|
|
|
|
uintptr_t a_miscelm = (uintptr_t)a;
|
|
|
|
uintptr_t b_miscelm = (uintptr_t)b;
|
|
|
|
|
|
|
|
assert(a != NULL);
|
|
|
|
assert(b != NULL);
|
|
|
|
|
|
|
|
return ((a_miscelm > b_miscelm) - (a_miscelm < b_miscelm));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate pairing heap functions. */
|
|
|
|
ph_gen(static UNUSED, arena_run_heap_, arena_run_heap_t, arena_chunk_map_misc_t,
|
|
|
|
ph_link, arena_run_addr_comp)
|
|
|
|
|
2015-05-05 00:58:36 +08:00
|
|
|
static size_t
|
2016-02-23 08:20:56 +08:00
|
|
|
run_quantize_floor_compute(size_t size)
|
2015-05-05 00:58:36 +08:00
|
|
|
{
|
|
|
|
size_t qsize;
|
|
|
|
|
|
|
|
assert(size != 0);
|
|
|
|
assert(size == PAGE_CEILING(size));
|
|
|
|
|
|
|
|
/* Don't change sizes that are valid small run sizes. */
|
|
|
|
if (size <= small_maxrun && small_run_tab[size >> LG_PAGE])
|
|
|
|
return (size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Round down to the nearest run size that can actually be requested
|
|
|
|
* during normal large allocation. Add large_pad so that cache index
|
|
|
|
* randomization can offset the allocation from the page boundary.
|
|
|
|
*/
|
|
|
|
qsize = index2size(size2index(size - large_pad + 1) - 1) + large_pad;
|
|
|
|
if (qsize <= SMALL_MAXCLASS + large_pad)
|
2016-02-23 08:20:56 +08:00
|
|
|
return (run_quantize_floor_compute(size - large_pad));
|
2015-05-05 00:58:36 +08:00
|
|
|
assert(qsize <= size);
|
|
|
|
return (qsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2016-02-23 08:20:56 +08:00
|
|
|
run_quantize_ceil_compute_hard(size_t size)
|
2015-05-05 00:58:36 +08:00
|
|
|
{
|
|
|
|
size_t large_run_size_next;
|
|
|
|
|
|
|
|
assert(size != 0);
|
|
|
|
assert(size == PAGE_CEILING(size));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the next quantized size greater than the input size.
|
|
|
|
* Quantized sizes comprise the union of run sizes that back small
|
|
|
|
* region runs, and run sizes that back large regions with no explicit
|
|
|
|
* alignment constraints.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (size > SMALL_MAXCLASS) {
|
|
|
|
large_run_size_next = PAGE_CEILING(index2size(size2index(size -
|
|
|
|
large_pad) + 1) + large_pad);
|
|
|
|
} else
|
|
|
|
large_run_size_next = SIZE_T_MAX;
|
|
|
|
if (size >= small_maxrun)
|
|
|
|
return (large_run_size_next);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
size += PAGE;
|
|
|
|
assert(size <= small_maxrun);
|
|
|
|
if (small_run_tab[size >> LG_PAGE]) {
|
|
|
|
if (large_run_size_next < size)
|
|
|
|
return (large_run_size_next);
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2016-02-23 08:20:56 +08:00
|
|
|
run_quantize_ceil_compute(size_t size)
|
2015-05-05 00:58:36 +08:00
|
|
|
{
|
2016-02-23 08:20:56 +08:00
|
|
|
size_t qsize = run_quantize_floor_compute(size);
|
2015-05-05 00:58:36 +08:00
|
|
|
|
|
|
|
if (qsize < size) {
|
|
|
|
/*
|
|
|
|
* Skip a quantization that may have an adequately large run,
|
|
|
|
* because under-sized runs may be mixed in. This only happens
|
|
|
|
* when an unusual size is requested, i.e. for aligned
|
|
|
|
* allocation, and is just one of several places where linear
|
|
|
|
* search would potentially find sufficiently aligned available
|
|
|
|
* memory somewhere lower.
|
|
|
|
*/
|
2016-02-23 08:20:56 +08:00
|
|
|
qsize = run_quantize_ceil_compute_hard(qsize);
|
2015-05-05 00:58:36 +08:00
|
|
|
}
|
|
|
|
return (qsize);
|
|
|
|
}
|
2016-02-23 08:20:56 +08:00
|
|
|
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef run_quantize_floor
|
2016-04-19 06:11:20 +08:00
|
|
|
#define run_quantize_floor JEMALLOC_N(n_run_quantize_floor)
|
2016-02-23 08:20:56 +08:00
|
|
|
#endif
|
|
|
|
static size_t
|
|
|
|
run_quantize_floor(size_t size)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
assert(size > 0);
|
|
|
|
assert(size <= run_quantize_max);
|
|
|
|
assert((size & PAGE_MASK) == 0);
|
|
|
|
|
|
|
|
ret = run_quantize_floor_tab[(size >> LG_PAGE) - 1];
|
|
|
|
assert(ret == run_quantize_floor_compute(size));
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef run_quantize_floor
|
|
|
|
#define run_quantize_floor JEMALLOC_N(run_quantize_floor)
|
2016-04-19 06:11:20 +08:00
|
|
|
run_quantize_t *run_quantize_floor = JEMALLOC_N(n_run_quantize_floor);
|
2016-02-23 08:20:56 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef run_quantize_ceil
|
2016-04-19 06:11:20 +08:00
|
|
|
#define run_quantize_ceil JEMALLOC_N(n_run_quantize_ceil)
|
2016-02-23 08:20:56 +08:00
|
|
|
#endif
|
|
|
|
static size_t
|
|
|
|
run_quantize_ceil(size_t size)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
assert(size > 0);
|
|
|
|
assert(size <= run_quantize_max);
|
|
|
|
assert((size & PAGE_MASK) == 0);
|
|
|
|
|
|
|
|
ret = run_quantize_ceil_tab[(size >> LG_PAGE) - 1];
|
|
|
|
assert(ret == run_quantize_ceil_compute(size));
|
|
|
|
return (ret);
|
|
|
|
}
|
2016-02-23 06:58:05 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef run_quantize_ceil
|
|
|
|
#define run_quantize_ceil JEMALLOC_N(run_quantize_ceil)
|
2016-04-19 06:11:20 +08:00
|
|
|
run_quantize_t *run_quantize_ceil = JEMALLOC_N(n_run_quantize_ceil);
|
2016-02-23 06:58:05 +08:00
|
|
|
#endif
|
2015-05-05 00:58:36 +08:00
|
|
|
|
2016-03-27 08:30:37 +08:00
|
|
|
static arena_run_heap_t *
|
2016-02-24 04:06:21 +08:00
|
|
|
arena_runs_avail_get(arena_t *arena, szind_t ind)
|
|
|
|
{
|
2015-03-07 09:14:05 +08:00
|
|
|
|
2016-02-24 04:06:21 +08:00
|
|
|
assert(ind >= runs_avail_bias);
|
|
|
|
assert(ind - runs_avail_bias < runs_avail_nclasses);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-02-24 04:06:21 +08:00
|
|
|
return (&arena->runs_avail[ind - runs_avail_bias]);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2012-10-31 06:42:37 +08:00
|
|
|
static void
|
|
|
|
arena_avail_insert(arena_t *arena, arena_chunk_t *chunk, size_t pageind,
|
2014-07-22 10:39:20 +08:00
|
|
|
size_t npages)
|
2012-10-31 06:42:37 +08:00
|
|
|
{
|
2016-02-24 04:06:21 +08:00
|
|
|
szind_t ind = size2index(run_quantize_floor(arena_miscelm_size_get(
|
2016-03-24 07:04:38 +08:00
|
|
|
arena_miscelm_get_const(chunk, pageind))));
|
2012-10-31 06:42:37 +08:00
|
|
|
assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >>
|
|
|
|
LG_PAGE));
|
2016-03-27 08:30:37 +08:00
|
|
|
arena_run_heap_insert(arena_runs_avail_get(arena, ind),
|
|
|
|
arena_miscelm_get_mutable(chunk, pageind));
|
2012-10-31 06:42:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
arena_avail_remove(arena_t *arena, arena_chunk_t *chunk, size_t pageind,
|
2014-07-22 10:39:20 +08:00
|
|
|
size_t npages)
|
2012-10-31 06:42:37 +08:00
|
|
|
{
|
2016-02-24 04:06:21 +08:00
|
|
|
szind_t ind = size2index(run_quantize_floor(arena_miscelm_size_get(
|
2016-03-24 07:04:38 +08:00
|
|
|
arena_miscelm_get_const(chunk, pageind))));
|
2012-10-31 06:42:37 +08:00
|
|
|
assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >>
|
|
|
|
LG_PAGE));
|
2016-03-27 08:30:37 +08:00
|
|
|
arena_run_heap_remove(arena_runs_avail_get(arena, ind),
|
|
|
|
arena_miscelm_get_mutable(chunk, pageind));
|
2012-10-31 06:42:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 05:45:58 +08:00
|
|
|
static void
|
2015-02-16 10:04:46 +08:00
|
|
|
arena_run_dirty_insert(arena_t *arena, arena_chunk_t *chunk, size_t pageind,
|
2014-08-15 05:45:58 +08:00
|
|
|
size_t npages)
|
|
|
|
{
|
2016-03-24 07:04:38 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm = arena_miscelm_get_mutable(chunk,
|
|
|
|
pageind);
|
2015-02-16 10:04:46 +08:00
|
|
|
|
2014-08-15 05:45:58 +08:00
|
|
|
assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >>
|
|
|
|
LG_PAGE));
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, pageind) == CHUNK_MAP_DIRTY);
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, pageind+npages-1) ==
|
|
|
|
CHUNK_MAP_DIRTY);
|
2015-02-16 10:04:46 +08:00
|
|
|
|
2016-03-08 17:04:48 +08:00
|
|
|
qr_new(&miscelm->rd, rd_link);
|
|
|
|
qr_meld(&arena->runs_dirty, &miscelm->rd, rd_link);
|
2014-08-15 05:45:58 +08:00
|
|
|
arena->ndirty += npages;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-02-16 10:04:46 +08:00
|
|
|
arena_run_dirty_remove(arena_t *arena, arena_chunk_t *chunk, size_t pageind,
|
2014-08-15 05:45:58 +08:00
|
|
|
size_t npages)
|
|
|
|
{
|
2016-03-24 07:04:38 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm = arena_miscelm_get_mutable(chunk,
|
|
|
|
pageind);
|
2015-02-16 10:04:46 +08:00
|
|
|
|
2014-08-15 05:45:58 +08:00
|
|
|
assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >>
|
|
|
|
LG_PAGE));
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, pageind) == CHUNK_MAP_DIRTY);
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, pageind+npages-1) ==
|
|
|
|
CHUNK_MAP_DIRTY);
|
2015-02-16 10:04:46 +08:00
|
|
|
|
2016-03-08 17:04:48 +08:00
|
|
|
qr_remove(&miscelm->rd, rd_link);
|
2015-02-16 10:04:46 +08:00
|
|
|
assert(arena->ndirty >= npages);
|
2014-08-15 05:45:58 +08:00
|
|
|
arena->ndirty -= npages;
|
|
|
|
}
|
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
static size_t
|
|
|
|
arena_chunk_dirty_npages(const extent_node_t *node)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (extent_node_size_get(node) >> LG_PAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-18 17:15:50 +08:00
|
|
|
arena_chunk_cache_maybe_insert(arena_t *arena, extent_node_t *node, bool cache)
|
2015-02-16 10:04:46 +08:00
|
|
|
{
|
|
|
|
|
2015-02-18 17:15:50 +08:00
|
|
|
if (cache) {
|
2015-02-18 14:23:10 +08:00
|
|
|
extent_node_dirty_linkage_init(node);
|
2015-02-18 17:15:50 +08:00
|
|
|
extent_node_dirty_insert(node, &arena->runs_dirty,
|
|
|
|
&arena->chunks_cache);
|
2015-02-16 10:04:46 +08:00
|
|
|
arena->ndirty += arena_chunk_dirty_npages(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-18 17:15:50 +08:00
|
|
|
arena_chunk_cache_maybe_remove(arena_t *arena, extent_node_t *node, bool dirty)
|
2015-02-16 10:04:46 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if (dirty) {
|
2015-02-18 17:15:50 +08:00
|
|
|
extent_node_dirty_remove(node);
|
2015-02-16 10:04:46 +08:00
|
|
|
assert(arena->ndirty >= arena_chunk_dirty_npages(node));
|
|
|
|
arena->ndirty -= arena_chunk_dirty_npages(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 07:38:08 +08:00
|
|
|
JEMALLOC_INLINE_C void *
|
2011-03-16 04:59:15 +08:00
|
|
|
arena_run_reg_alloc(arena_run_t *run, arena_bin_info_t *bin_info)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
2016-02-26 12:51:00 +08:00
|
|
|
size_t regind;
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm;
|
|
|
|
void *rpages;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2010-03-14 05:41:58 +08:00
|
|
|
assert(run->nfree > 0);
|
2014-10-04 01:16:09 +08:00
|
|
|
assert(!bitmap_full(run->bitmap, &bin_info->bitmap_info));
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-02-25 04:42:23 +08:00
|
|
|
regind = (unsigned)bitmap_sfu(run->bitmap, &bin_info->bitmap_info);
|
2014-09-29 16:31:39 +08:00
|
|
|
miscelm = arena_run_to_miscelm(run);
|
|
|
|
rpages = arena_miscelm_to_rpages(miscelm);
|
|
|
|
ret = (void *)((uintptr_t)rpages + (uintptr_t)bin_info->reg0_offset +
|
2012-04-06 15:35:09 +08:00
|
|
|
(uintptr_t)(bin_info->reg_interval * regind));
|
2010-03-14 05:41:58 +08:00
|
|
|
run->nfree--;
|
|
|
|
return (ret);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2014-10-31 07:38:08 +08:00
|
|
|
JEMALLOC_INLINE_C void
|
2010-03-14 05:41:58 +08:00
|
|
|
arena_run_reg_dalloc(arena_run_t *run, void *ptr)
|
2010-02-11 02:37:56 +08:00
|
|
|
{
|
2011-03-16 04:59:15 +08:00
|
|
|
arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
|
2012-05-02 15:30:36 +08:00
|
|
|
size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
|
|
|
|
size_t mapbits = arena_mapbits_get(chunk, pageind);
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t binind = arena_ptr_small_binind_get(ptr, mapbits);
|
2011-03-16 04:59:15 +08:00
|
|
|
arena_bin_info_t *bin_info = &arena_bin_info[binind];
|
2016-02-26 12:51:00 +08:00
|
|
|
size_t regind = arena_run_regind(run, bin_info, ptr);
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
|
2011-03-16 04:59:15 +08:00
|
|
|
assert(run->nfree < bin_info->nregs);
|
2010-03-14 05:41:58 +08:00
|
|
|
/* Freeing an interior pointer can cause assertion failure. */
|
2014-09-29 16:31:39 +08:00
|
|
|
assert(((uintptr_t)ptr -
|
|
|
|
((uintptr_t)arena_miscelm_to_rpages(arena_run_to_miscelm(run)) +
|
2012-04-06 15:35:09 +08:00
|
|
|
(uintptr_t)bin_info->reg0_offset)) %
|
|
|
|
(uintptr_t)bin_info->reg_interval == 0);
|
2014-09-29 16:31:39 +08:00
|
|
|
assert((uintptr_t)ptr >=
|
|
|
|
(uintptr_t)arena_miscelm_to_rpages(arena_run_to_miscelm(run)) +
|
2011-03-16 04:59:15 +08:00
|
|
|
(uintptr_t)bin_info->reg0_offset);
|
Use bitmaps to track small regions.
The previous free list implementation, which embedded singly linked
lists in available regions, had the unfortunate side effect of causing
many cache misses during thread cache fills. Fix this in two places:
- arena_run_t: Use a new bitmap implementation to track which regions
are available. Furthermore, revert to preferring the
lowest available region (as jemalloc did with its old
bitmap-based approach).
- tcache_t: Move read-only tcache_bin_t metadata into
tcache_bin_info_t, and add a contiguous array of pointers
to tcache_t in order to track cached objects. This
substantially increases the size of tcache_t, but results
in much higher data locality for common tcache operations.
As a side benefit, it is again possible to efficiently
flush the least recently used cached objects, so this
change changes flushing from MRU to LRU.
The new bitmap implementation uses a multi-level summary approach to
make finding the lowest available region very fast. In practice,
bitmaps only have one or two levels, though the implementation is
general enough to handle extremely large bitmaps, mainly so that large
page sizes can still be entertained.
Fix tcache_bin_flush_large() to always flush statistics, in the same way
that tcache_bin_flush_small() was recently fixed.
Use JEMALLOC_DEBUG rather than NDEBUG.
Add dassert(), and use it for debug-only asserts.
2011-03-17 01:30:13 +08:00
|
|
|
/* Freeing an unallocated pointer can cause assertion failure. */
|
2014-09-29 16:31:39 +08:00
|
|
|
assert(bitmap_get(run->bitmap, &bin_info->bitmap_info, regind));
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-09-29 16:31:39 +08:00
|
|
|
bitmap_unset(run->bitmap, &bin_info->bitmap_info, regind);
|
2010-03-14 05:41:58 +08:00
|
|
|
run->nfree++;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-31 07:38:08 +08:00
|
|
|
JEMALLOC_INLINE_C void
|
2013-01-22 12:04:42 +08:00
|
|
|
arena_run_zero(arena_chunk_t *chunk, size_t run_ind, size_t npages)
|
|
|
|
{
|
|
|
|
|
2014-04-16 07:35:08 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED((void *)((uintptr_t)chunk +
|
|
|
|
(run_ind << LG_PAGE)), (npages << LG_PAGE));
|
2013-01-22 12:04:42 +08:00
|
|
|
memset((void *)((uintptr_t)chunk + (run_ind << LG_PAGE)), 0,
|
|
|
|
(npages << LG_PAGE));
|
|
|
|
}
|
|
|
|
|
2014-10-31 07:38:08 +08:00
|
|
|
JEMALLOC_INLINE_C void
|
2013-10-20 14:48:40 +08:00
|
|
|
arena_run_page_mark_zeroed(arena_chunk_t *chunk, size_t run_ind)
|
|
|
|
{
|
|
|
|
|
2014-04-16 07:35:08 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_DEFINED((void *)((uintptr_t)chunk + (run_ind
|
|
|
|
<< LG_PAGE)), PAGE);
|
2013-10-20 14:48:40 +08:00
|
|
|
}
|
|
|
|
|
2014-10-31 07:38:08 +08:00
|
|
|
JEMALLOC_INLINE_C void
|
2013-01-22 12:04:42 +08:00
|
|
|
arena_run_page_validate_zeroed(arena_chunk_t *chunk, size_t run_ind)
|
2010-10-19 08:45:40 +08:00
|
|
|
{
|
2010-10-25 11:08:37 +08:00
|
|
|
size_t i;
|
2012-04-02 22:04:34 +08:00
|
|
|
UNUSED size_t *p = (size_t *)((uintptr_t)chunk + (run_ind << LG_PAGE));
|
2010-10-25 11:08:37 +08:00
|
|
|
|
2013-10-20 14:48:40 +08:00
|
|
|
arena_run_page_mark_zeroed(chunk, run_ind);
|
2012-04-02 22:04:34 +08:00
|
|
|
for (i = 0; i < PAGE / sizeof(size_t); i++)
|
2010-10-19 08:45:40 +08:00
|
|
|
assert(p[i] == 0);
|
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
static void
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_add(arena_t *arena, size_t add_pages)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
if (config_stats) {
|
2016-02-27 09:29:35 +08:00
|
|
|
size_t cactive_add = CHUNK_CEILING((arena->nactive +
|
|
|
|
add_pages) << LG_PAGE) - CHUNK_CEILING(arena->nactive <<
|
2014-08-07 14:38:39 +08:00
|
|
|
LG_PAGE);
|
2016-02-27 09:29:35 +08:00
|
|
|
if (cactive_add != 0)
|
|
|
|
stats_cactive_add(cactive_add);
|
|
|
|
}
|
2016-02-28 04:34:50 +08:00
|
|
|
arena->nactive += add_pages;
|
2016-02-27 09:29:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_sub(arena_t *arena, size_t sub_pages)
|
2016-02-27 09:29:35 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if (config_stats) {
|
|
|
|
size_t cactive_sub = CHUNK_CEILING(arena->nactive << LG_PAGE) -
|
|
|
|
CHUNK_CEILING((arena->nactive - sub_pages) << LG_PAGE);
|
|
|
|
if (cactive_sub != 0)
|
|
|
|
stats_cactive_sub(cactive_sub);
|
2014-01-15 08:23:03 +08:00
|
|
|
}
|
2016-02-28 04:34:50 +08:00
|
|
|
arena->nactive -= sub_pages;
|
2014-01-15 08:23:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
arena_run_split_remove(arena_t *arena, arena_chunk_t *chunk, size_t run_ind,
|
2015-08-05 01:49:46 +08:00
|
|
|
size_t flag_dirty, size_t flag_decommitted, size_t need_pages)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
|
|
|
size_t total_pages, rem_pages;
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
assert(flag_dirty == 0 || flag_decommitted == 0);
|
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
total_pages = arena_mapbits_unallocated_size_get(chunk, run_ind) >>
|
|
|
|
LG_PAGE;
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, run_ind+total_pages-1) ==
|
|
|
|
flag_dirty);
|
|
|
|
assert(need_pages <= total_pages);
|
|
|
|
rem_pages = total_pages - need_pages;
|
|
|
|
|
2014-07-22 10:39:20 +08:00
|
|
|
arena_avail_remove(arena, chunk, run_ind, total_pages);
|
2014-08-15 05:45:58 +08:00
|
|
|
if (flag_dirty != 0)
|
2015-02-16 10:04:46 +08:00
|
|
|
arena_run_dirty_remove(arena, chunk, run_ind, total_pages);
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_add(arena, need_pages);
|
2014-01-15 08:23:03 +08:00
|
|
|
|
|
|
|
/* Keep track of trailing unused pages for later use. */
|
|
|
|
if (rem_pages > 0) {
|
2015-08-05 01:49:46 +08:00
|
|
|
size_t flags = flag_dirty | flag_decommitted;
|
2015-08-12 03:42:33 +08:00
|
|
|
size_t flag_unzeroed_mask = (flags == 0) ? CHUNK_MAP_UNZEROED :
|
|
|
|
0;
|
|
|
|
|
|
|
|
arena_mapbits_unallocated_set(chunk, run_ind+need_pages,
|
|
|
|
(rem_pages << LG_PAGE), flags |
|
|
|
|
(arena_mapbits_unzeroed_get(chunk, run_ind+need_pages) &
|
|
|
|
flag_unzeroed_mask));
|
|
|
|
arena_mapbits_unallocated_set(chunk, run_ind+total_pages-1,
|
|
|
|
(rem_pages << LG_PAGE), flags |
|
|
|
|
(arena_mapbits_unzeroed_get(chunk, run_ind+total_pages-1) &
|
|
|
|
flag_unzeroed_mask));
|
|
|
|
if (flag_dirty != 0) {
|
|
|
|
arena_run_dirty_insert(arena, chunk, run_ind+need_pages,
|
|
|
|
rem_pages);
|
2014-01-15 08:23:03 +08:00
|
|
|
}
|
2014-07-22 10:39:20 +08:00
|
|
|
arena_avail_insert(arena, chunk, run_ind+need_pages, rem_pages);
|
2014-01-15 08:23:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
static bool
|
2014-01-15 08:23:03 +08:00
|
|
|
arena_run_split_large_helper(arena_t *arena, arena_run_t *run, size_t size,
|
|
|
|
bool remove, bool zero)
|
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm;
|
2015-09-04 18:15:28 +08:00
|
|
|
size_t flag_dirty, flag_decommitted, run_ind, need_pages;
|
2015-08-12 03:42:33 +08:00
|
|
|
size_t flag_unzeroed_mask;
|
2012-05-02 15:30:36 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
|
2014-09-29 16:31:39 +08:00
|
|
|
miscelm = arena_run_to_miscelm(run);
|
|
|
|
run_ind = arena_miscelm_to_pageind(miscelm);
|
2012-05-02 15:30:36 +08:00
|
|
|
flag_dirty = arena_mapbits_dirty_get(chunk, run_ind);
|
2015-08-05 01:49:46 +08:00
|
|
|
flag_decommitted = arena_mapbits_decommitted_get(chunk, run_ind);
|
2012-04-02 22:04:34 +08:00
|
|
|
need_pages = (size >> LG_PAGE);
|
2010-01-17 01:53:50 +08:00
|
|
|
assert(need_pages > 0);
|
|
|
|
|
2015-08-10 07:47:27 +08:00
|
|
|
if (flag_decommitted != 0 && arena->chunk_hooks.commit(chunk, chunksize,
|
|
|
|
run_ind << LG_PAGE, size, arena->ind))
|
2015-08-05 01:49:46 +08:00
|
|
|
return (true);
|
|
|
|
|
2013-10-30 09:17:42 +08:00
|
|
|
if (remove) {
|
2014-01-15 08:23:03 +08:00
|
|
|
arena_run_split_remove(arena, chunk, run_ind, flag_dirty,
|
2015-08-05 01:49:46 +08:00
|
|
|
flag_decommitted, need_pages);
|
2014-01-15 08:23:03 +08:00
|
|
|
}
|
2013-10-30 09:17:42 +08:00
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
if (zero) {
|
2015-08-12 03:42:33 +08:00
|
|
|
if (flag_decommitted != 0) {
|
|
|
|
/* The run is untouched, and therefore zeroed. */
|
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_DEFINED((void
|
|
|
|
*)((uintptr_t)chunk + (run_ind << LG_PAGE)),
|
|
|
|
(need_pages << LG_PAGE));
|
|
|
|
} else if (flag_dirty != 0) {
|
|
|
|
/* The run is dirty, so all pages must be zeroed. */
|
|
|
|
arena_run_zero(chunk, run_ind, need_pages);
|
|
|
|
} else {
|
2013-10-30 09:17:42 +08:00
|
|
|
/*
|
2014-01-15 08:23:03 +08:00
|
|
|
* The run is clean, so some pages may be zeroed (i.e.
|
|
|
|
* never before touched).
|
2013-10-30 09:17:42 +08:00
|
|
|
*/
|
2015-09-04 18:15:28 +08:00
|
|
|
size_t i;
|
2014-01-15 08:23:03 +08:00
|
|
|
for (i = 0; i < need_pages; i++) {
|
|
|
|
if (arena_mapbits_unzeroed_get(chunk, run_ind+i)
|
|
|
|
!= 0)
|
|
|
|
arena_run_zero(chunk, run_ind+i, 1);
|
|
|
|
else if (config_debug) {
|
|
|
|
arena_run_page_validate_zeroed(chunk,
|
|
|
|
run_ind+i);
|
|
|
|
} else {
|
|
|
|
arena_run_page_mark_zeroed(chunk,
|
|
|
|
run_ind+i);
|
2010-03-19 11:36:40 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
}
|
2010-03-19 11:36:40 +08:00
|
|
|
} else {
|
2014-04-16 07:35:08 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED((void *)((uintptr_t)chunk +
|
2013-10-20 14:48:40 +08:00
|
|
|
(run_ind << LG_PAGE)), (need_pages << LG_PAGE));
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2014-01-15 08:23:03 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the last element first, in case the run only contains one page
|
|
|
|
* (i.e. both statements set the same element).
|
|
|
|
*/
|
2015-08-12 03:42:33 +08:00
|
|
|
flag_unzeroed_mask = (flag_dirty | flag_decommitted) == 0 ?
|
|
|
|
CHUNK_MAP_UNZEROED : 0;
|
|
|
|
arena_mapbits_large_set(chunk, run_ind+need_pages-1, 0, flag_dirty |
|
|
|
|
(flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk,
|
|
|
|
run_ind+need_pages-1)));
|
|
|
|
arena_mapbits_large_set(chunk, run_ind, size, flag_dirty |
|
|
|
|
(flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, run_ind)));
|
2015-08-05 01:49:46 +08:00
|
|
|
return (false);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
static bool
|
2014-01-15 08:23:03 +08:00
|
|
|
arena_run_split_large(arena_t *arena, arena_run_t *run, size_t size, bool zero)
|
2013-10-30 09:17:42 +08:00
|
|
|
{
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
return (arena_run_split_large_helper(arena, run, size, true, zero));
|
2013-10-30 09:17:42 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
static bool
|
2014-01-15 08:23:03 +08:00
|
|
|
arena_run_init_large(arena_t *arena, arena_run_t *run, size_t size, bool zero)
|
2013-10-30 09:17:42 +08:00
|
|
|
{
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
return (arena_run_split_large_helper(arena, run, size, false, zero));
|
2014-01-15 08:23:03 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
static bool
|
2014-01-15 08:23:03 +08:00
|
|
|
arena_run_split_small(arena_t *arena, arena_run_t *run, size_t size,
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t binind)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm;
|
2015-08-05 01:49:46 +08:00
|
|
|
size_t flag_dirty, flag_decommitted, run_ind, need_pages, i;
|
2014-01-15 08:23:03 +08:00
|
|
|
|
|
|
|
assert(binind != BININD_INVALID);
|
|
|
|
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
|
2014-09-29 16:31:39 +08:00
|
|
|
miscelm = arena_run_to_miscelm(run);
|
|
|
|
run_ind = arena_miscelm_to_pageind(miscelm);
|
2014-01-15 08:23:03 +08:00
|
|
|
flag_dirty = arena_mapbits_dirty_get(chunk, run_ind);
|
2015-08-05 01:49:46 +08:00
|
|
|
flag_decommitted = arena_mapbits_decommitted_get(chunk, run_ind);
|
2014-01-15 08:23:03 +08:00
|
|
|
need_pages = (size >> LG_PAGE);
|
|
|
|
assert(need_pages > 0);
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
if (flag_decommitted != 0 && arena->chunk_hooks.commit(chunk, chunksize,
|
|
|
|
run_ind << LG_PAGE, size, arena->ind))
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
arena_run_split_remove(arena, chunk, run_ind, flag_dirty,
|
|
|
|
flag_decommitted, need_pages);
|
2014-01-15 08:23:03 +08:00
|
|
|
|
2014-10-11 14:01:03 +08:00
|
|
|
for (i = 0; i < need_pages; i++) {
|
2015-08-12 03:42:33 +08:00
|
|
|
size_t flag_unzeroed = arena_mapbits_unzeroed_get(chunk,
|
|
|
|
run_ind+i);
|
|
|
|
arena_mapbits_small_set(chunk, run_ind+i, i, binind,
|
|
|
|
flag_unzeroed);
|
|
|
|
if (config_debug && flag_dirty == 0 && flag_unzeroed == 0)
|
2014-01-15 08:23:03 +08:00
|
|
|
arena_run_page_validate_zeroed(chunk, run_ind+i);
|
|
|
|
}
|
2014-04-16 07:35:08 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED((void *)((uintptr_t)chunk +
|
2014-01-15 08:23:03 +08:00
|
|
|
(run_ind << LG_PAGE)), (need_pages << LG_PAGE));
|
2015-08-05 01:49:46 +08:00
|
|
|
return (false);
|
2013-10-30 09:17:42 +08:00
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
static arena_chunk_t *
|
2014-01-15 08:23:03 +08:00
|
|
|
arena_chunk_init_spare(arena_t *arena)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
assert(arena->spare != NULL);
|
2010-03-19 11:36:40 +08:00
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
chunk = arena->spare;
|
|
|
|
arena->spare = NULL;
|
2010-01-25 09:13:07 +08:00
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
assert(arena_mapbits_allocated_get(chunk, map_bias) == 0);
|
|
|
|
assert(arena_mapbits_allocated_get(chunk, chunk_npages-1) == 0);
|
|
|
|
assert(arena_mapbits_unallocated_size_get(chunk, map_bias) ==
|
2014-10-06 08:54:10 +08:00
|
|
|
arena_maxrun);
|
2014-01-15 08:23:03 +08:00
|
|
|
assert(arena_mapbits_unallocated_size_get(chunk, chunk_npages-1) ==
|
2014-10-06 08:54:10 +08:00
|
|
|
arena_maxrun);
|
2014-01-15 08:23:03 +08:00
|
|
|
assert(arena_mapbits_dirty_get(chunk, map_bias) ==
|
|
|
|
arena_mapbits_dirty_get(chunk, chunk_npages-1));
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
return (chunk);
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-02-19 08:40:53 +08:00
|
|
|
static bool
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_register(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
bool zero)
|
2015-02-19 08:40:53 +08:00
|
|
|
{
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
/*
|
|
|
|
* The extent node notion of "committed" doesn't directly apply to
|
2015-08-10 07:47:27 +08:00
|
|
|
* arena chunks. Arbitrarily mark them as committed. The commit state
|
|
|
|
* of runs is tracked individually, and upon chunk deallocation the
|
|
|
|
* entire chunk is in a consistent commit state.
|
2015-08-05 01:49:46 +08:00
|
|
|
*/
|
|
|
|
extent_node_init(&chunk->node, arena, chunk, chunksize, zero, true);
|
2015-02-19 08:40:53 +08:00
|
|
|
extent_node_achunk_set(&chunk->node, true);
|
2016-05-11 13:21:10 +08:00
|
|
|
return (chunk_register(tsdn, chunk, &chunk->node));
|
2015-02-19 08:40:53 +08:00
|
|
|
}
|
|
|
|
|
2014-05-16 13:22:27 +08:00
|
|
|
static arena_chunk_t *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_alloc_internal_hard(tsdn_t *tsdn, arena_t *arena,
|
2016-04-14 14:36:15 +08:00
|
|
|
chunk_hooks_t *chunk_hooks, bool *zero, bool *commit)
|
2014-05-16 13:22:27 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
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
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk = (arena_chunk_t *)chunk_alloc_wrapper(tsdn, arena, chunk_hooks,
|
2016-04-14 14:36:15 +08:00
|
|
|
NULL, chunksize, chunksize, zero, commit);
|
2015-08-05 01:49:46 +08:00
|
|
|
if (chunk != NULL && !*commit) {
|
|
|
|
/* Commit header. */
|
|
|
|
if (chunk_hooks->commit(chunk, chunksize, 0, map_bias <<
|
|
|
|
LG_PAGE, arena->ind)) {
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_dalloc_wrapper(tsdn, arena, chunk_hooks,
|
2016-04-14 14:36:15 +08:00
|
|
|
(void *)chunk, chunksize, *zero, *commit);
|
2015-08-05 01:49:46 +08:00
|
|
|
chunk = NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
if (chunk != NULL && arena_chunk_register(tsdn, arena, chunk, *zero)) {
|
2015-08-05 01:49:46 +08:00
|
|
|
if (!*commit) {
|
|
|
|
/* Undo commit of header. */
|
|
|
|
chunk_hooks->decommit(chunk, chunksize, 0, map_bias <<
|
|
|
|
LG_PAGE, arena->ind);
|
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_dalloc_wrapper(tsdn, arena, chunk_hooks, (void *)chunk,
|
2016-03-31 09:36:04 +08:00
|
|
|
chunksize, *zero, *commit);
|
2015-02-19 08:40:53 +08:00
|
|
|
chunk = NULL;
|
Move centralized chunk management into arenas.
Migrate all centralized data structures related to huge allocations and
recyclable chunks into arena_t, so that each arena can manage huge
allocations and recyclable virtual memory completely independently of
other arenas.
Add chunk node caching to arenas, in order to avoid contention on the
base allocator.
Use chunks_rtree to look up huge allocations rather than a red-black
tree. Maintain a per arena unsorted list of huge allocations (which
will be needed to enumerate huge allocations during arena reset).
Remove the --enable-ivsalloc option, make ivsalloc() always available,
and use it for size queries if --enable-debug is enabled. The only
practical implications to this removal are that 1) ivsalloc() is now
always available during live debugging (and the underlying radix tree is
available during core-based debugging), and 2) size query validation can
no longer be enabled independent of --enable-debug.
Remove the stats.chunks.{current,total,high} mallctls, and replace their
underlying statistics with simpler atomically updated counters used
exclusively for gdump triggering. These statistics are no longer very
useful because each arena manages chunks independently, and per arena
statistics provide similar information.
Simplify chunk synchronization code, now that base chunk allocation
cannot cause recursive lock acquisition.
2015-02-12 04:24:27 +08:00
|
|
|
}
|
2015-02-19 08:40:53 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2015-02-19 08:40:53 +08:00
|
|
|
return (chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static arena_chunk_t *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_alloc_internal(tsdn_t *tsdn, arena_t *arena, bool *zero,
|
|
|
|
bool *commit)
|
2015-02-19 08:40:53 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
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;
|
2015-02-19 08:40:53 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk = chunk_alloc_cache(tsdn, arena, &chunk_hooks, NULL, chunksize,
|
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
|
|
|
chunksize, zero, true);
|
2015-08-05 01:49:46 +08:00
|
|
|
if (chunk != NULL) {
|
2016-05-11 13:21:10 +08:00
|
|
|
if (arena_chunk_register(tsdn, arena, chunk, *zero)) {
|
|
|
|
chunk_dalloc_cache(tsdn, arena, &chunk_hooks, chunk,
|
2015-08-10 07:47:27 +08:00
|
|
|
chunksize, true);
|
2015-08-05 01:49:46 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
*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
|
|
|
}
|
|
|
|
if (chunk == NULL) {
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk = arena_chunk_alloc_internal_hard(tsdn, arena,
|
2016-04-14 14:36:15 +08:00
|
|
|
&chunk_hooks, zero, 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
|
|
|
}
|
2015-02-19 08:40:53 +08:00
|
|
|
|
2014-11-28 03:22:36 +08:00
|
|
|
if (config_stats && chunk != NULL) {
|
2014-05-16 13:22:27 +08:00
|
|
|
arena->stats.mapped += chunksize;
|
2014-11-28 03:22:36 +08:00
|
|
|
arena->stats.metadata_mapped += (map_bias << LG_PAGE);
|
|
|
|
}
|
2014-05-16 13:22:27 +08:00
|
|
|
|
|
|
|
return (chunk);
|
|
|
|
}
|
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
static arena_chunk_t *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_init_hard(tsdn_t *tsdn, arena_t *arena)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
2015-08-05 01:49:46 +08:00
|
|
|
bool zero, commit;
|
2015-08-11 14:03:34 +08:00
|
|
|
size_t flag_unzeroed, flag_decommitted, i;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
assert(arena->spare == NULL);
|
2012-10-31 06:42:37 +08:00
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
zero = false;
|
2015-08-05 01:49:46 +08:00
|
|
|
commit = false;
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk = arena_chunk_alloc_internal(tsdn, arena, &zero, &commit);
|
2014-01-15 08:23:03 +08:00
|
|
|
if (chunk == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the map to contain one maximal free untouched run. Mark
|
2016-04-01 02:19:46 +08:00
|
|
|
* the pages as zeroed if arena_chunk_alloc_internal() returned a zeroed
|
|
|
|
* or decommitted chunk.
|
2014-01-15 08:23:03 +08:00
|
|
|
*/
|
2015-08-11 14:03:34 +08:00
|
|
|
flag_unzeroed = (zero || !commit) ? 0 : CHUNK_MAP_UNZEROED;
|
|
|
|
flag_decommitted = commit ? 0 : CHUNK_MAP_DECOMMITTED;
|
|
|
|
arena_mapbits_unallocated_set(chunk, map_bias, arena_maxrun,
|
|
|
|
flag_unzeroed | flag_decommitted);
|
2014-01-15 08:23:03 +08:00
|
|
|
/*
|
|
|
|
* There is no need to initialize the internal page map entries unless
|
|
|
|
* the chunk is not zeroed.
|
|
|
|
*/
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!zero) {
|
2014-04-16 07:35:08 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(
|
2016-03-24 07:04:38 +08:00
|
|
|
(void *)arena_bitselm_get_const(chunk, map_bias+1),
|
|
|
|
(size_t)((uintptr_t)arena_bitselm_get_const(chunk,
|
|
|
|
chunk_npages-1) -
|
|
|
|
(uintptr_t)arena_bitselm_get_const(chunk, map_bias+1)));
|
2014-01-15 08:23:03 +08:00
|
|
|
for (i = map_bias+1; i < chunk_npages-1; i++)
|
2015-08-11 14:03:34 +08:00
|
|
|
arena_mapbits_internal_set(chunk, i, flag_unzeroed);
|
2014-01-15 08:23:03 +08:00
|
|
|
} else {
|
2014-08-30 04:34:40 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_DEFINED((void
|
2016-03-24 07:04:38 +08:00
|
|
|
*)arena_bitselm_get_const(chunk, map_bias+1),
|
|
|
|
(size_t)((uintptr_t)arena_bitselm_get_const(chunk,
|
|
|
|
chunk_npages-1) -
|
|
|
|
(uintptr_t)arena_bitselm_get_const(chunk, map_bias+1)));
|
2014-01-15 08:23:03 +08:00
|
|
|
if (config_debug) {
|
|
|
|
for (i = map_bias+1; i < chunk_npages-1; i++) {
|
|
|
|
assert(arena_mapbits_unzeroed_get(chunk, i) ==
|
2015-08-11 14:03:34 +08:00
|
|
|
flag_unzeroed);
|
2012-05-02 15:30:36 +08:00
|
|
|
}
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
}
|
2010-03-19 11:36:40 +08:00
|
|
|
}
|
2014-10-06 08:54:10 +08:00
|
|
|
arena_mapbits_unallocated_set(chunk, chunk_npages-1, arena_maxrun,
|
2015-08-11 14:03:34 +08:00
|
|
|
flag_unzeroed);
|
2014-01-15 08:23:03 +08:00
|
|
|
|
|
|
|
return (chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static arena_chunk_t *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_alloc(tsdn_t *tsdn, arena_t *arena)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
|
|
|
|
if (arena->spare != NULL)
|
|
|
|
chunk = arena_chunk_init_spare(arena);
|
2014-03-26 13:36:05 +08:00
|
|
|
else {
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk = arena_chunk_init_hard(tsdn, arena);
|
2014-03-26 13:36:05 +08:00
|
|
|
if (chunk == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-04-23 05:37:17 +08:00
|
|
|
ql_elm_new(&chunk->node, ql_link);
|
|
|
|
ql_tail_insert(&arena->achunks, &chunk->node, ql_link);
|
2014-07-22 10:39:20 +08:00
|
|
|
arena_avail_insert(arena, chunk, map_bias, chunk_npages-map_bias);
|
2012-10-31 06:42:37 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
return (chunk);
|
|
|
|
}
|
|
|
|
|
2016-04-23 05:37:17 +08:00
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_discard(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk)
|
2016-04-23 05:37:17 +08:00
|
|
|
{
|
|
|
|
bool committed;
|
|
|
|
chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER;
|
|
|
|
|
|
|
|
chunk_deregister(chunk, &chunk->node);
|
|
|
|
|
|
|
|
committed = (arena_mapbits_decommitted_get(chunk, map_bias) == 0);
|
|
|
|
if (!committed) {
|
|
|
|
/*
|
|
|
|
* Decommit the header. Mark the chunk as decommitted even if
|
|
|
|
* header decommit fails, since treating a partially committed
|
|
|
|
* chunk as committed has a high potential for causing later
|
|
|
|
* access of decommitted memory.
|
|
|
|
*/
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_hooks = chunk_hooks_get(tsdn, arena);
|
2016-04-23 05:37:17 +08:00
|
|
|
chunk_hooks.decommit(chunk, chunksize, 0, map_bias << LG_PAGE,
|
|
|
|
arena->ind);
|
|
|
|
}
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_dalloc_cache(tsdn, arena, &chunk_hooks, (void *)chunk, chunksize,
|
2016-04-23 05:37:17 +08:00
|
|
|
committed);
|
|
|
|
|
|
|
|
if (config_stats) {
|
|
|
|
arena->stats.mapped -= chunksize;
|
|
|
|
arena->stats.metadata_mapped -= (map_bias << LG_PAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_spare_discard(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *spare)
|
2016-04-23 05:37:17 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
assert(arena->spare != spare);
|
|
|
|
|
|
|
|
if (arena_mapbits_dirty_get(spare, map_bias) != 0) {
|
|
|
|
arena_run_dirty_remove(arena, spare, map_bias,
|
|
|
|
chunk_npages-map_bias);
|
|
|
|
}
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_discard(tsdn, arena, spare);
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_dalloc(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk)
|
2014-10-15 13:20:00 +08:00
|
|
|
{
|
2016-04-23 05:37:17 +08:00
|
|
|
arena_chunk_t *spare;
|
2014-10-15 13:20:00 +08:00
|
|
|
|
|
|
|
assert(arena_mapbits_allocated_get(chunk, map_bias) == 0);
|
|
|
|
assert(arena_mapbits_allocated_get(chunk, chunk_npages-1) == 0);
|
|
|
|
assert(arena_mapbits_unallocated_size_get(chunk, map_bias) ==
|
|
|
|
arena_maxrun);
|
|
|
|
assert(arena_mapbits_unallocated_size_get(chunk, chunk_npages-1) ==
|
|
|
|
arena_maxrun);
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, map_bias) ==
|
|
|
|
arena_mapbits_dirty_get(chunk, chunk_npages-1));
|
2015-08-10 07:47:27 +08:00
|
|
|
assert(arena_mapbits_decommitted_get(chunk, map_bias) ==
|
|
|
|
arena_mapbits_decommitted_get(chunk, chunk_npages-1));
|
2014-10-15 13:20:00 +08:00
|
|
|
|
2016-02-24 04:06:21 +08:00
|
|
|
/* Remove run from runs_avail, so that the arena does not use it. */
|
2014-10-15 13:20:00 +08:00
|
|
|
arena_avail_remove(arena, chunk, map_bias, chunk_npages-map_bias);
|
|
|
|
|
2016-04-23 05:37:17 +08:00
|
|
|
ql_remove(&arena->achunks, &chunk->node, ql_link);
|
|
|
|
spare = arena->spare;
|
|
|
|
arena->spare = chunk;
|
|
|
|
if (spare != NULL)
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_spare_discard(tsdn, arena, spare);
|
2014-10-15 13:20:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
arena_huge_malloc_stats_update(arena_t *arena, size_t usize)
|
|
|
|
{
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t index = size2index(usize) - nlclasses - NBINS;
|
2014-10-15 13:20:00 +08:00
|
|
|
|
|
|
|
cassert(config_stats);
|
|
|
|
|
|
|
|
arena->stats.nmalloc_huge++;
|
|
|
|
arena->stats.allocated_huge += usize;
|
|
|
|
arena->stats.hstats[index].nmalloc++;
|
|
|
|
arena->stats.hstats[index].curhchunks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
arena_huge_malloc_stats_update_undo(arena_t *arena, size_t usize)
|
|
|
|
{
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t index = size2index(usize) - nlclasses - NBINS;
|
2014-10-15 13:20:00 +08:00
|
|
|
|
|
|
|
cassert(config_stats);
|
|
|
|
|
|
|
|
arena->stats.nmalloc_huge--;
|
|
|
|
arena->stats.allocated_huge -= usize;
|
|
|
|
arena->stats.hstats[index].nmalloc--;
|
|
|
|
arena->stats.hstats[index].curhchunks--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
arena_huge_dalloc_stats_update(arena_t *arena, size_t usize)
|
|
|
|
{
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t index = size2index(usize) - nlclasses - NBINS;
|
2014-10-15 13:20:00 +08:00
|
|
|
|
|
|
|
cassert(config_stats);
|
|
|
|
|
|
|
|
arena->stats.ndalloc_huge++;
|
|
|
|
arena->stats.allocated_huge -= usize;
|
|
|
|
arena->stats.hstats[index].ndalloc++;
|
|
|
|
arena->stats.hstats[index].curhchunks--;
|
|
|
|
}
|
|
|
|
|
2016-04-26 04:26:54 +08:00
|
|
|
static void
|
|
|
|
arena_huge_reset_stats_cancel(arena_t *arena, size_t usize)
|
|
|
|
{
|
|
|
|
szind_t index = size2index(usize) - nlclasses - NBINS;
|
|
|
|
|
|
|
|
cassert(config_stats);
|
|
|
|
|
|
|
|
arena->stats.ndalloc_huge++;
|
|
|
|
arena->stats.hstats[index].ndalloc--;
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:20:00 +08:00
|
|
|
static void
|
|
|
|
arena_huge_dalloc_stats_update_undo(arena_t *arena, size_t usize)
|
|
|
|
{
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t index = size2index(usize) - nlclasses - NBINS;
|
2014-10-15 13:20:00 +08:00
|
|
|
|
|
|
|
cassert(config_stats);
|
|
|
|
|
|
|
|
arena->stats.ndalloc_huge--;
|
|
|
|
arena->stats.allocated_huge += usize;
|
|
|
|
arena->stats.hstats[index].ndalloc--;
|
|
|
|
arena->stats.hstats[index].curhchunks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
arena_huge_ralloc_stats_update(arena_t *arena, size_t oldsize, size_t usize)
|
|
|
|
{
|
|
|
|
|
|
|
|
arena_huge_dalloc_stats_update(arena, oldsize);
|
|
|
|
arena_huge_malloc_stats_update(arena, usize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
arena_huge_ralloc_stats_update_undo(arena_t *arena, size_t oldsize,
|
|
|
|
size_t usize)
|
2014-05-16 13:22:27 +08:00
|
|
|
{
|
2014-10-15 13:20:00 +08:00
|
|
|
|
|
|
|
arena_huge_dalloc_stats_update_undo(arena, oldsize);
|
|
|
|
arena_huge_malloc_stats_update_undo(arena, usize);
|
|
|
|
}
|
|
|
|
|
Move centralized chunk management into arenas.
Migrate all centralized data structures related to huge allocations and
recyclable chunks into arena_t, so that each arena can manage huge
allocations and recyclable virtual memory completely independently of
other arenas.
Add chunk node caching to arenas, in order to avoid contention on the
base allocator.
Use chunks_rtree to look up huge allocations rather than a red-black
tree. Maintain a per arena unsorted list of huge allocations (which
will be needed to enumerate huge allocations during arena reset).
Remove the --enable-ivsalloc option, make ivsalloc() always available,
and use it for size queries if --enable-debug is enabled. The only
practical implications to this removal are that 1) ivsalloc() is now
always available during live debugging (and the underlying radix tree is
available during core-based debugging), and 2) size query validation can
no longer be enabled independent of --enable-debug.
Remove the stats.chunks.{current,total,high} mallctls, and replace their
underlying statistics with simpler atomically updated counters used
exclusively for gdump triggering. These statistics are no longer very
useful because each arena manages chunks independently, and per arena
statistics provide similar information.
Simplify chunk synchronization code, now that base chunk allocation
cannot cause recursive lock acquisition.
2015-02-12 04:24:27 +08:00
|
|
|
extent_node_t *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_node_alloc(tsdn_t *tsdn, arena_t *arena)
|
Move centralized chunk management into arenas.
Migrate all centralized data structures related to huge allocations and
recyclable chunks into arena_t, so that each arena can manage huge
allocations and recyclable virtual memory completely independently of
other arenas.
Add chunk node caching to arenas, in order to avoid contention on the
base allocator.
Use chunks_rtree to look up huge allocations rather than a red-black
tree. Maintain a per arena unsorted list of huge allocations (which
will be needed to enumerate huge allocations during arena reset).
Remove the --enable-ivsalloc option, make ivsalloc() always available,
and use it for size queries if --enable-debug is enabled. The only
practical implications to this removal are that 1) ivsalloc() is now
always available during live debugging (and the underlying radix tree is
available during core-based debugging), and 2) size query validation can
no longer be enabled independent of --enable-debug.
Remove the stats.chunks.{current,total,high} mallctls, and replace their
underlying statistics with simpler atomically updated counters used
exclusively for gdump triggering. These statistics are no longer very
useful because each arena manages chunks independently, and per arena
statistics provide similar information.
Simplify chunk synchronization code, now that base chunk allocation
cannot cause recursive lock acquisition.
2015-02-12 04:24:27 +08:00
|
|
|
{
|
|
|
|
extent_node_t *node;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->node_cache_mtx);
|
2015-02-16 08:43:52 +08:00
|
|
|
node = ql_last(&arena->node_cache, ql_link);
|
Move centralized chunk management into arenas.
Migrate all centralized data structures related to huge allocations and
recyclable chunks into arena_t, so that each arena can manage huge
allocations and recyclable virtual memory completely independently of
other arenas.
Add chunk node caching to arenas, in order to avoid contention on the
base allocator.
Use chunks_rtree to look up huge allocations rather than a red-black
tree. Maintain a per arena unsorted list of huge allocations (which
will be needed to enumerate huge allocations during arena reset).
Remove the --enable-ivsalloc option, make ivsalloc() always available,
and use it for size queries if --enable-debug is enabled. The only
practical implications to this removal are that 1) ivsalloc() is now
always available during live debugging (and the underlying radix tree is
available during core-based debugging), and 2) size query validation can
no longer be enabled independent of --enable-debug.
Remove the stats.chunks.{current,total,high} mallctls, and replace their
underlying statistics with simpler atomically updated counters used
exclusively for gdump triggering. These statistics are no longer very
useful because each arena manages chunks independently, and per arena
statistics provide similar information.
Simplify chunk synchronization code, now that base chunk allocation
cannot cause recursive lock acquisition.
2015-02-12 04:24:27 +08:00
|
|
|
if (node == NULL) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->node_cache_mtx);
|
|
|
|
return (base_alloc(tsdn, sizeof(extent_node_t)));
|
Move centralized chunk management into arenas.
Migrate all centralized data structures related to huge allocations and
recyclable chunks into arena_t, so that each arena can manage huge
allocations and recyclable virtual memory completely independently of
other arenas.
Add chunk node caching to arenas, in order to avoid contention on the
base allocator.
Use chunks_rtree to look up huge allocations rather than a red-black
tree. Maintain a per arena unsorted list of huge allocations (which
will be needed to enumerate huge allocations during arena reset).
Remove the --enable-ivsalloc option, make ivsalloc() always available,
and use it for size queries if --enable-debug is enabled. The only
practical implications to this removal are that 1) ivsalloc() is now
always available during live debugging (and the underlying radix tree is
available during core-based debugging), and 2) size query validation can
no longer be enabled independent of --enable-debug.
Remove the stats.chunks.{current,total,high} mallctls, and replace their
underlying statistics with simpler atomically updated counters used
exclusively for gdump triggering. These statistics are no longer very
useful because each arena manages chunks independently, and per arena
statistics provide similar information.
Simplify chunk synchronization code, now that base chunk allocation
cannot cause recursive lock acquisition.
2015-02-12 04:24:27 +08:00
|
|
|
}
|
2015-02-16 08:43:52 +08:00
|
|
|
ql_tail_remove(&arena->node_cache, extent_node_t, ql_link);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->node_cache_mtx);
|
Move centralized chunk management into arenas.
Migrate all centralized data structures related to huge allocations and
recyclable chunks into arena_t, so that each arena can manage huge
allocations and recyclable virtual memory completely independently of
other arenas.
Add chunk node caching to arenas, in order to avoid contention on the
base allocator.
Use chunks_rtree to look up huge allocations rather than a red-black
tree. Maintain a per arena unsorted list of huge allocations (which
will be needed to enumerate huge allocations during arena reset).
Remove the --enable-ivsalloc option, make ivsalloc() always available,
and use it for size queries if --enable-debug is enabled. The only
practical implications to this removal are that 1) ivsalloc() is now
always available during live debugging (and the underlying radix tree is
available during core-based debugging), and 2) size query validation can
no longer be enabled independent of --enable-debug.
Remove the stats.chunks.{current,total,high} mallctls, and replace their
underlying statistics with simpler atomically updated counters used
exclusively for gdump triggering. These statistics are no longer very
useful because each arena manages chunks independently, and per arena
statistics provide similar information.
Simplify chunk synchronization code, now that base chunk allocation
cannot cause recursive lock acquisition.
2015-02-12 04:24:27 +08:00
|
|
|
return (node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_node_dalloc(tsdn_t *tsdn, arena_t *arena, extent_node_t *node)
|
Move centralized chunk management into arenas.
Migrate all centralized data structures related to huge allocations and
recyclable chunks into arena_t, so that each arena can manage huge
allocations and recyclable virtual memory completely independently of
other arenas.
Add chunk node caching to arenas, in order to avoid contention on the
base allocator.
Use chunks_rtree to look up huge allocations rather than a red-black
tree. Maintain a per arena unsorted list of huge allocations (which
will be needed to enumerate huge allocations during arena reset).
Remove the --enable-ivsalloc option, make ivsalloc() always available,
and use it for size queries if --enable-debug is enabled. The only
practical implications to this removal are that 1) ivsalloc() is now
always available during live debugging (and the underlying radix tree is
available during core-based debugging), and 2) size query validation can
no longer be enabled independent of --enable-debug.
Remove the stats.chunks.{current,total,high} mallctls, and replace their
underlying statistics with simpler atomically updated counters used
exclusively for gdump triggering. These statistics are no longer very
useful because each arena manages chunks independently, and per arena
statistics provide similar information.
Simplify chunk synchronization code, now that base chunk allocation
cannot cause recursive lock acquisition.
2015-02-12 04:24:27 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->node_cache_mtx);
|
2015-02-16 08:43:52 +08:00
|
|
|
ql_elm_new(node, ql_link);
|
|
|
|
ql_tail_insert(&arena->node_cache, node, ql_link);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->node_cache_mtx);
|
Move centralized chunk management into arenas.
Migrate all centralized data structures related to huge allocations and
recyclable chunks into arena_t, so that each arena can manage huge
allocations and recyclable virtual memory completely independently of
other arenas.
Add chunk node caching to arenas, in order to avoid contention on the
base allocator.
Use chunks_rtree to look up huge allocations rather than a red-black
tree. Maintain a per arena unsorted list of huge allocations (which
will be needed to enumerate huge allocations during arena reset).
Remove the --enable-ivsalloc option, make ivsalloc() always available,
and use it for size queries if --enable-debug is enabled. The only
practical implications to this removal are that 1) ivsalloc() is now
always available during live debugging (and the underlying radix tree is
available during core-based debugging), and 2) size query validation can
no longer be enabled independent of --enable-debug.
Remove the stats.chunks.{current,total,high} mallctls, and replace their
underlying statistics with simpler atomically updated counters used
exclusively for gdump triggering. These statistics are no longer very
useful because each arena manages chunks independently, and per arena
statistics provide similar information.
Simplify chunk synchronization code, now that base chunk allocation
cannot cause recursive lock acquisition.
2015-02-12 04:24:27 +08:00
|
|
|
}
|
|
|
|
|
2015-02-19 08:40:53 +08:00
|
|
|
static void *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_alloc_huge_hard(tsdn_t *tsdn, arena_t *arena,
|
2016-04-14 14:36:15 +08:00
|
|
|
chunk_hooks_t *chunk_hooks, size_t usize, size_t alignment, bool *zero,
|
|
|
|
size_t csize)
|
2015-02-19 08:40:53 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
2015-08-05 01:49:46 +08:00
|
|
|
bool commit = true;
|
2015-02-19 08:40:53 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = chunk_alloc_wrapper(tsdn, arena, chunk_hooks, NULL, csize,
|
2016-04-14 14:36:15 +08:00
|
|
|
alignment, zero, &commit);
|
2015-02-19 08:40:53 +08:00
|
|
|
if (ret == NULL) {
|
|
|
|
/* Revert optimistic stats updates. */
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2015-02-19 08:40:53 +08:00
|
|
|
if (config_stats) {
|
|
|
|
arena_huge_malloc_stats_update_undo(arena, usize);
|
|
|
|
arena->stats.mapped -= usize;
|
|
|
|
}
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_sub(arena, usize >> LG_PAGE);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2015-02-19 08:40:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:20:00 +08:00
|
|
|
void *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_alloc_huge(tsdn_t *tsdn, arena_t *arena, size_t usize,
|
2016-04-14 14:36:15 +08:00
|
|
|
size_t alignment, bool *zero)
|
2014-10-15 13:20:00 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
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;
|
2014-10-15 13:20:00 +08:00
|
|
|
size_t csize = CHUNK_CEILING(usize);
|
2014-05-16 13:22:27 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2015-02-19 08:40:53 +08:00
|
|
|
|
|
|
|
/* Optimistically update stats. */
|
2014-10-15 13:20:00 +08:00
|
|
|
if (config_stats) {
|
|
|
|
arena_huge_malloc_stats_update(arena, usize);
|
|
|
|
arena->stats.mapped += usize;
|
|
|
|
}
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_add(arena, usize >> LG_PAGE);
|
2014-10-15 13:20:00 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = chunk_alloc_cache(tsdn, arena, &chunk_hooks, NULL, csize,
|
2016-04-14 14:36:15 +08:00
|
|
|
alignment, zero, true);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2014-10-15 13:20:00 +08:00
|
|
|
if (ret == NULL) {
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = arena_chunk_alloc_huge_hard(tsdn, arena, &chunk_hooks,
|
2016-04-14 14:36:15 +08:00
|
|
|
usize, alignment, zero, csize);
|
2014-10-15 13:20:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
2014-05-16 13:22:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_dalloc_huge(tsdn_t *tsdn, arena_t *arena, void *chunk, size_t usize)
|
2014-05-16 13:22:27 +08:00
|
|
|
{
|
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;
|
2015-02-19 08:40:53 +08:00
|
|
|
size_t csize;
|
2014-05-16 13:22:27 +08:00
|
|
|
|
2015-02-19 08:40:53 +08:00
|
|
|
csize = CHUNK_CEILING(usize);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2014-05-16 13:22:27 +08:00
|
|
|
if (config_stats) {
|
2014-10-15 13:20:00 +08:00
|
|
|
arena_huge_dalloc_stats_update(arena, usize);
|
2014-10-13 13:53:59 +08:00
|
|
|
arena->stats.mapped -= usize;
|
2014-05-16 13:22:27 +08:00
|
|
|
}
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_sub(arena, usize >> LG_PAGE);
|
2015-02-19 08:40:53 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_dalloc_cache(tsdn, arena, &chunk_hooks, chunk, csize, true);
|
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2014-05-16 13:22:27 +08:00
|
|
|
}
|
|
|
|
|
2014-10-15 13:20:00 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_ralloc_huge_similar(tsdn_t *tsdn, arena_t *arena, void *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
size_t oldsize, size_t usize)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2014-07-19 05:21:17 +08:00
|
|
|
|
2014-10-15 13:20:00 +08:00
|
|
|
assert(CHUNK_CEILING(oldsize) == CHUNK_CEILING(usize));
|
|
|
|
assert(oldsize != usize);
|
2012-05-11 08:09:17 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2014-10-15 13:20:00 +08:00
|
|
|
if (config_stats)
|
|
|
|
arena_huge_ralloc_stats_update(arena, oldsize, usize);
|
2016-02-28 04:34:50 +08:00
|
|
|
if (oldsize < usize)
|
|
|
|
arena_nactive_add(arena, (usize - oldsize) >> LG_PAGE);
|
|
|
|
else
|
|
|
|
arena_nactive_sub(arena, (oldsize - usize) >> LG_PAGE);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2014-10-15 13:20:00 +08:00
|
|
|
}
|
2010-04-14 11:53:21 +08:00
|
|
|
|
2014-10-15 13:20:00 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_ralloc_huge_shrink(tsdn_t *tsdn, arena_t *arena, void *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
size_t oldsize, size_t usize)
|
2014-10-15 13:20:00 +08:00
|
|
|
{
|
|
|
|
size_t udiff = oldsize - usize;
|
|
|
|
size_t cdiff = CHUNK_CEILING(oldsize) - CHUNK_CEILING(usize);
|
2010-03-15 10:43:56 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2014-10-15 13:20:00 +08:00
|
|
|
if (config_stats) {
|
|
|
|
arena_huge_ralloc_stats_update(arena, oldsize, usize);
|
2016-02-28 04:34:50 +08:00
|
|
|
if (cdiff != 0)
|
2014-10-15 13:20:00 +08:00
|
|
|
arena->stats.mapped -= cdiff;
|
|
|
|
}
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_sub(arena, udiff >> LG_PAGE);
|
2015-02-19 08:40:53 +08:00
|
|
|
|
2014-11-18 01:54:49 +08:00
|
|
|
if (cdiff != 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;
|
2015-02-19 08:40:53 +08:00
|
|
|
void *nchunk = (void *)((uintptr_t)chunk +
|
|
|
|
CHUNK_CEILING(usize));
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_dalloc_cache(tsdn, arena, &chunk_hooks, nchunk, cdiff,
|
2016-04-14 14:36:15 +08:00
|
|
|
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
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2015-02-19 08:40:53 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
static bool
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_ralloc_huge_expand_hard(tsdn_t *tsdn, arena_t *arena,
|
2016-04-14 14:36:15 +08:00
|
|
|
chunk_hooks_t *chunk_hooks, void *chunk, size_t oldsize, size_t usize,
|
|
|
|
bool *zero, void *nchunk, size_t udiff, size_t cdiff)
|
2015-02-19 08:40:53 +08:00
|
|
|
{
|
|
|
|
bool err;
|
2015-08-05 01:49:46 +08:00
|
|
|
bool commit = true;
|
2015-02-19 08:40:53 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
err = (chunk_alloc_wrapper(tsdn, arena, chunk_hooks, nchunk, cdiff,
|
2016-04-14 14:36:15 +08:00
|
|
|
chunksize, zero, &commit) == NULL);
|
2015-02-19 08:40:53 +08:00
|
|
|
if (err) {
|
|
|
|
/* Revert optimistic stats updates. */
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2015-02-19 08:40:53 +08:00
|
|
|
if (config_stats) {
|
|
|
|
arena_huge_ralloc_stats_update_undo(arena, oldsize,
|
|
|
|
usize);
|
|
|
|
arena->stats.mapped -= cdiff;
|
|
|
|
}
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_sub(arena, udiff >> LG_PAGE);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
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 if (chunk_hooks->merge(chunk, CHUNK_CEILING(oldsize), nchunk,
|
|
|
|
cdiff, true, arena->ind)) {
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_dalloc_wrapper(tsdn, arena, chunk_hooks, nchunk, cdiff,
|
2016-04-14 14:36:15 +08:00
|
|
|
*zero, 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
|
|
|
err = true;
|
2014-11-18 01:54:49 +08:00
|
|
|
}
|
2015-02-19 08:40:53 +08:00
|
|
|
return (err);
|
2014-10-15 13:20:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_ralloc_huge_expand(tsdn_t *tsdn, arena_t *arena, void *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
size_t oldsize, size_t usize, bool *zero)
|
2014-10-15 13:20:00 +08:00
|
|
|
{
|
2015-02-19 08:40:53 +08:00
|
|
|
bool err;
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_hooks_t chunk_hooks = chunk_hooks_get(tsdn, arena);
|
2015-02-19 08:40:53 +08:00
|
|
|
void *nchunk = (void *)((uintptr_t)chunk + CHUNK_CEILING(oldsize));
|
2014-10-15 13:20:00 +08:00
|
|
|
size_t udiff = usize - oldsize;
|
|
|
|
size_t cdiff = CHUNK_CEILING(usize) - CHUNK_CEILING(oldsize);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2015-02-19 08:40:53 +08:00
|
|
|
|
|
|
|
/* Optimistically update stats. */
|
2014-10-15 13:20:00 +08:00
|
|
|
if (config_stats) {
|
|
|
|
arena_huge_ralloc_stats_update(arena, oldsize, usize);
|
|
|
|
arena->stats.mapped += cdiff;
|
|
|
|
}
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_add(arena, udiff >> LG_PAGE);
|
2014-10-15 13:20:00 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
err = (chunk_alloc_cache(tsdn, arena, &chunk_hooks, nchunk, cdiff,
|
2016-04-14 14:36:15 +08:00
|
|
|
chunksize, zero, true) == NULL);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2015-02-19 08:40:53 +08:00
|
|
|
if (err) {
|
2016-05-11 13:21:10 +08:00
|
|
|
err = arena_chunk_ralloc_huge_expand_hard(tsdn, arena,
|
2016-04-14 14:36:15 +08:00
|
|
|
&chunk_hooks, chunk, oldsize, usize, zero, nchunk, udiff,
|
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
|
|
|
cdiff);
|
|
|
|
} else if (chunk_hooks.merge(chunk, CHUNK_CEILING(oldsize), nchunk,
|
|
|
|
cdiff, true, arena->ind)) {
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_dalloc_wrapper(tsdn, arena, &chunk_hooks, nchunk, cdiff,
|
2016-04-14 14:36:15 +08:00
|
|
|
*zero, 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
|
|
|
err = true;
|
2014-10-15 13:20:00 +08:00
|
|
|
}
|
|
|
|
|
2015-02-19 08:40:53 +08:00
|
|
|
return (err);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2015-07-16 07:02:21 +08:00
|
|
|
/*
|
|
|
|
* Do first-best-fit run selection, i.e. select the lowest run that best fits.
|
2016-02-24 04:06:21 +08:00
|
|
|
* Run sizes are indexed, so not all candidate runs are necessarily exactly the
|
|
|
|
* same size.
|
2015-07-16 07:02:21 +08:00
|
|
|
*/
|
2010-01-17 01:53:50 +08:00
|
|
|
static arena_run_t *
|
2015-07-16 07:02:21 +08:00
|
|
|
arena_run_first_best_fit(arena_t *arena, size_t size)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
2016-02-24 04:06:21 +08:00
|
|
|
szind_t ind, i;
|
|
|
|
|
|
|
|
ind = size2index(run_quantize_ceil(size));
|
2016-02-25 03:02:49 +08:00
|
|
|
for (i = ind; i < runs_avail_nclasses + runs_avail_bias; i++) {
|
2016-03-27 08:30:37 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm = arena_run_heap_first(
|
|
|
|
arena_runs_avail_get(arena, i));
|
|
|
|
if (miscelm != NULL)
|
2016-02-24 04:06:21 +08:00
|
|
|
return (&miscelm->run);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
2015-03-07 11:57:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static arena_run_t *
|
|
|
|
arena_run_alloc_large_helper(arena_t *arena, size_t size, bool zero)
|
|
|
|
{
|
2015-07-16 07:02:21 +08:00
|
|
|
arena_run_t *run = arena_run_first_best_fit(arena, s2u(size));
|
2015-08-05 01:49:46 +08:00
|
|
|
if (run != NULL) {
|
|
|
|
if (arena_run_split_large(arena, run, size, zero))
|
|
|
|
run = NULL;
|
|
|
|
}
|
2015-03-07 11:57:36 +08:00
|
|
|
return (run);
|
2014-01-15 08:23:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static arena_run_t *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_alloc_large(tsdn_t *tsdn, arena_t *arena, size_t size, bool zero)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
arena_run_t *run;
|
|
|
|
|
2014-10-10 08:54:06 +08:00
|
|
|
assert(size <= arena_maxrun);
|
2015-05-05 00:58:36 +08:00
|
|
|
assert(size == PAGE_CEILING(size));
|
2014-01-15 08:23:03 +08:00
|
|
|
|
|
|
|
/* Search the arena's chunks for the lowest best fit. */
|
|
|
|
run = arena_run_alloc_large_helper(arena, size, zero);
|
|
|
|
if (run != NULL)
|
|
|
|
return (run);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No usable runs. Create a new chunk from which to allocate the run.
|
|
|
|
*/
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk = arena_chunk_alloc(tsdn, arena);
|
2014-01-15 08:23:03 +08:00
|
|
|
if (chunk != NULL) {
|
2016-03-24 07:04:38 +08:00
|
|
|
run = &arena_miscelm_get_mutable(chunk, map_bias)->run;
|
2015-08-05 01:49:46 +08:00
|
|
|
if (arena_run_split_large(arena, run, size, zero))
|
|
|
|
run = NULL;
|
2014-01-15 08:23:03 +08:00
|
|
|
return (run);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* arena_chunk_alloc() failed, but another thread may have made
|
|
|
|
* sufficient memory available while this one dropped arena->lock in
|
|
|
|
* arena_chunk_alloc(), so search one more time.
|
|
|
|
*/
|
|
|
|
return (arena_run_alloc_large_helper(arena, size, zero));
|
|
|
|
}
|
|
|
|
|
|
|
|
static arena_run_t *
|
2015-08-20 06:21:32 +08:00
|
|
|
arena_run_alloc_small_helper(arena_t *arena, size_t size, szind_t binind)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2015-07-16 07:02:21 +08:00
|
|
|
arena_run_t *run = arena_run_first_best_fit(arena, size);
|
2015-08-05 01:49:46 +08:00
|
|
|
if (run != NULL) {
|
|
|
|
if (arena_run_split_small(arena, run, size, binind))
|
|
|
|
run = NULL;
|
|
|
|
}
|
2015-03-07 11:57:36 +08:00
|
|
|
return (run);
|
2012-05-11 06:47:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static arena_run_t *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_alloc_small(tsdn_t *tsdn, arena_t *arena, size_t size, szind_t binind)
|
2012-05-11 06:47:24 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
arena_run_t *run;
|
|
|
|
|
2014-10-10 08:54:06 +08:00
|
|
|
assert(size <= arena_maxrun);
|
2015-05-05 00:58:36 +08:00
|
|
|
assert(size == PAGE_CEILING(size));
|
2014-01-15 08:23:03 +08:00
|
|
|
assert(binind != BININD_INVALID);
|
2012-05-11 06:47:24 +08:00
|
|
|
|
|
|
|
/* Search the arena's chunks for the lowest best fit. */
|
2014-01-15 08:23:03 +08:00
|
|
|
run = arena_run_alloc_small_helper(arena, size, binind);
|
2012-05-11 06:47:24 +08:00
|
|
|
if (run != NULL)
|
|
|
|
return (run);
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/*
|
|
|
|
* No usable runs. Create a new chunk from which to allocate the run.
|
|
|
|
*/
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk = arena_chunk_alloc(tsdn, arena);
|
2010-03-15 10:43:56 +08:00
|
|
|
if (chunk != NULL) {
|
2016-03-24 07:04:38 +08:00
|
|
|
run = &arena_miscelm_get_mutable(chunk, map_bias)->run;
|
2015-08-05 01:49:46 +08:00
|
|
|
if (arena_run_split_small(arena, run, size, binind))
|
|
|
|
run = NULL;
|
2010-03-15 10:43:56 +08:00
|
|
|
return (run);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* arena_chunk_alloc() failed, but another thread may have made
|
|
|
|
* sufficient memory available while this one dropped arena->lock in
|
|
|
|
* arena_chunk_alloc(), so search one more time.
|
|
|
|
*/
|
2014-01-15 08:23:03 +08:00
|
|
|
return (arena_run_alloc_small_helper(arena, size, binind));
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2015-03-19 09:55:33 +08:00
|
|
|
static bool
|
|
|
|
arena_lg_dirty_mult_valid(ssize_t lg_dirty_mult)
|
|
|
|
{
|
|
|
|
|
2015-03-25 06:59:28 +08:00
|
|
|
return (lg_dirty_mult >= -1 && lg_dirty_mult < (ssize_t)(sizeof(size_t)
|
|
|
|
<< 3));
|
2015-03-19 09:55:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_lg_dirty_mult_get(tsdn_t *tsdn, arena_t *arena)
|
2015-03-19 09:55:33 +08:00
|
|
|
{
|
|
|
|
ssize_t lg_dirty_mult;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2015-03-19 09:55:33 +08:00
|
|
|
lg_dirty_mult = arena->lg_dirty_mult;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2015-03-19 09:55:33 +08:00
|
|
|
|
|
|
|
return (lg_dirty_mult);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_lg_dirty_mult_set(tsdn_t *tsdn, arena_t *arena, ssize_t lg_dirty_mult)
|
2015-03-19 09:55:33 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if (!arena_lg_dirty_mult_valid(lg_dirty_mult))
|
|
|
|
return (true);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2015-03-19 09:55:33 +08:00
|
|
|
arena->lg_dirty_mult = lg_dirty_mult;
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_maybe_purge(tsdn, arena);
|
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2015-03-19 09:55:33 +08:00
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
static void
|
|
|
|
arena_decay_deadline_init(arena_t *arena)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(opt_purge == purge_mode_decay);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a new deadline that is uniformly random within the next
|
|
|
|
* epoch after the current one.
|
|
|
|
*/
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_copy(&arena->decay_deadline, &arena->decay_epoch);
|
|
|
|
nstime_add(&arena->decay_deadline, &arena->decay_interval);
|
2016-02-20 12:09:31 +08:00
|
|
|
if (arena->decay_time > 0) {
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_t jitter;
|
|
|
|
|
|
|
|
nstime_init(&jitter, prng_range(&arena->decay_jitter_state,
|
|
|
|
nstime_ns(&arena->decay_interval)));
|
|
|
|
nstime_add(&arena->decay_deadline, &jitter);
|
2016-02-20 12:09:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-02-22 03:25:02 +08:00
|
|
|
arena_decay_deadline_reached(const arena_t *arena, const nstime_t *time)
|
2016-02-20 12:09:31 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
assert(opt_purge == purge_mode_decay);
|
|
|
|
|
2016-02-22 03:25:02 +08:00
|
|
|
return (nstime_compare(&arena->decay_deadline, time) <= 0);
|
2016-02-20 12:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
arena_decay_backlog_npages_limit(const arena_t *arena)
|
|
|
|
{
|
|
|
|
static const uint64_t h_steps[] = {
|
|
|
|
#define STEP(step, h, x, y) \
|
|
|
|
h,
|
|
|
|
SMOOTHSTEP
|
|
|
|
#undef STEP
|
|
|
|
};
|
|
|
|
uint64_t sum;
|
|
|
|
size_t npages_limit_backlog;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
assert(opt_purge == purge_mode_decay);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For each element of decay_backlog, multiply by the corresponding
|
|
|
|
* fixed-point smoothstep decay factor. Sum the products, then divide
|
|
|
|
* to round down to the nearest whole number of pages.
|
|
|
|
*/
|
|
|
|
sum = 0;
|
|
|
|
for (i = 0; i < SMOOTHSTEP_NSTEPS; i++)
|
|
|
|
sum += arena->decay_backlog[i] * h_steps[i];
|
2016-04-12 15:50:54 +08:00
|
|
|
npages_limit_backlog = (size_t)(sum >> SMOOTHSTEP_BFP);
|
2016-02-20 12:09:31 +08:00
|
|
|
|
|
|
|
return (npages_limit_backlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-22 03:25:02 +08:00
|
|
|
arena_decay_epoch_advance(arena_t *arena, const nstime_t *time)
|
2016-02-20 12:09:31 +08:00
|
|
|
{
|
2016-04-12 15:50:54 +08:00
|
|
|
uint64_t nadvance_u64;
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_t delta;
|
2016-02-20 12:09:31 +08:00
|
|
|
size_t ndirty_delta;
|
|
|
|
|
|
|
|
assert(opt_purge == purge_mode_decay);
|
|
|
|
assert(arena_decay_deadline_reached(arena, time));
|
|
|
|
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_copy(&delta, time);
|
|
|
|
nstime_subtract(&delta, &arena->decay_epoch);
|
2016-04-12 15:50:54 +08:00
|
|
|
nadvance_u64 = nstime_divide(&delta, &arena->decay_interval);
|
|
|
|
assert(nadvance_u64 > 0);
|
2016-02-20 12:09:31 +08:00
|
|
|
|
2016-04-12 15:50:54 +08:00
|
|
|
/* Add nadvance_u64 decay intervals to epoch. */
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_copy(&delta, &arena->decay_interval);
|
2016-04-12 15:50:54 +08:00
|
|
|
nstime_imultiply(&delta, nadvance_u64);
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_add(&arena->decay_epoch, &delta);
|
2016-02-20 12:09:31 +08:00
|
|
|
|
|
|
|
/* Set a new deadline. */
|
|
|
|
arena_decay_deadline_init(arena);
|
|
|
|
|
|
|
|
/* Update the backlog. */
|
2016-04-12 15:50:54 +08:00
|
|
|
if (nadvance_u64 >= SMOOTHSTEP_NSTEPS) {
|
2016-02-20 12:09:31 +08:00
|
|
|
memset(arena->decay_backlog, 0, (SMOOTHSTEP_NSTEPS-1) *
|
|
|
|
sizeof(size_t));
|
|
|
|
} else {
|
2016-04-12 15:50:54 +08:00
|
|
|
size_t nadvance_z = (size_t)nadvance_u64;
|
|
|
|
|
|
|
|
assert((uint64_t)nadvance_z == nadvance_u64);
|
|
|
|
|
|
|
|
memmove(arena->decay_backlog, &arena->decay_backlog[nadvance_z],
|
|
|
|
(SMOOTHSTEP_NSTEPS - nadvance_z) * sizeof(size_t));
|
|
|
|
if (nadvance_z > 1) {
|
2016-02-20 12:09:31 +08:00
|
|
|
memset(&arena->decay_backlog[SMOOTHSTEP_NSTEPS -
|
2016-04-12 15:50:54 +08:00
|
|
|
nadvance_z], 0, (nadvance_z-1) * sizeof(size_t));
|
2016-02-20 12:09:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ndirty_delta = (arena->ndirty > arena->decay_ndirty) ? arena->ndirty -
|
|
|
|
arena->decay_ndirty : 0;
|
|
|
|
arena->decay_ndirty = arena->ndirty;
|
|
|
|
arena->decay_backlog[SMOOTHSTEP_NSTEPS-1] = ndirty_delta;
|
|
|
|
arena->decay_backlog_npages_limit =
|
|
|
|
arena_decay_backlog_npages_limit(arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
arena_decay_npages_limit(arena_t *arena)
|
|
|
|
{
|
|
|
|
size_t npages_limit;
|
|
|
|
|
|
|
|
assert(opt_purge == purge_mode_decay);
|
|
|
|
|
|
|
|
npages_limit = arena->decay_backlog_npages_limit;
|
|
|
|
|
|
|
|
/* Add in any dirty pages created during the current epoch. */
|
|
|
|
if (arena->ndirty > arena->decay_ndirty)
|
|
|
|
npages_limit += arena->ndirty - arena->decay_ndirty;
|
|
|
|
|
|
|
|
return (npages_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
arena_decay_init(arena_t *arena, ssize_t decay_time)
|
|
|
|
{
|
|
|
|
|
|
|
|
arena->decay_time = decay_time;
|
|
|
|
if (decay_time > 0) {
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_init2(&arena->decay_interval, decay_time, 0);
|
|
|
|
nstime_idivide(&arena->decay_interval, SMOOTHSTEP_NSTEPS);
|
2016-02-20 12:09:31 +08:00
|
|
|
}
|
|
|
|
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_init(&arena->decay_epoch, 0);
|
|
|
|
nstime_update(&arena->decay_epoch);
|
2016-02-20 12:09:31 +08:00
|
|
|
arena->decay_jitter_state = (uint64_t)(uintptr_t)arena;
|
|
|
|
arena_decay_deadline_init(arena);
|
|
|
|
arena->decay_ndirty = arena->ndirty;
|
|
|
|
arena->decay_backlog_npages_limit = 0;
|
|
|
|
memset(arena->decay_backlog, 0, SMOOTHSTEP_NSTEPS * sizeof(size_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
arena_decay_time_valid(ssize_t decay_time)
|
|
|
|
{
|
|
|
|
|
2016-03-03 14:41:32 +08:00
|
|
|
if (decay_time < -1)
|
|
|
|
return (false);
|
|
|
|
if (decay_time == -1 || (uint64_t)decay_time <= NSTIME_SEC_MAX)
|
|
|
|
return (true);
|
|
|
|
return (false);
|
2016-02-20 12:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_decay_time_get(tsdn_t *tsdn, arena_t *arena)
|
2016-02-20 12:09:31 +08:00
|
|
|
{
|
|
|
|
ssize_t decay_time;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2016-02-20 12:09:31 +08:00
|
|
|
decay_time = arena->decay_time;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2016-02-20 12:09:31 +08:00
|
|
|
|
|
|
|
return (decay_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_decay_time_set(tsdn_t *tsdn, arena_t *arena, ssize_t decay_time)
|
2016-02-20 12:09:31 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if (!arena_decay_time_valid(decay_time))
|
|
|
|
return (true);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2016-02-20 12:09:31 +08:00
|
|
|
/*
|
|
|
|
* Restart decay backlog from scratch, which may cause many dirty pages
|
|
|
|
* to be immediately purged. It would conceptually be possible to map
|
|
|
|
* the old backlog onto the new backlog, but there is no justification
|
|
|
|
* for such complexity since decay_time changes are intended to be
|
|
|
|
* infrequent, either between the {-1, 0, >0} states, or a one-time
|
|
|
|
* arbitrary change during initial arena configuration.
|
|
|
|
*/
|
|
|
|
arena_decay_init(arena, decay_time);
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_maybe_purge(tsdn, arena);
|
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2016-02-20 12:09:31 +08:00
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2016-02-20 11:51:23 +08:00
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_maybe_purge_ratio(tsdn_t *tsdn, arena_t *arena)
|
2010-03-15 08:36:10 +08:00
|
|
|
{
|
2012-10-31 06:42:37 +08:00
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
assert(opt_purge == purge_mode_ratio);
|
|
|
|
|
2012-10-31 06:42:37 +08:00
|
|
|
/* Don't purge if the option is disabled. */
|
2015-03-19 09:55:33 +08:00
|
|
|
if (arena->lg_dirty_mult < 0)
|
2012-10-31 06:42:37 +08:00
|
|
|
return;
|
2016-02-20 11:51:23 +08:00
|
|
|
|
2012-10-31 06:42:37 +08:00
|
|
|
/*
|
2015-06-23 09:50:32 +08:00
|
|
|
* Iterate, since preventing recursive purging could otherwise leave too
|
|
|
|
* many dirty pages.
|
2012-10-31 06:42:37 +08:00
|
|
|
*/
|
2015-06-23 09:50:32 +08:00
|
|
|
while (true) {
|
|
|
|
size_t threshold = (arena->nactive >> arena->lg_dirty_mult);
|
|
|
|
if (threshold < chunk_npages)
|
|
|
|
threshold = chunk_npages;
|
|
|
|
/*
|
|
|
|
* Don't purge unless the number of purgeable pages exceeds the
|
|
|
|
* threshold.
|
|
|
|
*/
|
|
|
|
if (arena->ndirty <= threshold)
|
|
|
|
return;
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_purge_to_limit(tsdn, arena, threshold);
|
2015-06-23 09:50:32 +08:00
|
|
|
}
|
2010-03-15 08:36:10 +08:00
|
|
|
}
|
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_maybe_purge_decay(tsdn_t *tsdn, arena_t *arena)
|
2016-02-20 12:09:31 +08:00
|
|
|
{
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_t time;
|
2016-02-20 12:09:31 +08:00
|
|
|
size_t ndirty_limit;
|
|
|
|
|
|
|
|
assert(opt_purge == purge_mode_decay);
|
|
|
|
|
|
|
|
/* Purge all or nothing if the option is disabled. */
|
|
|
|
if (arena->decay_time <= 0) {
|
|
|
|
if (arena->decay_time == 0)
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_purge_to_limit(tsdn, arena, 0);
|
2016-02-20 12:09:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_copy(&time, &arena->decay_epoch);
|
|
|
|
if (unlikely(nstime_update(&time))) {
|
2016-02-20 12:09:31 +08:00
|
|
|
/* Time went backwards. Force an epoch advance. */
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_copy(&time, &arena->decay_deadline);
|
2016-02-20 12:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (arena_decay_deadline_reached(arena, &time))
|
|
|
|
arena_decay_epoch_advance(arena, &time);
|
|
|
|
|
|
|
|
ndirty_limit = arena_decay_npages_limit(arena);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't try to purge unless the number of purgeable pages exceeds the
|
|
|
|
* current limit.
|
|
|
|
*/
|
|
|
|
if (arena->ndirty <= ndirty_limit)
|
|
|
|
return;
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_purge_to_limit(tsdn, arena, ndirty_limit);
|
2016-02-20 12:09:31 +08:00
|
|
|
}
|
|
|
|
|
2016-02-20 11:51:23 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_maybe_purge(tsdn_t *tsdn, arena_t *arena)
|
2016-02-20 11:51:23 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Don't recursively purge. */
|
|
|
|
if (arena->purging)
|
|
|
|
return;
|
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
if (opt_purge == purge_mode_ratio)
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_maybe_purge_ratio(tsdn, arena);
|
2016-02-20 12:09:31 +08:00
|
|
|
else
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_maybe_purge_decay(tsdn, arena);
|
2016-02-20 11:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-07-22 01:23:36 +08:00
|
|
|
static size_t
|
|
|
|
arena_dirty_count(arena_t *arena)
|
|
|
|
{
|
|
|
|
size_t ndirty = 0;
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_runs_dirty_link_t *rdelm;
|
2015-02-16 10:04:46 +08:00
|
|
|
extent_node_t *chunkselm;
|
|
|
|
|
2015-03-11 09:15:40 +08:00
|
|
|
for (rdelm = qr_next(&arena->runs_dirty, rd_link),
|
2015-02-18 17:15:50 +08:00
|
|
|
chunkselm = qr_next(&arena->chunks_cache, cc_link);
|
2015-03-11 09:15:40 +08:00
|
|
|
rdelm != &arena->runs_dirty; rdelm = qr_next(rdelm, rd_link)) {
|
2015-02-16 10:04:46 +08:00
|
|
|
size_t npages;
|
|
|
|
|
2015-03-11 09:29:49 +08:00
|
|
|
if (rdelm == &chunkselm->rd) {
|
2015-02-16 10:04:46 +08:00
|
|
|
npages = extent_node_size_get(chunkselm) >> LG_PAGE;
|
2015-02-18 17:15:50 +08:00
|
|
|
chunkselm = qr_next(chunkselm, cc_link);
|
2015-02-16 10:04:46 +08:00
|
|
|
} else {
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(
|
|
|
|
rdelm);
|
|
|
|
arena_chunk_map_misc_t *miscelm =
|
|
|
|
arena_rd_to_miscelm(rdelm);
|
|
|
|
size_t pageind = arena_miscelm_to_pageind(miscelm);
|
2015-02-16 10:04:46 +08:00
|
|
|
assert(arena_mapbits_allocated_get(chunk, pageind) ==
|
|
|
|
0);
|
|
|
|
assert(arena_mapbits_large_get(chunk, pageind) == 0);
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, pageind) != 0);
|
|
|
|
npages = arena_mapbits_unallocated_size_get(chunk,
|
|
|
|
pageind) >> LG_PAGE;
|
|
|
|
}
|
2014-07-22 01:23:36 +08:00
|
|
|
ndirty += npages;
|
|
|
|
}
|
|
|
|
|
2014-11-01 17:29:10 +08:00
|
|
|
return (ndirty);
|
2014-01-15 08:23:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_stash_dirty(tsdn_t *tsdn, arena_t *arena, chunk_hooks_t *chunk_hooks,
|
2016-02-20 11:51:23 +08:00
|
|
|
size_t ndirty_limit, arena_runs_dirty_link_t *purge_runs_sentinel,
|
2015-02-16 10:04:46 +08:00
|
|
|
extent_node_t *purge_chunks_sentinel)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_runs_dirty_link_t *rdelm, *rdelm_next;
|
2015-02-16 10:04:46 +08:00
|
|
|
extent_node_t *chunkselm;
|
2014-07-22 09:09:04 +08:00
|
|
|
size_t nstashed = 0;
|
2012-10-31 06:42:37 +08:00
|
|
|
|
2016-02-20 11:51:23 +08:00
|
|
|
/* Stash runs/chunks according to ndirty_limit. */
|
2015-03-11 09:15:40 +08:00
|
|
|
for (rdelm = qr_next(&arena->runs_dirty, rd_link),
|
2015-02-18 17:15:50 +08:00
|
|
|
chunkselm = qr_next(&arena->chunks_cache, cc_link);
|
2015-03-11 09:15:40 +08:00
|
|
|
rdelm != &arena->runs_dirty; rdelm = rdelm_next) {
|
2015-02-16 10:04:46 +08:00
|
|
|
size_t npages;
|
2015-03-11 09:15:40 +08:00
|
|
|
rdelm_next = qr_next(rdelm, rd_link);
|
2015-02-16 10:04:46 +08:00
|
|
|
|
2015-03-11 09:29:49 +08:00
|
|
|
if (rdelm == &chunkselm->rd) {
|
2015-02-19 08:40:53 +08:00
|
|
|
extent_node_t *chunkselm_next;
|
|
|
|
bool zero;
|
2015-02-16 10:04:46 +08:00
|
|
|
UNUSED void *chunk;
|
|
|
|
|
2016-02-20 11:51:23 +08:00
|
|
|
npages = extent_node_size_get(chunkselm) >> LG_PAGE;
|
2016-02-20 12:09:31 +08:00
|
|
|
if (opt_purge == purge_mode_decay && arena->ndirty -
|
|
|
|
(nstashed + npages) < ndirty_limit)
|
|
|
|
break;
|
2016-02-20 11:51:23 +08:00
|
|
|
|
2015-02-18 17:15:50 +08:00
|
|
|
chunkselm_next = qr_next(chunkselm, cc_link);
|
2015-02-16 10:04:46 +08:00
|
|
|
/*
|
2015-02-19 08:40:53 +08:00
|
|
|
* Allocate. chunkselm remains valid due to the
|
|
|
|
* dalloc_node=false argument to chunk_alloc_cache().
|
2015-02-16 10:04:46 +08:00
|
|
|
*/
|
|
|
|
zero = false;
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk = chunk_alloc_cache(tsdn, arena, chunk_hooks,
|
2015-02-19 08:40:53 +08:00
|
|
|
extent_node_addr_get(chunkselm),
|
|
|
|
extent_node_size_get(chunkselm), chunksize, &zero,
|
|
|
|
false);
|
|
|
|
assert(chunk == extent_node_addr_get(chunkselm));
|
|
|
|
assert(zero == extent_node_zeroed_get(chunkselm));
|
|
|
|
extent_node_dirty_insert(chunkselm, purge_runs_sentinel,
|
2015-02-18 17:15:50 +08:00
|
|
|
purge_chunks_sentinel);
|
2016-02-20 11:51:23 +08:00
|
|
|
assert(npages == (extent_node_size_get(chunkselm) >>
|
|
|
|
LG_PAGE));
|
2015-02-16 10:04:46 +08:00
|
|
|
chunkselm = chunkselm_next;
|
|
|
|
} else {
|
|
|
|
arena_chunk_t *chunk =
|
2015-03-11 09:15:40 +08:00
|
|
|
(arena_chunk_t *)CHUNK_ADDR2BASE(rdelm);
|
|
|
|
arena_chunk_map_misc_t *miscelm =
|
|
|
|
arena_rd_to_miscelm(rdelm);
|
|
|
|
size_t pageind = arena_miscelm_to_pageind(miscelm);
|
|
|
|
arena_run_t *run = &miscelm->run;
|
2015-02-16 10:04:46 +08:00
|
|
|
size_t run_size =
|
|
|
|
arena_mapbits_unallocated_size_get(chunk, pageind);
|
2014-07-22 09:09:04 +08:00
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
npages = run_size >> LG_PAGE;
|
2016-02-20 12:09:31 +08:00
|
|
|
if (opt_purge == purge_mode_decay && arena->ndirty -
|
|
|
|
(nstashed + npages) < ndirty_limit)
|
|
|
|
break;
|
2014-07-22 09:09:04 +08:00
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
assert(pageind + npages <= chunk_npages);
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, pageind) ==
|
|
|
|
arena_mapbits_dirty_get(chunk, pageind+npages-1));
|
2014-08-15 05:45:58 +08:00
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
/*
|
|
|
|
* If purging the spare chunk's run, make it available
|
|
|
|
* prior to allocation.
|
|
|
|
*/
|
|
|
|
if (chunk == arena->spare)
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_alloc(tsdn, arena);
|
2015-02-16 10:04:46 +08:00
|
|
|
|
|
|
|
/* Temporarily allocate the free dirty run. */
|
|
|
|
arena_run_split_large(arena, run, run_size, false);
|
2015-02-18 14:25:56 +08:00
|
|
|
/* Stash. */
|
2015-02-16 10:04:46 +08:00
|
|
|
if (false)
|
2015-03-11 09:15:40 +08:00
|
|
|
qr_new(rdelm, rd_link); /* Redundant. */
|
2015-02-16 10:04:46 +08:00
|
|
|
else {
|
2015-03-11 09:15:40 +08:00
|
|
|
assert(qr_next(rdelm, rd_link) == rdelm);
|
|
|
|
assert(qr_prev(rdelm, rd_link) == rdelm);
|
2015-02-16 10:04:46 +08:00
|
|
|
}
|
2015-03-11 09:15:40 +08:00
|
|
|
qr_meld(purge_runs_sentinel, rdelm, rd_link);
|
2015-02-16 10:04:46 +08:00
|
|
|
}
|
2014-07-22 09:09:04 +08:00
|
|
|
|
|
|
|
nstashed += npages;
|
2016-02-20 12:09:31 +08:00
|
|
|
if (opt_purge == purge_mode_ratio && arena->ndirty - nstashed <=
|
|
|
|
ndirty_limit)
|
2014-07-22 09:09:04 +08:00
|
|
|
break;
|
2010-03-15 08:36:10 +08:00
|
|
|
}
|
2014-07-22 09:09:04 +08:00
|
|
|
|
|
|
|
return (nstashed);
|
2014-01-15 08:23:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_purge_stashed(tsdn_t *tsdn, arena_t *arena, chunk_hooks_t *chunk_hooks,
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_runs_dirty_link_t *purge_runs_sentinel,
|
2015-02-16 10:04:46 +08:00
|
|
|
extent_node_t *purge_chunks_sentinel)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
2014-07-22 09:09:04 +08:00
|
|
|
size_t npurged, nmadvise;
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_runs_dirty_link_t *rdelm;
|
2015-02-16 10:04:46 +08:00
|
|
|
extent_node_t *chunkselm;
|
2010-03-15 08:36:10 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats)
|
|
|
|
nmadvise = 0;
|
2012-10-31 06:42:37 +08:00
|
|
|
npurged = 0;
|
2014-07-22 09:09:04 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2015-03-11 09:15:40 +08:00
|
|
|
for (rdelm = qr_next(purge_runs_sentinel, rd_link),
|
2015-02-18 17:15:50 +08:00
|
|
|
chunkselm = qr_next(purge_chunks_sentinel, cc_link);
|
2015-03-11 09:15:40 +08:00
|
|
|
rdelm != purge_runs_sentinel; rdelm = qr_next(rdelm, rd_link)) {
|
2015-02-16 10:04:46 +08:00
|
|
|
size_t npages;
|
|
|
|
|
2015-03-11 09:29:49 +08:00
|
|
|
if (rdelm == &chunkselm->rd) {
|
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
|
|
|
/*
|
|
|
|
* Don't actually purge the chunk here because 1)
|
|
|
|
* chunkselm is embedded in the chunk and must remain
|
|
|
|
* valid, and 2) we deallocate the chunk in
|
|
|
|
* arena_unstash_purged(), where it is destroyed,
|
|
|
|
* decommitted, or purged, depending on chunk
|
|
|
|
* deallocation policy.
|
|
|
|
*/
|
2015-02-16 10:04:46 +08:00
|
|
|
size_t size = extent_node_size_get(chunkselm);
|
|
|
|
npages = size >> LG_PAGE;
|
2015-02-18 17:15:50 +08:00
|
|
|
chunkselm = qr_next(chunkselm, cc_link);
|
2015-02-16 10:04:46 +08:00
|
|
|
} else {
|
2015-08-11 14:03:34 +08:00
|
|
|
size_t pageind, run_size, flag_unzeroed, flags, i;
|
|
|
|
bool decommitted;
|
2015-08-05 01:49:46 +08:00
|
|
|
arena_chunk_t *chunk =
|
|
|
|
(arena_chunk_t *)CHUNK_ADDR2BASE(rdelm);
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm =
|
|
|
|
arena_rd_to_miscelm(rdelm);
|
|
|
|
pageind = arena_miscelm_to_pageind(miscelm);
|
2015-02-16 10:04:46 +08:00
|
|
|
run_size = arena_mapbits_large_size_get(chunk, pageind);
|
|
|
|
npages = run_size >> LG_PAGE;
|
2014-07-22 09:09:04 +08:00
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
assert(pageind + npages <= chunk_npages);
|
2015-08-10 07:47:27 +08:00
|
|
|
assert(!arena_mapbits_decommitted_get(chunk, pageind));
|
|
|
|
assert(!arena_mapbits_decommitted_get(chunk,
|
|
|
|
pageind+npages-1));
|
2015-08-05 01:49:46 +08:00
|
|
|
decommitted = !chunk_hooks->decommit(chunk, chunksize,
|
|
|
|
pageind << LG_PAGE, npages << LG_PAGE, arena->ind);
|
|
|
|
if (decommitted) {
|
2015-08-11 14:03:34 +08:00
|
|
|
flag_unzeroed = 0;
|
|
|
|
flags = CHUNK_MAP_DECOMMITTED;
|
2015-08-05 01:49:46 +08:00
|
|
|
} else {
|
2016-05-11 13:21:10 +08:00
|
|
|
flag_unzeroed = chunk_purge_wrapper(tsdn, arena,
|
2015-08-05 01:49:46 +08:00
|
|
|
chunk_hooks, chunk, chunksize, pageind <<
|
2015-08-11 14:03:34 +08:00
|
|
|
LG_PAGE, run_size) ? CHUNK_MAP_UNZEROED : 0;
|
|
|
|
flags = flag_unzeroed;
|
2015-08-05 01:49:46 +08:00
|
|
|
}
|
2015-08-11 14:03:34 +08:00
|
|
|
arena_mapbits_large_set(chunk, pageind+npages-1, 0,
|
|
|
|
flags);
|
|
|
|
arena_mapbits_large_set(chunk, pageind, run_size,
|
|
|
|
flags);
|
2014-07-22 09:09:04 +08:00
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
/*
|
2015-08-11 14:03:34 +08:00
|
|
|
* Set the unzeroed flag for internal pages, now that
|
2015-03-19 09:55:33 +08:00
|
|
|
* chunk_purge_wrapper() has returned whether the pages
|
|
|
|
* were zeroed as a side effect of purging. This chunk
|
|
|
|
* map modification is safe even though the arena mutex
|
2015-02-16 10:04:46 +08:00
|
|
|
* isn't currently owned by this thread, because the run
|
|
|
|
* is marked as allocated, thus protecting it from being
|
|
|
|
* modified by any other thread. As long as these
|
|
|
|
* writes don't perturb the first and last elements'
|
|
|
|
* CHUNK_MAP_ALLOCATED bits, behavior is well defined.
|
|
|
|
*/
|
2015-08-11 14:03:34 +08:00
|
|
|
for (i = 1; i < npages-1; i++) {
|
|
|
|
arena_mapbits_internal_set(chunk, pageind+i,
|
2015-02-16 10:04:46 +08:00
|
|
|
flag_unzeroed);
|
|
|
|
}
|
2012-10-09 08:56:11 +08:00
|
|
|
}
|
2014-07-22 09:09:04 +08:00
|
|
|
|
2012-10-31 06:42:37 +08:00
|
|
|
npurged += npages;
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats)
|
|
|
|
nmadvise++;
|
2010-03-15 08:36:10 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2014-07-22 09:09:04 +08:00
|
|
|
|
|
|
|
if (config_stats) {
|
2012-02-11 12:22:09 +08:00
|
|
|
arena->stats.nmadvise += nmadvise;
|
2014-07-22 09:09:04 +08:00
|
|
|
arena->stats.purged += npurged;
|
|
|
|
}
|
2010-03-15 08:36:10 +08:00
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
return (npurged);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_unstash_purged(tsdn_t *tsdn, arena_t *arena, chunk_hooks_t *chunk_hooks,
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_runs_dirty_link_t *purge_runs_sentinel,
|
2015-02-16 10:04:46 +08:00
|
|
|
extent_node_t *purge_chunks_sentinel)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_runs_dirty_link_t *rdelm, *rdelm_next;
|
2015-02-16 10:04:46 +08:00
|
|
|
extent_node_t *chunkselm;
|
2014-01-15 08:23:03 +08:00
|
|
|
|
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
|
|
|
/* Deallocate chunks/runs. */
|
2015-03-11 09:15:40 +08:00
|
|
|
for (rdelm = qr_next(purge_runs_sentinel, rd_link),
|
2015-02-18 17:15:50 +08:00
|
|
|
chunkselm = qr_next(purge_chunks_sentinel, cc_link);
|
2015-03-11 09:15:40 +08:00
|
|
|
rdelm != purge_runs_sentinel; rdelm = rdelm_next) {
|
|
|
|
rdelm_next = qr_next(rdelm, rd_link);
|
2015-03-11 09:29:49 +08:00
|
|
|
if (rdelm == &chunkselm->rd) {
|
2015-02-16 10:04:46 +08:00
|
|
|
extent_node_t *chunkselm_next = qr_next(chunkselm,
|
2015-02-18 17:15:50 +08:00
|
|
|
cc_link);
|
2015-02-18 14:25:56 +08:00
|
|
|
void *addr = extent_node_addr_get(chunkselm);
|
|
|
|
size_t size = extent_node_size_get(chunkselm);
|
2015-02-18 17:15:50 +08:00
|
|
|
bool zeroed = extent_node_zeroed_get(chunkselm);
|
2015-08-10 07:47:27 +08:00
|
|
|
bool committed = extent_node_committed_get(chunkselm);
|
2015-02-18 17:15:50 +08:00
|
|
|
extent_node_dirty_remove(chunkselm);
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_node_dalloc(tsdn, arena, chunkselm);
|
2015-02-16 10:04:46 +08:00
|
|
|
chunkselm = chunkselm_next;
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_dalloc_wrapper(tsdn, arena, chunk_hooks, addr,
|
2016-04-14 14:36:15 +08:00
|
|
|
size, zeroed, committed);
|
2015-02-16 10:04:46 +08:00
|
|
|
} else {
|
2015-08-05 01:49:46 +08:00
|
|
|
arena_chunk_t *chunk =
|
|
|
|
(arena_chunk_t *)CHUNK_ADDR2BASE(rdelm);
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm =
|
|
|
|
arena_rd_to_miscelm(rdelm);
|
2015-08-05 01:49:46 +08:00
|
|
|
size_t pageind = arena_miscelm_to_pageind(miscelm);
|
|
|
|
bool decommitted = (arena_mapbits_decommitted_get(chunk,
|
|
|
|
pageind) != 0);
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_run_t *run = &miscelm->run;
|
|
|
|
qr_remove(rdelm, rd_link);
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_dalloc(tsdn, arena, run, false, true,
|
2016-04-14 14:36:15 +08:00
|
|
|
decommitted);
|
2015-02-16 10:04:46 +08:00
|
|
|
}
|
2010-03-15 08:36:10 +08:00
|
|
|
}
|
2012-10-31 06:42:37 +08:00
|
|
|
}
|
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
/*
|
|
|
|
* NB: ndirty_limit is interpreted differently depending on opt_purge:
|
|
|
|
* - purge_mode_ratio: Purge as few dirty run/chunks as possible to reach the
|
|
|
|
* desired state:
|
|
|
|
* (arena->ndirty <= ndirty_limit)
|
|
|
|
* - purge_mode_decay: Purge as many dirty runs/chunks as possible without
|
|
|
|
* violating the invariant:
|
|
|
|
* (arena->ndirty >= ndirty_limit)
|
|
|
|
*/
|
2015-03-19 09:55:33 +08:00
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_purge_to_limit(tsdn_t *tsdn, arena_t *arena, size_t ndirty_limit)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2016-05-11 13:21:10 +08:00
|
|
|
chunk_hooks_t chunk_hooks = chunk_hooks_get(tsdn, arena);
|
2016-02-20 11:51:23 +08:00
|
|
|
size_t npurge, npurged;
|
2015-03-11 09:15:40 +08:00
|
|
|
arena_runs_dirty_link_t purge_runs_sentinel;
|
2015-02-16 10:04:46 +08:00
|
|
|
extent_node_t purge_chunks_sentinel;
|
2014-07-22 09:09:04 +08:00
|
|
|
|
2015-06-23 09:50:32 +08:00
|
|
|
arena->purging = true;
|
|
|
|
|
2014-11-01 17:29:10 +08:00
|
|
|
/*
|
|
|
|
* Calls to arena_dirty_count() are disabled even for debug builds
|
|
|
|
* because overhead grows nonlinearly as memory usage increases.
|
|
|
|
*/
|
|
|
|
if (false && config_debug) {
|
2014-07-22 10:39:20 +08:00
|
|
|
size_t ndirty = arena_dirty_count(arena);
|
2014-07-22 01:23:36 +08:00
|
|
|
assert(ndirty == arena->ndirty);
|
2010-03-05 13:35:07 +08:00
|
|
|
}
|
2016-02-20 12:09:31 +08:00
|
|
|
assert(opt_purge != purge_mode_ratio || (arena->nactive >>
|
|
|
|
arena->lg_dirty_mult) < arena->ndirty || ndirty_limit == 0);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
qr_new(&purge_runs_sentinel, rd_link);
|
2015-02-18 14:23:10 +08:00
|
|
|
extent_node_dirty_linkage_init(&purge_chunks_sentinel);
|
2015-02-16 10:04:46 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
npurge = arena_stash_dirty(tsdn, arena, &chunk_hooks, ndirty_limit,
|
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
|
|
|
&purge_runs_sentinel, &purge_chunks_sentinel);
|
2016-02-20 11:51:23 +08:00
|
|
|
if (npurge == 0)
|
|
|
|
goto label_return;
|
2016-05-11 13:21:10 +08:00
|
|
|
npurged = arena_purge_stashed(tsdn, arena, &chunk_hooks,
|
2016-04-14 14:36:15 +08:00
|
|
|
&purge_runs_sentinel, &purge_chunks_sentinel);
|
2016-02-20 11:51:23 +08:00
|
|
|
assert(npurged == npurge);
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_unstash_purged(tsdn, arena, &chunk_hooks, &purge_runs_sentinel,
|
2015-02-16 10:04:46 +08:00
|
|
|
&purge_chunks_sentinel);
|
2015-06-23 09:50:32 +08:00
|
|
|
|
2016-02-20 11:51:23 +08:00
|
|
|
if (config_stats)
|
|
|
|
arena->stats.npurge++;
|
|
|
|
|
|
|
|
label_return:
|
2015-06-23 09:50:32 +08:00
|
|
|
arena->purging = false;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2010-10-01 07:55:08 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_purge(tsdn_t *tsdn, arena_t *arena, bool all)
|
2010-10-01 07:55:08 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2016-02-20 12:09:31 +08:00
|
|
|
if (all)
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_purge_to_limit(tsdn, arena, 0);
|
2016-02-20 12:09:31 +08:00
|
|
|
else
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_maybe_purge(tsdn, arena);
|
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2010-10-01 07:55:08 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 05:37:17 +08:00
|
|
|
static void
|
|
|
|
arena_achunk_prof_reset(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk)
|
|
|
|
{
|
|
|
|
size_t pageind, npages;
|
|
|
|
|
|
|
|
cassert(config_prof);
|
|
|
|
assert(opt_prof);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate over the allocated runs and remove profiled allocations from
|
|
|
|
* the sample set.
|
|
|
|
*/
|
|
|
|
for (pageind = map_bias; pageind < chunk_npages; pageind += npages) {
|
|
|
|
if (arena_mapbits_allocated_get(chunk, pageind) != 0) {
|
|
|
|
if (arena_mapbits_large_get(chunk, pageind) != 0) {
|
|
|
|
void *ptr = (void *)((uintptr_t)chunk + (pageind
|
|
|
|
<< LG_PAGE));
|
2016-05-11 13:21:10 +08:00
|
|
|
size_t usize = isalloc(tsd_tsdn(tsd), ptr,
|
|
|
|
config_prof);
|
2016-04-23 05:37:17 +08:00
|
|
|
|
|
|
|
prof_free(tsd, ptr, usize);
|
|
|
|
npages = arena_mapbits_large_size_get(chunk,
|
|
|
|
pageind) >> LG_PAGE;
|
|
|
|
} else {
|
|
|
|
/* Skip small run. */
|
|
|
|
size_t binind = arena_mapbits_binind_get(chunk,
|
|
|
|
pageind);
|
|
|
|
arena_bin_info_t *bin_info =
|
|
|
|
&arena_bin_info[binind];
|
|
|
|
npages = bin_info->run_size >> LG_PAGE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Skip unallocated run. */
|
|
|
|
npages = arena_mapbits_unallocated_size_get(chunk,
|
|
|
|
pageind) >> LG_PAGE;
|
|
|
|
}
|
|
|
|
assert(pageind + npages <= chunk_npages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
arena_reset(tsd_t *tsd, arena_t *arena)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
extent_node_t *node;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Locking in this function is unintuitive. The caller guarantees that
|
|
|
|
* no concurrent operations are happening in this arena, but there are
|
|
|
|
* still reasons that some locking is necessary:
|
|
|
|
*
|
|
|
|
* - Some of the functions in the transitive closure of calls assume
|
|
|
|
* appropriate locks are held, and in some cases these locks are
|
|
|
|
* temporarily dropped to avoid lock order reversal or deadlock due to
|
|
|
|
* reentry.
|
|
|
|
* - mallctl("epoch", ...) may concurrently refresh stats. While
|
|
|
|
* strictly speaking this is a "concurrent operation", disallowing
|
|
|
|
* stats refreshes would impose an inconvenient burden.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Remove large allocations from prof sample set. */
|
|
|
|
if (config_prof && opt_prof) {
|
|
|
|
ql_foreach(node, &arena->achunks, ql_link) {
|
|
|
|
arena_achunk_prof_reset(tsd, arena,
|
|
|
|
extent_node_addr_get(node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 04:26:54 +08:00
|
|
|
/* Reset curruns for large size classes. */
|
|
|
|
if (config_stats) {
|
|
|
|
for (i = 0; i < nlclasses; i++)
|
|
|
|
arena->stats.lstats[i].curruns = 0;
|
|
|
|
}
|
|
|
|
|
2016-04-23 05:37:17 +08:00
|
|
|
/* Huge allocations. */
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &arena->huge_mtx);
|
2016-04-23 05:37:17 +08:00
|
|
|
for (node = ql_last(&arena->huge, ql_link); node != NULL; node =
|
|
|
|
ql_last(&arena->huge, ql_link)) {
|
|
|
|
void *ptr = extent_node_addr_get(node);
|
2016-04-26 04:26:54 +08:00
|
|
|
size_t usize;
|
2016-04-23 05:37:17 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &arena->huge_mtx);
|
2016-04-26 04:26:54 +08:00
|
|
|
if (config_stats || (config_prof && opt_prof))
|
2016-05-11 13:21:10 +08:00
|
|
|
usize = isalloc(tsd_tsdn(tsd), ptr, config_prof);
|
2016-04-26 04:26:54 +08:00
|
|
|
/* Remove huge allocation from prof sample set. */
|
|
|
|
if (config_prof && opt_prof)
|
2016-04-23 05:37:17 +08:00
|
|
|
prof_free(tsd, ptr, usize);
|
2016-05-11 13:21:10 +08:00
|
|
|
huge_dalloc(tsd_tsdn(tsd), ptr);
|
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &arena->huge_mtx);
|
2016-04-26 04:26:54 +08:00
|
|
|
/* Cancel out unwanted effects on stats. */
|
|
|
|
if (config_stats)
|
|
|
|
arena_huge_reset_stats_cancel(arena, usize);
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &arena->huge_mtx);
|
2016-04-23 05:37:17 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &arena->lock);
|
2016-04-23 05:37:17 +08:00
|
|
|
|
|
|
|
/* Bins. */
|
|
|
|
for (i = 0; i < NBINS; i++) {
|
|
|
|
arena_bin_t *bin = &arena->bins[i];
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
|
2016-04-23 05:37:17 +08:00
|
|
|
bin->runcur = NULL;
|
|
|
|
arena_run_heap_new(&bin->runs);
|
|
|
|
if (config_stats) {
|
|
|
|
bin->stats.curregs = 0;
|
|
|
|
bin->stats.curruns = 0;
|
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-initialize runs_dirty such that the chunks_cache and runs_dirty
|
|
|
|
* chains directly correspond.
|
|
|
|
*/
|
|
|
|
qr_new(&arena->runs_dirty, rd_link);
|
|
|
|
for (node = qr_next(&arena->chunks_cache, cc_link);
|
|
|
|
node != &arena->chunks_cache; node = qr_next(node, cc_link)) {
|
|
|
|
qr_new(&node->rd, rd_link);
|
|
|
|
qr_meld(&arena->runs_dirty, &node->rd, rd_link);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Arena chunks. */
|
|
|
|
for (node = ql_last(&arena->achunks, ql_link); node != NULL; node =
|
|
|
|
ql_last(&arena->achunks, ql_link)) {
|
|
|
|
ql_remove(&arena->achunks, node, ql_link);
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_discard(tsd_tsdn(tsd), arena,
|
|
|
|
extent_node_addr_get(node));
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Spare. */
|
|
|
|
if (arena->spare != NULL) {
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_discard(tsd_tsdn(tsd), arena, arena->spare);
|
2016-04-23 05:37:17 +08:00
|
|
|
arena->spare = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!arena->purging);
|
|
|
|
arena->nactive = 0;
|
|
|
|
|
|
|
|
for(i = 0; i < runs_avail_nclasses; i++)
|
|
|
|
arena_run_heap_new(&arena->runs_avail[i]);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsd_tsdn(tsd), &arena->lock);
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
static void
|
2014-01-15 08:23:03 +08:00
|
|
|
arena_run_coalesce(arena_t *arena, arena_chunk_t *chunk, size_t *p_size,
|
2015-08-05 01:49:46 +08:00
|
|
|
size_t *p_run_ind, size_t *p_run_pages, size_t flag_dirty,
|
|
|
|
size_t flag_decommitted)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2014-01-15 08:23:03 +08:00
|
|
|
size_t size = *p_size;
|
|
|
|
size_t run_ind = *p_run_ind;
|
|
|
|
size_t run_pages = *p_run_pages;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/* Try to coalesce forward. */
|
|
|
|
if (run_ind + run_pages < chunk_npages &&
|
2012-05-02 15:30:36 +08:00
|
|
|
arena_mapbits_allocated_get(chunk, run_ind+run_pages) == 0 &&
|
2015-08-05 01:49:46 +08:00
|
|
|
arena_mapbits_dirty_get(chunk, run_ind+run_pages) == flag_dirty &&
|
|
|
|
arena_mapbits_decommitted_get(chunk, run_ind+run_pages) ==
|
|
|
|
flag_decommitted) {
|
2012-05-02 15:30:36 +08:00
|
|
|
size_t nrun_size = arena_mapbits_unallocated_size_get(chunk,
|
|
|
|
run_ind+run_pages);
|
2012-04-02 22:04:34 +08:00
|
|
|
size_t nrun_pages = nrun_size >> LG_PAGE;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove successor from runs_avail; the coalesced run is
|
|
|
|
* inserted later.
|
|
|
|
*/
|
2012-05-02 15:30:36 +08:00
|
|
|
assert(arena_mapbits_unallocated_size_get(chunk,
|
|
|
|
run_ind+run_pages+nrun_pages-1) == nrun_size);
|
|
|
|
assert(arena_mapbits_dirty_get(chunk,
|
|
|
|
run_ind+run_pages+nrun_pages-1) == flag_dirty);
|
2015-08-05 01:49:46 +08:00
|
|
|
assert(arena_mapbits_decommitted_get(chunk,
|
|
|
|
run_ind+run_pages+nrun_pages-1) == flag_decommitted);
|
2014-07-22 10:39:20 +08:00
|
|
|
arena_avail_remove(arena, chunk, run_ind+run_pages, nrun_pages);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
/*
|
|
|
|
* If the successor is dirty, remove it from the set of dirty
|
|
|
|
* pages.
|
|
|
|
*/
|
2014-07-19 05:21:17 +08:00
|
|
|
if (flag_dirty != 0) {
|
2015-02-16 10:04:46 +08:00
|
|
|
arena_run_dirty_remove(arena, chunk, run_ind+run_pages,
|
2014-08-15 05:45:58 +08:00
|
|
|
nrun_pages);
|
2014-07-19 05:21:17 +08:00
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
size += nrun_size;
|
2010-10-18 10:56:09 +08:00
|
|
|
run_pages += nrun_pages;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-05-02 15:30:36 +08:00
|
|
|
arena_mapbits_unallocated_size_set(chunk, run_ind, size);
|
|
|
|
arena_mapbits_unallocated_size_set(chunk, run_ind+run_pages-1,
|
|
|
|
size);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to coalesce backward. */
|
2014-01-15 08:23:03 +08:00
|
|
|
if (run_ind > map_bias && arena_mapbits_allocated_get(chunk,
|
|
|
|
run_ind-1) == 0 && arena_mapbits_dirty_get(chunk, run_ind-1) ==
|
2015-08-05 01:49:46 +08:00
|
|
|
flag_dirty && arena_mapbits_decommitted_get(chunk, run_ind-1) ==
|
|
|
|
flag_decommitted) {
|
2012-05-02 15:30:36 +08:00
|
|
|
size_t prun_size = arena_mapbits_unallocated_size_get(chunk,
|
|
|
|
run_ind-1);
|
2012-04-02 22:04:34 +08:00
|
|
|
size_t prun_pages = prun_size >> LG_PAGE;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2010-10-18 10:56:09 +08:00
|
|
|
run_ind -= prun_pages;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove predecessor from runs_avail; the coalesced run is
|
|
|
|
* inserted later.
|
|
|
|
*/
|
2012-05-02 15:30:36 +08:00
|
|
|
assert(arena_mapbits_unallocated_size_get(chunk, run_ind) ==
|
|
|
|
prun_size);
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, run_ind) == flag_dirty);
|
2015-08-05 01:49:46 +08:00
|
|
|
assert(arena_mapbits_decommitted_get(chunk, run_ind) ==
|
|
|
|
flag_decommitted);
|
2014-07-22 10:39:20 +08:00
|
|
|
arena_avail_remove(arena, chunk, run_ind, prun_pages);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
/*
|
|
|
|
* If the predecessor is dirty, remove it from the set of dirty
|
|
|
|
* pages.
|
|
|
|
*/
|
|
|
|
if (flag_dirty != 0) {
|
|
|
|
arena_run_dirty_remove(arena, chunk, run_ind,
|
|
|
|
prun_pages);
|
|
|
|
}
|
2014-07-19 05:21:17 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
size += prun_size;
|
2010-10-18 10:56:09 +08:00
|
|
|
run_pages += prun_pages;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-05-02 15:30:36 +08:00
|
|
|
arena_mapbits_unallocated_size_set(chunk, run_ind, size);
|
|
|
|
arena_mapbits_unallocated_size_set(chunk, run_ind+run_pages-1,
|
|
|
|
size);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
*p_size = size;
|
|
|
|
*p_run_ind = run_ind;
|
|
|
|
*p_run_pages = run_pages;
|
|
|
|
}
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
static size_t
|
|
|
|
arena_run_size_get(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run,
|
|
|
|
size_t run_ind)
|
2014-01-15 08:23:03 +08:00
|
|
|
{
|
2015-08-05 01:49:46 +08:00
|
|
|
size_t size;
|
2014-01-15 08:23:03 +08:00
|
|
|
|
|
|
|
assert(run_ind >= map_bias);
|
|
|
|
assert(run_ind < chunk_npages);
|
2015-08-05 01:49:46 +08:00
|
|
|
|
2014-01-15 08:23:03 +08:00
|
|
|
if (arena_mapbits_large_get(chunk, run_ind) != 0) {
|
|
|
|
size = arena_mapbits_large_size_get(chunk, run_ind);
|
2015-08-05 01:49:46 +08:00
|
|
|
assert(size == PAGE || arena_mapbits_large_size_get(chunk,
|
2014-01-15 08:23:03 +08:00
|
|
|
run_ind+(size>>LG_PAGE)-1) == 0);
|
|
|
|
} else {
|
2014-10-11 14:01:03 +08:00
|
|
|
arena_bin_info_t *bin_info = &arena_bin_info[run->binind];
|
2014-01-15 08:23:03 +08:00
|
|
|
size = bin_info->run_size;
|
|
|
|
}
|
2015-08-05 01:49:46 +08:00
|
|
|
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_dalloc(tsdn_t *tsdn, arena_t *arena, arena_run_t *run, bool dirty,
|
2016-04-14 14:36:15 +08:00
|
|
|
bool cleaned, bool decommitted)
|
2015-08-05 01:49:46 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
arena_chunk_map_misc_t *miscelm;
|
|
|
|
size_t size, run_ind, run_pages, flag_dirty, flag_decommitted;
|
|
|
|
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
|
|
|
|
miscelm = arena_run_to_miscelm(run);
|
|
|
|
run_ind = arena_miscelm_to_pageind(miscelm);
|
|
|
|
assert(run_ind >= map_bias);
|
|
|
|
assert(run_ind < chunk_npages);
|
|
|
|
size = arena_run_size_get(arena, chunk, run, run_ind);
|
2014-01-15 08:23:03 +08:00
|
|
|
run_pages = (size >> LG_PAGE);
|
2016-02-28 04:34:50 +08:00
|
|
|
arena_nactive_sub(arena, run_pages);
|
2014-01-15 08:23:03 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The run is dirty if the caller claims to have dirtied it, as well as
|
|
|
|
* if it was already dirty before being allocated and the caller
|
|
|
|
* doesn't claim to have cleaned it.
|
|
|
|
*/
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, run_ind) ==
|
|
|
|
arena_mapbits_dirty_get(chunk, run_ind+run_pages-1));
|
2015-08-05 01:49:46 +08:00
|
|
|
if (!cleaned && !decommitted && arena_mapbits_dirty_get(chunk, run_ind)
|
|
|
|
!= 0)
|
2014-01-15 08:23:03 +08:00
|
|
|
dirty = true;
|
|
|
|
flag_dirty = dirty ? CHUNK_MAP_DIRTY : 0;
|
2015-08-05 01:49:46 +08:00
|
|
|
flag_decommitted = decommitted ? CHUNK_MAP_DECOMMITTED : 0;
|
2014-01-15 08:23:03 +08:00
|
|
|
|
|
|
|
/* Mark pages as unallocated in the chunk map. */
|
2015-08-05 01:49:46 +08:00
|
|
|
if (dirty || decommitted) {
|
|
|
|
size_t flags = flag_dirty | flag_decommitted;
|
|
|
|
arena_mapbits_unallocated_set(chunk, run_ind, size, flags);
|
2014-01-15 08:23:03 +08:00
|
|
|
arena_mapbits_unallocated_set(chunk, run_ind+run_pages-1, size,
|
2015-08-05 01:49:46 +08:00
|
|
|
flags);
|
2014-01-15 08:23:03 +08:00
|
|
|
} else {
|
|
|
|
arena_mapbits_unallocated_set(chunk, run_ind, size,
|
|
|
|
arena_mapbits_unzeroed_get(chunk, run_ind));
|
|
|
|
arena_mapbits_unallocated_set(chunk, run_ind+run_pages-1, size,
|
|
|
|
arena_mapbits_unzeroed_get(chunk, run_ind+run_pages-1));
|
|
|
|
}
|
|
|
|
|
2015-08-05 01:49:46 +08:00
|
|
|
arena_run_coalesce(arena, chunk, &size, &run_ind, &run_pages,
|
|
|
|
flag_dirty, flag_decommitted);
|
2014-01-15 08:23:03 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/* Insert into runs_avail, now that coalescing is complete. */
|
2012-05-02 15:30:36 +08:00
|
|
|
assert(arena_mapbits_unallocated_size_get(chunk, run_ind) ==
|
|
|
|
arena_mapbits_unallocated_size_get(chunk, run_ind+run_pages-1));
|
|
|
|
assert(arena_mapbits_dirty_get(chunk, run_ind) ==
|
|
|
|
arena_mapbits_dirty_get(chunk, run_ind+run_pages-1));
|
2015-08-05 01:49:46 +08:00
|
|
|
assert(arena_mapbits_decommitted_get(chunk, run_ind) ==
|
|
|
|
arena_mapbits_decommitted_get(chunk, run_ind+run_pages-1));
|
2014-07-22 10:39:20 +08:00
|
|
|
arena_avail_insert(arena, chunk, run_ind, run_pages);
|
2010-04-14 11:53:21 +08:00
|
|
|
|
2014-08-15 05:45:58 +08:00
|
|
|
if (dirty)
|
2015-02-16 10:04:46 +08:00
|
|
|
arena_run_dirty_insert(arena, chunk, run_ind, run_pages);
|
2014-07-19 05:21:17 +08:00
|
|
|
|
2012-05-02 15:30:36 +08:00
|
|
|
/* Deallocate chunk if it is now completely unused. */
|
2014-10-06 08:54:10 +08:00
|
|
|
if (size == arena_maxrun) {
|
2012-05-02 15:30:36 +08:00
|
|
|
assert(run_ind == map_bias);
|
2014-10-06 08:54:10 +08:00
|
|
|
assert(run_pages == (arena_maxrun >> LG_PAGE));
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_chunk_dalloc(tsdn, arena, chunk);
|
2012-05-02 15:30:36 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2010-01-28 10:27:09 +08:00
|
|
|
/*
|
2010-04-14 11:53:21 +08:00
|
|
|
* It is okay to do dirty page processing here even if the chunk was
|
2010-01-28 10:27:09 +08:00
|
|
|
* deallocated above, since in that case it is the spare. Waiting
|
|
|
|
* until after possible chunk deallocation to do dirty processing
|
|
|
|
* allows for an old spare to be fully deallocated, thus decreasing the
|
|
|
|
* chances of spuriously crossing the dirty page purging threshold.
|
|
|
|
*/
|
2010-04-14 11:53:21 +08:00
|
|
|
if (dirty)
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_maybe_purge(tsdn, arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_trim_head(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
arena_run_t *run, size_t oldsize, size_t newsize)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run);
|
|
|
|
size_t pageind = arena_miscelm_to_pageind(miscelm);
|
2012-04-02 22:04:34 +08:00
|
|
|
size_t head_npages = (oldsize - newsize) >> LG_PAGE;
|
2012-05-02 15:30:36 +08:00
|
|
|
size_t flag_dirty = arena_mapbits_dirty_get(chunk, pageind);
|
2015-08-12 03:42:33 +08:00
|
|
|
size_t flag_decommitted = arena_mapbits_decommitted_get(chunk, pageind);
|
|
|
|
size_t flag_unzeroed_mask = (flag_dirty | flag_decommitted) == 0 ?
|
|
|
|
CHUNK_MAP_UNZEROED : 0;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
assert(oldsize > newsize);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the chunk map so that arena_run_dalloc() can treat the
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
* leading run as separately allocated. Set the last element of each
|
|
|
|
* run first, in case of single-page runs.
|
2010-01-17 01:53:50 +08:00
|
|
|
*/
|
2012-05-02 15:30:36 +08:00
|
|
|
assert(arena_mapbits_large_size_get(chunk, pageind) == oldsize);
|
2015-08-12 03:42:33 +08:00
|
|
|
arena_mapbits_large_set(chunk, pageind+head_npages-1, 0, flag_dirty |
|
|
|
|
(flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk,
|
|
|
|
pageind+head_npages-1)));
|
|
|
|
arena_mapbits_large_set(chunk, pageind, oldsize-newsize, flag_dirty |
|
|
|
|
(flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, pageind)));
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_debug) {
|
2012-04-02 22:04:34 +08:00
|
|
|
UNUSED size_t tail_npages = newsize >> LG_PAGE;
|
2012-05-02 15:30:36 +08:00
|
|
|
assert(arena_mapbits_large_size_get(chunk,
|
|
|
|
pageind+head_npages+tail_npages-1) == 0);
|
|
|
|
assert(arena_mapbits_dirty_get(chunk,
|
|
|
|
pageind+head_npages+tail_npages-1) == flag_dirty);
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
}
|
2012-05-11 11:59:39 +08:00
|
|
|
arena_mapbits_large_set(chunk, pageind+head_npages, newsize,
|
2015-08-12 03:42:33 +08:00
|
|
|
flag_dirty | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk,
|
|
|
|
pageind+head_npages)));
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_dalloc(tsdn, arena, run, false, false, (flag_decommitted !=
|
2016-04-14 14:36:15 +08:00
|
|
|
0));
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_trim_tail(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
arena_run_t *run, size_t oldsize, size_t newsize, bool dirty)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run);
|
|
|
|
size_t pageind = arena_miscelm_to_pageind(miscelm);
|
2012-04-02 22:04:34 +08:00
|
|
|
size_t head_npages = newsize >> LG_PAGE;
|
2012-05-02 15:30:36 +08:00
|
|
|
size_t flag_dirty = arena_mapbits_dirty_get(chunk, pageind);
|
2015-08-12 03:42:33 +08:00
|
|
|
size_t flag_decommitted = arena_mapbits_decommitted_get(chunk, pageind);
|
|
|
|
size_t flag_unzeroed_mask = (flag_dirty | flag_decommitted) == 0 ?
|
|
|
|
CHUNK_MAP_UNZEROED : 0;
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_chunk_map_misc_t *tail_miscelm;
|
|
|
|
arena_run_t *tail_run;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
assert(oldsize > newsize);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the chunk map so that arena_run_dalloc() can treat the
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
* trailing run as separately allocated. Set the last element of each
|
|
|
|
* run first, in case of single-page runs.
|
2010-01-17 01:53:50 +08:00
|
|
|
*/
|
2012-05-02 15:30:36 +08:00
|
|
|
assert(arena_mapbits_large_size_get(chunk, pageind) == oldsize);
|
2015-08-12 03:42:33 +08:00
|
|
|
arena_mapbits_large_set(chunk, pageind+head_npages-1, 0, flag_dirty |
|
|
|
|
(flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk,
|
|
|
|
pageind+head_npages-1)));
|
|
|
|
arena_mapbits_large_set(chunk, pageind, newsize, flag_dirty |
|
|
|
|
(flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, pageind)));
|
2012-05-02 15:30:36 +08:00
|
|
|
|
|
|
|
if (config_debug) {
|
|
|
|
UNUSED size_t tail_npages = (oldsize - newsize) >> LG_PAGE;
|
|
|
|
assert(arena_mapbits_large_size_get(chunk,
|
|
|
|
pageind+head_npages+tail_npages-1) == 0);
|
|
|
|
assert(arena_mapbits_dirty_get(chunk,
|
|
|
|
pageind+head_npages+tail_npages-1) == flag_dirty);
|
|
|
|
}
|
|
|
|
arena_mapbits_large_set(chunk, pageind+head_npages, oldsize-newsize,
|
2015-08-12 03:42:33 +08:00
|
|
|
flag_dirty | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk,
|
|
|
|
pageind+head_npages)));
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-03-24 07:04:38 +08:00
|
|
|
tail_miscelm = arena_miscelm_get_mutable(chunk, pageind + head_npages);
|
2014-09-29 16:31:39 +08:00
|
|
|
tail_run = &tail_miscelm->run;
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_dalloc(tsdn, arena, tail_run, dirty, false, (flag_decommitted
|
2016-04-14 14:36:15 +08:00
|
|
|
!= 0));
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2012-02-14 09:36:52 +08:00
|
|
|
static void
|
|
|
|
arena_bin_runs_insert(arena_bin_t *bin, arena_run_t *run)
|
|
|
|
{
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run);
|
2012-02-14 09:36:52 +08:00
|
|
|
|
2016-03-27 08:30:37 +08:00
|
|
|
arena_run_heap_insert(&bin->runs, miscelm);
|
2012-02-14 09:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static arena_run_t *
|
|
|
|
arena_bin_nonfull_run_tryget(arena_bin_t *bin)
|
|
|
|
{
|
2016-03-27 08:30:37 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm;
|
|
|
|
|
|
|
|
miscelm = arena_run_heap_remove_first(&bin->runs);
|
|
|
|
if (miscelm == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (config_stats)
|
|
|
|
bin->stats.reruns++;
|
|
|
|
|
|
|
|
return (&miscelm->run);
|
2012-02-14 09:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static arena_run_t *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_bin_nonfull_run_get(tsdn_t *tsdn, arena_t *arena, arena_bin_t *bin)
|
2012-02-14 09:36:52 +08:00
|
|
|
{
|
|
|
|
arena_run_t *run;
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t binind;
|
2012-02-14 09:36:52 +08:00
|
|
|
arena_bin_info_t *bin_info;
|
|
|
|
|
|
|
|
/* Look for a usable run. */
|
|
|
|
run = arena_bin_nonfull_run_tryget(bin);
|
|
|
|
if (run != NULL)
|
|
|
|
return (run);
|
2010-01-17 01:53:50 +08:00
|
|
|
/* No existing runs have any space available. */
|
|
|
|
|
2011-03-16 04:59:15 +08:00
|
|
|
binind = arena_bin_index(arena, bin);
|
|
|
|
bin_info = &arena_bin_info[binind];
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/* Allocate a new run. */
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &bin->lock);
|
2010-03-16 13:25:23 +08:00
|
|
|
/******************************/
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
|
|
|
run = arena_run_alloc_small(tsdn, arena, bin_info->run_size, binind);
|
2010-03-15 10:43:56 +08:00
|
|
|
if (run != NULL) {
|
|
|
|
/* Initialize run internals. */
|
2014-10-11 14:01:03 +08:00
|
|
|
run->binind = binind;
|
2011-03-16 04:59:15 +08:00
|
|
|
run->nfree = bin_info->nregs;
|
2014-09-29 16:31:39 +08:00
|
|
|
bitmap_init(run->bitmap, &bin_info->bitmap_info);
|
2010-03-16 13:25:23 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2010-03-16 13:25:23 +08:00
|
|
|
/********************************/
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &bin->lock);
|
2010-03-16 13:25:23 +08:00
|
|
|
if (run != NULL) {
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
|
|
|
bin->stats.nruns++;
|
|
|
|
bin->stats.curruns++;
|
|
|
|
}
|
2010-03-15 10:43:56 +08:00
|
|
|
return (run);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-01-15 08:23:03 +08:00
|
|
|
* arena_run_alloc_small() failed, but another thread may have made
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
* sufficient memory available while this one dropped bin->lock above,
|
2010-03-15 10:43:56 +08:00
|
|
|
* so search one more time.
|
|
|
|
*/
|
2012-02-14 09:36:52 +08:00
|
|
|
run = arena_bin_nonfull_run_tryget(bin);
|
|
|
|
if (run != NULL)
|
2010-03-15 10:43:56 +08:00
|
|
|
return (run);
|
|
|
|
|
|
|
|
return (NULL);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2010-03-14 05:41:58 +08:00
|
|
|
/* Re-fill bin->runcur, then call arena_run_reg_alloc(). */
|
2010-01-17 01:53:50 +08:00
|
|
|
static void *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_bin_malloc_hard(tsdn_t *tsdn, arena_t *arena, arena_bin_t *bin)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t binind;
|
2011-03-16 04:59:15 +08:00
|
|
|
arena_bin_info_t *bin_info;
|
2010-03-15 10:43:56 +08:00
|
|
|
arena_run_t *run;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2011-03-16 04:59:15 +08:00
|
|
|
binind = arena_bin_index(arena, bin);
|
|
|
|
bin_info = &arena_bin_info[binind];
|
2010-03-15 10:43:56 +08:00
|
|
|
bin->runcur = NULL;
|
2016-05-11 13:21:10 +08:00
|
|
|
run = arena_bin_nonfull_run_get(tsdn, arena, bin);
|
2010-03-15 10:43:56 +08:00
|
|
|
if (bin->runcur != NULL && bin->runcur->nfree > 0) {
|
|
|
|
/*
|
|
|
|
* Another thread updated runcur while this one ran without the
|
|
|
|
* bin lock in arena_bin_nonfull_run_get().
|
|
|
|
*/
|
2015-09-04 18:15:28 +08:00
|
|
|
void *ret;
|
2010-03-15 10:43:56 +08:00
|
|
|
assert(bin->runcur->nfree > 0);
|
2011-03-16 04:59:15 +08:00
|
|
|
ret = arena_run_reg_alloc(bin->runcur, bin_info);
|
2010-03-15 10:43:56 +08:00
|
|
|
if (run != NULL) {
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
arena_chunk_t *chunk;
|
|
|
|
|
|
|
|
/*
|
2014-01-15 08:23:03 +08:00
|
|
|
* arena_run_alloc_small() may have allocated run, or
|
|
|
|
* it may have pulled run from the bin's run tree.
|
|
|
|
* Therefore it is unsafe to make any assumptions about
|
|
|
|
* how run has previously been used, and
|
|
|
|
* arena_bin_lower_run() must be called, as if a region
|
|
|
|
* were just deallocated from the run.
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
*/
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
|
2016-04-14 14:36:15 +08:00
|
|
|
if (run->nfree == bin_info->nregs) {
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_bin_run(tsdn, arena, chunk, run,
|
2016-04-14 14:36:15 +08:00
|
|
|
bin);
|
|
|
|
} else
|
2010-10-18 11:57:30 +08:00
|
|
|
arena_bin_lower_run(arena, chunk, run, bin);
|
2010-03-15 10:43:56 +08:00
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (run == NULL)
|
2010-01-17 01:53:50 +08:00
|
|
|
return (NULL);
|
2010-03-15 10:43:56 +08:00
|
|
|
|
|
|
|
bin->runcur = run;
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
assert(bin->runcur->nfree > 0);
|
|
|
|
|
2011-03-16 04:59:15 +08:00
|
|
|
return (arena_run_reg_alloc(bin->runcur, bin_info));
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_tcache_fill_small(tsdn_t *tsdn, arena_t *arena, tcache_bin_t *tbin,
|
2016-02-20 12:09:31 +08:00
|
|
|
szind_t binind, uint64_t prof_accumbytes)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
unsigned i, nfill;
|
|
|
|
arena_bin_t *bin;
|
|
|
|
|
|
|
|
assert(tbin->ncached == 0);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
if (config_prof && arena_prof_accum(tsdn, arena, prof_accumbytes))
|
|
|
|
prof_idump(tsdn);
|
2010-03-16 13:25:23 +08:00
|
|
|
bin = &arena->bins[binind];
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &bin->lock);
|
2011-03-21 15:18:17 +08:00
|
|
|
for (i = 0, nfill = (tcache_bin_info[binind].ncached_max >>
|
|
|
|
tbin->lg_fill_div); i < nfill; i++) {
|
2015-09-04 18:15:28 +08:00
|
|
|
arena_run_t *run;
|
|
|
|
void *ptr;
|
2010-01-17 01:53:50 +08:00
|
|
|
if ((run = bin->runcur) != NULL && run->nfree > 0)
|
2011-03-16 04:59:15 +08:00
|
|
|
ptr = arena_run_reg_alloc(run, &arena_bin_info[binind]);
|
2010-01-17 01:53:50 +08:00
|
|
|
else
|
2016-05-11 13:21:10 +08:00
|
|
|
ptr = arena_bin_malloc_hard(tsdn, arena, bin);
|
2014-10-06 04:05:10 +08:00
|
|
|
if (ptr == NULL) {
|
|
|
|
/*
|
|
|
|
* OOM. tbin->avail isn't yet filled down to its first
|
|
|
|
* element, so the successful allocations (if any) must
|
2015-10-28 06:12:10 +08:00
|
|
|
* be moved just before tbin->avail before bailing out.
|
2014-10-06 04:05:10 +08:00
|
|
|
*/
|
|
|
|
if (i > 0) {
|
2015-10-28 06:12:10 +08:00
|
|
|
memmove(tbin->avail - i, tbin->avail - nfill,
|
2014-10-06 04:05:10 +08:00
|
|
|
i * sizeof(void *));
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
break;
|
2014-10-06 04:05:10 +08:00
|
|
|
}
|
2014-12-09 05:12:41 +08:00
|
|
|
if (config_fill && unlikely(opt_junk_alloc)) {
|
2012-04-06 15:35:09 +08:00
|
|
|
arena_alloc_junk_small(ptr, &arena_bin_info[binind],
|
|
|
|
true);
|
|
|
|
}
|
2011-03-19 01:53:15 +08:00
|
|
|
/* Insert such that low regions get used first. */
|
2015-10-28 06:12:10 +08:00
|
|
|
*(tbin->avail - nfill + i) = ptr;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
|
|
|
bin->stats.nmalloc += i;
|
|
|
|
bin->stats.nrequests += tbin->tstats.nrequests;
|
2014-10-13 13:53:59 +08:00
|
|
|
bin->stats.curregs += i;
|
2012-02-11 12:22:09 +08:00
|
|
|
bin->stats.nfills++;
|
|
|
|
tbin->tstats.nrequests = 0;
|
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &bin->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
tbin->ncached = i;
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_decay_tick(tsdn, arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2012-04-06 15:35:09 +08:00
|
|
|
void
|
|
|
|
arena_alloc_junk_small(void *ptr, arena_bin_info_t *bin_info, bool zero)
|
|
|
|
{
|
|
|
|
|
2016-03-28 14:28:39 +08:00
|
|
|
size_t redzone_size = bin_info->redzone_size;
|
|
|
|
|
2012-04-06 15:35:09 +08:00
|
|
|
if (zero) {
|
2016-03-28 14:28:39 +08:00
|
|
|
memset((void *)((uintptr_t)ptr - redzone_size),
|
|
|
|
JEMALLOC_ALLOC_JUNK, redzone_size);
|
|
|
|
memset((void *)((uintptr_t)ptr + bin_info->reg_size),
|
|
|
|
JEMALLOC_ALLOC_JUNK, redzone_size);
|
2012-04-06 15:35:09 +08:00
|
|
|
} else {
|
2016-03-28 14:28:39 +08:00
|
|
|
memset((void *)((uintptr_t)ptr - redzone_size),
|
|
|
|
JEMALLOC_ALLOC_JUNK, bin_info->reg_interval);
|
2012-04-06 15:35:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 07:14:36 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef arena_redzone_corruption
|
2016-04-19 06:11:20 +08:00
|
|
|
#define arena_redzone_corruption JEMALLOC_N(n_arena_redzone_corruption)
|
2013-12-18 07:14:36 +08:00
|
|
|
#endif
|
|
|
|
static void
|
|
|
|
arena_redzone_corruption(void *ptr, size_t usize, bool after,
|
|
|
|
size_t offset, uint8_t byte)
|
|
|
|
{
|
|
|
|
|
2015-07-24 04:56:25 +08:00
|
|
|
malloc_printf("<jemalloc>: Corrupt redzone %zu byte%s %s %p "
|
|
|
|
"(size %zu), byte=%#x\n", offset, (offset == 1) ? "" : "s",
|
2013-12-18 07:14:36 +08:00
|
|
|
after ? "after" : "before", ptr, usize, byte);
|
|
|
|
}
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef arena_redzone_corruption
|
2014-01-08 08:47:56 +08:00
|
|
|
#define arena_redzone_corruption JEMALLOC_N(arena_redzone_corruption)
|
|
|
|
arena_redzone_corruption_t *arena_redzone_corruption =
|
2016-04-19 06:11:20 +08:00
|
|
|
JEMALLOC_N(n_arena_redzone_corruption);
|
2013-12-18 07:14:36 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
arena_redzones_validate(void *ptr, arena_bin_info_t *bin_info, bool reset)
|
2012-04-06 15:35:09 +08:00
|
|
|
{
|
|
|
|
bool error = false;
|
|
|
|
|
2014-12-09 05:12:41 +08:00
|
|
|
if (opt_junk_alloc) {
|
2015-09-04 18:15:28 +08:00
|
|
|
size_t size = bin_info->reg_size;
|
|
|
|
size_t redzone_size = bin_info->redzone_size;
|
|
|
|
size_t i;
|
|
|
|
|
2014-12-09 05:12:41 +08:00
|
|
|
for (i = 1; i <= redzone_size; i++) {
|
|
|
|
uint8_t *byte = (uint8_t *)((uintptr_t)ptr - i);
|
2016-03-28 14:28:39 +08:00
|
|
|
if (*byte != JEMALLOC_ALLOC_JUNK) {
|
2014-12-09 05:12:41 +08:00
|
|
|
error = true;
|
2015-08-05 01:49:46 +08:00
|
|
|
arena_redzone_corruption(ptr, size, false, i,
|
|
|
|
*byte);
|
2014-12-09 05:12:41 +08:00
|
|
|
if (reset)
|
2016-03-28 14:28:39 +08:00
|
|
|
*byte = JEMALLOC_ALLOC_JUNK;
|
2014-12-09 05:12:41 +08:00
|
|
|
}
|
2012-04-06 15:35:09 +08:00
|
|
|
}
|
2014-12-09 05:12:41 +08:00
|
|
|
for (i = 0; i < redzone_size; i++) {
|
|
|
|
uint8_t *byte = (uint8_t *)((uintptr_t)ptr + size + i);
|
2016-03-28 14:28:39 +08:00
|
|
|
if (*byte != JEMALLOC_ALLOC_JUNK) {
|
2014-12-09 05:12:41 +08:00
|
|
|
error = true;
|
2015-08-05 01:49:46 +08:00
|
|
|
arena_redzone_corruption(ptr, size, true, i,
|
|
|
|
*byte);
|
2014-12-09 05:12:41 +08:00
|
|
|
if (reset)
|
2016-03-28 14:28:39 +08:00
|
|
|
*byte = JEMALLOC_ALLOC_JUNK;
|
2014-12-09 05:12:41 +08:00
|
|
|
}
|
2012-04-06 15:35:09 +08:00
|
|
|
}
|
|
|
|
}
|
2014-12-09 05:12:41 +08:00
|
|
|
|
2012-04-06 15:35:09 +08:00
|
|
|
if (opt_abort && error)
|
|
|
|
abort();
|
2013-12-18 07:14:36 +08:00
|
|
|
}
|
|
|
|
|
2014-01-08 08:47:56 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef arena_dalloc_junk_small
|
2016-04-19 06:11:20 +08:00
|
|
|
#define arena_dalloc_junk_small JEMALLOC_N(n_arena_dalloc_junk_small)
|
2014-01-08 08:47:56 +08:00
|
|
|
#endif
|
2013-12-18 07:14:36 +08:00
|
|
|
void
|
|
|
|
arena_dalloc_junk_small(void *ptr, arena_bin_info_t *bin_info)
|
|
|
|
{
|
|
|
|
size_t redzone_size = bin_info->redzone_size;
|
2012-04-06 15:35:09 +08:00
|
|
|
|
2013-12-18 07:14:36 +08:00
|
|
|
arena_redzones_validate(ptr, bin_info, false);
|
2016-03-28 14:28:39 +08:00
|
|
|
memset((void *)((uintptr_t)ptr - redzone_size), JEMALLOC_FREE_JUNK,
|
2012-04-06 15:35:09 +08:00
|
|
|
bin_info->reg_interval);
|
|
|
|
}
|
2014-01-08 08:47:56 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef arena_dalloc_junk_small
|
|
|
|
#define arena_dalloc_junk_small JEMALLOC_N(arena_dalloc_junk_small)
|
|
|
|
arena_dalloc_junk_small_t *arena_dalloc_junk_small =
|
2016-04-19 06:11:20 +08:00
|
|
|
JEMALLOC_N(n_arena_dalloc_junk_small);
|
2014-01-08 08:47:56 +08:00
|
|
|
#endif
|
2012-04-06 15:35:09 +08:00
|
|
|
|
2013-12-18 07:14:36 +08:00
|
|
|
void
|
|
|
|
arena_quarantine_junk_small(void *ptr, size_t usize)
|
|
|
|
{
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t binind;
|
2013-12-18 07:14:36 +08:00
|
|
|
arena_bin_info_t *bin_info;
|
|
|
|
cassert(config_fill);
|
2014-12-09 05:12:41 +08:00
|
|
|
assert(opt_junk_free);
|
2013-12-18 07:14:36 +08:00
|
|
|
assert(opt_quarantine);
|
|
|
|
assert(usize <= SMALL_MAXCLASS);
|
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
binind = size2index(usize);
|
2013-12-18 07:14:36 +08:00
|
|
|
bin_info = &arena_bin_info[binind];
|
|
|
|
arena_redzones_validate(ptr, bin_info, true);
|
|
|
|
}
|
|
|
|
|
2016-02-20 10:40:03 +08:00
|
|
|
static void *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_malloc_small(tsdn_t *tsdn, arena_t *arena, szind_t binind, bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
arena_bin_t *bin;
|
2016-02-26 07:29:49 +08:00
|
|
|
size_t usize;
|
2010-01-17 01:53:50 +08:00
|
|
|
arena_run_t *run;
|
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
assert(binind < NBINS);
|
2010-01-17 01:53:50 +08:00
|
|
|
bin = &arena->bins[binind];
|
2016-02-26 07:29:49 +08:00
|
|
|
usize = index2size(binind);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &bin->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
if ((run = bin->runcur) != NULL && run->nfree > 0)
|
2011-03-16 04:59:15 +08:00
|
|
|
ret = arena_run_reg_alloc(run, &arena_bin_info[binind]);
|
2010-01-17 01:53:50 +08:00
|
|
|
else
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = arena_bin_malloc_hard(tsdn, arena, bin);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
if (ret == NULL) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &bin->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
|
|
|
bin->stats.nmalloc++;
|
|
|
|
bin->stats.nrequests++;
|
2014-10-13 13:53:59 +08:00
|
|
|
bin->stats.curregs++;
|
2012-02-11 12:22:09 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &bin->lock);
|
|
|
|
if (config_prof && !isthreaded && arena_prof_accum(tsdn, arena, usize))
|
|
|
|
prof_idump(tsdn);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!zero) {
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_fill) {
|
2014-12-09 05:12:41 +08:00
|
|
|
if (unlikely(opt_junk_alloc)) {
|
2012-04-06 15:35:09 +08:00
|
|
|
arena_alloc_junk_small(ret,
|
|
|
|
&arena_bin_info[binind], false);
|
2014-09-12 07:20:44 +08:00
|
|
|
} else if (unlikely(opt_zero))
|
2016-02-26 07:29:49 +08:00
|
|
|
memset(ret, 0, usize);
|
2012-02-11 12:22:09 +08:00
|
|
|
}
|
2016-02-26 07:29:49 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, usize);
|
2012-04-06 15:35:09 +08:00
|
|
|
} else {
|
2014-12-09 05:12:41 +08:00
|
|
|
if (config_fill && unlikely(opt_junk_alloc)) {
|
2012-04-06 15:35:09 +08:00
|
|
|
arena_alloc_junk_small(ret, &arena_bin_info[binind],
|
|
|
|
true);
|
|
|
|
}
|
2016-02-26 07:29:49 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, usize);
|
|
|
|
memset(ret, 0, usize);
|
2012-04-06 15:35:09 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_decay_tick(tsdn, arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_malloc_large(tsdn_t *tsdn, arena_t *arena, szind_t binind, bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
2014-10-06 08:54:10 +08:00
|
|
|
size_t usize;
|
2015-05-05 00:58:36 +08:00
|
|
|
uintptr_t random_offset;
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_run_t *run;
|
|
|
|
arena_chunk_map_misc_t *miscelm;
|
2016-03-01 06:30:19 +08:00
|
|
|
UNUSED bool idump JEMALLOC_CC_SILENCE_INIT(false);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/* Large allocation. */
|
2015-10-28 06:12:10 +08:00
|
|
|
usize = index2size(binind);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2015-05-05 00:58:36 +08:00
|
|
|
if (config_cache_oblivious) {
|
2015-07-08 00:32:05 +08:00
|
|
|
uint64_t r;
|
|
|
|
|
2015-05-05 00:58:36 +08:00
|
|
|
/*
|
|
|
|
* Compute a uniformly distributed offset within the first page
|
|
|
|
* that is a multiple of the cacheline size, e.g. [0 .. 63) * 64
|
|
|
|
* for 4 KiB pages and 64-byte cachelines.
|
|
|
|
*/
|
2016-02-10 08:28:40 +08:00
|
|
|
r = prng_lg_range(&arena->offset_state, LG_PAGE - LG_CACHELINE);
|
2015-05-05 00:58:36 +08:00
|
|
|
random_offset = ((uintptr_t)r) << LG_CACHELINE;
|
|
|
|
} else
|
|
|
|
random_offset = 0;
|
2016-05-11 13:21:10 +08:00
|
|
|
run = arena_run_alloc_large(tsdn, arena, usize + large_pad, zero);
|
2014-09-29 16:31:39 +08:00
|
|
|
if (run == NULL) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
2014-09-29 16:31:39 +08:00
|
|
|
miscelm = arena_run_to_miscelm(run);
|
2015-05-05 00:58:36 +08:00
|
|
|
ret = (void *)((uintptr_t)arena_miscelm_to_rpages(miscelm) +
|
|
|
|
random_offset);
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
2015-10-28 06:12:10 +08:00
|
|
|
szind_t index = binind - NBINS;
|
2014-10-06 08:54:10 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
arena->stats.nmalloc_large++;
|
|
|
|
arena->stats.nrequests_large++;
|
2014-10-06 08:54:10 +08:00
|
|
|
arena->stats.allocated_large += usize;
|
|
|
|
arena->stats.lstats[index].nmalloc++;
|
|
|
|
arena->stats.lstats[index].nrequests++;
|
|
|
|
arena->stats.lstats[index].curruns++;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_prof)
|
2014-10-06 08:54:10 +08:00
|
|
|
idump = arena_prof_accum_locked(arena, usize);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2013-02-07 03:59:30 +08:00
|
|
|
if (config_prof && idump)
|
2016-05-11 13:21:10 +08:00
|
|
|
prof_idump(tsdn);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!zero) {
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_fill) {
|
2014-12-09 05:12:41 +08:00
|
|
|
if (unlikely(opt_junk_alloc))
|
2016-03-28 14:28:39 +08:00
|
|
|
memset(ret, JEMALLOC_ALLOC_JUNK, usize);
|
2014-09-12 07:20:44 +08:00
|
|
|
else if (unlikely(opt_zero))
|
2014-10-06 08:54:10 +08:00
|
|
|
memset(ret, 0, usize);
|
2012-02-11 12:22:09 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_decay_tick(tsdn, arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2016-02-20 10:40:03 +08:00
|
|
|
void *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_malloc_hard(tsdn_t *tsdn, arena_t *arena, size_t size, szind_t ind,
|
2016-04-23 05:34:14 +08:00
|
|
|
bool zero)
|
2016-02-20 10:40:03 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
assert(!tsdn_null(tsdn) || arena != NULL);
|
|
|
|
|
|
|
|
if (likely(!tsdn_null(tsdn)))
|
|
|
|
arena = arena_choose(tsdn_tsd(tsdn), arena);
|
2016-02-20 10:40:03 +08:00
|
|
|
if (unlikely(arena == NULL))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (likely(size <= SMALL_MAXCLASS))
|
2016-05-11 13:21:10 +08:00
|
|
|
return (arena_malloc_small(tsdn, arena, ind, zero));
|
2016-02-20 10:40:03 +08:00
|
|
|
if (likely(size <= large_maxclass))
|
2016-05-11 13:21:10 +08:00
|
|
|
return (arena_malloc_large(tsdn, arena, ind, zero));
|
|
|
|
return (huge_malloc(tsdn, arena, index2size(ind), zero));
|
2016-02-20 10:40:03 +08:00
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/* Only handles large allocations that require more than page alignment. */
|
2015-02-13 06:06:37 +08:00
|
|
|
static void *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_palloc_large(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,
|
2015-02-13 06:06:37 +08:00
|
|
|
bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
2012-04-12 09:13:45 +08:00
|
|
|
size_t alloc_size, leadsize, trailsize;
|
|
|
|
arena_run_t *run;
|
2010-01-17 01:53:50 +08:00
|
|
|
arena_chunk_t *chunk;
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm;
|
|
|
|
void *rpages;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
assert(!tsdn_null(tsdn) || arena != NULL);
|
2015-07-24 08:13:18 +08:00
|
|
|
assert(usize == PAGE_CEILING(usize));
|
2010-10-21 08:39:18 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
if (likely(!tsdn_null(tsdn)))
|
|
|
|
arena = arena_choose(tsdn_tsd(tsdn), arena);
|
2015-02-13 06:06:37 +08:00
|
|
|
if (unlikely(arena == NULL))
|
|
|
|
return (NULL);
|
|
|
|
|
2010-10-21 08:39:18 +08:00
|
|
|
alignment = PAGE_CEILING(alignment);
|
2016-06-08 05:19:50 +08:00
|
|
|
alloc_size = usize + large_pad + alignment - PAGE;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
|
|
|
run = arena_run_alloc_large(tsdn, arena, alloc_size, false);
|
2012-04-12 09:13:45 +08:00
|
|
|
if (run == NULL) {
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
2012-04-12 09:13:45 +08:00
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run);
|
2014-09-29 16:31:39 +08:00
|
|
|
miscelm = arena_run_to_miscelm(run);
|
|
|
|
rpages = arena_miscelm_to_rpages(miscelm);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-09-29 16:31:39 +08:00
|
|
|
leadsize = ALIGNMENT_CEILING((uintptr_t)rpages, alignment) -
|
|
|
|
(uintptr_t)rpages;
|
2015-07-24 08:13:18 +08:00
|
|
|
assert(alloc_size >= leadsize + usize);
|
|
|
|
trailsize = alloc_size - leadsize - usize - large_pad;
|
2012-04-12 09:13:45 +08:00
|
|
|
if (leadsize != 0) {
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_chunk_map_misc_t *head_miscelm = miscelm;
|
|
|
|
arena_run_t *head_run = run;
|
|
|
|
|
2016-03-24 07:04:38 +08:00
|
|
|
miscelm = arena_miscelm_get_mutable(chunk,
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_miscelm_to_pageind(head_miscelm) + (leadsize >>
|
|
|
|
LG_PAGE));
|
|
|
|
run = &miscelm->run;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_trim_head(tsdn, arena, chunk, head_run, alloc_size,
|
2014-09-29 16:31:39 +08:00
|
|
|
alloc_size - leadsize);
|
2012-04-12 09:13:45 +08:00
|
|
|
}
|
|
|
|
if (trailsize != 0) {
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_trim_tail(tsdn, arena, chunk, run, usize + large_pad +
|
2015-07-24 08:13:18 +08:00
|
|
|
trailsize, usize + large_pad, false);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2015-08-05 01:49:46 +08:00
|
|
|
if (arena_run_init_large(arena, run, usize + large_pad, zero)) {
|
|
|
|
size_t run_ind =
|
|
|
|
arena_miscelm_to_pageind(arena_run_to_miscelm(run));
|
2015-08-10 07:47:27 +08:00
|
|
|
bool dirty = (arena_mapbits_dirty_get(chunk, run_ind) != 0);
|
|
|
|
bool decommitted = (arena_mapbits_decommitted_get(chunk,
|
|
|
|
run_ind) != 0);
|
2015-08-05 01:49:46 +08:00
|
|
|
|
2015-08-10 07:47:27 +08:00
|
|
|
assert(decommitted); /* Cause of OOM. */
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_dalloc(tsdn, arena, run, dirty, false, decommitted);
|
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2015-08-05 01:49:46 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
2014-09-29 16:31:39 +08:00
|
|
|
ret = arena_miscelm_to_rpages(miscelm);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t index = size2index(usize) - NBINS;
|
2014-10-06 08:54:10 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
arena->stats.nmalloc_large++;
|
|
|
|
arena->stats.nrequests_large++;
|
2015-07-24 08:13:18 +08:00
|
|
|
arena->stats.allocated_large += usize;
|
2014-10-06 08:54:10 +08:00
|
|
|
arena->stats.lstats[index].nmalloc++;
|
|
|
|
arena->stats.lstats[index].nrequests++;
|
|
|
|
arena->stats.lstats[index].curruns++;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (config_fill && !zero) {
|
2014-12-09 05:12:41 +08:00
|
|
|
if (unlikely(opt_junk_alloc))
|
2016-03-28 14:28:39 +08:00
|
|
|
memset(ret, JEMALLOC_ALLOC_JUNK, usize);
|
2014-09-12 07:20:44 +08:00
|
|
|
else if (unlikely(opt_zero))
|
2015-07-24 08:13:18 +08:00
|
|
|
memset(ret, 0, usize);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_decay_tick(tsdn, arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2015-02-13 06:06:37 +08:00
|
|
|
void *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,
|
2015-02-13 06:06:37 +08:00
|
|
|
bool zero, tcache_t *tcache)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
2015-05-05 00:58:36 +08:00
|
|
|
if (usize <= SMALL_MAXCLASS && (alignment < PAGE || (alignment == PAGE
|
2015-05-20 08:42:31 +08:00
|
|
|
&& (usize & PAGE_MASK) == 0))) {
|
|
|
|
/* Small; alignment doesn't require special run placement. */
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = arena_malloc(tsdn, arena, usize, size2index(usize), zero,
|
2015-10-28 06:12:10 +08:00
|
|
|
tcache, true);
|
2015-09-12 11:50:20 +08:00
|
|
|
} else if (usize <= large_maxclass && alignment <= PAGE) {
|
2015-05-20 08:42:31 +08:00
|
|
|
/*
|
|
|
|
* Large; alignment doesn't require special run placement.
|
|
|
|
* However, the cached pointer may be at a random offset from
|
|
|
|
* the base of the run, so do some bit manipulation to retrieve
|
|
|
|
* the base.
|
|
|
|
*/
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = arena_malloc(tsdn, arena, usize, size2index(usize), zero,
|
2015-10-28 06:12:10 +08:00
|
|
|
tcache, true);
|
2015-05-20 08:42:31 +08:00
|
|
|
if (config_cache_oblivious)
|
|
|
|
ret = (void *)((uintptr_t)ret & ~PAGE_MASK);
|
|
|
|
} else {
|
2015-09-12 11:50:20 +08:00
|
|
|
if (likely(usize <= large_maxclass)) {
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = arena_palloc_large(tsdn, arena, usize, alignment,
|
2015-02-13 06:06:37 +08:00
|
|
|
zero);
|
|
|
|
} else if (likely(alignment <= chunksize))
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = huge_malloc(tsdn, arena, usize, zero);
|
2015-02-13 06:06:37 +08:00
|
|
|
else {
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = huge_palloc(tsdn, arena, usize, alignment, zero);
|
2015-02-13 06:06:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2010-04-01 07:45:04 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_prof_promoted(tsdn_t *tsdn, const void *ptr, size_t size)
|
2010-04-01 07:45:04 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
2014-10-06 08:54:10 +08:00
|
|
|
size_t pageind;
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t binind;
|
2010-04-01 07:45:04 +08:00
|
|
|
|
2012-04-19 04:38:40 +08:00
|
|
|
cassert(config_prof);
|
2010-04-01 07:45:04 +08:00
|
|
|
assert(ptr != NULL);
|
|
|
|
assert(CHUNK_ADDR2BASE(ptr) != ptr);
|
2016-05-11 13:21:10 +08:00
|
|
|
assert(isalloc(tsdn, ptr, false) == LARGE_MINCLASS);
|
|
|
|
assert(isalloc(tsdn, ptr, true) == LARGE_MINCLASS);
|
2012-02-29 08:50:47 +08:00
|
|
|
assert(size <= SMALL_MAXCLASS);
|
2010-04-01 07:45:04 +08:00
|
|
|
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
2012-04-02 22:04:34 +08:00
|
|
|
pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
|
2014-10-06 08:54:10 +08:00
|
|
|
binind = size2index(size);
|
2012-02-29 08:50:47 +08:00
|
|
|
assert(binind < NBINS);
|
2012-05-02 15:30:36 +08:00
|
|
|
arena_mapbits_large_binind_set(chunk, pageind, binind);
|
2010-04-01 07:45:04 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
assert(isalloc(tsdn, ptr, false) == LARGE_MINCLASS);
|
|
|
|
assert(isalloc(tsdn, ptr, true) == size);
|
2010-04-01 07:45:04 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
static void
|
2010-10-18 15:04:44 +08:00
|
|
|
arena_dissociate_bin_run(arena_chunk_t *chunk, arena_run_t *run,
|
2010-01-17 01:53:50 +08:00
|
|
|
arena_bin_t *bin)
|
|
|
|
{
|
|
|
|
|
2010-03-19 11:36:40 +08:00
|
|
|
/* Dissociate run from bin. */
|
2010-01-17 01:53:50 +08:00
|
|
|
if (run == bin->runcur)
|
|
|
|
bin->runcur = NULL;
|
2011-03-16 04:59:15 +08:00
|
|
|
else {
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t binind = arena_bin_index(extent_node_arena_get(
|
2015-02-16 10:04:46 +08:00
|
|
|
&chunk->node), bin);
|
2011-03-16 04:59:15 +08:00
|
|
|
arena_bin_info_t *bin_info = &arena_bin_info[binind];
|
|
|
|
|
2016-03-27 08:30:37 +08:00
|
|
|
/*
|
|
|
|
* The following block's conditional is necessary because if the
|
|
|
|
* run only contains one region, then it never gets inserted
|
|
|
|
* into the non-full runs tree.
|
|
|
|
*/
|
2011-03-16 04:59:15 +08:00
|
|
|
if (bin_info->nregs != 1) {
|
2016-03-27 08:30:37 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm =
|
|
|
|
arena_run_to_miscelm(run);
|
|
|
|
|
|
|
|
arena_run_heap_remove(&bin->runs, miscelm);
|
2011-03-16 04:59:15 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2010-10-18 15:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_bin_run(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
arena_run_t *run, arena_bin_t *bin)
|
2010-10-18 15:04:44 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
assert(run != bin->runcur);
|
2010-03-14 12:32:56 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &bin->lock);
|
2010-03-15 10:43:56 +08:00
|
|
|
/******************************/
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
|
|
|
arena_run_dalloc(tsdn, arena, run, true, false, false);
|
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2010-03-15 10:43:56 +08:00
|
|
|
/****************************/
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &bin->lock);
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats)
|
|
|
|
bin->stats.curruns--;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
static void
|
|
|
|
arena_bin_lower_run(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run,
|
|
|
|
arena_bin_t *bin)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
|
2010-10-18 11:57:30 +08:00
|
|
|
/*
|
2012-02-14 09:36:52 +08:00
|
|
|
* Make sure that if bin->runcur is non-NULL, it refers to the lowest
|
|
|
|
* non-full run. It is okay to NULL runcur out rather than proactively
|
|
|
|
* keeping it pointing at the lowest non-full run.
|
2010-10-18 11:57:30 +08:00
|
|
|
*/
|
2012-02-14 09:36:52 +08:00
|
|
|
if ((uintptr_t)run < (uintptr_t)bin->runcur) {
|
2010-10-18 11:57:30 +08:00
|
|
|
/* Switch runcur. */
|
2012-02-14 09:36:52 +08:00
|
|
|
if (bin->runcur->nfree > 0)
|
|
|
|
arena_bin_runs_insert(bin, bin->runcur);
|
2010-10-18 11:57:30 +08:00
|
|
|
bin->runcur = run;
|
2012-02-14 09:36:52 +08:00
|
|
|
if (config_stats)
|
|
|
|
bin->stats.reruns++;
|
|
|
|
} else
|
|
|
|
arena_bin_runs_insert(bin, run);
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:54:06 +08:00
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_bin_locked_impl(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
void *ptr, arena_chunk_map_bits_t *bitselm, bool junked)
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
{
|
2014-09-29 16:31:39 +08:00
|
|
|
size_t pageind, rpages_ind;
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
arena_run_t *run;
|
|
|
|
arena_bin_t *bin;
|
2012-04-25 05:22:02 +08:00
|
|
|
arena_bin_info_t *bin_info;
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t binind;
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
|
2012-04-02 22:04:34 +08:00
|
|
|
pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
|
2014-09-29 16:31:39 +08:00
|
|
|
rpages_ind = pageind - arena_mapbits_small_runind_get(chunk, pageind);
|
2016-03-24 07:04:38 +08:00
|
|
|
run = &arena_miscelm_get_mutable(chunk, rpages_ind)->run;
|
2014-10-11 14:01:03 +08:00
|
|
|
binind = run->binind;
|
|
|
|
bin = &arena->bins[binind];
|
2012-04-25 05:22:02 +08:00
|
|
|
bin_info = &arena_bin_info[binind];
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
|
2014-12-09 05:12:41 +08:00
|
|
|
if (!junked && config_fill && unlikely(opt_junk_free))
|
2012-04-06 15:35:09 +08:00
|
|
|
arena_dalloc_junk_small(ptr, bin_info);
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
|
|
|
|
arena_run_reg_dalloc(run, ptr);
|
2011-03-16 04:59:15 +08:00
|
|
|
if (run->nfree == bin_info->nregs) {
|
2010-10-18 15:04:44 +08:00
|
|
|
arena_dissociate_bin_run(chunk, run, bin);
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_bin_run(tsdn, arena, chunk, run, bin);
|
2010-10-18 15:04:44 +08:00
|
|
|
} else if (run->nfree == 1 && run != bin->runcur)
|
2010-10-18 11:57:30 +08:00
|
|
|
arena_bin_lower_run(arena, chunk, run, bin);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
|
|
|
bin->stats.ndalloc++;
|
2014-10-13 13:53:59 +08:00
|
|
|
bin->stats.curregs--;
|
2012-02-11 12:22:09 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:54:06 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_bin_junked_locked(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
arena_chunk_t *chunk, void *ptr, arena_chunk_map_bits_t *bitselm)
|
2014-10-10 08:54:06 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_bin_locked_impl(tsdn, arena, chunk, ptr, bitselm, true);
|
2014-10-10 08:54:06 +08:00
|
|
|
}
|
|
|
|
|
2012-05-02 15:30:36 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_bin(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk, void *ptr,
|
2014-08-30 04:34:40 +08:00
|
|
|
size_t pageind, arena_chunk_map_bits_t *bitselm)
|
2012-05-02 15:30:36 +08:00
|
|
|
{
|
|
|
|
arena_run_t *run;
|
|
|
|
arena_bin_t *bin;
|
2014-09-29 16:31:39 +08:00
|
|
|
size_t rpages_ind;
|
2012-05-02 15:30:36 +08:00
|
|
|
|
2014-09-29 16:31:39 +08:00
|
|
|
rpages_ind = pageind - arena_mapbits_small_runind_get(chunk, pageind);
|
2016-03-24 07:04:38 +08:00
|
|
|
run = &arena_miscelm_get_mutable(chunk, rpages_ind)->run;
|
2014-10-11 14:01:03 +08:00
|
|
|
bin = &arena->bins[run->binind];
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &bin->lock);
|
|
|
|
arena_dalloc_bin_locked_impl(tsdn, arena, chunk, ptr, bitselm, false);
|
|
|
|
malloc_mutex_unlock(tsdn, &bin->lock);
|
2012-05-02 15:30:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_small(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk,
|
|
|
|
void *ptr, size_t pageind)
|
2012-05-02 15:30:36 +08:00
|
|
|
{
|
2014-08-30 04:34:40 +08:00
|
|
|
arena_chunk_map_bits_t *bitselm;
|
2012-05-02 15:30:36 +08:00
|
|
|
|
|
|
|
if (config_debug) {
|
2012-05-03 07:11:03 +08:00
|
|
|
/* arena_ptr_small_binind_get() does extra sanity checking. */
|
|
|
|
assert(arena_ptr_small_binind_get(ptr, arena_mapbits_get(chunk,
|
|
|
|
pageind)) != BININD_INVALID);
|
2012-05-02 15:30:36 +08:00
|
|
|
}
|
2016-03-24 07:04:38 +08:00
|
|
|
bitselm = arena_bitselm_get_mutable(chunk, pageind);
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_bin(tsdn, arena, chunk, ptr, pageind, bitselm);
|
|
|
|
arena_decay_tick(tsdn, arena);
|
2012-05-02 15:30:36 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-01-08 08:47:56 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef arena_dalloc_junk_large
|
2016-04-19 06:11:20 +08:00
|
|
|
#define arena_dalloc_junk_large JEMALLOC_N(n_arena_dalloc_junk_large)
|
2014-01-08 08:47:56 +08:00
|
|
|
#endif
|
2014-10-10 08:54:06 +08:00
|
|
|
void
|
2014-01-08 08:47:56 +08:00
|
|
|
arena_dalloc_junk_large(void *ptr, size_t usize)
|
|
|
|
{
|
|
|
|
|
2014-12-09 05:12:41 +08:00
|
|
|
if (config_fill && unlikely(opt_junk_free))
|
2016-03-28 14:28:39 +08:00
|
|
|
memset(ptr, JEMALLOC_FREE_JUNK, usize);
|
2014-01-08 08:47:56 +08:00
|
|
|
}
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef arena_dalloc_junk_large
|
|
|
|
#define arena_dalloc_junk_large JEMALLOC_N(arena_dalloc_junk_large)
|
|
|
|
arena_dalloc_junk_large_t *arena_dalloc_junk_large =
|
2016-04-19 06:11:20 +08:00
|
|
|
JEMALLOC_N(n_arena_dalloc_junk_large);
|
2014-01-08 08:47:56 +08:00
|
|
|
#endif
|
|
|
|
|
2015-09-21 00:58:10 +08:00
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_large_locked_impl(tsdn_t *tsdn, arena_t *arena,
|
|
|
|
arena_chunk_t *chunk, void *ptr, bool junked)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2014-09-29 16:31:39 +08:00
|
|
|
size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
|
2016-03-24 07:04:38 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm = arena_miscelm_get_mutable(chunk,
|
|
|
|
pageind);
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_run_t *run = &miscelm->run;
|
2010-01-31 19:57:29 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_fill || config_stats) {
|
2015-05-05 00:58:36 +08:00
|
|
|
size_t usize = arena_mapbits_large_size_get(chunk, pageind) -
|
|
|
|
large_pad;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-10-10 08:54:06 +08:00
|
|
|
if (!junked)
|
|
|
|
arena_dalloc_junk_large(ptr, usize);
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t index = size2index(usize) - NBINS;
|
2014-10-06 08:54:10 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
arena->stats.ndalloc_large++;
|
2014-01-08 08:47:56 +08:00
|
|
|
arena->stats.allocated_large -= usize;
|
2014-10-06 08:54:10 +08:00
|
|
|
arena->stats.lstats[index].ndalloc++;
|
|
|
|
arena->stats.lstats[index].curruns--;
|
2012-02-11 12:22:09 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_run_dalloc(tsdn, arena, run, true, false, false);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:54:06 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_large_junked_locked(tsdn_t *tsdn, arena_t *arena,
|
2016-04-14 14:36:15 +08:00
|
|
|
arena_chunk_t *chunk, void *ptr)
|
2014-10-10 08:54:06 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_large_locked_impl(tsdn, arena, chunk, ptr, true);
|
2014-10-10 08:54:06 +08:00
|
|
|
}
|
|
|
|
|
2012-05-02 15:30:36 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dalloc_large(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk,
|
|
|
|
void *ptr)
|
2012-05-02 15:30:36 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
|
|
|
arena_dalloc_large_locked_impl(tsdn, arena, chunk, ptr, false);
|
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
|
|
|
arena_decay_tick(tsdn, arena);
|
2012-05-02 15:30:36 +08:00
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
static void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_ralloc_large_shrink(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
void *ptr, size_t oldsize, size_t size)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2014-09-29 16:31:39 +08:00
|
|
|
size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
|
2016-03-24 07:04:38 +08:00
|
|
|
arena_chunk_map_misc_t *miscelm = arena_miscelm_get_mutable(chunk,
|
|
|
|
pageind);
|
2014-09-29 16:31:39 +08:00
|
|
|
arena_run_t *run = &miscelm->run;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
assert(size < oldsize);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shrink the run, and make trailing pages available for other
|
|
|
|
* allocations.
|
|
|
|
*/
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
|
|
|
arena_run_trim_tail(tsdn, arena, chunk, run, oldsize + large_pad, size +
|
2015-05-05 00:58:36 +08:00
|
|
|
large_pad, true);
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t oldindex = size2index(oldsize) - NBINS;
|
|
|
|
szind_t index = size2index(size) - NBINS;
|
2014-10-06 08:54:10 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
arena->stats.ndalloc_large++;
|
|
|
|
arena->stats.allocated_large -= oldsize;
|
2014-10-06 08:54:10 +08:00
|
|
|
arena->stats.lstats[oldindex].ndalloc++;
|
|
|
|
arena->stats.lstats[oldindex].curruns--;
|
2012-02-11 12:22:09 +08:00
|
|
|
|
|
|
|
arena->stats.nmalloc_large++;
|
|
|
|
arena->stats.nrequests_large++;
|
|
|
|
arena->stats.allocated_large += size;
|
2014-10-06 08:54:10 +08:00
|
|
|
arena->stats.lstats[index].nmalloc++;
|
|
|
|
arena->stats.lstats[index].nrequests++;
|
|
|
|
arena->stats.lstats[index].curruns++;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_ralloc_large_grow(tsdn_t *tsdn, arena_t *arena, arena_chunk_t *chunk,
|
2016-04-14 14:36:15 +08:00
|
|
|
void *ptr, size_t oldsize, size_t usize_min, size_t usize_max, bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2012-04-02 22:04:34 +08:00
|
|
|
size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
|
2015-08-07 14:34:12 +08:00
|
|
|
size_t npages = (oldsize + large_pad) >> LG_PAGE;
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
size_t followsize;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-05-05 00:58:36 +08:00
|
|
|
assert(oldsize == arena_mapbits_large_size_get(chunk, pageind) -
|
|
|
|
large_pad);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/* Try to extend the run. */
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2015-09-12 07:18:53 +08:00
|
|
|
if (pageind+npages >= chunk_npages || arena_mapbits_allocated_get(chunk,
|
|
|
|
pageind+npages) != 0)
|
|
|
|
goto label_fail;
|
|
|
|
followsize = arena_mapbits_unallocated_size_get(chunk, pageind+npages);
|
|
|
|
if (oldsize + followsize >= usize_min) {
|
2010-01-17 01:53:50 +08:00
|
|
|
/*
|
|
|
|
* The next run is available and sufficiently large. Split the
|
|
|
|
* following run, then merge the first part with the existing
|
|
|
|
* allocation.
|
|
|
|
*/
|
2014-12-18 00:46:35 +08:00
|
|
|
arena_run_t *run;
|
2015-09-12 07:18:53 +08:00
|
|
|
size_t usize, splitsize, size, flag_dirty, flag_unzeroed_mask;
|
2014-10-06 08:54:10 +08:00
|
|
|
|
2015-09-12 07:18:53 +08:00
|
|
|
usize = usize_max;
|
2014-10-06 08:54:10 +08:00
|
|
|
while (oldsize + followsize < usize)
|
|
|
|
usize = index2size(size2index(usize)-1);
|
|
|
|
assert(usize >= usize_min);
|
2015-09-12 07:18:53 +08:00
|
|
|
assert(usize >= oldsize);
|
2015-08-07 14:34:12 +08:00
|
|
|
splitsize = usize - oldsize;
|
2015-09-12 07:18:53 +08:00
|
|
|
if (splitsize == 0)
|
|
|
|
goto label_fail;
|
2014-10-06 08:54:10 +08:00
|
|
|
|
2016-03-24 07:04:38 +08:00
|
|
|
run = &arena_miscelm_get_mutable(chunk, pageind+npages)->run;
|
2015-09-12 07:18:53 +08:00
|
|
|
if (arena_run_split_large(arena, run, splitsize, zero))
|
|
|
|
goto label_fail;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-09-25 07:38:45 +08:00
|
|
|
if (config_cache_oblivious && zero) {
|
|
|
|
/*
|
|
|
|
* Zero the trailing bytes of the original allocation's
|
|
|
|
* last page, since they are in an indeterminate state.
|
2015-09-25 13:21:55 +08:00
|
|
|
* There will always be trailing bytes, because ptr's
|
|
|
|
* offset from the beginning of the run is a multiple of
|
|
|
|
* CACHELINE in [0 .. PAGE).
|
2015-09-25 07:38:45 +08:00
|
|
|
*/
|
2015-09-25 13:21:55 +08:00
|
|
|
void *zbase = (void *)((uintptr_t)ptr + oldsize);
|
|
|
|
void *zpast = PAGE_ADDR2BASE((void *)((uintptr_t)zbase +
|
|
|
|
PAGE));
|
|
|
|
size_t nzero = (uintptr_t)zpast - (uintptr_t)zbase;
|
|
|
|
assert(nzero > 0);
|
|
|
|
memset(zbase, 0, nzero);
|
2015-09-25 07:38:45 +08:00
|
|
|
}
|
|
|
|
|
2010-10-18 15:04:44 +08:00
|
|
|
size = oldsize + splitsize;
|
2015-08-07 14:34:12 +08:00
|
|
|
npages = (size + large_pad) >> LG_PAGE;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
/*
|
|
|
|
* Mark the extended run as dirty if either portion of the run
|
|
|
|
* was dirty before allocation. This is rather pedantic,
|
|
|
|
* because there's not actually any sequence of events that
|
|
|
|
* could cause the resulting run to be passed to
|
|
|
|
* arena_run_dalloc() with the dirty argument set to false
|
|
|
|
* (which is when dirty flag consistency would really matter).
|
|
|
|
*/
|
2012-05-02 15:30:36 +08:00
|
|
|
flag_dirty = arena_mapbits_dirty_get(chunk, pageind) |
|
|
|
|
arena_mapbits_dirty_get(chunk, pageind+npages-1);
|
2015-08-12 03:42:33 +08:00
|
|
|
flag_unzeroed_mask = flag_dirty == 0 ? CHUNK_MAP_UNZEROED : 0;
|
2015-08-07 14:34:12 +08:00
|
|
|
arena_mapbits_large_set(chunk, pageind, size + large_pad,
|
2015-08-12 03:42:33 +08:00
|
|
|
flag_dirty | (flag_unzeroed_mask &
|
|
|
|
arena_mapbits_unzeroed_get(chunk, pageind)));
|
|
|
|
arena_mapbits_large_set(chunk, pageind+npages-1, 0, flag_dirty |
|
|
|
|
(flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk,
|
|
|
|
pageind+npages-1)));
|
2010-01-31 19:49:35 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t oldindex = size2index(oldsize) - NBINS;
|
|
|
|
szind_t index = size2index(size) - NBINS;
|
2014-10-06 08:54:10 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
arena->stats.ndalloc_large++;
|
|
|
|
arena->stats.allocated_large -= oldsize;
|
2014-10-06 08:54:10 +08:00
|
|
|
arena->stats.lstats[oldindex].ndalloc++;
|
|
|
|
arena->stats.lstats[oldindex].curruns--;
|
2012-02-11 12:22:09 +08:00
|
|
|
|
|
|
|
arena->stats.nmalloc_large++;
|
|
|
|
arena->stats.nrequests_large++;
|
|
|
|
arena->stats.allocated_large += size;
|
2014-10-06 08:54:10 +08:00
|
|
|
arena->stats.lstats[index].nmalloc++;
|
|
|
|
arena->stats.lstats[index].nrequests++;
|
|
|
|
arena->stats.lstats[index].curruns++;
|
Fix numerous arena bugs.
In arena_ralloc_large_grow(), update the map element for the end of the
newly grown run, rather than the interior map element that was the
beginning of the appended run. This is a long-standing bug, and it had
the potential to cause massive corruption, but triggering it required
roughly the following sequence of events:
1) Large in-place growing realloc(), with left-over space in the run
that followed the large object.
2) Allocation of the remainder run left over from (1).
3) Deallocation of the remainder run *before* deallocation of the
large run, with unfortunate interior map state left over from
previous run allocation/deallocation activity, such that one or
more pages of allocated memory would be treated as part of the
remainder run during run coalescing.
In summary, this was a bad bug, but it was difficult to trigger.
In arena_bin_malloc_hard(), if another thread wins the race to allocate
a bin run, dispose of the spare run via arena_bin_lower_run() rather
than arena_run_dalloc(), since the run has already been prepared for use
as a bin run. This bug has existed since March 14, 2010:
e00572b384c81bd2aba57fac32f7077a34388915
mmap()/munmap() without arena->lock or bin->lock.
Fix bugs in arena_dalloc_bin_run(), arena_trim_head(),
arena_trim_tail(), and arena_ralloc_large_grow() that could cause the
CHUNK_MAP_UNZEROED map bit to become corrupted. These are all
long-standing bugs, but the chances of them actually causing problems
was much lower before the CHUNK_MAP_ZEROED --> CHUNK_MAP_UNZEROED
conversion.
Fix a large run statistics regression in arena_ralloc_large_grow() that
was introduced on September 17, 2010:
8e3c3c61b5bb676a705450708e7e79698cdc9e0c
Add {,r,s,d}allocm().
Add debug code to validate that supposedly pre-zeroed memory really is.
2010-10-18 08:51:37 +08:00
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (false);
|
|
|
|
}
|
2015-09-12 07:18:53 +08:00
|
|
|
label_fail:
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2014-01-08 08:47:56 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef arena_ralloc_junk_large
|
2016-04-19 06:11:20 +08:00
|
|
|
#define arena_ralloc_junk_large JEMALLOC_N(n_arena_ralloc_junk_large)
|
2014-01-08 08:47:56 +08:00
|
|
|
#endif
|
|
|
|
static void
|
|
|
|
arena_ralloc_junk_large(void *ptr, size_t old_usize, size_t usize)
|
|
|
|
{
|
|
|
|
|
2014-12-09 05:12:41 +08:00
|
|
|
if (config_fill && unlikely(opt_junk_free)) {
|
2016-03-28 14:28:39 +08:00
|
|
|
memset((void *)((uintptr_t)ptr + usize), JEMALLOC_FREE_JUNK,
|
2014-01-08 08:47:56 +08:00
|
|
|
old_usize - usize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
#undef arena_ralloc_junk_large
|
|
|
|
#define arena_ralloc_junk_large JEMALLOC_N(arena_ralloc_junk_large)
|
|
|
|
arena_ralloc_junk_large_t *arena_ralloc_junk_large =
|
2016-04-19 06:11:20 +08:00
|
|
|
JEMALLOC_N(n_arena_ralloc_junk_large);
|
2014-01-08 08:47:56 +08:00
|
|
|
#endif
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/*
|
|
|
|
* Try to resize a large allocation, in order to avoid copying. This will
|
|
|
|
* always fail if growing an object, and the following run is already in use.
|
|
|
|
*/
|
|
|
|
static bool
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_ralloc_large(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t usize_min,
|
2015-09-12 07:18:53 +08:00
|
|
|
size_t usize_max, bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2015-09-12 07:18:53 +08:00
|
|
|
arena_chunk_t *chunk;
|
|
|
|
arena_t *arena;
|
2014-10-06 08:54:10 +08:00
|
|
|
|
2015-09-12 07:18:53 +08:00
|
|
|
if (oldsize == usize_max) {
|
|
|
|
/* Current size class is compatible and maximal. */
|
2010-01-17 01:53:50 +08:00
|
|
|
return (false);
|
2015-09-12 07:18:53 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-09-12 07:18:53 +08:00
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
|
|
|
arena = extent_node_arena_get(&chunk->node);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-09-12 07:18:53 +08:00
|
|
|
if (oldsize < usize_max) {
|
2016-05-11 13:21:10 +08:00
|
|
|
bool ret = arena_ralloc_large_grow(tsdn, arena, chunk, ptr,
|
2016-04-14 14:36:15 +08:00
|
|
|
oldsize, usize_min, usize_max, zero);
|
2015-09-12 07:18:53 +08:00
|
|
|
if (config_fill && !ret && !zero) {
|
|
|
|
if (unlikely(opt_junk_alloc)) {
|
2016-03-28 14:28:39 +08:00
|
|
|
memset((void *)((uintptr_t)ptr + oldsize),
|
|
|
|
JEMALLOC_ALLOC_JUNK,
|
2016-05-11 13:21:10 +08:00
|
|
|
isalloc(tsdn, ptr, config_prof) - oldsize);
|
2015-09-12 07:18:53 +08:00
|
|
|
} else if (unlikely(opt_zero)) {
|
|
|
|
memset((void *)((uintptr_t)ptr + oldsize), 0,
|
2016-05-11 13:21:10 +08:00
|
|
|
isalloc(tsdn, ptr, config_prof) - oldsize);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-12 07:18:53 +08:00
|
|
|
return (ret);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2015-09-12 07:18:53 +08:00
|
|
|
|
|
|
|
assert(oldsize > usize_max);
|
|
|
|
/* Fill before shrinking in order avoid a race. */
|
|
|
|
arena_ralloc_junk_large(ptr, oldsize, usize_max);
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_ralloc_large_shrink(tsdn, arena, chunk, ptr, oldsize, usize_max);
|
2015-09-12 07:18:53 +08:00
|
|
|
return (false);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2014-01-13 07:05:44 +08:00
|
|
|
bool
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_ralloc_no_move(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,
|
2016-02-20 12:09:31 +08:00
|
|
|
size_t extra, bool zero)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2015-09-12 07:18:53 +08:00
|
|
|
size_t usize_min, usize_max;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-02-26 07:29:49 +08:00
|
|
|
/* Calls with non-zero extra had to clamp extra. */
|
|
|
|
assert(extra == 0 || size + extra <= HUGE_MAXCLASS);
|
|
|
|
|
|
|
|
if (unlikely(size > HUGE_MAXCLASS))
|
|
|
|
return (true);
|
|
|
|
|
2015-09-12 07:18:53 +08:00
|
|
|
usize_min = s2u(size);
|
|
|
|
usize_max = s2u(size + extra);
|
2015-09-12 11:50:20 +08:00
|
|
|
if (likely(oldsize <= large_maxclass && usize_min <= large_maxclass)) {
|
2016-02-20 12:09:31 +08:00
|
|
|
arena_chunk_t *chunk;
|
|
|
|
|
2015-02-13 06:06:37 +08:00
|
|
|
/*
|
|
|
|
* Avoid moving the allocation if the size class can be left the
|
|
|
|
* same.
|
|
|
|
*/
|
2015-09-12 07:18:53 +08:00
|
|
|
if (oldsize <= SMALL_MAXCLASS) {
|
|
|
|
assert(arena_bin_info[size2index(oldsize)].reg_size ==
|
|
|
|
oldsize);
|
2016-02-20 11:24:58 +08:00
|
|
|
if ((usize_max > SMALL_MAXCLASS ||
|
|
|
|
size2index(usize_max) != size2index(oldsize)) &&
|
|
|
|
(size > oldsize || usize_max < oldsize))
|
|
|
|
return (true);
|
2015-09-12 07:18:53 +08:00
|
|
|
} else {
|
2016-02-20 11:24:58 +08:00
|
|
|
if (usize_max <= SMALL_MAXCLASS)
|
|
|
|
return (true);
|
2016-05-11 13:21:10 +08:00
|
|
|
if (arena_ralloc_large(tsdn, ptr, oldsize, usize_min,
|
2016-02-20 11:24:58 +08:00
|
|
|
usize_max, zero))
|
|
|
|
return (true);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_decay_tick(tsdn, extent_node_arena_get(&chunk->node));
|
2016-02-20 11:24:58 +08:00
|
|
|
return (false);
|
2015-09-12 07:18:53 +08:00
|
|
|
} else {
|
2016-05-11 13:21:10 +08:00
|
|
|
return (huge_ralloc_no_move(tsdn, ptr, oldsize, usize_min,
|
2016-02-20 12:09:31 +08:00
|
|
|
usize_max, zero));
|
2015-09-12 07:18:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_ralloc_move_helper(tsdn_t *tsdn, arena_t *arena, size_t usize,
|
2015-09-12 07:18:53 +08:00
|
|
|
size_t alignment, bool zero, tcache_t *tcache)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (alignment == 0)
|
2016-05-11 13:21:10 +08:00
|
|
|
return (arena_malloc(tsdn, arena, usize, size2index(usize),
|
|
|
|
zero, tcache, true));
|
2015-09-12 07:18:53 +08:00
|
|
|
usize = sa2u(usize, alignment);
|
2016-02-26 07:29:49 +08:00
|
|
|
if (unlikely(usize == 0 || usize > HUGE_MAXCLASS))
|
2015-09-12 07:18:53 +08:00
|
|
|
return (NULL);
|
2016-05-11 13:21:10 +08:00
|
|
|
return (ipalloct(tsdn, usize, alignment, zero, tcache, arena));
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2014-09-23 12:09:23 +08:00
|
|
|
arena_ralloc(tsd_t *tsd, arena_t *arena, void *ptr, size_t oldsize, size_t size,
|
2015-09-12 07:18:53 +08:00
|
|
|
size_t alignment, bool zero, tcache_t *tcache)
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
2015-09-12 07:18:53 +08:00
|
|
|
size_t usize;
|
|
|
|
|
|
|
|
usize = s2u(size);
|
2016-02-26 07:29:49 +08:00
|
|
|
if (unlikely(usize == 0 || size > HUGE_MAXCLASS))
|
2015-09-12 07:18:53 +08:00
|
|
|
return (NULL);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
|
2015-09-12 11:50:20 +08:00
|
|
|
if (likely(usize <= large_maxclass)) {
|
2015-02-13 06:06:37 +08:00
|
|
|
size_t copysize;
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
|
2015-02-13 06:06:37 +08:00
|
|
|
/* Try to avoid moving the allocation. */
|
2016-05-11 13:21:10 +08:00
|
|
|
if (!arena_ralloc_no_move(tsd_tsdn(tsd), ptr, oldsize, usize, 0,
|
|
|
|
zero))
|
2015-02-13 06:06:37 +08:00
|
|
|
return (ptr);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-02-13 06:06:37 +08:00
|
|
|
/*
|
|
|
|
* size and oldsize are different enough that we need to move
|
|
|
|
* the object. In that case, fall back to allocating new space
|
|
|
|
* and copying.
|
|
|
|
*/
|
2016-05-11 13:21:10 +08:00
|
|
|
ret = arena_ralloc_move_helper(tsd_tsdn(tsd), arena, usize,
|
|
|
|
alignment, zero, tcache);
|
2015-09-12 07:18:53 +08:00
|
|
|
if (ret == NULL)
|
|
|
|
return (NULL);
|
Add {,r,s,d}allocm().
Add allocm(), rallocm(), sallocm(), and dallocm(), which are a
functional superset of malloc(), calloc(), posix_memalign(),
malloc_usable_size(), and free().
2010-09-18 06:46:18 +08:00
|
|
|
|
2015-02-13 06:06:37 +08:00
|
|
|
/*
|
|
|
|
* Junk/zero-filling were already done by
|
|
|
|
* ipalloc()/arena_malloc().
|
|
|
|
*/
|
|
|
|
|
2015-09-12 07:18:53 +08:00
|
|
|
copysize = (usize < oldsize) ? usize : oldsize;
|
2015-02-13 06:06:37 +08:00
|
|
|
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, copysize);
|
|
|
|
memcpy(ret, ptr, copysize);
|
2016-05-07 03:16:00 +08:00
|
|
|
isqalloc(tsd, ptr, oldsize, tcache, true);
|
2015-02-13 06:06:37 +08:00
|
|
|
} else {
|
2015-09-12 07:18:53 +08:00
|
|
|
ret = huge_ralloc(tsd, arena, ptr, oldsize, usize, alignment,
|
|
|
|
zero, tcache);
|
2015-02-13 06:06:37 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2012-10-12 04:53:15 +08:00
|
|
|
dss_prec_t
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dss_prec_get(tsdn_t *tsdn, arena_t *arena)
|
2012-10-12 04:53:15 +08:00
|
|
|
{
|
|
|
|
dss_prec_t ret;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2012-10-12 04:53:15 +08:00
|
|
|
ret = arena->dss_prec;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2012-10-12 04:53:15 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2014-04-16 03:09:48 +08:00
|
|
|
bool
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_dss_prec_set(tsdn_t *tsdn, arena_t *arena, 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-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2012-10-12 04:53:15 +08:00
|
|
|
arena->dss_prec = dss_prec;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2014-04-16 03:09:48 +08:00
|
|
|
return (false);
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
|
2015-03-19 09:55:33 +08:00
|
|
|
ssize_t
|
|
|
|
arena_lg_dirty_mult_default_get(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return ((ssize_t)atomic_read_z((size_t *)&lg_dirty_mult_default));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
arena_lg_dirty_mult_default_set(ssize_t lg_dirty_mult)
|
|
|
|
{
|
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
if (opt_purge != purge_mode_ratio)
|
|
|
|
return (true);
|
2015-03-19 09:55:33 +08:00
|
|
|
if (!arena_lg_dirty_mult_valid(lg_dirty_mult))
|
|
|
|
return (true);
|
|
|
|
atomic_write_z((size_t *)&lg_dirty_mult_default, (size_t)lg_dirty_mult);
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
ssize_t
|
|
|
|
arena_decay_time_default_get(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return ((ssize_t)atomic_read_z((size_t *)&decay_time_default));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
arena_decay_time_default_set(ssize_t decay_time)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (opt_purge != purge_mode_decay)
|
|
|
|
return (true);
|
|
|
|
if (!arena_decay_time_valid(decay_time))
|
|
|
|
return (true);
|
|
|
|
atomic_write_z((size_t *)&decay_time_default, (size_t)decay_time);
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2016-02-28 12:40:13 +08:00
|
|
|
static void
|
|
|
|
arena_basic_stats_merge_locked(arena_t *arena, unsigned *nthreads,
|
|
|
|
const char **dss, ssize_t *lg_dirty_mult, ssize_t *decay_time,
|
|
|
|
size_t *nactive, size_t *ndirty)
|
2012-10-12 04:53:15 +08:00
|
|
|
{
|
|
|
|
|
2016-04-23 05:34:14 +08:00
|
|
|
*nthreads += arena_nthreads_get(arena, false);
|
2012-10-12 04:53:15 +08:00
|
|
|
*dss = dss_prec_names[arena->dss_prec];
|
2015-03-25 07:36:12 +08:00
|
|
|
*lg_dirty_mult = arena->lg_dirty_mult;
|
2016-02-20 12:09:31 +08:00
|
|
|
*decay_time = arena->decay_time;
|
2012-10-12 04:53:15 +08:00
|
|
|
*nactive += arena->nactive;
|
|
|
|
*ndirty += arena->ndirty;
|
2016-02-28 12:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_basic_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,
|
2016-04-14 14:36:15 +08:00
|
|
|
const char **dss, ssize_t *lg_dirty_mult, ssize_t *decay_time,
|
|
|
|
size_t *nactive, size_t *ndirty)
|
2016-02-28 12:40:13 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2016-02-28 12:40:13 +08:00
|
|
|
arena_basic_stats_merge_locked(arena, nthreads, dss, lg_dirty_mult,
|
|
|
|
decay_time, nactive, ndirty);
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2016-02-28 12:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,
|
2016-04-14 14:36:15 +08:00
|
|
|
const char **dss, ssize_t *lg_dirty_mult, ssize_t *decay_time,
|
|
|
|
size_t *nactive, size_t *ndirty, arena_stats_t *astats,
|
|
|
|
malloc_bin_stats_t *bstats, malloc_large_stats_t *lstats,
|
|
|
|
malloc_huge_stats_t *hstats)
|
2016-02-28 12:40:13 +08:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
cassert(config_stats);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &arena->lock);
|
2016-02-28 12:40:13 +08:00
|
|
|
arena_basic_stats_merge_locked(arena, nthreads, dss, lg_dirty_mult,
|
|
|
|
decay_time, nactive, ndirty);
|
2012-10-12 04:53:15 +08:00
|
|
|
|
|
|
|
astats->mapped += arena->stats.mapped;
|
2016-05-04 13:11:35 +08:00
|
|
|
astats->retained += arena->stats.retained;
|
2012-10-12 04:53:15 +08:00
|
|
|
astats->npurge += arena->stats.npurge;
|
|
|
|
astats->nmadvise += arena->stats.nmadvise;
|
|
|
|
astats->purged += arena->stats.purged;
|
2014-11-28 03:22:36 +08:00
|
|
|
astats->metadata_mapped += arena->stats.metadata_mapped;
|
|
|
|
astats->metadata_allocated += arena_metadata_allocated_get(arena);
|
2012-10-12 04:53:15 +08:00
|
|
|
astats->allocated_large += arena->stats.allocated_large;
|
|
|
|
astats->nmalloc_large += arena->stats.nmalloc_large;
|
|
|
|
astats->ndalloc_large += arena->stats.ndalloc_large;
|
|
|
|
astats->nrequests_large += arena->stats.nrequests_large;
|
2014-05-16 13:22:27 +08:00
|
|
|
astats->allocated_huge += arena->stats.allocated_huge;
|
|
|
|
astats->nmalloc_huge += arena->stats.nmalloc_huge;
|
|
|
|
astats->ndalloc_huge += arena->stats.ndalloc_huge;
|
2012-10-12 04:53:15 +08:00
|
|
|
|
|
|
|
for (i = 0; i < nlclasses; i++) {
|
|
|
|
lstats[i].nmalloc += arena->stats.lstats[i].nmalloc;
|
|
|
|
lstats[i].ndalloc += arena->stats.lstats[i].ndalloc;
|
|
|
|
lstats[i].nrequests += arena->stats.lstats[i].nrequests;
|
|
|
|
lstats[i].curruns += arena->stats.lstats[i].curruns;
|
|
|
|
}
|
2014-10-13 13:53:59 +08:00
|
|
|
|
|
|
|
for (i = 0; i < nhclasses; i++) {
|
|
|
|
hstats[i].nmalloc += arena->stats.hstats[i].nmalloc;
|
|
|
|
hstats[i].ndalloc += arena->stats.hstats[i].ndalloc;
|
|
|
|
hstats[i].curhchunks += arena->stats.hstats[i].curhchunks;
|
|
|
|
}
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &arena->lock);
|
2012-10-12 04:53:15 +08:00
|
|
|
|
|
|
|
for (i = 0; i < NBINS; i++) {
|
|
|
|
arena_bin_t *bin = &arena->bins[i];
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &bin->lock);
|
2012-10-12 04:53:15 +08:00
|
|
|
bstats[i].nmalloc += bin->stats.nmalloc;
|
|
|
|
bstats[i].ndalloc += bin->stats.ndalloc;
|
|
|
|
bstats[i].nrequests += bin->stats.nrequests;
|
2014-10-13 13:53:59 +08:00
|
|
|
bstats[i].curregs += bin->stats.curregs;
|
2012-10-12 04:53:15 +08:00
|
|
|
if (config_tcache) {
|
|
|
|
bstats[i].nfills += bin->stats.nfills;
|
|
|
|
bstats[i].nflushes += bin->stats.nflushes;
|
|
|
|
}
|
|
|
|
bstats[i].nruns += bin->stats.nruns;
|
|
|
|
bstats[i].reruns += bin->stats.reruns;
|
|
|
|
bstats[i].curruns += bin->stats.curruns;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &bin->lock);
|
2012-10-12 04:53:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 15:58:10 +08:00
|
|
|
unsigned
|
2016-04-23 05:34:14 +08:00
|
|
|
arena_nthreads_get(arena_t *arena, bool internal)
|
2016-02-25 15:58:10 +08:00
|
|
|
{
|
|
|
|
|
2016-04-23 05:34:14 +08:00
|
|
|
return (atomic_read_u(&arena->nthreads[internal]));
|
2016-02-25 15:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-23 05:34:14 +08:00
|
|
|
arena_nthreads_inc(arena_t *arena, bool internal)
|
2016-02-25 15:58:10 +08:00
|
|
|
{
|
|
|
|
|
2016-04-23 05:34:14 +08:00
|
|
|
atomic_add_u(&arena->nthreads[internal], 1);
|
2016-02-25 15:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-23 05:34:14 +08:00
|
|
|
arena_nthreads_dec(arena_t *arena, bool internal)
|
2016-02-25 15:58:10 +08:00
|
|
|
{
|
|
|
|
|
2016-04-23 05:34:14 +08:00
|
|
|
atomic_sub_u(&arena->nthreads[internal], 1);
|
2016-02-25 15:58:10 +08:00
|
|
|
}
|
|
|
|
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
arena_t *
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_new(tsdn_t *tsdn, unsigned ind)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
arena_t *arena;
|
2016-02-24 04:06:21 +08:00
|
|
|
size_t arena_size;
|
2010-01-17 01:53:50 +08:00
|
|
|
unsigned i;
|
|
|
|
|
2016-02-24 04:06:21 +08:00
|
|
|
/* Compute arena size to incorporate sufficient runs_avail elements. */
|
2016-03-27 08:30:37 +08:00
|
|
|
arena_size = offsetof(arena_t, runs_avail) + (sizeof(arena_run_heap_t) *
|
2016-02-25 12:10:02 +08:00
|
|
|
runs_avail_nclasses);
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
/*
|
2014-10-13 13:53:59 +08:00
|
|
|
* Allocate arena, arena->lstats, and arena->hstats contiguously, mainly
|
|
|
|
* because there is no way to clean up if base_alloc() OOMs.
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
*/
|
|
|
|
if (config_stats) {
|
2016-05-11 13:21:10 +08:00
|
|
|
arena = (arena_t *)base_alloc(tsdn,
|
|
|
|
CACHELINE_CEILING(arena_size) + QUANTUM_CEILING(nlclasses *
|
|
|
|
sizeof(malloc_large_stats_t) + nhclasses) *
|
|
|
|
sizeof(malloc_huge_stats_t));
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
} else
|
2016-05-11 13:21:10 +08:00
|
|
|
arena = (arena_t *)base_alloc(tsdn, arena_size);
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
if (arena == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
arena->ind = ind;
|
2016-04-23 05:34:14 +08:00
|
|
|
arena->nthreads[0] = arena->nthreads[1] = 0;
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&arena->lock, "arena", WITNESS_RANK_ARENA))
|
Move centralized chunk management into arenas.
Migrate all centralized data structures related to huge allocations and
recyclable chunks into arena_t, so that each arena can manage huge
allocations and recyclable virtual memory completely independently of
other arenas.
Add chunk node caching to arenas, in order to avoid contention on the
base allocator.
Use chunks_rtree to look up huge allocations rather than a red-black
tree. Maintain a per arena unsorted list of huge allocations (which
will be needed to enumerate huge allocations during arena reset).
Remove the --enable-ivsalloc option, make ivsalloc() always available,
and use it for size queries if --enable-debug is enabled. The only
practical implications to this removal are that 1) ivsalloc() is now
always available during live debugging (and the underlying radix tree is
available during core-based debugging), and 2) size query validation can
no longer be enabled independent of --enable-debug.
Remove the stats.chunks.{current,total,high} mallctls, and replace their
underlying statistics with simpler atomically updated counters used
exclusively for gdump triggering. These statistics are no longer very
useful because each arena manages chunks independently, and per arena
statistics provide similar information.
Simplify chunk synchronization code, now that base chunk allocation
cannot cause recursive lock acquisition.
2015-02-12 04:24:27 +08:00
|
|
|
return (NULL);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats) {
|
|
|
|
memset(&arena->stats, 0, sizeof(arena_stats_t));
|
2014-11-18 02:31:59 +08:00
|
|
|
arena->stats.lstats = (malloc_large_stats_t *)((uintptr_t)arena
|
2016-02-24 04:06:21 +08:00
|
|
|
+ CACHELINE_CEILING(arena_size));
|
2012-02-11 12:22:09 +08:00
|
|
|
memset(arena->stats.lstats, 0, nlclasses *
|
|
|
|
sizeof(malloc_large_stats_t));
|
2014-11-18 02:31:59 +08:00
|
|
|
arena->stats.hstats = (malloc_huge_stats_t *)((uintptr_t)arena
|
2016-02-24 04:06:21 +08:00
|
|
|
+ CACHELINE_CEILING(arena_size) +
|
2014-10-13 13:53:59 +08:00
|
|
|
QUANTUM_CEILING(nlclasses * sizeof(malloc_large_stats_t)));
|
|
|
|
memset(arena->stats.hstats, 0, nhclasses *
|
|
|
|
sizeof(malloc_huge_stats_t));
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_tcache)
|
|
|
|
ql_new(&arena->tcache_ql);
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_prof)
|
|
|
|
arena->prof_accumbytes = 0;
|
2010-02-12 05:19:21 +08:00
|
|
|
|
2015-05-05 00:58:36 +08:00
|
|
|
if (config_cache_oblivious) {
|
|
|
|
/*
|
|
|
|
* A nondeterministic seed based on the address of arena reduces
|
|
|
|
* the likelihood of lockstep non-uniform cache index
|
|
|
|
* utilization among identical concurrent processes, but at the
|
|
|
|
* cost of test repeatability. For debug builds, instead use a
|
|
|
|
* deterministic seed.
|
|
|
|
*/
|
|
|
|
arena->offset_state = config_debug ? ind :
|
|
|
|
(uint64_t)(uintptr_t)arena;
|
|
|
|
}
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
arena->dss_prec = chunk_dss_prec_get(tsdn);
|
2012-10-12 04:53:15 +08:00
|
|
|
|
2016-04-23 05:37:17 +08:00
|
|
|
ql_new(&arena->achunks);
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
arena->spare = NULL;
|
|
|
|
|
2015-03-19 09:55:33 +08:00
|
|
|
arena->lg_dirty_mult = arena_lg_dirty_mult_default_get();
|
2015-06-23 09:50:32 +08:00
|
|
|
arena->purging = false;
|
2010-01-17 01:53:50 +08:00
|
|
|
arena->nactive = 0;
|
|
|
|
arena->ndirty = 0;
|
|
|
|
|
2016-02-24 04:06:21 +08:00
|
|
|
for(i = 0; i < runs_avail_nclasses; i++)
|
2016-03-27 08:30:37 +08:00
|
|
|
arena_run_heap_new(&arena->runs_avail[i]);
|
2015-02-16 10:04:46 +08:00
|
|
|
qr_new(&arena->runs_dirty, rd_link);
|
2015-02-18 17:15:50 +08:00
|
|
|
qr_new(&arena->chunks_cache, cc_link);
|
2015-02-16 10:04:46 +08:00
|
|
|
|
2016-02-20 12:09:31 +08:00
|
|
|
if (opt_purge == purge_mode_decay)
|
|
|
|
arena_decay_init(arena, arena_decay_time_default_get());
|
|
|
|
|
2015-02-16 10:04:46 +08:00
|
|
|
ql_new(&arena->huge);
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&arena->huge_mtx, "arena_huge",
|
|
|
|
WITNESS_RANK_ARENA_HUGE))
|
2015-02-16 10:04:46 +08:00
|
|
|
return (NULL);
|
|
|
|
|
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
|
|
|
extent_tree_szad_new(&arena->chunks_szad_cached);
|
|
|
|
extent_tree_ad_new(&arena->chunks_ad_cached);
|
|
|
|
extent_tree_szad_new(&arena->chunks_szad_retained);
|
|
|
|
extent_tree_ad_new(&arena->chunks_ad_retained);
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&arena->chunks_mtx, "arena_chunks",
|
|
|
|
WITNESS_RANK_ARENA_CHUNKS))
|
2015-02-16 10:04:46 +08:00
|
|
|
return (NULL);
|
|
|
|
ql_new(&arena->node_cache);
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&arena->node_cache_mtx, "arena_node_cache",
|
|
|
|
WITNESS_RANK_ARENA_NODE_CACHE))
|
2015-02-16 10:04:46 +08:00
|
|
|
return (NULL);
|
|
|
|
|
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
|
|
|
arena->chunk_hooks = chunk_hooks_default;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/* Initialize bins. */
|
2012-02-29 08:50:47 +08:00
|
|
|
for (i = 0; i < NBINS; i++) {
|
2016-04-23 05:36:48 +08:00
|
|
|
arena_bin_t *bin = &arena->bins[i];
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&bin->lock, "arena_bin",
|
|
|
|
WITNESS_RANK_ARENA_BIN))
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
return (NULL);
|
2010-01-17 01:53:50 +08:00
|
|
|
bin->runcur = NULL;
|
2016-03-27 08:30:37 +08:00
|
|
|
arena_run_heap_new(&bin->runs);
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats)
|
|
|
|
memset(&bin->stats, 0, sizeof(malloc_bin_stats_t));
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
return (arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2011-03-16 04:59:15 +08:00
|
|
|
/*
|
|
|
|
* Calculate bin_info->run_size such that it meets the following constraints:
|
|
|
|
*
|
2014-10-06 08:54:10 +08:00
|
|
|
* *) bin_info->run_size <= arena_maxrun
|
2011-03-23 00:00:56 +08:00
|
|
|
* *) bin_info->nregs <= RUN_MAXREGS
|
2011-03-16 04:59:15 +08:00
|
|
|
*
|
2014-09-29 16:31:39 +08:00
|
|
|
* bin_info->nregs and bin_info->reg0_offset are also calculated here, since
|
|
|
|
* these settings are all interdependent.
|
2011-03-16 04:59:15 +08:00
|
|
|
*/
|
2014-09-29 16:31:39 +08:00
|
|
|
static void
|
|
|
|
bin_info_run_size_calc(arena_bin_info_t *bin_info)
|
2011-03-16 04:59:15 +08:00
|
|
|
{
|
2012-04-06 15:35:09 +08:00
|
|
|
size_t pad_size;
|
2014-09-29 16:31:39 +08:00
|
|
|
size_t try_run_size, perfect_run_size, actual_run_size;
|
|
|
|
uint32_t try_nregs, perfect_nregs, actual_nregs;
|
2011-03-16 04:59:15 +08:00
|
|
|
|
2012-04-06 15:35:09 +08:00
|
|
|
/*
|
|
|
|
* Determine redzone size based on minimum alignment and minimum
|
|
|
|
* redzone size. Add padding to the end of the run if it is needed to
|
|
|
|
* align the regions. The padding allows each redzone to be half the
|
|
|
|
* minimum alignment; without the padding, each redzone would have to
|
|
|
|
* be twice as large in order to maintain alignment.
|
|
|
|
*/
|
2014-09-12 07:20:44 +08:00
|
|
|
if (config_fill && unlikely(opt_redzone)) {
|
2016-02-25 02:32:45 +08:00
|
|
|
size_t align_min = ZU(1) << (ffs_zu(bin_info->reg_size) - 1);
|
2012-04-06 15:35:09 +08:00
|
|
|
if (align_min <= REDZONE_MINSIZE) {
|
|
|
|
bin_info->redzone_size = REDZONE_MINSIZE;
|
|
|
|
pad_size = 0;
|
|
|
|
} else {
|
|
|
|
bin_info->redzone_size = align_min >> 1;
|
|
|
|
pad_size = bin_info->redzone_size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bin_info->redzone_size = 0;
|
|
|
|
pad_size = 0;
|
|
|
|
}
|
|
|
|
bin_info->reg_interval = bin_info->reg_size +
|
|
|
|
(bin_info->redzone_size << 1);
|
|
|
|
|
2011-03-16 04:59:15 +08:00
|
|
|
/*
|
2014-09-29 16:31:39 +08:00
|
|
|
* Compute run size under ideal conditions (no redzones, no limit on run
|
|
|
|
* size).
|
2011-03-16 04:59:15 +08:00
|
|
|
*/
|
2014-09-29 16:31:39 +08:00
|
|
|
try_run_size = PAGE;
|
2016-02-25 04:42:23 +08:00
|
|
|
try_nregs = (uint32_t)(try_run_size / bin_info->reg_size);
|
2011-03-16 04:59:15 +08:00
|
|
|
do {
|
2014-09-29 16:31:39 +08:00
|
|
|
perfect_run_size = try_run_size;
|
|
|
|
perfect_nregs = try_nregs;
|
2011-03-16 04:59:15 +08:00
|
|
|
|
2012-04-02 22:04:34 +08:00
|
|
|
try_run_size += PAGE;
|
2016-02-25 04:42:23 +08:00
|
|
|
try_nregs = (uint32_t)(try_run_size / bin_info->reg_size);
|
2014-09-29 16:31:39 +08:00
|
|
|
} while (perfect_run_size != perfect_nregs * bin_info->reg_size);
|
|
|
|
assert(perfect_nregs <= RUN_MAXREGS);
|
|
|
|
|
|
|
|
actual_run_size = perfect_run_size;
|
2016-02-25 04:42:23 +08:00
|
|
|
actual_nregs = (uint32_t)((actual_run_size - pad_size) /
|
|
|
|
bin_info->reg_interval);
|
2014-09-29 16:31:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Redzones can require enough padding that not even a single region can
|
|
|
|
* fit within the number of pages that would normally be dedicated to a
|
|
|
|
* run for this size class. Increase the run size until at least one
|
|
|
|
* region fits.
|
|
|
|
*/
|
|
|
|
while (actual_nregs == 0) {
|
|
|
|
assert(config_fill && unlikely(opt_redzone));
|
|
|
|
|
|
|
|
actual_run_size += PAGE;
|
2016-02-25 04:42:23 +08:00
|
|
|
actual_nregs = (uint32_t)((actual_run_size - pad_size) /
|
|
|
|
bin_info->reg_interval);
|
2014-09-29 16:31:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure that the run will fit within an arena chunk.
|
|
|
|
*/
|
2014-10-06 08:54:10 +08:00
|
|
|
while (actual_run_size > arena_maxrun) {
|
2014-09-29 16:31:39 +08:00
|
|
|
actual_run_size -= PAGE;
|
2016-02-25 04:42:23 +08:00
|
|
|
actual_nregs = (uint32_t)((actual_run_size - pad_size) /
|
|
|
|
bin_info->reg_interval);
|
2014-09-29 16:31:39 +08:00
|
|
|
}
|
|
|
|
assert(actual_nregs > 0);
|
2015-03-07 09:14:05 +08:00
|
|
|
assert(actual_run_size == s2u(actual_run_size));
|
2011-03-16 04:59:15 +08:00
|
|
|
|
|
|
|
/* Copy final settings. */
|
2014-09-29 16:31:39 +08:00
|
|
|
bin_info->run_size = actual_run_size;
|
|
|
|
bin_info->nregs = actual_nregs;
|
2016-02-25 04:42:23 +08:00
|
|
|
bin_info->reg0_offset = (uint32_t)(actual_run_size - (actual_nregs *
|
|
|
|
bin_info->reg_interval) - pad_size + bin_info->redzone_size);
|
2012-04-06 15:35:09 +08:00
|
|
|
|
2015-05-05 00:58:36 +08:00
|
|
|
if (actual_run_size > small_maxrun)
|
|
|
|
small_maxrun = actual_run_size;
|
|
|
|
|
2012-04-06 15:35:09 +08:00
|
|
|
assert(bin_info->reg0_offset - bin_info->redzone_size + (bin_info->nregs
|
|
|
|
* bin_info->reg_interval) + pad_size == bin_info->run_size);
|
2011-03-16 04:59:15 +08:00
|
|
|
}
|
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
static void
|
2011-03-16 04:59:15 +08:00
|
|
|
bin_info_init(void)
|
|
|
|
{
|
|
|
|
arena_bin_info_t *bin_info;
|
2012-02-29 08:50:47 +08:00
|
|
|
|
2015-05-05 00:58:36 +08:00
|
|
|
#define BIN_INFO_INIT_bin_yes(index, size) \
|
2014-05-29 07:11:55 +08:00
|
|
|
bin_info = &arena_bin_info[index]; \
|
2012-02-29 08:50:47 +08:00
|
|
|
bin_info->reg_size = size; \
|
2014-09-29 16:31:39 +08:00
|
|
|
bin_info_run_size_calc(bin_info); \
|
2012-02-29 08:50:47 +08:00
|
|
|
bitmap_info_init(&bin_info->bitmap_info, bin_info->nregs);
|
2014-05-29 07:11:55 +08:00
|
|
|
#define BIN_INFO_INIT_bin_no(index, size)
|
|
|
|
#define SC(index, lg_grp, lg_delta, ndelta, bin, lg_delta_lookup) \
|
|
|
|
BIN_INFO_INIT_bin_##bin(index, (ZU(1)<<lg_grp) + (ZU(ndelta)<<lg_delta))
|
2012-02-29 08:50:47 +08:00
|
|
|
SIZE_CLASSES
|
2014-05-29 07:11:55 +08:00
|
|
|
#undef BIN_INFO_INIT_bin_yes
|
|
|
|
#undef BIN_INFO_INIT_bin_no
|
|
|
|
#undef SC
|
2011-03-16 04:59:15 +08:00
|
|
|
}
|
|
|
|
|
2015-05-05 00:58:36 +08:00
|
|
|
static bool
|
|
|
|
small_run_size_init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(small_maxrun != 0);
|
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
small_run_tab = (bool *)base_alloc(NULL, sizeof(bool) * (small_maxrun >>
|
2015-05-05 00:58:36 +08:00
|
|
|
LG_PAGE));
|
|
|
|
if (small_run_tab == NULL)
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
#define TAB_INIT_bin_yes(index, size) { \
|
|
|
|
arena_bin_info_t *bin_info = &arena_bin_info[index]; \
|
|
|
|
small_run_tab[bin_info->run_size >> LG_PAGE] = true; \
|
|
|
|
}
|
|
|
|
#define TAB_INIT_bin_no(index, size)
|
|
|
|
#define SC(index, lg_grp, lg_delta, ndelta, bin, lg_delta_lookup) \
|
|
|
|
TAB_INIT_bin_##bin(index, (ZU(1)<<lg_grp) + (ZU(ndelta)<<lg_delta))
|
|
|
|
SIZE_CLASSES
|
|
|
|
#undef TAB_INIT_bin_yes
|
|
|
|
#undef TAB_INIT_bin_no
|
|
|
|
#undef SC
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2016-02-23 08:20:56 +08:00
|
|
|
static bool
|
|
|
|
run_quantize_init(void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
run_quantize_max = chunksize + large_pad;
|
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
run_quantize_floor_tab = (size_t *)base_alloc(NULL, sizeof(size_t) *
|
2016-02-23 08:20:56 +08:00
|
|
|
(run_quantize_max >> LG_PAGE));
|
|
|
|
if (run_quantize_floor_tab == NULL)
|
|
|
|
return (true);
|
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
run_quantize_ceil_tab = (size_t *)base_alloc(NULL, sizeof(size_t) *
|
2016-02-23 08:20:56 +08:00
|
|
|
(run_quantize_max >> LG_PAGE));
|
|
|
|
if (run_quantize_ceil_tab == NULL)
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
for (i = 1; i <= run_quantize_max >> LG_PAGE; i++) {
|
|
|
|
size_t run_size = i << LG_PAGE;
|
|
|
|
|
|
|
|
run_quantize_floor_tab[i-1] =
|
|
|
|
run_quantize_floor_compute(run_size);
|
|
|
|
run_quantize_ceil_tab[i-1] =
|
|
|
|
run_quantize_ceil_compute(run_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:58:36 +08:00
|
|
|
bool
|
2010-01-30 06:30:41 +08:00
|
|
|
arena_boot(void)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2010-10-02 08:35:43 +08:00
|
|
|
unsigned i;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-03-19 09:55:33 +08:00
|
|
|
arena_lg_dirty_mult_default_set(opt_lg_dirty_mult);
|
2016-02-20 12:09:31 +08:00
|
|
|
arena_decay_time_default_set(opt_decay_time);
|
2015-03-19 09:55:33 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/*
|
|
|
|
* Compute the header size such that it is large enough to contain the
|
2010-10-02 08:35:43 +08:00
|
|
|
* page map. The page map is biased to omit entries for the header
|
|
|
|
* itself, so some iteration is necessary to compute the map bias.
|
|
|
|
*
|
|
|
|
* 1) Compute safe header_size and map_bias values that include enough
|
|
|
|
* space for an unbiased page map.
|
|
|
|
* 2) Refine map_bias based on (1) to omit the header pages in the page
|
|
|
|
* map. The resulting map_bias may be one too small.
|
|
|
|
* 3) Refine map_bias based on (2). The result will be >= the result
|
|
|
|
* from (2), and will always be correct.
|
2010-01-17 01:53:50 +08:00
|
|
|
*/
|
2010-10-02 08:35:43 +08:00
|
|
|
map_bias = 0;
|
|
|
|
for (i = 0; i < 3; i++) {
|
2015-09-04 18:15:28 +08:00
|
|
|
size_t header_size = offsetof(arena_chunk_t, map_bits) +
|
2014-08-30 04:34:40 +08:00
|
|
|
((sizeof(arena_chunk_map_bits_t) +
|
|
|
|
sizeof(arena_chunk_map_misc_t)) * (chunk_npages-map_bias));
|
2014-09-29 16:31:39 +08:00
|
|
|
map_bias = (header_size + PAGE_MASK) >> LG_PAGE;
|
2010-10-02 08:35:43 +08:00
|
|
|
}
|
|
|
|
assert(map_bias > 0);
|
|
|
|
|
2014-08-30 04:34:40 +08:00
|
|
|
map_misc_offset = offsetof(arena_chunk_t, map_bits) +
|
|
|
|
sizeof(arena_chunk_map_bits_t) * (chunk_npages-map_bias);
|
|
|
|
|
2014-10-06 08:54:10 +08:00
|
|
|
arena_maxrun = chunksize - (map_bias << LG_PAGE);
|
2014-10-10 08:54:06 +08:00
|
|
|
assert(arena_maxrun > 0);
|
2015-09-12 11:50:20 +08:00
|
|
|
large_maxclass = index2size(size2index(chunksize)-1);
|
|
|
|
if (large_maxclass > arena_maxrun) {
|
2014-10-06 08:54:10 +08:00
|
|
|
/*
|
|
|
|
* For small chunk sizes it's possible for there to be fewer
|
|
|
|
* non-header pages available than are necessary to serve the
|
|
|
|
* size classes just below chunksize.
|
|
|
|
*/
|
2015-09-12 11:50:20 +08:00
|
|
|
large_maxclass = arena_maxrun;
|
2014-10-06 08:54:10 +08:00
|
|
|
}
|
2015-09-12 11:50:20 +08:00
|
|
|
assert(large_maxclass > 0);
|
|
|
|
nlclasses = size2index(large_maxclass) - size2index(SMALL_MAXCLASS);
|
2014-10-13 13:53:59 +08:00
|
|
|
nhclasses = NSIZES - nlclasses - NBINS;
|
2010-01-30 06:30:41 +08:00
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
bin_info_init();
|
2016-02-23 08:20:56 +08:00
|
|
|
if (small_run_size_init())
|
|
|
|
return (true);
|
|
|
|
if (run_quantize_init())
|
|
|
|
return (true);
|
|
|
|
|
2016-02-24 04:06:21 +08:00
|
|
|
runs_avail_bias = size2index(PAGE);
|
|
|
|
runs_avail_nclasses = size2index(run_quantize_max)+1 - runs_avail_bias;
|
|
|
|
|
2016-02-23 08:20:56 +08:00
|
|
|
return (false);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2012-03-14 07:31:41 +08:00
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_prefork0(tsdn_t *tsdn, arena_t *arena)
|
2012-03-14 07:31:41 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_prefork(tsdn, &arena->lock);
|
2016-04-26 14:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_prefork1(tsdn_t *tsdn, arena_t *arena)
|
2016-04-26 14:14:40 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_prefork(tsdn, &arena->chunks_mtx);
|
2016-04-26 14:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_prefork2(tsdn_t *tsdn, arena_t *arena)
|
2016-04-26 14:14:40 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_prefork(tsdn, &arena->node_cache_mtx);
|
2016-04-26 14:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_prefork3(tsdn_t *tsdn, arena_t *arena)
|
2016-04-26 14:14:40 +08:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2012-03-14 07:31:41 +08:00
|
|
|
for (i = 0; i < NBINS; i++)
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_prefork(tsdn, &arena->bins[i].lock);
|
|
|
|
malloc_mutex_prefork(tsdn, &arena->huge_mtx);
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_postfork_parent(tsdn_t *tsdn, arena_t *arena)
|
2012-03-14 07:31:41 +08:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_parent(tsdn, &arena->huge_mtx);
|
2012-03-14 07:31:41 +08:00
|
|
|
for (i = 0; i < NBINS; i++)
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_parent(tsdn, &arena->bins[i].lock);
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &arena->node_cache_mtx);
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &arena->chunks_mtx);
|
|
|
|
malloc_mutex_postfork_parent(tsdn, &arena->lock);
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
arena_postfork_child(tsdn_t *tsdn, arena_t *arena)
|
2012-03-14 07:31:41 +08:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_child(tsdn, &arena->huge_mtx);
|
2012-03-14 07:31:41 +08:00
|
|
|
for (i = 0; i < NBINS; i++)
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_child(tsdn, &arena->bins[i].lock);
|
|
|
|
malloc_mutex_postfork_child(tsdn, &arena->node_cache_mtx);
|
|
|
|
malloc_mutex_postfork_child(tsdn, &arena->chunks_mtx);
|
|
|
|
malloc_mutex_postfork_child(tsdn, &arena->lock);
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|