submit-and-wait.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: Test that io_uring_submit_and_wait_timeout() returns the
  5. * right value (submit count) and that it doesn't end up waiting twice.
  6. *
  7. */
  8. #include <errno.h>
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <fcntl.h>
  14. #include <sys/time.h>
  15. #include "liburing.h"
  16. #include "test.h"
  17. static unsigned long long mtime_since(const struct timeval *s,
  18. const struct timeval *e)
  19. {
  20. long long sec, usec;
  21. sec = e->tv_sec - s->tv_sec;
  22. usec = (e->tv_usec - s->tv_usec);
  23. if (sec > 0 && usec < 0) {
  24. sec--;
  25. usec += 1000000;
  26. }
  27. sec *= 1000;
  28. usec /= 1000;
  29. return sec + usec;
  30. }
  31. static unsigned long long mtime_since_now(struct timeval *tv)
  32. {
  33. struct timeval end;
  34. gettimeofday(&end, NULL);
  35. return mtime_since(tv, &end);
  36. }
  37. static int test(struct io_uring *ring)
  38. {
  39. struct io_uring_cqe *cqe;
  40. struct io_uring_sqe *sqe;
  41. struct __kernel_timespec ts;
  42. struct timeval tv;
  43. int ret, i;
  44. for (i = 0; i < 1; i++) {
  45. sqe = io_uring_get_sqe(ring);
  46. if (!sqe) {
  47. fprintf(stderr, "get sqe failed at %d\n", i);
  48. goto err;
  49. }
  50. io_uring_prep_nop(sqe);
  51. }
  52. ts.tv_sec = 1;
  53. ts.tv_nsec = 0;
  54. gettimeofday(&tv, NULL);
  55. ret = io_uring_submit_and_wait_timeout(ring, &cqe, 2, &ts, NULL);
  56. if (ret < 0) {
  57. fprintf(stderr, "submit_and_wait_timeout: %d\n", ret);
  58. goto err;
  59. }
  60. ret = mtime_since_now(&tv);
  61. /* allow some slack, should be around 1s */
  62. if (ret > 1200) {
  63. fprintf(stderr, "wait took too long: %d\n", ret);
  64. goto err;
  65. }
  66. return 0;
  67. err:
  68. return 1;
  69. }
  70. static int test_ring(void)
  71. {
  72. struct io_uring ring;
  73. struct io_uring_params p = { };
  74. int ret;
  75. p.flags = 0;
  76. ret = io_uring_queue_init_params(8, &ring, &p);
  77. if (ret) {
  78. fprintf(stderr, "ring setup failed: %d\n", ret);
  79. return 1;
  80. }
  81. ret = test(&ring);
  82. if (ret) {
  83. fprintf(stderr, "test failed\n");
  84. goto err;
  85. }
  86. err:
  87. io_uring_queue_exit(&ring);
  88. return ret;
  89. }
  90. int main(int argc, char *argv[])
  91. {
  92. if (argc > 1)
  93. return 0;
  94. return test_ring();
  95. }