inconsistency-check.c 4.9 KB

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