base.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "test/jemalloc_test.h"
  2. #include "test/extent_hooks.h"
  3. static extent_hooks_t hooks_null = {
  4. extent_alloc_hook,
  5. NULL, /* dalloc */
  6. NULL, /* destroy */
  7. NULL, /* commit */
  8. NULL, /* decommit */
  9. NULL, /* purge_lazy */
  10. NULL, /* purge_forced */
  11. NULL, /* split */
  12. NULL /* merge */
  13. };
  14. static extent_hooks_t hooks_not_null = {
  15. extent_alloc_hook,
  16. extent_dalloc_hook,
  17. extent_destroy_hook,
  18. NULL, /* commit */
  19. extent_decommit_hook,
  20. extent_purge_lazy_hook,
  21. extent_purge_forced_hook,
  22. NULL, /* split */
  23. NULL /* merge */
  24. };
  25. TEST_BEGIN(test_base_hooks_default) {
  26. base_t *base;
  27. size_t allocated0, allocated1, resident, mapped, n_thp;
  28. tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
  29. base = base_new(tsdn, 0,
  30. (extent_hooks_t *)&ehooks_default_extent_hooks,
  31. /* metadata_use_hooks */ true);
  32. if (config_stats) {
  33. base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
  34. &n_thp);
  35. expect_zu_ge(allocated0, sizeof(base_t),
  36. "Base header should count as allocated");
  37. if (opt_metadata_thp == metadata_thp_always) {
  38. expect_zu_gt(n_thp, 0,
  39. "Base should have 1 THP at least.");
  40. }
  41. }
  42. expect_ptr_not_null(base_alloc(tsdn, base, 42, 1),
  43. "Unexpected base_alloc() failure");
  44. if (config_stats) {
  45. base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
  46. &n_thp);
  47. expect_zu_ge(allocated1 - allocated0, 42,
  48. "At least 42 bytes were allocated by base_alloc()");
  49. }
  50. base_delete(tsdn, base);
  51. }
  52. TEST_END
  53. TEST_BEGIN(test_base_hooks_null) {
  54. extent_hooks_t hooks_orig;
  55. base_t *base;
  56. size_t allocated0, allocated1, resident, mapped, n_thp;
  57. extent_hooks_prep();
  58. try_dalloc = false;
  59. try_destroy = true;
  60. try_decommit = false;
  61. try_purge_lazy = false;
  62. try_purge_forced = false;
  63. memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
  64. memcpy(&hooks, &hooks_null, sizeof(extent_hooks_t));
  65. tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
  66. base = base_new(tsdn, 0, &hooks, /* metadata_use_hooks */ true);
  67. expect_ptr_not_null(base, "Unexpected base_new() failure");
  68. if (config_stats) {
  69. base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
  70. &n_thp);
  71. expect_zu_ge(allocated0, sizeof(base_t),
  72. "Base header should count as allocated");
  73. if (opt_metadata_thp == metadata_thp_always) {
  74. expect_zu_gt(n_thp, 0,
  75. "Base should have 1 THP at least.");
  76. }
  77. }
  78. expect_ptr_not_null(base_alloc(tsdn, base, 42, 1),
  79. "Unexpected base_alloc() failure");
  80. if (config_stats) {
  81. base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
  82. &n_thp);
  83. expect_zu_ge(allocated1 - allocated0, 42,
  84. "At least 42 bytes were allocated by base_alloc()");
  85. }
  86. base_delete(tsdn, base);
  87. memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
  88. }
  89. TEST_END
  90. TEST_BEGIN(test_base_hooks_not_null) {
  91. extent_hooks_t hooks_orig;
  92. base_t *base;
  93. void *p, *q, *r, *r_exp;
  94. extent_hooks_prep();
  95. try_dalloc = false;
  96. try_destroy = true;
  97. try_decommit = false;
  98. try_purge_lazy = false;
  99. try_purge_forced = false;
  100. memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
  101. memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));
  102. tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
  103. did_alloc = false;
  104. base = base_new(tsdn, 0, &hooks, /* metadata_use_hooks */ true);
  105. expect_ptr_not_null(base, "Unexpected base_new() failure");
  106. expect_true(did_alloc, "Expected alloc");
  107. /*
  108. * Check for tight packing at specified alignment under simple
  109. * conditions.
  110. */
  111. {
  112. const size_t alignments[] = {
  113. 1,
  114. QUANTUM,
  115. QUANTUM << 1,
  116. CACHELINE,
  117. CACHELINE << 1,
  118. };
  119. unsigned i;
  120. for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
  121. size_t alignment = alignments[i];
  122. size_t align_ceil = ALIGNMENT_CEILING(alignment,
  123. QUANTUM);
  124. p = base_alloc(tsdn, base, 1, alignment);
  125. expect_ptr_not_null(p,
  126. "Unexpected base_alloc() failure");
  127. expect_ptr_eq(p,
  128. (void *)(ALIGNMENT_CEILING((uintptr_t)p,
  129. alignment)), "Expected quantum alignment");
  130. q = base_alloc(tsdn, base, alignment, alignment);
  131. expect_ptr_not_null(q,
  132. "Unexpected base_alloc() failure");
  133. expect_ptr_eq((void *)((uintptr_t)p + align_ceil), q,
  134. "Minimal allocation should take up %zu bytes",
  135. align_ceil);
  136. r = base_alloc(tsdn, base, 1, alignment);
  137. expect_ptr_not_null(r,
  138. "Unexpected base_alloc() failure");
  139. expect_ptr_eq((void *)((uintptr_t)q + align_ceil), r,
  140. "Minimal allocation should take up %zu bytes",
  141. align_ceil);
  142. }
  143. }
  144. /*
  145. * Allocate an object that cannot fit in the first block, then verify
  146. * that the first block's remaining space is considered for subsequent
  147. * allocation.
  148. */
  149. expect_zu_ge(edata_bsize_get(&base->blocks->edata), QUANTUM,
  150. "Remainder insufficient for test");
  151. /* Use up all but one quantum of block. */
  152. while (edata_bsize_get(&base->blocks->edata) > QUANTUM) {
  153. p = base_alloc(tsdn, base, QUANTUM, QUANTUM);
  154. expect_ptr_not_null(p, "Unexpected base_alloc() failure");
  155. }
  156. r_exp = edata_addr_get(&base->blocks->edata);
  157. expect_zu_eq(base->extent_sn_next, 1, "One extant block expected");
  158. q = base_alloc(tsdn, base, QUANTUM + 1, QUANTUM);
  159. expect_ptr_not_null(q, "Unexpected base_alloc() failure");
  160. expect_ptr_ne(q, r_exp, "Expected allocation from new block");
  161. expect_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");
  162. r = base_alloc(tsdn, base, QUANTUM, QUANTUM);
  163. expect_ptr_not_null(r, "Unexpected base_alloc() failure");
  164. expect_ptr_eq(r, r_exp, "Expected allocation from first block");
  165. expect_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");
  166. /*
  167. * Check for proper alignment support when normal blocks are too small.
  168. */
  169. {
  170. const size_t alignments[] = {
  171. HUGEPAGE,
  172. HUGEPAGE << 1
  173. };
  174. unsigned i;
  175. for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
  176. size_t alignment = alignments[i];
  177. p = base_alloc(tsdn, base, QUANTUM, alignment);
  178. expect_ptr_not_null(p,
  179. "Unexpected base_alloc() failure");
  180. expect_ptr_eq(p,
  181. (void *)(ALIGNMENT_CEILING((uintptr_t)p,
  182. alignment)), "Expected %zu-byte alignment",
  183. alignment);
  184. }
  185. }
  186. called_dalloc = called_destroy = called_decommit = called_purge_lazy =
  187. called_purge_forced = false;
  188. base_delete(tsdn, base);
  189. expect_true(called_dalloc, "Expected dalloc call");
  190. expect_true(!called_destroy, "Unexpected destroy call");
  191. expect_true(called_decommit, "Expected decommit call");
  192. expect_true(called_purge_lazy, "Expected purge_lazy call");
  193. expect_true(called_purge_forced, "Expected purge_forced call");
  194. try_dalloc = true;
  195. try_destroy = true;
  196. try_decommit = true;
  197. try_purge_lazy = true;
  198. try_purge_forced = true;
  199. memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
  200. }
  201. TEST_END
  202. TEST_BEGIN(test_base_ehooks_get_for_metadata_default_hook) {
  203. extent_hooks_prep();
  204. memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));
  205. base_t *base;
  206. tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
  207. base = base_new(tsdn, 0, &hooks, /* metadata_use_hooks */ false);
  208. ehooks_t *ehooks = base_ehooks_get_for_metadata(base);
  209. expect_true(ehooks_are_default(ehooks),
  210. "Expected default extent hook functions pointer");
  211. base_delete(tsdn, base);
  212. }
  213. TEST_END
  214. TEST_BEGIN(test_base_ehooks_get_for_metadata_custom_hook) {
  215. extent_hooks_prep();
  216. memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));
  217. base_t *base;
  218. tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
  219. base = base_new(tsdn, 0, &hooks, /* metadata_use_hooks */ true);
  220. ehooks_t *ehooks = base_ehooks_get_for_metadata(base);
  221. expect_ptr_eq(&hooks, ehooks_get_extent_hooks_ptr(ehooks),
  222. "Expected user-specified extend hook functions pointer");
  223. base_delete(tsdn, base);
  224. }
  225. TEST_END
  226. int
  227. main(void) {
  228. return test(
  229. test_base_hooks_default,
  230. test_base_hooks_null,
  231. test_base_hooks_not_null,
  232. test_base_ehooks_get_for_metadata_default_hook,
  233. test_base_ehooks_get_for_metadata_custom_hook);
  234. }