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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | #include <mach/mach.h> #include <mach/mach_time.h> #include <mach/clock_types.h> #include <sys/time.h> #include <spawn.h> #include <sys/wait.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #include <errno.h> #include <darwintest.h> #if (defined(__arm__) || defined(__arm64__)) #define HAS_KERNEL_TIME_TRAPS extern uint64_t mach_absolute_time_kernel(void); extern uint64_t mach_continuous_time_kernel(void); #endif extern char **environ; static const int64_t one_mil = 1000*1000; #define to_ns(ticks) (((ticks) * tb_info.numer) / (tb_info.denom)) #define to_ms(ticks) (to_ns(ticks)/one_mil) static mach_timebase_info_data_t tb_info; static void update(uint64_t *a, uint64_t *c) { mach_get_times(a,c,NULL); } T_DECL(mct_monotonic, "Testing mach_continuous_time returns sane, monotonic values", T_META_ALL_VALID_ARCHS(true)) { mach_timebase_info(&tb_info); #ifdef HAS_KERNEL_TIME_TRAPS bool kernel = false; #endif volatile uint64_t multiple_test = to_ms(mach_continuous_time()); for(int i = 0; i < 20; i++) { uint64_t tmp; const char *test_type = "user"; #ifdef HAS_KERNEL_TIME_TRAPS if (kernel) { test_type = "kernel"; tmp = mach_continuous_time_kernel(); } else tmp = mach_continuous_time(); kernel = !kernel; #else tmp = mach_continuous_time(); #endif tmp = to_ms(tmp); T_ASSERT_GE(tmp, multiple_test, "mach_continuous_time (%s) must be monotonic", test_type); // each successive call shouldn't be more than 100ms in the future T_ASSERT_LE(tmp - multiple_test, 100ULL, "mach_continuous_time (%s) should not jump forward too fast", test_type); multiple_test = tmp; } } T_DECL(mat_monotonic, "Testing mach_absolute_time returns sane, monotonic values", T_META_ALL_VALID_ARCHS(true)) { mach_timebase_info(&tb_info); #ifdef HAS_KERNEL_TIME_TRAPS bool kernel = false; #endif volatile uint64_t multiple_test = to_ms(mach_absolute_time()); for(int i = 0; i < 20; i++) { uint64_t tmp; const char *test_type = "user"; #ifdef HAS_KERNEL_TIME_TRAPS if (kernel) { test_type = "kernel"; tmp = mach_absolute_time_kernel(); } else tmp = mach_absolute_time(); kernel = !kernel; #endif tmp = mach_absolute_time(); tmp = to_ms(tmp); T_ASSERT_GE(tmp, multiple_test, "mach_absolute_time (%s) must be monotonic", test_type); // each successive call shouldn't be more than 100ms in the future T_ASSERT_LE(tmp - multiple_test, 100ULL, "mach_absolute_time (%s) should not jump forward too fast", test_type); multiple_test = tmp; } } T_DECL(mct_pause, "Testing mach_continuous_time and mach_absolute_time don't diverge") { mach_timebase_info(&tb_info); uint64_t abs_now; uint64_t cnt_now; int before_diff, after_diff; update(&abs_now, &cnt_now); before_diff = (int)(to_ms(cnt_now) - to_ms(abs_now)); sleep(1); update(&abs_now, &cnt_now); after_diff = (int)(to_ms(cnt_now) - to_ms(abs_now)); T_ASSERT_LE(abs(after_diff - before_diff), 1, "mach_continuous_time and mach_absolute_time should not diverge"); } #ifdef HAS_KERNEL_TIME_TRAPS static void update_kern(uint64_t *abs, uint64_t *cont) { uint64_t abs1, abs2, cont1, cont2; do { abs1 = mach_absolute_time_kernel(); cont1 = mach_continuous_time_kernel(); abs2 = mach_absolute_time_kernel(); cont2 = mach_continuous_time_kernel(); } while (to_ms(abs2 - abs1) || to_ms(cont2 - cont1)); *abs = abs2; *cont = cont2; } #endif #ifdef HAS_KERNEL_TIME_TRAPS T_DECL(mct_pause_kern, "Testing kernel mach_continuous_time and mach_absolute_time don't diverge") { mach_timebase_info(&tb_info); uint64_t abs_now; uint64_t cnt_now; int before_diff, after_diff; update_kern(&abs_now, &cnt_now); before_diff = (int)(to_ms(cnt_now) - to_ms(abs_now)); sleep(1); update_kern(&abs_now, &cnt_now); after_diff = (int)(to_ms(cnt_now) - to_ms(abs_now)); T_ASSERT_LE(abs(after_diff - before_diff), 1, "mach_continuous_time_kernel and mach_absolute_time_kernel should not diverge"); } #endif T_DECL(mct_sleep, "Testing mach_continuous_time behavior over system sleep"){ #ifndef MCT_SLEEP_TEST T_SKIP("Skipping test that sleeps the device; compile with MCT_SLEEP_TEST define to enable."); #endif mach_timebase_info(&tb_info); uint64_t abs_now; uint64_t cnt_now; int before_diff, after_diff = 0; T_LOG("Testing mach_continuous_time is ~5 seconds ahead of mach_absolute_time after 5 second sleep"); update(&abs_now, &cnt_now); before_diff = (int)(to_ms(cnt_now) - to_ms(abs_now)); // performs: // pmset relative wake 5 // pmset sleepnow pid_t pid; int spawn_ret = 0; time_t before_sleep = time(NULL); int ct_ms_before_sleep = (int)to_ms(cnt_now); int ab_ms_before_sleep = (int)to_ms(abs_now); char *const pmset1_args[] = {"/usr/bin/pmset", "relative", "wake", "5", NULL}; T_ASSERT_POSIX_ZERO((spawn_ret = posix_spawn(&pid, pmset1_args[0], NULL, NULL, pmset1_args, environ)), NULL); T_ASSERT_EQ(waitpid(pid, &spawn_ret, 0), pid, "waitpid failed"); T_ASSERT_EQ(spawn_ret, 0, "pmset relative wait 5 failed"); char *const pmset2_args[] = {"/usr/bin/pmset", "sleepnow", NULL}; T_ASSERT_POSIX_ZERO((spawn_ret = posix_spawn(&pid, pmset2_args[0], NULL, NULL, pmset2_args, environ)), NULL); T_ASSERT_EQ(waitpid(pid, &spawn_ret, 0), pid, "waitpid failed"); T_ASSERT_EQ(spawn_ret, 0, "pmset relative wait 5 failed"); // wait for device to sleep (up to 30 seconds) for(int i = 0; i < 30; i++) { update(&abs_now, &cnt_now); after_diff = (int)(to_ms(cnt_now) - to_ms(abs_now)); // on OSX, there's enough latency between calls to MCT and MAT // when the system is going down for sleep for values to diverge a few ms if(abs(before_diff - after_diff) > 2) { break; } sleep(1); T_LOG("waited %d seconds for sleep...", i+1); } if((after_diff - before_diff) < 4000) { T_LOG("Device slept for less than 4 seconds, did it really sleep? (%d ms change between abs and cont)", after_diff - before_diff); } time_t after_sleep = time(NULL); int cal_sleep_diff = (int)(double)difftime(after_sleep, before_sleep); int ct_sleep_diff = ((int)to_ms(cnt_now) - ct_ms_before_sleep)/1000; int ab_sleep_diff = ((int)to_ms(abs_now) - ab_ms_before_sleep)/1000; T_LOG("Calendar progressed: %d sec; continuous time progressed: %d sec; absolute time progressed %d sec", cal_sleep_diff, ct_sleep_diff, ab_sleep_diff); T_ASSERT_LE(abs(ct_sleep_diff - cal_sleep_diff), 2, "continuous time should progress at ~ same rate as calendar"); } T_DECL(mct_settimeofday, "Testing mach_continuous_time behavior over settimeofday"){ if (geteuid() != 0){ T_SKIP("The settimeofday() test requires root privileges to run."); } mach_timebase_info(&tb_info); struct timeval saved_tv; struct timezone saved_tz; int before, after; T_ASSERT_POSIX_ZERO(gettimeofday(&saved_tv, &saved_tz), NULL); struct timeval forward_tv = saved_tv; // move time forward by two minutes, ensure mach_continuous_time keeps // chugging along with mach_absolute_time forward_tv.tv_sec += 2*60; before = (int)to_ms(mach_continuous_time()); T_ASSERT_POSIX_ZERO(settimeofday(&forward_tv, &saved_tz), NULL); after = (int)to_ms(mach_continuous_time()); T_ASSERT_POSIX_ZERO(settimeofday(&saved_tv, &saved_tz), NULL); T_ASSERT_LT(abs(before - after), 1000, "mach_continuous_time should not jump more than 1s"); } #ifdef HAS_KERNEL_TIME_TRAPS T_DECL(mct_settimeofday_kern, "Testing kernel mach_continuous_time behavior over settimeofday"){ if (geteuid() != 0){ T_SKIP("The settimeofday() test requires root privileges to run."); } mach_timebase_info(&tb_info); struct timeval saved_tv; struct timezone saved_tz; int before, after; T_ASSERT_POSIX_ZERO(gettimeofday(&saved_tv, &saved_tz), NULL); struct timeval forward_tv = saved_tv; // move time forward by two minutes, ensure mach_continuous_time keeps // chugging along with mach_absolute_time forward_tv.tv_sec += 2*60; before = (int)to_ms(mach_continuous_time_kernel()); T_ASSERT_POSIX_ZERO(settimeofday(&forward_tv, &saved_tz), NULL); after = (int)to_ms(mach_continuous_time_kernel()); T_ASSERT_POSIX_ZERO(settimeofday(&saved_tv, &saved_tz), NULL); T_ASSERT_LT(abs(before - after), 1000, "mach_continuous_time_kernel should not jump more than 1s"); } #endif T_DECL(mct_aproximate, "Testing mach_continuous_approximate_time()", T_META_ALL_VALID_ARCHS(true)) { mach_timebase_info(&tb_info); uint64_t absolute = to_ns(mach_continuous_time()); uint64_t approximate = to_ns(mach_continuous_approximate_time()); T_EXPECT_LE(llabs((long long)absolute - (long long)approximate), (long long)(25*NSEC_PER_MSEC), NULL); } T_DECL(mach_time_perf, "mach_time performance") { { dt_stat_time_t s = dt_stat_time_create("mach_absolute_time"); T_STAT_MEASURE_LOOP(s) { uint64_t t; t = mach_absolute_time(); } dt_stat_finalize(s); } { dt_stat_time_t s = dt_stat_time_create("mach_continuous_time"); T_STAT_MEASURE_LOOP(s) { uint64_t t; t = mach_continuous_time(); } dt_stat_finalize(s); } } T_DECL(mach_time_perf_instructions, "instructions retired for mach_time", T_META_TYPE_PERF, T_META_ASROOT(YES)) { { dt_stat_thread_instructions_t s = dt_stat_thread_instructions_create("mach_absolute_time"); T_STAT_MEASURE_LOOP(s) { uint64_t t; t = mach_absolute_time(); } dt_stat_finalize(s); } { dt_stat_thread_instructions_t s = dt_stat_thread_instructions_create("mach_continuous_time"); T_STAT_MEASURE_LOOP(s) { uint64_t t; t = mach_continuous_time(); } dt_stat_finalize(s); } } #ifdef HAS_KERNEL_TIME_TRAPS T_DECL(mach_time_perf_kern, "kernel mach_time performance") { { dt_stat_time_t s = dt_stat_time_create("mach_absolute_time_kernel"); T_STAT_MEASURE_LOOP(s) { uint64_t t; t = mach_absolute_time_kernel(); } dt_stat_finalize(s); } { dt_stat_time_t s = dt_stat_time_create("mach_continuous_time_kernel"); T_STAT_MEASURE_LOOP(s) { uint64_t t; t = mach_continuous_time_kernel(); } dt_stat_finalize(s); } } T_DECL(mach_time_perf_instructions_kern, "instructions retired for kernel mach_time", T_META_TYPE_PERF, T_META_ASROOT(YES)) { { dt_stat_thread_instructions_t s = dt_stat_thread_instructions_create("mach_absolute_time_kernel"); T_STAT_MEASURE_LOOP(s) { uint64_t t; t = mach_absolute_time_kernel(); } dt_stat_finalize(s); } { dt_stat_thread_instructions_t s = dt_stat_thread_instructions_create("mach_continuous_time_kernel"); T_STAT_MEASURE_LOOP(s) { uint64_t t; t = mach_continuous_time_kernel(); } dt_stat_finalize(s); } } #endif |