fadvise.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: basic fadvise test
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include <sys/types.h>
  13. #include <sys/time.h>
  14. #include "helpers.h"
  15. #include "liburing.h"
  16. #define FILE_SIZE (128 * 1024)
  17. #define LOOPS 100
  18. #define MIN_LOOPS 10
  19. static unsigned long long utime_since(const struct timeval *s,
  20. const struct timeval *e)
  21. {
  22. long long sec, usec;
  23. sec = e->tv_sec - s->tv_sec;
  24. usec = (e->tv_usec - s->tv_usec);
  25. if (sec > 0 && usec < 0) {
  26. sec--;
  27. usec += 1000000;
  28. }
  29. sec *= 1000000;
  30. return sec + usec;
  31. }
  32. static unsigned long long utime_since_now(struct timeval *tv)
  33. {
  34. struct timeval end;
  35. gettimeofday(&end, NULL);
  36. return utime_since(tv, &end);
  37. }
  38. static int do_fadvise(struct io_uring *ring, int fd, off_t offset, off_t len,
  39. int advice)
  40. {
  41. struct io_uring_sqe *sqe;
  42. struct io_uring_cqe *cqe;
  43. int ret;
  44. sqe = io_uring_get_sqe(ring);
  45. if (!sqe) {
  46. fprintf(stderr, "failed to get sqe\n");
  47. return 1;
  48. }
  49. io_uring_prep_fadvise(sqe, fd, offset, len, advice);
  50. sqe->user_data = advice;
  51. ret = io_uring_submit_and_wait(ring, 1);
  52. if (ret != 1) {
  53. fprintf(stderr, "submit: %d\n", ret);
  54. return 1;
  55. }
  56. ret = io_uring_wait_cqe(ring, &cqe);
  57. if (ret) {
  58. fprintf(stderr, "wait: %d\n", ret);
  59. return 1;
  60. }
  61. ret = cqe->res;
  62. if (ret == -EINVAL || ret == -EBADF) {
  63. fprintf(stdout, "Fadvise not supported, skipping\n");
  64. unlink(".fadvise.tmp");
  65. exit(T_EXIT_SKIP);
  66. } else if (ret) {
  67. fprintf(stderr, "cqe->res=%d\n", cqe->res);
  68. }
  69. io_uring_cqe_seen(ring, cqe);
  70. return ret;
  71. }
  72. static long do_read(int fd, char *buf)
  73. {
  74. struct timeval tv;
  75. int ret;
  76. long t;
  77. ret = lseek(fd, 0, SEEK_SET);
  78. if (ret) {
  79. perror("lseek");
  80. return -1;
  81. }
  82. gettimeofday(&tv, NULL);
  83. ret = read(fd, buf, FILE_SIZE);
  84. t = utime_since_now(&tv);
  85. if (ret < 0) {
  86. perror("read");
  87. return -1;
  88. } else if (ret != FILE_SIZE) {
  89. fprintf(stderr, "short read1: %d\n", ret);
  90. return -1;
  91. }
  92. return t;
  93. }
  94. static int test_fadvise(struct io_uring *ring, const char *filename)
  95. {
  96. unsigned long cached_read, uncached_read, cached_read2;
  97. int fd, ret;
  98. char *buf;
  99. fd = open(filename, O_RDONLY);
  100. if (fd < 0) {
  101. perror("open");
  102. return 1;
  103. }
  104. buf = t_malloc(FILE_SIZE);
  105. cached_read = do_read(fd, buf);
  106. if (cached_read == -1)
  107. return 1;
  108. ret = do_fadvise(ring, fd, 0, FILE_SIZE, POSIX_FADV_DONTNEED);
  109. if (ret)
  110. return 1;
  111. uncached_read = do_read(fd, buf);
  112. if (uncached_read == -1)
  113. return 1;
  114. ret = do_fadvise(ring, fd, 0, FILE_SIZE, POSIX_FADV_DONTNEED);
  115. if (ret)
  116. return 1;
  117. ret = do_fadvise(ring, fd, 0, FILE_SIZE, POSIX_FADV_WILLNEED);
  118. if (ret)
  119. return 1;
  120. fsync(fd);
  121. cached_read2 = do_read(fd, buf);
  122. if (cached_read2 == -1)
  123. return 1;
  124. if (cached_read < uncached_read &&
  125. cached_read2 < uncached_read)
  126. return 0;
  127. return 2;
  128. }
  129. int main(int argc, char *argv[])
  130. {
  131. struct io_uring ring;
  132. int ret, i, good, bad;
  133. char *fname;
  134. if (argc > 1) {
  135. fname = argv[1];
  136. } else {
  137. fname = ".fadvise.tmp";
  138. t_create_file(fname, FILE_SIZE);
  139. }
  140. if (io_uring_queue_init(8, &ring, 0)) {
  141. fprintf(stderr, "ring creation failed\n");
  142. goto err;
  143. }
  144. good = bad = 0;
  145. for (i = 0; i < LOOPS; i++) {
  146. ret = test_fadvise(&ring, fname);
  147. if (ret == 1) {
  148. fprintf(stderr, "read_fadvise failed\n");
  149. goto err;
  150. } else if (!ret)
  151. good++;
  152. else if (ret == 2)
  153. bad++;
  154. if (i >= MIN_LOOPS && !bad)
  155. break;
  156. }
  157. /* too hard to reliably test, just ignore */
  158. if ((0) && bad > good) {
  159. fprintf(stderr, "Suspicious timings\n");
  160. goto err;
  161. }
  162. if (fname != argv[1])
  163. unlink(fname);
  164. io_uring_queue_exit(&ring);
  165. return T_EXIT_PASS;
  166. err:
  167. if (fname != argv[1])
  168. unlink(fname);
  169. return T_EXIT_FAIL;
  170. }