nanosleep.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Make sure timers don't return early
  2. * by: john stultz (johnstul@us.ibm.com)
  3. * John Stultz (john.stultz@linaro.org)
  4. * (C) Copyright IBM 2012
  5. * (C) Copyright Linaro 2013 2015
  6. * Licensed under the GPLv2
  7. *
  8. * To build:
  9. * $ gcc nanosleep.c -o nanosleep -lrt
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include <stdio.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 NSEC_PER_SEC 1000000000ULL
  41. #define CLOCK_REALTIME 0
  42. #define CLOCK_MONOTONIC 1
  43. #define CLOCK_PROCESS_CPUTIME_ID 2
  44. #define CLOCK_THREAD_CPUTIME_ID 3
  45. #define CLOCK_MONOTONIC_RAW 4
  46. #define CLOCK_REALTIME_COARSE 5
  47. #define CLOCK_MONOTONIC_COARSE 6
  48. #define CLOCK_BOOTTIME 7
  49. #define CLOCK_REALTIME_ALARM 8
  50. #define CLOCK_BOOTTIME_ALARM 9
  51. #define CLOCK_HWSPECIFIC 10
  52. #define CLOCK_TAI 11
  53. #define NR_CLOCKIDS 12
  54. #define UNSUPPORTED 0xf00f
  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. if (a.tv_sec < b.tv_sec)
  87. return 1;
  88. if (a.tv_sec > b.tv_sec)
  89. return 0;
  90. if (a.tv_nsec > b.tv_nsec)
  91. return 0;
  92. return 1;
  93. }
  94. struct timespec timespec_add(struct timespec ts, unsigned long long ns)
  95. {
  96. ts.tv_nsec += ns;
  97. while (ts.tv_nsec >= NSEC_PER_SEC) {
  98. ts.tv_nsec -= NSEC_PER_SEC;
  99. ts.tv_sec++;
  100. }
  101. return ts;
  102. }
  103. int nanosleep_test(int clockid, long long ns)
  104. {
  105. struct timespec now, target, rel;
  106. /* First check abs time */
  107. if (clock_gettime(clockid, &now))
  108. return UNSUPPORTED;
  109. target = timespec_add(now, ns);
  110. if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
  111. return UNSUPPORTED;
  112. clock_gettime(clockid, &now);
  113. if (!in_order(target, now))
  114. return -1;
  115. /* Second check reltime */
  116. clock_gettime(clockid, &now);
  117. rel.tv_sec = 0;
  118. rel.tv_nsec = 0;
  119. rel = timespec_add(rel, ns);
  120. target = timespec_add(now, ns);
  121. clock_nanosleep(clockid, 0, &rel, NULL);
  122. clock_gettime(clockid, &now);
  123. if (!in_order(target, now))
  124. return -1;
  125. return 0;
  126. }
  127. int main(int argc, char **argv)
  128. {
  129. long long length;
  130. int clockid, ret;
  131. for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
  132. /* Skip cputime clockids since nanosleep won't increment cputime */
  133. if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
  134. clockid == CLOCK_THREAD_CPUTIME_ID ||
  135. clockid == CLOCK_HWSPECIFIC)
  136. continue;
  137. printf("Nanosleep %-31s ", clockstring(clockid));
  138. length = 10;
  139. while (length <= (NSEC_PER_SEC * 10)) {
  140. ret = nanosleep_test(clockid, length);
  141. if (ret == UNSUPPORTED) {
  142. printf("[UNSUPPORTED]\n");
  143. goto next;
  144. }
  145. if (ret < 0) {
  146. printf("[FAILED]\n");
  147. return ksft_exit_fail();
  148. }
  149. length *= 100;
  150. }
  151. printf("[OK]\n");
  152. next:
  153. ret = 0;
  154. }
  155. return ksft_exit_pass();
  156. }