Rtree: Clean up compact/non-compact split.

This commit is contained in:
David Goldblatt 2020-03-15 15:49:42 -07:00 committed by David Goldblatt
parent 93b99dd140
commit dc26b30094
3 changed files with 83 additions and 156 deletions

View File

@ -43,13 +43,18 @@ struct rtree_node_elm_s {
atomic_p_t child; /* (rtree_{node,leaf}_elm_t *) */ atomic_p_t child; /* (rtree_{node,leaf}_elm_t *) */
}; };
typedef struct rtree_leaf_elm_contents_s rtree_leaf_elm_contents_t; typedef struct rtree_metadata_s rtree_metadata_t;
struct rtree_leaf_elm_contents_s { struct rtree_metadata_s {
edata_t *edata;
szind_t szind; szind_t szind;
bool slab; bool slab;
}; };
typedef struct rtree_contents_s rtree_contents_t;
struct rtree_contents_s {
edata_t *edata;
rtree_metadata_t metadata;
};
struct rtree_leaf_elm_s { struct rtree_leaf_elm_s {
#ifdef RTREE_LEAF_COMPACT #ifdef RTREE_LEAF_COMPACT
/* /*
@ -67,8 +72,11 @@ struct rtree_leaf_elm_s {
atomic_p_t le_bits; atomic_p_t le_bits;
#else #else
atomic_p_t le_edata; /* (edata_t *) */ atomic_p_t le_edata; /* (edata_t *) */
atomic_u_t le_szind; /* (szind_t) */ /*
atomic_b_t le_slab; /* (bool) */ * slab is stored in the low bit; szind is stored in the next lowest
* bits.
*/
atomic_u_t le_metadata;
#endif #endif
}; };
@ -171,25 +179,25 @@ rtree_leaf_elm_bits_read(tsdn_t *tsdn, rtree_t *rtree,
} }
JEMALLOC_ALWAYS_INLINE uintptr_t JEMALLOC_ALWAYS_INLINE uintptr_t
rtree_leaf_elm_bits_encode(rtree_leaf_elm_contents_t contents) { rtree_leaf_elm_bits_encode(rtree_contents_t contents) {
uintptr_t edata_bits = (uintptr_t)contents.edata uintptr_t edata_bits = (uintptr_t)contents.edata
& (((uintptr_t)1 << LG_VADDR) - 1); & (((uintptr_t)1 << LG_VADDR) - 1);
uintptr_t szind_bits = (uintptr_t)contents.szind << LG_VADDR; uintptr_t szind_bits = (uintptr_t)contents.metadata.szind << LG_VADDR;
/* /*
* Slab shares the low bit of edata; we know edata is on an even address * Slab shares the low bit of edata; we know edata is on an even address
* (in fact, it's 128 bytes on 64-bit systems; we can enforce this * (in fact, it's 128 bytes on 64-bit systems; we can enforce this
* alignment if we want to steal 6 extra rtree leaf bits someday. * alignment if we want to steal 6 extra rtree leaf bits someday.
*/ */
uintptr_t slab_bits = (uintptr_t)contents.slab; uintptr_t slab_bits = (uintptr_t)contents.metadata.slab;
return szind_bits | edata_bits | slab_bits; return szind_bits | edata_bits | slab_bits;
} }
JEMALLOC_ALWAYS_INLINE rtree_leaf_elm_contents_t JEMALLOC_ALWAYS_INLINE rtree_contents_t
rtree_leaf_elm_bits_decode(uintptr_t bits) { rtree_leaf_elm_bits_decode(uintptr_t bits) {
rtree_leaf_elm_contents_t contents; rtree_contents_t contents;
/* Do the easy things first. */ /* Do the easy things first. */
contents.szind = bits >> LG_VADDR; contents.metadata.szind = bits >> LG_VADDR;
contents.slab = (bool)(bits & 1); contents.metadata.slab = (bool)(bits & 1);
# ifdef __aarch64__ # ifdef __aarch64__
/* /*
* aarch64 doesn't sign extend the highest virtual address bit to set * aarch64 doesn't sign extend the highest virtual address bit to set
@ -210,109 +218,42 @@ rtree_leaf_elm_bits_decode(uintptr_t bits) {
# endif /* RTREE_LEAF_COMPACT */ # endif /* RTREE_LEAF_COMPACT */
JEMALLOC_ALWAYS_INLINE edata_t * JEMALLOC_ALWAYS_INLINE rtree_contents_t
rtree_leaf_elm_edata_read(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_read(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *elm,
rtree_leaf_elm_t *elm, bool dependent) { bool dependent) {
#ifdef RTREE_LEAF_COMPACT #ifdef RTREE_LEAF_COMPACT
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent); uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits); rtree_contents_t contents = rtree_leaf_elm_bits_decode(bits);
return contents.edata; return contents;
#else #else
edata_t *edata = (edata_t *)atomic_load_p(&elm->le_edata, dependent rtree_contents_t contents;
unsigned metadata_bits = atomic_load_u(&elm->le_metadata, dependent
? ATOMIC_RELAXED : ATOMIC_ACQUIRE); ? ATOMIC_RELAXED : ATOMIC_ACQUIRE);
return edata; contents.metadata.slab = (bool)(metadata_bits & 1);
#endif contents.metadata.szind = (metadata_bits >> 1);
}
JEMALLOC_ALWAYS_INLINE szind_t contents.edata = (edata_t *)atomic_load_p(&elm->le_edata, dependent
rtree_leaf_elm_szind_read(tsdn_t *tsdn, rtree_t *rtree, ? ATOMIC_RELAXED : ATOMIC_ACQUIRE);
rtree_leaf_elm_t *elm, bool dependent) {
#ifdef RTREE_LEAF_COMPACT
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits);
return contents.szind;
#else
return (szind_t)atomic_load_u(&elm->le_szind, dependent ? ATOMIC_RELAXED
: ATOMIC_ACQUIRE);
#endif
}
JEMALLOC_ALWAYS_INLINE bool return contents;
rtree_leaf_elm_slab_read(tsdn_t *tsdn, rtree_t *rtree,
rtree_leaf_elm_t *elm, bool dependent) {
#ifdef RTREE_LEAF_COMPACT
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits);
return contents.slab;
#else
return atomic_load_b(&elm->le_slab, dependent ? ATOMIC_RELAXED :
ATOMIC_ACQUIRE);
#endif
}
static inline void
rtree_leaf_elm_edata_write(tsdn_t *tsdn, rtree_t *rtree,
rtree_leaf_elm_t *elm, edata_t *edata) {
#ifdef RTREE_LEAF_COMPACT
uintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, true);
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(
old_bits);
contents.edata = edata;
uintptr_t bits = rtree_leaf_elm_bits_encode(contents);
atomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);
#else
atomic_store_p(&elm->le_edata, edata, ATOMIC_RELEASE);
#endif
}
static inline void
rtree_leaf_elm_szind_write(tsdn_t *tsdn, rtree_t *rtree,
rtree_leaf_elm_t *elm, szind_t szind) {
assert(szind <= SC_NSIZES);
#ifdef RTREE_LEAF_COMPACT
uintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm,
true);
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(
old_bits);
contents.szind = szind;
uintptr_t bits = rtree_leaf_elm_bits_encode(contents);
atomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);
#else
atomic_store_u(&elm->le_szind, szind, ATOMIC_RELEASE);
#endif
}
static inline void
rtree_leaf_elm_slab_write(tsdn_t *tsdn, rtree_t *rtree,
rtree_leaf_elm_t *elm, bool slab) {
#ifdef RTREE_LEAF_COMPACT
uintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm,
true);
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(
old_bits);
contents.slab = slab;
uintptr_t bits = rtree_leaf_elm_bits_encode(contents);
atomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);
#else
atomic_store_b(&elm->le_slab, slab, ATOMIC_RELEASE);
#endif #endif
} }
static inline void static inline void
rtree_leaf_elm_write(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_write(tsdn_t *tsdn, rtree_t *rtree,
rtree_leaf_elm_t *elm, rtree_leaf_elm_contents_t contents) { rtree_leaf_elm_t *elm, rtree_contents_t contents) {
#ifdef RTREE_LEAF_COMPACT #ifdef RTREE_LEAF_COMPACT
uintptr_t bits = rtree_leaf_elm_bits_encode(contents); uintptr_t bits = rtree_leaf_elm_bits_encode(contents);
atomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE); atomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);
#else #else
rtree_leaf_elm_slab_write(tsdn, rtree, elm, slab); unsigned metadata_bits = ((unsigned)contents.metadata.slab
rtree_leaf_elm_szind_write(tsdn, rtree, elm, szind); | ((unsigned)contents.metadata.szind << 1));
atomic_store_u(&elm->le_metadata, metadata_bits, ATOMIC_RELEASE);
/* /*
* Write edata last, since the element is atomically considered valid * Write edata last, since the element is atomically considered valid
* as soon as the edata field is non-NULL. * as soon as the edata field is non-NULL.
*/ */
rtree_leaf_elm_edata_write(tsdn, rtree, elm, edata); atomic_store_p(&elm->le_edata, contents.edata, ATOMIC_RELEASE);
#endif #endif
} }
@ -320,13 +261,15 @@ static inline void
rtree_leaf_elm_szind_slab_update(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_szind_slab_update(tsdn_t *tsdn, rtree_t *rtree,
rtree_leaf_elm_t *elm, szind_t szind, bool slab) { rtree_leaf_elm_t *elm, szind_t szind, bool slab) {
assert(!slab || szind < SC_NBINS); assert(!slab || szind < SC_NBINS);
rtree_contents_t contents = rtree_leaf_elm_read(
tsdn, rtree, elm, /* dependent */ true);
/* /*
* The caller implicitly assures that it is the only writer to the szind * The caller implicitly assures that it is the only writer to the szind
* and slab fields, and that the edata field cannot currently change. * and slab fields, and that the edata field cannot currently change.
*/ */
rtree_leaf_elm_slab_write(tsdn, rtree, elm, slab); contents.metadata.slab = slab;
rtree_leaf_elm_szind_write(tsdn, rtree, elm, szind); contents.metadata.szind = szind;
rtree_leaf_elm_write(tsdn, rtree, elm, contents);
} }
JEMALLOC_ALWAYS_INLINE rtree_leaf_elm_t * JEMALLOC_ALWAYS_INLINE rtree_leaf_elm_t *
@ -400,11 +343,11 @@ rtree_write(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx, uintptr_t key,
return true; return true;
} }
assert(rtree_leaf_elm_edata_read(tsdn, rtree, elm, false) == NULL); assert(rtree_leaf_elm_read(tsdn, rtree, elm, false).edata == NULL);
rtree_leaf_elm_contents_t contents; rtree_contents_t contents;
contents.edata = edata; contents.edata = edata;
contents.szind = szind; contents.metadata.szind = szind;
contents.slab = slab; contents.metadata.slab = slab;
rtree_leaf_elm_write(tsdn, rtree, elm, contents); rtree_leaf_elm_write(tsdn, rtree, elm, contents);
return false; return false;
@ -430,7 +373,7 @@ rtree_edata_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
if (!dependent && elm == NULL) { if (!dependent && elm == NULL) {
return NULL; return NULL;
} }
return rtree_leaf_elm_edata_read(tsdn, rtree, elm, dependent); return rtree_leaf_elm_read(tsdn, rtree, elm, dependent).edata;
} }
JEMALLOC_ALWAYS_INLINE szind_t JEMALLOC_ALWAYS_INLINE szind_t
@ -441,7 +384,7 @@ rtree_szind_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
if (!dependent && elm == NULL) { if (!dependent && elm == NULL) {
return SC_NSIZES; return SC_NSIZES;
} }
return rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent); return rtree_leaf_elm_read(tsdn, rtree, elm, dependent).metadata.szind;
} }
/* /*
@ -458,18 +401,12 @@ rtree_edata_szind_slab_read(tsdn_t *tsdn, rtree_t *rtree,
if (!dependent && elm == NULL) { if (!dependent && elm == NULL) {
return true; return true;
} }
#ifdef RTREE_LEAF_COMPACT rtree_contents_t contents = rtree_leaf_elm_read(tsdn, rtree, elm,
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent); dependent);
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits);
*r_edata = contents.edata; *r_edata = contents.edata;
*r_szind = contents.szind; *r_szind = contents.metadata.szind;
*r_slab = contents.slab; *r_slab = contents.metadata.slab;
#else
*r_edata = rtree_leaf_elm_edata_read(tsdn, rtree, elm, dependent);
*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);
*r_slab = rtree_leaf_elm_slab_read(tsdn, rtree, elm, dependent);
#endif
return false; return false;
} }
@ -495,22 +432,16 @@ rtree_szind_slab_read_fast(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
uintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1); uintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1);
elm = &leaf[subkey]; elm = &leaf[subkey];
#ifdef RTREE_LEAF_COMPACT rtree_contents_t contents = rtree_leaf_elm_read(tsdn, rtree,
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, /* dependent */ true);
elm, true); *r_szind = contents.metadata.szind;
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode( *r_slab = contents.metadata.slab;
bits);
*r_szind = contents.szind;
*r_slab = contents.slab;
#else
*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, true);
*r_slab = rtree_leaf_elm_slab_read(tsdn, rtree, elm, true);
#endif
return true; return true;
} else { } else {
return false; return false;
} }
} }
JEMALLOC_ALWAYS_INLINE bool JEMALLOC_ALWAYS_INLINE bool
rtree_szind_slab_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx, rtree_szind_slab_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
uintptr_t key, bool dependent, szind_t *r_szind, bool *r_slab) { uintptr_t key, bool dependent, szind_t *r_szind, bool *r_slab) {
@ -519,15 +450,11 @@ rtree_szind_slab_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
if (!dependent && elm == NULL) { if (!dependent && elm == NULL) {
return true; return true;
} }
#ifdef RTREE_LEAF_COMPACT rtree_contents_t contents = rtree_leaf_elm_read(tsdn, rtree, elm,
uintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent); /* dependent */ true);
rtree_leaf_elm_contents_t contents = rtree_leaf_elm_bits_decode(bits); *r_szind = contents.metadata.szind;
*r_szind = contents.szind; *r_slab = contents.metadata.slab;
*r_slab = contents.slab;
#else
*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);
*r_slab = rtree_leaf_elm_slab_read(tsdn, rtree, elm, dependent);
#endif
return false; return false;
} }
@ -544,12 +471,12 @@ static inline void
rtree_clear(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx, rtree_clear(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,
uintptr_t key) { uintptr_t key) {
rtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key, true); rtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key, true);
assert(rtree_leaf_elm_edata_read(tsdn, rtree, elm, false) != assert(rtree_leaf_elm_read(tsdn, rtree, elm,
NULL); /* dependent */ false).edata != NULL);
rtree_leaf_elm_contents_t contents; rtree_contents_t contents;
contents.edata = NULL; contents.edata = NULL;
contents.szind = SC_NSIZES; contents.metadata.szind = SC_NSIZES;
contents.slab = false; contents.metadata.slab = false;
rtree_leaf_elm_write(tsdn, rtree, elm, contents); rtree_leaf_elm_write(tsdn, rtree, elm, contents);
} }

View File

@ -65,12 +65,12 @@ emap_unlock_edata2(tsdn_t *tsdn, emap_t *emap, edata_t *edata1,
static inline emap_lock_result_t static inline emap_lock_result_t
emap_try_lock_rtree_leaf_elm(tsdn_t *tsdn, emap_t *emap, rtree_leaf_elm_t *elm, emap_try_lock_rtree_leaf_elm(tsdn_t *tsdn, emap_t *emap, rtree_leaf_elm_t *elm,
edata_t **result, bool inactive_only) { edata_t **result, bool inactive_only) {
edata_t *edata1 = rtree_leaf_elm_edata_read(tsdn, &emap->rtree, edata_t *edata1 = rtree_leaf_elm_read(tsdn, &emap->rtree, elm,
elm, true); /* dependent */ true).edata;
/* Slab implies active extents and should be skipped. */ /* Slab implies active extents and should be skipped. */
if (edata1 == NULL || (inactive_only && rtree_leaf_elm_slab_read(tsdn, if (edata1 == NULL || (inactive_only && rtree_leaf_elm_read(tsdn,
&emap->rtree, elm, true))) { &emap->rtree, elm, /* dependent */ true).metadata.slab)) {
return emap_lock_result_no_extent; return emap_lock_result_no_extent;
} }
@ -79,8 +79,8 @@ emap_try_lock_rtree_leaf_elm(tsdn_t *tsdn, emap_t *emap, rtree_leaf_elm_t *elm,
* the leaf->edata mapping. We have to recheck while holding the lock. * the leaf->edata mapping. We have to recheck while holding the lock.
*/ */
emap_lock_edata(tsdn, emap, edata1); emap_lock_edata(tsdn, emap, edata1);
edata_t *edata2 = rtree_leaf_elm_edata_read(tsdn, &emap->rtree, elm, edata_t *edata2 = rtree_leaf_elm_read(tsdn, &emap->rtree, elm,
true); /* dependent */ true).edata;
if (edata1 == edata2) { if (edata1 == edata2) {
*result = edata1; *result = edata1;
@ -137,10 +137,10 @@ emap_rtree_leaf_elms_lookup(tsdn_t *tsdn, emap_t *emap, rtree_ctx_t *rtree_ctx,
static void static void
emap_rtree_write_acquired(tsdn_t *tsdn, emap_t *emap, rtree_leaf_elm_t *elm_a, emap_rtree_write_acquired(tsdn_t *tsdn, emap_t *emap, rtree_leaf_elm_t *elm_a,
rtree_leaf_elm_t *elm_b, edata_t *edata, szind_t szind, bool slab) { rtree_leaf_elm_t *elm_b, edata_t *edata, szind_t szind, bool slab) {
rtree_leaf_elm_contents_t contents; rtree_contents_t contents;
contents.edata = edata; contents.edata = edata;
contents.szind = szind; contents.metadata.szind = szind;
contents.slab = slab; contents.metadata.slab = slab;
rtree_leaf_elm_write(tsdn, &emap->rtree, elm_a, contents); rtree_leaf_elm_write(tsdn, &emap->rtree, elm_a, contents);
if (elm_b != NULL) { if (elm_b != NULL) {
rtree_leaf_elm_write(tsdn, &emap->rtree, elm_b, contents); rtree_leaf_elm_write(tsdn, &emap->rtree, elm_b, contents);
@ -278,10 +278,10 @@ emap_merge_prepare(tsdn_t *tsdn, emap_t *emap, emap_prepare_t *prepare,
void void
emap_merge_commit(tsdn_t *tsdn, emap_t *emap, emap_prepare_t *prepare, emap_merge_commit(tsdn_t *tsdn, emap_t *emap, emap_prepare_t *prepare,
edata_t *lead, edata_t *trail) { edata_t *lead, edata_t *trail) {
rtree_leaf_elm_contents_t clear_contents; rtree_contents_t clear_contents;
clear_contents.edata = NULL; clear_contents.edata = NULL;
clear_contents.szind = SC_NSIZES; clear_contents.metadata.szind = SC_NSIZES;
clear_contents.slab = false; clear_contents.metadata.slab = false;
if (prepare->lead_elm_b != NULL) { if (prepare->lead_elm_b != NULL) {
rtree_leaf_elm_write(tsdn, &emap->rtree, rtree_leaf_elm_write(tsdn, &emap->rtree,

View File

@ -137,10 +137,10 @@ TEST_BEGIN(test_rtree_random) {
&rtree_ctx, keys[i], false, true); &rtree_ctx, keys[i], false, true);
expect_ptr_not_null(elm, expect_ptr_not_null(elm,
"Unexpected rtree_leaf_elm_lookup() failure"); "Unexpected rtree_leaf_elm_lookup() failure");
rtree_leaf_elm_contents_t contents; rtree_contents_t contents;
contents.edata = &edata; contents.edata = &edata;
contents.szind = SC_NSIZES; contents.metadata.szind = SC_NSIZES;
contents.slab = false; contents.metadata.slab = false;
rtree_leaf_elm_write(tsdn, rtree, elm, contents); rtree_leaf_elm_write(tsdn, rtree, elm, contents);
expect_ptr_eq(rtree_edata_read(tsdn, rtree, &rtree_ctx, expect_ptr_eq(rtree_edata_read(tsdn, rtree, &rtree_ctx,
keys[i], true), &edata, keys[i], true), &edata,