nanosleep.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <errno.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. #include <sys/time.h>
  26. #include <sys/timex.h>
  27. #include <string.h>
  28. #include <signal.h>
  29. #ifdef KTEST
  30. #include "../kselftest.h"
  31. #else
  32. static inline int ksft_exit_pass(void)
  33. {
  34. exit(0);
  35. }
  36. static inline int ksft_exit_fail(void)
  37. {
  38. exit(1);
  39. }
  40. #endif
  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. #define UNSUPPORTED 0xf00f
  56. char *clockstring(int clockid)
  57. {
  58. switch (clockid) {
  59. case CLOCK_REALTIME:
  60. return "CLOCK_REALTIME";
  61. case CLOCK_MONOTONIC:
  62. return "CLOCK_MONOTONIC";
  63. case CLOCK_PROCESS_CPUTIME_ID:
  64. return "CLOCK_PROCESS_CPUTIME_ID";
  65. case CLOCK_THREAD_CPUTIME_ID:
  66. return "CLOCK_THREAD_CPUTIME_ID";
  67. case CLOCK_MONOTONIC_RAW:
  68. return "CLOCK_MONOTONIC_RAW";
  69. case CLOCK_REALTIME_COARSE:
  70. return "CLOCK_REALTIME_COARSE";
  71. case CLOCK_MONOTONIC_COARSE:
  72. return "CLOCK_MONOTONIC_COARSE";
  73. case CLOCK_BOOTTIME:
  74. return "CLOCK_BOOTTIME";
  75. case CLOCK_REALTIME_ALARM:
  76. return "CLOCK_REALTIME_ALARM";
  77. case CLOCK_BOOTTIME_ALARM:
  78. return "CLOCK_BOOTTIME_ALARM";
  79. case CLOCK_TAI:
  80. return "CLOCK_TAI";
  81. };
  82. return "UNKNOWN_CLOCKID";
  83. }
  84. /* returns 1 if a <= b, 0 otherwise */
  85. static inline int in_order(struct timespec a, struct timespec b)
  86. {
  87. if (a.tv_sec < b.tv_sec)
  88. return 1;
  89. if (a.tv_sec > b.tv_sec)
  90. return 0;
  91. if (a.tv_nsec > b.tv_nsec)
  92. return 0;
  93. return 1;
  94. }
  95. struct timespec timespec_add(struct timespec ts, unsigned long long ns)
  96. {
  97. ts.tv_nsec += ns;
  98. while (ts.tv_nsec >= NSEC_PER_SEC) {
  99. ts.tv_nsec -= NSEC_PER_SEC;
  100. ts.tv_sec++;
  101. }
  102. return ts;
  103. }
  104. int nanosleep_test(int clockid, long long ns)
  105. {
  106. struct timespec now, target, rel;
  107. /* First check abs time */
  108. if (clock_gettime(clockid, &now))
  109. return UNSUPPORTED;
  110. target = timespec_add(now, ns);
  111. if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
  112. return UNSUPPORTED;
  113. clock_gettime(clockid, &now);
  114. if (!in_order(target, now))
  115. return -1;
  116. /* Second check reltime */
  117. clock_gettime(clockid, &now);
  118. rel.tv_sec = 0;
  119. rel.tv_nsec = 0;
  120. rel = timespec_add(rel, ns);
  121. target = timespec_add(now, ns);
  122. clock_nanosleep(clockid, 0, &rel, NULL);
  123. clock_gettime(clockid, &now);
  124. if (!in_order(target, now))
  125. return -1;
  126. return 0;
  127. }
  128. int main(int argc, char **argv)
  129. {
  130. long long length;
  131. int clockid, ret;
  132. for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
  133. /* Skip cputime clockids since nanosleep won't increment cputime */
  134. if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
  135. clockid == CLOCK_THREAD_CPUTIME_ID ||
  136. clockid == CLOCK_HWSPECIFIC)
  137. continue;
  138. printf("Nanosleep %-31s ", clockstring(clockid));
  139. length = 10;
  140. while (length <= (NSEC_PER_SEC * 10)) {
  141. ret = nanosleep_test(clockid, length);
  142. if (ret == UNSUPPORTED) {
  143. printf("[UNSUPPORTED]\n");
  144. goto next;
  145. }
  146. if (ret < 0) {
  147. printf("[FAILED]\n");
  148. return ksft_exit_fail();
  149. }
  150. length *= 100;
  151. }
  152. printf("[OK]\n");
  153. next:
  154. ret = 0;
  155. }
  156. return ksft_exit_pass();
  157. }