nanosleep.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Make sure timers don't return early
  2. * by: john stultz ([email protected])
  3. * John Stultz ([email protected])
  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. #include "../kselftest.h"
  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. #define UNSUPPORTED 0xf00f
  45. char *clockstring(int clockid)
  46. {
  47. switch (clockid) {
  48. case CLOCK_REALTIME:
  49. return "CLOCK_REALTIME";
  50. case CLOCK_MONOTONIC:
  51. return "CLOCK_MONOTONIC";
  52. case CLOCK_PROCESS_CPUTIME_ID:
  53. return "CLOCK_PROCESS_CPUTIME_ID";
  54. case CLOCK_THREAD_CPUTIME_ID:
  55. return "CLOCK_THREAD_CPUTIME_ID";
  56. case CLOCK_MONOTONIC_RAW:
  57. return "CLOCK_MONOTONIC_RAW";
  58. case CLOCK_REALTIME_COARSE:
  59. return "CLOCK_REALTIME_COARSE";
  60. case CLOCK_MONOTONIC_COARSE:
  61. return "CLOCK_MONOTONIC_COARSE";
  62. case CLOCK_BOOTTIME:
  63. return "CLOCK_BOOTTIME";
  64. case CLOCK_REALTIME_ALARM:
  65. return "CLOCK_REALTIME_ALARM";
  66. case CLOCK_BOOTTIME_ALARM:
  67. return "CLOCK_BOOTTIME_ALARM";
  68. case CLOCK_TAI:
  69. return "CLOCK_TAI";
  70. };
  71. return "UNKNOWN_CLOCKID";
  72. }
  73. /* returns 1 if a <= b, 0 otherwise */
  74. static inline int in_order(struct timespec a, struct timespec b)
  75. {
  76. if (a.tv_sec < b.tv_sec)
  77. return 1;
  78. if (a.tv_sec > b.tv_sec)
  79. return 0;
  80. if (a.tv_nsec > b.tv_nsec)
  81. return 0;
  82. return 1;
  83. }
  84. struct timespec timespec_add(struct timespec ts, unsigned long long ns)
  85. {
  86. ts.tv_nsec += ns;
  87. while (ts.tv_nsec >= NSEC_PER_SEC) {
  88. ts.tv_nsec -= NSEC_PER_SEC;
  89. ts.tv_sec++;
  90. }
  91. return ts;
  92. }
  93. int nanosleep_test(int clockid, long long ns)
  94. {
  95. struct timespec now, target, rel;
  96. /* First check abs time */
  97. if (clock_gettime(clockid, &now))
  98. return UNSUPPORTED;
  99. target = timespec_add(now, ns);
  100. if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
  101. return UNSUPPORTED;
  102. clock_gettime(clockid, &now);
  103. if (!in_order(target, now))
  104. return -1;
  105. /* Second check reltime */
  106. clock_gettime(clockid, &now);
  107. rel.tv_sec = 0;
  108. rel.tv_nsec = 0;
  109. rel = timespec_add(rel, ns);
  110. target = timespec_add(now, ns);
  111. clock_nanosleep(clockid, 0, &rel, NULL);
  112. clock_gettime(clockid, &now);
  113. if (!in_order(target, now))
  114. return -1;
  115. return 0;
  116. }
  117. int main(int argc, char **argv)
  118. {
  119. long long length;
  120. int clockid, ret;
  121. ksft_print_header();
  122. ksft_set_plan(NR_CLOCKIDS);
  123. for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
  124. /* Skip cputime clockids since nanosleep won't increment cputime */
  125. if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
  126. clockid == CLOCK_THREAD_CPUTIME_ID ||
  127. clockid == CLOCK_HWSPECIFIC) {
  128. ksft_test_result_skip("%-31s\n", clockstring(clockid));
  129. continue;
  130. }
  131. fflush(stdout);
  132. length = 10;
  133. while (length <= (NSEC_PER_SEC * 10)) {
  134. ret = nanosleep_test(clockid, length);
  135. if (ret == UNSUPPORTED) {
  136. ksft_test_result_skip("%-31s\n", clockstring(clockid));
  137. goto next;
  138. }
  139. if (ret < 0) {
  140. ksft_test_result_fail("%-31s\n", clockstring(clockid));
  141. ksft_exit_fail();
  142. }
  143. length *= 100;
  144. }
  145. ksft_test_result_pass("%-31s\n", clockstring(clockid));
  146. next:
  147. ret = 0;
  148. }
  149. ksft_exit_pass();
  150. }