fadvise.c 3.4 KB

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