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 | #include <stdlib.h> #include <mach/vm_page_size.h> #include <../src/internal.h> #include <darwintest.h> #include <perfcheck_keys.h> #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 (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)); } uint64_t total_time = 0; int count = 0; 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); for (;;) { void *ptr = malloc(size); T_QUIET; T_ASSERT_NOTNULL(ptr, "malloc for size %llu failed", (uint64_t)size); dt_stat_token start = dt_stat_time_begin(s); ptr = realloc(ptr, size + amount); dt_stat_token end = dt_stat_time_begin(s); T_QUIET; T_ASSERT_NOTNULL(ptr, "realloc from size %llu to size %llu failed", (uint64_t)size, (uint64_t)(size + amount)); total_time += end - start; free(ptr); 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; } } } dt_stat_finalize(s); } // 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_ENVVAR("MallocNanoZone=0")) { realloc_tests("NoNano", false); } T_DECL(realloc_perf_nanov2, "realloc with nanoV2", T_META_TAG_PERF, T_META_ENVVAR("MallocNanoZone=V2")) { #if CONFIG_NANOZONE realloc_tests("Nanov2", true); #else // CONFIG_NANOZONE T_SKIP("Nano allocator not configured"); #endif // CONFIG_NANOZONE } |