selftests: timers: Add leap-second timer edge testing to leap-a-day.c
[cascardo/linux.git] / tools / testing / selftests / timers / leap-a-day.c
1 /* Leap second stress test
2  *              by: John Stultz (john.stultz@linaro.org)
3  *              (C) Copyright IBM 2012
4  *              (C) Copyright 2013, 2015 Linaro Limited
5  *              Licensed under the GPLv2
6  *
7  *  This test signals the kernel to insert a leap second
8  *  every day at midnight GMT. This allows for stessing the
9  *  kernel's leap-second behavior, as well as how well applications
10  *  handle the leap-second discontinuity.
11  *
12  *  Usage: leap-a-day [-s] [-i <num>]
13  *
14  *  Options:
15  *      -s:     Each iteration, set the date to 10 seconds before midnight GMT.
16  *              This speeds up the number of leapsecond transitions tested,
17  *              but because it calls settimeofday frequently, advancing the
18  *              time by 24 hours every ~16 seconds, it may cause application
19  *              disruption.
20  *
21  *      -i:     Number of iterations to run (default: infinite)
22  *
23  *  Other notes: Disabling NTP prior to running this is advised, as the two
24  *               may conflict in their commands to the kernel.
25  *
26  *  To build:
27  *      $ gcc leap-a-day.c -o leap-a-day -lrt
28  *
29  *   This program is free software: you can redistribute it and/or modify
30  *   it under the terms of the GNU General Public License as published by
31  *   the Free Software Foundation, either version 2 of the License, or
32  *   (at your option) any later version.
33  *
34  *   This program is distributed in the hope that it will be useful,
35  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
36  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  *   GNU General Public License for more details.
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 <sys/errno.h>
48 #include <string.h>
49 #include <signal.h>
50 #include <unistd.h>
51 #ifdef KTEST
52 #include "../kselftest.h"
53 #else
54 static inline int ksft_exit_pass(void)
55 {
56         exit(0);
57 }
58 static inline int ksft_exit_fail(void)
59 {
60         exit(1);
61 }
62 #endif
63
64 #define NSEC_PER_SEC 1000000000ULL
65 #define CLOCK_TAI 11
66
67 time_t next_leap;
68 int error_found;
69
70 /* returns 1 if a <= b, 0 otherwise */
71 static inline int in_order(struct timespec a, struct timespec b)
72 {
73         if (a.tv_sec < b.tv_sec)
74                 return 1;
75         if (a.tv_sec > b.tv_sec)
76                 return 0;
77         if (a.tv_nsec > b.tv_nsec)
78                 return 0;
79         return 1;
80 }
81
82 struct timespec timespec_add(struct timespec ts, unsigned long long ns)
83 {
84         ts.tv_nsec += ns;
85         while (ts.tv_nsec >= NSEC_PER_SEC) {
86                 ts.tv_nsec -= NSEC_PER_SEC;
87                 ts.tv_sec++;
88         }
89         return ts;
90 }
91
92 char *time_state_str(int state)
93 {
94         switch (state) {
95         case TIME_OK:   return "TIME_OK";
96         case TIME_INS:  return "TIME_INS";
97         case TIME_DEL:  return "TIME_DEL";
98         case TIME_OOP:  return "TIME_OOP";
99         case TIME_WAIT: return "TIME_WAIT";
100         case TIME_BAD:  return "TIME_BAD";
101         }
102         return "ERROR";
103 }
104
105 /* clear NTP time_status & time_state */
106 int clear_time_state(void)
107 {
108         struct timex tx;
109         int ret;
110
111         /*
112          * We have to call adjtime twice here, as kernels
113          * prior to 6b1859dba01c7 (included in 3.5 and
114          * -stable), had an issue with the state machine
115          * and wouldn't clear the STA_INS/DEL flag directly.
116          */
117         tx.modes = ADJ_STATUS;
118         tx.status = STA_PLL;
119         ret = adjtimex(&tx);
120
121         /* Clear maxerror, as it can cause UNSYNC to be set */
122         tx.modes = ADJ_MAXERROR;
123         tx.maxerror = 0;
124         ret = adjtimex(&tx);
125
126         /* Clear the status */
127         tx.modes = ADJ_STATUS;
128         tx.status = 0;
129         ret = adjtimex(&tx);
130
131         return ret;
132 }
133
134 /* Make sure we cleanup on ctrl-c */
135 void handler(int unused)
136 {
137         clear_time_state();
138         exit(0);
139 }
140
141 void sigalarm(int signo)
142 {
143         struct timex tx;
144         char buf[26];
145         int ret;
146
147         tx.modes = 0;
148         ret = adjtimex(&tx);
149
150         ctime_r(&tx.time.tv_sec, buf);
151         buf[strlen(buf)-1] = 0; /*remove trailing\n */
152         printf("%s + %6ld us (%i)\t%s - TIMER FIRED\n",
153                                         buf,
154                                         tx.time.tv_usec,
155                                         tx.tai,
156                                         time_state_str(ret));
157
158         if (tx.time.tv_sec < next_leap) {
159                 printf("Error: Early timer expiration!\n");
160                 error_found = 1;
161         }
162         if (ret != TIME_WAIT) {
163                 printf("Error: Incorrect NTP state?\n");
164                 error_found = 1;
165         }
166 }
167
168
169 /* Test for known hrtimer failure */
170 void test_hrtimer_failure(void)
171 {
172         struct timespec now, target;
173
174         clock_gettime(CLOCK_REALTIME, &now);
175         target = timespec_add(now, NSEC_PER_SEC/2);
176         clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &target, NULL);
177         clock_gettime(CLOCK_REALTIME, &now);
178
179         if (!in_order(target, now)) {
180                 printf("ERROR: hrtimer early expiration failure observed.\n");
181                 error_found = 1;
182         }
183 }
184
185 int main(int argc, char **argv)
186 {
187         timer_t tm1;
188         struct itimerspec its1;
189         struct sigevent se;
190         struct sigaction act;
191         int signum = SIGRTMAX;
192         int settime = 0;
193         int tai_time = 0;
194         int insert = 1;
195         int iterations = -1;
196         int opt;
197
198         /* Process arguments */
199         while ((opt = getopt(argc, argv, "sti:")) != -1) {
200                 switch (opt) {
201                 case 's':
202                         printf("Setting time to speed up testing\n");
203                         settime = 1;
204                         break;
205                 case 'i':
206                         iterations = atoi(optarg);
207                         break;
208                 case 't':
209                         tai_time = 1;
210                         break;
211                 default:
212                         printf("Usage: %s [-s] [-i <iterations>]\n", argv[0]);
213                         printf("        -s: Set time to right before leap second each iteration\n");
214                         printf("        -i: Number of iterations\n");
215                         printf("        -t: Print TAI time\n");
216                         exit(-1);
217                 }
218         }
219
220         /* Make sure TAI support is present if -t was used */
221         if (tai_time) {
222                 struct timespec ts;
223
224                 if (clock_gettime(CLOCK_TAI, &ts)) {
225                         printf("System doesn't support CLOCK_TAI\n");
226                         ksft_exit_fail();
227                 }
228         }
229
230         signal(SIGINT, handler);
231         signal(SIGKILL, handler);
232
233         /* Set up timer signal handler: */
234         sigfillset(&act.sa_mask);
235         act.sa_flags = 0;
236         act.sa_handler = sigalarm;
237         sigaction(signum, &act, NULL);
238
239         if (iterations < 0)
240                 printf("This runs continuously. Press ctrl-c to stop\n");
241         else
242                 printf("Running for %i iterations. Press ctrl-c to stop\n", iterations);
243
244         printf("\n");
245         while (1) {
246                 int ret;
247                 struct timespec ts;
248                 struct timex tx;
249                 time_t now;
250
251                 /* Get the current time */
252                 clock_gettime(CLOCK_REALTIME, &ts);
253
254                 /* Calculate the next possible leap second 23:59:60 GMT */
255                 next_leap = ts.tv_sec;
256                 next_leap += 86400 - (next_leap % 86400);
257
258                 if (settime) {
259                         struct timeval tv;
260
261                         tv.tv_sec = next_leap - 10;
262                         tv.tv_usec = 0;
263                         settimeofday(&tv, NULL);
264                         printf("Setting time to %s", ctime(&tv.tv_sec));
265                 }
266
267                 /* Reset NTP time state */
268                 clear_time_state();
269
270                 /* Set the leap second insert flag */
271                 tx.modes = ADJ_STATUS;
272                 if (insert)
273                         tx.status = STA_INS;
274                 else
275                         tx.status = STA_DEL;
276                 ret = adjtimex(&tx);
277                 if (ret < 0) {
278                         printf("Error: Problem setting STA_INS/STA_DEL!: %s\n",
279                                                         time_state_str(ret));
280                         return ksft_exit_fail();
281                 }
282
283                 /* Validate STA_INS was set */
284                 tx.modes = 0;
285                 ret = adjtimex(&tx);
286                 if (tx.status != STA_INS && tx.status != STA_DEL) {
287                         printf("Error: STA_INS/STA_DEL not set!: %s\n",
288                                                         time_state_str(ret));
289                         return ksft_exit_fail();
290                 }
291
292                 if (tai_time) {
293                         printf("Using TAI time,"
294                                 " no inconsistencies should be seen!\n");
295                 }
296
297                 printf("Scheduling leap second for %s", ctime(&next_leap));
298
299                 /* Set up timer */
300                 printf("Setting timer for %s", ctime(&next_leap));
301                 memset(&se, 0, sizeof(se));
302                 se.sigev_notify = SIGEV_SIGNAL;
303                 se.sigev_signo = signum;
304                 se.sigev_value.sival_int = 0;
305                 if (timer_create(CLOCK_REALTIME, &se, &tm1) == -1) {
306                         printf("Error: timer_create failed\n");
307                         return ksft_exit_fail();
308                 }
309                 its1.it_value.tv_sec = next_leap;
310                 its1.it_value.tv_nsec = 0;
311                 its1.it_interval.tv_sec = 0;
312                 its1.it_interval.tv_nsec = 0;
313                 timer_settime(tm1, TIMER_ABSTIME, &its1, NULL);
314
315                 /* Wake up 3 seconds before leap */
316                 ts.tv_sec = next_leap - 3;
317                 ts.tv_nsec = 0;
318
319
320                 while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL))
321                         printf("Something woke us up, returning to sleep\n");
322
323                 /* Validate STA_INS is still set */
324                 tx.modes = 0;
325                 ret = adjtimex(&tx);
326                 if (tx.status != STA_INS && tx.status != STA_DEL) {
327                         printf("Something cleared STA_INS/STA_DEL, setting it again.\n");
328                         tx.modes = ADJ_STATUS;
329                         if (insert)
330                                 tx.status = STA_INS;
331                         else
332                                 tx.status = STA_DEL;
333                         ret = adjtimex(&tx);
334                 }
335
336                 /* Check adjtimex output every half second */
337                 now = tx.time.tv_sec;
338                 while (now < next_leap + 2) {
339                         char buf[26];
340                         struct timespec tai;
341                         int ret;
342
343                         tx.modes = 0;
344                         ret = adjtimex(&tx);
345
346                         if (tai_time) {
347                                 clock_gettime(CLOCK_TAI, &tai);
348                                 printf("%ld sec, %9ld ns\t%s\n",
349                                                 tai.tv_sec,
350                                                 tai.tv_nsec,
351                                                 time_state_str(ret));
352                         } else {
353                                 ctime_r(&tx.time.tv_sec, buf);
354                                 buf[strlen(buf)-1] = 0; /*remove trailing\n */
355
356                                 printf("%s + %6ld us (%i)\t%s\n",
357                                                 buf,
358                                                 tx.time.tv_usec,
359                                                 tx.tai,
360                                                 time_state_str(ret));
361                         }
362                         now = tx.time.tv_sec;
363                         /* Sleep for another half second */
364                         ts.tv_sec = 0;
365                         ts.tv_nsec = NSEC_PER_SEC / 2;
366                         clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
367                 }
368                 /* Switch to using other mode */
369                 insert = !insert;
370
371                 /* Note if kernel has known hrtimer failure */
372                 test_hrtimer_failure();
373
374                 printf("Leap complete\n");
375                 if (error_found) {
376                         printf("Errors observed\n");
377                         clear_time_state();
378                         return ksft_exit_fail();
379                 }
380                 printf("\n");
381                 if ((iterations != -1) && !(--iterations))
382                         break;
383         }
384
385         clear_time_state();
386         return ksft_exit_pass();
387 }