huge.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "test/jemalloc_test.h"
  2. /* Threshold: 2 << 20 = 2097152. */
  3. const char *malloc_conf = "oversize_threshold:2097152";
  4. #define HUGE_SZ (2 << 20)
  5. #define SMALL_SZ (8)
  6. TEST_BEGIN(huge_bind_thread) {
  7. unsigned arena1, arena2;
  8. size_t sz = sizeof(unsigned);
  9. /* Bind to a manual arena. */
  10. expect_d_eq(mallctl("arenas.create", &arena1, &sz, NULL, 0), 0,
  11. "Failed to create arena");
  12. expect_d_eq(mallctl("thread.arena", NULL, NULL, &arena1,
  13. sizeof(arena1)), 0, "Fail to bind thread");
  14. void *ptr = mallocx(HUGE_SZ, 0);
  15. expect_ptr_not_null(ptr, "Fail to allocate huge size");
  16. expect_d_eq(mallctl("arenas.lookup", &arena2, &sz, &ptr,
  17. sizeof(ptr)), 0, "Unexpected mallctl() failure");
  18. expect_u_eq(arena1, arena2, "Wrong arena used after binding");
  19. dallocx(ptr, 0);
  20. /* Switch back to arena 0. */
  21. test_skip_if(have_percpu_arena &&
  22. PERCPU_ARENA_ENABLED(opt_percpu_arena));
  23. arena2 = 0;
  24. expect_d_eq(mallctl("thread.arena", NULL, NULL, &arena2,
  25. sizeof(arena2)), 0, "Fail to bind thread");
  26. ptr = mallocx(SMALL_SZ, MALLOCX_TCACHE_NONE);
  27. expect_d_eq(mallctl("arenas.lookup", &arena2, &sz, &ptr,
  28. sizeof(ptr)), 0, "Unexpected mallctl() failure");
  29. expect_u_eq(arena2, 0, "Wrong arena used after binding");
  30. dallocx(ptr, MALLOCX_TCACHE_NONE);
  31. /* Then huge allocation should use the huge arena. */
  32. ptr = mallocx(HUGE_SZ, 0);
  33. expect_ptr_not_null(ptr, "Fail to allocate huge size");
  34. expect_d_eq(mallctl("arenas.lookup", &arena2, &sz, &ptr,
  35. sizeof(ptr)), 0, "Unexpected mallctl() failure");
  36. expect_u_ne(arena2, 0, "Wrong arena used after binding");
  37. expect_u_ne(arena1, arena2, "Wrong arena used after binding");
  38. dallocx(ptr, 0);
  39. }
  40. TEST_END
  41. TEST_BEGIN(huge_mallocx) {
  42. unsigned arena1, arena2;
  43. size_t sz = sizeof(unsigned);
  44. expect_d_eq(mallctl("arenas.create", &arena1, &sz, NULL, 0), 0,
  45. "Failed to create arena");
  46. void *huge = mallocx(HUGE_SZ, MALLOCX_ARENA(arena1));
  47. expect_ptr_not_null(huge, "Fail to allocate huge size");
  48. expect_d_eq(mallctl("arenas.lookup", &arena2, &sz, &huge,
  49. sizeof(huge)), 0, "Unexpected mallctl() failure");
  50. expect_u_eq(arena1, arena2, "Wrong arena used for mallocx");
  51. dallocx(huge, MALLOCX_ARENA(arena1));
  52. void *huge2 = mallocx(HUGE_SZ, 0);
  53. expect_ptr_not_null(huge, "Fail to allocate huge size");
  54. expect_d_eq(mallctl("arenas.lookup", &arena2, &sz, &huge2,
  55. sizeof(huge2)), 0, "Unexpected mallctl() failure");
  56. expect_u_ne(arena1, arena2,
  57. "Huge allocation should not come from the manual arena.");
  58. expect_u_ne(arena2, 0,
  59. "Huge allocation should not come from the arena 0.");
  60. dallocx(huge2, 0);
  61. }
  62. TEST_END
  63. TEST_BEGIN(huge_allocation) {
  64. unsigned arena1, arena2;
  65. void *ptr = mallocx(HUGE_SZ, 0);
  66. expect_ptr_not_null(ptr, "Fail to allocate huge size");
  67. size_t sz = sizeof(unsigned);
  68. expect_d_eq(mallctl("arenas.lookup", &arena1, &sz, &ptr, sizeof(ptr)),
  69. 0, "Unexpected mallctl() failure");
  70. expect_u_gt(arena1, 0, "Huge allocation should not come from arena 0");
  71. dallocx(ptr, 0);
  72. ptr = mallocx(HUGE_SZ >> 1, 0);
  73. expect_ptr_not_null(ptr, "Fail to allocate half huge size");
  74. expect_d_eq(mallctl("arenas.lookup", &arena2, &sz, &ptr,
  75. sizeof(ptr)), 0, "Unexpected mallctl() failure");
  76. expect_u_ne(arena1, arena2, "Wrong arena used for half huge");
  77. dallocx(ptr, 0);
  78. ptr = mallocx(SMALL_SZ, MALLOCX_TCACHE_NONE);
  79. expect_ptr_not_null(ptr, "Fail to allocate small size");
  80. expect_d_eq(mallctl("arenas.lookup", &arena2, &sz, &ptr,
  81. sizeof(ptr)), 0, "Unexpected mallctl() failure");
  82. expect_u_ne(arena1, arena2,
  83. "Huge and small should be from different arenas");
  84. dallocx(ptr, 0);
  85. }
  86. TEST_END
  87. int
  88. main(void) {
  89. return test(
  90. huge_allocation,
  91. huge_mallocx,
  92. huge_bind_thread);
  93. }