transhuge-stress.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Stress test for transparent huge pages, memory compaction and migration.
  3. *
  4. * Authors: Konstantin Khlebnikov <koct9i@gmail.com>
  5. *
  6. * This is free and unencumbered software released into the public domain.
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <err.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <string.h>
  16. #include <sys/mman.h>
  17. #define PAGE_SHIFT 12
  18. #define HPAGE_SHIFT 21
  19. #define PAGE_SIZE (1 << PAGE_SHIFT)
  20. #define HPAGE_SIZE (1 << HPAGE_SHIFT)
  21. #define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0)
  22. #define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1))
  23. int pagemap_fd;
  24. int64_t allocate_transhuge(void *ptr)
  25. {
  26. uint64_t ent[2];
  27. /* drop pmd */
  28. if (mmap(ptr, HPAGE_SIZE, PROT_READ | PROT_WRITE,
  29. MAP_FIXED | MAP_ANONYMOUS |
  30. MAP_NORESERVE | MAP_PRIVATE, -1, 0) != ptr)
  31. errx(2, "mmap transhuge");
  32. if (madvise(ptr, HPAGE_SIZE, MADV_HUGEPAGE))
  33. err(2, "MADV_HUGEPAGE");
  34. /* allocate transparent huge page */
  35. *(volatile void **)ptr = ptr;
  36. if (pread(pagemap_fd, ent, sizeof(ent),
  37. (uintptr_t)ptr >> (PAGE_SHIFT - 3)) != sizeof(ent))
  38. err(2, "read pagemap");
  39. if (PAGEMAP_PRESENT(ent[0]) && PAGEMAP_PRESENT(ent[1]) &&
  40. PAGEMAP_PFN(ent[0]) + 1 == PAGEMAP_PFN(ent[1]) &&
  41. !(PAGEMAP_PFN(ent[0]) & ((1 << (HPAGE_SHIFT - PAGE_SHIFT)) - 1)))
  42. return PAGEMAP_PFN(ent[0]);
  43. return -1;
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. size_t ram, len;
  48. void *ptr, *p;
  49. struct timespec a, b;
  50. double s;
  51. uint8_t *map;
  52. size_t map_len;
  53. ram = sysconf(_SC_PHYS_PAGES);
  54. if (ram > SIZE_MAX / sysconf(_SC_PAGESIZE) / 4)
  55. ram = SIZE_MAX / 4;
  56. else
  57. ram *= sysconf(_SC_PAGESIZE);
  58. if (argc == 1)
  59. len = ram;
  60. else if (!strcmp(argv[1], "-h"))
  61. errx(1, "usage: %s [size in MiB]", argv[0]);
  62. else
  63. len = atoll(argv[1]) << 20;
  64. warnx("allocate %zd transhuge pages, using %zd MiB virtual memory"
  65. " and %zd MiB of ram", len >> HPAGE_SHIFT, len >> 20,
  66. len >> (20 + HPAGE_SHIFT - PAGE_SHIFT - 1));
  67. pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
  68. if (pagemap_fd < 0)
  69. err(2, "open pagemap");
  70. len -= len % HPAGE_SIZE;
  71. ptr = mmap(NULL, len + HPAGE_SIZE, PROT_READ | PROT_WRITE,
  72. MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE, -1, 0);
  73. if (ptr == MAP_FAILED)
  74. err(2, "initial mmap");
  75. ptr += HPAGE_SIZE - (uintptr_t)ptr % HPAGE_SIZE;
  76. if (madvise(ptr, len, MADV_HUGEPAGE))
  77. err(2, "MADV_HUGEPAGE");
  78. map_len = ram >> (HPAGE_SHIFT - 1);
  79. map = malloc(map_len);
  80. if (!map)
  81. errx(2, "map malloc");
  82. while (1) {
  83. int nr_succeed = 0, nr_failed = 0, nr_pages = 0;
  84. memset(map, 0, map_len);
  85. clock_gettime(CLOCK_MONOTONIC, &a);
  86. for (p = ptr; p < ptr + len; p += HPAGE_SIZE) {
  87. int64_t pfn;
  88. pfn = allocate_transhuge(p);
  89. if (pfn < 0) {
  90. nr_failed++;
  91. } else {
  92. size_t idx = pfn >> (HPAGE_SHIFT - PAGE_SHIFT);
  93. nr_succeed++;
  94. if (idx >= map_len) {
  95. map = realloc(map, idx + 1);
  96. if (!map)
  97. errx(2, "map realloc");
  98. memset(map + map_len, 0, idx + 1 - map_len);
  99. map_len = idx + 1;
  100. }
  101. if (!map[idx])
  102. nr_pages++;
  103. map[idx] = 1;
  104. }
  105. /* split transhuge page, keep last page */
  106. if (madvise(p, HPAGE_SIZE - PAGE_SIZE, MADV_DONTNEED))
  107. err(2, "MADV_DONTNEED");
  108. }
  109. clock_gettime(CLOCK_MONOTONIC, &b);
  110. s = b.tv_sec - a.tv_sec + (b.tv_nsec - a.tv_nsec) / 1000000000.;
  111. warnx("%.3f s/loop, %.3f ms/page, %10.3f MiB/s\t"
  112. "%4d succeed, %4d failed, %4d different pages",
  113. s, s * 1000 / (len >> HPAGE_SHIFT), len / s / (1 << 20),
  114. nr_succeed, nr_failed, nr_pages);
  115. }
  116. }