10 #include <sys/types.h>
12 #include <linux/types.h>
14 #define MB (1UL << 20)
15 #define PAGE_SIZE sysconf(_SC_PAGESIZE)
17 #define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark)
18 #define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)
19 #define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark)
21 /* Just the flags we need, copied from mm.h: */
22 #define FOLL_WRITE 0x01 /* check pte is writable */
24 struct gup_benchmark {
29 __u32 nr_pages_per_call;
31 __u64 expansion[10]; /* For future use */
34 int main(int argc, char **argv)
36 struct gup_benchmark gup;
37 unsigned long size = 128 * MB;
38 int i, fd, filed, opt, nr_pages = 1, thp = -1, repeats = 1, write = 0;
39 int cmd = GUP_FAST_BENCHMARK, flags = MAP_PRIVATE;
40 char *file = "/dev/zero";
43 while ((opt = getopt(argc, argv, "m:r:n:f:tTLUwSH")) != -1) {
46 size = atoi(optarg) * MB;
49 repeats = atoi(optarg);
52 nr_pages = atoi(optarg);
61 cmd = GUP_LONGTERM_BENCHMARK;
73 flags &= ~MAP_PRIVATE;
77 flags |= (MAP_HUGETLB | MAP_ANONYMOUS);
84 filed = open(file, O_RDWR|O_CREAT);
90 gup.nr_pages_per_call = nr_pages;
92 gup.flags |= FOLL_WRITE;
94 fd = open("/sys/kernel/debug/gup_benchmark", O_RDWR);
96 perror("open"), exit(1);
98 p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0);
100 perror("mmap"), exit(1);
101 gup.addr = (unsigned long)p;
104 madvise(p, size, MADV_HUGEPAGE);
106 madvise(p, size, MADV_NOHUGEPAGE);
108 for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE)
111 for (i = 0; i < repeats; i++) {
113 if (ioctl(fd, cmd, &gup))
114 perror("ioctl"), exit(1);
116 printf("Time: get:%lld put:%lld us", gup.get_delta_usec,
118 if (gup.size != size)
119 printf(", truncated (size: %lld)", gup.size);