inconsistency-check.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include <include/vdso/time64.h>
  29. #include "kselftest.h"
  30. /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */
  31. #define CLOCK_HWSPECIFIC 10
  32. #define CALLS_PER_LOOP 64
  33. char *clockstring(int clockid)
  34. {
  35. switch (clockid) {
  36. case CLOCK_REALTIME:
  37. return "CLOCK_REALTIME";
  38. case CLOCK_MONOTONIC:
  39. return "CLOCK_MONOTONIC";
  40. case CLOCK_PROCESS_CPUTIME_ID:
  41. return "CLOCK_PROCESS_CPUTIME_ID";
  42. case CLOCK_THREAD_CPUTIME_ID:
  43. return "CLOCK_THREAD_CPUTIME_ID";
  44. case CLOCK_MONOTONIC_RAW:
  45. return "CLOCK_MONOTONIC_RAW";
  46. case CLOCK_REALTIME_COARSE:
  47. return "CLOCK_REALTIME_COARSE";
  48. case CLOCK_MONOTONIC_COARSE:
  49. return "CLOCK_MONOTONIC_COARSE";
  50. case CLOCK_BOOTTIME:
  51. return "CLOCK_BOOTTIME";
  52. case CLOCK_REALTIME_ALARM:
  53. return "CLOCK_REALTIME_ALARM";
  54. case CLOCK_BOOTTIME_ALARM:
  55. return "CLOCK_BOOTTIME_ALARM";
  56. case CLOCK_TAI:
  57. return "CLOCK_TAI";
  58. }
  59. return "UNKNOWN_CLOCKID";
  60. }
  61. /* returns 1 if a <= b, 0 otherwise */
  62. static inline int in_order(struct timespec a, struct timespec b)
  63. {
  64. /* use unsigned to avoid false positives on 2038 rollover */
  65. if ((unsigned long)a.tv_sec < (unsigned long)b.tv_sec)
  66. return 1;
  67. if ((unsigned long)a.tv_sec > (unsigned long)b.tv_sec)
  68. return 0;
  69. if (a.tv_nsec > b.tv_nsec)
  70. return 0;
  71. return 1;
  72. }
  73. int consistency_test(int clock_type, unsigned long seconds)
  74. {
  75. struct timespec list[CALLS_PER_LOOP];
  76. int i, inconsistent;
  77. long now, then;
  78. time_t t;
  79. char *start_str;
  80. clock_gettime(clock_type, &list[0]);
  81. now = then = list[0].tv_sec;
  82. /* timestamp start of test */
  83. t = time(0);
  84. start_str = ctime(&t);
  85. while (seconds == -1 || now - then < seconds) {
  86. inconsistent = -1;
  87. /* Fill list */
  88. for (i = 0; i < CALLS_PER_LOOP; i++)
  89. clock_gettime(clock_type, &list[i]);
  90. /* Check for inconsistencies */
  91. for (i = 0; i < CALLS_PER_LOOP - 1; i++)
  92. if (!in_order(list[i], list[i+1]))
  93. inconsistent = i;
  94. /* display inconsistency */
  95. if (inconsistent >= 0) {
  96. unsigned long long delta;
  97. ksft_print_msg("\%s\n", start_str);
  98. for (i = 0; i < CALLS_PER_LOOP; i++) {
  99. if (i == inconsistent)
  100. ksft_print_msg("--------------------\n");
  101. ksft_print_msg("%lu:%lu\n", list[i].tv_sec,
  102. list[i].tv_nsec);
  103. if (i == inconsistent + 1)
  104. ksft_print_msg("--------------------\n");
  105. }
  106. delta = list[inconsistent].tv_sec * NSEC_PER_SEC;
  107. delta += list[inconsistent].tv_nsec;
  108. delta -= list[inconsistent+1].tv_sec * NSEC_PER_SEC;
  109. delta -= list[inconsistent+1].tv_nsec;
  110. ksft_print_msg("Delta: %llu ns\n", delta);
  111. fflush(0);
  112. /* timestamp inconsistency*/
  113. t = time(0);
  114. ksft_print_msg("%s\n", ctime(&t));
  115. return -1;
  116. }
  117. now = list[0].tv_sec;
  118. }
  119. return 0;
  120. }
  121. int main(int argc, char *argv[])
  122. {
  123. int clockid, opt;
  124. int userclock = CLOCK_REALTIME;
  125. int maxclocks = CLOCK_TAI + 1;
  126. int runtime = 10;
  127. struct timespec ts;
  128. /* Process arguments */
  129. while ((opt = getopt(argc, argv, "t:c:")) != -1) {
  130. switch (opt) {
  131. case 't':
  132. runtime = atoi(optarg);
  133. break;
  134. case 'c':
  135. userclock = atoi(optarg);
  136. maxclocks = userclock + 1;
  137. break;
  138. default:
  139. printf("Usage: %s [-t <secs>] [-c <clockid>]\n", argv[0]);
  140. printf(" -t: Number of seconds to run\n");
  141. printf(" -c: clockid to use (default, all clockids)\n");
  142. exit(-1);
  143. }
  144. }
  145. setbuf(stdout, NULL);
  146. ksft_print_header();
  147. ksft_set_plan(maxclocks - userclock);
  148. for (clockid = userclock; clockid < maxclocks; clockid++) {
  149. if (clockid == CLOCK_HWSPECIFIC || clock_gettime(clockid, &ts)) {
  150. ksft_test_result_skip("%-31s\n", clockstring(clockid));
  151. continue;
  152. }
  153. if (consistency_test(clockid, runtime)) {
  154. ksft_test_result_fail("%-31s\n", clockstring(clockid));
  155. ksft_exit_fail();
  156. } else {
  157. ksft_test_result_pass("%-31s\n", clockstring(clockid));
  158. }
  159. }
  160. ksft_exit_pass();
  161. }