compaction_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * A test for the patch "Allow compaction of unevictable pages".
  5. * With this patch we should be able to allocate at least 1/4
  6. * of RAM in huge pages. Without the patch much less is
  7. * allocated.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/mman.h>
  12. #include <sys/resource.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include "../kselftest.h"
  18. #define MAP_SIZE 1048576
  19. struct map_list {
  20. void *map;
  21. struct map_list *next;
  22. };
  23. int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize)
  24. {
  25. char buffer[256] = {0};
  26. char *cmd = "cat /proc/meminfo | grep -i memfree | grep -o '[0-9]*'";
  27. FILE *cmdfile = popen(cmd, "r");
  28. if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
  29. perror("Failed to read meminfo\n");
  30. return -1;
  31. }
  32. pclose(cmdfile);
  33. *memfree = atoll(buffer);
  34. cmd = "cat /proc/meminfo | grep -i hugepagesize | grep -o '[0-9]*'";
  35. cmdfile = popen(cmd, "r");
  36. if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
  37. perror("Failed to read meminfo\n");
  38. return -1;
  39. }
  40. pclose(cmdfile);
  41. *hugepagesize = atoll(buffer);
  42. return 0;
  43. }
  44. int prereq(void)
  45. {
  46. char allowed;
  47. int fd;
  48. fd = open("/proc/sys/vm/compact_unevictable_allowed",
  49. O_RDONLY | O_NONBLOCK);
  50. if (fd < 0) {
  51. perror("Failed to open\n"
  52. "/proc/sys/vm/compact_unevictable_allowed\n");
  53. return -1;
  54. }
  55. if (read(fd, &allowed, sizeof(char)) != sizeof(char)) {
  56. perror("Failed to read from\n"
  57. "/proc/sys/vm/compact_unevictable_allowed\n");
  58. close(fd);
  59. return -1;
  60. }
  61. close(fd);
  62. if (allowed == '1')
  63. return 0;
  64. return -1;
  65. }
  66. int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
  67. {
  68. int fd;
  69. int compaction_index = 0;
  70. char initial_nr_hugepages[10] = {0};
  71. char nr_hugepages[10] = {0};
  72. /* We want to test with 80% of available memory. Else, OOM killer comes
  73. in to play */
  74. mem_free = mem_free * 0.8;
  75. fd = open("/proc/sys/vm/nr_hugepages", O_RDWR | O_NONBLOCK);
  76. if (fd < 0) {
  77. perror("Failed to open /proc/sys/vm/nr_hugepages");
  78. return -1;
  79. }
  80. if (read(fd, initial_nr_hugepages, sizeof(initial_nr_hugepages)) <= 0) {
  81. perror("Failed to read from /proc/sys/vm/nr_hugepages");
  82. goto close_fd;
  83. }
  84. /* Start with the initial condition of 0 huge pages*/
  85. if (write(fd, "0", sizeof(char)) != sizeof(char)) {
  86. perror("Failed to write 0 to /proc/sys/vm/nr_hugepages\n");
  87. goto close_fd;
  88. }
  89. lseek(fd, 0, SEEK_SET);
  90. /* Request a large number of huge pages. The Kernel will allocate
  91. as much as it can */
  92. if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) {
  93. perror("Failed to write 100000 to /proc/sys/vm/nr_hugepages\n");
  94. goto close_fd;
  95. }
  96. lseek(fd, 0, SEEK_SET);
  97. if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
  98. perror("Failed to re-read from /proc/sys/vm/nr_hugepages\n");
  99. goto close_fd;
  100. }
  101. /* We should have been able to request at least 1/3 rd of the memory in
  102. huge pages */
  103. compaction_index = mem_free/(atoi(nr_hugepages) * hugepage_size);
  104. if (compaction_index > 3) {
  105. printf("No of huge pages allocated = %d\n",
  106. (atoi(nr_hugepages)));
  107. fprintf(stderr, "ERROR: Less that 1/%d of memory is available\n"
  108. "as huge pages\n", compaction_index);
  109. goto close_fd;
  110. }
  111. printf("No of huge pages allocated = %d\n",
  112. (atoi(nr_hugepages)));
  113. lseek(fd, 0, SEEK_SET);
  114. if (write(fd, initial_nr_hugepages, strlen(initial_nr_hugepages))
  115. != strlen(initial_nr_hugepages)) {
  116. perror("Failed to write value to /proc/sys/vm/nr_hugepages\n");
  117. goto close_fd;
  118. }
  119. close(fd);
  120. return 0;
  121. close_fd:
  122. close(fd);
  123. printf("Not OK. Compaction test failed.");
  124. return -1;
  125. }
  126. int main(int argc, char **argv)
  127. {
  128. struct rlimit lim;
  129. struct map_list *list, *entry;
  130. size_t page_size, i;
  131. void *map = NULL;
  132. unsigned long mem_free = 0;
  133. unsigned long hugepage_size = 0;
  134. unsigned long mem_fragmentable = 0;
  135. if (prereq() != 0) {
  136. printf("Either the sysctl compact_unevictable_allowed is not\n"
  137. "set to 1 or couldn't read the proc file.\n"
  138. "Skipping the test\n");
  139. return KSFT_SKIP;
  140. }
  141. lim.rlim_cur = RLIM_INFINITY;
  142. lim.rlim_max = RLIM_INFINITY;
  143. if (setrlimit(RLIMIT_MEMLOCK, &lim)) {
  144. perror("Failed to set rlimit:\n");
  145. return -1;
  146. }
  147. page_size = getpagesize();
  148. list = NULL;
  149. if (read_memory_info(&mem_free, &hugepage_size) != 0) {
  150. printf("ERROR: Cannot read meminfo\n");
  151. return -1;
  152. }
  153. mem_fragmentable = mem_free * 0.8 / 1024;
  154. while (mem_fragmentable > 0) {
  155. map = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
  156. MAP_ANONYMOUS | MAP_PRIVATE | MAP_LOCKED, -1, 0);
  157. if (map == MAP_FAILED)
  158. break;
  159. entry = malloc(sizeof(struct map_list));
  160. if (!entry) {
  161. munmap(map, MAP_SIZE);
  162. break;
  163. }
  164. entry->map = map;
  165. entry->next = list;
  166. list = entry;
  167. /* Write something (in this case the address of the map) to
  168. * ensure that KSM can't merge the mapped pages
  169. */
  170. for (i = 0; i < MAP_SIZE; i += page_size)
  171. *(unsigned long *)(map + i) = (unsigned long)map + i;
  172. mem_fragmentable--;
  173. }
  174. for (entry = list; entry != NULL; entry = entry->next) {
  175. munmap(entry->map, MAP_SIZE);
  176. if (!entry->next)
  177. break;
  178. entry = entry->next;
  179. }
  180. if (check_compaction(mem_free, hugepage_size) == 0)
  181. return 0;
  182. return -1;
  183. }