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.
21 int mmap_flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE;
22 #define PROT_RW (PROT_READ | PROT_WRITE)
24 int main(int argc, char **argv)
28 struct timespec start, a, b;
37 ram = sysconf(_SC_PHYS_PAGES);
38 if (ram > SIZE_MAX / psize() / 4)
45 if (!strcmp(argv[i], "-h"))
46 errx(1, "usage: %s [-f <filename>] [-d <duration>] [size in MiB]", argv[0]);
47 else if (!strcmp(argv[i], "-f"))
49 else if (!strcmp(argv[i], "-d"))
50 duration = atoi(argv[++i]);
52 len = atoll(argv[i]) << 20;
56 backing_fd = open(name, O_RDWR);
58 errx(2, "open %s", name);
59 mmap_flags = MAP_SHARED;
62 warnx("allocate %zd transhuge pages, using %zd MiB virtual memory"
63 " and %zd MiB of ram", len >> HPAGE_SHIFT, len >> 20,
64 ram >> (20 + HPAGE_SHIFT - pshift() - 1));
66 pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
68 err(2, "open pagemap");
70 len -= len % HPAGE_SIZE;
71 ptr = mmap(NULL, len + HPAGE_SIZE, PROT_RW, mmap_flags, backing_fd, 0);
72 if (ptr == MAP_FAILED)
73 err(2, "initial mmap");
74 ptr += HPAGE_SIZE - (uintptr_t)ptr % HPAGE_SIZE;
76 if (madvise(ptr, len, MADV_HUGEPAGE))
77 err(2, "MADV_HUGEPAGE");
79 map_len = ram >> (HPAGE_SHIFT - 1);
80 map = malloc(map_len);
82 errx(2, "map malloc");
84 clock_gettime(CLOCK_MONOTONIC, &start);
87 int nr_succeed = 0, nr_failed = 0, nr_pages = 0;
89 memset(map, 0, map_len);
91 clock_gettime(CLOCK_MONOTONIC, &a);
92 for (p = ptr; p < ptr + len; p += HPAGE_SIZE) {
95 pfn = allocate_transhuge(p, pagemap_fd);
100 size_t idx = pfn >> (HPAGE_SHIFT - pshift());
103 if (idx >= map_len) {
104 map = realloc(map, idx + 1);
106 errx(2, "map realloc");
107 memset(map + map_len, 0, idx + 1 - map_len);
115 /* split transhuge page, keep last page */
116 if (madvise(p, HPAGE_SIZE - psize(), MADV_DONTNEED))
117 err(2, "MADV_DONTNEED");
119 clock_gettime(CLOCK_MONOTONIC, &b);
120 s = b.tv_sec - a.tv_sec + (b.tv_nsec - a.tv_nsec) / 1000000000.;
122 warnx("%.3f s/loop, %.3f ms/page, %10.3f MiB/s\t"
123 "%4d succeed, %4d failed, %4d different pages",
124 s, s * 1000 / (len >> HPAGE_SHIFT), len / s / (1 << 20),
125 nr_succeed, nr_failed, nr_pages);
127 if (duration > 0 && b.tv_sec - start.tv_sec >= duration)