2017-04-11 09:17:55 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
|
|
|
#include "jemalloc/internal/jemalloc_internal_includes.h"
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2017-04-12 05:43:12 +08:00
|
|
|
#include "jemalloc/internal/assert.h"
|
2017-05-24 03:28:19 +08:00
|
|
|
#include "jemalloc/internal/mutex.h"
|
2017-04-12 05:43:12 +08:00
|
|
|
|
2016-03-28 18:06:35 +08:00
|
|
|
/*
|
|
|
|
* Only the most significant bits of keys passed to rtree_{read,write}() are
|
|
|
|
* used.
|
|
|
|
*/
|
2015-01-31 14:54:08 +08:00
|
|
|
bool
|
2020-02-18 05:11:10 +08:00
|
|
|
rtree_new(rtree_t *rtree, base_t *base, bool zeroed) {
|
2017-03-23 07:38:03 +08:00
|
|
|
#ifdef JEMALLOC_JET
|
|
|
|
if (!zeroed) {
|
|
|
|
memset(rtree, 0, sizeof(rtree_t)); /* Clear root. */
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
assert(zeroed);
|
|
|
|
#endif
|
2020-02-18 05:11:10 +08:00
|
|
|
rtree->base = base;
|
2017-03-23 07:38:03 +08:00
|
|
|
|
2017-05-16 06:38:15 +08:00
|
|
|
if (malloc_mutex_init(&rtree->init_lock, "rtree", WITNESS_RANK_RTREE,
|
|
|
|
malloc_mutex_rank_exclusive)) {
|
2017-02-07 05:17:12 +08:00
|
|
|
return true;
|
2015-01-31 14:54:08 +08:00
|
|
|
}
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2010-09-06 01:35:13 +08:00
|
|
|
}
|
2012-10-10 05:46:22 +08:00
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static rtree_node_elm_t *
|
2020-02-18 05:11:10 +08:00
|
|
|
rtree_node_alloc(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {
|
|
|
|
return (rtree_node_elm_t *)base_alloc(tsdn, rtree->base,
|
|
|
|
nelms * sizeof(rtree_node_elm_t), CACHELINE);
|
2016-04-16 15:36:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static rtree_leaf_elm_t *
|
2020-02-18 05:11:10 +08:00
|
|
|
rtree_leaf_alloc(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {
|
|
|
|
return (rtree_leaf_elm_t *)base_alloc(tsdn, rtree->base,
|
|
|
|
nelms * sizeof(rtree_leaf_elm_t), CACHELINE);
|
2017-03-17 00:46:42 +08:00
|
|
|
}
|
2014-01-03 08:08:28 +08:00
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static rtree_node_elm_t *
|
2016-04-16 15:36:11 +08:00
|
|
|
rtree_node_init(tsdn_t *tsdn, rtree_t *rtree, unsigned level,
|
2017-03-10 06:49:32 +08:00
|
|
|
atomic_p_t *elmp) {
|
2016-11-01 07:23:33 +08:00
|
|
|
malloc_mutex_lock(tsdn, &rtree->init_lock);
|
2017-03-10 06:49:32 +08:00
|
|
|
/*
|
|
|
|
* If *elmp is non-null, then it was initialized with the init lock
|
|
|
|
* held, so we can get by with 'relaxed' here.
|
|
|
|
*/
|
2017-03-17 00:46:42 +08:00
|
|
|
rtree_node_elm_t *node = atomic_load_p(elmp, ATOMIC_RELAXED);
|
2016-11-01 07:23:33 +08:00
|
|
|
if (node == NULL) {
|
2016-04-16 15:36:11 +08:00
|
|
|
node = rtree_node_alloc(tsdn, rtree, ZU(1) <<
|
2017-02-07 05:17:12 +08:00
|
|
|
rtree_levels[level].bits);
|
2016-11-01 07:23:33 +08:00
|
|
|
if (node == NULL) {
|
|
|
|
malloc_mutex_unlock(tsdn, &rtree->init_lock);
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2016-11-01 07:23:33 +08:00
|
|
|
}
|
2017-03-10 06:49:32 +08:00
|
|
|
/*
|
|
|
|
* Even though we hold the lock, a later reader might not; we
|
|
|
|
* need release semantics.
|
|
|
|
*/
|
|
|
|
atomic_store_p(elmp, node, ATOMIC_RELEASE);
|
2015-01-31 14:54:08 +08:00
|
|
|
}
|
2016-11-01 07:23:33 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &rtree->init_lock);
|
2012-10-10 05:46:22 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return node;
|
2012-10-10 05:46:22 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static rtree_leaf_elm_t *
|
|
|
|
rtree_leaf_init(tsdn_t *tsdn, rtree_t *rtree, atomic_p_t *elmp) {
|
|
|
|
malloc_mutex_lock(tsdn, &rtree->init_lock);
|
|
|
|
/*
|
|
|
|
* If *elmp is non-null, then it was initialized with the init lock
|
|
|
|
* held, so we can get by with 'relaxed' here.
|
|
|
|
*/
|
|
|
|
rtree_leaf_elm_t *leaf = atomic_load_p(elmp, ATOMIC_RELAXED);
|
|
|
|
if (leaf == NULL) {
|
|
|
|
leaf = rtree_leaf_alloc(tsdn, rtree, ZU(1) <<
|
|
|
|
rtree_levels[RTREE_HEIGHT-1].bits);
|
|
|
|
if (leaf == NULL) {
|
|
|
|
malloc_mutex_unlock(tsdn, &rtree->init_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Even though we hold the lock, a later reader might not; we
|
|
|
|
* need release semantics.
|
|
|
|
*/
|
|
|
|
atomic_store_p(elmp, leaf, ATOMIC_RELEASE);
|
|
|
|
}
|
|
|
|
malloc_mutex_unlock(tsdn, &rtree->init_lock);
|
|
|
|
|
|
|
|
return leaf;
|
|
|
|
}
|
|
|
|
|
2017-02-04 11:44:33 +08:00
|
|
|
static bool
|
2017-03-17 00:46:42 +08:00
|
|
|
rtree_node_valid(rtree_node_elm_t *node) {
|
2017-02-04 11:44:33 +08:00
|
|
|
return ((uintptr_t)node != (uintptr_t)0);
|
|
|
|
}
|
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static bool
|
|
|
|
rtree_leaf_valid(rtree_leaf_elm_t *leaf) {
|
|
|
|
return ((uintptr_t)leaf != (uintptr_t)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static rtree_node_elm_t *
|
|
|
|
rtree_child_node_tryread(rtree_node_elm_t *elm, bool dependent) {
|
|
|
|
rtree_node_elm_t *node;
|
2017-02-04 11:44:33 +08:00
|
|
|
|
2017-03-10 06:49:32 +08:00
|
|
|
if (dependent) {
|
2017-03-17 00:46:42 +08:00
|
|
|
node = (rtree_node_elm_t *)atomic_load_p(&elm->child,
|
2017-03-10 06:49:32 +08:00
|
|
|
ATOMIC_RELAXED);
|
|
|
|
} else {
|
2017-03-17 00:46:42 +08:00
|
|
|
node = (rtree_node_elm_t *)atomic_load_p(&elm->child,
|
2017-03-10 06:49:32 +08:00
|
|
|
ATOMIC_ACQUIRE);
|
2017-02-04 11:44:33 +08:00
|
|
|
}
|
2017-03-10 06:49:32 +08:00
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
assert(!dependent || node != NULL);
|
|
|
|
return node;
|
2017-02-04 11:44:33 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static rtree_node_elm_t *
|
|
|
|
rtree_child_node_read(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *elm,
|
|
|
|
unsigned level, bool dependent) {
|
|
|
|
rtree_node_elm_t *node;
|
2017-02-04 11:44:33 +08:00
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
node = rtree_child_node_tryread(elm, dependent);
|
|
|
|
if (!dependent && unlikely(!rtree_node_valid(node))) {
|
|
|
|
node = rtree_node_init(tsdn, rtree, level + 1, &elm->child);
|
2017-02-04 11:44:33 +08:00
|
|
|
}
|
2017-03-17 00:46:42 +08:00
|
|
|
assert(!dependent || node != NULL);
|
|
|
|
return node;
|
2017-02-04 11:44:33 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static rtree_leaf_elm_t *
|
|
|
|
rtree_child_leaf_tryread(rtree_node_elm_t *elm, bool dependent) {
|
|
|
|
rtree_leaf_elm_t *leaf;
|
|
|
|
|
2017-03-10 06:49:32 +08:00
|
|
|
if (dependent) {
|
2017-03-17 00:46:42 +08:00
|
|
|
leaf = (rtree_leaf_elm_t *)atomic_load_p(&elm->child,
|
2017-03-10 06:49:32 +08:00
|
|
|
ATOMIC_RELAXED);
|
|
|
|
} else {
|
2017-03-17 00:46:42 +08:00
|
|
|
leaf = (rtree_leaf_elm_t *)atomic_load_p(&elm->child,
|
2017-03-10 06:49:32 +08:00
|
|
|
ATOMIC_ACQUIRE);
|
2017-02-04 11:44:33 +08:00
|
|
|
}
|
2017-03-17 00:46:42 +08:00
|
|
|
|
|
|
|
assert(!dependent || leaf != NULL);
|
|
|
|
return leaf;
|
2017-02-04 11:44:33 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static rtree_leaf_elm_t *
|
|
|
|
rtree_child_leaf_read(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *elm,
|
|
|
|
unsigned level, bool dependent) {
|
|
|
|
rtree_leaf_elm_t *leaf;
|
|
|
|
|
|
|
|
leaf = rtree_child_leaf_tryread(elm, dependent);
|
|
|
|
if (!dependent && unlikely(!rtree_leaf_valid(leaf))) {
|
|
|
|
leaf = rtree_leaf_init(tsdn, rtree, &elm->child);
|
2017-02-04 11:44:33 +08:00
|
|
|
}
|
2017-03-17 00:46:42 +08:00
|
|
|
assert(!dependent || leaf != NULL);
|
|
|
|
return leaf;
|
2012-10-10 05:46:22 +08:00
|
|
|
}
|
2016-04-18 03:55:10 +08:00
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
rtree_leaf_elm_t *
|
|
|
|
rtree_leaf_elm_lookup_hard(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
|
2017-02-04 12:12:49 +08:00
|
|
|
uintptr_t key, bool dependent, bool init_missing) {
|
2017-03-17 00:46:42 +08:00
|
|
|
rtree_node_elm_t *node;
|
|
|
|
rtree_leaf_elm_t *leaf;
|
|
|
|
#if RTREE_HEIGHT > 1
|
2017-03-23 07:38:03 +08:00
|
|
|
node = rtree->root;
|
2017-03-17 00:46:42 +08:00
|
|
|
#else
|
2017-03-23 07:38:03 +08:00
|
|
|
leaf = rtree->root;
|
2017-03-17 00:46:42 +08:00
|
|
|
#endif
|
2017-02-04 12:12:49 +08:00
|
|
|
|
2017-04-15 02:05:38 +08:00
|
|
|
if (config_debug) {
|
|
|
|
uintptr_t leafkey = rtree_leafkey(key);
|
|
|
|
for (unsigned i = 0; i < RTREE_CTX_NCACHE; i++) {
|
|
|
|
assert(rtree_ctx->cache[i].leafkey != leafkey);
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < RTREE_CTX_NCACHE_L2; i++) {
|
|
|
|
assert(rtree_ctx->l2_cache[i].leafkey != leafkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
#define RTREE_GET_CHILD(level) { \
|
2017-02-07 05:17:12 +08:00
|
|
|
assert(level < RTREE_HEIGHT-1); \
|
2017-03-23 07:38:03 +08:00
|
|
|
if (level != 0 && !dependent && \
|
|
|
|
unlikely(!rtree_node_valid(node))) { \
|
2017-02-04 12:12:49 +08:00
|
|
|
return NULL; \
|
|
|
|
} \
|
2017-02-07 05:17:12 +08:00
|
|
|
uintptr_t subkey = rtree_subkey(key, level); \
|
2017-03-17 00:46:42 +08:00
|
|
|
if (level + 2 < RTREE_HEIGHT) { \
|
|
|
|
node = init_missing ? \
|
|
|
|
rtree_child_node_read(tsdn, rtree, \
|
|
|
|
&node[subkey], level, dependent) : \
|
|
|
|
rtree_child_node_tryread(&node[subkey], \
|
|
|
|
dependent); \
|
|
|
|
} else { \
|
|
|
|
leaf = init_missing ? \
|
|
|
|
rtree_child_leaf_read(tsdn, rtree, \
|
|
|
|
&node[subkey], level, dependent) : \
|
|
|
|
rtree_child_leaf_tryread(&node[subkey], \
|
|
|
|
dependent); \
|
|
|
|
} \
|
2017-02-04 12:12:49 +08:00
|
|
|
}
|
2017-04-15 02:05:38 +08:00
|
|
|
/*
|
|
|
|
* Cache replacement upon hard lookup (i.e. L1 & L2 rtree cache miss):
|
|
|
|
* (1) evict last entry in L2 cache; (2) move the collision slot from L1
|
|
|
|
* cache down to L2; and 3) fill L1.
|
|
|
|
*/
|
2017-02-07 05:17:12 +08:00
|
|
|
#define RTREE_GET_LEAF(level) { \
|
|
|
|
assert(level == RTREE_HEIGHT-1); \
|
2017-03-17 00:46:42 +08:00
|
|
|
if (!dependent && unlikely(!rtree_leaf_valid(leaf))) { \
|
2017-02-04 12:12:49 +08:00
|
|
|
return NULL; \
|
|
|
|
} \
|
2017-04-15 02:05:38 +08:00
|
|
|
if (RTREE_CTX_NCACHE_L2 > 1) { \
|
|
|
|
memmove(&rtree_ctx->l2_cache[1], \
|
|
|
|
&rtree_ctx->l2_cache[0], \
|
2017-02-05 18:50:59 +08:00
|
|
|
sizeof(rtree_ctx_cache_elm_t) * \
|
2017-04-15 02:05:38 +08:00
|
|
|
(RTREE_CTX_NCACHE_L2 - 1)); \
|
2017-02-04 12:12:49 +08:00
|
|
|
} \
|
2017-04-15 02:05:38 +08:00
|
|
|
size_t slot = rtree_cache_direct_map(key); \
|
|
|
|
rtree_ctx->l2_cache[0].leafkey = \
|
|
|
|
rtree_ctx->cache[slot].leafkey; \
|
|
|
|
rtree_ctx->l2_cache[0].leaf = \
|
|
|
|
rtree_ctx->cache[slot].leaf; \
|
2017-02-05 18:50:59 +08:00
|
|
|
uintptr_t leafkey = rtree_leafkey(key); \
|
2017-04-15 02:05:38 +08:00
|
|
|
rtree_ctx->cache[slot].leafkey = leafkey; \
|
|
|
|
rtree_ctx->cache[slot].leaf = leaf; \
|
2017-02-07 05:17:12 +08:00
|
|
|
uintptr_t subkey = rtree_subkey(key, level); \
|
2017-03-17 00:46:42 +08:00
|
|
|
return &leaf[subkey]; \
|
2017-02-04 12:12:49 +08:00
|
|
|
}
|
2017-02-07 05:17:12 +08:00
|
|
|
if (RTREE_HEIGHT > 1) {
|
2017-03-17 00:46:42 +08:00
|
|
|
RTREE_GET_CHILD(0)
|
2017-02-07 05:17:12 +08:00
|
|
|
}
|
|
|
|
if (RTREE_HEIGHT > 2) {
|
2017-03-17 00:46:42 +08:00
|
|
|
RTREE_GET_CHILD(1)
|
2017-02-07 05:17:12 +08:00
|
|
|
}
|
|
|
|
if (RTREE_HEIGHT > 3) {
|
|
|
|
for (unsigned i = 2; i < RTREE_HEIGHT-1; i++) {
|
2017-03-17 00:46:42 +08:00
|
|
|
RTREE_GET_CHILD(i)
|
2017-02-07 05:17:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RTREE_GET_LEAF(RTREE_HEIGHT-1)
|
2017-03-17 00:46:42 +08:00
|
|
|
#undef RTREE_GET_CHILD
|
2017-02-04 12:12:49 +08:00
|
|
|
#undef RTREE_GET_LEAF
|
|
|
|
not_reached();
|
|
|
|
}
|
|
|
|
|
2017-04-05 02:09:45 +08:00
|
|
|
void
|
2017-03-30 04:18:02 +08:00
|
|
|
rtree_ctx_data_init(rtree_ctx_t *ctx) {
|
|
|
|
for (unsigned i = 0; i < RTREE_CTX_NCACHE; i++) {
|
|
|
|
rtree_ctx_cache_elm_t *cache = &ctx->cache[i];
|
|
|
|
cache->leafkey = RTREE_LEAFKEY_INVALID;
|
|
|
|
cache->leaf = NULL;
|
|
|
|
}
|
2017-04-15 02:05:38 +08:00
|
|
|
for (unsigned i = 0; i < RTREE_CTX_NCACHE_L2; i++) {
|
|
|
|
rtree_ctx_cache_elm_t *cache = &ctx->l2_cache[i];
|
|
|
|
cache->leafkey = RTREE_LEAFKEY_INVALID;
|
|
|
|
cache->leaf = NULL;
|
|
|
|
}
|
2017-04-05 02:09:45 +08:00
|
|
|
}
|