thp_swap_allocator_test.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * thp_swap_allocator_test
  4. *
  5. * The purpose of this test program is helping check if THP swpout
  6. * can correctly get swap slots to swap out as a whole instead of
  7. * being split. It randomly releases swap entries through madvise
  8. * DONTNEED and swapin/out on two memory areas: a memory area for
  9. * 64KB THP and the other area for small folios. The second memory
  10. * can be enabled by "-s".
  11. * Before running the program, we need to setup a zRAM or similar
  12. * swap device by:
  13. * echo lzo > /sys/block/zram0/comp_algorithm
  14. * echo 64M > /sys/block/zram0/disksize
  15. * echo never > /sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled
  16. * echo always > /sys/kernel/mm/transparent_hugepage/hugepages-64kB/enabled
  17. * mkswap /dev/zram0
  18. * swapon /dev/zram0
  19. * The expected result should be 0% anon swpout fallback ratio w/ or
  20. * w/o "-s".
  21. *
  22. * Author(s): Barry Song <v-songbaohua@oppo.com>
  23. */
  24. #define _GNU_SOURCE
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <linux/mman.h>
  30. #include <sys/mman.h>
  31. #include <errno.h>
  32. #include <time.h>
  33. #define MEMSIZE_MTHP (60 * 1024 * 1024)
  34. #define MEMSIZE_SMALLFOLIO (4 * 1024 * 1024)
  35. #define ALIGNMENT_MTHP (64 * 1024)
  36. #define ALIGNMENT_SMALLFOLIO (4 * 1024)
  37. #define TOTAL_DONTNEED_MTHP (16 * 1024 * 1024)
  38. #define TOTAL_DONTNEED_SMALLFOLIO (1 * 1024 * 1024)
  39. #define MTHP_FOLIO_SIZE (64 * 1024)
  40. #define SWPOUT_PATH \
  41. "/sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/swpout"
  42. #define SWPOUT_FALLBACK_PATH \
  43. "/sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/swpout_fallback"
  44. static void *aligned_alloc_mem(size_t size, size_t alignment)
  45. {
  46. void *mem = NULL;
  47. if (posix_memalign(&mem, alignment, size) != 0) {
  48. perror("posix_memalign");
  49. return NULL;
  50. }
  51. return mem;
  52. }
  53. /*
  54. * This emulates the behavior of native libc and Java heap,
  55. * as well as process exit and munmap. It helps generate mTHP
  56. * and ensures that iterations can proceed with mTHP, as we
  57. * currently don't support large folios swap-in.
  58. */
  59. static void random_madvise_dontneed(void *mem, size_t mem_size,
  60. size_t align_size, size_t total_dontneed_size)
  61. {
  62. size_t num_pages = total_dontneed_size / align_size;
  63. size_t i;
  64. size_t offset;
  65. void *addr;
  66. for (i = 0; i < num_pages; ++i) {
  67. offset = (rand() % (mem_size / align_size)) * align_size;
  68. addr = (char *)mem + offset;
  69. if (madvise(addr, align_size, MADV_DONTNEED) != 0)
  70. perror("madvise dontneed");
  71. memset(addr, 0x11, align_size);
  72. }
  73. }
  74. static void random_swapin(void *mem, size_t mem_size,
  75. size_t align_size, size_t total_swapin_size)
  76. {
  77. size_t num_pages = total_swapin_size / align_size;
  78. size_t i;
  79. size_t offset;
  80. void *addr;
  81. for (i = 0; i < num_pages; ++i) {
  82. offset = (rand() % (mem_size / align_size)) * align_size;
  83. addr = (char *)mem + offset;
  84. memset(addr, 0x11, align_size);
  85. }
  86. }
  87. static unsigned long read_stat(const char *path)
  88. {
  89. FILE *file;
  90. unsigned long value;
  91. file = fopen(path, "r");
  92. if (!file) {
  93. perror("fopen");
  94. return 0;
  95. }
  96. if (fscanf(file, "%lu", &value) != 1) {
  97. perror("fscanf");
  98. fclose(file);
  99. return 0;
  100. }
  101. fclose(file);
  102. return value;
  103. }
  104. int main(int argc, char *argv[])
  105. {
  106. int use_small_folio = 0, aligned_swapin = 0;
  107. void *mem1 = NULL, *mem2 = NULL;
  108. int i;
  109. for (i = 1; i < argc; ++i) {
  110. if (strcmp(argv[i], "-s") == 0)
  111. use_small_folio = 1;
  112. else if (strcmp(argv[i], "-a") == 0)
  113. aligned_swapin = 1;
  114. }
  115. mem1 = aligned_alloc_mem(MEMSIZE_MTHP, ALIGNMENT_MTHP);
  116. if (mem1 == NULL) {
  117. fprintf(stderr, "Failed to allocate large folios memory\n");
  118. return EXIT_FAILURE;
  119. }
  120. if (madvise(mem1, MEMSIZE_MTHP, MADV_HUGEPAGE) != 0) {
  121. perror("madvise hugepage for mem1");
  122. free(mem1);
  123. return EXIT_FAILURE;
  124. }
  125. if (use_small_folio) {
  126. mem2 = aligned_alloc_mem(MEMSIZE_SMALLFOLIO, ALIGNMENT_SMALLFOLIO);
  127. if (mem2 == NULL) {
  128. fprintf(stderr, "Failed to allocate small folios memory\n");
  129. free(mem1);
  130. return EXIT_FAILURE;
  131. }
  132. if (madvise(mem2, MEMSIZE_SMALLFOLIO, MADV_NOHUGEPAGE) != 0) {
  133. perror("madvise nohugepage for mem2");
  134. free(mem1);
  135. free(mem2);
  136. return EXIT_FAILURE;
  137. }
  138. }
  139. /* warm-up phase to occupy the swapfile */
  140. memset(mem1, 0x11, MEMSIZE_MTHP);
  141. madvise(mem1, MEMSIZE_MTHP, MADV_PAGEOUT);
  142. if (use_small_folio) {
  143. memset(mem2, 0x11, MEMSIZE_SMALLFOLIO);
  144. madvise(mem2, MEMSIZE_SMALLFOLIO, MADV_PAGEOUT);
  145. }
  146. /* iterations with newly created mTHP, swap-in, and swap-out */
  147. for (i = 0; i < 100; ++i) {
  148. unsigned long initial_swpout;
  149. unsigned long initial_swpout_fallback;
  150. unsigned long final_swpout;
  151. unsigned long final_swpout_fallback;
  152. unsigned long swpout_inc;
  153. unsigned long swpout_fallback_inc;
  154. double fallback_percentage;
  155. initial_swpout = read_stat(SWPOUT_PATH);
  156. initial_swpout_fallback = read_stat(SWPOUT_FALLBACK_PATH);
  157. /*
  158. * The following setup creates a 1:1 ratio of mTHP to small folios
  159. * since large folio swap-in isn't supported yet. Once we support
  160. * mTHP swap-in, we'll likely need to reduce MEMSIZE_MTHP and
  161. * increase MEMSIZE_SMALLFOLIO to maintain the ratio.
  162. */
  163. random_swapin(mem1, MEMSIZE_MTHP,
  164. aligned_swapin ? ALIGNMENT_MTHP : ALIGNMENT_SMALLFOLIO,
  165. TOTAL_DONTNEED_MTHP);
  166. random_madvise_dontneed(mem1, MEMSIZE_MTHP, ALIGNMENT_MTHP,
  167. TOTAL_DONTNEED_MTHP);
  168. if (use_small_folio) {
  169. random_swapin(mem2, MEMSIZE_SMALLFOLIO,
  170. ALIGNMENT_SMALLFOLIO,
  171. TOTAL_DONTNEED_SMALLFOLIO);
  172. }
  173. if (madvise(mem1, MEMSIZE_MTHP, MADV_PAGEOUT) != 0) {
  174. perror("madvise pageout for mem1");
  175. free(mem1);
  176. if (mem2 != NULL)
  177. free(mem2);
  178. return EXIT_FAILURE;
  179. }
  180. if (use_small_folio) {
  181. if (madvise(mem2, MEMSIZE_SMALLFOLIO, MADV_PAGEOUT) != 0) {
  182. perror("madvise pageout for mem2");
  183. free(mem1);
  184. free(mem2);
  185. return EXIT_FAILURE;
  186. }
  187. }
  188. final_swpout = read_stat(SWPOUT_PATH);
  189. final_swpout_fallback = read_stat(SWPOUT_FALLBACK_PATH);
  190. swpout_inc = final_swpout - initial_swpout;
  191. swpout_fallback_inc = final_swpout_fallback - initial_swpout_fallback;
  192. fallback_percentage = (double)swpout_fallback_inc /
  193. (swpout_fallback_inc + swpout_inc) * 100;
  194. printf("Iteration %d: swpout inc: %lu, swpout fallback inc: %lu, Fallback percentage: %.2f%%\n",
  195. i + 1, swpout_inc, swpout_fallback_inc, fallback_percentage);
  196. }
  197. free(mem1);
  198. if (mem2 != NULL)
  199. free(mem2);
  200. return EXIT_SUCCESS;
  201. }