2010-01-17 01:53:50 +08:00
|
|
|
#define JEMALLOC_BASE_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-03-14 07:31:41 +08:00
|
|
|
static malloc_mutex_t base_mtx;
|
2016-05-18 05:58:56 +08:00
|
|
|
static extent_heap_t base_avail[NSIZES];
|
2016-03-24 12:09:28 +08:00
|
|
|
static extent_t *base_extents;
|
2014-11-28 03:22:36 +08:00
|
|
|
static size_t base_allocated;
|
2015-03-24 08:25:57 +08:00
|
|
|
static size_t base_resident;
|
|
|
|
static size_t base_mapped;
|
2014-11-28 03:22:36 +08:00
|
|
|
|
2010-01-24 18:53:40 +08:00
|
|
|
/******************************************************************************/
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-03-24 12:09:28 +08:00
|
|
|
static extent_t *
|
|
|
|
base_extent_try_alloc(tsdn_t *tsdn)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2016-03-24 12:09:28 +08:00
|
|
|
extent_t *extent;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, &base_mtx);
|
2016-04-18 03:33:39 +08:00
|
|
|
|
2016-03-24 12:09:28 +08:00
|
|
|
if (base_extents == NULL)
|
2015-01-31 13:49:19 +08:00
|
|
|
return (NULL);
|
2016-03-24 12:09:28 +08:00
|
|
|
extent = base_extents;
|
|
|
|
base_extents = *(extent_t **)extent;
|
|
|
|
return (extent);
|
2015-01-31 13:49:19 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-01-31 13:49:19 +08:00
|
|
|
static void
|
2016-03-24 12:09:28 +08:00
|
|
|
base_extent_dalloc(tsdn_t *tsdn, extent_t *extent)
|
2015-01-31 13:49:19 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, &base_mtx);
|
2016-04-18 03:33:39 +08:00
|
|
|
|
2016-03-24 12:09:28 +08:00
|
|
|
*(extent_t **)extent = base_extents;
|
|
|
|
base_extents = extent;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2016-03-24 12:09:28 +08:00
|
|
|
static extent_t *
|
2016-06-02 03:59:02 +08:00
|
|
|
base_extent_alloc(tsdn_t *tsdn, size_t minsize)
|
2015-01-31 13:49:19 +08:00
|
|
|
{
|
2016-03-24 12:09:28 +08:00
|
|
|
extent_t *extent;
|
2015-01-31 13:49:19 +08:00
|
|
|
size_t csize, nsize;
|
|
|
|
void *addr;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_assert_owner(tsdn, &base_mtx);
|
2015-01-31 13:49:19 +08:00
|
|
|
assert(minsize != 0);
|
2016-03-24 12:09:28 +08:00
|
|
|
extent = base_extent_try_alloc(tsdn);
|
|
|
|
/* Allocate enough space to also carve an extent out if necessary. */
|
|
|
|
nsize = (extent == NULL) ? CACHELINE_CEILING(sizeof(extent_t)) : 0;
|
2015-01-31 13:49:19 +08:00
|
|
|
csize = CHUNK_CEILING(minsize + nsize);
|
2016-05-18 08:43:30 +08:00
|
|
|
/*
|
2016-06-02 03:59:02 +08:00
|
|
|
* Directly call extent_alloc_mmap() because it's critical to allocate
|
2016-05-18 08:43:30 +08:00
|
|
|
* untouched demand-zeroed virtual memory.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
bool zero = true;
|
|
|
|
bool commit = true;
|
2016-06-02 03:59:02 +08:00
|
|
|
addr = extent_alloc_mmap(NULL, csize, PAGE, &zero, &commit);
|
2016-05-18 08:43:30 +08:00
|
|
|
}
|
2015-01-31 13:49:19 +08:00
|
|
|
if (addr == NULL) {
|
2016-03-24 12:09:28 +08:00
|
|
|
if (extent != NULL)
|
|
|
|
base_extent_dalloc(tsdn, extent);
|
2015-01-31 13:49:19 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
2015-03-24 08:25:57 +08:00
|
|
|
base_mapped += csize;
|
2016-03-24 12:09:28 +08:00
|
|
|
if (extent == NULL) {
|
|
|
|
extent = (extent_t *)addr;
|
2015-03-24 08:25:57 +08:00
|
|
|
addr = (void *)((uintptr_t)addr + nsize);
|
2015-01-31 13:49:19 +08:00
|
|
|
csize -= nsize;
|
2015-03-24 08:25:57 +08:00
|
|
|
if (config_stats) {
|
2015-01-31 13:49:19 +08:00
|
|
|
base_allocated += nsize;
|
2015-03-24 08:25:57 +08:00
|
|
|
base_resident += PAGE_CEILING(nsize);
|
|
|
|
}
|
2015-01-31 13:49:19 +08:00
|
|
|
}
|
2016-05-30 09:34:50 +08:00
|
|
|
extent_init(extent, NULL, addr, csize, 0, true, true, true, false);
|
2016-03-24 12:09:28 +08:00
|
|
|
return (extent);
|
2015-01-31 13:49:19 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
* base_alloc() guarantees demand-zeroed memory, in order to make multi-page
|
|
|
|
* sparse data structures such as radix tree nodes efficient with respect to
|
|
|
|
* physical memory usage.
|
|
|
|
*/
|
|
|
|
void *
|
2016-05-11 13:21:10 +08:00
|
|
|
base_alloc(tsdn_t *tsdn, size_t size)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
2016-05-18 05:58:56 +08:00
|
|
|
size_t csize;
|
|
|
|
szind_t i;
|
2016-03-24 12:09:28 +08:00
|
|
|
extent_t *extent;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2015-01-31 13:49:19 +08:00
|
|
|
/*
|
|
|
|
* Round size up to nearest multiple of the cacheline size, so that
|
|
|
|
* there is no chance of false cache line sharing.
|
|
|
|
*/
|
2010-01-17 01:53:50 +08:00
|
|
|
csize = CACHELINE_CEILING(size);
|
|
|
|
|
2016-05-18 05:58:56 +08:00
|
|
|
extent = NULL;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &base_mtx);
|
2016-05-18 05:58:56 +08:00
|
|
|
for (i = size2index(csize); i < NSIZES; i++) {
|
|
|
|
extent = extent_heap_remove_first(&base_avail[i]);
|
|
|
|
if (extent != NULL) {
|
|
|
|
/* Use existing space. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (extent == NULL) {
|
2015-01-31 13:49:19 +08:00
|
|
|
/* Try to allocate more space. */
|
2016-06-02 03:59:02 +08:00
|
|
|
extent = base_extent_alloc(tsdn, csize);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2016-03-24 12:09:28 +08:00
|
|
|
if (extent == 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
|
|
|
ret = NULL;
|
|
|
|
goto label_return;
|
|
|
|
}
|
2015-01-31 13:49:19 +08:00
|
|
|
|
2016-03-24 12:09:28 +08:00
|
|
|
ret = extent_addr_get(extent);
|
|
|
|
if (extent_size_get(extent) > csize) {
|
2016-05-18 05:58:56 +08:00
|
|
|
szind_t index_floor;
|
|
|
|
|
2016-03-24 12:09:28 +08:00
|
|
|
extent_addr_set(extent, (void *)((uintptr_t)ret + csize));
|
|
|
|
extent_size_set(extent, extent_size_get(extent) - csize);
|
2016-05-18 05:58:56 +08:00
|
|
|
/*
|
|
|
|
* Compute the index for the largest size class that does not
|
|
|
|
* exceed extent's size.
|
|
|
|
*/
|
|
|
|
index_floor = size2index(extent_size_get(extent) + 1) - 1;
|
|
|
|
extent_heap_insert(&base_avail[index_floor], extent);
|
2015-01-31 13:49:19 +08:00
|
|
|
} else
|
2016-03-24 12:09:28 +08:00
|
|
|
base_extent_dalloc(tsdn, extent);
|
2015-03-24 08:25:57 +08:00
|
|
|
if (config_stats) {
|
2014-11-28 03:22:36 +08:00
|
|
|
base_allocated += csize;
|
2015-03-24 08:25:57 +08:00
|
|
|
/*
|
|
|
|
* Add one PAGE to base_resident for every page boundary that is
|
|
|
|
* crossed by the new allocation.
|
|
|
|
*/
|
|
|
|
base_resident += PAGE_CEILING((uintptr_t)ret + csize) -
|
|
|
|
PAGE_CEILING((uintptr_t)ret);
|
|
|
|
}
|
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
|
|
|
label_return:
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &base_mtx);
|
2012-02-03 14:04:57 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2015-03-24 08:25:57 +08:00
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
base_stats_get(tsdn_t *tsdn, size_t *allocated, size_t *resident,
|
|
|
|
size_t *mapped)
|
2014-11-28 03:22:36 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_lock(tsdn, &base_mtx);
|
2015-05-29 06:03:58 +08:00
|
|
|
assert(base_allocated <= base_resident);
|
|
|
|
assert(base_resident <= base_mapped);
|
2015-03-24 08:25:57 +08:00
|
|
|
*allocated = base_allocated;
|
|
|
|
*resident = base_resident;
|
|
|
|
*mapped = base_mapped;
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &base_mtx);
|
2014-11-28 03:22:36 +08:00
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
bool
|
|
|
|
base_boot(void)
|
|
|
|
{
|
2016-05-18 05:58:56 +08:00
|
|
|
szind_t i;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2016-04-14 14:36:15 +08:00
|
|
|
if (malloc_mutex_init(&base_mtx, "base", WITNESS_RANK_BASE))
|
2010-01-17 01:53:50 +08:00
|
|
|
return (true);
|
2016-05-18 05:58:56 +08:00
|
|
|
for (i = 0; i < NSIZES; i++)
|
|
|
|
extent_heap_new(&base_avail[i]);
|
2016-03-24 12:09:28 +08:00
|
|
|
base_extents = NULL;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
2012-03-14 07:31:41 +08:00
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
base_prefork(tsdn_t *tsdn)
|
2012-03-14 07:31:41 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_prefork(tsdn, &base_mtx);
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
base_postfork_parent(tsdn_t *tsdn)
|
2012-03-14 07:31:41 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_parent(tsdn, &base_mtx);
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-11 13:21:10 +08:00
|
|
|
base_postfork_child(tsdn_t *tsdn)
|
2012-03-14 07:31:41 +08:00
|
|
|
{
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
malloc_mutex_postfork_child(tsdn, &base_mtx);
|
2012-03-14 07:31:41 +08:00
|
|
|
}
|