2
0

arena_reset.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #ifndef ARENA_RESET_PROF_C_
  2. #include "test/jemalloc_test.h"
  3. #endif
  4. #include "jemalloc/internal/extent_mmap.h"
  5. #include "jemalloc/internal/rtree.h"
  6. #include "test/extent_hooks.h"
  7. static unsigned
  8. get_nsizes_impl(const char *cmd) {
  9. unsigned ret;
  10. size_t z;
  11. z = sizeof(unsigned);
  12. assert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
  13. "Unexpected mallctl(\"%s\", ...) failure", cmd);
  14. return ret;
  15. }
  16. static unsigned
  17. get_nsmall(void) {
  18. return get_nsizes_impl("arenas.nbins");
  19. }
  20. static unsigned
  21. get_nlarge(void) {
  22. return get_nsizes_impl("arenas.nlextents");
  23. }
  24. static size_t
  25. get_size_impl(const char *cmd, size_t ind) {
  26. size_t ret;
  27. size_t z;
  28. size_t mib[4];
  29. size_t miblen = 4;
  30. z = sizeof(size_t);
  31. assert_d_eq(mallctlnametomib(cmd, mib, &miblen),
  32. 0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
  33. mib[2] = ind;
  34. z = sizeof(size_t);
  35. assert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
  36. 0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
  37. return ret;
  38. }
  39. static size_t
  40. get_small_size(size_t ind) {
  41. return get_size_impl("arenas.bin.0.size", ind);
  42. }
  43. static size_t
  44. get_large_size(size_t ind) {
  45. return get_size_impl("arenas.lextent.0.size", ind);
  46. }
  47. /* Like ivsalloc(), but safe to call on discarded allocations. */
  48. static size_t
  49. vsalloc(tsdn_t *tsdn, const void *ptr) {
  50. rtree_ctx_t rtree_ctx_fallback;
  51. rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
  52. extent_t *extent;
  53. szind_t szind;
  54. if (rtree_extent_szind_read(tsdn, &extents_rtree, rtree_ctx,
  55. (uintptr_t)ptr, false, &extent, &szind)) {
  56. return 0;
  57. }
  58. if (extent == NULL) {
  59. return 0;
  60. }
  61. if (extent_state_get(extent) != extent_state_active) {
  62. return 0;
  63. }
  64. if (szind == NSIZES) {
  65. return 0;
  66. }
  67. return sz_index2size(szind);
  68. }
  69. static unsigned
  70. do_arena_create(extent_hooks_t *h) {
  71. unsigned arena_ind;
  72. size_t sz = sizeof(unsigned);
  73. assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz,
  74. (void *)(h != NULL ? &h : NULL), (h != NULL ? sizeof(h) : 0)), 0,
  75. "Unexpected mallctl() failure");
  76. return arena_ind;
  77. }
  78. static void
  79. do_arena_reset_pre(unsigned arena_ind, void ***ptrs, unsigned *nptrs) {
  80. #define NLARGE 32
  81. unsigned nsmall, nlarge, i;
  82. size_t sz;
  83. int flags;
  84. tsdn_t *tsdn;
  85. flags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;
  86. nsmall = get_nsmall();
  87. nlarge = get_nlarge() > NLARGE ? NLARGE : get_nlarge();
  88. *nptrs = nsmall + nlarge;
  89. *ptrs = (void **)malloc(*nptrs * sizeof(void *));
  90. assert_ptr_not_null(*ptrs, "Unexpected malloc() failure");
  91. /* Allocate objects with a wide range of sizes. */
  92. for (i = 0; i < nsmall; i++) {
  93. sz = get_small_size(i);
  94. (*ptrs)[i] = mallocx(sz, flags);
  95. assert_ptr_not_null((*ptrs)[i],
  96. "Unexpected mallocx(%zu, %#x) failure", sz, flags);
  97. }
  98. for (i = 0; i < nlarge; i++) {
  99. sz = get_large_size(i);
  100. (*ptrs)[nsmall + i] = mallocx(sz, flags);
  101. assert_ptr_not_null((*ptrs)[i],
  102. "Unexpected mallocx(%zu, %#x) failure", sz, flags);
  103. }
  104. tsdn = tsdn_fetch();
  105. /* Verify allocations. */
  106. for (i = 0; i < *nptrs; i++) {
  107. assert_zu_gt(ivsalloc(tsdn, (*ptrs)[i]), 0,
  108. "Allocation should have queryable size");
  109. }
  110. }
  111. static void
  112. do_arena_reset_post(void **ptrs, unsigned nptrs, unsigned arena_ind) {
  113. tsdn_t *tsdn;
  114. unsigned i;
  115. tsdn = tsdn_fetch();
  116. if (have_background_thread) {
  117. malloc_mutex_lock(tsdn,
  118. &background_thread_info[arena_ind % ncpus].mtx);
  119. }
  120. /* Verify allocations no longer exist. */
  121. for (i = 0; i < nptrs; i++) {
  122. assert_zu_eq(vsalloc(tsdn, ptrs[i]), 0,
  123. "Allocation should no longer exist");
  124. }
  125. if (have_background_thread) {
  126. malloc_mutex_unlock(tsdn,
  127. &background_thread_info[arena_ind % ncpus].mtx);
  128. }
  129. free(ptrs);
  130. }
  131. static void
  132. do_arena_reset_destroy(const char *name, unsigned arena_ind) {
  133. size_t mib[3];
  134. size_t miblen;
  135. miblen = sizeof(mib)/sizeof(size_t);
  136. assert_d_eq(mallctlnametomib(name, mib, &miblen), 0,
  137. "Unexpected mallctlnametomib() failure");
  138. mib[1] = (size_t)arena_ind;
  139. assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,
  140. "Unexpected mallctlbymib() failure");
  141. }
  142. static void
  143. do_arena_reset(unsigned arena_ind) {
  144. do_arena_reset_destroy("arena.0.reset", arena_ind);
  145. }
  146. static void
  147. do_arena_destroy(unsigned arena_ind) {
  148. do_arena_reset_destroy("arena.0.destroy", arena_ind);
  149. }
  150. TEST_BEGIN(test_arena_reset) {
  151. unsigned arena_ind;
  152. void **ptrs;
  153. unsigned nptrs;
  154. arena_ind = do_arena_create(NULL);
  155. do_arena_reset_pre(arena_ind, &ptrs, &nptrs);
  156. do_arena_reset(arena_ind);
  157. do_arena_reset_post(ptrs, nptrs, arena_ind);
  158. }
  159. TEST_END
  160. static bool
  161. arena_i_initialized(unsigned arena_ind, bool refresh) {
  162. bool initialized;
  163. size_t mib[3];
  164. size_t miblen, sz;
  165. if (refresh) {
  166. uint64_t epoch = 1;
  167. assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch,
  168. sizeof(epoch)), 0, "Unexpected mallctl() failure");
  169. }
  170. miblen = sizeof(mib)/sizeof(size_t);
  171. assert_d_eq(mallctlnametomib("arena.0.initialized", mib, &miblen), 0,
  172. "Unexpected mallctlnametomib() failure");
  173. mib[1] = (size_t)arena_ind;
  174. sz = sizeof(initialized);
  175. assert_d_eq(mallctlbymib(mib, miblen, (void *)&initialized, &sz, NULL,
  176. 0), 0, "Unexpected mallctlbymib() failure");
  177. return initialized;
  178. }
  179. TEST_BEGIN(test_arena_destroy_initial) {
  180. assert_false(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),
  181. "Destroyed arena stats should not be initialized");
  182. }
  183. TEST_END
  184. TEST_BEGIN(test_arena_destroy_hooks_default) {
  185. unsigned arena_ind, arena_ind_another, arena_ind_prev;
  186. void **ptrs;
  187. unsigned nptrs;
  188. arena_ind = do_arena_create(NULL);
  189. do_arena_reset_pre(arena_ind, &ptrs, &nptrs);
  190. assert_false(arena_i_initialized(arena_ind, false),
  191. "Arena stats should not be initialized");
  192. assert_true(arena_i_initialized(arena_ind, true),
  193. "Arena stats should be initialized");
  194. /*
  195. * Create another arena before destroying one, to better verify arena
  196. * index reuse.
  197. */
  198. arena_ind_another = do_arena_create(NULL);
  199. do_arena_destroy(arena_ind);
  200. assert_false(arena_i_initialized(arena_ind, true),
  201. "Arena stats should not be initialized");
  202. assert_true(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),
  203. "Destroyed arena stats should be initialized");
  204. do_arena_reset_post(ptrs, nptrs, arena_ind);
  205. arena_ind_prev = arena_ind;
  206. arena_ind = do_arena_create(NULL);
  207. do_arena_reset_pre(arena_ind, &ptrs, &nptrs);
  208. assert_u_eq(arena_ind, arena_ind_prev,
  209. "Arena index should have been recycled");
  210. do_arena_destroy(arena_ind);
  211. do_arena_reset_post(ptrs, nptrs, arena_ind);
  212. do_arena_destroy(arena_ind_another);
  213. }
  214. TEST_END
  215. /*
  216. * Actually unmap extents, regardless of opt_retain, so that attempts to access
  217. * a destroyed arena's memory will segfault.
  218. */
  219. static bool
  220. extent_dalloc_unmap(extent_hooks_t *extent_hooks, void *addr, size_t size,
  221. bool committed, unsigned arena_ind) {
  222. TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, "
  223. "arena_ind=%u)\n", __func__, extent_hooks, addr, size, committed ?
  224. "true" : "false", arena_ind);
  225. assert_ptr_eq(extent_hooks, &hooks,
  226. "extent_hooks should be same as pointer used to set hooks");
  227. assert_ptr_eq(extent_hooks->dalloc, extent_dalloc_unmap,
  228. "Wrong hook function");
  229. called_dalloc = true;
  230. if (!try_dalloc) {
  231. return true;
  232. }
  233. pages_unmap(addr, size);
  234. did_dalloc = true;
  235. return false;
  236. }
  237. static extent_hooks_t hooks_orig;
  238. static extent_hooks_t hooks_unmap = {
  239. extent_alloc_hook,
  240. extent_dalloc_unmap, /* dalloc */
  241. extent_destroy_hook,
  242. extent_commit_hook,
  243. extent_decommit_hook,
  244. extent_purge_lazy_hook,
  245. extent_purge_forced_hook,
  246. extent_split_hook,
  247. extent_merge_hook
  248. };
  249. TEST_BEGIN(test_arena_destroy_hooks_unmap) {
  250. unsigned arena_ind;
  251. void **ptrs;
  252. unsigned nptrs;
  253. extent_hooks_prep();
  254. try_decommit = false;
  255. memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
  256. memcpy(&hooks, &hooks_unmap, sizeof(extent_hooks_t));
  257. did_alloc = false;
  258. arena_ind = do_arena_create(&hooks);
  259. do_arena_reset_pre(arena_ind, &ptrs, &nptrs);
  260. assert_true(did_alloc, "Expected alloc");
  261. assert_false(arena_i_initialized(arena_ind, false),
  262. "Arena stats should not be initialized");
  263. assert_true(arena_i_initialized(arena_ind, true),
  264. "Arena stats should be initialized");
  265. did_dalloc = false;
  266. do_arena_destroy(arena_ind);
  267. assert_true(did_dalloc, "Expected dalloc");
  268. assert_false(arena_i_initialized(arena_ind, true),
  269. "Arena stats should not be initialized");
  270. assert_true(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),
  271. "Destroyed arena stats should be initialized");
  272. do_arena_reset_post(ptrs, nptrs, arena_ind);
  273. memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
  274. }
  275. TEST_END
  276. int
  277. main(void) {
  278. return test(
  279. test_arena_reset,
  280. test_arena_destroy_initial,
  281. test_arena_destroy_hooks_default,
  282. test_arena_destroy_hooks_unmap);
  283. }