slab_sizes.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "test/jemalloc_test.h"
  2. /* Note that this test relies on the unusual slab sizes set in slab_sizes.sh. */
  3. TEST_BEGIN(test_slab_sizes) {
  4. unsigned nbins;
  5. size_t page;
  6. size_t sizemib[4];
  7. size_t slabmib[4];
  8. size_t len;
  9. len = sizeof(nbins);
  10. assert_d_eq(mallctl("arenas.nbins", &nbins, &len, NULL, 0), 0,
  11. "nbins mallctl failure");
  12. len = sizeof(page);
  13. assert_d_eq(mallctl("arenas.page", &page, &len, NULL, 0), 0,
  14. "page mallctl failure");
  15. len = 4;
  16. assert_d_eq(mallctlnametomib("arenas.bin.0.size", sizemib, &len), 0,
  17. "bin size mallctlnametomib failure");
  18. len = 4;
  19. assert_d_eq(mallctlnametomib("arenas.bin.0.slab_size", slabmib, &len),
  20. 0, "slab size mallctlnametomib failure");
  21. size_t biggest_slab_seen = 0;
  22. for (unsigned i = 0; i < nbins; i++) {
  23. size_t bin_size;
  24. size_t slab_size;
  25. len = sizeof(size_t);
  26. sizemib[2] = i;
  27. slabmib[2] = i;
  28. assert_d_eq(mallctlbymib(sizemib, 4, (void *)&bin_size, &len,
  29. NULL, 0), 0, "bin size mallctlbymib failure");
  30. len = sizeof(size_t);
  31. assert_d_eq(mallctlbymib(slabmib, 4, (void *)&slab_size, &len,
  32. NULL, 0), 0, "slab size mallctlbymib failure");
  33. if (bin_size < 100) {
  34. /*
  35. * Then we should be as close to 17 as possible. Since
  36. * not all page sizes are valid (because of bitmap
  37. * limitations on the number of items in a slab), we
  38. * should at least make sure that the number of pages
  39. * goes up.
  40. */
  41. assert_zu_ge(slab_size, biggest_slab_seen,
  42. "Slab sizes should go up");
  43. biggest_slab_seen = slab_size;
  44. } else if (
  45. (100 <= bin_size && bin_size < 128)
  46. || (128 < bin_size && bin_size <= 200)) {
  47. assert_zu_eq(slab_size, page,
  48. "Forced-small slabs should be small");
  49. } else if (bin_size == 128) {
  50. assert_zu_eq(slab_size, 2 * page,
  51. "Forced-2-page slab should be 2 pages");
  52. } else if (200 < bin_size && bin_size <= 4096) {
  53. assert_zu_ge(slab_size, biggest_slab_seen,
  54. "Slab sizes should go up");
  55. biggest_slab_seen = slab_size;
  56. }
  57. }
  58. /*
  59. * For any reasonable configuration, 17 pages should be a valid slab
  60. * size for 4096-byte items.
  61. */
  62. assert_zu_eq(biggest_slab_seen, 17 * page, "Didn't hit page target");
  63. }
  64. TEST_END
  65. int
  66. main(void) {
  67. return test(
  68. test_slab_sizes);
  69. }