cq-peek-batch.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test CQ peek-batch
  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. #include "helpers.h"
  15. static int queue_n_nops(struct io_uring *ring, int n, int offset)
  16. {
  17. struct io_uring_sqe *sqe;
  18. int i, ret;
  19. for (i = 0; i < n; i++) {
  20. sqe = io_uring_get_sqe(ring);
  21. if (!sqe) {
  22. printf("get sqe failed\n");
  23. goto err;
  24. }
  25. io_uring_prep_nop(sqe);
  26. sqe->user_data = i + offset;
  27. }
  28. ret = io_uring_submit(ring);
  29. if (ret < n) {
  30. printf("Submitted only %d\n", ret);
  31. goto err;
  32. } else if (ret < 0) {
  33. printf("sqe submit failed: %d\n", ret);
  34. goto err;
  35. }
  36. return 0;
  37. err:
  38. return 1;
  39. }
  40. #define CHECK_BATCH(ring, got, cqes, count, expected) do {\
  41. got = io_uring_peek_batch_cqe((ring), cqes, count);\
  42. if (got != expected) {\
  43. printf("Got %d CQs, expected %d\n", got, expected);\
  44. goto err;\
  45. }\
  46. } while(0)
  47. int main(int argc, char *argv[])
  48. {
  49. struct io_uring_cqe *cqes[8];
  50. struct io_uring ring;
  51. int ret, i;
  52. unsigned got;
  53. if (argc > 1)
  54. return T_EXIT_SKIP;
  55. ret = io_uring_queue_init(4, &ring, 0);
  56. if (ret) {
  57. printf("ring setup failed\n");
  58. return T_EXIT_FAIL;
  59. }
  60. CHECK_BATCH(&ring, got, cqes, 4, 0);
  61. if (queue_n_nops(&ring, 4, 0))
  62. goto err;
  63. CHECK_BATCH(&ring, got, cqes, 4, 4);
  64. for (i=0;i<4;i++) {
  65. if (i != cqes[i]->user_data) {
  66. printf("Got user_data %" PRIu64 ", expected %d\n",
  67. (uint64_t) cqes[i]->user_data, i);
  68. goto err;
  69. }
  70. }
  71. if (queue_n_nops(&ring, 4, 4))
  72. goto err;
  73. io_uring_cq_advance(&ring, 4);
  74. CHECK_BATCH(&ring, got, cqes, 4, 4);
  75. for (i=0;i<4;i++) {
  76. if (i + 4 != cqes[i]->user_data) {
  77. printf("Got user_data %" PRIu64 ", expected %d\n",
  78. (uint64_t) cqes[i]->user_data, i + 4);
  79. goto err;
  80. }
  81. }
  82. io_uring_cq_advance(&ring, 8);
  83. io_uring_queue_exit(&ring);
  84. return T_EXIT_PASS;
  85. err:
  86. io_uring_queue_exit(&ring);
  87. return T_EXIT_FAIL;
  88. }