extent.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "test/jemalloc_test.h"
  2. #include "test/extent_hooks.h"
  3. static bool
  4. check_background_thread_enabled(void) {
  5. bool enabled;
  6. size_t sz = sizeof(bool);
  7. int ret = mallctl("background_thread", (void *)&enabled, &sz, NULL,0);
  8. if (ret == ENOENT) {
  9. return false;
  10. }
  11. assert_d_eq(ret, 0, "Unexpected mallctl error");
  12. return enabled;
  13. }
  14. static void
  15. test_extent_body(unsigned arena_ind) {
  16. void *p;
  17. size_t large0, large1, large2, sz;
  18. size_t purge_mib[3];
  19. size_t purge_miblen;
  20. int flags;
  21. bool xallocx_success_a, xallocx_success_b, xallocx_success_c;
  22. flags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;
  23. /* Get large size classes. */
  24. sz = sizeof(size_t);
  25. assert_d_eq(mallctl("arenas.lextent.0.size", (void *)&large0, &sz, NULL,
  26. 0), 0, "Unexpected arenas.lextent.0.size failure");
  27. assert_d_eq(mallctl("arenas.lextent.1.size", (void *)&large1, &sz, NULL,
  28. 0), 0, "Unexpected arenas.lextent.1.size failure");
  29. assert_d_eq(mallctl("arenas.lextent.2.size", (void *)&large2, &sz, NULL,
  30. 0), 0, "Unexpected arenas.lextent.2.size failure");
  31. /* Test dalloc/decommit/purge cascade. */
  32. purge_miblen = sizeof(purge_mib)/sizeof(size_t);
  33. assert_d_eq(mallctlnametomib("arena.0.purge", purge_mib, &purge_miblen),
  34. 0, "Unexpected mallctlnametomib() failure");
  35. purge_mib[1] = (size_t)arena_ind;
  36. called_alloc = false;
  37. try_alloc = true;
  38. try_dalloc = false;
  39. try_decommit = false;
  40. p = mallocx(large0 * 2, flags);
  41. assert_ptr_not_null(p, "Unexpected mallocx() error");
  42. assert_true(called_alloc, "Expected alloc call");
  43. called_dalloc = false;
  44. called_decommit = false;
  45. did_purge_lazy = false;
  46. did_purge_forced = false;
  47. called_split = false;
  48. xallocx_success_a = (xallocx(p, large0, 0, flags) == large0);
  49. assert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0),
  50. 0, "Unexpected arena.%u.purge error", arena_ind);
  51. if (xallocx_success_a) {
  52. assert_true(called_dalloc, "Expected dalloc call");
  53. assert_true(called_decommit, "Expected decommit call");
  54. assert_true(did_purge_lazy || did_purge_forced,
  55. "Expected purge");
  56. }
  57. assert_true(called_split, "Expected split call");
  58. dallocx(p, flags);
  59. try_dalloc = true;
  60. /* Test decommit/commit and observe split/merge. */
  61. try_dalloc = false;
  62. try_decommit = true;
  63. p = mallocx(large0 * 2, flags);
  64. assert_ptr_not_null(p, "Unexpected mallocx() error");
  65. did_decommit = false;
  66. did_commit = false;
  67. called_split = false;
  68. did_split = false;
  69. did_merge = false;
  70. xallocx_success_b = (xallocx(p, large0, 0, flags) == large0);
  71. assert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0),
  72. 0, "Unexpected arena.%u.purge error", arena_ind);
  73. if (xallocx_success_b) {
  74. assert_true(did_split, "Expected split");
  75. }
  76. xallocx_success_c = (xallocx(p, large0 * 2, 0, flags) == large0 * 2);
  77. if (did_split) {
  78. assert_b_eq(did_decommit, did_commit,
  79. "Expected decommit/commit match");
  80. }
  81. if (xallocx_success_b && xallocx_success_c) {
  82. assert_true(did_merge, "Expected merge");
  83. }
  84. dallocx(p, flags);
  85. try_dalloc = true;
  86. try_decommit = false;
  87. /* Make sure non-large allocation succeeds. */
  88. p = mallocx(42, flags);
  89. assert_ptr_not_null(p, "Unexpected mallocx() error");
  90. dallocx(p, flags);
  91. }
  92. static void
  93. test_manual_hook_auto_arena(void) {
  94. unsigned narenas;
  95. size_t old_size, new_size, sz;
  96. size_t hooks_mib[3];
  97. size_t hooks_miblen;
  98. extent_hooks_t *new_hooks, *old_hooks;
  99. extent_hooks_prep();
  100. sz = sizeof(unsigned);
  101. /* Get number of auto arenas. */
  102. assert_d_eq(mallctl("opt.narenas", (void *)&narenas, &sz, NULL, 0),
  103. 0, "Unexpected mallctl() failure");
  104. if (narenas == 1) {
  105. return;
  106. }
  107. /* Install custom extent hooks on arena 1 (might not be initialized). */
  108. hooks_miblen = sizeof(hooks_mib)/sizeof(size_t);
  109. assert_d_eq(mallctlnametomib("arena.0.extent_hooks", hooks_mib,
  110. &hooks_miblen), 0, "Unexpected mallctlnametomib() failure");
  111. hooks_mib[1] = 1;
  112. old_size = sizeof(extent_hooks_t *);
  113. new_hooks = &hooks;
  114. new_size = sizeof(extent_hooks_t *);
  115. assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,
  116. &old_size, (void *)&new_hooks, new_size), 0,
  117. "Unexpected extent_hooks error");
  118. static bool auto_arena_created = false;
  119. if (old_hooks != &hooks) {
  120. assert_b_eq(auto_arena_created, false,
  121. "Expected auto arena 1 created only once.");
  122. auto_arena_created = true;
  123. }
  124. }
  125. static void
  126. test_manual_hook_body(void) {
  127. unsigned arena_ind;
  128. size_t old_size, new_size, sz;
  129. size_t hooks_mib[3];
  130. size_t hooks_miblen;
  131. extent_hooks_t *new_hooks, *old_hooks;
  132. extent_hooks_prep();
  133. sz = sizeof(unsigned);
  134. assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
  135. 0, "Unexpected mallctl() failure");
  136. /* Install custom extent hooks. */
  137. hooks_miblen = sizeof(hooks_mib)/sizeof(size_t);
  138. assert_d_eq(mallctlnametomib("arena.0.extent_hooks", hooks_mib,
  139. &hooks_miblen), 0, "Unexpected mallctlnametomib() failure");
  140. hooks_mib[1] = (size_t)arena_ind;
  141. old_size = sizeof(extent_hooks_t *);
  142. new_hooks = &hooks;
  143. new_size = sizeof(extent_hooks_t *);
  144. assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,
  145. &old_size, (void *)&new_hooks, new_size), 0,
  146. "Unexpected extent_hooks error");
  147. assert_ptr_ne(old_hooks->alloc, extent_alloc_hook,
  148. "Unexpected extent_hooks error");
  149. assert_ptr_ne(old_hooks->dalloc, extent_dalloc_hook,
  150. "Unexpected extent_hooks error");
  151. assert_ptr_ne(old_hooks->commit, extent_commit_hook,
  152. "Unexpected extent_hooks error");
  153. assert_ptr_ne(old_hooks->decommit, extent_decommit_hook,
  154. "Unexpected extent_hooks error");
  155. assert_ptr_ne(old_hooks->purge_lazy, extent_purge_lazy_hook,
  156. "Unexpected extent_hooks error");
  157. assert_ptr_ne(old_hooks->purge_forced, extent_purge_forced_hook,
  158. "Unexpected extent_hooks error");
  159. assert_ptr_ne(old_hooks->split, extent_split_hook,
  160. "Unexpected extent_hooks error");
  161. assert_ptr_ne(old_hooks->merge, extent_merge_hook,
  162. "Unexpected extent_hooks error");
  163. if (!check_background_thread_enabled()) {
  164. test_extent_body(arena_ind);
  165. }
  166. /* Restore extent hooks. */
  167. assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, NULL, NULL,
  168. (void *)&old_hooks, new_size), 0, "Unexpected extent_hooks error");
  169. assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,
  170. &old_size, NULL, 0), 0, "Unexpected extent_hooks error");
  171. assert_ptr_eq(old_hooks, default_hooks, "Unexpected extent_hooks error");
  172. assert_ptr_eq(old_hooks->alloc, default_hooks->alloc,
  173. "Unexpected extent_hooks error");
  174. assert_ptr_eq(old_hooks->dalloc, default_hooks->dalloc,
  175. "Unexpected extent_hooks error");
  176. assert_ptr_eq(old_hooks->commit, default_hooks->commit,
  177. "Unexpected extent_hooks error");
  178. assert_ptr_eq(old_hooks->decommit, default_hooks->decommit,
  179. "Unexpected extent_hooks error");
  180. assert_ptr_eq(old_hooks->purge_lazy, default_hooks->purge_lazy,
  181. "Unexpected extent_hooks error");
  182. assert_ptr_eq(old_hooks->purge_forced, default_hooks->purge_forced,
  183. "Unexpected extent_hooks error");
  184. assert_ptr_eq(old_hooks->split, default_hooks->split,
  185. "Unexpected extent_hooks error");
  186. assert_ptr_eq(old_hooks->merge, default_hooks->merge,
  187. "Unexpected extent_hooks error");
  188. }
  189. TEST_BEGIN(test_extent_manual_hook) {
  190. test_manual_hook_auto_arena();
  191. test_manual_hook_body();
  192. /* Test failure paths. */
  193. try_split = false;
  194. test_manual_hook_body();
  195. try_merge = false;
  196. test_manual_hook_body();
  197. try_purge_lazy = false;
  198. try_purge_forced = false;
  199. test_manual_hook_body();
  200. try_split = try_merge = try_purge_lazy = try_purge_forced = true;
  201. }
  202. TEST_END
  203. TEST_BEGIN(test_extent_auto_hook) {
  204. unsigned arena_ind;
  205. size_t new_size, sz;
  206. extent_hooks_t *new_hooks;
  207. extent_hooks_prep();
  208. sz = sizeof(unsigned);
  209. new_hooks = &hooks;
  210. new_size = sizeof(extent_hooks_t *);
  211. assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz,
  212. (void *)&new_hooks, new_size), 0, "Unexpected mallctl() failure");
  213. test_skip_if(check_background_thread_enabled());
  214. test_extent_body(arena_ind);
  215. }
  216. TEST_END
  217. int
  218. main(void) {
  219. return test(
  220. test_extent_manual_hook,
  221. test_extent_auto_hook);
  222. }