mallctl.c 26 KB

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