test_cachestat.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #define __SANE_USERSPACE_TYPES__ // Use ll64
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. #include <linux/kernel.h>
  7. #include <linux/magic.h>
  8. #include <linux/mman.h>
  9. #include <sys/mman.h>
  10. #include <sys/shm.h>
  11. #include <sys/syscall.h>
  12. #include <sys/vfs.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include "../kselftest.h"
  18. #define NR_TESTS 9
  19. static const char * const dev_files[] = {
  20. "/dev/zero", "/dev/null", "/dev/urandom",
  21. "/proc/version", "/proc"
  22. };
  23. void print_cachestat(struct cachestat *cs)
  24. {
  25. ksft_print_msg(
  26. "Using cachestat: Cached: %llu, Dirty: %llu, Writeback: %llu, Evicted: %llu, Recently Evicted: %llu\n",
  27. cs->nr_cache, cs->nr_dirty, cs->nr_writeback,
  28. cs->nr_evicted, cs->nr_recently_evicted);
  29. }
  30. bool write_exactly(int fd, size_t filesize)
  31. {
  32. int random_fd = open("/dev/urandom", O_RDONLY);
  33. char *cursor, *data;
  34. int remained;
  35. bool ret;
  36. if (random_fd < 0) {
  37. ksft_print_msg("Unable to access urandom.\n");
  38. ret = false;
  39. goto out;
  40. }
  41. data = malloc(filesize);
  42. if (!data) {
  43. ksft_print_msg("Unable to allocate data.\n");
  44. ret = false;
  45. goto close_random_fd;
  46. }
  47. remained = filesize;
  48. cursor = data;
  49. while (remained) {
  50. ssize_t read_len = read(random_fd, cursor, remained);
  51. if (read_len <= 0) {
  52. ksft_print_msg("Unable to read from urandom.\n");
  53. ret = false;
  54. goto out_free_data;
  55. }
  56. remained -= read_len;
  57. cursor += read_len;
  58. }
  59. /* write random data to fd */
  60. remained = filesize;
  61. cursor = data;
  62. while (remained) {
  63. ssize_t write_len = write(fd, cursor, remained);
  64. if (write_len <= 0) {
  65. ksft_print_msg("Unable write random data to file.\n");
  66. ret = false;
  67. goto out_free_data;
  68. }
  69. remained -= write_len;
  70. cursor += write_len;
  71. }
  72. ret = true;
  73. out_free_data:
  74. free(data);
  75. close_random_fd:
  76. close(random_fd);
  77. out:
  78. return ret;
  79. }
  80. /*
  81. * fsync() is implemented via noop_fsync() on tmpfs. This makes the fsync()
  82. * test fail below, so we need to check for test file living on a tmpfs.
  83. */
  84. static bool is_on_tmpfs(int fd)
  85. {
  86. struct statfs statfs_buf;
  87. if (fstatfs(fd, &statfs_buf))
  88. return false;
  89. return statfs_buf.f_type == TMPFS_MAGIC;
  90. }
  91. /*
  92. * Open/create the file at filename, (optionally) write random data to it
  93. * (exactly num_pages), then test the cachestat syscall on this file.
  94. *
  95. * If test_fsync == true, fsync the file, then check the number of dirty
  96. * pages.
  97. */
  98. static int test_cachestat(const char *filename, bool write_random, bool create,
  99. bool test_fsync, unsigned long num_pages,
  100. int open_flags, mode_t open_mode)
  101. {
  102. size_t PS = sysconf(_SC_PAGESIZE);
  103. int filesize = num_pages * PS;
  104. int ret = KSFT_PASS;
  105. long syscall_ret;
  106. struct cachestat cs;
  107. struct cachestat_range cs_range = { 0, filesize };
  108. int fd = open(filename, open_flags, open_mode);
  109. if (fd == -1) {
  110. ksft_print_msg("Unable to create/open file.\n");
  111. ret = KSFT_FAIL;
  112. goto out;
  113. } else {
  114. ksft_print_msg("Create/open %s\n", filename);
  115. }
  116. if (write_random) {
  117. if (!write_exactly(fd, filesize)) {
  118. ksft_print_msg("Unable to access urandom.\n");
  119. ret = KSFT_FAIL;
  120. goto out1;
  121. }
  122. }
  123. syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
  124. ksft_print_msg("Cachestat call returned %ld\n", syscall_ret);
  125. if (syscall_ret) {
  126. ksft_print_msg("Cachestat returned non-zero.\n");
  127. ret = KSFT_FAIL;
  128. goto out1;
  129. } else {
  130. print_cachestat(&cs);
  131. if (write_random) {
  132. if (cs.nr_cache + cs.nr_evicted != num_pages) {
  133. ksft_print_msg(
  134. "Total number of cached and evicted pages is off.\n");
  135. ret = KSFT_FAIL;
  136. }
  137. }
  138. }
  139. if (test_fsync) {
  140. if (is_on_tmpfs(fd)) {
  141. ret = KSFT_SKIP;
  142. } else if (fsync(fd)) {
  143. ksft_print_msg("fsync fails.\n");
  144. ret = KSFT_FAIL;
  145. } else {
  146. syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
  147. ksft_print_msg("Cachestat call (after fsync) returned %ld\n",
  148. syscall_ret);
  149. if (!syscall_ret) {
  150. print_cachestat(&cs);
  151. if (cs.nr_dirty) {
  152. ret = KSFT_FAIL;
  153. ksft_print_msg(
  154. "Number of dirty should be zero after fsync.\n");
  155. }
  156. } else {
  157. ksft_print_msg("Cachestat (after fsync) returned non-zero.\n");
  158. ret = KSFT_FAIL;
  159. goto out1;
  160. }
  161. }
  162. }
  163. out1:
  164. close(fd);
  165. if (create)
  166. remove(filename);
  167. out:
  168. return ret;
  169. }
  170. bool test_cachestat_shmem(void)
  171. {
  172. size_t PS = sysconf(_SC_PAGESIZE);
  173. size_t filesize = PS * 512 * 2; /* 2 2MB huge pages */
  174. int syscall_ret;
  175. size_t compute_len = PS * 512;
  176. struct cachestat_range cs_range = { PS, compute_len };
  177. char *filename = "tmpshmcstat";
  178. struct cachestat cs;
  179. bool ret = true;
  180. unsigned long num_pages = compute_len / PS;
  181. int fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
  182. if (fd < 0) {
  183. ksft_print_msg("Unable to create shmem file.\n");
  184. ret = false;
  185. goto out;
  186. }
  187. if (ftruncate(fd, filesize)) {
  188. ksft_print_msg("Unable to truncate shmem file.\n");
  189. ret = false;
  190. goto close_fd;
  191. }
  192. if (!write_exactly(fd, filesize)) {
  193. ksft_print_msg("Unable to write to shmem file.\n");
  194. ret = false;
  195. goto close_fd;
  196. }
  197. syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
  198. if (syscall_ret) {
  199. ksft_print_msg("Cachestat returned non-zero.\n");
  200. ret = false;
  201. goto close_fd;
  202. } else {
  203. print_cachestat(&cs);
  204. if (cs.nr_cache + cs.nr_evicted != num_pages) {
  205. ksft_print_msg(
  206. "Total number of cached and evicted pages is off.\n");
  207. ret = false;
  208. }
  209. }
  210. close_fd:
  211. shm_unlink(filename);
  212. out:
  213. return ret;
  214. }
  215. int main(void)
  216. {
  217. int ret;
  218. ksft_print_header();
  219. ret = syscall(__NR_cachestat, -1, NULL, NULL, 0);
  220. if (ret == -1 && errno == ENOSYS)
  221. ksft_exit_skip("cachestat syscall not available\n");
  222. ksft_set_plan(NR_TESTS);
  223. if (ret == -1 && errno == EBADF) {
  224. ksft_test_result_pass("bad file descriptor recognized\n");
  225. ret = 0;
  226. } else {
  227. ksft_test_result_fail("bad file descriptor ignored\n");
  228. ret = 1;
  229. }
  230. for (int i = 0; i < 5; i++) {
  231. const char *dev_filename = dev_files[i];
  232. if (test_cachestat(dev_filename, false, false, false,
  233. 4, O_RDONLY, 0400) == KSFT_PASS)
  234. ksft_test_result_pass("cachestat works with %s\n", dev_filename);
  235. else {
  236. ksft_test_result_fail("cachestat fails with %s\n", dev_filename);
  237. ret = 1;
  238. }
  239. }
  240. if (test_cachestat("tmpfilecachestat", true, true,
  241. false, 4, O_CREAT | O_RDWR, 0600) == KSFT_PASS)
  242. ksft_test_result_pass("cachestat works with a normal file\n");
  243. else {
  244. ksft_test_result_fail("cachestat fails with normal file\n");
  245. ret = 1;
  246. }
  247. switch (test_cachestat("tmpfilecachestat", true, true,
  248. true, 4, O_CREAT | O_RDWR, 0600)) {
  249. case KSFT_FAIL:
  250. ksft_test_result_fail("cachestat fsync fails with normal file\n");
  251. ret = KSFT_FAIL;
  252. break;
  253. case KSFT_PASS:
  254. ksft_test_result_pass("cachestat fsync works with a normal file\n");
  255. break;
  256. case KSFT_SKIP:
  257. ksft_test_result_skip("tmpfilecachestat is on tmpfs\n");
  258. break;
  259. }
  260. if (test_cachestat_shmem())
  261. ksft_test_result_pass("cachestat works with a shmem file\n");
  262. else {
  263. ksft_test_result_fail("cachestat fails with a shmem file\n");
  264. ret = 1;
  265. }
  266. return ret;
  267. }