12 typedef unsigned long long u64;
14 static size_t length = 1 << 24;
16 static u64 read_rss(void)
18 char buf[4096], *s = buf;
22 fd = open("/proc/self/statm", O_RDONLY);
24 memset(buf, 0, sizeof(buf));
25 read(fd, buf, sizeof(buf) - 1);
26 for (i = 0; i < 1; i++)
27 s = strchr(s, ' ') + 1;
28 rss = strtoull(s, NULL, 10);
29 return rss << 12; /* assumes 4k pagesize */
32 static void do_mmap(int fd, int extra_flags, int unmap)
35 int flags = MAP_PRIVATE | MAP_POPULATE | extra_flags;
39 p = mmap(NULL, length, PROT_READ | PROT_WRITE, flags, fd, 0);
40 assert(p != MAP_FAILED ||
41 !"mmap returned an unexpected error");
43 assert(llabs(after - before - length) < 0x40000 ||
44 !"rss didn't grow as expected");
49 assert(llabs(after - before) < 0x40000 ||
50 !"rss didn't shrink as expected");
53 static int open_file(const char *path)
58 fd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL
59 | O_LARGEFILE | O_CLOEXEC, 0600);
62 err = ftruncate(fd, length);
71 fd = open_file("/dev/shm/hugetlbhog");
72 hugefd = open_file("/hugepages/hugetlbhog");
74 system("echo 100 > /proc/sys/vm/nr_hugepages");
75 do_mmap(-1, MAP_ANONYMOUS, 1);
77 do_mmap(-1, MAP_ANONYMOUS | MAP_HUGETLB, 1);
78 do_mmap(hugefd, 0, 1);
79 do_mmap(hugefd, MAP_HUGETLB, 1);
80 /* Leak the last one to test do_exit() */
81 do_mmap(-1, MAP_ANONYMOUS | MAP_HUGETLB, 0);
82 printf("oll korrekt.\n");