summaryrefslogtreecommitdiffstats
path: root/deps/jemalloc/test/unit/edata_cache.c
blob: af1110a9554897368410becfb56d42190d326bba (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
#include "test/jemalloc_test.h"

#include "jemalloc/internal/edata_cache.h"

static void
test_edata_cache_init(edata_cache_t *edata_cache) {
	base_t *base = base_new(TSDN_NULL, /* ind */ 1,
	    &ehooks_default_extent_hooks, /* metadata_use_hooks */ true);
	assert_ptr_not_null(base, "");
	bool err = edata_cache_init(edata_cache, base);
	assert_false(err, "");
}

static void
test_edata_cache_destroy(edata_cache_t *edata_cache) {
	base_delete(TSDN_NULL, edata_cache->base);
}

TEST_BEGIN(test_edata_cache) {
	edata_cache_t ec;
	test_edata_cache_init(&ec);

	/* Get one */
	edata_t *ed1 = edata_cache_get(TSDN_NULL, &ec);
	assert_ptr_not_null(ed1, "");

	/* Cache should be empty */
	assert_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 0, "");

	/* Get another */
	edata_t *ed2 = edata_cache_get(TSDN_NULL, &ec);
	assert_ptr_not_null(ed2, "");

	/* Still empty */
	assert_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 0, "");

	/* Put one back, and the cache should now have one item */
	edata_cache_put(TSDN_NULL, &ec, ed1);
	assert_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 1, "");

	/* Reallocating should reuse the item, and leave an empty cache. */
	edata_t *ed1_again = edata_cache_get(TSDN_NULL, &ec);
	assert_ptr_eq(ed1, ed1_again, "");
	assert_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 0, "");

	test_edata_cache_destroy(&ec);
}
TEST_END

static size_t
ecf_count(edata_cache_fast_t *ecf) {
	size_t count = 0;
	edata_t *cur;
	ql_foreach(cur, &ecf->list.head, ql_link_inactive) {
		count++;
	}
	return count;
}

TEST_BEGIN(test_edata_cache_fast_simple) {
	edata_cache_t ec;
	edata_cache_fast_t ecf;

	test_edata_cache_init(&ec);
	edata_cache_fast_init(&ecf, &ec);

	edata_t *ed1 = edata_cache_fast_get(TSDN_NULL, &ecf);
	expect_ptr_not_null(ed1, "");
	expect_zu_eq(ecf_count(&ecf), 0, "");
	expect_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 0, "");

	edata_t *ed2 = edata_cache_fast_get(TSDN_NULL, &ecf);
	expect_ptr_not_null(ed2, "");
	expect_zu_eq(ecf_count(&ecf), 0, "");
	expect_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 0, "");

	edata_cache_fast_put(TSDN_NULL, &ecf, ed1);
	expect_zu_eq(ecf_count(&ecf), 1, "");
	expect_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 0, "");

	edata_cache_fast_put(TSDN_NULL, &ecf, ed2);
	expect_zu_eq(ecf_count(&ecf), 2, "");
	expect_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 0, "");

	/* LIFO ordering. */
	expect_ptr_eq(ed2, edata_cache_fast_get(TSDN_NULL, &ecf), "");
	expect_zu_eq(ecf_count(&ecf), 1, "");
	expect_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 0, "");

	expect_ptr_eq(ed1, edata_cache_fast_get(TSDN_NULL, &ecf), "");
	expect_zu_eq(ecf_count(&ecf), 0, "");
	expect_zu_eq(atomic_load_zu(&ec.count, ATOMIC_RELAXED), 0, "");

	test_edata_cache_destroy(&ec);
}
TEST_END

TEST_BEGIN(test_edata_cache_fill) {
	edata_cache_t ec;
	edata_cache_fast_t ecf;

	test_edata_cache_init(&ec);
	edata_cache_fast_init(&ecf, &ec);

	edata_t *allocs[EDATA_CACHE_FAST_FILL * 2];

	/*
	 * If the fallback cache can't satisfy the request, we shouldn't do
	 * extra allocations until compelled to.  Put half the fill goal in the
	 * fallback.
	 */
	for (int i = 0; i < EDATA_CACHE_FAST_FILL / 2; i++) {
		allocs[i] = edata_cache_get(TSDN_NULL, &ec);
	}
	for (int i = 0; i < EDATA_CACHE_FAST_FILL / 2; i++) {
		edata_cache_put(TSDN_NULL, &ec, allocs[i]);
	}
	expect_zu_eq(EDATA_CACHE_FAST_FILL / 2,
	    atomic_load_zu(&ec.count, ATOMIC_RELAXED), "");

	allocs[0] = edata_cache_fast_get(TSDN_NULL, &ecf);
	expect_zu_eq(EDATA_CACHE_FAST_FILL / 2 - 1, ecf_count(&ecf),
	    "Should have grabbed all edatas available but no more.");

	for (int i = 1; i < EDATA_CACHE_FAST_FILL / 2; i++) {
		allocs[i] = edata_cache_fast_get(TSDN_NULL, &ecf);
		expect_ptr_not_null(allocs[i], "");
	}
	expect_zu_eq(0, ecf_count(&ecf), "");

	/* When forced, we should alloc from the base. */
	edata_t *edata = edata_cache_fast_get(TSDN_NULL, &ecf);
	expect_ptr_not_null(edata, "");
	expect_zu_eq(0, ecf_count(&ecf), "Allocated more than necessary");
	expect_zu_eq(0, atomic_load_zu(&ec.count, ATOMIC_RELAXED),
	    "Allocated more than necessary");

	/*
	 * We should correctly fill in the common case where the fallback isn't
	 * exhausted, too.
	 */
	for (int i = 0; i < EDATA_CACHE_FAST_FILL * 2; i++) {
		allocs[i] = edata_cache_get(TSDN_NULL, &ec);
		expect_ptr_not_null(allocs[i], "");
	}
	for (int i = 0; i < EDATA_CACHE_FAST_FILL * 2; i++) {
		edata_cache_put(TSDN_NULL, &ec, allocs[i]);
	}

	allocs[0] = edata_cache_fast_get(TSDN_NULL, &ecf);
	expect_zu_eq(EDATA_CACHE_FAST_FILL - 1, ecf_count(&ecf), "");
	expect_zu_eq(EDATA_CACHE_FAST_FILL,
	    atomic_load_zu(&ec.count, ATOMIC_RELAXED), "");
	for (int i = 1; i < EDATA_CACHE_FAST_FILL; i++) {
		expect_zu_eq(EDATA_CACHE_FAST_FILL - i, ecf_count(&ecf), "");
		expect_zu_eq(EDATA_CACHE_FAST_FILL,
		    atomic_load_zu(&ec.count, ATOMIC_RELAXED), "");
		allocs[i] = edata_cache_fast_get(TSDN_NULL, &ecf);
		expect_ptr_not_null(allocs[i], "");
	}
	expect_zu_eq(0, ecf_count(&ecf), "");
	expect_zu_eq(EDATA_CACHE_FAST_FILL,
	    atomic_load_zu(&ec.count, ATOMIC_RELAXED), "");

	allocs[0] = edata_cache_fast_get(TSDN_NULL, &ecf);
	expect_zu_eq(EDATA_CACHE_FAST_FILL - 1, ecf_count(&ecf), "");
	expect_zu_eq(0, atomic_load_zu(&ec.count, ATOMIC_RELAXED), "");
	for (int i = 1; i < EDATA_CACHE_FAST_FILL; i++) {
		expect_zu_eq(EDATA_CACHE_FAST_FILL - i, ecf_count(&ecf), "");
		expect_zu_eq(0, atomic_load_zu(&ec.count, ATOMIC_RELAXED), "");
		allocs[i] = edata_cache_fast_get(TSDN_NULL, &ecf);
		expect_ptr_not_null(allocs[i], "");
	}
	expect_zu_eq(0, ecf_count(&ecf), "");
	expect_zu_eq(0, atomic_load_zu(&ec.count, ATOMIC_RELAXED), "");

	test_edata_cache_destroy(&ec);
}
TEST_END

TEST_BEGIN(test_edata_cache_disable) {
	edata_cache_t ec;
	edata_cache_fast_t ecf;

	test_edata_cache_init(&ec);
	edata_cache_fast_init(&ecf, &ec);

	for (int i = 0; i < EDATA_CACHE_FAST_FILL; i++) {
		edata_t *edata = edata_cache_get(TSDN_NULL, &ec);
		expect_ptr_not_null(edata, "");
		edata_cache_fast_put(TSDN_NULL, &ecf, edata);
	}

	expect_zu_eq(EDATA_CACHE_FAST_FILL, ecf_count(&ecf), "");
	expect_zu_eq(0, atomic_load_zu(&ec.count, ATOMIC_RELAXED), "");

	edata_cache_fast_disable(TSDN_NULL, &ecf);

	expect_zu_eq(0, ecf_count(&ecf), "");
	expect_zu_eq(EDATA_CACHE_FAST_FILL,
	    atomic_load_zu(&ec.count, ATOMIC_RELAXED), "Disabling should flush");

	edata_t *edata = edata_cache_fast_get(TSDN_NULL, &ecf);
	expect_zu_eq(0, ecf_count(&ecf), "");
	expect_zu_eq(EDATA_CACHE_FAST_FILL - 1,
	    atomic_load_zu(&ec.count, ATOMIC_RELAXED),
	    "Disabled ecf should forward on get");

	edata_cache_fast_put(TSDN_NULL, &ecf, edata);
	expect_zu_eq(0, ecf_count(&ecf), "");
	expect_zu_eq(EDATA_CACHE_FAST_FILL,
	    atomic_load_zu(&ec.count, ATOMIC_RELAXED),
	    "Disabled ecf should forward on put");

	test_edata_cache_destroy(&ec);
}
TEST_END

int
main(void) {
	return test(
	    test_edata_cache,
	    test_edata_cache_fast_simple,
	    test_edata_cache_fill,
	    test_edata_cache_disable);
}