]> git.saurik.com Git - apple/xnu.git/blobdiff - tests/mach_continuous_time.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / mach_continuous_time.c
index a7d773bfb19a2572efb5930c9ed2ca18e481cc60..fe782e0f9186fc94fd7332084e83e0ec4a7821c7 100644 (file)
@@ -19,10 +19,10 @@ 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;
+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)
@@ -30,12 +30,13 @@ static const int64_t one_mil = 1000*1000;
 static mach_timebase_info_data_t tb_info;
 
 static void
-update(uint64_t *a, uint64_t *c) {
-       mach_get_times(a,c,NULL);
+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))
+    T_META_ALL_VALID_ARCHS(true), T_META_RUN_CONCURRENTLY(true))
 {
        mach_timebase_info(&tb_info);
 #ifdef HAS_KERNEL_TIME_TRAPS
@@ -43,15 +44,16 @@ T_DECL(mct_monotonic, "Testing mach_continuous_time returns sane, monotonic valu
 #endif
 
        volatile uint64_t multiple_test = to_ms(mach_continuous_time());
-       for(int i = 0; i < 20; i++) {
+       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
+               } else {
                        tmp = mach_continuous_time();
+               }
                kernel = !kernel;
 #else
                tmp = mach_continuous_time();
@@ -67,7 +69,7 @@ T_DECL(mct_monotonic, "Testing mach_continuous_time returns sane, monotonic valu
 }
 
 T_DECL(mat_monotonic, "Testing mach_absolute_time returns sane, monotonic values",
-               T_META_ALL_VALID_ARCHS(true))
+    T_META_ALL_VALID_ARCHS(true), T_META_RUN_CONCURRENTLY(true))
 {
        mach_timebase_info(&tb_info);
 #ifdef HAS_KERNEL_TIME_TRAPS
@@ -75,15 +77,16 @@ T_DECL(mat_monotonic, "Testing mach_absolute_time returns sane, monotonic values
 #endif
 
        volatile uint64_t multiple_test = to_ms(mach_absolute_time());
-       for(int i = 0; i < 20; i++) {
+       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
+               } else {
                        tmp = mach_absolute_time();
+               }
                kernel = !kernel;
 #endif
                tmp = mach_absolute_time();
@@ -97,7 +100,8 @@ T_DECL(mat_monotonic, "Testing mach_absolute_time returns sane, monotonic values
        }
 }
 
-T_DECL(mct_pause, "Testing mach_continuous_time and mach_absolute_time don't diverge")
+T_DECL(mct_pause, "Testing mach_continuous_time and mach_absolute_time don't diverge",
+    T_META_RUN_CONCURRENTLY(true))
 {
        mach_timebase_info(&tb_info);
 
@@ -117,7 +121,8 @@ T_DECL(mct_pause, "Testing mach_continuous_time and mach_absolute_time don't div
 }
 
 #ifdef HAS_KERNEL_TIME_TRAPS
-static void update_kern(uint64_t *abs, uint64_t *cont)
+static void
+update_kern(uint64_t *abs, uint64_t *cont)
 {
        uint64_t abs1, abs2, cont1, cont2;
        do {
@@ -132,7 +137,8 @@ static void update_kern(uint64_t *abs, uint64_t *cont)
 #endif
 
 #ifdef HAS_KERNEL_TIME_TRAPS
-T_DECL(mct_pause_kern, "Testing kernel mach_continuous_time and mach_absolute_time don't diverge")
+T_DECL(mct_pause_kern, "Testing kernel mach_continuous_time and mach_absolute_time don't diverge",
+    T_META_RUN_CONCURRENTLY(true))
 {
        mach_timebase_info(&tb_info);
 
@@ -190,40 +196,40 @@ T_DECL(mct_sleep, "Testing mach_continuous_time behavior over system sleep"){
        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++) {
+       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) {
+               if (abs(before_diff - after_diff) > 2) {
                        break;
                }
 
                sleep(1);
-               T_LOG("waited %d seconds for sleep...", i+1);
+               T_LOG("waited %d seconds for sleep...", i + 1);
        }
 
-       if((after_diff - before_diff) < 4000) {
+       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);
+                   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;
+       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);
+           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");
+           "continuous time should progress at ~ same rate as calendar");
 }
 
 T_DECL(mct_settimeofday, "Testing mach_continuous_time behavior over settimeofday"){
-       if (geteuid() != 0){
+       if (geteuid() != 0) {
                T_SKIP("The settimeofday() test requires root privileges to run.");
        }
        mach_timebase_info(&tb_info);
@@ -237,7 +243,7 @@ T_DECL(mct_settimeofday, "Testing mach_continuous_time behavior over settimeofda
        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;
+       forward_tv.tv_sec += 2 * 60;
 
        before = (int)to_ms(mach_continuous_time());
        T_ASSERT_POSIX_ZERO(settimeofday(&forward_tv, &saved_tz), NULL);
@@ -250,7 +256,7 @@ T_DECL(mct_settimeofday, "Testing mach_continuous_time behavior over settimeofda
 
 #ifdef HAS_KERNEL_TIME_TRAPS
 T_DECL(mct_settimeofday_kern, "Testing kernel mach_continuous_time behavior over settimeofday"){
-       if (geteuid() != 0){
+       if (geteuid() != 0) {
                T_SKIP("The settimeofday() test requires root privileges to run.");
        }
        mach_timebase_info(&tb_info);
@@ -264,7 +270,7 @@ T_DECL(mct_settimeofday_kern, "Testing kernel mach_continuous_time behavior over
        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;
+       forward_tv.tv_sec += 2 * 60;
 
        before = (int)to_ms(mach_continuous_time_kernel());
        T_ASSERT_POSIX_ZERO(settimeofday(&forward_tv, &saved_tz), NULL);
@@ -277,14 +283,14 @@ T_DECL(mct_settimeofday_kern, "Testing kernel mach_continuous_time behavior over
 #endif
 
 T_DECL(mct_aproximate, "Testing mach_continuous_approximate_time()",
-               T_META_ALL_VALID_ARCHS(true))
+    T_META_ALL_VALID_ARCHS(true), T_META_RUN_CONCURRENTLY(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_EXPECT_LE(llabs((long long)absolute - (long long)approximate), (long long)(25 * NSEC_PER_MSEC), NULL);
 }
 
 T_DECL(mach_time_perf, "mach_time performance") {
@@ -364,4 +370,3 @@ T_DECL(mach_time_perf_instructions_kern, "instructions retired for kernel mach_t
        }
 }
 #endif
-