2 * Stress test for transparent huge pages, memory compaction and migration.
4 * Authors: Konstantin Khlebnikov <koct9i@gmail.com>
6 * This is free and unencumbered software released into the public domain.
20 #define HPAGE_SHIFT 21
22 #define PAGE_SIZE (1 << PAGE_SHIFT)
23 #define HPAGE_SIZE (1 << HPAGE_SHIFT)
25 #define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0)
26 #define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1))
30 int mmap_flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE;
31 #define PROT_RW (PROT_READ | PROT_WRITE)
33 int64_t allocate_transhuge(void *ptr)
38 if (mmap(ptr, HPAGE_SIZE, PROT_RW, MAP_FIXED | mmap_flags,
39 backing_fd, 0) != ptr)
40 errx(2, "mmap transhuge");
42 if (madvise(ptr, HPAGE_SIZE, MADV_HUGEPAGE))
43 err(2, "MADV_HUGEPAGE");
45 /* allocate transparent huge page */
46 *(volatile void **)ptr = ptr;
48 if (pread(pagemap_fd, ent, sizeof(ent),
49 (uintptr_t)ptr >> (PAGE_SHIFT - 3)) != sizeof(ent))
50 err(2, "read pagemap");
52 if (PAGEMAP_PRESENT(ent[0]) && PAGEMAP_PRESENT(ent[1]) &&
53 PAGEMAP_PFN(ent[0]) + 1 == PAGEMAP_PFN(ent[1]) &&
54 !(PAGEMAP_PFN(ent[0]) & ((1 << (HPAGE_SHIFT - PAGE_SHIFT)) - 1)))
55 return PAGEMAP_PFN(ent[0]);
60 int main(int argc, char **argv)
71 ram = sysconf(_SC_PHYS_PAGES);
72 if (ram > SIZE_MAX / sysconf(_SC_PAGESIZE) / 4)
75 ram *= sysconf(_SC_PAGESIZE);
79 if (!strcmp(argv[i], "-h"))
80 errx(1, "usage: %s [size in MiB]", argv[0]);
81 else if (!strcmp(argv[i], "-f"))
84 len = atoll(argv[i]) << 20;
88 backing_fd = open(name, O_RDWR);
90 errx(2, "open %s", name);
91 mmap_flags = MAP_SHARED;
94 warnx("allocate %zd transhuge pages, using %zd MiB virtual memory"
95 " and %zd MiB of ram", len >> HPAGE_SHIFT, len >> 20,
96 ram >> (20 + HPAGE_SHIFT - PAGE_SHIFT - 1));
98 pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
100 err(2, "open pagemap");
102 len -= len % HPAGE_SIZE;
103 ptr = mmap(NULL, len + HPAGE_SIZE, PROT_RW, mmap_flags, backing_fd, 0);
104 if (ptr == MAP_FAILED)
105 err(2, "initial mmap");
106 ptr += HPAGE_SIZE - (uintptr_t)ptr % HPAGE_SIZE;
108 if (madvise(ptr, len, MADV_HUGEPAGE))
109 err(2, "MADV_HUGEPAGE");
111 map_len = ram >> (HPAGE_SHIFT - 1);
112 map = malloc(map_len);
114 errx(2, "map malloc");
117 int nr_succeed = 0, nr_failed = 0, nr_pages = 0;
119 memset(map, 0, map_len);
121 clock_gettime(CLOCK_MONOTONIC, &a);
122 for (p = ptr; p < ptr + len; p += HPAGE_SIZE) {
125 pfn = allocate_transhuge(p);
130 size_t idx = pfn >> (HPAGE_SHIFT - PAGE_SHIFT);
133 if (idx >= map_len) {
134 map = realloc(map, idx + 1);
136 errx(2, "map realloc");
137 memset(map + map_len, 0, idx + 1 - map_len);
145 /* split transhuge page, keep last page */
146 if (madvise(p, HPAGE_SIZE - PAGE_SIZE, MADV_DONTNEED))
147 err(2, "MADV_DONTNEED");
149 clock_gettime(CLOCK_MONOTONIC, &b);
150 s = b.tv_sec - a.tv_sec + (b.tv_nsec - a.tv_nsec) / 1000000000.;
152 warnx("%.3f s/loop, %.3f ms/page, %10.3f MiB/s\t"
153 "%4d succeed, %4d failed, %4d different pages",
154 s, s * 1000 / (len >> HPAGE_SHIFT), len / s / (1 << 20),
155 nr_succeed, nr_failed, nr_pages);