sq-space_left.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test SQ queue space left
  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 "liburing.h"
  14. static int test_left(void)
  15. {
  16. struct io_uring_sqe *sqe;
  17. struct io_uring ring;
  18. int ret, i = 0, s;
  19. ret = io_uring_queue_init(8, &ring, 0);
  20. if (ret) {
  21. fprintf(stderr, "ring setup failed: %d\n", ret);
  22. return 1;
  23. }
  24. if ((s = io_uring_sq_space_left(&ring)) != 8) {
  25. fprintf(stderr, "Got %d SQEs left, expected %d\n", s, 8);
  26. goto err;
  27. }
  28. i = 0;
  29. while ((sqe = io_uring_get_sqe(&ring)) != NULL) {
  30. i++;
  31. if ((s = io_uring_sq_space_left(&ring)) != 8 - i) {
  32. fprintf(stderr, "Got %d SQEs left, expected %d\n", s, 8 - i);
  33. goto err;
  34. }
  35. }
  36. if (i != 8) {
  37. fprintf(stderr, "Got %d SQEs, expected %d\n", i, 8);
  38. goto err;
  39. }
  40. io_uring_queue_exit(&ring);
  41. return 0;
  42. err:
  43. io_uring_queue_exit(&ring);
  44. return 1;
  45. }
  46. static int test_sync(void)
  47. {
  48. struct io_uring_sqe *sqe;
  49. struct io_uring ring;
  50. int ret, i;
  51. ret = io_uring_queue_init(32, &ring, 0);
  52. if (ret) {
  53. fprintf(stderr, "ring setup failed: %d\n", ret);
  54. return 1;
  55. }
  56. /* prep 8 NOPS */
  57. for (i = 0; i < 8; i++) {
  58. sqe = io_uring_get_sqe(&ring);
  59. if (!sqe) {
  60. fprintf(stderr, "get sqe failed\n");
  61. goto err;
  62. }
  63. io_uring_prep_nop(sqe);
  64. }
  65. /* prep known bad command, this should terminate submission */
  66. sqe = io_uring_get_sqe(&ring);
  67. if (!sqe) {
  68. fprintf(stderr, "get sqe failed\n");
  69. goto err;
  70. }
  71. io_uring_prep_nop(sqe);
  72. sqe->opcode = 0xfe;
  73. /* prep 8 NOPS */
  74. for (i = 0; i < 8; i++) {
  75. sqe = io_uring_get_sqe(&ring);
  76. if (!sqe) {
  77. fprintf(stderr, "get sqe failed\n");
  78. goto err;
  79. }
  80. io_uring_prep_nop(sqe);
  81. }
  82. /* we should have 8 + 1 + 8 pending now */
  83. ret = io_uring_sq_ready(&ring);
  84. if (ret != 17) {
  85. fprintf(stderr, "%d ready, wanted 17\n", ret);
  86. goto err;
  87. }
  88. ret = io_uring_submit(&ring);
  89. /* should submit 8 successfully, then error #9 and stop */
  90. if (ret != 9) {
  91. fprintf(stderr, "submitted %d, wanted 9\n", ret);
  92. goto err;
  93. }
  94. /* should now have 8 ready, with 9 gone */
  95. ret = io_uring_sq_ready(&ring);
  96. if (ret != 8) {
  97. fprintf(stderr, "%d ready, wanted 8\n", ret);
  98. goto err;
  99. }
  100. ret = io_uring_submit(&ring);
  101. /* the last 8 should submit fine */
  102. if (ret != 8) {
  103. fprintf(stderr, "submitted %d, wanted 8\n", ret);
  104. goto err;
  105. }
  106. ret = io_uring_sq_ready(&ring);
  107. if (ret) {
  108. fprintf(stderr, "%d ready, wanted 0\n", ret);
  109. goto err;
  110. }
  111. io_uring_queue_exit(&ring);
  112. return 0;
  113. err:
  114. io_uring_queue_exit(&ring);
  115. return 1;
  116. }
  117. int main(int argc, char *argv[])
  118. {
  119. int ret;
  120. if (argc > 1)
  121. return 0;
  122. ret = test_left();
  123. if (ret) {
  124. fprintf(stderr, "test_left failed\n");
  125. return ret;
  126. }
  127. ret = test_sync();
  128. if (ret) {
  129. fprintf(stderr, "test_sync failed\n");
  130. return ret;
  131. }
  132. return 0;
  133. }