mm/gup_benchmark.c: add additional pinning methods
[muen/linux.git] / tools / testing / selftests / vm / gup_benchmark.c
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
30 int main(int argc, char **argv)
31 {
32         struct gup_benchmark gup;
33         unsigned long size = 128 * MB;
34         int i, fd, opt, nr_pages = 1, thp = -1, repeats = 1, write = 0;
35         int cmd = GUP_FAST_BENCHMARK;
36         char *p;
37
38         while ((opt = getopt(argc, argv, "m:r:n:tTLU")) != -1) {
39                 switch (opt) {
40                 case 'm':
41                         size = atoi(optarg) * MB;
42                         break;
43                 case 'r':
44                         repeats = atoi(optarg);
45                         break;
46                 case 'n':
47                         nr_pages = atoi(optarg);
48                         break;
49                 case 't':
50                         thp = 1;
51                         break;
52                 case 'T':
53                         thp = 0;
54                         break;
55                 case 'L':
56                         cmd = GUP_LONGTERM_BENCHMARK;
57                         break;
58                 case 'U':
59                         cmd = GUP_BENCHMARK;
60                         break;
61                 case 'w':
62                         write = 1;
63                 default:
64                         return -1;
65                 }
66         }
67
68         gup.nr_pages_per_call = nr_pages;
69         gup.flags = write;
70
71         fd = open("/sys/kernel/debug/gup_benchmark", O_RDWR);
72         if (fd == -1)
73                 perror("open"), exit(1);
74
75         p = mmap(NULL, size, PROT_READ | PROT_WRITE,
76                         MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
77         if (p == MAP_FAILED)
78                 perror("mmap"), exit(1);
79         gup.addr = (unsigned long)p;
80
81         if (thp == 1)
82                 madvise(p, size, MADV_HUGEPAGE);
83         else if (thp == 0)
84                 madvise(p, size, MADV_NOHUGEPAGE);
85
86         for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE)
87                 p[0] = 0;
88
89         for (i = 0; i < repeats; i++) {
90                 gup.size = size;
91                 if (ioctl(fd, cmd, &gup))
92                         perror("ioctl"), exit(1);
93
94                 printf("Time: get:%lld put:%lld us", gup.get_delta_usec,
95                         gup.put_delta_usec);
96                 if (gup.size != size)
97                         printf(", truncated (size: %lld)", gup.size);
98                 printf("\n");
99         }
100
101         return 0;
102 }