tst-clock_gettime.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Test clock_gettime function.
  2. Copyright (C) 2024-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <stdbool.h>
  16. #include <stdio.h>
  17. #include <time.h>
  18. #include <support/check.h>
  19. #include <support/test-driver.h>
  20. #include <support/xsignal.h>
  21. /* Compare two struct timespec values, returning a value -1, 0 or 1. */
  22. int
  23. compare_timespec (const struct timespec *tv1, const struct timespec *tv2)
  24. {
  25. if (tv1->tv_sec < tv2->tv_sec)
  26. return -1;
  27. if (tv1->tv_sec > tv2->tv_sec)
  28. return 1;
  29. if (tv1->tv_nsec < tv2->tv_nsec)
  30. return -1;
  31. if (tv1->tv_nsec > tv2->tv_nsec)
  32. return 1;
  33. return 0;
  34. }
  35. struct test_clockid
  36. {
  37. clockid_t clockid;
  38. const char *name;
  39. bool is_cputime;
  40. bool fail_ok;
  41. };
  42. #define CLOCK(clockid) { clockid, # clockid, false, false }
  43. #define CLOCK_CPU(clockid) { clockid, # clockid, true, false }
  44. #define CLOCK_FAIL_OK(clockid) { clockid, # clockid, false, true }
  45. static const struct test_clockid clocks[] =
  46. {
  47. CLOCK (CLOCK_REALTIME),
  48. #ifdef CLOCK_MONOTONIC
  49. CLOCK (CLOCK_MONOTONIC),
  50. #endif
  51. #ifdef CLOCK_PROCESS_CPUTIME_ID
  52. CLOCK_CPU (CLOCK_PROCESS_CPUTIME_ID),
  53. #endif
  54. #ifdef CLOCK_THREAD_CPUTIME_ID
  55. CLOCK_CPU (CLOCK_THREAD_CPUTIME_ID),
  56. #endif
  57. #ifdef CLOCK_MONOTONIC_RAW
  58. CLOCK (CLOCK_MONOTONIC_RAW),
  59. #endif
  60. #ifdef CLOCK_REALTIME_COARSE
  61. CLOCK (CLOCK_REALTIME_COARSE),
  62. #endif
  63. #ifdef CLOCK_MONOTONIC_COARSE
  64. CLOCK (CLOCK_MONOTONIC_COARSE),
  65. #endif
  66. #ifdef CLOCK_BOOTTIME
  67. CLOCK (CLOCK_BOOTTIME),
  68. #endif
  69. #ifdef CLOCK_REALTIME_ALARM
  70. CLOCK_FAIL_OK (CLOCK_REALTIME_ALARM),
  71. #endif
  72. #ifdef CLOCK_BOOTTIME_ALARM
  73. CLOCK_FAIL_OK (CLOCK_BOOTTIME_ALARM),
  74. #endif
  75. #ifdef CLOCK_TAI
  76. CLOCK (CLOCK_TAI),
  77. #endif
  78. };
  79. volatile int sigalrm_received;
  80. void
  81. handle_sigalrm (int sig)
  82. {
  83. sigalrm_received = 1;
  84. }
  85. int
  86. do_test (void)
  87. {
  88. /* Verify that the calls to clock_gettime succeed, that the time does
  89. not decrease, and that time returns a truncated (not rounded)
  90. version of the time. */
  91. for (size_t i = 0; i < sizeof clocks / sizeof clocks[0]; i++)
  92. {
  93. printf ("testing %s\n", clocks[i].name);
  94. struct timespec ts1, ts2, ts3;
  95. int ret;
  96. time_t t1;
  97. t1 = time (NULL);
  98. TEST_VERIFY_EXIT (t1 != (time_t) -1);
  99. ret = clock_gettime (clocks[i].clockid, &ts1);
  100. if (clocks[i].fail_ok && ret == -1)
  101. {
  102. printf ("failed (OK for this clock): %m\n");
  103. continue;
  104. }
  105. TEST_VERIFY_EXIT (ret == 0);
  106. if (clocks[i].clockid == CLOCK_REALTIME)
  107. TEST_VERIFY (t1 <= ts1.tv_sec);
  108. TEST_VERIFY (ts1.tv_nsec >= 0);
  109. TEST_VERIFY (ts1.tv_nsec < 1000000000);
  110. ret = clock_gettime (clocks[i].clockid, &ts2);
  111. TEST_VERIFY_EXIT (ret == 0);
  112. TEST_VERIFY (compare_timespec (&ts1, &ts2) <= 0);
  113. TEST_VERIFY (ts2.tv_nsec >= 0);
  114. TEST_VERIFY (ts2.tv_nsec < 1000000000);
  115. /* Also verify that after sleeping, the time returned has
  116. increased. Repeat several times to verify that each time,
  117. the time from the time function is truncated not rounded.
  118. For CPU time clocks, the time spent spinning on the CPU, and
  119. so whether we end in the later half of a second, is not
  120. predictable; thus, only test once for those clocks. */
  121. const struct timespec duration = { .tv_nsec = 100000000 };
  122. for (int j = 0; j < 5; j++)
  123. {
  124. if (clocks[i].is_cputime)
  125. {
  126. timer_t timer;
  127. ret = timer_create (CLOCK_PROCESS_CPUTIME_ID, NULL, &timer);
  128. TEST_VERIFY_EXIT (ret == 0);
  129. sigalrm_received = 0;
  130. xsignal (SIGALRM, handle_sigalrm);
  131. struct itimerspec t =
  132. { .it_value =
  133. {
  134. .tv_sec = 0,
  135. .tv_nsec = 200000000
  136. }
  137. };
  138. ret = timer_settime (timer, 0, &t, NULL);
  139. TEST_VERIFY_EXIT (ret == 0);
  140. while (sigalrm_received == 0)
  141. ;
  142. xsignal (SIGALRM, SIG_DFL);
  143. ret = timer_delete (timer);
  144. TEST_VERIFY_EXIT (ret == 0);
  145. }
  146. else
  147. {
  148. ret = nanosleep (&duration, NULL);
  149. TEST_VERIFY_EXIT (ret == 0);
  150. }
  151. t1 = time (NULL);
  152. TEST_VERIFY_EXIT (t1 != (time_t) -1);
  153. ret = clock_gettime (clocks[i].clockid, &ts3);
  154. TEST_VERIFY_EXIT (ret == 0);
  155. TEST_VERIFY (compare_timespec (&ts2, &ts3) < 0);
  156. if (clocks[i].clockid == CLOCK_REALTIME)
  157. TEST_VERIFY (t1 <= ts3.tv_sec);
  158. TEST_VERIFY (ts3.tv_nsec >= 0);
  159. TEST_VERIFY (ts3.tv_nsec < 1000000000);
  160. ts2 = ts3;
  161. if (clocks[i].is_cputime)
  162. break;
  163. }
  164. }
  165. return 0;
  166. }
  167. #define TIMEOUT 60
  168. #include <support/test-driver.c>