2010-01-17 01:53:50 +08:00
|
|
|
#define JEMALLOC_TCACHE_C_
|
2010-02-12 06:45:59 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
2012-02-14 04:29:49 +08:00
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
malloc_tsd_data(, tcache, tcache_t *, NULL)
|
2012-03-27 09:54:44 +08:00
|
|
|
malloc_tsd_data(, tcache_enabled, tcache_enabled_t, tcache_enabled_default)
|
2012-03-22 09:33:03 +08:00
|
|
|
|
2010-03-08 07:34:14 +08:00
|
|
|
bool opt_tcache = true;
|
2010-10-24 09:37:06 +08:00
|
|
|
ssize_t opt_lg_tcache_max = LG_TCACHE_MAXCLASS_DEFAULT;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
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
|
|
|
tcache_bin_info_t *tcache_bin_info;
|
|
|
|
static unsigned stack_nelms; /* Total stack elms per tcache. */
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
size_t nhbins;
|
|
|
|
size_t tcache_maxclass;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2012-04-20 09:28:03 +08:00
|
|
|
size_t tcache_salloc(const void *ptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (arena_salloc(ptr, false));
|
|
|
|
}
|
|
|
|
|
2012-05-02 15:30:36 +08:00
|
|
|
void
|
|
|
|
tcache_event_hard(tcache_t *tcache)
|
|
|
|
{
|
|
|
|
size_t binind = tcache->next_gc_bin;
|
|
|
|
tcache_bin_t *tbin = &tcache->tbins[binind];
|
|
|
|
tcache_bin_info_t *tbin_info = &tcache_bin_info[binind];
|
|
|
|
|
|
|
|
if (tbin->low_water > 0) {
|
|
|
|
/*
|
|
|
|
* Flush (ceiling) 3/4 of the objects below the low water mark.
|
|
|
|
*/
|
|
|
|
if (binind < NBINS) {
|
|
|
|
tcache_bin_flush_small(tbin, binind, tbin->ncached -
|
|
|
|
tbin->low_water + (tbin->low_water >> 2), tcache);
|
|
|
|
} else {
|
|
|
|
tcache_bin_flush_large(tbin, binind, tbin->ncached -
|
|
|
|
tbin->low_water + (tbin->low_water >> 2), tcache);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Reduce fill count by 2X. Limit lg_fill_div such that the
|
|
|
|
* fill count is always at least 1.
|
|
|
|
*/
|
|
|
|
if ((tbin_info->ncached_max >> (tbin->lg_fill_div+1)) >= 1)
|
|
|
|
tbin->lg_fill_div++;
|
|
|
|
} else if (tbin->low_water < 0) {
|
|
|
|
/*
|
|
|
|
* Increase fill count by 2X. Make sure lg_fill_div stays
|
|
|
|
* greater than 0.
|
|
|
|
*/
|
|
|
|
if (tbin->lg_fill_div > 1)
|
|
|
|
tbin->lg_fill_div--;
|
|
|
|
}
|
|
|
|
tbin->low_water = tbin->ncached;
|
|
|
|
|
|
|
|
tcache->next_gc_bin++;
|
|
|
|
if (tcache->next_gc_bin == nhbins)
|
|
|
|
tcache->next_gc_bin = 0;
|
|
|
|
tcache->ev_cnt = 0;
|
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
void *
|
2010-03-18 07:27:39 +08:00
|
|
|
tcache_alloc_small_hard(tcache_t *tcache, tcache_bin_t *tbin, size_t binind)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
arena_tcache_fill_small(tcache->arena, tbin, binind,
|
|
|
|
config_prof ? tcache->prof_accumbytes : 0);
|
|
|
|
if (config_prof)
|
|
|
|
tcache->prof_accumbytes = 0;
|
2010-03-18 07:27:39 +08:00
|
|
|
ret = tcache_alloc_easy(tbin);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-11 12:22:09 +08:00
|
|
|
tcache_bin_flush_small(tcache_bin_t *tbin, size_t binind, unsigned rem,
|
|
|
|
tcache_t *tcache)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
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
|
|
|
void *ptr;
|
2010-03-08 07:34:14 +08:00
|
|
|
unsigned i, nflush, ndeferred;
|
2011-03-15 03:56:51 +08:00
|
|
|
bool merged_stats = false;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
assert(binind < NBINS);
|
2010-03-14 12:32:56 +08:00
|
|
|
assert(rem <= tbin->ncached);
|
|
|
|
|
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
|
|
|
for (nflush = tbin->ncached - rem; nflush > 0; nflush = ndeferred) {
|
2010-03-14 12:32:56 +08:00
|
|
|
/* Lock the arena bin associated with the first object. */
|
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
|
|
|
arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(
|
|
|
|
tbin->avail[0]);
|
2010-03-08 07:34:14 +08:00
|
|
|
arena_t *arena = chunk->arena;
|
2010-03-14 12:32:56 +08:00
|
|
|
arena_bin_t *bin = &arena->bins[binind];
|
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_prof && arena == tcache->arena) {
|
2013-02-07 03:59:30 +08:00
|
|
|
if (arena_prof_accum(arena, tcache->prof_accumbytes))
|
|
|
|
prof_idump();
|
2010-02-12 05:19:21 +08:00
|
|
|
tcache->prof_accumbytes = 0;
|
2010-03-16 13:25:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
malloc_mutex_lock(&bin->lock);
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats && arena == tcache->arena) {
|
2011-03-15 03:56:51 +08:00
|
|
|
assert(merged_stats == false);
|
|
|
|
merged_stats = true;
|
2010-03-14 12:32:56 +08:00
|
|
|
bin->stats.nflushes++;
|
|
|
|
bin->stats.nrequests += tbin->tstats.nrequests;
|
|
|
|
tbin->tstats.nrequests = 0;
|
2010-02-12 05:19:21 +08:00
|
|
|
}
|
2010-03-08 07:34:14 +08:00
|
|
|
ndeferred = 0;
|
|
|
|
for (i = 0; i < nflush; i++) {
|
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
|
|
|
ptr = tbin->avail[i];
|
2010-03-08 07:34:14 +08:00
|
|
|
assert(ptr != NULL);
|
2010-01-17 01:53:50 +08:00
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
|
|
|
if (chunk->arena == arena) {
|
2010-10-02 08:35:43 +08:00
|
|
|
size_t pageind = ((uintptr_t)ptr -
|
2012-04-02 22:04:34 +08:00
|
|
|
(uintptr_t)chunk) >> LG_PAGE;
|
2014-08-30 04:34:40 +08:00
|
|
|
arena_chunk_map_bits_t *bitselm =
|
|
|
|
arena_bitselm_get(chunk, pageind);
|
2012-05-02 15:30:36 +08:00
|
|
|
arena_dalloc_bin_locked(arena, chunk, ptr,
|
2014-08-30 04:34:40 +08:00
|
|
|
bitselm);
|
2010-01-17 01:53:50 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* This object was allocated via a different
|
2010-03-14 12:32:56 +08:00
|
|
|
* arena bin than the one that is currently
|
|
|
|
* locked. Stash the object, so that it can be
|
|
|
|
* handled in a future pass.
|
2010-01-17 01:53:50 +08:00
|
|
|
*/
|
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
|
|
|
tbin->avail[ndeferred] = ptr;
|
2010-01-17 01:53:50 +08:00
|
|
|
ndeferred++;
|
|
|
|
}
|
|
|
|
}
|
2010-03-14 12:32:56 +08:00
|
|
|
malloc_mutex_unlock(&bin->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats && merged_stats == false) {
|
2011-03-15 03:56:51 +08:00
|
|
|
/*
|
|
|
|
* The flush loop didn't happen to flush to this thread's
|
|
|
|
* arena, so the stats didn't get merged. Manually do so now.
|
|
|
|
*/
|
|
|
|
arena_bin_t *bin = &tcache->arena->bins[binind];
|
|
|
|
malloc_mutex_lock(&bin->lock);
|
|
|
|
bin->stats.nflushes++;
|
|
|
|
bin->stats.nrequests += tbin->tstats.nrequests;
|
|
|
|
tbin->tstats.nrequests = 0;
|
|
|
|
malloc_mutex_unlock(&bin->lock);
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
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
|
|
|
memmove(tbin->avail, &tbin->avail[tbin->ncached - rem],
|
|
|
|
rem * sizeof(void *));
|
2010-03-08 07:34:14 +08:00
|
|
|
tbin->ncached = rem;
|
2011-03-21 15:18:17 +08:00
|
|
|
if ((int)tbin->ncached < tbin->low_water)
|
2010-03-14 12:32:56 +08:00
|
|
|
tbin->low_water = tbin->ncached;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2010-03-18 07:27:39 +08:00
|
|
|
void
|
2012-02-11 12:22:09 +08:00
|
|
|
tcache_bin_flush_large(tcache_bin_t *tbin, size_t binind, unsigned rem,
|
|
|
|
tcache_t *tcache)
|
2010-03-18 07:27:39 +08:00
|
|
|
{
|
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
|
|
|
void *ptr;
|
2010-03-18 07:27:39 +08:00
|
|
|
unsigned i, nflush, ndeferred;
|
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
|
|
|
bool merged_stats = false;
|
2010-03-18 07:27:39 +08:00
|
|
|
|
|
|
|
assert(binind < nhbins);
|
|
|
|
assert(rem <= tbin->ncached);
|
|
|
|
|
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
|
|
|
for (nflush = tbin->ncached - rem; nflush > 0; nflush = ndeferred) {
|
2010-03-18 07:27:39 +08:00
|
|
|
/* Lock the arena associated with the first object. */
|
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
|
|
|
arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(
|
|
|
|
tbin->avail[0]);
|
2010-03-18 07:27:39 +08:00
|
|
|
arena_t *arena = chunk->arena;
|
2013-02-07 03:59:30 +08:00
|
|
|
UNUSED bool idump;
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2013-02-07 03:59:30 +08:00
|
|
|
if (config_prof)
|
|
|
|
idump = false;
|
2010-03-18 07:27:39 +08:00
|
|
|
malloc_mutex_lock(&arena->lock);
|
2012-02-11 12:22:09 +08:00
|
|
|
if ((config_prof || config_stats) && arena == tcache->arena) {
|
|
|
|
if (config_prof) {
|
2013-02-07 03:59:30 +08:00
|
|
|
idump = arena_prof_accum_locked(arena,
|
2012-02-11 12:22:09 +08:00
|
|
|
tcache->prof_accumbytes);
|
|
|
|
tcache->prof_accumbytes = 0;
|
|
|
|
}
|
|
|
|
if (config_stats) {
|
|
|
|
merged_stats = true;
|
|
|
|
arena->stats.nrequests_large +=
|
|
|
|
tbin->tstats.nrequests;
|
2012-02-29 08:50:47 +08:00
|
|
|
arena->stats.lstats[binind - NBINS].nrequests +=
|
2012-02-11 12:22:09 +08:00
|
|
|
tbin->tstats.nrequests;
|
|
|
|
tbin->tstats.nrequests = 0;
|
|
|
|
}
|
2010-03-18 07:27:39 +08:00
|
|
|
}
|
|
|
|
ndeferred = 0;
|
|
|
|
for (i = 0; i < nflush; i++) {
|
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
|
|
|
ptr = tbin->avail[i];
|
2010-03-18 07:27:39 +08:00
|
|
|
assert(ptr != NULL);
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
|
|
|
if (chunk->arena == arena)
|
2012-05-02 15:30:36 +08:00
|
|
|
arena_dalloc_large_locked(arena, chunk, ptr);
|
2010-03-18 07:27:39 +08:00
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* This object was allocated via a different
|
|
|
|
* arena than the one that is currently locked.
|
|
|
|
* Stash the object, so that it can be handled
|
|
|
|
* in a future pass.
|
|
|
|
*/
|
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
|
|
|
tbin->avail[ndeferred] = ptr;
|
2010-03-18 07:27:39 +08:00
|
|
|
ndeferred++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
malloc_mutex_unlock(&arena->lock);
|
2013-02-07 03:59:30 +08:00
|
|
|
if (config_prof && idump)
|
|
|
|
prof_idump();
|
2010-03-18 07:27:39 +08:00
|
|
|
}
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats && merged_stats == false) {
|
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
|
|
|
/*
|
|
|
|
* The flush loop didn't happen to flush to this thread's
|
|
|
|
* arena, so the stats didn't get merged. Manually do so now.
|
|
|
|
*/
|
|
|
|
arena_t *arena = tcache->arena;
|
|
|
|
malloc_mutex_lock(&arena->lock);
|
|
|
|
arena->stats.nrequests_large += tbin->tstats.nrequests;
|
2012-02-29 08:50:47 +08:00
|
|
|
arena->stats.lstats[binind - NBINS].nrequests +=
|
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
|
|
|
tbin->tstats.nrequests;
|
|
|
|
tbin->tstats.nrequests = 0;
|
|
|
|
malloc_mutex_unlock(&arena->lock);
|
|
|
|
}
|
2010-03-18 07:27:39 +08:00
|
|
|
|
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
|
|
|
memmove(tbin->avail, &tbin->avail[tbin->ncached - rem],
|
|
|
|
rem * sizeof(void *));
|
2010-03-18 07:27:39 +08:00
|
|
|
tbin->ncached = rem;
|
2011-03-21 15:18:17 +08:00
|
|
|
if ((int)tbin->ncached < tbin->low_water)
|
2010-03-18 07:27:39 +08:00
|
|
|
tbin->low_water = tbin->ncached;
|
|
|
|
}
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
void
|
|
|
|
tcache_arena_associate(tcache_t *tcache, arena_t *arena)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (config_stats) {
|
|
|
|
/* Link into list of extant tcaches. */
|
|
|
|
malloc_mutex_lock(&arena->lock);
|
|
|
|
ql_elm_new(tcache, link);
|
|
|
|
ql_tail_insert(&arena->tcache_ql, tcache, link);
|
|
|
|
malloc_mutex_unlock(&arena->lock);
|
|
|
|
}
|
|
|
|
tcache->arena = arena;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tcache_arena_dissociate(tcache_t *tcache)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (config_stats) {
|
|
|
|
/* Unlink from list of extant tcaches. */
|
|
|
|
malloc_mutex_lock(&tcache->arena->lock);
|
|
|
|
ql_remove(&tcache->arena->tcache_ql, tcache, link);
|
|
|
|
tcache_stats_merge(tcache, tcache->arena);
|
2013-10-22 06:00:06 +08:00
|
|
|
malloc_mutex_unlock(&tcache->arena->lock);
|
2012-03-22 09:33:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 04:28:37 +08:00
|
|
|
tcache_t *
|
|
|
|
tcache_get_hard(tcache_t *tcache, bool create)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (tcache == NULL) {
|
|
|
|
if (create == false) {
|
|
|
|
/*
|
|
|
|
* Creating a tcache here would cause
|
|
|
|
* allocation as a side effect of free().
|
|
|
|
* Ordinarily that would be okay since
|
|
|
|
* tcache_create() failure is a soft failure
|
|
|
|
* that doesn't propagate. However, if TLS
|
|
|
|
* data are freed via free() as in glibc,
|
|
|
|
* subtle corruption could result from setting
|
|
|
|
* a TLS variable after its backing memory is
|
|
|
|
* freed.
|
|
|
|
*/
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (tcache_enabled_get() == false) {
|
|
|
|
tcache_enabled_set(false); /* Memoize. */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (tcache_create(choose_arena(NULL)));
|
|
|
|
}
|
|
|
|
if (tcache == TCACHE_STATE_PURGATORY) {
|
|
|
|
/*
|
|
|
|
* Make a note that an allocator function was called
|
|
|
|
* after tcache_thread_cleanup() was called.
|
|
|
|
*/
|
|
|
|
tcache = TCACHE_STATE_REINCARNATED;
|
|
|
|
tcache_tsd_set(&tcache);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (tcache == TCACHE_STATE_REINCARNATED)
|
|
|
|
return (NULL);
|
|
|
|
not_reached();
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
tcache_t *
|
|
|
|
tcache_create(arena_t *arena)
|
|
|
|
{
|
|
|
|
tcache_t *tcache;
|
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
|
|
|
size_t size, stack_offset;
|
2010-03-08 07:34:14 +08:00
|
|
|
unsigned i;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2010-10-02 09:02:43 +08:00
|
|
|
size = offsetof(tcache_t, tbins) + (sizeof(tcache_bin_t) * nhbins);
|
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
|
|
|
/* Naturally align the pointer stacks. */
|
|
|
|
size = PTR_CEILING(size);
|
|
|
|
stack_offset = size;
|
|
|
|
size += stack_nelms * sizeof(void *);
|
2010-03-08 07:34:14 +08:00
|
|
|
/*
|
|
|
|
* Round up to the nearest multiple of the cacheline size, in order to
|
|
|
|
* avoid the possibility of false cacheline sharing.
|
|
|
|
*
|
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
|
|
|
* That this works relies on the same logic as in ipalloc(), but we
|
|
|
|
* cannot directly call ipalloc() here due to tcache bootstrapping
|
|
|
|
* issues.
|
2010-03-08 07:34:14 +08:00
|
|
|
*/
|
|
|
|
size = (size + CACHELINE_MASK) & (-CACHELINE);
|
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
if (size <= SMALL_MAXCLASS)
|
2010-03-08 07:34:14 +08:00
|
|
|
tcache = (tcache_t *)arena_malloc_small(arena, size, true);
|
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
|
|
|
else if (size <= tcache_maxclass)
|
|
|
|
tcache = (tcache_t *)arena_malloc_large(arena, size, true);
|
2010-03-08 07:34:14 +08:00
|
|
|
else
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
tcache = (tcache_t *)icalloct(size, false, arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
if (tcache == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
tcache_arena_associate(tcache, arena);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2010-03-18 07:27:39 +08:00
|
|
|
assert((TCACHE_NSLOTS_SMALL_MAX & 1U) == 0);
|
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
|
|
|
for (i = 0; i < nhbins; i++) {
|
2011-03-21 15:18:17 +08:00
|
|
|
tcache->tbins[i].lg_fill_div = 1;
|
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
|
|
|
tcache->tbins[i].avail = (void **)((uintptr_t)tcache +
|
|
|
|
(uintptr_t)stack_offset);
|
|
|
|
stack_offset += tcache_bin_info[i].ncached_max * sizeof(void *);
|
2010-03-08 07:34:14 +08:00
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
tcache_tsd_set(&tcache);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
|
|
|
return (tcache);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tcache_destroy(tcache_t *tcache)
|
|
|
|
{
|
|
|
|
unsigned i;
|
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
|
|
|
size_t tcache_size;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
tcache_arena_dissociate(tcache);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-02-29 08:50:47 +08:00
|
|
|
for (i = 0; i < NBINS; i++) {
|
2010-03-08 07:34:14 +08:00
|
|
|
tcache_bin_t *tbin = &tcache->tbins[i];
|
2012-02-11 12:22:09 +08:00
|
|
|
tcache_bin_flush_small(tbin, i, 0, tcache);
|
2010-03-08 07:34:14 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats && tbin->tstats.nrequests != 0) {
|
2010-03-08 07:34:14 +08:00
|
|
|
arena_t *arena = tcache->arena;
|
|
|
|
arena_bin_t *bin = &arena->bins[i];
|
2010-03-14 12:32:56 +08:00
|
|
|
malloc_mutex_lock(&bin->lock);
|
2010-03-08 07:34:14 +08:00
|
|
|
bin->stats.nrequests += tbin->tstats.nrequests;
|
2010-03-14 12:32:56 +08:00
|
|
|
malloc_mutex_unlock(&bin->lock);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 07:27:39 +08:00
|
|
|
for (; i < nhbins; i++) {
|
|
|
|
tcache_bin_t *tbin = &tcache->tbins[i];
|
2012-02-11 12:22:09 +08:00
|
|
|
tcache_bin_flush_large(tbin, i, 0, tcache);
|
2010-03-18 07:27:39 +08:00
|
|
|
|
2012-02-11 12:22:09 +08:00
|
|
|
if (config_stats && tbin->tstats.nrequests != 0) {
|
2010-03-18 07:27:39 +08:00
|
|
|
arena_t *arena = tcache->arena;
|
|
|
|
malloc_mutex_lock(&arena->lock);
|
|
|
|
arena->stats.nrequests_large += tbin->tstats.nrequests;
|
2012-02-29 08:50:47 +08:00
|
|
|
arena->stats.lstats[i - NBINS].nrequests +=
|
2010-03-18 07:27:39 +08:00
|
|
|
tbin->tstats.nrequests;
|
|
|
|
malloc_mutex_unlock(&arena->lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 03:59:30 +08:00
|
|
|
if (config_prof && tcache->prof_accumbytes > 0 &&
|
|
|
|
arena_prof_accum(tcache->arena, tcache->prof_accumbytes))
|
|
|
|
prof_idump();
|
2010-02-12 05:19:21 +08:00
|
|
|
|
2012-04-06 15:35:09 +08:00
|
|
|
tcache_size = arena_salloc(tcache, false);
|
2012-02-29 08:50:47 +08:00
|
|
|
if (tcache_size <= SMALL_MAXCLASS) {
|
2010-01-17 01:53:50 +08:00
|
|
|
arena_chunk_t *chunk = CHUNK_ADDR2BASE(tcache);
|
|
|
|
arena_t *arena = chunk->arena;
|
2010-10-02 08:35:43 +08:00
|
|
|
size_t pageind = ((uintptr_t)tcache - (uintptr_t)chunk) >>
|
2012-04-02 22:04:34 +08:00
|
|
|
LG_PAGE;
|
2014-08-30 04:34:40 +08:00
|
|
|
arena_chunk_map_bits_t *bitselm = arena_bitselm_get(chunk,
|
|
|
|
pageind);
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2014-08-30 04:34:40 +08:00
|
|
|
arena_dalloc_bin(arena, chunk, tcache, pageind, bitselm);
|
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
|
|
|
} else if (tcache_size <= tcache_maxclass) {
|
|
|
|
arena_chunk_t *chunk = CHUNK_ADDR2BASE(tcache);
|
|
|
|
arena_t *arena = chunk->arena;
|
|
|
|
|
|
|
|
arena_dalloc_large(arena, chunk, tcache);
|
2010-01-17 01:53:50 +08:00
|
|
|
} else
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
idalloct(tcache, false);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
void
|
2010-01-17 01:53:50 +08:00
|
|
|
tcache_thread_cleanup(void *arg)
|
|
|
|
{
|
2012-03-22 09:33:03 +08:00
|
|
|
tcache_t *tcache = *(tcache_t **)arg;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-03-27 09:54:44 +08:00
|
|
|
if (tcache == TCACHE_STATE_DISABLED) {
|
|
|
|
/* Do nothing. */
|
|
|
|
} else if (tcache == TCACHE_STATE_REINCARNATED) {
|
2010-09-06 01:35:13 +08:00
|
|
|
/*
|
|
|
|
* Another destructor called an allocator function after this
|
2012-04-07 03:41:55 +08:00
|
|
|
* destructor was called. Reset tcache to
|
|
|
|
* TCACHE_STATE_PURGATORY in order to receive another callback.
|
2010-09-06 01:35:13 +08:00
|
|
|
*/
|
2012-03-27 09:54:44 +08:00
|
|
|
tcache = TCACHE_STATE_PURGATORY;
|
2012-03-22 09:33:03 +08:00
|
|
|
tcache_tsd_set(&tcache);
|
2012-03-27 09:54:44 +08:00
|
|
|
} else if (tcache == TCACHE_STATE_PURGATORY) {
|
|
|
|
/*
|
|
|
|
* The previous time this destructor was called, we set the key
|
2012-04-07 03:41:55 +08:00
|
|
|
* to TCACHE_STATE_PURGATORY so that other destructors wouldn't
|
|
|
|
* cause re-creation of the tcache. This time, do nothing, so
|
|
|
|
* that the destructor will not be called again.
|
2012-03-27 09:54:44 +08:00
|
|
|
*/
|
2010-09-06 01:35:13 +08:00
|
|
|
} else if (tcache != NULL) {
|
2012-03-27 09:54:44 +08:00
|
|
|
assert(tcache != TCACHE_STATE_PURGATORY);
|
2010-01-17 01:53:50 +08:00
|
|
|
tcache_destroy(tcache);
|
2012-03-27 09:54:44 +08:00
|
|
|
tcache = TCACHE_STATE_PURGATORY;
|
2012-03-22 09:33:03 +08:00
|
|
|
tcache_tsd_set(&tcache);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 06:00:06 +08:00
|
|
|
/* Caller must own arena->lock. */
|
2010-01-17 01:53:50 +08:00
|
|
|
void
|
|
|
|
tcache_stats_merge(tcache_t *tcache, arena_t *arena)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2013-10-22 06:00:06 +08:00
|
|
|
cassert(config_stats);
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
/* Merge and reset tcache stats. */
|
2012-02-29 08:50:47 +08:00
|
|
|
for (i = 0; i < NBINS; i++) {
|
2010-01-17 01:53:50 +08:00
|
|
|
arena_bin_t *bin = &arena->bins[i];
|
2010-03-08 07:34:14 +08:00
|
|
|
tcache_bin_t *tbin = &tcache->tbins[i];
|
2010-03-14 12:32:56 +08:00
|
|
|
malloc_mutex_lock(&bin->lock);
|
2010-03-08 07:34:14 +08:00
|
|
|
bin->stats.nrequests += tbin->tstats.nrequests;
|
2010-03-14 12:32:56 +08:00
|
|
|
malloc_mutex_unlock(&bin->lock);
|
2010-03-08 07:34:14 +08:00
|
|
|
tbin->tstats.nrequests = 0;
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
2010-03-18 07:27:39 +08:00
|
|
|
|
|
|
|
for (; i < nhbins; i++) {
|
2012-02-29 08:50:47 +08:00
|
|
|
malloc_large_stats_t *lstats = &arena->stats.lstats[i - NBINS];
|
2010-03-18 07:27:39 +08:00
|
|
|
tcache_bin_t *tbin = &tcache->tbins[i];
|
|
|
|
arena->stats.nrequests_large += tbin->tstats.nrequests;
|
|
|
|
lstats->nrequests += tbin->tstats.nrequests;
|
|
|
|
tbin->tstats.nrequests = 0;
|
|
|
|
}
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
bool
|
2012-03-22 09:33:03 +08:00
|
|
|
tcache_boot0(void)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
2012-04-07 03:41:55 +08:00
|
|
|
unsigned i;
|
2010-01-17 01:53:50 +08:00
|
|
|
|
2012-04-07 03:41:55 +08:00
|
|
|
/*
|
|
|
|
* If necessary, clamp opt_lg_tcache_max, now that arena_maxclass is
|
|
|
|
* known.
|
|
|
|
*/
|
|
|
|
if (opt_lg_tcache_max < 0 || (1U << opt_lg_tcache_max) < SMALL_MAXCLASS)
|
|
|
|
tcache_maxclass = SMALL_MAXCLASS;
|
|
|
|
else if ((1U << opt_lg_tcache_max) > arena_maxclass)
|
|
|
|
tcache_maxclass = arena_maxclass;
|
|
|
|
else
|
|
|
|
tcache_maxclass = (1U << opt_lg_tcache_max);
|
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
|
|
|
|
2012-04-07 03:41:55 +08:00
|
|
|
nhbins = NBINS + (tcache_maxclass >> LG_PAGE);
|
|
|
|
|
|
|
|
/* Initialize tcache_bin_info. */
|
|
|
|
tcache_bin_info = (tcache_bin_info_t *)base_alloc(nhbins *
|
|
|
|
sizeof(tcache_bin_info_t));
|
|
|
|
if (tcache_bin_info == NULL)
|
|
|
|
return (true);
|
|
|
|
stack_nelms = 0;
|
|
|
|
for (i = 0; i < NBINS; i++) {
|
|
|
|
if ((arena_bin_info[i].nregs << 1) <= TCACHE_NSLOTS_SMALL_MAX) {
|
|
|
|
tcache_bin_info[i].ncached_max =
|
|
|
|
(arena_bin_info[i].nregs << 1);
|
|
|
|
} else {
|
|
|
|
tcache_bin_info[i].ncached_max =
|
|
|
|
TCACHE_NSLOTS_SMALL_MAX;
|
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
|
|
|
}
|
2012-04-07 03:41:55 +08:00
|
|
|
stack_nelms += tcache_bin_info[i].ncached_max;
|
|
|
|
}
|
|
|
|
for (; i < nhbins; i++) {
|
|
|
|
tcache_bin_info[i].ncached_max = TCACHE_NSLOTS_LARGE;
|
|
|
|
stack_nelms += tcache_bin_info[i].ncached_max;
|
2012-03-22 09:33:03 +08:00
|
|
|
}
|
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
|
|
|
|
2012-03-22 09:33:03 +08:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
tcache_boot1(void)
|
|
|
|
{
|
|
|
|
|
2012-04-07 03:41:55 +08:00
|
|
|
if (tcache_tsd_boot() || tcache_enabled_tsd_boot())
|
|
|
|
return (true);
|
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
|
|
|
|
|
|
|
return (false);
|
2010-01-17 01:53:50 +08:00
|
|
|
}
|