wait-timeout.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various timeout tests
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <sys/time.h>
  14. #include <sys/wait.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <assert.h>
  18. #include "helpers.h"
  19. #include "liburing.h"
  20. #include "../src/syscall.h"
  21. #define IO_NSEC_PER_SEC 1000000000ULL
  22. static bool support_abs = false;
  23. static bool support_clock = false;
  24. static unsigned long long timespec_to_ns(struct timespec *ts)
  25. {
  26. return ts->tv_nsec + ts->tv_sec * IO_NSEC_PER_SEC;
  27. }
  28. static struct timespec ns_to_timespec(unsigned long long t)
  29. {
  30. struct timespec ts;
  31. ts.tv_sec = t / IO_NSEC_PER_SEC;
  32. ts.tv_nsec = t - ts.tv_sec * IO_NSEC_PER_SEC;
  33. return ts;
  34. }
  35. static long long ns_since(struct timespec *ts)
  36. {
  37. struct timespec now;
  38. int ret;
  39. ret = clock_gettime(CLOCK_MONOTONIC, &now);
  40. if (ret) {
  41. fprintf(stderr, "clock_gettime failed\n");
  42. exit(T_EXIT_FAIL);
  43. }
  44. return timespec_to_ns(&now) - timespec_to_ns(ts);
  45. }
  46. static int t_io_uring_wait(struct io_uring *ring, int nr, unsigned enter_flags,
  47. struct timespec *ts)
  48. {
  49. struct __kernel_timespec kts = {
  50. .tv_sec = ts->tv_sec,
  51. .tv_nsec = ts->tv_nsec
  52. };
  53. struct io_uring_getevents_arg arg = {
  54. .sigmask = 0,
  55. .sigmask_sz = _NSIG / 8,
  56. .ts = (unsigned long) &kts
  57. };
  58. int ret;
  59. enter_flags |= IORING_ENTER_GETEVENTS | IORING_ENTER_EXT_ARG;
  60. ret = io_uring_enter2(ring->ring_fd, 0, nr, enter_flags,
  61. (void *)&arg, sizeof(arg));
  62. return ret;
  63. }
  64. static int probe_timers(void)
  65. {
  66. struct io_uring_clock_register cr = { .clockid = CLOCK_MONOTONIC, };
  67. struct io_uring ring;
  68. struct timespec ts;
  69. int ret;
  70. ret = io_uring_queue_init(8, &ring, 0);
  71. if (ret) {
  72. fprintf(stderr, "probe ring setup failed: %d\n", ret);
  73. return ret;
  74. }
  75. ret = clock_gettime(CLOCK_MONOTONIC, &ts);
  76. if (ret) {
  77. fprintf(stderr, "clock_gettime failed\n");
  78. return ret;
  79. }
  80. ret = t_io_uring_wait(&ring, 0, IORING_ENTER_ABS_TIMER, &ts);
  81. if (!ret) {
  82. support_abs = true;
  83. } else if (ret != -EINVAL) {
  84. fprintf(stderr, "wait failed %i\n", ret);
  85. return ret;
  86. }
  87. ret = io_uring_register_clock(&ring, &cr);
  88. if (!ret) {
  89. support_clock = true;
  90. } else if (ret != -EINVAL) {
  91. fprintf(stderr, "io_uring_register_clock %i\n", ret);
  92. return ret;
  93. }
  94. io_uring_queue_exit(&ring);
  95. return 0;
  96. }
  97. static int test_timeout(bool abs, bool set_clock)
  98. {
  99. unsigned enter_flags = abs ? IORING_ENTER_ABS_TIMER : 0;
  100. struct io_uring ring;
  101. struct timespec start, end, ts;
  102. long long dt;
  103. int ret;
  104. ret = io_uring_queue_init(8, &ring, 0);
  105. if (ret) {
  106. fprintf(stderr, "ring setup failed: %d\n", ret);
  107. return 1;
  108. }
  109. if (set_clock) {
  110. struct io_uring_clock_register cr = {};
  111. cr.clockid = CLOCK_BOOTTIME;
  112. ret = io_uring_register_clock(&ring, &cr);
  113. if (ret) {
  114. fprintf(stderr, "io_uring_register_clock failed\n");
  115. return 1;
  116. }
  117. }
  118. /* pass current time */
  119. ret = clock_gettime(CLOCK_MONOTONIC, &start);
  120. assert(ret == 0);
  121. ts = abs ? start : ns_to_timespec(0);
  122. ret = t_io_uring_wait(&ring, 1, enter_flags, &ts);
  123. if (ret != -ETIME) {
  124. fprintf(stderr, "wait current time failed, %i\n", ret);
  125. return 1;
  126. }
  127. if (ns_since(&start) >= IO_NSEC_PER_SEC) {
  128. fprintf(stderr, "current time test failed\n");
  129. return 1;
  130. }
  131. if (abs) {
  132. /* expired time */
  133. ret = clock_gettime(CLOCK_MONOTONIC, &start);
  134. assert(ret == 0);
  135. ts = ns_to_timespec(timespec_to_ns(&start) - IO_NSEC_PER_SEC);
  136. ret = t_io_uring_wait(&ring, 1, enter_flags, &ts);
  137. if (ret != -ETIME) {
  138. fprintf(stderr, "expired timeout wait failed, %i\n", ret);
  139. return 1;
  140. }
  141. ret = clock_gettime(CLOCK_MONOTONIC, &end);
  142. assert(ret == 0);
  143. if (ns_since(&start) >= IO_NSEC_PER_SEC) {
  144. fprintf(stderr, "expired timer test failed\n");
  145. return 1;
  146. }
  147. }
  148. /* 1s wait */
  149. ret = clock_gettime(CLOCK_MONOTONIC, &start);
  150. assert(ret == 0);
  151. dt = 2 * IO_NSEC_PER_SEC + (abs ? timespec_to_ns(&start) : 0);
  152. ts = ns_to_timespec(dt);
  153. ret = t_io_uring_wait(&ring, 1, enter_flags, &ts);
  154. if (ret != -ETIME) {
  155. fprintf(stderr, "wait timeout failed, %i\n", ret);
  156. return 1;
  157. }
  158. dt = ns_since(&start);
  159. if (dt < IO_NSEC_PER_SEC || dt > 3 * IO_NSEC_PER_SEC) {
  160. fprintf(stderr, "early wake up, %lld\n", dt);
  161. return 1;
  162. }
  163. return 0;
  164. }
  165. static int test_clock_setup(void)
  166. {
  167. struct io_uring ring;
  168. struct io_uring_clock_register cr = {};
  169. int ret;
  170. ret = io_uring_queue_init(8, &ring, 0);
  171. if (ret) {
  172. fprintf(stderr, "ring setup failed: %d\n", ret);
  173. return T_EXIT_FAIL;
  174. }
  175. ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, NULL, 0);
  176. if (!ret) {
  177. fprintf(stderr, "invalid null clock registration %i\n", ret);
  178. return T_EXIT_FAIL;
  179. }
  180. cr.clockid = -1;
  181. ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, &cr, 0);
  182. if (ret != -EINVAL) {
  183. fprintf(stderr, "invalid clockid registration %i\n", ret);
  184. return T_EXIT_FAIL;
  185. }
  186. cr.clockid = CLOCK_MONOTONIC;
  187. ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, &cr, 0);
  188. if (ret) {
  189. fprintf(stderr, "clock monotonic registration failed %i\n", ret);
  190. return T_EXIT_FAIL;
  191. }
  192. cr.clockid = CLOCK_BOOTTIME;
  193. ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, &cr, 0);
  194. if (ret) {
  195. fprintf(stderr, "clock boottime registration failed %i\n", ret);
  196. return T_EXIT_FAIL;
  197. }
  198. cr.clockid = CLOCK_MONOTONIC;
  199. ret = __sys_io_uring_register(ring.ring_fd, IORING_REGISTER_CLOCK, &cr, 0);
  200. if (ret) {
  201. fprintf(stderr, "2nd clock monotonic registration failed %i\n", ret);
  202. return T_EXIT_FAIL;
  203. }
  204. io_uring_queue_exit(&ring);
  205. return 0;
  206. }
  207. int main(int argc, char *argv[])
  208. {
  209. int ret, i;
  210. if (argc > 1)
  211. return 0;
  212. ret = probe_timers();
  213. if (ret) {
  214. fprintf(stderr, "probe failed\n");
  215. return T_EXIT_FAIL;
  216. }
  217. if (!support_abs && !support_clock)
  218. return T_EXIT_SKIP;
  219. if (support_clock) {
  220. ret = test_clock_setup();
  221. if (ret) {
  222. fprintf(stderr, "test_clock_setup failed\n");
  223. return T_EXIT_FAIL;
  224. }
  225. }
  226. for (i = 0; i < 4; i++) {
  227. bool abs = i & 1;
  228. bool clock = i & 2;
  229. if (abs && !support_abs)
  230. continue;
  231. if (clock && !support_clock)
  232. continue;
  233. ret = test_timeout(abs, clock);
  234. if (ret) {
  235. fprintf(stderr, "test_timeout failed %i %i\n",
  236. abs, clock);
  237. return ret;
  238. }
  239. }
  240. return 0;
  241. }