1 #include "test/jemalloc_test.h"
2
TEST_BEGIN(test_mallctl_errors)3 TEST_BEGIN(test_mallctl_errors)
4 {
5 uint64_t epoch;
6 size_t sz;
7
8 assert_d_eq(mallctl("no_such_name", NULL, NULL, NULL, 0), ENOENT,
9 "mallctl() should return ENOENT for non-existent names");
10
11 assert_d_eq(mallctl("version", NULL, NULL, "0.0.0", strlen("0.0.0")),
12 EPERM, "mallctl() should return EPERM on attempt to write "
13 "read-only value");
14
15 assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)-1),
16 EINVAL, "mallctl() should return EINVAL for input size mismatch");
17 assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)+1),
18 EINVAL, "mallctl() should return EINVAL for input size mismatch");
19
20 sz = sizeof(epoch)-1;
21 assert_d_eq(mallctl("epoch", &epoch, &sz, NULL, 0), EINVAL,
22 "mallctl() should return EINVAL for output size mismatch");
23 sz = sizeof(epoch)+1;
24 assert_d_eq(mallctl("epoch", &epoch, &sz, NULL, 0), EINVAL,
25 "mallctl() should return EINVAL for output size mismatch");
26 }
27 TEST_END
28
TEST_BEGIN(test_mallctlnametomib_errors)29 TEST_BEGIN(test_mallctlnametomib_errors)
30 {
31 size_t mib[1];
32 size_t miblen;
33
34 miblen = sizeof(mib)/sizeof(size_t);
35 assert_d_eq(mallctlnametomib("no_such_name", mib, &miblen), ENOENT,
36 "mallctlnametomib() should return ENOENT for non-existent names");
37 }
38 TEST_END
39
TEST_BEGIN(test_mallctlbymib_errors)40 TEST_BEGIN(test_mallctlbymib_errors)
41 {
42 uint64_t epoch;
43 size_t sz;
44 size_t mib[1];
45 size_t miblen;
46
47 miblen = sizeof(mib)/sizeof(size_t);
48 assert_d_eq(mallctlnametomib("version", mib, &miblen), 0,
49 "Unexpected mallctlnametomib() failure");
50
51 assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, "0.0.0",
52 strlen("0.0.0")), EPERM, "mallctl() should return EPERM on "
53 "attempt to write read-only value");
54
55 miblen = sizeof(mib)/sizeof(size_t);
56 assert_d_eq(mallctlnametomib("epoch", mib, &miblen), 0,
57 "Unexpected mallctlnametomib() failure");
58
59 assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &epoch,
60 sizeof(epoch)-1), EINVAL,
61 "mallctlbymib() should return EINVAL for input size mismatch");
62 assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &epoch,
63 sizeof(epoch)+1), EINVAL,
64 "mallctlbymib() should return EINVAL for input size mismatch");
65
66 sz = sizeof(epoch)-1;
67 assert_d_eq(mallctlbymib(mib, miblen, &epoch, &sz, NULL, 0), EINVAL,
68 "mallctlbymib() should return EINVAL for output size mismatch");
69 sz = sizeof(epoch)+1;
70 assert_d_eq(mallctlbymib(mib, miblen, &epoch, &sz, NULL, 0), EINVAL,
71 "mallctlbymib() should return EINVAL for output size mismatch");
72 }
73 TEST_END
74
TEST_BEGIN(test_mallctl_read_write)75 TEST_BEGIN(test_mallctl_read_write)
76 {
77 uint64_t old_epoch, new_epoch;
78 size_t sz = sizeof(old_epoch);
79
80 /* Blind. */
81 assert_d_eq(mallctl("epoch", NULL, NULL, NULL, 0), 0,
82 "Unexpected mallctl() failure");
83 assert_zu_eq(sz, sizeof(old_epoch), "Unexpected output size");
84
85 /* Read. */
86 assert_d_eq(mallctl("epoch", &old_epoch, &sz, NULL, 0), 0,
87 "Unexpected mallctl() failure");
88 assert_zu_eq(sz, sizeof(old_epoch), "Unexpected output size");
89
90 /* Write. */
91 assert_d_eq(mallctl("epoch", NULL, NULL, &new_epoch, sizeof(new_epoch)),
92 0, "Unexpected mallctl() failure");
93 assert_zu_eq(sz, sizeof(old_epoch), "Unexpected output size");
94
95 /* Read+write. */
96 assert_d_eq(mallctl("epoch", &old_epoch, &sz, &new_epoch,
97 sizeof(new_epoch)), 0, "Unexpected mallctl() failure");
98 assert_zu_eq(sz, sizeof(old_epoch), "Unexpected output size");
99 }
100 TEST_END
101
TEST_BEGIN(test_mallctlnametomib_short_mib)102 TEST_BEGIN(test_mallctlnametomib_short_mib)
103 {
104 size_t mib[4];
105 size_t miblen;
106
107 miblen = 3;
108 mib[3] = 42;
109 assert_d_eq(mallctlnametomib("arenas.bin.0.nregs", mib, &miblen), 0,
110 "Unexpected mallctlnametomib() failure");
111 assert_zu_eq(miblen, 3, "Unexpected mib output length");
112 assert_zu_eq(mib[3], 42,
113 "mallctlnametomib() wrote past the end of the input mib");
114 }
115 TEST_END
116
TEST_BEGIN(test_mallctl_config)117 TEST_BEGIN(test_mallctl_config)
118 {
119
120 #define TEST_MALLCTL_CONFIG(config) do { \
121 bool oldval; \
122 size_t sz = sizeof(oldval); \
123 assert_d_eq(mallctl("config."#config, &oldval, &sz, NULL, 0), \
124 0, "Unexpected mallctl() failure"); \
125 assert_b_eq(oldval, config_##config, "Incorrect config value"); \
126 assert_zu_eq(sz, sizeof(oldval), "Unexpected output size"); \
127 } while (0)
128
129 TEST_MALLCTL_CONFIG(cache_oblivious);
130 TEST_MALLCTL_CONFIG(debug);
131 TEST_MALLCTL_CONFIG(fill);
132 TEST_MALLCTL_CONFIG(lazy_lock);
133 TEST_MALLCTL_CONFIG(munmap);
134 TEST_MALLCTL_CONFIG(prof);
135 TEST_MALLCTL_CONFIG(prof_libgcc);
136 TEST_MALLCTL_CONFIG(prof_libunwind);
137 TEST_MALLCTL_CONFIG(stats);
138 TEST_MALLCTL_CONFIG(tcache);
139 TEST_MALLCTL_CONFIG(tls);
140 TEST_MALLCTL_CONFIG(utrace);
141 TEST_MALLCTL_CONFIG(valgrind);
142 TEST_MALLCTL_CONFIG(xmalloc);
143
144 #undef TEST_MALLCTL_CONFIG
145 }
146 TEST_END
147
TEST_BEGIN(test_mallctl_opt)148 TEST_BEGIN(test_mallctl_opt)
149 {
150 bool config_always = true;
151
152 #define TEST_MALLCTL_OPT(t, opt, config) do { \
153 t oldval; \
154 size_t sz = sizeof(oldval); \
155 int expected = config_##config ? 0 : ENOENT; \
156 int result = mallctl("opt."#opt, &oldval, &sz, NULL, 0); \
157 assert_d_eq(result, expected, \
158 "Unexpected mallctl() result for opt."#opt); \
159 assert_zu_eq(sz, sizeof(oldval), "Unexpected output size"); \
160 } while (0)
161
162 TEST_MALLCTL_OPT(bool, abort, always);
163 TEST_MALLCTL_OPT(size_t, lg_chunk, always);
164 TEST_MALLCTL_OPT(const char *, dss, always);
165 TEST_MALLCTL_OPT(size_t, narenas, always);
166 TEST_MALLCTL_OPT(ssize_t, lg_dirty_mult, always);
167 TEST_MALLCTL_OPT(bool, stats_print, always);
168 TEST_MALLCTL_OPT(const char *, junk, fill);
169 TEST_MALLCTL_OPT(size_t, quarantine, fill);
170 TEST_MALLCTL_OPT(bool, redzone, fill);
171 TEST_MALLCTL_OPT(bool, zero, fill);
172 TEST_MALLCTL_OPT(bool, utrace, utrace);
173 TEST_MALLCTL_OPT(bool, xmalloc, xmalloc);
174 TEST_MALLCTL_OPT(bool, tcache, tcache);
175 TEST_MALLCTL_OPT(size_t, lg_tcache_max, tcache);
176 TEST_MALLCTL_OPT(bool, prof, prof);
177 TEST_MALLCTL_OPT(const char *, prof_prefix, prof);
178 TEST_MALLCTL_OPT(bool, prof_active, prof);
179 TEST_MALLCTL_OPT(ssize_t, lg_prof_sample, prof);
180 TEST_MALLCTL_OPT(bool, prof_accum, prof);
181 TEST_MALLCTL_OPT(ssize_t, lg_prof_interval, prof);
182 TEST_MALLCTL_OPT(bool, prof_gdump, prof);
183 TEST_MALLCTL_OPT(bool, prof_final, prof);
184 TEST_MALLCTL_OPT(bool, prof_leak, prof);
185
186 #undef TEST_MALLCTL_OPT
187 }
188 TEST_END
189
TEST_BEGIN(test_manpage_example)190 TEST_BEGIN(test_manpage_example)
191 {
192 unsigned nbins, i;
193 size_t mib[4];
194 size_t len, miblen;
195
196 len = sizeof(nbins);
197 assert_d_eq(mallctl("arenas.nbins", &nbins, &len, NULL, 0), 0,
198 "Unexpected mallctl() failure");
199
200 miblen = 4;
201 assert_d_eq(mallctlnametomib("arenas.bin.0.size", mib, &miblen), 0,
202 "Unexpected mallctlnametomib() failure");
203 for (i = 0; i < nbins; i++) {
204 size_t bin_size;
205
206 mib[2] = i;
207 len = sizeof(bin_size);
208 assert_d_eq(mallctlbymib(mib, miblen, &bin_size, &len, NULL, 0),
209 0, "Unexpected mallctlbymib() failure");
210 /* Do something with bin_size... */
211 }
212 }
213 TEST_END
214
TEST_BEGIN(test_tcache_none)215 TEST_BEGIN(test_tcache_none)
216 {
217 void *p0, *q, *p1;
218
219 test_skip_if(!config_tcache);
220
221 /* Allocate p and q. */
222 p0 = mallocx(42, 0);
223 assert_ptr_not_null(p0, "Unexpected mallocx() failure");
224 q = mallocx(42, 0);
225 assert_ptr_not_null(q, "Unexpected mallocx() failure");
226
227 /* Deallocate p and q, but bypass the tcache for q. */
228 dallocx(p0, 0);
229 dallocx(q, MALLOCX_TCACHE_NONE);
230
231 /* Make sure that tcache-based allocation returns p, not q. */
232 p1 = mallocx(42, 0);
233 assert_ptr_not_null(p1, "Unexpected mallocx() failure");
234 assert_ptr_eq(p0, p1, "Expected tcache to allocate cached region");
235
236 /* Clean up. */
237 dallocx(p1, MALLOCX_TCACHE_NONE);
238 }
239 TEST_END
240
TEST_BEGIN(test_tcache)241 TEST_BEGIN(test_tcache)
242 {
243 #define NTCACHES 10
244 unsigned tis[NTCACHES];
245 void *ps[NTCACHES];
246 void *qs[NTCACHES];
247 unsigned i;
248 size_t sz, psz, qsz;
249
250 test_skip_if(!config_tcache);
251
252 psz = 42;
253 qsz = nallocx(psz, 0) + 1;
254
255 /* Create tcaches. */
256 for (i = 0; i < NTCACHES; i++) {
257 sz = sizeof(unsigned);
258 assert_d_eq(mallctl("tcache.create", &tis[i], &sz, NULL, 0), 0,
259 "Unexpected mallctl() failure, i=%u", i);
260 }
261
262 /* Exercise tcache ID recycling. */
263 for (i = 0; i < NTCACHES; i++) {
264 assert_d_eq(mallctl("tcache.destroy", NULL, NULL, &tis[i],
265 sizeof(unsigned)), 0, "Unexpected mallctl() failure, i=%u",
266 i);
267 }
268 for (i = 0; i < NTCACHES; i++) {
269 sz = sizeof(unsigned);
270 assert_d_eq(mallctl("tcache.create", &tis[i], &sz, NULL, 0), 0,
271 "Unexpected mallctl() failure, i=%u", i);
272 }
273
274 /* Flush empty tcaches. */
275 for (i = 0; i < NTCACHES; i++) {
276 assert_d_eq(mallctl("tcache.flush", NULL, NULL, &tis[i],
277 sizeof(unsigned)), 0, "Unexpected mallctl() failure, i=%u",
278 i);
279 }
280
281 /* Cache some allocations. */
282 for (i = 0; i < NTCACHES; i++) {
283 ps[i] = mallocx(psz, MALLOCX_TCACHE(tis[i]));
284 assert_ptr_not_null(ps[i], "Unexpected mallocx() failure, i=%u",
285 i);
286 dallocx(ps[i], MALLOCX_TCACHE(tis[i]));
287
288 qs[i] = mallocx(qsz, MALLOCX_TCACHE(tis[i]));
289 assert_ptr_not_null(qs[i], "Unexpected mallocx() failure, i=%u",
290 i);
291 dallocx(qs[i], MALLOCX_TCACHE(tis[i]));
292 }
293
294 /* Verify that tcaches allocate cached regions. */
295 for (i = 0; i < NTCACHES; i++) {
296 void *p0 = ps[i];
297 ps[i] = mallocx(psz, MALLOCX_TCACHE(tis[i]));
298 assert_ptr_not_null(ps[i], "Unexpected mallocx() failure, i=%u",
299 i);
300 assert_ptr_eq(ps[i], p0,
301 "Expected mallocx() to allocate cached region, i=%u", i);
302 }
303
304 /* Verify that reallocation uses cached regions. */
305 for (i = 0; i < NTCACHES; i++) {
306 void *q0 = qs[i];
307 qs[i] = rallocx(ps[i], qsz, MALLOCX_TCACHE(tis[i]));
308 assert_ptr_not_null(qs[i], "Unexpected rallocx() failure, i=%u",
309 i);
310 assert_ptr_eq(qs[i], q0,
311 "Expected rallocx() to allocate cached region, i=%u", i);
312 /* Avoid undefined behavior in case of test failure. */
313 if (qs[i] == NULL)
314 qs[i] = ps[i];
315 }
316 for (i = 0; i < NTCACHES; i++)
317 dallocx(qs[i], MALLOCX_TCACHE(tis[i]));
318
319 /* Flush some non-empty tcaches. */
320 for (i = 0; i < NTCACHES/2; i++) {
321 assert_d_eq(mallctl("tcache.flush", NULL, NULL, &tis[i],
322 sizeof(unsigned)), 0, "Unexpected mallctl() failure, i=%u",
323 i);
324 }
325
326 /* Destroy tcaches. */
327 for (i = 0; i < NTCACHES; i++) {
328 assert_d_eq(mallctl("tcache.destroy", NULL, NULL, &tis[i],
329 sizeof(unsigned)), 0, "Unexpected mallctl() failure, i=%u",
330 i);
331 }
332 }
333 TEST_END
334
TEST_BEGIN(test_thread_arena)335 TEST_BEGIN(test_thread_arena)
336 {
337 unsigned arena_old, arena_new, narenas;
338 size_t sz = sizeof(unsigned);
339
340 assert_d_eq(mallctl("arenas.narenas", &narenas, &sz, NULL, 0), 0,
341 "Unexpected mallctl() failure");
342 assert_u_eq(narenas, opt_narenas, "Number of arenas incorrect");
343 arena_new = narenas - 1;
344 assert_d_eq(mallctl("thread.arena", &arena_old, &sz, &arena_new,
345 sizeof(unsigned)), 0, "Unexpected mallctl() failure");
346 arena_new = 0;
347 assert_d_eq(mallctl("thread.arena", &arena_old, &sz, &arena_new,
348 sizeof(unsigned)), 0, "Unexpected mallctl() failure");
349 }
350 TEST_END
351
TEST_BEGIN(test_arena_i_lg_dirty_mult)352 TEST_BEGIN(test_arena_i_lg_dirty_mult)
353 {
354 ssize_t lg_dirty_mult, orig_lg_dirty_mult, prev_lg_dirty_mult;
355 size_t sz = sizeof(ssize_t);
356
357 assert_d_eq(mallctl("arena.0.lg_dirty_mult", &orig_lg_dirty_mult, &sz,
358 NULL, 0), 0, "Unexpected mallctl() failure");
359
360 lg_dirty_mult = -2;
361 assert_d_eq(mallctl("arena.0.lg_dirty_mult", NULL, NULL,
362 &lg_dirty_mult, sizeof(ssize_t)), EFAULT,
363 "Unexpected mallctl() success");
364
365 lg_dirty_mult = (sizeof(size_t) << 3);
366 assert_d_eq(mallctl("arena.0.lg_dirty_mult", NULL, NULL,
367 &lg_dirty_mult, sizeof(ssize_t)), EFAULT,
368 "Unexpected mallctl() success");
369
370 for (prev_lg_dirty_mult = orig_lg_dirty_mult, lg_dirty_mult = -1;
371 lg_dirty_mult < (ssize_t)(sizeof(size_t) << 3); prev_lg_dirty_mult
372 = lg_dirty_mult, lg_dirty_mult++) {
373 ssize_t old_lg_dirty_mult;
374
375 assert_d_eq(mallctl("arena.0.lg_dirty_mult", &old_lg_dirty_mult,
376 &sz, &lg_dirty_mult, sizeof(ssize_t)), 0,
377 "Unexpected mallctl() failure");
378 assert_zd_eq(old_lg_dirty_mult, prev_lg_dirty_mult,
379 "Unexpected old arena.0.lg_dirty_mult");
380 }
381 }
382 TEST_END
383
TEST_BEGIN(test_arena_i_purge)384 TEST_BEGIN(test_arena_i_purge)
385 {
386 unsigned narenas;
387 size_t sz = sizeof(unsigned);
388 size_t mib[3];
389 size_t miblen = 3;
390
391 assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
392 "Unexpected mallctl() failure");
393
394 assert_d_eq(mallctl("arenas.narenas", &narenas, &sz, NULL, 0), 0,
395 "Unexpected mallctl() failure");
396 assert_d_eq(mallctlnametomib("arena.0.purge", mib, &miblen), 0,
397 "Unexpected mallctlnametomib() failure");
398 mib[1] = narenas;
399 assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,
400 "Unexpected mallctlbymib() failure");
401 }
402 TEST_END
403
TEST_BEGIN(test_arena_i_dss)404 TEST_BEGIN(test_arena_i_dss)
405 {
406 const char *dss_prec_old, *dss_prec_new;
407 size_t sz = sizeof(dss_prec_old);
408 size_t mib[3];
409 size_t miblen;
410
411 miblen = sizeof(mib)/sizeof(size_t);
412 assert_d_eq(mallctlnametomib("arena.0.dss", mib, &miblen), 0,
413 "Unexpected mallctlnametomib() error");
414
415 dss_prec_new = "disabled";
416 assert_d_eq(mallctlbymib(mib, miblen, &dss_prec_old, &sz, &dss_prec_new,
417 sizeof(dss_prec_new)), 0, "Unexpected mallctl() failure");
418 assert_str_ne(dss_prec_old, "primary",
419 "Unexpected default for dss precedence");
420
421 assert_d_eq(mallctlbymib(mib, miblen, &dss_prec_new, &sz, &dss_prec_old,
422 sizeof(dss_prec_old)), 0, "Unexpected mallctl() failure");
423
424 assert_d_eq(mallctlbymib(mib, miblen, &dss_prec_old, &sz, NULL, 0), 0,
425 "Unexpected mallctl() failure");
426 assert_str_ne(dss_prec_old, "primary",
427 "Unexpected value for dss precedence");
428
429 mib[1] = narenas_total_get();
430 dss_prec_new = "disabled";
431 assert_d_eq(mallctlbymib(mib, miblen, &dss_prec_old, &sz, &dss_prec_new,
432 sizeof(dss_prec_new)), 0, "Unexpected mallctl() failure");
433 assert_str_ne(dss_prec_old, "primary",
434 "Unexpected default for dss precedence");
435
436 assert_d_eq(mallctlbymib(mib, miblen, &dss_prec_new, &sz, &dss_prec_old,
437 sizeof(dss_prec_new)), 0, "Unexpected mallctl() failure");
438
439 assert_d_eq(mallctlbymib(mib, miblen, &dss_prec_old, &sz, NULL, 0), 0,
440 "Unexpected mallctl() failure");
441 assert_str_ne(dss_prec_old, "primary",
442 "Unexpected value for dss precedence");
443 }
444 TEST_END
445
TEST_BEGIN(test_arenas_initialized)446 TEST_BEGIN(test_arenas_initialized)
447 {
448 unsigned narenas;
449 size_t sz = sizeof(narenas);
450
451 assert_d_eq(mallctl("arenas.narenas", &narenas, &sz, NULL, 0), 0,
452 "Unexpected mallctl() failure");
453 {
454 VARIABLE_ARRAY(bool, initialized, narenas);
455
456 sz = narenas * sizeof(bool);
457 assert_d_eq(mallctl("arenas.initialized", initialized, &sz,
458 NULL, 0), 0, "Unexpected mallctl() failure");
459 }
460 }
461 TEST_END
462
TEST_BEGIN(test_arenas_lg_dirty_mult)463 TEST_BEGIN(test_arenas_lg_dirty_mult)
464 {
465 ssize_t lg_dirty_mult, orig_lg_dirty_mult, prev_lg_dirty_mult;
466 size_t sz = sizeof(ssize_t);
467
468 assert_d_eq(mallctl("arenas.lg_dirty_mult", &orig_lg_dirty_mult, &sz,
469 NULL, 0), 0, "Unexpected mallctl() failure");
470
471 lg_dirty_mult = -2;
472 assert_d_eq(mallctl("arenas.lg_dirty_mult", NULL, NULL,
473 &lg_dirty_mult, sizeof(ssize_t)), EFAULT,
474 "Unexpected mallctl() success");
475
476 lg_dirty_mult = (sizeof(size_t) << 3);
477 assert_d_eq(mallctl("arenas.lg_dirty_mult", NULL, NULL,
478 &lg_dirty_mult, sizeof(ssize_t)), EFAULT,
479 "Unexpected mallctl() success");
480
481 for (prev_lg_dirty_mult = orig_lg_dirty_mult, lg_dirty_mult = -1;
482 lg_dirty_mult < (ssize_t)(sizeof(size_t) << 3); prev_lg_dirty_mult =
483 lg_dirty_mult, lg_dirty_mult++) {
484 ssize_t old_lg_dirty_mult;
485
486 assert_d_eq(mallctl("arenas.lg_dirty_mult", &old_lg_dirty_mult,
487 &sz, &lg_dirty_mult, sizeof(ssize_t)), 0,
488 "Unexpected mallctl() failure");
489 assert_zd_eq(old_lg_dirty_mult, prev_lg_dirty_mult,
490 "Unexpected old arenas.lg_dirty_mult");
491 }
492 }
493 TEST_END
494
TEST_BEGIN(test_arenas_constants)495 TEST_BEGIN(test_arenas_constants)
496 {
497
498 #define TEST_ARENAS_CONSTANT(t, name, expected) do { \
499 t name; \
500 size_t sz = sizeof(t); \
501 assert_d_eq(mallctl("arenas."#name, &name, &sz, NULL, 0), 0, \
502 "Unexpected mallctl() failure"); \
503 assert_zu_eq(name, expected, "Incorrect "#name" size"); \
504 } while (0)
505
506 TEST_ARENAS_CONSTANT(size_t, quantum, QUANTUM);
507 TEST_ARENAS_CONSTANT(size_t, page, PAGE);
508 TEST_ARENAS_CONSTANT(unsigned, nbins, NBINS);
509 TEST_ARENAS_CONSTANT(unsigned, nlruns, nlclasses);
510 TEST_ARENAS_CONSTANT(unsigned, nhchunks, nhclasses);
511
512 #undef TEST_ARENAS_CONSTANT
513 }
514 TEST_END
515
TEST_BEGIN(test_arenas_bin_constants)516 TEST_BEGIN(test_arenas_bin_constants)
517 {
518
519 #define TEST_ARENAS_BIN_CONSTANT(t, name, expected) do { \
520 t name; \
521 size_t sz = sizeof(t); \
522 assert_d_eq(mallctl("arenas.bin.0."#name, &name, &sz, NULL, 0), \
523 0, "Unexpected mallctl() failure"); \
524 assert_zu_eq(name, expected, "Incorrect "#name" size"); \
525 } while (0)
526
527 TEST_ARENAS_BIN_CONSTANT(size_t, size, arena_bin_info[0].reg_size);
528 TEST_ARENAS_BIN_CONSTANT(uint32_t, nregs, arena_bin_info[0].nregs);
529 TEST_ARENAS_BIN_CONSTANT(size_t, run_size, arena_bin_info[0].run_size);
530
531 #undef TEST_ARENAS_BIN_CONSTANT
532 }
533 TEST_END
534
TEST_BEGIN(test_arenas_lrun_constants)535 TEST_BEGIN(test_arenas_lrun_constants)
536 {
537
538 #define TEST_ARENAS_LRUN_CONSTANT(t, name, expected) do { \
539 t name; \
540 size_t sz = sizeof(t); \
541 assert_d_eq(mallctl("arenas.lrun.0."#name, &name, &sz, NULL, \
542 0), 0, "Unexpected mallctl() failure"); \
543 assert_zu_eq(name, expected, "Incorrect "#name" size"); \
544 } while (0)
545
546 TEST_ARENAS_LRUN_CONSTANT(size_t, size, LARGE_MINCLASS);
547
548 #undef TEST_ARENAS_LRUN_CONSTANT
549 }
550 TEST_END
551
TEST_BEGIN(test_arenas_hchunk_constants)552 TEST_BEGIN(test_arenas_hchunk_constants)
553 {
554
555 #define TEST_ARENAS_HCHUNK_CONSTANT(t, name, expected) do { \
556 t name; \
557 size_t sz = sizeof(t); \
558 assert_d_eq(mallctl("arenas.hchunk.0."#name, &name, &sz, NULL, \
559 0), 0, "Unexpected mallctl() failure"); \
560 assert_zu_eq(name, expected, "Incorrect "#name" size"); \
561 } while (0)
562
563 TEST_ARENAS_HCHUNK_CONSTANT(size_t, size, chunksize);
564
565 #undef TEST_ARENAS_HCHUNK_CONSTANT
566 }
567 TEST_END
568
TEST_BEGIN(test_arenas_extend)569 TEST_BEGIN(test_arenas_extend)
570 {
571 unsigned narenas_before, arena, narenas_after;
572 size_t sz = sizeof(unsigned);
573
574 assert_d_eq(mallctl("arenas.narenas", &narenas_before, &sz, NULL, 0), 0,
575 "Unexpected mallctl() failure");
576 assert_d_eq(mallctl("arenas.extend", &arena, &sz, NULL, 0), 0,
577 "Unexpected mallctl() failure");
578 assert_d_eq(mallctl("arenas.narenas", &narenas_after, &sz, NULL, 0), 0,
579 "Unexpected mallctl() failure");
580
581 assert_u_eq(narenas_before+1, narenas_after,
582 "Unexpected number of arenas before versus after extension");
583 assert_u_eq(arena, narenas_after-1, "Unexpected arena index");
584 }
585 TEST_END
586
TEST_BEGIN(test_stats_arenas)587 TEST_BEGIN(test_stats_arenas)
588 {
589
590 #define TEST_STATS_ARENAS(t, name) do { \
591 t name; \
592 size_t sz = sizeof(t); \
593 assert_d_eq(mallctl("stats.arenas.0."#name, &name, &sz, NULL, \
594 0), 0, "Unexpected mallctl() failure"); \
595 } while (0)
596
597 TEST_STATS_ARENAS(const char *, dss);
598 TEST_STATS_ARENAS(unsigned, nthreads);
599 TEST_STATS_ARENAS(size_t, pactive);
600 TEST_STATS_ARENAS(size_t, pdirty);
601
602 #undef TEST_STATS_ARENAS
603 }
604 TEST_END
605
606 int
main(void)607 main(void)
608 {
609
610 return (test(
611 test_mallctl_errors,
612 test_mallctlnametomib_errors,
613 test_mallctlbymib_errors,
614 test_mallctl_read_write,
615 test_mallctlnametomib_short_mib,
616 test_mallctl_config,
617 test_mallctl_opt,
618 test_manpage_example,
619 test_tcache_none,
620 test_tcache,
621 test_thread_arena,
622 test_arena_i_lg_dirty_mult,
623 test_arena_i_purge,
624 test_arena_i_dss,
625 test_arenas_initialized,
626 test_arenas_lg_dirty_mult,
627 test_arenas_constants,
628 test_arenas_bin_constants,
629 test_arenas_lrun_constants,
630 test_arenas_hchunk_constants,
631 test_arenas_extend,
632 test_stats_arenas));
633 }
634