compaction_test.c 5.1 KB

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