prof_stats.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "test/jemalloc_test.h"
  2. #define N_PTRS 3
  3. static void
  4. test_combinations(szind_t ind, size_t sizes_array[N_PTRS],
  5. int flags_array[N_PTRS]) {
  6. #define MALLCTL_STR_LEN 64
  7. assert(opt_prof && opt_prof_stats);
  8. char mallctl_live_str[MALLCTL_STR_LEN];
  9. char mallctl_accum_str[MALLCTL_STR_LEN];
  10. if (ind < SC_NBINS) {
  11. malloc_snprintf(mallctl_live_str, MALLCTL_STR_LEN,
  12. "prof.stats.bins.%u.live", (unsigned)ind);
  13. malloc_snprintf(mallctl_accum_str, MALLCTL_STR_LEN,
  14. "prof.stats.bins.%u.accum", (unsigned)ind);
  15. } else {
  16. malloc_snprintf(mallctl_live_str, MALLCTL_STR_LEN,
  17. "prof.stats.lextents.%u.live", (unsigned)(ind - SC_NBINS));
  18. malloc_snprintf(mallctl_accum_str, MALLCTL_STR_LEN,
  19. "prof.stats.lextents.%u.accum", (unsigned)(ind - SC_NBINS));
  20. }
  21. size_t stats_len = 2 * sizeof(uint64_t);
  22. uint64_t live_stats_orig[2];
  23. assert_d_eq(mallctl(mallctl_live_str, &live_stats_orig, &stats_len,
  24. NULL, 0), 0, "");
  25. uint64_t accum_stats_orig[2];
  26. assert_d_eq(mallctl(mallctl_accum_str, &accum_stats_orig, &stats_len,
  27. NULL, 0), 0, "");
  28. void *ptrs[N_PTRS];
  29. uint64_t live_req_sum = 0;
  30. uint64_t live_count = 0;
  31. uint64_t accum_req_sum = 0;
  32. uint64_t accum_count = 0;
  33. for (size_t i = 0; i < N_PTRS; ++i) {
  34. size_t sz = sizes_array[i];
  35. int flags = flags_array[i];
  36. void *p = mallocx(sz, flags);
  37. assert_ptr_not_null(p, "malloc() failed");
  38. assert(TEST_MALLOC_SIZE(p) == sz_index2size(ind));
  39. ptrs[i] = p;
  40. live_req_sum += sz;
  41. live_count++;
  42. accum_req_sum += sz;
  43. accum_count++;
  44. uint64_t live_stats[2];
  45. assert_d_eq(mallctl(mallctl_live_str, &live_stats, &stats_len,
  46. NULL, 0), 0, "");
  47. expect_u64_eq(live_stats[0] - live_stats_orig[0],
  48. live_req_sum, "");
  49. expect_u64_eq(live_stats[1] - live_stats_orig[1],
  50. live_count, "");
  51. uint64_t accum_stats[2];
  52. assert_d_eq(mallctl(mallctl_accum_str, &accum_stats, &stats_len,
  53. NULL, 0), 0, "");
  54. expect_u64_eq(accum_stats[0] - accum_stats_orig[0],
  55. accum_req_sum, "");
  56. expect_u64_eq(accum_stats[1] - accum_stats_orig[1],
  57. accum_count, "");
  58. }
  59. for (size_t i = 0; i < N_PTRS; ++i) {
  60. size_t sz = sizes_array[i];
  61. int flags = flags_array[i];
  62. sdallocx(ptrs[i], sz, flags);
  63. live_req_sum -= sz;
  64. live_count--;
  65. uint64_t live_stats[2];
  66. assert_d_eq(mallctl(mallctl_live_str, &live_stats, &stats_len,
  67. NULL, 0), 0, "");
  68. expect_u64_eq(live_stats[0] - live_stats_orig[0],
  69. live_req_sum, "");
  70. expect_u64_eq(live_stats[1] - live_stats_orig[1],
  71. live_count, "");
  72. uint64_t accum_stats[2];
  73. assert_d_eq(mallctl(mallctl_accum_str, &accum_stats, &stats_len,
  74. NULL, 0), 0, "");
  75. expect_u64_eq(accum_stats[0] - accum_stats_orig[0],
  76. accum_req_sum, "");
  77. expect_u64_eq(accum_stats[1] - accum_stats_orig[1],
  78. accum_count, "");
  79. }
  80. #undef MALLCTL_STR_LEN
  81. }
  82. static void
  83. test_szind_wrapper(szind_t ind) {
  84. size_t sizes_array[N_PTRS];
  85. int flags_array[N_PTRS];
  86. for (size_t i = 0, sz = sz_index2size(ind) - N_PTRS; i < N_PTRS;
  87. ++i, ++sz) {
  88. sizes_array[i] = sz;
  89. flags_array[i] = 0;
  90. }
  91. test_combinations(ind, sizes_array, flags_array);
  92. }
  93. TEST_BEGIN(test_prof_stats) {
  94. test_skip_if(!config_prof);
  95. test_szind_wrapper(0);
  96. test_szind_wrapper(1);
  97. test_szind_wrapper(2);
  98. test_szind_wrapper(SC_NBINS);
  99. test_szind_wrapper(SC_NBINS + 1);
  100. test_szind_wrapper(SC_NBINS + 2);
  101. }
  102. TEST_END
  103. static void
  104. test_szind_aligned_wrapper(szind_t ind, unsigned lg_align) {
  105. size_t sizes_array[N_PTRS];
  106. int flags_array[N_PTRS];
  107. int flags = MALLOCX_LG_ALIGN(lg_align);
  108. for (size_t i = 0, sz = sz_index2size(ind) - N_PTRS; i < N_PTRS;
  109. ++i, ++sz) {
  110. sizes_array[i] = sz;
  111. flags_array[i] = flags;
  112. }
  113. test_combinations(
  114. sz_size2index(sz_sa2u(sz_index2size(ind), 1 << lg_align)),
  115. sizes_array, flags_array);
  116. }
  117. TEST_BEGIN(test_prof_stats_aligned) {
  118. test_skip_if(!config_prof);
  119. for (szind_t ind = 0; ind < 10; ++ind) {
  120. for (unsigned lg_align = 0; lg_align < 10; ++lg_align) {
  121. test_szind_aligned_wrapper(ind, lg_align);
  122. }
  123. }
  124. for (szind_t ind = SC_NBINS - 5; ind < SC_NBINS + 5; ++ind) {
  125. for (unsigned lg_align = SC_LG_LARGE_MINCLASS - 5;
  126. lg_align < SC_LG_LARGE_MINCLASS + 5; ++lg_align) {
  127. test_szind_aligned_wrapper(ind, lg_align);
  128. }
  129. }
  130. }
  131. TEST_END
  132. int
  133. main(void) {
  134. return test(
  135. test_prof_stats,
  136. test_prof_stats_aligned);
  137. }