leap-a-day.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* Leap second stress test
  2. * by: John Stultz ([email protected])
  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 stressing 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. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <time.h>
  42. #include <sys/time.h>
  43. #include <sys/timex.h>
  44. #include <sys/errno.h>
  45. #include <string.h>
  46. #include <signal.h>
  47. #include <unistd.h>
  48. #include "../kselftest.h"
  49. #define NSEC_PER_SEC 1000000000ULL
  50. #define CLOCK_TAI 11
  51. time_t next_leap;
  52. int error_found;
  53. /* returns 1 if a <= b, 0 otherwise */
  54. static inline int in_order(struct timespec a, struct timespec b)
  55. {
  56. if (a.tv_sec < b.tv_sec)
  57. return 1;
  58. if (a.tv_sec > b.tv_sec)
  59. return 0;
  60. if (a.tv_nsec > b.tv_nsec)
  61. return 0;
  62. return 1;
  63. }
  64. struct timespec timespec_add(struct timespec ts, unsigned long long ns)
  65. {
  66. ts.tv_nsec += ns;
  67. while (ts.tv_nsec >= NSEC_PER_SEC) {
  68. ts.tv_nsec -= NSEC_PER_SEC;
  69. ts.tv_sec++;
  70. }
  71. return ts;
  72. }
  73. char *time_state_str(int state)
  74. {
  75. switch (state) {
  76. case TIME_OK: return "TIME_OK";
  77. case TIME_INS: return "TIME_INS";
  78. case TIME_DEL: return "TIME_DEL";
  79. case TIME_OOP: return "TIME_OOP";
  80. case TIME_WAIT: return "TIME_WAIT";
  81. case TIME_BAD: return "TIME_BAD";
  82. }
  83. return "ERROR";
  84. }
  85. /* clear NTP time_status & time_state */
  86. int clear_time_state(void)
  87. {
  88. struct timex tx;
  89. int ret;
  90. /*
  91. * We have to call adjtime twice here, as kernels
  92. * prior to 6b1859dba01c7 (included in 3.5 and
  93. * -stable), had an issue with the state machine
  94. * and wouldn't clear the STA_INS/DEL flag directly.
  95. */
  96. tx.modes = ADJ_STATUS;
  97. tx.status = STA_PLL;
  98. ret = adjtimex(&tx);
  99. /* Clear maxerror, as it can cause UNSYNC to be set */
  100. tx.modes = ADJ_MAXERROR;
  101. tx.maxerror = 0;
  102. ret = adjtimex(&tx);
  103. /* Clear the status */
  104. tx.modes = ADJ_STATUS;
  105. tx.status = 0;
  106. ret = adjtimex(&tx);
  107. return ret;
  108. }
  109. /* Make sure we cleanup on ctrl-c */
  110. void handler(int unused)
  111. {
  112. clear_time_state();
  113. exit(0);
  114. }
  115. void sigalarm(int signo)
  116. {
  117. struct timex tx;
  118. int ret;
  119. tx.modes = 0;
  120. ret = adjtimex(&tx);
  121. if (tx.time.tv_sec < next_leap) {
  122. printf("Error: Early timer expiration! (Should be %ld)\n", next_leap);
  123. error_found = 1;
  124. printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
  125. tx.time.tv_sec,
  126. tx.time.tv_usec,
  127. tx.tai,
  128. time_state_str(ret));
  129. }
  130. if (ret != TIME_WAIT) {
  131. printf("Error: Timer seeing incorrect NTP state? (Should be TIME_WAIT)\n");
  132. error_found = 1;
  133. printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
  134. tx.time.tv_sec,
  135. tx.time.tv_usec,
  136. tx.tai,
  137. time_state_str(ret));
  138. }
  139. }
  140. /* Test for known hrtimer failure */
  141. void test_hrtimer_failure(void)
  142. {
  143. struct timespec now, target;
  144. clock_gettime(CLOCK_REALTIME, &now);
  145. target = timespec_add(now, NSEC_PER_SEC/2);
  146. clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &target, NULL);
  147. clock_gettime(CLOCK_REALTIME, &now);
  148. if (!in_order(target, now)) {
  149. printf("ERROR: hrtimer early expiration failure observed.\n");
  150. error_found = 1;
  151. }
  152. }
  153. int main(int argc, char **argv)
  154. {
  155. timer_t tm1;
  156. struct itimerspec its1;
  157. struct sigevent se;
  158. struct sigaction act;
  159. int signum = SIGRTMAX;
  160. int settime = 1;
  161. int tai_time = 0;
  162. int insert = 1;
  163. int iterations = 10;
  164. int opt;
  165. /* Process arguments */
  166. while ((opt = getopt(argc, argv, "sti:")) != -1) {
  167. switch (opt) {
  168. case 'w':
  169. printf("Only setting leap-flag, not changing time. It could take up to a day for leap to trigger.\n");
  170. settime = 0;
  171. break;
  172. case 'i':
  173. iterations = atoi(optarg);
  174. break;
  175. case 't':
  176. tai_time = 1;
  177. break;
  178. default:
  179. printf("Usage: %s [-w] [-i <iterations>]\n", argv[0]);
  180. printf(" -w: Set flag and wait for leap second each iteration");
  181. printf(" (default sets time to right before leapsecond)\n");
  182. printf(" -i: Number of iterations (-1 = infinite, default is 10)\n");
  183. printf(" -t: Print TAI time\n");
  184. exit(-1);
  185. }
  186. }
  187. /* Make sure TAI support is present if -t was used */
  188. if (tai_time) {
  189. struct timespec ts;
  190. if (clock_gettime(CLOCK_TAI, &ts)) {
  191. printf("System doesn't support CLOCK_TAI\n");
  192. ksft_exit_fail();
  193. }
  194. }
  195. signal(SIGINT, handler);
  196. signal(SIGKILL, handler);
  197. /* Set up timer signal handler: */
  198. sigfillset(&act.sa_mask);
  199. act.sa_flags = 0;
  200. act.sa_handler = sigalarm;
  201. sigaction(signum, &act, NULL);
  202. if (iterations < 0)
  203. printf("This runs continuously. Press ctrl-c to stop\n");
  204. else
  205. printf("Running for %i iterations. Press ctrl-c to stop\n", iterations);
  206. printf("\n");
  207. while (1) {
  208. int ret;
  209. struct timespec ts;
  210. struct timex tx;
  211. time_t now;
  212. /* Get the current time */
  213. clock_gettime(CLOCK_REALTIME, &ts);
  214. /* Calculate the next possible leap second 23:59:60 GMT */
  215. next_leap = ts.tv_sec;
  216. next_leap += 86400 - (next_leap % 86400);
  217. if (settime) {
  218. struct timeval tv;
  219. tv.tv_sec = next_leap - 10;
  220. tv.tv_usec = 0;
  221. settimeofday(&tv, NULL);
  222. printf("Setting time to %s", ctime(&tv.tv_sec));
  223. }
  224. /* Reset NTP time state */
  225. clear_time_state();
  226. /* Set the leap second insert flag */
  227. tx.modes = ADJ_STATUS;
  228. if (insert)
  229. tx.status = STA_INS;
  230. else
  231. tx.status = STA_DEL;
  232. ret = adjtimex(&tx);
  233. if (ret < 0) {
  234. printf("Error: Problem setting STA_INS/STA_DEL!: %s\n",
  235. time_state_str(ret));
  236. return ksft_exit_fail();
  237. }
  238. /* Validate STA_INS was set */
  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. if (tai_time) {
  247. printf("Using TAI time,"
  248. " no inconsistencies should be seen!\n");
  249. }
  250. printf("Scheduling leap second for %s", ctime(&next_leap));
  251. /* Set up timer */
  252. printf("Setting timer for %ld - %s", next_leap, ctime(&next_leap));
  253. memset(&se, 0, sizeof(se));
  254. se.sigev_notify = SIGEV_SIGNAL;
  255. se.sigev_signo = signum;
  256. se.sigev_value.sival_int = 0;
  257. if (timer_create(CLOCK_REALTIME, &se, &tm1) == -1) {
  258. printf("Error: timer_create failed\n");
  259. return ksft_exit_fail();
  260. }
  261. its1.it_value.tv_sec = next_leap;
  262. its1.it_value.tv_nsec = 0;
  263. its1.it_interval.tv_sec = 0;
  264. its1.it_interval.tv_nsec = 0;
  265. timer_settime(tm1, TIMER_ABSTIME, &its1, NULL);
  266. /* Wake up 3 seconds before leap */
  267. ts.tv_sec = next_leap - 3;
  268. ts.tv_nsec = 0;
  269. while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL))
  270. printf("Something woke us up, returning to sleep\n");
  271. /* Validate STA_INS is still set */
  272. tx.modes = 0;
  273. ret = adjtimex(&tx);
  274. if (tx.status != STA_INS && tx.status != STA_DEL) {
  275. printf("Something cleared STA_INS/STA_DEL, setting it again.\n");
  276. tx.modes = ADJ_STATUS;
  277. if (insert)
  278. tx.status = STA_INS;
  279. else
  280. tx.status = STA_DEL;
  281. ret = adjtimex(&tx);
  282. }
  283. /* Check adjtimex output every half second */
  284. now = tx.time.tv_sec;
  285. while (now < next_leap + 2) {
  286. char buf[26];
  287. struct timespec tai;
  288. int ret;
  289. tx.modes = 0;
  290. ret = adjtimex(&tx);
  291. if (tai_time) {
  292. clock_gettime(CLOCK_TAI, &tai);
  293. printf("%ld sec, %9ld ns\t%s\n",
  294. tai.tv_sec,
  295. tai.tv_nsec,
  296. time_state_str(ret));
  297. } else {
  298. ctime_r(&tx.time.tv_sec, buf);
  299. buf[strlen(buf)-1] = 0; /*remove trailing\n */
  300. printf("%s + %6ld us (%i)\t%s\n",
  301. buf,
  302. tx.time.tv_usec,
  303. tx.tai,
  304. time_state_str(ret));
  305. }
  306. now = tx.time.tv_sec;
  307. /* Sleep for another half second */
  308. ts.tv_sec = 0;
  309. ts.tv_nsec = NSEC_PER_SEC / 2;
  310. clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
  311. }
  312. /* Switch to using other mode */
  313. insert = !insert;
  314. /* Note if kernel has known hrtimer failure */
  315. test_hrtimer_failure();
  316. printf("Leap complete\n");
  317. if (error_found) {
  318. printf("Errors observed\n");
  319. clear_time_state();
  320. return ksft_exit_fail();
  321. }
  322. printf("\n");
  323. if ((iterations != -1) && !(--iterations))
  324. break;
  325. }
  326. clear_time_state();
  327. return ksft_exit_pass();
  328. }