2014-01-03 08:08:28 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2017-05-24 05:26:31 +08:00
|
|
|
#include "jemalloc/internal/rtree.h"
|
|
|
|
|
2016-04-16 15:36:11 +08:00
|
|
|
rtree_node_alloc_t *rtree_node_alloc_orig;
|
|
|
|
rtree_node_dalloc_t *rtree_node_dalloc_orig;
|
2017-03-17 00:46:42 +08:00
|
|
|
rtree_leaf_alloc_t *rtree_leaf_alloc_orig;
|
|
|
|
rtree_leaf_dalloc_t *rtree_leaf_dalloc_orig;
|
2016-04-16 15:36:11 +08:00
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
/* Potentially too large to safely place on the stack. */
|
|
|
|
rtree_t test_rtree;
|
2016-04-16 15:36:11 +08:00
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static rtree_node_elm_t *
|
2017-01-16 08:56:30 +08:00
|
|
|
rtree_node_alloc_intercept(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {
|
2017-03-17 00:46:42 +08:00
|
|
|
rtree_node_elm_t *node;
|
2015-01-31 14:54:08 +08:00
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
if (rtree != &test_rtree) {
|
2016-04-16 15:36:11 +08:00
|
|
|
return rtree_node_alloc_orig(tsdn, rtree, nelms);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-04-16 15:36:11 +08:00
|
|
|
|
2016-11-01 07:23:33 +08:00
|
|
|
malloc_mutex_unlock(tsdn, &rtree->init_lock);
|
2017-03-17 00:46:42 +08:00
|
|
|
node = (rtree_node_elm_t *)calloc(nelms, sizeof(rtree_node_elm_t));
|
2016-03-28 18:06:35 +08:00
|
|
|
assert_ptr_not_null(node, "Unexpected calloc() failure");
|
2016-11-01 07:23:33 +08:00
|
|
|
malloc_mutex_lock(tsdn, &rtree->init_lock);
|
2016-03-28 18:06:35 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return node;
|
2015-01-31 14:54:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-03-17 00:46:42 +08:00
|
|
|
rtree_node_dalloc_intercept(tsdn_t *tsdn, rtree_t *rtree,
|
|
|
|
rtree_node_elm_t *node) {
|
2017-03-23 07:38:03 +08:00
|
|
|
if (rtree != &test_rtree) {
|
2016-04-16 15:36:11 +08:00
|
|
|
rtree_node_dalloc_orig(tsdn, rtree, node);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-31 14:54:08 +08:00
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
2017-03-17 00:46:42 +08:00
|
|
|
static rtree_leaf_elm_t *
|
|
|
|
rtree_leaf_alloc_intercept(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {
|
|
|
|
rtree_leaf_elm_t *leaf;
|
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
if (rtree != &test_rtree) {
|
2017-03-17 00:46:42 +08:00
|
|
|
return rtree_leaf_alloc_orig(tsdn, rtree, nelms);
|
|
|
|
}
|
|
|
|
|
|
|
|
malloc_mutex_unlock(tsdn, &rtree->init_lock);
|
|
|
|
leaf = (rtree_leaf_elm_t *)calloc(nelms, sizeof(rtree_leaf_elm_t));
|
|
|
|
assert_ptr_not_null(leaf, "Unexpected calloc() failure");
|
|
|
|
malloc_mutex_lock(tsdn, &rtree->init_lock);
|
|
|
|
|
|
|
|
return leaf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rtree_leaf_dalloc_intercept(tsdn_t *tsdn, rtree_t *rtree,
|
|
|
|
rtree_leaf_elm_t *leaf) {
|
2017-03-23 07:38:03 +08:00
|
|
|
if (rtree != &test_rtree) {
|
2017-03-17 00:46:42 +08:00
|
|
|
rtree_leaf_dalloc_orig(tsdn, rtree, leaf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(leaf);
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_rtree_read_empty) {
|
2016-04-16 15:36:11 +08:00
|
|
|
tsdn_t *tsdn;
|
2014-01-03 08:08:28 +08:00
|
|
|
|
2016-04-16 15:36:11 +08:00
|
|
|
tsdn = tsdn_fetch();
|
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_t *rtree = &test_rtree;
|
2017-03-30 04:18:02 +08:00
|
|
|
rtree_ctx_t rtree_ctx;
|
|
|
|
rtree_ctx_data_init(&rtree_ctx);
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_false(rtree_new(rtree, false), "Unexpected rtree_new() failure");
|
|
|
|
assert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx, PAGE,
|
2017-03-17 08:57:52 +08:00
|
|
|
false), "rtree_extent_read() should return NULL for empty tree");
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_delete(tsdn, rtree);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2016-03-28 18:06:35 +08:00
|
|
|
#undef NTHREADS
|
|
|
|
#undef NITERS
|
|
|
|
#undef SEED
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_rtree_extrema) {
|
2016-03-24 12:09:28 +08:00
|
|
|
extent_t extent_a, extent_b;
|
2018-07-12 07:05:58 +08:00
|
|
|
extent_init(&extent_a, NULL, NULL, SC_LARGE_MINCLASS, false,
|
|
|
|
sz_size2index(SC_LARGE_MINCLASS), 0,
|
2017-12-15 04:46:39 +08:00
|
|
|
extent_state_active, false, false, true);
|
|
|
|
extent_init(&extent_b, NULL, NULL, 0, false, SC_NSIZES, 0,
|
2017-09-19 08:25:57 +08:00
|
|
|
extent_state_active, false, false, true);
|
2016-04-16 15:36:11 +08:00
|
|
|
|
2017-03-17 08:57:52 +08:00
|
|
|
tsdn_t *tsdn = tsdn_fetch();
|
2014-01-03 08:08:28 +08:00
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_t *rtree = &test_rtree;
|
2017-03-30 04:18:02 +08:00
|
|
|
rtree_ctx_t rtree_ctx;
|
|
|
|
rtree_ctx_data_init(&rtree_ctx);
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_false(rtree_new(rtree, false), "Unexpected rtree_new() failure");
|
2014-01-03 08:08:28 +08:00
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_false(rtree_write(tsdn, rtree, &rtree_ctx, PAGE, &extent_a,
|
2017-03-17 08:57:52 +08:00
|
|
|
extent_szind_get(&extent_a), extent_slab_get(&extent_a)),
|
2017-02-07 05:17:12 +08:00
|
|
|
"Unexpected rtree_write() failure");
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_szind_slab_update(tsdn, rtree, &rtree_ctx, PAGE,
|
2017-03-17 08:57:52 +08:00
|
|
|
extent_szind_get(&extent_a), extent_slab_get(&extent_a));
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx, PAGE, true),
|
2017-02-05 18:50:59 +08:00
|
|
|
&extent_a,
|
2017-03-17 08:57:52 +08:00
|
|
|
"rtree_extent_read() should return previously set value");
|
2014-01-03 08:08:28 +08:00
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_false(rtree_write(tsdn, rtree, &rtree_ctx, ~((uintptr_t)0),
|
2017-03-17 08:57:52 +08:00
|
|
|
&extent_b, extent_szind_get_maybe_invalid(&extent_b),
|
|
|
|
extent_slab_get(&extent_b)), "Unexpected rtree_write() failure");
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,
|
2017-03-17 08:57:52 +08:00
|
|
|
~((uintptr_t)0), true), &extent_b,
|
|
|
|
"rtree_extent_read() should return previously set value");
|
2017-02-07 05:17:12 +08:00
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_delete(tsdn, rtree);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_rtree_bits) {
|
2017-02-07 05:17:12 +08:00
|
|
|
tsdn_t *tsdn = tsdn_fetch();
|
2014-01-03 08:08:28 +08:00
|
|
|
|
2017-02-05 18:50:59 +08:00
|
|
|
uintptr_t keys[] = {PAGE, PAGE + 1,
|
|
|
|
PAGE + (((uintptr_t)1) << LG_PAGE) - 1};
|
2016-04-16 15:36:11 +08:00
|
|
|
|
2017-02-07 05:17:12 +08:00
|
|
|
extent_t extent;
|
2017-12-15 04:46:39 +08:00
|
|
|
extent_init(&extent, NULL, NULL, 0, false, SC_NSIZES, 0,
|
2017-09-19 08:25:57 +08:00
|
|
|
extent_state_active, false, false, true);
|
2017-03-17 08:57:52 +08:00
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_t *rtree = &test_rtree;
|
2017-03-30 04:18:02 +08:00
|
|
|
rtree_ctx_t rtree_ctx;
|
|
|
|
rtree_ctx_data_init(&rtree_ctx);
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_false(rtree_new(rtree, false), "Unexpected rtree_new() failure");
|
2017-02-07 05:17:12 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < sizeof(keys)/sizeof(uintptr_t); i++) {
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_false(rtree_write(tsdn, rtree, &rtree_ctx, keys[i],
|
2017-12-15 04:46:39 +08:00
|
|
|
&extent, SC_NSIZES, false),
|
2017-03-17 08:57:52 +08:00
|
|
|
"Unexpected rtree_write() failure");
|
2017-02-07 05:17:12 +08:00
|
|
|
for (unsigned j = 0; j < sizeof(keys)/sizeof(uintptr_t); j++) {
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,
|
|
|
|
keys[j], true), &extent,
|
|
|
|
"rtree_extent_read() should return previously set "
|
|
|
|
"value and ignore insignificant key bits; i=%u, "
|
|
|
|
"j=%u, set key=%#"FMTxPTR", get key=%#"FMTxPTR, i,
|
|
|
|
j, keys[i], keys[j]);
|
2017-02-07 05:17:12 +08:00
|
|
|
}
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,
|
2017-02-05 18:50:59 +08:00
|
|
|
(((uintptr_t)2) << LG_PAGE), false),
|
2017-02-07 05:17:12 +08:00
|
|
|
"Only leftmost rtree leaf should be set; i=%u", i);
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_clear(tsdn, rtree, &rtree_ctx, keys[i]);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
2017-02-07 05:17:12 +08:00
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_delete(tsdn, rtree);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_rtree_random) {
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NSET 16
|
|
|
|
#define SEED 42
|
2017-02-07 05:17:12 +08:00
|
|
|
sfmt_t *sfmt = init_gen_rand(SEED);
|
|
|
|
tsdn_t *tsdn = tsdn_fetch();
|
|
|
|
uintptr_t keys[NSET];
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_t *rtree = &test_rtree;
|
2017-03-30 04:18:02 +08:00
|
|
|
rtree_ctx_t rtree_ctx;
|
|
|
|
rtree_ctx_data_init(&rtree_ctx);
|
2017-02-07 05:17:12 +08:00
|
|
|
|
2017-03-17 08:57:52 +08:00
|
|
|
extent_t extent;
|
2017-12-15 04:46:39 +08:00
|
|
|
extent_init(&extent, NULL, NULL, 0, false, SC_NSIZES, 0,
|
2017-09-19 08:25:57 +08:00
|
|
|
extent_state_active, false, false, true);
|
2017-03-17 08:57:52 +08:00
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_false(rtree_new(rtree, false), "Unexpected rtree_new() failure");
|
2017-02-07 05:17:12 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < NSET; i++) {
|
|
|
|
keys[i] = (uintptr_t)gen_rand64(sfmt);
|
2017-05-16 05:23:51 +08:00
|
|
|
rtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree,
|
2017-03-17 00:46:42 +08:00
|
|
|
&rtree_ctx, keys[i], false, true);
|
2017-02-07 05:17:12 +08:00
|
|
|
assert_ptr_not_null(elm,
|
2017-05-16 05:23:51 +08:00
|
|
|
"Unexpected rtree_leaf_elm_lookup() failure");
|
2017-12-15 04:46:39 +08:00
|
|
|
rtree_leaf_elm_write(tsdn, rtree, elm, &extent, SC_NSIZES,
|
|
|
|
false);
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,
|
2017-03-17 08:57:52 +08:00
|
|
|
keys[i], true), &extent,
|
|
|
|
"rtree_extent_read() should return previously set value");
|
2017-02-07 05:17:12 +08:00
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < NSET; i++) {
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,
|
2017-03-17 08:57:52 +08:00
|
|
|
keys[i], true), &extent,
|
|
|
|
"rtree_extent_read() should return previously set value, "
|
|
|
|
"i=%u", i);
|
2017-02-07 05:17:12 +08:00
|
|
|
}
|
2014-01-03 08:08:28 +08:00
|
|
|
|
2017-02-07 05:17:12 +08:00
|
|
|
for (unsigned i = 0; i < NSET; i++) {
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_clear(tsdn, rtree, &rtree_ctx, keys[i]);
|
|
|
|
assert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,
|
2017-03-17 08:57:52 +08:00
|
|
|
keys[i], true),
|
|
|
|
"rtree_extent_read() should return previously set value");
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|
2017-02-07 05:17:12 +08:00
|
|
|
for (unsigned i = 0; i < NSET; i++) {
|
2017-03-23 07:38:03 +08:00
|
|
|
assert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,
|
2017-03-17 08:57:52 +08:00
|
|
|
keys[i], true),
|
|
|
|
"rtree_extent_read() should return previously set value");
|
2017-02-07 05:17:12 +08:00
|
|
|
}
|
|
|
|
|
2017-03-23 07:38:03 +08:00
|
|
|
rtree_delete(tsdn, rtree);
|
2014-01-03 08:08:28 +08:00
|
|
|
fini_gen_rand(sfmt);
|
|
|
|
#undef NSET
|
|
|
|
#undef SEED
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2016-04-16 15:36:11 +08:00
|
|
|
rtree_node_alloc_orig = rtree_node_alloc;
|
|
|
|
rtree_node_alloc = rtree_node_alloc_intercept;
|
|
|
|
rtree_node_dalloc_orig = rtree_node_dalloc;
|
|
|
|
rtree_node_dalloc = rtree_node_dalloc_intercept;
|
2017-03-17 00:46:42 +08:00
|
|
|
rtree_leaf_alloc_orig = rtree_leaf_alloc;
|
|
|
|
rtree_leaf_alloc = rtree_leaf_alloc_intercept;
|
|
|
|
rtree_leaf_dalloc_orig = rtree_leaf_dalloc;
|
|
|
|
rtree_leaf_dalloc = rtree_leaf_dalloc_intercept;
|
2016-04-16 15:36:11 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2016-03-28 18:06:35 +08:00
|
|
|
test_rtree_read_empty,
|
2014-01-03 08:08:28 +08:00
|
|
|
test_rtree_extrema,
|
|
|
|
test_rtree_bits,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_rtree_random);
|
2014-01-03 08:08:28 +08:00
|
|
|
}
|