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
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <err.h>
13 #include <time.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <string.h>
17 #include <sys/mman.h>
18 #include "vm_util.h"
19
20 int backing_fd = -1;
21 int mmap_flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE;
22 #define PROT_RW (PROT_READ | PROT_WRITE)
23
main(int argc,char ** argv)24 int main(int argc, char **argv)
25 {
26 size_t ram, len;
27 void *ptr, *p;
28 struct timespec start, a, b;
29 int i = 0;
30 char *name = NULL;
31 double s;
32 uint8_t *map;
33 size_t map_len;
34 int pagemap_fd;
35 int duration = 0;
36
37 ram = sysconf(_SC_PHYS_PAGES);
38 if (ram > SIZE_MAX / psize() / 4)
39 ram = SIZE_MAX / 4;
40 else
41 ram *= psize();
42 len = ram;
43
44 while (++i < argc) {
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"))
48 name = argv[++i];
49 else if (!strcmp(argv[i], "-d"))
50 duration = atoi(argv[++i]);
51 else
52 len = atoll(argv[i]) << 20;
53 }
54
55 if (name) {
56 backing_fd = open(name, O_RDWR);
57 if (backing_fd == -1)
58 errx(2, "open %s", name);
59 mmap_flags = MAP_SHARED;
60 }
61
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));
65
66 pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
67 if (pagemap_fd < 0)
68 err(2, "open pagemap");
69
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;
75
76 if (madvise(ptr, len, MADV_HUGEPAGE))
77 err(2, "MADV_HUGEPAGE");
78
79 map_len = ram >> (HPAGE_SHIFT - 1);
80 map = malloc(map_len);
81 if (!map)
82 errx(2, "map malloc");
83
84 clock_gettime(CLOCK_MONOTONIC, &start);
85
86 while (1) {
87 int nr_succeed = 0, nr_failed = 0, nr_pages = 0;
88
89 memset(map, 0, map_len);
90
91 clock_gettime(CLOCK_MONOTONIC, &a);
92 for (p = ptr; p < ptr + len; p += HPAGE_SIZE) {
93 int64_t pfn;
94
95 pfn = allocate_transhuge(p, pagemap_fd);
96
97 if (pfn < 0) {
98 nr_failed++;
99 } else {
100 size_t idx = pfn >> (HPAGE_SHIFT - pshift());
101
102 nr_succeed++;
103 if (idx >= map_len) {
104 map = realloc(map, idx + 1);
105 if (!map)
106 errx(2, "map realloc");
107 memset(map + map_len, 0, idx + 1 - map_len);
108 map_len = idx + 1;
109 }
110 if (!map[idx])
111 nr_pages++;
112 map[idx] = 1;
113 }
114
115 /* split transhuge page, keep last page */
116 if (madvise(p, HPAGE_SIZE - psize(), MADV_DONTNEED))
117 err(2, "MADV_DONTNEED");
118 }
119 clock_gettime(CLOCK_MONOTONIC, &b);
120 s = b.tv_sec - a.tv_sec + (b.tv_nsec - a.tv_nsec) / 1000000000.;
121
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);
126
127 if (duration > 0 && b.tv_sec - start.tv_sec >= duration)
128 return 0;
129 }
130 }
131