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 | #include <darwintest.h> #include <dispatch/dispatch.h> #include <malloc/malloc.h> #include <os/lock.h> #include <stdlib.h> #include <sys/queue.h> #if TARGET_OS_WATCH #define TEST_TIMEOUT 1200 #endif // TARGET_OS_WATCH TAILQ_HEAD(thead, entry); struct entry { TAILQ_ENTRY(entry) next; }; static void stress(size_t sz, size_t cnt) { struct thead head = TAILQ_HEAD_INITIALIZER(head); TAILQ_INIT(&head); for (int t=0; t<100; t++) { for (int i=0; i<cnt; i++) { struct entry *p = calloc(1, sz); T_QUIET; T_ASSERT_NOTNULL(p, "Failed to make allocation with size %zu", sz); TAILQ_INSERT_TAIL(&head, p, next); } int i=0; struct entry *p; while ((p = TAILQ_FIRST(&head)) != NULL) { TAILQ_REMOVE(&head, p, next); free((void *)p); i++; } } } T_DECL(tiny_mem_pressure, "tiny memory pressure", #if TARGET_OS_WATCH T_META_TIMEOUT(TEST_TIMEOUT), #endif // TARGET_OS_WATCH T_META_ENVVAR("MallocDebugReport=stderr"), T_META_ENVVAR("MallocScribble=1"), T_META_ENVVAR("MallocSpaceEfficient=1"), T_META_ENVVAR("MallocMaxMagazines=1"), T_META_CHECK_LEAKS(false)) { dispatch_queue_t q = dispatch_queue_create("pressure queue", 0); // serial dispatch_async(q, ^{ while (1) { malloc_zone_pressure_relief(0, 0); usleep(100000); } }); stress(128, 50000); T_PASS("didn't crash"); } T_DECL(small_mem_pressure, "small memory pressure thread", #if TARGET_OS_WATCH T_META_TIMEOUT(TEST_TIMEOUT), #endif // TARGET_OS_WATCH #if TARGET_OS_OSX T_META_ALL_VALID_ARCHS(true), // test Rosetta // darwintest multi-arch support relies on the first line of stderr // being reserved for arch(1) complaining about a given slice being // unsupported, so we can only put the malloc debug reporting on stderr // when we don't need that T_META_ENVVAR("MallocDebugReport=none"), #else // TARGET_OS_OSX T_META_ENVVAR("MallocDebugReport=stderr"), #endif // TARGET_OS_OSX T_META_ENVVAR("MallocScribble=1"), T_META_ENVVAR("MallocSpaceEfficient=1"), T_META_ENVVAR("MallocMaxMagazines=1"), T_META_CHECK_LEAKS(false)) { dispatch_queue_t q = dispatch_queue_create("pressure queue", 0); // serial dispatch_async(q, ^{ while (1) { malloc_zone_pressure_relief(0, 0); usleep(10000); } }); stress(512, 20000); T_PASS("didn't crash"); } // Disabled until rdar://83904507 is fixed // // Need to compile the test out entirely because T_META_MAYFAIL doesn't handle // test crashes - rdar://86164532 #if 0 T_DECL(medium_mem_pressure, "medium memory pressure thread", #if TARGET_OS_WATCH T_META_TIMEOUT(TEST_TIMEOUT), #endif // TARGET_OS_WATCH T_META_ENVVAR("MallocDebugReport=stderr"), T_META_ENVVAR("MallocScribble=1"), T_META_ENVVAR("MallocSpaceEfficient=1"), T_META_ENVVAR("MallocMaxMagazines=1"), T_META_MAYFAIL("Disabled until rdar://83904507 is fixed"), T_META_CHECK_LEAKS(false)) { dispatch_queue_t q = dispatch_queue_create("pressure queue", 0); // serial dispatch_async(q, ^{ while (1) { malloc_zone_pressure_relief(0, 0); usleep(100000); } }); stress(64*1024, 1000); T_PASS("didn't crash"); } #endif T_DECL(tiny_mem_pressure_multi, "test memory pressure in tiny on threads", #if TARGET_OS_WATCH T_META_TIMEOUT(TEST_TIMEOUT), #endif // TARGET_OS_WATCH T_META_CHECK_LEAKS(false)) { dispatch_group_t g = dispatch_group_create(); for (int i=0; i<16; i++) { dispatch_group_async(g, dispatch_get_global_queue(0, 0), ^{ stress(128, 100000); }); } dispatch_group_notify(g, dispatch_get_global_queue(0, 0), ^{ T_PASS("didn't crash!"); T_END; }); dispatch_release(g); while (1) { T_LOG("malloc_zone_pressure_relief"); malloc_zone_pressure_relief(malloc_default_zone(), 0); sleep(1); } } T_DECL(small_mem_pressure_multi, "test memory pressure in small on threads", #if TARGET_OS_WATCH T_META_TIMEOUT(TEST_TIMEOUT), #endif // TARGET_OS_WATCH T_META_CHECK_LEAKS(false)) { dispatch_group_t g = dispatch_group_create(); for (int i=0; i<3; i++) { dispatch_group_async(g, dispatch_get_global_queue(0, 0), ^{ stress(1024, 100000); }); } dispatch_group_notify(g, dispatch_get_global_queue(0, 0), ^{ T_PASS("didn't crash!"); T_END; }); dispatch_release(g); while (1) { T_LOG("malloc_zone_pressure_relief"); malloc_zone_pressure_relief(malloc_default_zone(), 0); sleep(1); } } T_DECL(medium_mem_pressure_multi, "test memory pressure in medium on threads", #if TARGET_OS_WATCH T_META_TIMEOUT(TEST_TIMEOUT), #endif // TARGET_OS_WATCH T_META_CHECK_LEAKS(false)) { dispatch_group_t g = dispatch_group_create(); for (int i=0; i<30; i++) { dispatch_group_async(g, dispatch_get_global_queue(0, 0), ^{ stress(64*1024, 1000); }); } dispatch_group_notify(g, dispatch_get_global_queue(0, 0), ^{ T_PASS("didn't crash!"); T_END; }); dispatch_release(g); while (1) { T_LOG("malloc_zone_pressure_relief"); malloc_zone_pressure_relief(malloc_default_zone(), 0); sleep(1); } } |