sqpoll-sleep.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. static unsigned long long mtime_since(const struct timeval *s,
  14. const struct timeval *e)
  15. {
  16. long long sec, usec;
  17. sec = e->tv_sec - s->tv_sec;
  18. usec = (e->tv_usec - s->tv_usec);
  19. if (sec > 0 && usec < 0) {
  20. sec--;
  21. usec += 1000000;
  22. }
  23. sec *= 1000;
  24. usec /= 1000;
  25. return sec + usec;
  26. }
  27. static unsigned long long mtime_since_now(struct timeval *tv)
  28. {
  29. struct timeval end;
  30. gettimeofday(&end, NULL);
  31. return mtime_since(tv, &end);
  32. }
  33. int main(int argc, char *argv[])
  34. {
  35. struct io_uring_params p = {};
  36. struct timeval tv;
  37. struct io_uring ring;
  38. int ret;
  39. if (argc > 1)
  40. return 0;
  41. p.flags = IORING_SETUP_SQPOLL;
  42. p.sq_thread_idle = 100;
  43. ret = io_uring_queue_init_params(1, &ring, &p);
  44. if (ret) {
  45. if (geteuid()) {
  46. printf("%s: skipped, not root\n", argv[0]);
  47. return 0;
  48. }
  49. fprintf(stderr, "queue_init=%d\n", ret);
  50. return 1;
  51. }
  52. gettimeofday(&tv, NULL);
  53. do {
  54. usleep(1000);
  55. if ((*ring.sq.kflags) & IORING_SQ_NEED_WAKEUP)
  56. return 0;
  57. } while (mtime_since_now(&tv) < 1000);
  58. return 1;
  59. }