madvise.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: basic madvise 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 <sys/mman.h>
  15. #include "helpers.h"
  16. #include "liburing.h"
  17. #define FILE_SIZE (128 * 1024)
  18. #define LOOPS 100
  19. #define MIN_LOOPS 10
  20. static int do_madvise(struct io_uring *ring, void *addr, off_t len, 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_madvise(sqe, addr, 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, "Madvise not supported, skipping\n");
  45. unlink(".madvise.tmp");
  46. exit(0);
  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_copy(int fd, char *buf, void *ptr)
  54. {
  55. struct timeval tv;
  56. gettimeofday(&tv, NULL);
  57. memcpy(buf, ptr, FILE_SIZE);
  58. return utime_since_now(&tv);
  59. }
  60. static int test_madvise(struct io_uring *ring, const char *filename)
  61. {
  62. unsigned long cached_read, uncached_read, cached_read2;
  63. int fd, ret;
  64. char *buf;
  65. void *ptr;
  66. fd = open(filename, O_RDONLY);
  67. if (fd < 0) {
  68. if (errno == EACCES || errno == EPERM)
  69. return T_EXIT_SKIP;
  70. perror("open");
  71. return 1;
  72. }
  73. buf = t_malloc(FILE_SIZE);
  74. ptr = mmap(NULL, FILE_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
  75. if (ptr == MAP_FAILED) {
  76. perror("mmap");
  77. return 1;
  78. }
  79. cached_read = do_copy(fd, buf, ptr);
  80. if (cached_read == -1)
  81. return 1;
  82. cached_read = do_copy(fd, buf, ptr);
  83. if (cached_read == -1)
  84. return 1;
  85. ret = do_madvise(ring, ptr, FILE_SIZE, MADV_DONTNEED);
  86. if (ret)
  87. return 1;
  88. uncached_read = do_copy(fd, buf, ptr);
  89. if (uncached_read == -1)
  90. return 1;
  91. ret = do_madvise(ring, ptr, FILE_SIZE, MADV_DONTNEED);
  92. if (ret)
  93. return 1;
  94. ret = do_madvise(ring, ptr, FILE_SIZE, MADV_WILLNEED);
  95. if (ret)
  96. return 1;
  97. msync(ptr, FILE_SIZE, MS_SYNC);
  98. cached_read2 = do_copy(fd, buf, ptr);
  99. if (cached_read2 == -1)
  100. return 1;
  101. if (cached_read < uncached_read &&
  102. cached_read2 < uncached_read) {
  103. free(buf);
  104. return 0;
  105. }
  106. free(buf);
  107. return 2;
  108. }
  109. int main(int argc, char *argv[])
  110. {
  111. struct io_uring ring;
  112. int ret, i, good, bad;
  113. char *fname;
  114. if (argc > 1) {
  115. fname = argv[1];
  116. } else {
  117. fname = ".madvise.tmp";
  118. t_create_file(fname, FILE_SIZE);
  119. }
  120. if (io_uring_queue_init(8, &ring, 0)) {
  121. fprintf(stderr, "ring creation failed\n");
  122. goto err;
  123. }
  124. good = bad = 0;
  125. for (i = 0; i < LOOPS; i++) {
  126. ret = test_madvise(&ring, fname);
  127. if (ret == T_EXIT_SKIP)
  128. goto skip;
  129. if (ret == 1) {
  130. fprintf(stderr, "test_madvise failed\n");
  131. goto err;
  132. } else if (!ret)
  133. good++;
  134. else if (ret == 2)
  135. bad++;
  136. if (i >= MIN_LOOPS && !bad)
  137. break;
  138. }
  139. /* too hard to reliably test, just ignore */
  140. if ((0) && bad > good)
  141. fprintf(stderr, "Suspicious timings (%u > %u)\n", bad, good);
  142. if (fname != argv[1])
  143. unlink(fname);
  144. io_uring_queue_exit(&ring);
  145. return T_EXIT_PASS;
  146. err:
  147. if (fname != argv[1])
  148. unlink(fname);
  149. return T_EXIT_FAIL;
  150. skip:
  151. if (fname != argv[1])
  152. unlink(fname);
  153. return T_EXIT_SKIP;
  154. }