0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <stdlib.h>
0010 #include <stdio.h>
0011 #include <stdint.h>
0012 #include <err.h>
0013 #include <time.h>
0014 #include <unistd.h>
0015 #include <fcntl.h>
0016 #include <string.h>
0017 #include <sys/mman.h>
0018 #include "util.h"
0019
0020 int backing_fd = -1;
0021 int mmap_flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE;
0022 #define PROT_RW (PROT_READ | PROT_WRITE)
0023
0024 int main(int argc, char **argv)
0025 {
0026 size_t ram, len;
0027 void *ptr, *p;
0028 struct timespec a, b;
0029 int i = 0;
0030 char *name = NULL;
0031 double s;
0032 uint8_t *map;
0033 size_t map_len;
0034 int pagemap_fd;
0035
0036 ram = sysconf(_SC_PHYS_PAGES);
0037 if (ram > SIZE_MAX / sysconf(_SC_PAGESIZE) / 4)
0038 ram = SIZE_MAX / 4;
0039 else
0040 ram *= sysconf(_SC_PAGESIZE);
0041 len = ram;
0042
0043 while (++i < argc) {
0044 if (!strcmp(argv[i], "-h"))
0045 errx(1, "usage: %s [size in MiB]", argv[0]);
0046 else if (!strcmp(argv[i], "-f"))
0047 name = argv[++i];
0048 else
0049 len = atoll(argv[i]) << 20;
0050 }
0051
0052 if (name) {
0053 backing_fd = open(name, O_RDWR);
0054 if (backing_fd == -1)
0055 errx(2, "open %s", name);
0056 mmap_flags = MAP_SHARED;
0057 }
0058
0059 warnx("allocate %zd transhuge pages, using %zd MiB virtual memory"
0060 " and %zd MiB of ram", len >> HPAGE_SHIFT, len >> 20,
0061 ram >> (20 + HPAGE_SHIFT - PAGE_SHIFT - 1));
0062
0063 pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
0064 if (pagemap_fd < 0)
0065 err(2, "open pagemap");
0066
0067 len -= len % HPAGE_SIZE;
0068 ptr = mmap(NULL, len + HPAGE_SIZE, PROT_RW, mmap_flags, backing_fd, 0);
0069 if (ptr == MAP_FAILED)
0070 err(2, "initial mmap");
0071 ptr += HPAGE_SIZE - (uintptr_t)ptr % HPAGE_SIZE;
0072
0073 if (madvise(ptr, len, MADV_HUGEPAGE))
0074 err(2, "MADV_HUGEPAGE");
0075
0076 map_len = ram >> (HPAGE_SHIFT - 1);
0077 map = malloc(map_len);
0078 if (!map)
0079 errx(2, "map malloc");
0080
0081 while (1) {
0082 int nr_succeed = 0, nr_failed = 0, nr_pages = 0;
0083
0084 memset(map, 0, map_len);
0085
0086 clock_gettime(CLOCK_MONOTONIC, &a);
0087 for (p = ptr; p < ptr + len; p += HPAGE_SIZE) {
0088 int64_t pfn;
0089
0090 pfn = allocate_transhuge(p, pagemap_fd);
0091
0092 if (pfn < 0) {
0093 nr_failed++;
0094 } else {
0095 size_t idx = pfn >> (HPAGE_SHIFT - PAGE_SHIFT);
0096
0097 nr_succeed++;
0098 if (idx >= map_len) {
0099 map = realloc(map, idx + 1);
0100 if (!map)
0101 errx(2, "map realloc");
0102 memset(map + map_len, 0, idx + 1 - map_len);
0103 map_len = idx + 1;
0104 }
0105 if (!map[idx])
0106 nr_pages++;
0107 map[idx] = 1;
0108 }
0109
0110
0111 if (madvise(p, HPAGE_SIZE - PAGE_SIZE, MADV_DONTNEED))
0112 err(2, "MADV_DONTNEED");
0113 }
0114 clock_gettime(CLOCK_MONOTONIC, &b);
0115 s = b.tv_sec - a.tv_sec + (b.tv_nsec - a.tv_nsec) / 1000000000.;
0116
0117 warnx("%.3f s/loop, %.3f ms/page, %10.3f MiB/s\t"
0118 "%4d succeed, %4d failed, %4d different pages",
0119 s, s * 1000 / (len >> HPAGE_SHIFT), len / s / (1 << 20),
0120 nr_succeed, nr_failed, nr_pages);
0121 }
0122 }