compaction_test.c 5.2 KB

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