1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include <sys/ioctl.h>
7 #include <sys/mman.h>
8 #include <sys/prctl.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11
12 #include <linux/types.h>
13
14 #define MB (1UL << 20)
15 #define PAGE_SIZE sysconf(_SC_PAGESIZE)
16
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)
20
21 struct gup_benchmark {
22 __u64 get_delta_usec;
23 __u64 put_delta_usec;
24 __u64 addr;
25 __u64 size;
26 __u32 nr_pages_per_call;
27 __u32 flags;
28 };
29
main(int argc,char ** argv)30 int main(int argc, char **argv)
31 {
32 struct gup_benchmark gup;
33 unsigned long size = 128 * MB;
34 int i, fd, filed, opt, nr_pages = 1, thp = -1, repeats = 1, write = 0;
35 int cmd = GUP_FAST_BENCHMARK, flags = MAP_PRIVATE;
36 char *file = "/dev/zero";
37 char *p;
38
39 while ((opt = getopt(argc, argv, "m:r:n:f:tTLUSH")) != -1) {
40 switch (opt) {
41 case 'm':
42 size = atoi(optarg) * MB;
43 break;
44 case 'r':
45 repeats = atoi(optarg);
46 break;
47 case 'n':
48 nr_pages = atoi(optarg);
49 break;
50 case 't':
51 thp = 1;
52 break;
53 case 'T':
54 thp = 0;
55 break;
56 case 'L':
57 cmd = GUP_LONGTERM_BENCHMARK;
58 break;
59 case 'U':
60 cmd = GUP_BENCHMARK;
61 break;
62 case 'w':
63 write = 1;
64 break;
65 case 'f':
66 file = optarg;
67 break;
68 case 'S':
69 flags &= ~MAP_PRIVATE;
70 flags |= MAP_SHARED;
71 break;
72 case 'H':
73 flags |= MAP_HUGETLB;
74 break;
75 default:
76 return -1;
77 }
78 }
79
80 filed = open(file, O_RDWR|O_CREAT);
81 if (filed < 0) {
82 perror("open");
83 exit(filed);
84 }
85
86 gup.nr_pages_per_call = nr_pages;
87 gup.flags = write;
88
89 fd = open("/sys/kernel/debug/gup_benchmark", O_RDWR);
90 if (fd == -1)
91 perror("open"), exit(1);
92
93 p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0);
94 if (p == MAP_FAILED)
95 perror("mmap"), exit(1);
96 gup.addr = (unsigned long)p;
97
98 if (thp == 1)
99 madvise(p, size, MADV_HUGEPAGE);
100 else if (thp == 0)
101 madvise(p, size, MADV_NOHUGEPAGE);
102
103 for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE)
104 p[0] = 0;
105
106 for (i = 0; i < repeats; i++) {
107 gup.size = size;
108 if (ioctl(fd, cmd, &gup))
109 perror("ioctl"), exit(1);
110
111 printf("Time: get:%lld put:%lld us", gup.get_delta_usec,
112 gup.put_delta_usec);
113 if (gup.size != size)
114 printf(", truncated (size: %lld)", gup.size);
115 printf("\n");
116 }
117
118 return 0;
119 }
120