6 #include <mach/clock_types.h>
9 #include <darwintest.h>
10 #include <darwintest_utils.h>
13 #define DAY 86400 /*1 day in sec*/
14 #define ERROR 2 /*2 us of error tolerance*/
16 T_DECL(settimeofday_29192647
,
17 "Verify that the syscall settimeofday is effective",
18 T_META_ASROOT(true), T_META_CHECK_LEAKS(NO
), T_META_LTEPHASE(LTE_POSTINIT
))
24 T_SKIP("settimeofday_29192647 test requires root privileges to run.");
28 T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
30 /* increment the time of one day */
31 new_time
= time
.tv_sec
+ DAY
;
33 time
.tv_sec
= new_time
;
36 T_LOG("Attemping to set the time one day after.");
39 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
42 T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
44 /* expext to be past new_time */
45 T_EXPECT_GE_LONG(time
.tv_sec
, new_time
, "Time successfully changed");
47 /* set the time back to previous value */
48 if (time
.tv_sec
>= new_time
) {
49 time
.tv_sec
= time
.tv_sec
- DAY
;
53 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
57 static void get_abs_to_us_scale_factor(uint64_t* numer
, uint64_t* denom
){
59 uint64_t old_abstime
, new_abstime
;
60 uint64_t old_time_usec
, new_time_usec
;
61 uint64_t time_conv1
, diff
;
62 mach_timebase_info_data_t timebaseInfo
= { 0, 0 };
64 T_QUIET
; T_ASSERT_EQ(mach_get_times(&old_abstime
, NULL
, &time
), KERN_SUCCESS
, NULL
);
66 old_time_usec
= (uint64_t)time
.tv_sec
* USEC_PER_SEC
+ (uint64_t)time
.tv_nsec
/1000;
70 T_QUIET
; T_ASSERT_EQ(mach_get_times(&new_abstime
, NULL
, &time
), KERN_SUCCESS
, NULL
);
72 new_time_usec
= (uint64_t)time
.tv_sec
* USEC_PER_SEC
+ (uint64_t)time
.tv_nsec
/1000;
74 /* this is conversion factors from abs to nanos */
75 T_ASSERT_EQ(mach_timebase_info(&timebaseInfo
), KERN_SUCCESS
, NULL
);
77 new_time_usec
-= old_time_usec
;
78 new_abstime
-= old_abstime
;
80 time_conv1
= new_abstime
;
81 time_conv1
*= timebaseInfo
.numer
;
82 time_conv1
/= timebaseInfo
.denom
* 1000;
84 if (time_conv1
> new_time_usec
)
85 diff
= time_conv1
- new_time_usec
;
87 diff
= new_time_usec
- time_conv1
;
89 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
);
91 *numer
= (uint64_t)timebaseInfo
.numer
;
92 *denom
= (uint64_t)timebaseInfo
.denom
* 1000;
96 #define ADJSTMENT 3333 /*3333 us*/
97 #define ADJTIME_OFFSET_PER_SEC 500
99 T_DECL(adjtime_29192647
,
100 "Verify that the syscall adjtime is effective",
101 T_META_CHECK_LEAKS(NO
), T_META_LTEPHASE(LTE_POSTINIT
), T_META_ASROOT(true))
103 struct timespec time
;
105 uint64_t old_abstime
, new_abstime
, abs_delta
;
106 uint64_t old_time_usec
, new_time_usec
, us_delta
, num
, den
;
107 unsigned int sleep_time
;
109 const char * lterdos_env
= NULL
;
111 #if defined(__i386__) || defined(__x86_64__)
112 T_SKIP("adjtime_29192647 test requires LTE to run.");
115 if (geteuid() != 0) {
116 T_SKIP("adjtime_29192647 test requires root privileges to run.");
119 lterdos_env
= getenv("LTERDOS");
121 if (lterdos_env
!= NULL
){
122 if (!(strcmp(lterdos_env
, "YES") == 0)) {
123 T_SKIP("adjtime_29192647 test requires LTE to run.");
127 T_SKIP("adjtime_29192647 test requires LTE to run.");
131 * Calibrate scale factor for converting from abs time to usec
133 get_abs_to_us_scale_factor(&num
, &den
);
135 T_QUIET
; T_ASSERT_EQ(mach_get_times(&old_abstime
, NULL
, &time
), KERN_SUCCESS
, NULL
);
137 old_time_usec
= (uint64_t)time
.tv_sec
* USEC_PER_SEC
+ (uint64_t)time
.tv_nsec
/1000;
140 adj
.tv_usec
= ADJSTMENT
;
142 T_LOG("Attemping to adjust the time of %d", ADJSTMENT
);
145 * If more than one second of adjustment
146 * the system slews at a rate of 5ms/s otherwise 500us/s
147 * until the last second is slewed the final < 500 usecs.
150 T_ASSERT_POSIX_ZERO(adjtime(&adj
, NULL
),NULL
);
153 * Wait that the full adjustment is applied.
154 * Note, add 2 more secs for take into account division error
155 * and that the last block of adj is fully elapsed.
157 sleep_time
= (ADJSTMENT
)/(ADJTIME_OFFSET_PER_SEC
)+2;
159 T_LOG("Waiting for %u sec\n", sleep_time
);
162 T_QUIET
; T_ASSERT_EQ(mach_get_times(&new_abstime
, NULL
, &time
), KERN_SUCCESS
, NULL
);
164 new_time_usec
= (uint64_t)time
.tv_sec
* USEC_PER_SEC
+ (uint64_t)time
.tv_nsec
/1000;
166 us_delta
= new_time_usec
- old_time_usec
;
167 us_delta
-= ADJSTMENT
;
169 /* abs time is not affected by adjtime */
170 abs_delta
= new_abstime
- old_abstime
;
175 diff
= (long) us_delta
- (long) abs_delta
;
177 /* expext that us_delta == abs_delta */
178 T_EXPECT_LE_LONG(diff
, (long) ERROR
, "Check abs time vs calendar time");
180 T_EXPECT_GE_LONG(diff
, (long) -ERROR
, "Check abs time vs calendar time");
184 #define FREQ_PPM 222 /*222 PPM(us/s)*/
186 #define OFFSET_US 123 /*123us*/
188 T_DECL(ntp_adjtime_29192647
,
189 "Verify that the syscall ntp_adjtime is effective",
190 T_META_CHECK_LEAKS(NO
), T_META_LTEPHASE(LTE_POSTINIT
), T_META_ASROOT(true))
192 struct timespec time
;
193 struct timex ntptime
;
194 uint64_t abstime1
, abstime2
, abs_delta
, num
, den
, time_delta
;
195 uint64_t time1_usec
, time2_usec
, time_conv
, us_delta
, app
;
198 unsigned int sleep_time
;
199 const char * lterdos_env
= NULL
;
201 #if defined(__i386__) || defined(__x86_64__)
202 T_SKIP("ntp_adjtime_29192647 test requires LTE to run.");
206 T_SKIP("ntp_adjtime_29192647 test requires root privileges to run.");
209 lterdos_env
= getenv("LTERDOS");
211 if (lterdos_env
!= NULL
){
212 if (!(strcmp(lterdos_env
, "YES") == 0)) {
213 T_SKIP("adjtime_29192647 test requires LTE to run.");
217 T_SKIP("adjtime_29192647 test requires LTE to run.");
221 * Calibrate scale factor for converting from abs time to usec
223 get_abs_to_us_scale_factor(&num
, &den
);
226 * scale frequency using ntp_adjtime;
228 memset(&ntptime
, 0, sizeof(ntptime
));
230 ntptime
.modes
= MOD_STATUS
;
231 ntptime
.status
= TIME_OK
;
232 /* ntp input freq is in ppm (us/s) * 2^16, max freq is 500 ppm */
233 freq
= (FREQ_PPM
) * 65536;
234 ntptime
.modes
|= MOD_FREQUENCY
;
237 T_LOG("Attemping to change calendar frequency of %d ppm", FREQ_PPM
);
240 T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
);
243 T_ASSERT_EQ(ntptime
.freq
, freq
, NULL
);
247 T_QUIET
; T_ASSERT_EQ(mach_get_times(&abstime1
, NULL
, &time
), KERN_SUCCESS
, NULL
);
249 time1_usec
= (uint64_t)time
.tv_sec
* USEC_PER_SEC
+ (uint64_t)time
.tv_nsec
/1000;
253 T_QUIET
; T_ASSERT_EQ(mach_get_times(&abstime2
, NULL
, &time
), KERN_SUCCESS
, NULL
);
255 time2_usec
= (uint64_t)time
.tv_sec
* USEC_PER_SEC
+ (uint64_t)time
.tv_nsec
/1000;
257 abs_delta
= abstime2
- abstime1
;
258 us_delta
= time2_usec
- time1_usec
;
260 time_conv
= abs_delta
;
264 app
= time_conv
/USEC_PER_SEC
; //sec elapsed
266 time_delta
= time_conv
;
267 time_delta
+= app
* (FREQ_PPM
);
269 app
= time_conv%USEC_PER_SEC
;
271 time_delta
+= (app
*(FREQ_PPM
))/USEC_PER_SEC
;
273 diff
= (long) us_delta
- (long) time_delta
;
275 /* expext that us_delta == time_delta */
276 T_EXPECT_LE_LONG(diff
, (long) ERROR
, "Check abs time vs calendar time");
278 T_EXPECT_GE_LONG(diff
, (long) -ERROR
, "Check abs time vs calendar time");
280 memset(&ntptime
, 0, sizeof(ntptime
));
282 /* reset freq to zero */
284 ntptime
.modes
= MOD_STATUS
;
285 ntptime
.status
= TIME_OK
;
286 ntptime
.modes
|= MOD_FREQUENCY
;
290 T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
);
293 T_ASSERT_EQ(ntptime
.freq
, freq
, NULL
);
298 * adjust the phase using ntp_adjtime;
300 memset(&ntptime
, 0, sizeof(ntptime
));
301 ntptime
.modes
|= MOD_STATUS
;
302 ntptime
.status
= TIME_OK
;
303 ntptime
.status
|= STA_PLL
|STA_FREQHOLD
;
305 /* ntp input phase can be both ns or us (MOD_MICRO), max offset is 500 ms */
306 ntptime
.offset
= OFFSET_US
;
307 ntptime
.modes
|= MOD_OFFSET
|MOD_MICRO
;
310 * The system will slew each sec of:
311 * slew = ntp.offset >> (SHIFT_PLL + time_constant);
312 * ntp.offset -= slew;
314 offset
= (OFFSET_US
) * 1000;
317 while((offset
>>SHIFT_PLL
)>0){
318 offset
-= offset
>> SHIFT_PLL
;
322 T_QUIET
; T_ASSERT_EQ(mach_get_times(&abstime1
, NULL
, &time
), KERN_SUCCESS
, NULL
);
324 time1_usec
= (uint64_t)time
.tv_sec
* USEC_PER_SEC
+ (uint64_t)time
.tv_nsec
/1000;
326 T_LOG("Attemping to change calendar phase of %d us", OFFSET_US
);
329 T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
);
332 T_ASSERT_EQ(ntptime
.offset
, (long) OFFSET_US
, NULL
);
334 T_LOG("Waiting for %u sec\n", sleep_time
);
337 T_QUIET
; T_ASSERT_EQ(mach_get_times(&abstime2
, NULL
, &time
), KERN_SUCCESS
, NULL
);
339 time2_usec
= (uint64_t)time
.tv_sec
* USEC_PER_SEC
+ (uint64_t)time
.tv_nsec
/1000;
341 abs_delta
= abstime2
- abstime1
;
342 us_delta
= time2_usec
- time1_usec
;
347 us_delta
-= OFFSET_US
;
349 diff
= (long) us_delta
- (long) abs_delta
;
351 /* expext that us_delta == abs_delta */
352 T_EXPECT_LE_LONG(diff
, (long) ERROR
, "Check abs time vs calendar time");
354 T_EXPECT_GE_LONG(diff
, (long) -ERROR
, "Check abs time vs calendar time");
356 memset(&ntptime
, 0, sizeof(ntptime
));
357 ntptime
.modes
= MOD_STATUS
;
358 ntptime
.status
= TIME_OK
;
359 ntptime
.modes
|= MOD_FREQUENCY
;
362 ntptime
.status
|= STA_PLL
;
364 ntptime
.modes
|= MOD_OFFSET
;
367 T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
);