]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/ntp_adjtime_29192647.c
xnu-4570.61.1.tar.gz
[apple/xnu.git] / tools / tests / darwintests / ntp_adjtime_29192647.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/mman.h>
6 #include <mach/clock_types.h>
7 #include <sys/timex.h>
8 #include <mach/mach.h>
9 #include <darwintest.h>
10 #include <darwintest_utils.h>
11
12
13 #define DAY 86400 /*1 day in sec*/
14 #define ERROR 2 /*2 us of error tolerance*/
15
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))
19 {
20 struct timeval time;
21 long new_time;
22
23 if (geteuid() != 0){
24 T_SKIP("settimeofday_29192647 test requires root privileges to run.");
25 }
26
27 T_QUIET;
28 T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
29
30 /* increment the time of one day */
31 new_time = time.tv_sec + DAY;
32
33 time.tv_sec = new_time;
34 time.tv_usec = 0;
35
36 T_LOG("Attemping to set the time one day after.");
37
38 T_WITH_ERRNO;
39 T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
40
41 T_QUIET;
42 T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
43
44 /* expext to be past new_time */
45 T_EXPECT_GE_LONG(time.tv_sec, new_time, "Time successfully changed");
46
47 /* set the time back to previous value */
48 if (time.tv_sec >= new_time) {
49 time.tv_sec = time.tv_sec - DAY;
50 time.tv_usec = 0;
51
52 T_WITH_ERRNO;
53 T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
54 }
55 }
56
57 static void get_abs_to_us_scale_factor(uint64_t* numer, uint64_t* denom){
58 struct timespec time;
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 };
63
64 T_QUIET; T_ASSERT_EQ(mach_get_times(&old_abstime, NULL, &time), KERN_SUCCESS, NULL);
65
66 old_time_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec/1000;
67
68 sleep(1);
69
70 T_QUIET; T_ASSERT_EQ(mach_get_times(&new_abstime, NULL, &time), KERN_SUCCESS, NULL);
71
72 new_time_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec/1000;
73
74 /* this is conversion factors from abs to nanos */
75 T_ASSERT_EQ(mach_timebase_info(&timebaseInfo), KERN_SUCCESS, NULL);
76
77 new_time_usec -= old_time_usec;
78 new_abstime -= old_abstime;
79
80 time_conv1 = new_abstime;
81 time_conv1 *= timebaseInfo.numer;
82 time_conv1 /= timebaseInfo.denom * 1000;
83
84 if (time_conv1 > new_time_usec)
85 diff = time_conv1 - new_time_usec;
86 else
87 diff = new_time_usec - time_conv1;
88
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);
90
91 *numer = (uint64_t)timebaseInfo.numer;
92 *denom = (uint64_t)timebaseInfo.denom * 1000;
93 }
94
95
96 #define ADJSTMENT 3333 /*3333 us*/
97 #define ADJTIME_OFFSET_PER_SEC 500
98
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))
102 {
103 struct timespec time;
104 struct timeval adj;
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;
108 long diff;
109 const char * lterdos_env = NULL;
110
111 #if defined(__i386__) || defined(__x86_64__)
112 T_SKIP("adjtime_29192647 test requires LTE to run.");
113 #endif
114
115 if (geteuid() != 0) {
116 T_SKIP("adjtime_29192647 test requires root privileges to run.");
117 }
118
119 lterdos_env = getenv("LTERDOS");
120
121 if (lterdos_env != NULL){
122 if (!(strcmp(lterdos_env, "YES") == 0)) {
123 T_SKIP("adjtime_29192647 test requires LTE to run.");
124 }
125 }
126 else {
127 T_SKIP("adjtime_29192647 test requires LTE to run.");
128 }
129
130 /*
131 * Calibrate scale factor for converting from abs time to usec
132 */
133 get_abs_to_us_scale_factor(&num, &den);
134
135 T_QUIET; T_ASSERT_EQ(mach_get_times(&old_abstime, NULL, &time), KERN_SUCCESS, NULL);
136
137 old_time_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec/1000;
138
139 adj.tv_sec = 0;
140 adj.tv_usec = ADJSTMENT;
141
142 T_LOG("Attemping to adjust the time of %d", ADJSTMENT);
143
144 /*
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.
148 */
149 T_WITH_ERRNO;
150 T_ASSERT_POSIX_ZERO(adjtime(&adj, NULL),NULL);
151
152 /*
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.
156 */
157 sleep_time = (ADJSTMENT)/(ADJTIME_OFFSET_PER_SEC)+2;
158
159 T_LOG("Waiting for %u sec\n", sleep_time);
160 sleep(sleep_time);
161
162 T_QUIET; T_ASSERT_EQ(mach_get_times(&new_abstime, NULL, &time), KERN_SUCCESS, NULL);
163
164 new_time_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec/1000;
165
166 us_delta = new_time_usec - old_time_usec;
167 us_delta -= ADJSTMENT;
168
169 /* abs time is not affected by adjtime */
170 abs_delta = new_abstime - old_abstime;
171
172 abs_delta *= num;
173 abs_delta /= den;
174
175 diff = (long) us_delta - (long) abs_delta;
176
177 /* expext that us_delta == abs_delta */
178 T_EXPECT_LE_LONG(diff, (long) ERROR, "Check abs time vs calendar time");
179
180 T_EXPECT_GE_LONG(diff, (long) -ERROR, "Check abs time vs calendar time");
181
182 }
183
184 #define FREQ_PPM 222 /*222 PPM(us/s)*/
185 #define SHIFT_PLL 4
186 #define OFFSET_US 123 /*123us*/
187
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))
191 {
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;
196 int64_t offset;
197 long diff, freq;
198 unsigned int sleep_time;
199 const char * lterdos_env = NULL;
200
201 #if defined(__i386__) || defined(__x86_64__)
202 T_SKIP("ntp_adjtime_29192647 test requires LTE to run.");
203 #endif
204
205 if (geteuid() != 0){
206 T_SKIP("ntp_adjtime_29192647 test requires root privileges to run.");
207 }
208
209 lterdos_env = getenv("LTERDOS");
210
211 if (lterdos_env != NULL){
212 if (!(strcmp(lterdos_env, "YES") == 0)) {
213 T_SKIP("adjtime_29192647 test requires LTE to run.");
214 }
215 }
216 else {
217 T_SKIP("adjtime_29192647 test requires LTE to run.");
218 }
219
220 /*
221 * Calibrate scale factor for converting from abs time to usec
222 */
223 get_abs_to_us_scale_factor(&num, &den);
224
225 /*
226 * scale frequency using ntp_adjtime;
227 */
228 memset(&ntptime, 0, sizeof(ntptime));
229
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;
235 ntptime.freq = freq;
236
237 T_LOG("Attemping to change calendar frequency of %d ppm", FREQ_PPM);
238
239 T_WITH_ERRNO;
240 T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL);
241
242 T_WITH_ERRNO;
243 T_ASSERT_EQ(ntptime.freq, freq, NULL);
244
245 sleep(2);
246
247 T_QUIET; T_ASSERT_EQ(mach_get_times(&abstime1, NULL, &time), KERN_SUCCESS, NULL);
248
249 time1_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec/1000;
250
251 sleep(1);
252
253 T_QUIET; T_ASSERT_EQ(mach_get_times(&abstime2, NULL, &time), KERN_SUCCESS, NULL);
254
255 time2_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec/1000;
256
257 abs_delta = abstime2 - abstime1;
258 us_delta = time2_usec - time1_usec;
259
260 time_conv = abs_delta;
261 time_conv *= num;
262 time_conv /= den;
263
264 app = time_conv/USEC_PER_SEC; //sec elapsed
265
266 time_delta = time_conv;
267 time_delta += app * (FREQ_PPM);
268
269 app = time_conv%USEC_PER_SEC;
270
271 time_delta += (app*(FREQ_PPM))/USEC_PER_SEC;
272
273 diff = (long) us_delta - (long) time_delta;
274
275 /* expext that us_delta == time_delta */
276 T_EXPECT_LE_LONG(diff, (long) ERROR, "Check abs time vs calendar time");
277
278 T_EXPECT_GE_LONG(diff, (long) -ERROR, "Check abs time vs calendar time");
279
280 memset(&ntptime, 0, sizeof(ntptime));
281
282 /* reset freq to zero */
283 freq = 0;
284 ntptime.modes = MOD_STATUS;
285 ntptime.status = TIME_OK;
286 ntptime.modes |= MOD_FREQUENCY;
287 ntptime.freq = freq;
288
289 T_WITH_ERRNO;
290 T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL);
291
292 T_WITH_ERRNO;
293 T_ASSERT_EQ(ntptime.freq, freq, NULL);
294
295 sleep(1);
296
297 /*
298 * adjust the phase using ntp_adjtime;
299 */
300 memset(&ntptime, 0, sizeof(ntptime));
301 ntptime.modes |= MOD_STATUS;
302 ntptime.status = TIME_OK;
303 ntptime.status |= STA_PLL|STA_FREQHOLD;
304
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;
308
309 /*
310 * The system will slew each sec of:
311 * slew = ntp.offset >> (SHIFT_PLL + time_constant);
312 * ntp.offset -= slew;
313 */
314 offset= (OFFSET_US) * 1000;
315 sleep_time = 2;
316
317 while((offset>>SHIFT_PLL)>0){
318 offset -= offset >> SHIFT_PLL;
319 sleep_time++;
320 }
321
322 T_QUIET; T_ASSERT_EQ(mach_get_times(&abstime1, NULL, &time), KERN_SUCCESS, NULL);
323
324 time1_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec/1000;
325
326 T_LOG("Attemping to change calendar phase of %d us", OFFSET_US);
327
328 T_WITH_ERRNO;
329 T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL);
330
331 T_WITH_ERRNO;
332 T_ASSERT_EQ(ntptime.offset, (long) OFFSET_US, NULL);
333
334 T_LOG("Waiting for %u sec\n", sleep_time);
335 sleep(sleep_time);
336
337 T_QUIET; T_ASSERT_EQ(mach_get_times(&abstime2, NULL, &time), KERN_SUCCESS, NULL);
338
339 time2_usec = (uint64_t)time.tv_sec * USEC_PER_SEC + (uint64_t)time.tv_nsec/1000;
340
341 abs_delta = abstime2 - abstime1;
342 us_delta = time2_usec - time1_usec;
343
344 abs_delta *= num;
345 abs_delta /= den;
346
347 us_delta -= OFFSET_US;
348
349 diff = (long) us_delta - (long) abs_delta;
350
351 /* expext that us_delta == abs_delta */
352 T_EXPECT_LE_LONG(diff, (long) ERROR, "Check abs time vs calendar time");
353
354 T_EXPECT_GE_LONG(diff, (long) -ERROR, "Check abs time vs calendar time");
355
356 memset(&ntptime, 0, sizeof(ntptime));
357 ntptime.modes = MOD_STATUS;
358 ntptime.status = TIME_OK;
359 ntptime.modes |= MOD_FREQUENCY;
360 ntptime.freq = 0;
361
362 ntptime.status |= STA_PLL;
363 ntptime.offset = 0;
364 ntptime.modes |= MOD_OFFSET;
365
366 T_WITH_ERRNO;
367 T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL);
368
369 }
370
371