mm/gup_benchmark: use proper FOLL_WRITE flags instead of hard-coding "1"
[linux-2.6-microblaze.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 /* Just the flags we need, copied from mm.h: */
22 #define FOLL_WRITE      0x01    /* check pte is writable */
23
24 struct gup_benchmark {
25         __u64 get_delta_usec;
26         __u64 put_delta_usec;
27         __u64 addr;
28         __u64 size;
29         __u32 nr_pages_per_call;
30         __u32 flags;
31         __u64 expansion[10];    /* For future use */
32 };
33
34 int main(int argc, char **argv)
35 {
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";
41         char *p;
42
43         while ((opt = getopt(argc, argv, "m:r:n:f:tTLUwSH")) != -1) {
44                 switch (opt) {
45                 case 'm':
46                         size = atoi(optarg) * MB;
47                         break;
48                 case 'r':
49                         repeats = atoi(optarg);
50                         break;
51                 case 'n':
52                         nr_pages = atoi(optarg);
53                         break;
54                 case 't':
55                         thp = 1;
56                         break;
57                 case 'T':
58                         thp = 0;
59                         break;
60                 case 'L':
61                         cmd = GUP_LONGTERM_BENCHMARK;
62                         break;
63                 case 'U':
64                         cmd = GUP_BENCHMARK;
65                         break;
66                 case 'w':
67                         write = 1;
68                         break;
69                 case 'f':
70                         file = optarg;
71                         break;
72                 case 'S':
73                         flags &= ~MAP_PRIVATE;
74                         flags |= MAP_SHARED;
75                         break;
76                 case 'H':
77                         flags |= (MAP_HUGETLB | MAP_ANONYMOUS);
78                         break;
79                 default:
80                         return -1;
81                 }
82         }
83
84         filed = open(file, O_RDWR|O_CREAT);
85         if (filed < 0) {
86                 perror("open");
87                 exit(filed);
88         }
89
90         gup.nr_pages_per_call = nr_pages;
91         if (write)
92                 gup.flags |= FOLL_WRITE;
93
94         fd = open("/sys/kernel/debug/gup_benchmark", O_RDWR);
95         if (fd == -1)
96                 perror("open"), exit(1);
97
98         p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0);
99         if (p == MAP_FAILED)
100                 perror("mmap"), exit(1);
101         gup.addr = (unsigned long)p;
102
103         if (thp == 1)
104                 madvise(p, size, MADV_HUGEPAGE);
105         else if (thp == 0)
106                 madvise(p, size, MADV_NOHUGEPAGE);
107
108         for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE)
109                 p[0] = 0;
110
111         for (i = 0; i < repeats; i++) {
112                 gup.size = size;
113                 if (ioctl(fd, cmd, &gup))
114                         perror("ioctl"), exit(1);
115
116                 printf("Time: get:%lld put:%lld us", gup.get_delta_usec,
117                         gup.put_delta_usec);
118                 if (gup.size != size)
119                         printf(", truncated (size: %lld)", gup.size);
120                 printf("\n");
121         }
122
123         return 0;
124 }