inconsistency-check.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Time inconsistency check test
  2. * by: john stultz (johnstul@us.ibm.com)
  3. * (C) Copyright IBM 2003, 2004, 2005, 2012
  4. * (C) Copyright Linaro Limited 2015
  5. * Licensed under the GPLv2
  6. *
  7. * To build:
  8. * $ gcc inconsistency-check.c -o inconsistency-check -lrt
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <sys/time.h>
  25. #include <sys/timex.h>
  26. #include <string.h>
  27. #include <signal.h>
  28. #ifdef KTEST
  29. #include "../kselftest.h"
  30. #else
  31. static inline int ksft_exit_pass(void)
  32. {
  33. exit(0);
  34. }
  35. static inline int ksft_exit_fail(void)
  36. {
  37. exit(1);
  38. }
  39. #endif
  40. #define CALLS_PER_LOOP 64
  41. #define NSEC_PER_SEC 1000000000ULL
  42. #define CLOCK_REALTIME 0
  43. #define CLOCK_MONOTONIC 1
  44. #define CLOCK_PROCESS_CPUTIME_ID 2
  45. #define CLOCK_THREAD_CPUTIME_ID 3
  46. #define CLOCK_MONOTONIC_RAW 4
  47. #define CLOCK_REALTIME_COARSE 5
  48. #define CLOCK_MONOTONIC_COARSE 6
  49. #define CLOCK_BOOTTIME 7
  50. #define CLOCK_REALTIME_ALARM 8
  51. #define CLOCK_BOOTTIME_ALARM 9
  52. #define CLOCK_HWSPECIFIC 10
  53. #define CLOCK_TAI 11
  54. #define NR_CLOCKIDS 12
  55. char *clockstring(int clockid)
  56. {
  57. switch (clockid) {
  58. case CLOCK_REALTIME:
  59. return "CLOCK_REALTIME";
  60. case CLOCK_MONOTONIC:
  61. return "CLOCK_MONOTONIC";
  62. case CLOCK_PROCESS_CPUTIME_ID:
  63. return "CLOCK_PROCESS_CPUTIME_ID";
  64. case CLOCK_THREAD_CPUTIME_ID:
  65. return "CLOCK_THREAD_CPUTIME_ID";
  66. case CLOCK_MONOTONIC_RAW:
  67. return "CLOCK_MONOTONIC_RAW";
  68. case CLOCK_REALTIME_COARSE:
  69. return "CLOCK_REALTIME_COARSE";
  70. case CLOCK_MONOTONIC_COARSE:
  71. return "CLOCK_MONOTONIC_COARSE";
  72. case CLOCK_BOOTTIME:
  73. return "CLOCK_BOOTTIME";
  74. case CLOCK_REALTIME_ALARM:
  75. return "CLOCK_REALTIME_ALARM";
  76. case CLOCK_BOOTTIME_ALARM:
  77. return "CLOCK_BOOTTIME_ALARM";
  78. case CLOCK_TAI:
  79. return "CLOCK_TAI";
  80. };
  81. return "UNKNOWN_CLOCKID";
  82. }
  83. /* returns 1 if a <= b, 0 otherwise */
  84. static inline int in_order(struct timespec a, struct timespec b)
  85. {
  86. /* use unsigned to avoid false positives on 2038 rollover */
  87. if ((unsigned long)a.tv_sec < (unsigned long)b.tv_sec)
  88. return 1;
  89. if ((unsigned long)a.tv_sec > (unsigned long)b.tv_sec)
  90. return 0;
  91. if (a.tv_nsec > b.tv_nsec)
  92. return 0;
  93. return 1;
  94. }
  95. int consistency_test(int clock_type, unsigned long seconds)
  96. {
  97. struct timespec list[CALLS_PER_LOOP];
  98. int i, inconsistent;
  99. long now, then;
  100. time_t t;
  101. char *start_str;
  102. clock_gettime(clock_type, &list[0]);
  103. now = then = list[0].tv_sec;
  104. /* timestamp start of test */
  105. t = time(0);
  106. start_str = ctime(&t);
  107. while (seconds == -1 || now - then < seconds) {
  108. inconsistent = 0;
  109. /* Fill list */
  110. for (i = 0; i < CALLS_PER_LOOP; i++)
  111. clock_gettime(clock_type, &list[i]);
  112. /* Check for inconsistencies */
  113. for (i = 0; i < CALLS_PER_LOOP - 1; i++)
  114. if (!in_order(list[i], list[i+1]))
  115. inconsistent = i;
  116. /* display inconsistency */
  117. if (inconsistent) {
  118. unsigned long long delta;
  119. printf("\%s\n", start_str);
  120. for (i = 0; i < CALLS_PER_LOOP; i++) {
  121. if (i == inconsistent)
  122. printf("--------------------\n");
  123. printf("%lu:%lu\n", list[i].tv_sec,
  124. list[i].tv_nsec);
  125. if (i == inconsistent + 1)
  126. printf("--------------------\n");
  127. }
  128. delta = list[inconsistent].tv_sec * NSEC_PER_SEC;
  129. delta += list[inconsistent].tv_nsec;
  130. delta -= list[inconsistent+1].tv_sec * NSEC_PER_SEC;
  131. delta -= list[inconsistent+1].tv_nsec;
  132. printf("Delta: %llu ns\n", delta);
  133. fflush(0);
  134. /* timestamp inconsistency*/
  135. t = time(0);
  136. printf("%s\n", ctime(&t));
  137. printf("[FAILED]\n");
  138. return -1;
  139. }
  140. now = list[0].tv_sec;
  141. }
  142. printf("[OK]\n");
  143. return 0;
  144. }
  145. int main(int argc, char *argv[])
  146. {
  147. int clockid, opt;
  148. int userclock = CLOCK_REALTIME;
  149. int maxclocks = NR_CLOCKIDS;
  150. int runtime = 10;
  151. struct timespec ts;
  152. /* Process arguments */
  153. while ((opt = getopt(argc, argv, "t:c:")) != -1) {
  154. switch (opt) {
  155. case 't':
  156. runtime = atoi(optarg);
  157. break;
  158. case 'c':
  159. userclock = atoi(optarg);
  160. maxclocks = userclock + 1;
  161. break;
  162. default:
  163. printf("Usage: %s [-t <secs>] [-c <clockid>]\n", argv[0]);
  164. printf(" -t: Number of seconds to run\n");
  165. printf(" -c: clockid to use (default, all clockids)\n");
  166. exit(-1);
  167. }
  168. }
  169. setbuf(stdout, NULL);
  170. for (clockid = userclock; clockid < maxclocks; clockid++) {
  171. if (clockid == CLOCK_HWSPECIFIC)
  172. continue;
  173. if (!clock_gettime(clockid, &ts)) {
  174. printf("Consistent %-30s ", clockstring(clockid));
  175. if (consistency_test(clockid, runtime))
  176. return ksft_exit_fail();
  177. }
  178. }
  179. return ksft_exit_pass();
  180. }