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