compaction_test.c 5.0 KB

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