2
0

extent_quantize.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_small_extent_size) {
  3. unsigned nbins, i;
  4. size_t sz, extent_size;
  5. size_t mib[4];
  6. size_t miblen = sizeof(mib) / sizeof(size_t);
  7. /*
  8. * Iterate over all small size classes, get their extent sizes, and
  9. * verify that the quantized size is the same as the extent size.
  10. */
  11. sz = sizeof(unsigned);
  12. assert_d_eq(mallctl("arenas.nbins", (void *)&nbins, &sz, NULL, 0), 0,
  13. "Unexpected mallctl failure");
  14. assert_d_eq(mallctlnametomib("arenas.bin.0.slab_size", mib, &miblen), 0,
  15. "Unexpected mallctlnametomib failure");
  16. for (i = 0; i < nbins; i++) {
  17. mib[2] = i;
  18. sz = sizeof(size_t);
  19. assert_d_eq(mallctlbymib(mib, miblen, (void *)&extent_size, &sz,
  20. NULL, 0), 0, "Unexpected mallctlbymib failure");
  21. assert_zu_eq(extent_size,
  22. extent_size_quantize_floor(extent_size),
  23. "Small extent quantization should be a no-op "
  24. "(extent_size=%zu)", extent_size);
  25. assert_zu_eq(extent_size,
  26. extent_size_quantize_ceil(extent_size),
  27. "Small extent quantization should be a no-op "
  28. "(extent_size=%zu)", extent_size);
  29. }
  30. }
  31. TEST_END
  32. TEST_BEGIN(test_large_extent_size) {
  33. bool cache_oblivious;
  34. unsigned nlextents, i;
  35. size_t sz, extent_size_prev, ceil_prev;
  36. size_t mib[4];
  37. size_t miblen = sizeof(mib) / sizeof(size_t);
  38. /*
  39. * Iterate over all large size classes, get their extent sizes, and
  40. * verify that the quantized size is the same as the extent size.
  41. */
  42. sz = sizeof(bool);
  43. assert_d_eq(mallctl("config.cache_oblivious", (void *)&cache_oblivious,
  44. &sz, NULL, 0), 0, "Unexpected mallctl failure");
  45. sz = sizeof(unsigned);
  46. assert_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
  47. 0), 0, "Unexpected mallctl failure");
  48. assert_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
  49. "Unexpected mallctlnametomib failure");
  50. for (i = 0; i < nlextents; i++) {
  51. size_t lextent_size, extent_size, floor, ceil;
  52. mib[2] = i;
  53. sz = sizeof(size_t);
  54. assert_d_eq(mallctlbymib(mib, miblen, (void *)&lextent_size,
  55. &sz, NULL, 0), 0, "Unexpected mallctlbymib failure");
  56. extent_size = cache_oblivious ? lextent_size + PAGE :
  57. lextent_size;
  58. floor = extent_size_quantize_floor(extent_size);
  59. ceil = extent_size_quantize_ceil(extent_size);
  60. assert_zu_eq(extent_size, floor,
  61. "Extent quantization should be a no-op for precise size "
  62. "(lextent_size=%zu, extent_size=%zu)", lextent_size,
  63. extent_size);
  64. assert_zu_eq(extent_size, ceil,
  65. "Extent quantization should be a no-op for precise size "
  66. "(lextent_size=%zu, extent_size=%zu)", lextent_size,
  67. extent_size);
  68. if (i > 0) {
  69. assert_zu_eq(extent_size_prev,
  70. extent_size_quantize_floor(extent_size - PAGE),
  71. "Floor should be a precise size");
  72. if (extent_size_prev < ceil_prev) {
  73. assert_zu_eq(ceil_prev, extent_size,
  74. "Ceiling should be a precise size "
  75. "(extent_size_prev=%zu, ceil_prev=%zu, "
  76. "extent_size=%zu)", extent_size_prev,
  77. ceil_prev, extent_size);
  78. }
  79. }
  80. if (i + 1 < nlextents) {
  81. extent_size_prev = floor;
  82. ceil_prev = extent_size_quantize_ceil(extent_size +
  83. PAGE);
  84. }
  85. }
  86. }
  87. TEST_END
  88. TEST_BEGIN(test_monotonic) {
  89. #define SZ_MAX ZU(4 * 1024 * 1024)
  90. unsigned i;
  91. size_t floor_prev, ceil_prev;
  92. floor_prev = 0;
  93. ceil_prev = 0;
  94. for (i = 1; i <= SZ_MAX >> LG_PAGE; i++) {
  95. size_t extent_size, floor, ceil;
  96. extent_size = i << LG_PAGE;
  97. floor = extent_size_quantize_floor(extent_size);
  98. ceil = extent_size_quantize_ceil(extent_size);
  99. assert_zu_le(floor, extent_size,
  100. "Floor should be <= (floor=%zu, extent_size=%zu, ceil=%zu)",
  101. floor, extent_size, ceil);
  102. assert_zu_ge(ceil, extent_size,
  103. "Ceiling should be >= (floor=%zu, extent_size=%zu, "
  104. "ceil=%zu)", floor, extent_size, ceil);
  105. assert_zu_le(floor_prev, floor, "Floor should be monotonic "
  106. "(floor_prev=%zu, floor=%zu, extent_size=%zu, ceil=%zu)",
  107. floor_prev, floor, extent_size, ceil);
  108. assert_zu_le(ceil_prev, ceil, "Ceiling should be monotonic "
  109. "(floor=%zu, extent_size=%zu, ceil_prev=%zu, ceil=%zu)",
  110. floor, extent_size, ceil_prev, ceil);
  111. floor_prev = floor;
  112. ceil_prev = ceil;
  113. }
  114. }
  115. TEST_END
  116. int
  117. main(void) {
  118. return test(
  119. test_small_extent_size,
  120. test_large_extent_size,
  121. test_monotonic);
  122. }