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#include <stdio.h>
43#include <stdlib.h>
44#include <time.h>
45#include <sys/time.h>
46#include <sys/timex.h>
47#include <string.h>
48#include <signal.h>
49#include <unistd.h>
50#ifdef KTEST
51#include "../kselftest.h"
52#else
53static inline int ksft_exit_pass(void)
54{
55 exit(0);
56}
57static inline int ksft_exit_fail(void)
58{
59 exit(1);
60}
61#endif
62
63#define NSEC_PER_SEC 1000000000ULL
64#define CLOCK_TAI 11
65
66
67static inline int in_order(struct timespec a, struct timespec b)
68{
69 if (a.tv_sec < b.tv_sec)
70 return 1;
71 if (a.tv_sec > b.tv_sec)
72 return 0;
73 if (a.tv_nsec > b.tv_nsec)
74 return 0;
75 return 1;
76}
77
78struct timespec timespec_add(struct timespec ts, unsigned long long ns)
79{
80 ts.tv_nsec += ns;
81 while (ts.tv_nsec >= NSEC_PER_SEC) {
82 ts.tv_nsec -= NSEC_PER_SEC;
83 ts.tv_sec++;
84 }
85 return ts;
86}
87
88char *time_state_str(int state)
89{
90 switch (state) {
91 case TIME_OK: return "TIME_OK";
92 case TIME_INS: return "TIME_INS";
93 case TIME_DEL: return "TIME_DEL";
94 case TIME_OOP: return "TIME_OOP";
95 case TIME_WAIT: return "TIME_WAIT";
96 case TIME_BAD: return "TIME_BAD";
97 }
98 return "ERROR";
99}
100
101
102int clear_time_state(void)
103{
104 struct timex tx;
105 int ret;
106
107
108
109
110
111
112
113 tx.modes = ADJ_STATUS;
114 tx.status = STA_PLL;
115 ret = adjtimex(&tx);
116
117
118 tx.modes = ADJ_MAXERROR;
119 tx.maxerror = 0;
120 ret = adjtimex(&tx);
121
122
123 tx.modes = ADJ_STATUS;
124 tx.status = 0;
125 ret = adjtimex(&tx);
126
127 return ret;
128}
129
130
131void handler(int unused)
132{
133 clear_time_state();
134 exit(0);
135}
136
137
138void test_hrtimer_failure(void)
139{
140 struct timespec now, target;
141
142 clock_gettime(CLOCK_REALTIME, &now);
143 target = timespec_add(now, NSEC_PER_SEC/2);
144 clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &target, NULL);
145 clock_gettime(CLOCK_REALTIME, &now);
146
147 if (!in_order(target, now))
148 printf("ERROR: hrtimer early expiration failure observed.\n");
149}
150
151int main(int argc, char **argv)
152{
153 int settime = 0;
154 int tai_time = 0;
155 int insert = 1;
156 int iterations = -1;
157 int opt;
158
159
160 while ((opt = getopt(argc, argv, "sti:")) != -1) {
161 switch (opt) {
162 case 's':
163 printf("Setting time to speed up testing\n");
164 settime = 1;
165 break;
166 case 'i':
167 iterations = atoi(optarg);
168 break;
169 case 't':
170 tai_time = 1;
171 break;
172 default:
173 printf("Usage: %s [-s] [-i <iterations>]\n", argv[0]);
174 printf(" -s: Set time to right before leap second each iteration\n");
175 printf(" -i: Number of iterations\n");
176 printf(" -t: Print TAI time\n");
177 exit(-1);
178 }
179 }
180
181
182 if (tai_time) {
183 struct timespec ts;
184
185 if (clock_gettime(CLOCK_TAI, &ts)) {
186 printf("System doesn't support CLOCK_TAI\n");
187 ksft_exit_fail();
188 }
189 }
190
191 signal(SIGINT, handler);
192 signal(SIGKILL, handler);
193
194 if (iterations < 0)
195 printf("This runs continuously. Press ctrl-c to stop\n");
196 else
197 printf("Running for %i iterations. Press ctrl-c to stop\n", iterations);
198
199 printf("\n");
200 while (1) {
201 int ret;
202 struct timespec ts;
203 struct timex tx;
204 time_t now, next_leap;
205
206
207 clock_gettime(CLOCK_REALTIME, &ts);
208
209
210 next_leap = ts.tv_sec;
211 next_leap += 86400 - (next_leap % 86400);
212
213 if (settime) {
214 struct timeval tv;
215
216 tv.tv_sec = next_leap - 10;
217 tv.tv_usec = 0;
218 settimeofday(&tv, NULL);
219 printf("Setting time to %s", ctime(&tv.tv_sec));
220 }
221
222
223 clear_time_state();
224
225
226 tx.modes = ADJ_STATUS;
227 if (insert)
228 tx.status = STA_INS;
229 else
230 tx.status = STA_DEL;
231 ret = adjtimex(&tx);
232 if (ret < 0) {
233 printf("Error: Problem setting STA_INS/STA_DEL!: %s\n",
234 time_state_str(ret));
235 return ksft_exit_fail();
236 }
237
238
239 tx.modes = 0;
240 ret = adjtimex(&tx);
241 if (tx.status != STA_INS && tx.status != STA_DEL) {
242 printf("Error: STA_INS/STA_DEL not set!: %s\n",
243 time_state_str(ret));
244 return ksft_exit_fail();
245 }
246
247 if (tai_time) {
248 printf("Using TAI time,"
249 " no inconsistencies should be seen!\n");
250 }
251
252 printf("Scheduling leap second for %s", ctime(&next_leap));
253
254
255 ts.tv_sec = next_leap - 3;
256 ts.tv_nsec = 0;
257
258 while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL))
259 printf("Something woke us up, returning to sleep\n");
260
261
262 tx.modes = 0;
263 ret = adjtimex(&tx);
264 if (tx.status != STA_INS && tx.status != STA_DEL) {
265 printf("Something cleared STA_INS/STA_DEL, setting it again.\n");
266 tx.modes = ADJ_STATUS;
267 if (insert)
268 tx.status = STA_INS;
269 else
270 tx.status = STA_DEL;
271 ret = adjtimex(&tx);
272 }
273
274
275 now = tx.time.tv_sec;
276 while (now < next_leap + 2) {
277 char buf[26];
278 struct timespec tai;
279
280 tx.modes = 0;
281 ret = adjtimex(&tx);
282
283 if (tai_time) {
284 clock_gettime(CLOCK_TAI, &tai);
285 printf("%ld sec, %9ld ns\t%s\n",
286 tai.tv_sec,
287 tai.tv_nsec,
288 time_state_str(ret));
289 } else {
290 ctime_r(&tx.time.tv_sec, buf);
291 buf[strlen(buf)-1] = 0;
292
293 printf("%s + %6ld us (%i)\t%s\n",
294 buf,
295 tx.time.tv_usec,
296 tx.tai,
297 time_state_str(ret));
298 }
299 now = tx.time.tv_sec;
300
301 ts.tv_sec = 0;
302 ts.tv_nsec = NSEC_PER_SEC / 2;
303 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
304 }
305
306 insert = !insert;
307
308
309 test_hrtimer_failure();
310
311 printf("Leap complete\n\n");
312
313 if ((iterations != -1) && !(--iterations))
314 break;
315 }
316
317 clear_time_state();
318 return ksft_exit_pass();
319}
320