leap-a-day.c 9.0 KB

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