2 #include <mach/mach_time.h>
3 #include <mach/clock_types.h>
13 #include <darwintest.h>
15 #if (defined(__arm__) || defined(__arm64__))
16 #define HAS_KERNEL_TIME_TRAPS
18 extern uint64_t mach_absolute_time_kernel(void);
19 extern uint64_t mach_continuous_time_kernel(void);
23 extern char **environ
;
25 static const int64_t one_mil
= 1000 * 1000;
27 #define to_ns(ticks) (((ticks) * tb_info.numer) / (tb_info.denom))
28 #define to_ms(ticks) (to_ns(ticks)/one_mil)
30 static mach_timebase_info_data_t tb_info
;
33 update(uint64_t *a
, uint64_t *c
)
35 mach_get_times(a
, c
, NULL
);
38 T_DECL(mct_monotonic
, "Testing mach_continuous_time returns sane, monotonic values",
39 T_META_ALL_VALID_ARCHS(true))
41 mach_timebase_info(&tb_info
);
42 #ifdef HAS_KERNEL_TIME_TRAPS
46 volatile uint64_t multiple_test
= to_ms(mach_continuous_time());
47 for (int i
= 0; i
< 20; i
++) {
49 const char *test_type
= "user";
50 #ifdef HAS_KERNEL_TIME_TRAPS
53 tmp
= mach_continuous_time_kernel();
55 tmp
= mach_continuous_time();
59 tmp
= mach_continuous_time();
62 T_ASSERT_GE(tmp
, multiple_test
, "mach_continuous_time (%s) must be monotonic", test_type
);
64 // each successive call shouldn't be more than 100ms in the future
65 T_ASSERT_LE(tmp
- multiple_test
, 100ULL, "mach_continuous_time (%s) should not jump forward too fast", test_type
);
71 T_DECL(mat_monotonic
, "Testing mach_absolute_time returns sane, monotonic values",
72 T_META_ALL_VALID_ARCHS(true))
74 mach_timebase_info(&tb_info
);
75 #ifdef HAS_KERNEL_TIME_TRAPS
79 volatile uint64_t multiple_test
= to_ms(mach_absolute_time());
80 for (int i
= 0; i
< 20; i
++) {
82 const char *test_type
= "user";
83 #ifdef HAS_KERNEL_TIME_TRAPS
86 tmp
= mach_absolute_time_kernel();
88 tmp
= mach_absolute_time();
92 tmp
= mach_absolute_time();
94 T_ASSERT_GE(tmp
, multiple_test
, "mach_absolute_time (%s) must be monotonic", test_type
);
96 // each successive call shouldn't be more than 100ms in the future
97 T_ASSERT_LE(tmp
- multiple_test
, 100ULL, "mach_absolute_time (%s) should not jump forward too fast", test_type
);
103 T_DECL(mct_pause
, "Testing mach_continuous_time and mach_absolute_time don't diverge")
105 mach_timebase_info(&tb_info
);
109 int before_diff
, after_diff
;
111 update(&abs_now
, &cnt_now
);
112 before_diff
= (int)(to_ms(cnt_now
) - to_ms(abs_now
));
116 update(&abs_now
, &cnt_now
);
117 after_diff
= (int)(to_ms(cnt_now
) - to_ms(abs_now
));
119 T_ASSERT_LE(abs(after_diff
- before_diff
), 1, "mach_continuous_time and mach_absolute_time should not diverge");
122 #ifdef HAS_KERNEL_TIME_TRAPS
124 update_kern(uint64_t *abs
, uint64_t *cont
)
126 uint64_t abs1
, abs2
, cont1
, cont2
;
128 abs1
= mach_absolute_time_kernel();
129 cont1
= mach_continuous_time_kernel();
130 abs2
= mach_absolute_time_kernel();
131 cont2
= mach_continuous_time_kernel();
132 } while (to_ms(abs2
- abs1
) || to_ms(cont2
- cont1
));
138 #ifdef HAS_KERNEL_TIME_TRAPS
139 T_DECL(mct_pause_kern
, "Testing kernel mach_continuous_time and mach_absolute_time don't diverge")
141 mach_timebase_info(&tb_info
);
145 int before_diff
, after_diff
;
147 update_kern(&abs_now
, &cnt_now
);
148 before_diff
= (int)(to_ms(cnt_now
) - to_ms(abs_now
));
152 update_kern(&abs_now
, &cnt_now
);
153 after_diff
= (int)(to_ms(cnt_now
) - to_ms(abs_now
));
155 T_ASSERT_LE(abs(after_diff
- before_diff
), 1, "mach_continuous_time_kernel and mach_absolute_time_kernel should not diverge");
159 T_DECL(mct_sleep
, "Testing mach_continuous_time behavior over system sleep"){
160 #ifndef MCT_SLEEP_TEST
161 T_SKIP("Skipping test that sleeps the device; compile with MCT_SLEEP_TEST define to enable.");
164 mach_timebase_info(&tb_info
);
168 int before_diff
, after_diff
= 0;
170 T_LOG("Testing mach_continuous_time is ~5 seconds ahead of mach_absolute_time after 5 second sleep");
171 update(&abs_now
, &cnt_now
);
172 before_diff
= (int)(to_ms(cnt_now
) - to_ms(abs_now
));
175 // pmset relative wake 5
180 time_t before_sleep
= time(NULL
);
181 int ct_ms_before_sleep
= (int)to_ms(cnt_now
);
182 int ab_ms_before_sleep
= (int)to_ms(abs_now
);
184 char *const pmset1_args
[] = {"/usr/bin/pmset", "relative", "wake", "5", NULL
};
185 T_ASSERT_POSIX_ZERO((spawn_ret
= posix_spawn(&pid
, pmset1_args
[0], NULL
, NULL
, pmset1_args
, environ
)), NULL
);
187 T_ASSERT_EQ(waitpid(pid
, &spawn_ret
, 0), pid
, "waitpid failed");
188 T_ASSERT_EQ(spawn_ret
, 0, "pmset relative wait 5 failed");
190 char *const pmset2_args
[] = {"/usr/bin/pmset", "sleepnow", NULL
};
191 T_ASSERT_POSIX_ZERO((spawn_ret
= posix_spawn(&pid
, pmset2_args
[0], NULL
, NULL
, pmset2_args
, environ
)), NULL
);
193 T_ASSERT_EQ(waitpid(pid
, &spawn_ret
, 0), pid
, "waitpid failed");
194 T_ASSERT_EQ(spawn_ret
, 0, "pmset relative wait 5 failed");
196 // wait for device to sleep (up to 30 seconds)
197 for (int i
= 0; i
< 30; i
++) {
198 update(&abs_now
, &cnt_now
);
199 after_diff
= (int)(to_ms(cnt_now
) - to_ms(abs_now
));
201 // on OSX, there's enough latency between calls to MCT and MAT
202 // when the system is going down for sleep for values to diverge a few ms
203 if (abs(before_diff
- after_diff
) > 2) {
208 T_LOG("waited %d seconds for sleep...", i
+ 1);
211 if ((after_diff
- before_diff
) < 4000) {
212 T_LOG("Device slept for less than 4 seconds, did it really sleep? (%d ms change between abs and cont)",
213 after_diff
- before_diff
);
216 time_t after_sleep
= time(NULL
);
218 int cal_sleep_diff
= (int)(double)difftime(after_sleep
, before_sleep
);
219 int ct_sleep_diff
= ((int)to_ms(cnt_now
) - ct_ms_before_sleep
) / 1000;
220 int ab_sleep_diff
= ((int)to_ms(abs_now
) - ab_ms_before_sleep
) / 1000;
222 T_LOG("Calendar progressed: %d sec; continuous time progressed: %d sec; absolute time progressed %d sec",
223 cal_sleep_diff
, ct_sleep_diff
, ab_sleep_diff
);
225 T_ASSERT_LE(abs(ct_sleep_diff
- cal_sleep_diff
), 2,
226 "continuous time should progress at ~ same rate as calendar");
229 T_DECL(mct_settimeofday
, "Testing mach_continuous_time behavior over settimeofday"){
230 if (geteuid() != 0) {
231 T_SKIP("The settimeofday() test requires root privileges to run.");
233 mach_timebase_info(&tb_info
);
235 struct timeval saved_tv
;
236 struct timezone saved_tz
;
239 T_ASSERT_POSIX_ZERO(gettimeofday(&saved_tv
, &saved_tz
), NULL
);
241 struct timeval forward_tv
= saved_tv
;
242 // move time forward by two minutes, ensure mach_continuous_time keeps
243 // chugging along with mach_absolute_time
244 forward_tv
.tv_sec
+= 2 * 60;
246 before
= (int)to_ms(mach_continuous_time());
247 T_ASSERT_POSIX_ZERO(settimeofday(&forward_tv
, &saved_tz
), NULL
);
249 after
= (int)to_ms(mach_continuous_time());
250 T_ASSERT_POSIX_ZERO(settimeofday(&saved_tv
, &saved_tz
), NULL
);
252 T_ASSERT_LT(abs(before
- after
), 1000, "mach_continuous_time should not jump more than 1s");
255 #ifdef HAS_KERNEL_TIME_TRAPS
256 T_DECL(mct_settimeofday_kern
, "Testing kernel mach_continuous_time behavior over settimeofday"){
257 if (geteuid() != 0) {
258 T_SKIP("The settimeofday() test requires root privileges to run.");
260 mach_timebase_info(&tb_info
);
262 struct timeval saved_tv
;
263 struct timezone saved_tz
;
266 T_ASSERT_POSIX_ZERO(gettimeofday(&saved_tv
, &saved_tz
), NULL
);
268 struct timeval forward_tv
= saved_tv
;
269 // move time forward by two minutes, ensure mach_continuous_time keeps
270 // chugging along with mach_absolute_time
271 forward_tv
.tv_sec
+= 2 * 60;
273 before
= (int)to_ms(mach_continuous_time_kernel());
274 T_ASSERT_POSIX_ZERO(settimeofday(&forward_tv
, &saved_tz
), NULL
);
276 after
= (int)to_ms(mach_continuous_time_kernel());
277 T_ASSERT_POSIX_ZERO(settimeofday(&saved_tv
, &saved_tz
), NULL
);
279 T_ASSERT_LT(abs(before
- after
), 1000, "mach_continuous_time_kernel should not jump more than 1s");
283 T_DECL(mct_aproximate
, "Testing mach_continuous_approximate_time()",
284 T_META_ALL_VALID_ARCHS(true))
286 mach_timebase_info(&tb_info
);
288 uint64_t absolute
= to_ns(mach_continuous_time());
289 uint64_t approximate
= to_ns(mach_continuous_approximate_time());
291 T_EXPECT_LE(llabs((long long)absolute
- (long long)approximate
), (long long)(25 * NSEC_PER_MSEC
), NULL
);
294 T_DECL(mach_time_perf
, "mach_time performance") {
296 dt_stat_time_t s
= dt_stat_time_create("mach_absolute_time");
297 T_STAT_MEASURE_LOOP(s
) {
299 t
= mach_absolute_time();
304 dt_stat_time_t s
= dt_stat_time_create("mach_continuous_time");
305 T_STAT_MEASURE_LOOP(s
) {
307 t
= mach_continuous_time();
313 T_DECL(mach_time_perf_instructions
, "instructions retired for mach_time", T_META_TYPE_PERF
, T_META_ASROOT(YES
)) {
315 dt_stat_thread_instructions_t s
= dt_stat_thread_instructions_create("mach_absolute_time");
316 T_STAT_MEASURE_LOOP(s
) {
318 t
= mach_absolute_time();
323 dt_stat_thread_instructions_t s
= dt_stat_thread_instructions_create("mach_continuous_time");
324 T_STAT_MEASURE_LOOP(s
) {
326 t
= mach_continuous_time();
332 #ifdef HAS_KERNEL_TIME_TRAPS
333 T_DECL(mach_time_perf_kern
, "kernel mach_time performance") {
335 dt_stat_time_t s
= dt_stat_time_create("mach_absolute_time_kernel");
336 T_STAT_MEASURE_LOOP(s
) {
338 t
= mach_absolute_time_kernel();
343 dt_stat_time_t s
= dt_stat_time_create("mach_continuous_time_kernel");
344 T_STAT_MEASURE_LOOP(s
) {
346 t
= mach_continuous_time_kernel();
352 T_DECL(mach_time_perf_instructions_kern
, "instructions retired for kernel mach_time", T_META_TYPE_PERF
, T_META_ASROOT(YES
)) {
354 dt_stat_thread_instructions_t s
= dt_stat_thread_instructions_create("mach_absolute_time_kernel");
355 T_STAT_MEASURE_LOOP(s
) {
357 t
= mach_absolute_time_kernel();
362 dt_stat_thread_instructions_t s
= dt_stat_thread_instructions_create("mach_continuous_time_kernel");
363 T_STAT_MEASURE_LOOP(s
) {
365 t
= mach_continuous_time_kernel();