sqwait.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test that the app can always get a new sqe after having
  5. * called io_uring_sqring_wait().
  6. *
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include "liburing.h"
  12. #include "helpers.h"
  13. #define NR_IOS 10000
  14. #define INFLIGHT 256
  15. #define FILE_SIZE (256 * 1024 * 1024)
  16. static int inflight;
  17. static int reap(struct io_uring *ring)
  18. {
  19. struct io_uring_cqe *cqe;
  20. int ret;
  21. while (inflight >= INFLIGHT / 2) {
  22. ret = io_uring_wait_cqe(ring, &cqe);
  23. if (ret < 0) {
  24. fprintf(stderr, "wait=%d\n", ret);
  25. return 1;
  26. }
  27. if (cqe->res < 0) {
  28. printf("cqe res %d\n", cqe->res);
  29. return 1;
  30. }
  31. io_uring_cqe_seen(ring, cqe);
  32. inflight--;
  33. }
  34. return 0;
  35. }
  36. int main(int argc, char *argv[])
  37. {
  38. struct io_uring_sqe *sqe;
  39. struct io_uring ring;
  40. int fd = -1, i, iov_off, ret, fret;
  41. struct iovec iovs[INFLIGHT];
  42. const char *fname;
  43. char buf[256];
  44. loff_t off;
  45. if (argc > 1) {
  46. fname = argv[1];
  47. } else {
  48. srand((unsigned)time(NULL));
  49. snprintf(buf, sizeof(buf), ".sqwait-%u-%u", (unsigned)rand(),
  50. (unsigned)getpid());
  51. fname = buf;
  52. t_create_file(fname, FILE_SIZE);
  53. }
  54. fret = T_EXIT_SKIP;
  55. ret = io_uring_queue_init(8, &ring, IORING_SETUP_SQPOLL);
  56. if (ret < 0) {
  57. if (errno == EINVAL || errno == EPERM)
  58. goto err;
  59. fprintf(stderr, "queue init %d\n", ret);
  60. fret = T_EXIT_FAIL;
  61. goto err;
  62. }
  63. fd = open(fname, O_RDONLY | O_DIRECT);
  64. if (fd < 0) {
  65. if (errno == EACCES || errno == EPERM || errno == EINVAL)
  66. return T_EXIT_SKIP;
  67. perror("open");
  68. fret = T_EXIT_FAIL;
  69. goto err;
  70. }
  71. for (i = 0; i < INFLIGHT; i++) {
  72. if (posix_memalign(&iovs[i].iov_base, 4096, 4096))
  73. goto err;
  74. iovs[i].iov_len = 4096;
  75. }
  76. iov_off = off = 0;
  77. for (i = 0; i < NR_IOS; i++) {
  78. struct iovec *iov = &iovs[iov_off];
  79. sqe = io_uring_get_sqe(&ring);
  80. if (!sqe) {
  81. ret = io_uring_sqring_wait(&ring);
  82. if (ret < 0) {
  83. if (ret == -EINVAL)
  84. return T_EXIT_SKIP;
  85. fprintf(stderr, "sqwait=%d\n", ret);
  86. fret = T_EXIT_FAIL;
  87. goto err;
  88. }
  89. sqe = io_uring_get_sqe(&ring);
  90. if (!sqe) {
  91. fprintf(stderr, "No sqe post wait\n");
  92. fret = T_EXIT_FAIL;
  93. goto err;
  94. }
  95. }
  96. io_uring_prep_read(sqe, fd, iov->iov_base, iov->iov_len, 0);
  97. io_uring_submit(&ring);
  98. inflight++;
  99. iov_off++;
  100. if (iov_off == INFLIGHT)
  101. iov_off = 0;
  102. off += 8192;
  103. if (off > FILE_SIZE - 8192)
  104. off = 0;
  105. if (reap(&ring)) {
  106. fret = T_EXIT_FAIL;
  107. goto err;
  108. }
  109. }
  110. if (fd != -1)
  111. close(fd);
  112. if (fname != argv[1])
  113. unlink(fname);
  114. io_uring_queue_exit(&ring);
  115. return T_EXIT_PASS;
  116. err:
  117. if (fd != -1)
  118. close(fd);
  119. if (fname != argv[1])
  120. unlink(fname);
  121. return fret;
  122. }