]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/mach_get_times.c
xnu-4570.51.1.tar.gz
[apple/xnu.git] / tools / tests / darwintests / mach_get_times.c
1 #include <stdlib.h>
2 #include <time.h>
3 #include <sys/time.h>
4 #include <mach/mach_time.h>
5
6 #include <darwintest.h>
7 #include <darwintest_utils.h>
8
9 #define T_LOG_VERBOSE(...)
10
11 #define timespec2nanosec(ts) ((uint64_t)((ts)->tv_sec) * NSEC_PER_SEC + (uint64_t)((ts)->tv_nsec))
12
13 T_DECL(mach_get_times, "mach_get_times()",
14 T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(true))
15 {
16 const int ITERATIONS = 500000 * dt_ncpu();
17 struct timespec gtod_ts;
18
19 uint64_t last_absolute, last_continuous, last_gtod;
20 T_QUIET; T_ASSERT_EQ(mach_get_times(&last_absolute, &last_continuous, &gtod_ts), KERN_SUCCESS, NULL);
21 last_gtod = timespec2nanosec(&gtod_ts);
22
23 for (int i = 0; i < ITERATIONS; i++) {
24 uint64_t absolute, continuous, gtod;
25 T_QUIET; T_ASSERT_EQ(mach_get_times(&absolute, &continuous, &gtod_ts), KERN_SUCCESS, NULL);
26 gtod = timespec2nanosec(&gtod_ts);
27
28 T_LOG_VERBOSE("[%d] abs: %llu.%09llu(+%llu)\tcont: %llu.%09llu(+%llu)\tgtod:%llu.%09llu(+%llu)", i,
29 absolute / NSEC_PER_SEC, absolute % NSEC_PER_SEC, absolute - last_absolute,
30 continuous / NSEC_PER_SEC, continuous % NSEC_PER_SEC, continuous - last_continuous,
31 gtod / NSEC_PER_SEC, gtod % NSEC_PER_SEC, gtod - last_gtod);
32
33 T_QUIET; T_EXPECT_EQ(absolute - last_absolute, continuous - last_continuous, NULL);
34
35 int64_t gtod_diff = (int64_t)gtod - (int64_t)last_gtod;
36 T_QUIET; T_ASSERT_LE((uint64_t)llabs(gtod_diff), NSEC_PER_SEC, NULL);
37
38 last_absolute = absolute;
39 last_continuous = continuous;
40 last_gtod = gtod;
41
42 gtod_ts.tv_sec = 0; gtod_ts.tv_nsec = 0;
43 }
44 }