timeout-new.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: tests for getevents timeout
  5. *
  6. */
  7. #include <stdio.h>
  8. #include <sys/time.h>
  9. #include <unistd.h>
  10. #include <pthread.h>
  11. #include "liburing.h"
  12. #define TIMEOUT_MSEC 200
  13. #define TIMEOUT_SEC 10
  14. static int thread_ret0, thread_ret1;
  15. static int cnt = 0;
  16. static pthread_mutex_t mutex;
  17. static void msec_to_ts(struct __kernel_timespec *ts, unsigned int msec)
  18. {
  19. ts->tv_sec = msec / 1000;
  20. ts->tv_nsec = (msec % 1000) * 1000000;
  21. }
  22. static unsigned long long mtime_since(const struct timeval *s,
  23. const struct timeval *e)
  24. {
  25. long long sec, usec;
  26. sec = e->tv_sec - s->tv_sec;
  27. usec = (e->tv_usec - s->tv_usec);
  28. if (sec > 0 && usec < 0) {
  29. sec--;
  30. usec += 1000000;
  31. }
  32. sec *= 1000;
  33. usec /= 1000;
  34. return sec + usec;
  35. }
  36. static unsigned long long mtime_since_now(struct timeval *tv)
  37. {
  38. struct timeval end;
  39. gettimeofday(&end, NULL);
  40. return mtime_since(tv, &end);
  41. }
  42. static int test_return_before_timeout(struct io_uring *ring)
  43. {
  44. struct io_uring_cqe *cqe;
  45. struct io_uring_sqe *sqe;
  46. int ret;
  47. bool retried = false;
  48. struct __kernel_timespec ts;
  49. msec_to_ts(&ts, TIMEOUT_MSEC);
  50. sqe = io_uring_get_sqe(ring);
  51. io_uring_prep_nop(sqe);
  52. ret = io_uring_submit(ring);
  53. if (ret <= 0) {
  54. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  55. return 1;
  56. }
  57. again:
  58. ret = io_uring_wait_cqe_timeout(ring, &cqe, &ts);
  59. if (ret == -ETIME && (ring->flags & IORING_SETUP_SQPOLL) && !retried) {
  60. /*
  61. * there is a small chance SQPOLL hasn't been waked up yet,
  62. * give it one more try.
  63. */
  64. printf("warning: funky SQPOLL timing\n");
  65. sleep(1);
  66. retried = true;
  67. goto again;
  68. } else if (ret < 0) {
  69. fprintf(stderr, "%s: timeout error: %d\n", __FUNCTION__, ret);
  70. return 1;
  71. }
  72. io_uring_cqe_seen(ring, cqe);
  73. return 0;
  74. }
  75. static int test_return_after_timeout(struct io_uring *ring)
  76. {
  77. struct io_uring_cqe *cqe;
  78. int ret;
  79. struct __kernel_timespec ts;
  80. struct timeval tv;
  81. unsigned long long exp;
  82. msec_to_ts(&ts, TIMEOUT_MSEC);
  83. gettimeofday(&tv, NULL);
  84. ret = io_uring_wait_cqe_timeout(ring, &cqe, &ts);
  85. exp = mtime_since_now(&tv);
  86. if (ret != -ETIME) {
  87. fprintf(stderr, "%s: timeout error: %d\n", __FUNCTION__, ret);
  88. return 1;
  89. }
  90. if (exp < TIMEOUT_MSEC / 2 || exp > (TIMEOUT_MSEC * 3) / 2) {
  91. fprintf(stderr, "%s: Timeout seems wonky (got %llu)\n", __FUNCTION__, exp);
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. static int __reap_thread_fn(void *data)
  97. {
  98. struct io_uring *ring = (struct io_uring *)data;
  99. struct io_uring_cqe *cqe;
  100. struct __kernel_timespec ts;
  101. msec_to_ts(&ts, TIMEOUT_SEC);
  102. pthread_mutex_lock(&mutex);
  103. cnt++;
  104. pthread_mutex_unlock(&mutex);
  105. return io_uring_wait_cqe_timeout(ring, &cqe, &ts);
  106. }
  107. static void *reap_thread_fn0(void *data)
  108. {
  109. thread_ret0 = __reap_thread_fn(data);
  110. return NULL;
  111. }
  112. static void *reap_thread_fn1(void *data)
  113. {
  114. thread_ret1 = __reap_thread_fn(data);
  115. return NULL;
  116. }
  117. /*
  118. * This is to test issuing a sqe in main thread and reaping it in two child-thread
  119. * at the same time. To see if timeout feature works or not.
  120. */
  121. static int test_multi_threads_timeout(void)
  122. {
  123. struct io_uring ring;
  124. int ret;
  125. bool both_wait = false;
  126. pthread_t reap_thread0, reap_thread1;
  127. struct io_uring_sqe *sqe;
  128. ret = io_uring_queue_init(8, &ring, 0);
  129. if (ret) {
  130. fprintf(stderr, "%s: ring setup failed: %d\n", __FUNCTION__, ret);
  131. return 1;
  132. }
  133. pthread_create(&reap_thread0, NULL, reap_thread_fn0, &ring);
  134. pthread_create(&reap_thread1, NULL, reap_thread_fn1, &ring);
  135. /*
  136. * make two threads both enter io_uring_wait_cqe_timeout() before issuing the sqe
  137. * as possible as we can. So that there are two threads in the ctx->wait queue.
  138. * In this way, we can test if a cqe wakes up two threads at the same time.
  139. */
  140. while(!both_wait) {
  141. pthread_mutex_lock(&mutex);
  142. if (cnt == 2)
  143. both_wait = true;
  144. pthread_mutex_unlock(&mutex);
  145. sleep(1);
  146. }
  147. sqe = io_uring_get_sqe(&ring);
  148. if (!sqe) {
  149. fprintf(stderr, "%s: get sqe failed\n", __FUNCTION__);
  150. goto err;
  151. }
  152. io_uring_prep_nop(sqe);
  153. ret = io_uring_submit(&ring);
  154. if (ret <= 0) {
  155. fprintf(stderr, "%s: sqe submit failed: %d\n", __FUNCTION__, ret);
  156. goto err;
  157. }
  158. pthread_join(reap_thread0, NULL);
  159. pthread_join(reap_thread1, NULL);
  160. if ((thread_ret0 && thread_ret0 != -ETIME) || (thread_ret1 && thread_ret1 != -ETIME)) {
  161. fprintf(stderr, "%s: thread wait cqe timeout failed: %d %d\n",
  162. __FUNCTION__, thread_ret0, thread_ret1);
  163. goto err;
  164. }
  165. return 0;
  166. err:
  167. return 1;
  168. }
  169. int main(int argc, char *argv[])
  170. {
  171. struct io_uring ring_normal, ring_sq;
  172. int ret;
  173. if (argc > 1)
  174. return 0;
  175. ret = io_uring_queue_init(8, &ring_normal, 0);
  176. if (ret) {
  177. fprintf(stderr, "ring_normal setup failed: %d\n", ret);
  178. return 1;
  179. }
  180. if (!(ring_normal.features & IORING_FEAT_EXT_ARG)) {
  181. fprintf(stderr, "feature IORING_FEAT_EXT_ARG not supported, skipping.\n");
  182. return 0;
  183. }
  184. ret = test_return_before_timeout(&ring_normal);
  185. if (ret) {
  186. fprintf(stderr, "ring_normal: test_return_before_timeout failed\n");
  187. return ret;
  188. }
  189. ret = test_return_after_timeout(&ring_normal);
  190. if (ret) {
  191. fprintf(stderr, "ring_normal: test_return_after_timeout failed\n");
  192. return ret;
  193. }
  194. ret = io_uring_queue_init(8, &ring_sq, IORING_SETUP_SQPOLL);
  195. if (ret) {
  196. fprintf(stderr, "ring_sq setup failed: %d\n", ret);
  197. return 1;
  198. }
  199. ret = test_return_before_timeout(&ring_sq);
  200. if (ret) {
  201. fprintf(stderr, "ring_sq: test_return_before_timeout failed\n");
  202. return ret;
  203. }
  204. ret = test_return_after_timeout(&ring_sq);
  205. if (ret) {
  206. fprintf(stderr, "ring_sq: test_return_after_timeout failed\n");
  207. return ret;
  208. }
  209. ret = test_multi_threads_timeout();
  210. if (ret) {
  211. fprintf(stderr, "test_multi_threads_timeout failed\n");
  212. return ret;
  213. }
  214. return 0;
  215. }