blob: af1c9a53e44d2341166fc4bb23083b5d2ee4038a [file] [log] [blame]
aravindfb7fe502014-05-05 15:16:56 -07001#include "test/jemalloc_test.h"
2
Mike Hommey4a2a3c92015-08-28 13:45:51 +09003#ifdef JEMALLOC_FILL
4const char *malloc_conf = "junk:false";
5#endif
6
Jason Evansb49a3342015-07-28 11:28:19 -04007static chunk_hooks_t orig_hooks;
8static chunk_hooks_t old_hooks;
9
10static bool do_dalloc = true;
11static bool do_decommit;
12
13static bool did_alloc;
14static bool did_dalloc;
15static bool did_commit;
16static bool did_decommit;
17static bool did_purge;
18static bool did_split;
19static bool did_merge;
20
21#if 0
22# define TRACE_HOOK(fmt, ...) malloc_printf(fmt, __VA_ARGS__)
23#else
24# define TRACE_HOOK(fmt, ...)
25#endif
aravindfb7fe502014-05-05 15:16:56 -070026
27void *
Daniel Micaya95018e2014-10-04 01:39:32 -040028chunk_alloc(void *new_addr, size_t size, size_t alignment, bool *zero,
Jason Evans8fadb1a2015-08-04 10:49:46 -070029 bool *commit, unsigned arena_ind)
aravindfb7fe502014-05-05 15:16:56 -070030{
31
Jason Evansb49a3342015-07-28 11:28:19 -040032 TRACE_HOOK("%s(new_addr=%p, size=%zu, alignment=%zu, *zero=%s, "
Jason Evans8fadb1a2015-08-04 10:49:46 -070033 "*commit=%s, arena_ind=%u)\n", __func__, new_addr, size, alignment,
34 *zero ? "true" : "false", *commit ? "true" : "false", arena_ind);
Jason Evansb49a3342015-07-28 11:28:19 -040035 did_alloc = true;
Jason Evans8fadb1a2015-08-04 10:49:46 -070036 return (old_hooks.alloc(new_addr, size, alignment, zero, commit,
37 arena_ind));
aravindfb7fe502014-05-05 15:16:56 -070038}
39
Jason Evans8d6a3e82015-03-18 18:55:33 -070040bool
Jason Evans8fadb1a2015-08-04 10:49:46 -070041chunk_dalloc(void *chunk, size_t size, bool committed, unsigned arena_ind)
Jason Evans8d6a3e82015-03-18 18:55:33 -070042{
43
Jason Evans8fadb1a2015-08-04 10:49:46 -070044 TRACE_HOOK("%s(chunk=%p, size=%zu, committed=%s, arena_ind=%u)\n",
45 __func__, chunk, size, committed ? "true" : "false", arena_ind);
Jason Evansb49a3342015-07-28 11:28:19 -040046 did_dalloc = true;
47 if (!do_dalloc)
48 return (true);
Jason Evans8fadb1a2015-08-04 10:49:46 -070049 return (old_hooks.dalloc(chunk, size, committed, arena_ind));
Jason Evans8d6a3e82015-03-18 18:55:33 -070050}
51
52bool
Jason Evans8fadb1a2015-08-04 10:49:46 -070053chunk_commit(void *chunk, size_t size, size_t offset, size_t length,
54 unsigned arena_ind)
Jason Evans8d6a3e82015-03-18 18:55:33 -070055{
Jason Evans03bf5b62015-08-12 10:26:54 -070056 bool err;
Jason Evans8d6a3e82015-03-18 18:55:33 -070057
Jason Evans8fadb1a2015-08-04 10:49:46 -070058 TRACE_HOOK("%s(chunk=%p, size=%zu, offset=%zu, length=%zu, "
59 "arena_ind=%u)\n", __func__, chunk, size, offset, length,
60 arena_ind);
Jason Evans03bf5b62015-08-12 10:26:54 -070061 err = old_hooks.commit(chunk, size, offset, length, arena_ind);
62 did_commit = !err;
63 return (err);
Jason Evansb49a3342015-07-28 11:28:19 -040064}
65
66bool
Jason Evans8fadb1a2015-08-04 10:49:46 -070067chunk_decommit(void *chunk, size_t size, size_t offset, size_t length,
68 unsigned arena_ind)
Jason Evansb49a3342015-07-28 11:28:19 -040069{
Jason Evans03bf5b62015-08-12 10:26:54 -070070 bool err;
Jason Evansb49a3342015-07-28 11:28:19 -040071
Jason Evans8fadb1a2015-08-04 10:49:46 -070072 TRACE_HOOK("%s(chunk=%p, size=%zu, offset=%zu, length=%zu, "
73 "arena_ind=%u)\n", __func__, chunk, size, offset, length,
74 arena_ind);
Jason Evans03bf5b62015-08-12 10:26:54 -070075 if (!do_decommit)
76 return (true);
77 err = old_hooks.decommit(chunk, size, offset, length, arena_ind);
78 did_decommit = !err;
79 return (err);
Jason Evansb49a3342015-07-28 11:28:19 -040080}
81
82bool
83chunk_purge(void *chunk, size_t size, size_t offset, size_t length,
84 unsigned arena_ind)
85{
86
87 TRACE_HOOK("%s(chunk=%p, size=%zu, offset=%zu, length=%zu "
88 "arena_ind=%u)\n", __func__, chunk, size, offset, length,
89 arena_ind);
90 did_purge = true;
91 return (old_hooks.purge(chunk, size, offset, length, arena_ind));
92}
93
94bool
95chunk_split(void *chunk, size_t size, size_t size_a, size_t size_b,
96 bool committed, unsigned arena_ind)
97{
98
99 TRACE_HOOK("%s(chunk=%p, size=%zu, size_a=%zu, size_b=%zu, "
100 "committed=%s, arena_ind=%u)\n", __func__, chunk, size, size_a,
101 size_b, committed ? "true" : "false", arena_ind);
102 did_split = true;
103 return (old_hooks.split(chunk, size, size_a, size_b, committed,
104 arena_ind));
105}
106
107bool
108chunk_merge(void *chunk_a, size_t size_a, void *chunk_b, size_t size_b,
109 bool committed, unsigned arena_ind)
110{
111
112 TRACE_HOOK("%s(chunk_a=%p, size_a=%zu, chunk_b=%p size_b=%zu, "
113 "committed=%s, arena_ind=%u)\n", __func__, chunk_a, size_a, chunk_b,
114 size_b, committed ? "true" : "false", arena_ind);
115 did_merge = true;
116 return (old_hooks.merge(chunk_a, size_a, chunk_b, size_b,
117 committed, arena_ind));
Jason Evans8d6a3e82015-03-18 18:55:33 -0700118}
119
aravindfb7fe502014-05-05 15:16:56 -0700120TEST_BEGIN(test_chunk)
121{
122 void *p;
Jason Evans8fadb1a2015-08-04 10:49:46 -0700123 size_t old_size, new_size, large0, large1, huge0, huge1, huge2, sz;
Jason Evansb49a3342015-07-28 11:28:19 -0400124 chunk_hooks_t new_hooks = {
125 chunk_alloc,
126 chunk_dalloc,
127 chunk_commit,
128 chunk_decommit,
129 chunk_purge,
130 chunk_split,
131 chunk_merge
132 };
Jason Evans828d9192015-08-12 15:21:07 -0700133 bool xallocx_success_a, xallocx_success_b, xallocx_success_c;
aravindfb7fe502014-05-05 15:16:56 -0700134
Jason Evansb49a3342015-07-28 11:28:19 -0400135 /* Install custom chunk hooks. */
136 old_size = sizeof(chunk_hooks_t);
137 new_size = sizeof(chunk_hooks_t);
138 assert_d_eq(mallctl("arena.0.chunk_hooks", &old_hooks, &old_size,
139 &new_hooks, new_size), 0, "Unexpected chunk_hooks error");
140 orig_hooks = old_hooks;
141 assert_ptr_ne(old_hooks.alloc, chunk_alloc, "Unexpected alloc error");
142 assert_ptr_ne(old_hooks.dalloc, chunk_dalloc,
143 "Unexpected dalloc error");
144 assert_ptr_ne(old_hooks.commit, chunk_commit,
145 "Unexpected commit error");
146 assert_ptr_ne(old_hooks.decommit, chunk_decommit,
147 "Unexpected decommit error");
148 assert_ptr_ne(old_hooks.purge, chunk_purge, "Unexpected purge error");
149 assert_ptr_ne(old_hooks.split, chunk_split, "Unexpected split error");
150 assert_ptr_ne(old_hooks.merge, chunk_merge, "Unexpected merge error");
aravindfb7fe502014-05-05 15:16:56 -0700151
Jason Evans8fadb1a2015-08-04 10:49:46 -0700152 /* Get large size classes. */
Jason Evans8d6a3e82015-03-18 18:55:33 -0700153 sz = sizeof(size_t);
Jason Evans8fadb1a2015-08-04 10:49:46 -0700154 assert_d_eq(mallctl("arenas.lrun.0.size", &large0, &sz, NULL, 0), 0,
155 "Unexpected arenas.lrun.0.size failure");
156 assert_d_eq(mallctl("arenas.lrun.1.size", &large1, &sz, NULL, 0), 0,
157 "Unexpected arenas.lrun.1.size failure");
158
159 /* Get huge size classes. */
Jason Evans8d6a3e82015-03-18 18:55:33 -0700160 assert_d_eq(mallctl("arenas.hchunk.0.size", &huge0, &sz, NULL, 0), 0,
161 "Unexpected arenas.hchunk.0.size failure");
162 assert_d_eq(mallctl("arenas.hchunk.1.size", &huge1, &sz, NULL, 0), 0,
163 "Unexpected arenas.hchunk.1.size failure");
164 assert_d_eq(mallctl("arenas.hchunk.2.size", &huge2, &sz, NULL, 0), 0,
165 "Unexpected arenas.hchunk.2.size failure");
Jason Evansb49a3342015-07-28 11:28:19 -0400166
167 /* Test dalloc/decommit/purge cascade. */
168 do_dalloc = false;
169 do_decommit = false;
170 p = mallocx(huge0 * 2, 0);
171 assert_ptr_not_null(p, "Unexpected mallocx() error");
172 did_dalloc = false;
173 did_decommit = false;
174 did_purge = false;
Jason Evans828d9192015-08-12 15:21:07 -0700175 did_split = false;
176 xallocx_success_a = (xallocx(p, huge0, 0, 0) == huge0);
Jason Evansb49a3342015-07-28 11:28:19 -0400177 assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
178 "Unexpected arena.0.purge error");
Jason Evans828d9192015-08-12 15:21:07 -0700179 if (xallocx_success_a) {
180 assert_true(did_dalloc, "Expected dalloc");
181 assert_false(did_decommit, "Unexpected decommit");
182 assert_true(did_purge, "Expected purge");
183 }
184 assert_true(did_split, "Expected split");
Jason Evansb49a3342015-07-28 11:28:19 -0400185 dallocx(p, 0);
186 do_dalloc = true;
187
188 /* Test decommit/commit and observe split/merge. */
189 do_dalloc = false;
190 do_decommit = true;
191 p = mallocx(huge0 * 2, 0);
192 assert_ptr_not_null(p, "Unexpected mallocx() error");
193 did_decommit = false;
194 did_commit = false;
195 did_split = false;
196 did_merge = false;
Jason Evans828d9192015-08-12 15:21:07 -0700197 xallocx_success_b = (xallocx(p, huge0, 0, 0) == huge0);
Jason Evansb49a3342015-07-28 11:28:19 -0400198 assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
199 "Unexpected arena.0.purge error");
Jason Evans828d9192015-08-12 15:21:07 -0700200 if (xallocx_success_b)
201 assert_true(did_split, "Expected split");
202 xallocx_success_c = (xallocx(p, huge0 * 2, 0, 0) == huge0 * 2);
Jason Evans03bf5b62015-08-12 10:26:54 -0700203 assert_b_eq(did_decommit, did_commit, "Expected decommit/commit match");
Jason Evans828d9192015-08-12 15:21:07 -0700204 if (xallocx_success_b && xallocx_success_c)
205 assert_true(did_merge, "Expected merge");
Jason Evansb49a3342015-07-28 11:28:19 -0400206 dallocx(p, 0);
207 do_dalloc = true;
208 do_decommit = false;
209
210 /* Test purge for partial-chunk huge allocations. */
Jason Evans8d6a3e82015-03-18 18:55:33 -0700211 if (huge0 * 2 > huge2) {
212 /*
Jason Evansd059b9d2015-07-24 18:21:42 -0700213 * There are at least four size classes per doubling, so a
214 * successful xallocx() from size=huge2 to size=huge1 is
215 * guaranteed to leave trailing purgeable memory.
Jason Evans8d6a3e82015-03-18 18:55:33 -0700216 */
217 p = mallocx(huge2, 0);
218 assert_ptr_not_null(p, "Unexpected mallocx() error");
Jason Evansb49a3342015-07-28 11:28:19 -0400219 did_purge = false;
Jason Evans8d6a3e82015-03-18 18:55:33 -0700220 assert_zu_eq(xallocx(p, huge1, 0, 0), huge1,
221 "Unexpected xallocx() failure");
Jason Evans8fadb1a2015-08-04 10:49:46 -0700222 assert_true(did_purge, "Expected purge");
Jason Evans8d6a3e82015-03-18 18:55:33 -0700223 dallocx(p, 0);
224 }
225
Jason Evans8fadb1a2015-08-04 10:49:46 -0700226 /* Test decommit for large allocations. */
227 do_decommit = true;
228 p = mallocx(large1, 0);
229 assert_ptr_not_null(p, "Unexpected mallocx() error");
230 assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
231 "Unexpected arena.0.purge error");
232 did_decommit = false;
233 assert_zu_eq(xallocx(p, large0, 0, 0), large0,
234 "Unexpected xallocx() failure");
235 assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
236 "Unexpected arena.0.purge error");
Jason Evans8fadb1a2015-08-04 10:49:46 -0700237 did_commit = false;
238 assert_zu_eq(xallocx(p, large1, 0, 0), large1,
239 "Unexpected xallocx() failure");
Jason Evans03bf5b62015-08-12 10:26:54 -0700240 assert_b_eq(did_decommit, did_commit, "Expected decommit/commit match");
Jason Evans8fadb1a2015-08-04 10:49:46 -0700241 dallocx(p, 0);
242 do_decommit = false;
243
Jason Evansb49a3342015-07-28 11:28:19 -0400244 /* Make sure non-huge allocation succeeds. */
aravindfb7fe502014-05-05 15:16:56 -0700245 p = mallocx(42, 0);
Jason Evans8d6a3e82015-03-18 18:55:33 -0700246 assert_ptr_not_null(p, "Unexpected mallocx() error");
Jason Evansb49a3342015-07-28 11:28:19 -0400247 dallocx(p, 0);
aravindfb7fe502014-05-05 15:16:56 -0700248
Jason Evansb49a3342015-07-28 11:28:19 -0400249 /* Restore chunk hooks. */
250 assert_d_eq(mallctl("arena.0.chunk_hooks", NULL, NULL, &old_hooks,
251 new_size), 0, "Unexpected chunk_hooks error");
252 assert_d_eq(mallctl("arena.0.chunk_hooks", &old_hooks, &old_size,
253 NULL, 0), 0, "Unexpected chunk_hooks error");
254 assert_ptr_eq(old_hooks.alloc, orig_hooks.alloc,
255 "Unexpected alloc error");
256 assert_ptr_eq(old_hooks.dalloc, orig_hooks.dalloc,
257 "Unexpected dalloc error");
258 assert_ptr_eq(old_hooks.commit, orig_hooks.commit,
259 "Unexpected commit error");
260 assert_ptr_eq(old_hooks.decommit, orig_hooks.decommit,
261 "Unexpected decommit error");
262 assert_ptr_eq(old_hooks.purge, orig_hooks.purge,
263 "Unexpected purge error");
264 assert_ptr_eq(old_hooks.split, orig_hooks.split,
265 "Unexpected split error");
266 assert_ptr_eq(old_hooks.merge, orig_hooks.merge,
267 "Unexpected merge error");
aravindfb7fe502014-05-05 15:16:56 -0700268}
269TEST_END
270
271int
272main(void)
273{
274
275 return (test(test_chunk));
276}