Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #include <stdlib.h> #include <../src/internal.h> #include <darwintest.h> #if !MALLOC_TARGET_EXCLAVES #include <mach/vm_page_size.h> #include <perfcheck_keys.h> #else #include <time.h> #endif // !MALLOC_TARGET_EXCLAVES #pragma mark - #pragma mark Helper code #define PAGE_SPREAD 8 #define MAX_NAME_SIZE 256 #define OUTLIER_TIME 20000 // This value is a guess that will be refined over time. #define PERFCHECK_THRESHOLD_PCT 10.0 static char name[MAX_NAME_SIZE]; #define NUMBER_OF_SAMPLES_FOR_BATCH(s) _dt_stat_batch_size((dt_stat_t)s) // Measures the time required to realloc a block of memory of given size // by a specified amount. static void realloc_by_amount(const char *base_metric_name, size_t size, ssize_t amount, bool amount_is_random) { #if !MALLOC_TARGET_EXCLAVES if (amount_is_random) { snprintf(name, MAX_NAME_SIZE, amount >= 0 ? "%s_from_%llu_up_random" : "%s_from_%llu_down_random", base_metric_name, (uint64_t)size); } else { snprintf(name, MAX_NAME_SIZE, amount >= 0 ? "%s_from_%llu_up_%ld" : "%s_from_%llu_down_%ld", base_metric_name, (uint64_t)size, labs(amount)); } #endif // !MALLOC_TARGET_EXCLAVES uint64_t total_time = 0; int count = 0; #if !MALLOC_TARGET_EXCLAVES dt_stat_time_t s = dt_stat_time_create(name); dt_stat_set_variable((dt_stat_t)s, "size (bytes)", (unsigned int)size); dt_stat_set_variable((dt_stat_t)s, "amount (bytes)", (int)amount); dt_stat_token now = dt_stat_time_begin(s); #endif // !MALLOC_TARGET_EXCLAVES for (;;) { void *ptr = malloc(size); T_QUIET; T_ASSERT_NOTNULL(ptr, "malloc for size %llu failed", (uint64_t)size); #if MALLOC_TARGET_EXCLAVES struct timespec start, end; int rc_start = clock_gettime(CLOCK_MONOTONIC_RAW, &start); ptr = realloc(ptr, size + amount); int rc_end = clock_gettime(CLOCK_MONOTONIC_RAW, &end); const uint64_t _NSEC_PER_SEC = 1000000000ull; uint64_t start_nsec = start.tv_sec * _NSEC_PER_SEC + start.tv_nsec; uint64_t end_nsec = end.tv_sec * _NSEC_PER_SEC + end.tv_nsec; total_time += end_nsec - start_nsec; #else dt_stat_token start = dt_stat_time_begin(s); ptr = realloc(ptr, size + amount); dt_stat_token end = dt_stat_time_begin(s); total_time += end - start; #endif // MALLOC_TARGET_EXCLAVES T_QUIET; T_ASSERT_NOTNULL(ptr, "realloc from size %llu to size %llu failed", (uint64_t)size, (uint64_t)(size + amount)); free(ptr); #if MALLOC_TARGET_EXCLAVES // On exclaves, run this test for the very arbitrary 10ms, or at // least 5 loops if (++count >= 5 && total_time >= (_NSEC_PER_SEC / 100)) { T_LOG("Ran realloc test for %d iterations, average time %llu", count, total_time / count); break; } #else if (++count >= NUMBER_OF_SAMPLES_FOR_BATCH(s)) { // Discard outliers or the test won't converge -- this should // be done in libdarwintest if (total_time/count < OUTLIER_TIME) { dt_stat_mach_time_add_batch(s, count, total_time); } total_time = 0; count = 0; if (dt_stat_stable(s)) { break; } } #endif // MALLOC_TARGET_EXCLAVES } #if !MALLOC_TARGET_EXCLAVES dt_stat_finalize(s); #endif // !MALLOC_TARGET_EXCLAVES } // Times a set of size adjustments from a given base. static void realloc_test_set(const char *base_name, size_t start_size) { ssize_t adj = 1; realloc_by_amount(base_name, start_size, adj, false); realloc_by_amount(base_name, start_size + adj, -adj, false); adj = 8; realloc_by_amount(base_name, start_size, adj, false); realloc_by_amount(base_name, start_size + adj, -adj, false); adj = 16; realloc_by_amount(base_name, start_size, adj, false); realloc_by_amount(base_name, start_size + adj, -adj, false); #if INCLUDE_RANDOM_SIZE_TESTS adj = 1 + arc4random_uniform(vm_page_size - 1); realloc_by_amount(base_name, start_size, adj, true); realloc_by_amount(base_name, start_size + adj, -adj, true); uint32_t pages = 1 + arc4random_uniform(PAGE_SPREAD); adj = pages * vm_page_size; realloc_by_amount(base_name, start_size, adj, true); realloc_by_amount(base_name, start_size + adj, -adj, true); #else // INCLUDE_RANDOM_SIZE_TESTS T_LOG("Skipping random size tests"); #endif // INCLUDE_RANDOM_SIZE_TESTS adj = vm_page_size; realloc_by_amount(base_name, start_size, adj, false); realloc_by_amount(base_name, start_size + adj, -adj, false); adj = 4 * vm_page_size; realloc_by_amount(base_name, start_size, adj, false); realloc_by_amount(base_name, start_size + adj, -adj, false); // Adjust up and then down by over half the size of the allocation -- // this skips the fast path in some allocators. adj = 4 * vm_page_size; realloc_by_amount(base_name, start_size, adj, false); realloc_by_amount(base_name, start_size + adj, -(((ssize_t)start_size) + adj)/2 - 1, false); adj = 32; realloc_by_amount(base_name, start_size, adj, false); realloc_by_amount(base_name, start_size + adj, -(((ssize_t)start_size) + adj)/2 - 1, false); } static void realloc_tests(const char *base_name, boolean_t using_nano) { // tiny or nano realloc_test_set(using_nano ? base_name : "Tiny", 8); if (!using_nano) { // No point in running these tests three times. // tiny realloc_test_set("Tiny", 512); // small realloc_test_set("Small", 2048); // large realloc_test_set("Large", 128 * 1024); } T_END; } #pragma mark - #pragma mark Tests for realloc() T_DECL(realloc_perf_base, "realloc without nano", T_META_TAG_PERF, T_META_TAG_VM_NOT_PREFERRED, T_META_ENVVAR("MallocNanoZone=0")) { realloc_tests("NoNano", false); } T_DECL(realloc_perf_nanov2, "realloc with nanoV2", T_META_TAG_PERF, T_META_TAG_VM_NOT_PREFERRED, T_META_ENVVAR("MallocNanoZone=V2")) { #if CONFIG_NANOZONE realloc_tests("Nanov2", true); #else // CONFIG_NANOZONE T_SKIP("Nano allocator not configured"); #endif // CONFIG_NANOZONE } |