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 368 | #include <stdio.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <sys/mman.h> #include <mach/clock_types.h> #include <sys/timex.h> #include <mach/mach.h> #include <darwintest.h> #include <darwintest_utils.h> #define DAY 86400 /*1 day in sec*/ #define ERROR 2 /*2 us of error tolerance*/ T_DECL(settimeofday_29192647, "Verify that the syscall settimeofday is effective", T_META_ASROOT(true), T_META_CHECK_LEAKS(NO), T_META_LTEPHASE(LTE_POSTINIT)) { struct timeval time; long new_time; if (geteuid() != 0) { T_SKIP("settimeofday_29192647 test requires root privileges to run."); } T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL); /* increment the time of one day */ new_time = time.tv_sec + DAY; time.tv_sec = new_time; time.tv_usec = 0; T_LOG("Attemping to set the time one day after."); T_WITH_ERRNO; T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL); T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL); /* expext to be past new_time */ T_EXPECT_GE_LONG(time.tv_sec, new_time, "Time successfully changed"); /* set the time back to previous value */ if (time.tv_sec >= new_time) { time.tv_sec = time.tv_sec - DAY; time.tv_usec = 0; T_WITH_ERRNO; T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL); } } static void get_abs_to_us_scale_factor(uint64_t* numer, uint64_t* denom) { struct timespec time; uint64_t old_abstime, new_abstime; uint64_t old_time_usec, new_time_usec; uint64_t time_conv1, diff; mach_timebase_info_data_t timebaseInfo = { 0, 0 }; T_QUIET; T_ASSERT_EQ(mach_get_times(&old_abstime, NULL, &time), KERN_SUCCESS, NULL); old_time_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec / 1000; sleep(1); T_QUIET; T_ASSERT_EQ(mach_get_times(&new_abstime, NULL, &time), KERN_SUCCESS, NULL); new_time_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec / 1000; /* this is conversion factors from abs to nanos */ T_ASSERT_EQ(mach_timebase_info(&timebaseInfo), KERN_SUCCESS, NULL); new_time_usec -= old_time_usec; new_abstime -= old_abstime; time_conv1 = new_abstime; time_conv1 *= timebaseInfo.numer; time_conv1 /= timebaseInfo.denom * 1000; if (time_conv1 > new_time_usec) { diff = time_conv1 - new_time_usec; } else { diff = new_time_usec - time_conv1; } T_EXPECT_LE_ULLONG(diff, (unsigned long long)ERROR, "Check scale factor time base (%u/%u) delta read usec %llu delta converted %llu delta abs %llu", timebaseInfo.numer, timebaseInfo.denom, time_conv1, new_time_usec, new_abstime); *numer = (uint64_t)timebaseInfo.numer; *denom = (uint64_t)timebaseInfo.denom * 1000; } #define ADJSTMENT 3333 /*3333 us*/ #define ADJTIME_OFFSET_PER_SEC 500 T_DECL(adjtime_29192647, "Verify that the syscall adjtime is effective", T_META_CHECK_LEAKS(NO), T_META_LTEPHASE(LTE_POSTINIT), T_META_ASROOT(true)) { struct timespec time; struct timeval adj; uint64_t old_abstime, new_abstime, abs_delta; uint64_t old_time_usec, new_time_usec, us_delta, num, den; unsigned int sleep_time; long diff; const char * lterdos_env = NULL; #if defined(__i386__) || defined(__x86_64__) T_SKIP("adjtime_29192647 test requires LTE to run."); #endif if (geteuid() != 0) { T_SKIP("adjtime_29192647 test requires root privileges to run."); } lterdos_env = getenv("LTERDOS"); if (lterdos_env != NULL) { if (!(strcmp(lterdos_env, "YES") == 0)) { T_SKIP("adjtime_29192647 test requires LTE to run."); } } else { T_SKIP("adjtime_29192647 test requires LTE to run."); } /* * Calibrate scale factor for converting from abs time to usec */ get_abs_to_us_scale_factor(&num, &den); T_QUIET; T_ASSERT_EQ(mach_get_times(&old_abstime, NULL, &time), KERN_SUCCESS, NULL); old_time_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec / 1000; adj.tv_sec = 0; adj.tv_usec = ADJSTMENT; T_LOG("Attemping to adjust the time of %d", ADJSTMENT); /* * If more than one second of adjustment * the system slews at a rate of 5ms/s otherwise 500us/s * until the last second is slewed the final < 500 usecs. */ T_WITH_ERRNO; T_ASSERT_POSIX_ZERO(adjtime(&adj, NULL), NULL); /* * Wait that the full adjustment is applied. * Note, add 2 more secs for take into account division error * and that the last block of adj is fully elapsed. */ sleep_time = (ADJSTMENT) / (ADJTIME_OFFSET_PER_SEC)+2; T_LOG("Waiting for %u sec\n", sleep_time); sleep(sleep_time); T_QUIET; T_ASSERT_EQ(mach_get_times(&new_abstime, NULL, &time), KERN_SUCCESS, NULL); new_time_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec / 1000; us_delta = new_time_usec - old_time_usec; us_delta -= ADJSTMENT; /* abs time is not affected by adjtime */ abs_delta = new_abstime - old_abstime; abs_delta *= num; abs_delta /= den; diff = (long) us_delta - (long) abs_delta; /* expext that us_delta == abs_delta */ T_EXPECT_LE_LONG(diff, (long) ERROR, "Check abs time vs calendar time"); T_EXPECT_GE_LONG(diff, (long) -ERROR, "Check abs time vs calendar time"); } #define FREQ_PPM 222 /*222 PPM(us/s)*/ #define SHIFT_PLL 4 #define OFFSET_US 123 /*123us*/ T_DECL(ntp_adjtime_29192647, "Verify that the syscall ntp_adjtime is effective", T_META_CHECK_LEAKS(NO), T_META_LTEPHASE(LTE_POSTINIT), T_META_ASROOT(true)) { struct timespec time; struct timex ntptime; uint64_t abstime1, abstime2, abs_delta, num, den, time_delta; uint64_t time1_usec, time2_usec, time_conv, us_delta, app; int64_t offset; long diff, freq; unsigned int sleep_time; const char * lterdos_env = NULL; #if defined(__i386__) || defined(__x86_64__) T_SKIP("ntp_adjtime_29192647 test requires LTE to run."); #endif if (geteuid() != 0) { T_SKIP("ntp_adjtime_29192647 test requires root privileges to run."); } lterdos_env = getenv("LTERDOS"); if (lterdos_env != NULL) { if (!(strcmp(lterdos_env, "YES") == 0)) { T_SKIP("adjtime_29192647 test requires LTE to run."); } } else { T_SKIP("adjtime_29192647 test requires LTE to run."); } /* * Calibrate scale factor for converting from abs time to usec */ get_abs_to_us_scale_factor(&num, &den); /* * scale frequency using ntp_adjtime; */ memset(&ntptime, 0, sizeof(ntptime)); ntptime.modes = MOD_STATUS; ntptime.status = TIME_OK; /* ntp input freq is in ppm (us/s) * 2^16, max freq is 500 ppm */ freq = (FREQ_PPM) * 65536; ntptime.modes |= MOD_FREQUENCY; ntptime.freq = freq; T_LOG("Attemping to change calendar frequency of %d ppm", FREQ_PPM); T_WITH_ERRNO; T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL); T_WITH_ERRNO; T_ASSERT_EQ(ntptime.freq, freq, NULL); sleep(2); T_QUIET; T_ASSERT_EQ(mach_get_times(&abstime1, NULL, &time), KERN_SUCCESS, NULL); time1_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec / 1000; sleep(1); T_QUIET; T_ASSERT_EQ(mach_get_times(&abstime2, NULL, &time), KERN_SUCCESS, NULL); time2_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec / 1000; abs_delta = abstime2 - abstime1; us_delta = time2_usec - time1_usec; time_conv = abs_delta; time_conv *= num; time_conv /= den; app = time_conv / USEC_PER_SEC; //sec elapsed time_delta = time_conv; time_delta += app * (FREQ_PPM); app = time_conv % USEC_PER_SEC; time_delta += (app * (FREQ_PPM)) / USEC_PER_SEC; diff = (long) us_delta - (long) time_delta; /* expext that us_delta == time_delta */ T_EXPECT_LE_LONG(diff, (long) ERROR, "Check abs time vs calendar time"); T_EXPECT_GE_LONG(diff, (long) -ERROR, "Check abs time vs calendar time"); memset(&ntptime, 0, sizeof(ntptime)); /* reset freq to zero */ freq = 0; ntptime.modes = MOD_STATUS; ntptime.status = TIME_OK; ntptime.modes |= MOD_FREQUENCY; ntptime.freq = freq; T_WITH_ERRNO; T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL); T_WITH_ERRNO; T_ASSERT_EQ(ntptime.freq, freq, NULL); sleep(1); /* * adjust the phase using ntp_adjtime; */ memset(&ntptime, 0, sizeof(ntptime)); ntptime.modes |= MOD_STATUS; ntptime.status = TIME_OK; ntptime.status |= STA_PLL | STA_FREQHOLD; /* ntp input phase can be both ns or us (MOD_MICRO), max offset is 500 ms */ ntptime.offset = OFFSET_US; ntptime.modes |= MOD_OFFSET | MOD_MICRO; /* * The system will slew each sec of: * slew = ntp.offset >> (SHIFT_PLL + time_constant); * ntp.offset -= slew; */ offset = (OFFSET_US) * 1000; sleep_time = 2; while ((offset >> SHIFT_PLL) > 0) { offset -= offset >> SHIFT_PLL; sleep_time++; } T_QUIET; T_ASSERT_EQ(mach_get_times(&abstime1, NULL, &time), KERN_SUCCESS, NULL); time1_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec / 1000; T_LOG("Attemping to change calendar phase of %d us", OFFSET_US); T_WITH_ERRNO; T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL); T_WITH_ERRNO; T_ASSERT_EQ(ntptime.offset, (long) OFFSET_US, NULL); T_LOG("Waiting for %u sec\n", sleep_time); sleep(sleep_time); T_QUIET; T_ASSERT_EQ(mach_get_times(&abstime2, NULL, &time), KERN_SUCCESS, NULL); time2_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec / 1000; abs_delta = abstime2 - abstime1; us_delta = time2_usec - time1_usec; abs_delta *= num; abs_delta /= den; us_delta -= OFFSET_US; diff = (long) us_delta - (long) abs_delta; /* expext that us_delta == abs_delta */ T_EXPECT_LE_LONG(diff, (long) ERROR, "Check abs time vs calendar time"); T_EXPECT_GE_LONG(diff, (long) -ERROR, "Check abs time vs calendar time"); memset(&ntptime, 0, sizeof(ntptime)); ntptime.modes = MOD_STATUS; ntptime.status = TIME_OK; ntptime.modes |= MOD_FREQUENCY; ntptime.freq = 0; ntptime.status |= STA_PLL; ntptime.offset = 0; ntptime.modes |= MOD_OFFSET; T_WITH_ERRNO; T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL); } |