base.c revision 1.1 1 1.1 christos #include "test/jemalloc_test.h"
2 1.1 christos
3 1.1 christos #include "test/extent_hooks.h"
4 1.1 christos
5 1.1 christos static extent_hooks_t hooks_null = {
6 1.1 christos extent_alloc_hook,
7 1.1 christos NULL, /* dalloc */
8 1.1 christos NULL, /* destroy */
9 1.1 christos NULL, /* commit */
10 1.1 christos NULL, /* decommit */
11 1.1 christos NULL, /* purge_lazy */
12 1.1 christos NULL, /* purge_forced */
13 1.1 christos NULL, /* split */
14 1.1 christos NULL /* merge */
15 1.1 christos };
16 1.1 christos
17 1.1 christos static extent_hooks_t hooks_not_null = {
18 1.1 christos extent_alloc_hook,
19 1.1 christos extent_dalloc_hook,
20 1.1 christos extent_destroy_hook,
21 1.1 christos NULL, /* commit */
22 1.1 christos extent_decommit_hook,
23 1.1 christos extent_purge_lazy_hook,
24 1.1 christos extent_purge_forced_hook,
25 1.1 christos NULL, /* split */
26 1.1 christos NULL /* merge */
27 1.1 christos };
28 1.1 christos
29 1.1 christos TEST_BEGIN(test_base_hooks_default) {
30 1.1 christos base_t *base;
31 1.1 christos size_t allocated0, allocated1, resident, mapped, n_thp;
32 1.1 christos
33 1.1 christos tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
34 1.1 christos base = base_new(tsdn, 0, (extent_hooks_t *)&extent_hooks_default);
35 1.1 christos
36 1.1 christos if (config_stats) {
37 1.1 christos base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
38 1.1 christos &n_thp);
39 1.1 christos assert_zu_ge(allocated0, sizeof(base_t),
40 1.1 christos "Base header should count as allocated");
41 1.1 christos if (opt_metadata_thp == metadata_thp_always) {
42 1.1 christos assert_zu_gt(n_thp, 0,
43 1.1 christos "Base should have 1 THP at least.");
44 1.1 christos }
45 1.1 christos }
46 1.1 christos
47 1.1 christos assert_ptr_not_null(base_alloc(tsdn, base, 42, 1),
48 1.1 christos "Unexpected base_alloc() failure");
49 1.1 christos
50 1.1 christos if (config_stats) {
51 1.1 christos base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
52 1.1 christos &n_thp);
53 1.1 christos assert_zu_ge(allocated1 - allocated0, 42,
54 1.1 christos "At least 42 bytes were allocated by base_alloc()");
55 1.1 christos }
56 1.1 christos
57 1.1 christos base_delete(tsdn, base);
58 1.1 christos }
59 1.1 christos TEST_END
60 1.1 christos
61 1.1 christos TEST_BEGIN(test_base_hooks_null) {
62 1.1 christos extent_hooks_t hooks_orig;
63 1.1 christos base_t *base;
64 1.1 christos size_t allocated0, allocated1, resident, mapped, n_thp;
65 1.1 christos
66 1.1 christos extent_hooks_prep();
67 1.1 christos try_dalloc = false;
68 1.1 christos try_destroy = true;
69 1.1 christos try_decommit = false;
70 1.1 christos try_purge_lazy = false;
71 1.1 christos try_purge_forced = false;
72 1.1 christos memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
73 1.1 christos memcpy(&hooks, &hooks_null, sizeof(extent_hooks_t));
74 1.1 christos
75 1.1 christos tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
76 1.1 christos base = base_new(tsdn, 0, &hooks);
77 1.1 christos assert_ptr_not_null(base, "Unexpected base_new() failure");
78 1.1 christos
79 1.1 christos if (config_stats) {
80 1.1 christos base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
81 1.1 christos &n_thp);
82 1.1 christos assert_zu_ge(allocated0, sizeof(base_t),
83 1.1 christos "Base header should count as allocated");
84 1.1 christos if (opt_metadata_thp == metadata_thp_always) {
85 1.1 christos assert_zu_gt(n_thp, 0,
86 1.1 christos "Base should have 1 THP at least.");
87 1.1 christos }
88 1.1 christos }
89 1.1 christos
90 1.1 christos assert_ptr_not_null(base_alloc(tsdn, base, 42, 1),
91 1.1 christos "Unexpected base_alloc() failure");
92 1.1 christos
93 1.1 christos if (config_stats) {
94 1.1 christos base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
95 1.1 christos &n_thp);
96 1.1 christos assert_zu_ge(allocated1 - allocated0, 42,
97 1.1 christos "At least 42 bytes were allocated by base_alloc()");
98 1.1 christos }
99 1.1 christos
100 1.1 christos base_delete(tsdn, base);
101 1.1 christos
102 1.1 christos memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
103 1.1 christos }
104 1.1 christos TEST_END
105 1.1 christos
106 1.1 christos TEST_BEGIN(test_base_hooks_not_null) {
107 1.1 christos extent_hooks_t hooks_orig;
108 1.1 christos base_t *base;
109 1.1 christos void *p, *q, *r, *r_exp;
110 1.1 christos
111 1.1 christos extent_hooks_prep();
112 1.1 christos try_dalloc = false;
113 1.1 christos try_destroy = true;
114 1.1 christos try_decommit = false;
115 1.1 christos try_purge_lazy = false;
116 1.1 christos try_purge_forced = false;
117 1.1 christos memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
118 1.1 christos memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));
119 1.1 christos
120 1.1 christos tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
121 1.1 christos did_alloc = false;
122 1.1 christos base = base_new(tsdn, 0, &hooks);
123 1.1 christos assert_ptr_not_null(base, "Unexpected base_new() failure");
124 1.1 christos assert_true(did_alloc, "Expected alloc");
125 1.1 christos
126 1.1 christos /*
127 1.1 christos * Check for tight packing at specified alignment under simple
128 1.1 christos * conditions.
129 1.1 christos */
130 1.1 christos {
131 1.1 christos const size_t alignments[] = {
132 1.1 christos 1,
133 1.1 christos QUANTUM,
134 1.1 christos QUANTUM << 1,
135 1.1 christos CACHELINE,
136 1.1 christos CACHELINE << 1,
137 1.1 christos };
138 1.1 christos unsigned i;
139 1.1 christos
140 1.1 christos for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
141 1.1 christos size_t alignment = alignments[i];
142 1.1 christos size_t align_ceil = ALIGNMENT_CEILING(alignment,
143 1.1 christos QUANTUM);
144 1.1 christos p = base_alloc(tsdn, base, 1, alignment);
145 1.1 christos assert_ptr_not_null(p,
146 1.1 christos "Unexpected base_alloc() failure");
147 1.1 christos assert_ptr_eq(p,
148 1.1 christos (void *)(ALIGNMENT_CEILING((uintptr_t)p,
149 1.1 christos alignment)), "Expected quantum alignment");
150 1.1 christos q = base_alloc(tsdn, base, alignment, alignment);
151 1.1 christos assert_ptr_not_null(q,
152 1.1 christos "Unexpected base_alloc() failure");
153 1.1 christos assert_ptr_eq((void *)((uintptr_t)p + align_ceil), q,
154 1.1 christos "Minimal allocation should take up %zu bytes",
155 1.1 christos align_ceil);
156 1.1 christos r = base_alloc(tsdn, base, 1, alignment);
157 1.1 christos assert_ptr_not_null(r,
158 1.1 christos "Unexpected base_alloc() failure");
159 1.1 christos assert_ptr_eq((void *)((uintptr_t)q + align_ceil), r,
160 1.1 christos "Minimal allocation should take up %zu bytes",
161 1.1 christos align_ceil);
162 1.1 christos }
163 1.1 christos }
164 1.1 christos
165 1.1 christos /*
166 1.1 christos * Allocate an object that cannot fit in the first block, then verify
167 1.1 christos * that the first block's remaining space is considered for subsequent
168 1.1 christos * allocation.
169 1.1 christos */
170 1.1 christos assert_zu_ge(extent_bsize_get(&base->blocks->extent), QUANTUM,
171 1.1 christos "Remainder insufficient for test");
172 1.1 christos /* Use up all but one quantum of block. */
173 1.1 christos while (extent_bsize_get(&base->blocks->extent) > QUANTUM) {
174 1.1 christos p = base_alloc(tsdn, base, QUANTUM, QUANTUM);
175 1.1 christos assert_ptr_not_null(p, "Unexpected base_alloc() failure");
176 1.1 christos }
177 1.1 christos r_exp = extent_addr_get(&base->blocks->extent);
178 1.1 christos assert_zu_eq(base->extent_sn_next, 1, "One extant block expected");
179 1.1 christos q = base_alloc(tsdn, base, QUANTUM + 1, QUANTUM);
180 1.1 christos assert_ptr_not_null(q, "Unexpected base_alloc() failure");
181 1.1 christos assert_ptr_ne(q, r_exp, "Expected allocation from new block");
182 1.1 christos assert_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");
183 1.1 christos r = base_alloc(tsdn, base, QUANTUM, QUANTUM);
184 1.1 christos assert_ptr_not_null(r, "Unexpected base_alloc() failure");
185 1.1 christos assert_ptr_eq(r, r_exp, "Expected allocation from first block");
186 1.1 christos assert_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");
187 1.1 christos
188 1.1 christos /*
189 1.1 christos * Check for proper alignment support when normal blocks are too small.
190 1.1 christos */
191 1.1 christos {
192 1.1 christos const size_t alignments[] = {
193 1.1 christos HUGEPAGE,
194 1.1 christos HUGEPAGE << 1
195 1.1 christos };
196 1.1 christos unsigned i;
197 1.1 christos
198 1.1 christos for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
199 1.1 christos size_t alignment = alignments[i];
200 1.1 christos p = base_alloc(tsdn, base, QUANTUM, alignment);
201 1.1 christos assert_ptr_not_null(p,
202 1.1 christos "Unexpected base_alloc() failure");
203 1.1 christos assert_ptr_eq(p,
204 1.1 christos (void *)(ALIGNMENT_CEILING((uintptr_t)p,
205 1.1 christos alignment)), "Expected %zu-byte alignment",
206 1.1 christos alignment);
207 1.1 christos }
208 1.1 christos }
209 1.1 christos
210 1.1 christos called_dalloc = called_destroy = called_decommit = called_purge_lazy =
211 1.1 christos called_purge_forced = false;
212 1.1 christos base_delete(tsdn, base);
213 1.1 christos assert_true(called_dalloc, "Expected dalloc call");
214 1.1 christos assert_true(!called_destroy, "Unexpected destroy call");
215 1.1 christos assert_true(called_decommit, "Expected decommit call");
216 1.1 christos assert_true(called_purge_lazy, "Expected purge_lazy call");
217 1.1 christos assert_true(called_purge_forced, "Expected purge_forced call");
218 1.1 christos
219 1.1 christos try_dalloc = true;
220 1.1 christos try_destroy = true;
221 1.1 christos try_decommit = true;
222 1.1 christos try_purge_lazy = true;
223 1.1 christos try_purge_forced = true;
224 1.1 christos memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
225 1.1 christos }
226 1.1 christos TEST_END
227 1.1 christos
228 1.1 christos int
229 1.1 christos main(void) {
230 1.1 christos return test(
231 1.1 christos test_base_hooks_default,
232 1.1 christos test_base_hooks_null,
233 1.1 christos test_base_hooks_not_null);
234 1.1 christos }
235