summaryrefslogtreecommitdiffstats
path: root/deps/jemalloc/test/unit/rtree.c
blob: 90adca1346a592d8a9d4f90a35933747cd6b7334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "test/jemalloc_test.h"

#include "jemalloc/internal/rtree.h"

rtree_node_alloc_t *rtree_node_alloc_orig;
rtree_node_dalloc_t *rtree_node_dalloc_orig;
rtree_leaf_alloc_t *rtree_leaf_alloc_orig;
rtree_leaf_dalloc_t *rtree_leaf_dalloc_orig;

/* Potentially too large to safely place on the stack. */
rtree_t test_rtree;

static rtree_node_elm_t *
rtree_node_alloc_intercept(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {
	rtree_node_elm_t *node;

	if (rtree != &test_rtree) {
		return rtree_node_alloc_orig(tsdn, rtree, nelms);
	}

	malloc_mutex_unlock(tsdn, &rtree->init_lock);
	node = (rtree_node_elm_t *)calloc(nelms, sizeof(rtree_node_elm_t));
	assert_ptr_not_null(node, "Unexpected calloc() failure");
	malloc_mutex_lock(tsdn, &rtree->init_lock);

	return node;
}

static void
rtree_node_dalloc_intercept(tsdn_t *tsdn, rtree_t *rtree,
    rtree_node_elm_t *node) {
	if (rtree != &test_rtree) {
		rtree_node_dalloc_orig(tsdn, rtree, node);
		return;
	}

	free(node);
}

static rtree_leaf_elm_t *
rtree_leaf_alloc_intercept(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {
	rtree_leaf_elm_t *leaf;

	if (rtree != &test_rtree) {
		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) {
	if (rtree != &test_rtree) {
		rtree_leaf_dalloc_orig(tsdn, rtree, leaf);
		return;
	}

	free(leaf);
}

TEST_BEGIN(test_rtree_read_empty) {
	tsdn_t *tsdn;

	tsdn = tsdn_fetch();

	rtree_t *rtree = &test_rtree;
	rtree_ctx_t rtree_ctx;
	rtree_ctx_data_init(&rtree_ctx);
	assert_false(rtree_new(rtree, false), "Unexpected rtree_new() failure");
	assert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx, PAGE,
	    false), "rtree_extent_read() should return NULL for empty tree");
	rtree_delete(tsdn, rtree);
}
TEST_END

#undef NTHREADS
#undef NITERS
#undef SEED

TEST_BEGIN(test_rtree_extrema) {
	extent_t extent_a, extent_b;
	extent_init(&extent_a, NULL, NULL, SC_LARGE_MINCLASS, false,
	    sz_size2index(SC_LARGE_MINCLASS), 0,
	    extent_state_active, false, false, true, EXTENT_NOT_HEAD);
	extent_init(&extent_b, NULL, NULL, 0, false, SC_NSIZES, 0,
	    extent_state_active, false, false, true, EXTENT_NOT_HEAD);

	tsdn_t *tsdn = tsdn_fetch();

	rtree_t *rtree = &test_rtree;
	rtree_ctx_t rtree_ctx;
	rtree_ctx_data_init(&rtree_ctx);
	assert_false(rtree_new(rtree, false), "Unexpected rtree_new() failure");

	assert_false(rtree_write(tsdn, rtree, &rtree_ctx, PAGE, &extent_a,
	    extent_szind_get(&extent_a), extent_slab_get(&extent_a)),
	    "Unexpected rtree_write() failure");
	rtree_szind_slab_update(tsdn, rtree, &rtree_ctx, PAGE,
	    extent_szind_get(&extent_a), extent_slab_get(&extent_a));
	assert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx, PAGE, true),
	    &extent_a,
	    "rtree_extent_read() should return previously set value");

	assert_false(rtree_write(tsdn, rtree, &rtree_ctx, ~((uintptr_t)0),
	    &extent_b, extent_szind_get_maybe_invalid(&extent_b),
	    extent_slab_get(&extent_b)), "Unexpected rtree_write() failure");
	assert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,
	    ~((uintptr_t)0), true), &extent_b,
	    "rtree_extent_read() should return previously set value");

	rtree_delete(tsdn, rtree);
}
TEST_END

TEST_BEGIN(test_rtree_bits) {
	tsdn_t *tsdn = tsdn_fetch();

	uintptr_t keys[] = {PAGE, PAGE + 1,
	    PAGE + (((uintptr_t)1) << LG_PAGE) - 1};

	extent_t extent;
	extent_init(&extent, NULL, NULL, 0, false, SC_NSIZES, 0,
	    extent_state_active, false, false, true, EXTENT_NOT_HEAD);

	rtree_t *rtree = &test_rtree;
	rtree_ctx_t rtree_ctx;
	rtree_ctx_data_init(&rtree_ctx);
	assert_false(rtree_new(rtree, false), "Unexpected rtree_new() failure");

	for (unsigned i = 0; i < sizeof(keys)/sizeof(uintptr_t); i++) {
		assert_false(rtree_write(tsdn, rtree, &rtree_ctx, keys[i],
		    &extent, SC_NSIZES, false),
		    "Unexpected rtree_write() failure");
		for (unsigned j = 0; j < sizeof(keys)/sizeof(uintptr_t); j++) {
			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]);
		}
		assert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,
		    (((uintptr_t)2) << LG_PAGE), false),
		    "Only leftmost rtree leaf should be set; i=%u", i);
		rtree_clear(tsdn, rtree, &rtree_ctx, keys[i]);
	}

	rtree_delete(tsdn, rtree);
}
TEST_END

TEST_BEGIN(test_rtree_random) {
#define NSET 16
#define SEED 42
	sfmt_t *sfmt = init_gen_rand(SEED);
	tsdn_t *tsdn = tsdn_fetch();
	uintptr_t keys[NSET];
	rtree_t *rtree = &test_rtree;
	rtree_ctx_t rtree_ctx;
	rtree_ctx_data_init(&rtree_ctx);

	extent_t extent;
	extent_init(&extent, NULL, NULL, 0, false, SC_NSIZES, 0,
	    extent_state_active, false, false, true, EXTENT_NOT_HEAD);

	assert_false(rtree_new(rtree, false), "Unexpected rtree_new() failure");

	for (unsigned i = 0; i < NSET; i++) {
		keys[i] = (uintptr_t)gen_rand64(sfmt);
		rtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree,
		    &rtree_ctx, keys[i], false, true);
		assert_ptr_not_null(elm,
		    "Unexpected rtree_leaf_elm_lookup() failure");
		rtree_leaf_elm_write(tsdn, rtree, elm, &extent, SC_NSIZES,
		    false);
		assert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,
		    keys[i], true), &extent,
		    "rtree_extent_read() should return previously set value");
	}
	for (unsigned i = 0; i < NSET; i++) {
		assert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,
		    keys[i], true), &extent,
		    "rtree_extent_read() should return previously set value, "
		    "i=%u", i);
	}

	for (unsigned i = 0; i < NSET; i++) {
		rtree_clear(tsdn, rtree, &rtree_ctx, keys[i]);
		assert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,
		    keys[i], true),
		   "rtree_extent_read() should return previously set value");
	}
	for (unsigned i = 0; i < NSET; i++) {
		assert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,
		    keys[i], true),
		    "rtree_extent_read() should return previously set value");
	}

	rtree_delete(tsdn, rtree);
	fini_gen_rand(sfmt);
#undef NSET
#undef SEED
}
TEST_END

int
main(void) {
	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;
	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;

	return test(
	    test_rtree_read_empty,
	    test_rtree_extrema,
	    test_rtree_bits,
	    test_rtree_random);
}