chunk.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "test/jemalloc_test.h"
  2. #ifdef JEMALLOC_FILL
  3. const char *malloc_conf = "junk:false";
  4. #endif
  5. static chunk_hooks_t orig_hooks;
  6. static chunk_hooks_t old_hooks;
  7. static bool do_dalloc = true;
  8. static bool do_decommit;
  9. static bool did_alloc;
  10. static bool did_dalloc;
  11. static bool did_commit;
  12. static bool did_decommit;
  13. static bool did_purge;
  14. static bool did_split;
  15. static bool did_merge;
  16. #if 0
  17. # define TRACE_HOOK(fmt, ...) malloc_printf(fmt, __VA_ARGS__)
  18. #else
  19. # define TRACE_HOOK(fmt, ...)
  20. #endif
  21. void *
  22. chunk_alloc(void *new_addr, size_t size, size_t alignment, bool *zero,
  23. bool *commit, unsigned arena_ind)
  24. {
  25. TRACE_HOOK("%s(new_addr=%p, size=%zu, alignment=%zu, *zero=%s, "
  26. "*commit=%s, arena_ind=%u)\n", __func__, new_addr, size, alignment,
  27. *zero ? "true" : "false", *commit ? "true" : "false", arena_ind);
  28. did_alloc = true;
  29. return (old_hooks.alloc(new_addr, size, alignment, zero, commit,
  30. arena_ind));
  31. }
  32. bool
  33. chunk_dalloc(void *chunk, size_t size, bool committed, unsigned arena_ind)
  34. {
  35. TRACE_HOOK("%s(chunk=%p, size=%zu, committed=%s, arena_ind=%u)\n",
  36. __func__, chunk, size, committed ? "true" : "false", arena_ind);
  37. did_dalloc = true;
  38. if (!do_dalloc)
  39. return (true);
  40. return (old_hooks.dalloc(chunk, size, committed, arena_ind));
  41. }
  42. bool
  43. chunk_commit(void *chunk, size_t size, size_t offset, size_t length,
  44. unsigned arena_ind)
  45. {
  46. bool err;
  47. TRACE_HOOK("%s(chunk=%p, size=%zu, offset=%zu, length=%zu, "
  48. "arena_ind=%u)\n", __func__, chunk, size, offset, length,
  49. arena_ind);
  50. err = old_hooks.commit(chunk, size, offset, length, arena_ind);
  51. did_commit = !err;
  52. return (err);
  53. }
  54. bool
  55. chunk_decommit(void *chunk, size_t size, size_t offset, size_t length,
  56. unsigned arena_ind)
  57. {
  58. bool err;
  59. TRACE_HOOK("%s(chunk=%p, size=%zu, offset=%zu, length=%zu, "
  60. "arena_ind=%u)\n", __func__, chunk, size, offset, length,
  61. arena_ind);
  62. if (!do_decommit)
  63. return (true);
  64. err = old_hooks.decommit(chunk, size, offset, length, arena_ind);
  65. did_decommit = !err;
  66. return (err);
  67. }
  68. bool
  69. chunk_purge(void *chunk, size_t size, size_t offset, size_t length,
  70. unsigned arena_ind)
  71. {
  72. TRACE_HOOK("%s(chunk=%p, size=%zu, offset=%zu, length=%zu "
  73. "arena_ind=%u)\n", __func__, chunk, size, offset, length,
  74. arena_ind);
  75. did_purge = true;
  76. return (old_hooks.purge(chunk, size, offset, length, arena_ind));
  77. }
  78. bool
  79. chunk_split(void *chunk, size_t size, size_t size_a, size_t size_b,
  80. bool committed, unsigned arena_ind)
  81. {
  82. TRACE_HOOK("%s(chunk=%p, size=%zu, size_a=%zu, size_b=%zu, "
  83. "committed=%s, arena_ind=%u)\n", __func__, chunk, size, size_a,
  84. size_b, committed ? "true" : "false", arena_ind);
  85. did_split = true;
  86. return (old_hooks.split(chunk, size, size_a, size_b, committed,
  87. arena_ind));
  88. }
  89. bool
  90. chunk_merge(void *chunk_a, size_t size_a, void *chunk_b, size_t size_b,
  91. bool committed, unsigned arena_ind)
  92. {
  93. TRACE_HOOK("%s(chunk_a=%p, size_a=%zu, chunk_b=%p size_b=%zu, "
  94. "committed=%s, arena_ind=%u)\n", __func__, chunk_a, size_a, chunk_b,
  95. size_b, committed ? "true" : "false", arena_ind);
  96. did_merge = true;
  97. return (old_hooks.merge(chunk_a, size_a, chunk_b, size_b,
  98. committed, arena_ind));
  99. }
  100. TEST_BEGIN(test_chunk)
  101. {
  102. void *p;
  103. size_t old_size, new_size, large0, large1, huge0, huge1, huge2, sz;
  104. chunk_hooks_t new_hooks = {
  105. chunk_alloc,
  106. chunk_dalloc,
  107. chunk_commit,
  108. chunk_decommit,
  109. chunk_purge,
  110. chunk_split,
  111. chunk_merge
  112. };
  113. bool xallocx_success_a, xallocx_success_b, xallocx_success_c;
  114. /* Install custom chunk hooks. */
  115. old_size = sizeof(chunk_hooks_t);
  116. new_size = sizeof(chunk_hooks_t);
  117. assert_d_eq(mallctl("arena.0.chunk_hooks", &old_hooks, &old_size,
  118. &new_hooks, new_size), 0, "Unexpected chunk_hooks error");
  119. orig_hooks = old_hooks;
  120. assert_ptr_ne(old_hooks.alloc, chunk_alloc, "Unexpected alloc error");
  121. assert_ptr_ne(old_hooks.dalloc, chunk_dalloc,
  122. "Unexpected dalloc error");
  123. assert_ptr_ne(old_hooks.commit, chunk_commit,
  124. "Unexpected commit error");
  125. assert_ptr_ne(old_hooks.decommit, chunk_decommit,
  126. "Unexpected decommit error");
  127. assert_ptr_ne(old_hooks.purge, chunk_purge, "Unexpected purge error");
  128. assert_ptr_ne(old_hooks.split, chunk_split, "Unexpected split error");
  129. assert_ptr_ne(old_hooks.merge, chunk_merge, "Unexpected merge error");
  130. /* Get large size classes. */
  131. sz = sizeof(size_t);
  132. assert_d_eq(mallctl("arenas.lrun.0.size", &large0, &sz, NULL, 0), 0,
  133. "Unexpected arenas.lrun.0.size failure");
  134. assert_d_eq(mallctl("arenas.lrun.1.size", &large1, &sz, NULL, 0), 0,
  135. "Unexpected arenas.lrun.1.size failure");
  136. /* Get huge size classes. */
  137. assert_d_eq(mallctl("arenas.hchunk.0.size", &huge0, &sz, NULL, 0), 0,
  138. "Unexpected arenas.hchunk.0.size failure");
  139. assert_d_eq(mallctl("arenas.hchunk.1.size", &huge1, &sz, NULL, 0), 0,
  140. "Unexpected arenas.hchunk.1.size failure");
  141. assert_d_eq(mallctl("arenas.hchunk.2.size", &huge2, &sz, NULL, 0), 0,
  142. "Unexpected arenas.hchunk.2.size failure");
  143. /* Test dalloc/decommit/purge cascade. */
  144. do_dalloc = false;
  145. do_decommit = false;
  146. p = mallocx(huge0 * 2, 0);
  147. assert_ptr_not_null(p, "Unexpected mallocx() error");
  148. did_dalloc = false;
  149. did_decommit = false;
  150. did_purge = false;
  151. did_split = false;
  152. xallocx_success_a = (xallocx(p, huge0, 0, 0) == huge0);
  153. assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
  154. "Unexpected arena.0.purge error");
  155. if (xallocx_success_a) {
  156. assert_true(did_dalloc, "Expected dalloc");
  157. assert_false(did_decommit, "Unexpected decommit");
  158. assert_true(did_purge, "Expected purge");
  159. }
  160. assert_true(did_split, "Expected split");
  161. dallocx(p, 0);
  162. do_dalloc = true;
  163. /* Test decommit/commit and observe split/merge. */
  164. do_dalloc = false;
  165. do_decommit = true;
  166. p = mallocx(huge0 * 2, 0);
  167. assert_ptr_not_null(p, "Unexpected mallocx() error");
  168. did_decommit = false;
  169. did_commit = false;
  170. did_split = false;
  171. did_merge = false;
  172. xallocx_success_b = (xallocx(p, huge0, 0, 0) == huge0);
  173. assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
  174. "Unexpected arena.0.purge error");
  175. if (xallocx_success_b)
  176. assert_true(did_split, "Expected split");
  177. xallocx_success_c = (xallocx(p, huge0 * 2, 0, 0) == huge0 * 2);
  178. assert_b_eq(did_decommit, did_commit, "Expected decommit/commit match");
  179. if (xallocx_success_b && xallocx_success_c)
  180. assert_true(did_merge, "Expected merge");
  181. dallocx(p, 0);
  182. do_dalloc = true;
  183. do_decommit = false;
  184. /* Test purge for partial-chunk huge allocations. */
  185. if (huge0 * 2 > huge2) {
  186. /*
  187. * There are at least four size classes per doubling, so a
  188. * successful xallocx() from size=huge2 to size=huge1 is
  189. * guaranteed to leave trailing purgeable memory.
  190. */
  191. p = mallocx(huge2, 0);
  192. assert_ptr_not_null(p, "Unexpected mallocx() error");
  193. did_purge = false;
  194. assert_zu_eq(xallocx(p, huge1, 0, 0), huge1,
  195. "Unexpected xallocx() failure");
  196. assert_true(did_purge, "Expected purge");
  197. dallocx(p, 0);
  198. }
  199. /* Test decommit for large allocations. */
  200. do_decommit = true;
  201. p = mallocx(large1, 0);
  202. assert_ptr_not_null(p, "Unexpected mallocx() error");
  203. assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
  204. "Unexpected arena.0.purge error");
  205. did_decommit = false;
  206. assert_zu_eq(xallocx(p, large0, 0, 0), large0,
  207. "Unexpected xallocx() failure");
  208. assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
  209. "Unexpected arena.0.purge error");
  210. did_commit = false;
  211. assert_zu_eq(xallocx(p, large1, 0, 0), large1,
  212. "Unexpected xallocx() failure");
  213. assert_b_eq(did_decommit, did_commit, "Expected decommit/commit match");
  214. dallocx(p, 0);
  215. do_decommit = false;
  216. /* Make sure non-huge allocation succeeds. */
  217. p = mallocx(42, 0);
  218. assert_ptr_not_null(p, "Unexpected mallocx() error");
  219. dallocx(p, 0);
  220. /* Restore chunk hooks. */
  221. assert_d_eq(mallctl("arena.0.chunk_hooks", NULL, NULL, &old_hooks,
  222. new_size), 0, "Unexpected chunk_hooks error");
  223. assert_d_eq(mallctl("arena.0.chunk_hooks", &old_hooks, &old_size,
  224. NULL, 0), 0, "Unexpected chunk_hooks error");
  225. assert_ptr_eq(old_hooks.alloc, orig_hooks.alloc,
  226. "Unexpected alloc error");
  227. assert_ptr_eq(old_hooks.dalloc, orig_hooks.dalloc,
  228. "Unexpected dalloc error");
  229. assert_ptr_eq(old_hooks.commit, orig_hooks.commit,
  230. "Unexpected commit error");
  231. assert_ptr_eq(old_hooks.decommit, orig_hooks.decommit,
  232. "Unexpected decommit error");
  233. assert_ptr_eq(old_hooks.purge, orig_hooks.purge,
  234. "Unexpected purge error");
  235. assert_ptr_eq(old_hooks.split, orig_hooks.split,
  236. "Unexpected split error");
  237. assert_ptr_eq(old_hooks.merge, orig_hooks.merge,
  238. "Unexpected merge error");
  239. }
  240. TEST_END
  241. int
  242. main(void)
  243. {
  244. return (test(test_chunk));
  245. }