sqpoll-sleep.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Test that the sqthread goes to sleep around the specified time, and that
  5. * the NEED_WAKEUP flag is then set.
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <sys/time.h>
  12. #include "liburing.h"
  13. #include "helpers.h"
  14. int main(int argc, char *argv[])
  15. {
  16. struct io_uring_params p = {};
  17. struct io_uring_sqe *sqe;
  18. struct io_uring_cqe *cqe;
  19. struct timeval tv;
  20. struct io_uring ring;
  21. unsigned long elapsed;
  22. bool seen_wakeup;
  23. int ret;
  24. if (argc > 1)
  25. return T_EXIT_SKIP;
  26. p.flags = IORING_SETUP_SQPOLL;
  27. p.sq_thread_idle = 100;
  28. ret = io_uring_queue_init_params(1, &ring, &p);
  29. if (ret) {
  30. if (geteuid()) {
  31. printf("%s: skipped, not root\n", argv[0]);
  32. return T_EXIT_SKIP;
  33. }
  34. fprintf(stderr, "queue_init=%d\n", ret);
  35. return T_EXIT_FAIL;
  36. }
  37. sqe = io_uring_get_sqe(&ring);
  38. io_uring_prep_nop(sqe);
  39. io_uring_submit(&ring);
  40. ret = io_uring_wait_cqe(&ring, &cqe);
  41. if (ret) {
  42. fprintf(stderr, "wait_cqe: %d\n", ret);
  43. return T_EXIT_FAIL;
  44. }
  45. io_uring_cqe_seen(&ring, cqe);
  46. elapsed = 0;
  47. seen_wakeup = false;
  48. gettimeofday(&tv, NULL);
  49. do {
  50. usleep(100);
  51. if (IO_URING_READ_ONCE(*ring.sq.kflags) & IORING_SQ_NEED_WAKEUP) {
  52. seen_wakeup = true;
  53. break;
  54. }
  55. elapsed = mtime_since_now(&tv);
  56. } while (elapsed < 1000);
  57. if (!seen_wakeup) {
  58. fprintf(stderr, "SQPOLL didn't flag wakeup\n");
  59. return T_EXIT_FAIL;
  60. }
  61. /* should be around 100 msec */
  62. if (elapsed < 90 || elapsed > 110) {
  63. fprintf(stderr, "SQPOLL wakeup timing off %lu\n", elapsed);
  64. return T_EXIT_FAIL;
  65. }
  66. return T_EXIT_PASS;
  67. }