7ad0e4b2f83c.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include "liburing.h"
  7. #include "helpers.h"
  8. static unsigned long long mtime_since(const struct timeval *s,
  9. const struct timeval *e)
  10. {
  11. long long sec, usec;
  12. sec = e->tv_sec - s->tv_sec;
  13. usec = (e->tv_usec - s->tv_usec);
  14. if (sec > 0 && usec < 0) {
  15. sec--;
  16. usec += 1000000;
  17. }
  18. sec *= 1000;
  19. usec /= 1000;
  20. return sec + usec;
  21. }
  22. static unsigned long long mtime_since_now(struct timeval *tv)
  23. {
  24. struct timeval end;
  25. gettimeofday(&end, NULL);
  26. return mtime_since(tv, &end);
  27. }
  28. int main(int argc, char *argv[])
  29. {
  30. struct __kernel_timespec ts1, ts2;
  31. struct io_uring_cqe *cqe;
  32. struct io_uring_sqe *sqe;
  33. struct io_uring ring;
  34. unsigned long msec;
  35. struct timeval tv;
  36. int ret;
  37. if (argc > 1)
  38. return T_EXIT_SKIP;
  39. ret = io_uring_queue_init(32, &ring, 0);
  40. if (ret) {
  41. fprintf(stderr, "io_uring_queue_init=%d\n", ret);
  42. return T_EXIT_FAIL;
  43. }
  44. sqe = io_uring_get_sqe(&ring);
  45. io_uring_prep_nop(sqe);
  46. ret = io_uring_submit(&ring);
  47. if (ret != 1) {
  48. fprintf(stderr, "io_uring_submit1=%d\n", ret);
  49. return T_EXIT_FAIL;
  50. }
  51. ts1.tv_sec = 5,
  52. ts1.tv_nsec = 0;
  53. ret = io_uring_wait_cqe_timeout(&ring, &cqe, &ts1);
  54. if (ret) {
  55. fprintf(stderr, "io_uring_wait_cqe_timeout=%d\n", ret);
  56. return T_EXIT_FAIL;
  57. }
  58. io_uring_cqe_seen(&ring, cqe);
  59. gettimeofday(&tv, NULL);
  60. ts2.tv_sec = 1;
  61. ts2.tv_nsec = 0;
  62. sqe = io_uring_get_sqe(&ring);
  63. io_uring_prep_timeout(sqe, &ts2, 0, 0);
  64. sqe->user_data = 89;
  65. ret = io_uring_submit(&ring);
  66. if (ret != 1) {
  67. fprintf(stderr, "io_uring_submit2=%d\n", ret);
  68. return T_EXIT_FAIL;
  69. }
  70. io_uring_wait_cqe(&ring, &cqe);
  71. io_uring_cqe_seen(&ring, cqe);
  72. msec = mtime_since_now(&tv);
  73. if (msec >= 900 && msec <= 1100) {
  74. io_uring_queue_exit(&ring);
  75. return T_EXIT_PASS;
  76. }
  77. fprintf(stderr, "%s: Timeout seems wonky (got %lu)\n", __FUNCTION__,
  78. msec);
  79. io_uring_queue_exit(&ring);
  80. return T_EXIT_FAIL;
  81. }