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"
|
2010-01-17 01:53:50 +08:00
|
|
|
#ifdef JEMALLOC_TCACHE
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Data. */
|
|
|
|
|
|
|
|
size_t opt_lg_tcache_nslots = LG_TCACHE_NSLOTS_DEFAULT;
|
|
|
|
ssize_t opt_lg_tcache_gc_sweep = LG_TCACHE_GC_SWEEP_DEFAULT;
|
|
|
|
|
|
|
|
/* Map of thread-specific caches. */
|
|
|
|
__thread tcache_t *tcache_tls JEMALLOC_ATTR(tls_model("initial-exec"));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same contents as tcache, but initialized such that the TSD destructor is
|
|
|
|
* called when a thread exits, so that the cache can be cleaned up.
|
|
|
|
*/
|
|
|
|
static pthread_key_t tcache_tsd;
|
|
|
|
|
|
|
|
size_t tcache_nslots;
|
|
|
|
unsigned tcache_gc_incr;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Function prototypes for non-inline static functions. */
|
|
|
|
|
|
|
|
static void tcache_thread_cleanup(void *arg);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
void *
|
|
|
|
tcache_alloc_hard(tcache_t *tcache, tcache_bin_t *tbin, size_t binind)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
2010-02-12 05:19:21 +08:00
|
|
|
arena_tcache_fill(tcache->arena, tbin, binind
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
, tcache->prof_accumbytes
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
tcache->prof_accumbytes = 0;
|
|
|
|
#endif
|
2010-01-17 01:53:50 +08:00
|
|
|
ret = tcache_bin_alloc(tbin);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-12 05:19:21 +08:00
|
|
|
tcache_bin_flush(tcache_bin_t *tbin, size_t binind, unsigned rem
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
, tcache_t *tcache
|
|
|
|
#endif
|
|
|
|
)
|
2010-01-17 01:53:50 +08:00
|
|
|
{
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
arena_t *arena;
|
|
|
|
void *ptr;
|
|
|
|
unsigned i, ndeferred, ncached;
|
|
|
|
|
|
|
|
for (ndeferred = tbin->ncached - rem; ndeferred > 0;) {
|
|
|
|
ncached = ndeferred;
|
|
|
|
/* Lock the arena associated with the first object. */
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(tbin->slots[0]);
|
|
|
|
arena = chunk->arena;
|
|
|
|
malloc_mutex_lock(&arena->lock);
|
2010-02-12 05:19:21 +08:00
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
if (arena == tcache->arena) {
|
|
|
|
arena_prof_accum(arena, tcache->prof_accumbytes);
|
|
|
|
tcache->prof_accumbytes = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-17 01:53:50 +08:00
|
|
|
/* Deallocate every object that belongs to the locked arena. */
|
|
|
|
for (i = ndeferred = 0; i < ncached; i++) {
|
|
|
|
ptr = tbin->slots[i];
|
|
|
|
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
|
|
|
|
if (chunk->arena == arena) {
|
|
|
|
size_t pageind = (((uintptr_t)ptr -
|
|
|
|
(uintptr_t)chunk) >> PAGE_SHIFT);
|
|
|
|
arena_chunk_map_t *mapelm =
|
|
|
|
&chunk->map[pageind];
|
|
|
|
arena_dalloc_bin(arena, chunk, ptr, mapelm);
|
|
|
|
} 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.
|
|
|
|
*/
|
|
|
|
tbin->slots[ndeferred] = ptr;
|
|
|
|
ndeferred++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef JEMALLOC_STATS
|
|
|
|
arena->bins[binind].stats.nflushes++;
|
|
|
|
{
|
|
|
|
arena_bin_t *bin = &arena->bins[binind];
|
|
|
|
bin->stats.nrequests += tbin->tstats.nrequests;
|
|
|
|
if (bin->reg_size <= small_maxclass) {
|
|
|
|
arena->stats.nmalloc_small +=
|
|
|
|
tbin->tstats.nrequests;
|
|
|
|
} else {
|
|
|
|
arena->stats.nmalloc_medium +=
|
|
|
|
tbin->tstats.nrequests;
|
|
|
|
}
|
|
|
|
tbin->tstats.nrequests = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
malloc_mutex_unlock(&arena->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rem > 0) {
|
|
|
|
/*
|
|
|
|
* Shift the remaining valid pointers to the base of the slots
|
|
|
|
* array.
|
|
|
|
*/
|
|
|
|
memmove(&tbin->slots[0], &tbin->slots[tbin->ncached - rem],
|
|
|
|
rem * sizeof(void *));
|
|
|
|
}
|
|
|
|
tbin->ncached = rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcache_bin_t *
|
|
|
|
tcache_bin_create(arena_t *arena)
|
|
|
|
{
|
|
|
|
tcache_bin_t *ret;
|
|
|
|
size_t tsize;
|
|
|
|
|
|
|
|
tsize = sizeof(tcache_bin_t) + (sizeof(void *) * (tcache_nslots - 1));
|
|
|
|
if (tsize <= small_maxclass)
|
|
|
|
ret = (tcache_bin_t *)arena_malloc_small(arena, tsize, false);
|
|
|
|
else if (tsize <= bin_maxclass)
|
|
|
|
ret = (tcache_bin_t *)arena_malloc_medium(arena, tsize, false);
|
|
|
|
else
|
|
|
|
ret = (tcache_bin_t *)imalloc(tsize);
|
|
|
|
if (ret == NULL)
|
|
|
|
return (NULL);
|
|
|
|
#ifdef JEMALLOC_STATS
|
|
|
|
memset(&ret->tstats, 0, sizeof(tcache_bin_stats_t));
|
|
|
|
#endif
|
|
|
|
ret->low_water = 0;
|
|
|
|
ret->high_water = 0;
|
|
|
|
ret->ncached = 0;
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tcache_bin_destroy(tcache_t *tcache, tcache_bin_t *tbin, unsigned binind)
|
|
|
|
{
|
|
|
|
arena_t *arena;
|
|
|
|
arena_chunk_t *chunk;
|
|
|
|
size_t pageind, tsize;
|
|
|
|
arena_chunk_map_t *mapelm;
|
|
|
|
|
|
|
|
chunk = CHUNK_ADDR2BASE(tbin);
|
|
|
|
arena = chunk->arena;
|
|
|
|
pageind = (((uintptr_t)tbin - (uintptr_t)chunk) >> PAGE_SHIFT);
|
|
|
|
mapelm = &chunk->map[pageind];
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_STATS
|
|
|
|
if (tbin->tstats.nrequests != 0) {
|
|
|
|
arena_t *arena = tcache->arena;
|
|
|
|
arena_bin_t *bin = &arena->bins[binind];
|
|
|
|
malloc_mutex_lock(&arena->lock);
|
|
|
|
bin->stats.nrequests += tbin->tstats.nrequests;
|
|
|
|
if (bin->reg_size <= small_maxclass)
|
|
|
|
arena->stats.nmalloc_small += tbin->tstats.nrequests;
|
|
|
|
else
|
|
|
|
arena->stats.nmalloc_medium += tbin->tstats.nrequests;
|
|
|
|
malloc_mutex_unlock(&arena->lock);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(tbin->ncached == 0);
|
|
|
|
tsize = sizeof(tcache_bin_t) + (sizeof(void *) * (tcache_nslots - 1));
|
|
|
|
if (tsize <= bin_maxclass) {
|
|
|
|
malloc_mutex_lock(&arena->lock);
|
|
|
|
arena_dalloc_bin(arena, chunk, tbin, mapelm);
|
|
|
|
malloc_mutex_unlock(&arena->lock);
|
|
|
|
} else
|
|
|
|
idalloc(tbin);
|
|
|
|
}
|
|
|
|
|
|
|
|
tcache_t *
|
|
|
|
tcache_create(arena_t *arena)
|
|
|
|
{
|
|
|
|
tcache_t *tcache;
|
|
|
|
|
|
|
|
if (sizeof(tcache_t) + (sizeof(tcache_bin_t *) * (nbins - 1)) <=
|
|
|
|
small_maxclass) {
|
|
|
|
tcache = (tcache_t *)arena_malloc_small(arena, sizeof(tcache_t)
|
|
|
|
+ (sizeof(tcache_bin_t *) * (nbins - 1)), true);
|
|
|
|
} else if (sizeof(tcache_t) + (sizeof(tcache_bin_t *) * (nbins - 1)) <=
|
|
|
|
bin_maxclass) {
|
|
|
|
tcache = (tcache_t *)arena_malloc_medium(arena, sizeof(tcache_t)
|
|
|
|
+ (sizeof(tcache_bin_t *) * (nbins - 1)), true);
|
|
|
|
} else {
|
|
|
|
tcache = (tcache_t *)icalloc(sizeof(tcache_t) +
|
|
|
|
(sizeof(tcache_bin_t *) * (nbins - 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tcache == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_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);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tcache->arena = arena;
|
|
|
|
|
|
|
|
tcache_tls = tcache;
|
|
|
|
pthread_setspecific(tcache_tsd, tcache);
|
|
|
|
|
|
|
|
return (tcache);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tcache_destroy(tcache_t *tcache)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_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);
|
|
|
|
malloc_mutex_unlock(&tcache->arena->lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (i = 0; i < nbins; i++) {
|
|
|
|
tcache_bin_t *tbin = tcache->tbins[i];
|
|
|
|
if (tbin != NULL) {
|
2010-02-12 05:19:21 +08:00
|
|
|
tcache_bin_flush(tbin, i, 0
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
, tcache
|
|
|
|
#endif
|
|
|
|
);
|
2010-01-17 01:53:50 +08:00
|
|
|
tcache_bin_destroy(tcache, tbin, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 05:19:21 +08:00
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
if (tcache->prof_accumbytes > 0) {
|
|
|
|
malloc_mutex_lock(&tcache->arena->lock);
|
|
|
|
arena_prof_accum(tcache->arena, tcache->prof_accumbytes);
|
|
|
|
malloc_mutex_unlock(&tcache->arena->lock);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-01-17 01:53:50 +08:00
|
|
|
if (arena_salloc(tcache) <= bin_maxclass) {
|
|
|
|
arena_chunk_t *chunk = CHUNK_ADDR2BASE(tcache);
|
|
|
|
arena_t *arena = chunk->arena;
|
|
|
|
size_t pageind = (((uintptr_t)tcache - (uintptr_t)chunk) >>
|
|
|
|
PAGE_SHIFT);
|
|
|
|
arena_chunk_map_t *mapelm = &chunk->map[pageind];
|
|
|
|
|
|
|
|
malloc_mutex_lock(&arena->lock);
|
|
|
|
arena_dalloc_bin(arena, chunk, tcache, mapelm);
|
|
|
|
malloc_mutex_unlock(&arena->lock);
|
|
|
|
} else
|
|
|
|
idalloc(tcache);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tcache_thread_cleanup(void *arg)
|
|
|
|
{
|
|
|
|
tcache_t *tcache = (tcache_t *)arg;
|
|
|
|
|
|
|
|
assert(tcache == tcache_tls);
|
|
|
|
if (tcache != NULL) {
|
|
|
|
assert(tcache != (void *)(uintptr_t)1);
|
|
|
|
tcache_destroy(tcache);
|
|
|
|
tcache_tls = (void *)(uintptr_t)1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_STATS
|
|
|
|
void
|
|
|
|
tcache_stats_merge(tcache_t *tcache, arena_t *arena)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/* Merge and reset tcache stats. */
|
|
|
|
for (i = 0; i < mbin0; i++) {
|
|
|
|
arena_bin_t *bin = &arena->bins[i];
|
|
|
|
tcache_bin_t *tbin = tcache->tbins[i];
|
|
|
|
if (tbin != NULL) {
|
|
|
|
bin->stats.nrequests += tbin->tstats.nrequests;
|
|
|
|
arena->stats.nmalloc_small += tbin->tstats.nrequests;
|
|
|
|
tbin->tstats.nrequests = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; i < nbins; i++) {
|
|
|
|
arena_bin_t *bin = &arena->bins[i];
|
|
|
|
tcache_bin_t *tbin = tcache->tbins[i];
|
|
|
|
if (tbin != NULL) {
|
|
|
|
bin->stats.nrequests += tbin->tstats.nrequests;
|
|
|
|
arena->stats.nmalloc_medium += tbin->tstats.nrequests;
|
|
|
|
tbin->tstats.nrequests = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
tcache_boot(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (opt_lg_tcache_nslots > 0) {
|
|
|
|
tcache_nslots = (1U << opt_lg_tcache_nslots);
|
|
|
|
|
|
|
|
/* Compute incremental GC event threshold. */
|
|
|
|
if (opt_lg_tcache_gc_sweep >= 0) {
|
|
|
|
tcache_gc_incr = ((1U << opt_lg_tcache_gc_sweep) /
|
|
|
|
nbins) + (((1U << opt_lg_tcache_gc_sweep) % nbins ==
|
|
|
|
0) ? 0 : 1);
|
|
|
|
} else
|
|
|
|
tcache_gc_incr = 0;
|
|
|
|
} else
|
|
|
|
tcache_nslots = 0;
|
|
|
|
|
|
|
|
if (tcache_nslots != 0) {
|
|
|
|
if (pthread_key_create(&tcache_tsd, tcache_thread_cleanup) !=
|
|
|
|
0) {
|
|
|
|
malloc_write4("<jemalloc>",
|
|
|
|
": Error in pthread_key_create()\n", "", "");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/******************************************************************************/
|
|
|
|
#endif /* JEMALLOC_TCACHE */
|