WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / vm / gup_test.c
blob6c6336dd3b7fbf48c394e5585ae47708e47de963
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/ioctl.h>
6 #include <sys/mman.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include "../../../../mm/gup_test.h"
11 #define MB (1UL << 20)
12 #define PAGE_SIZE sysconf(_SC_PAGESIZE)
14 /* Just the flags we need, copied from mm.h: */
15 #define FOLL_WRITE 0x01 /* check pte is writable */
17 static char *cmd_to_str(unsigned long cmd)
19 switch (cmd) {
20 case GUP_FAST_BENCHMARK:
21 return "GUP_FAST_BENCHMARK";
22 case PIN_FAST_BENCHMARK:
23 return "PIN_FAST_BENCHMARK";
24 case PIN_LONGTERM_BENCHMARK:
25 return "PIN_LONGTERM_BENCHMARK";
26 case GUP_BASIC_TEST:
27 return "GUP_BASIC_TEST";
28 case PIN_BASIC_TEST:
29 return "PIN_BASIC_TEST";
30 case DUMP_USER_PAGES_TEST:
31 return "DUMP_USER_PAGES_TEST";
33 return "Unknown command";
36 int main(int argc, char **argv)
38 struct gup_test gup = { 0 };
39 unsigned long size = 128 * MB;
40 int i, fd, filed, opt, nr_pages = 1, thp = -1, repeats = 1, write = 0;
41 unsigned long cmd = GUP_FAST_BENCHMARK;
42 int flags = MAP_PRIVATE;
43 char *file = "/dev/zero";
44 char *p;
46 while ((opt = getopt(argc, argv, "m:r:n:F:f:abctTLUuwSH")) != -1) {
47 switch (opt) {
48 case 'a':
49 cmd = PIN_FAST_BENCHMARK;
50 break;
51 case 'b':
52 cmd = PIN_BASIC_TEST;
53 break;
54 case 'L':
55 cmd = PIN_LONGTERM_BENCHMARK;
56 break;
57 case 'c':
58 cmd = DUMP_USER_PAGES_TEST;
60 * Dump page 0 (index 1). May be overridden later, by
61 * user's non-option arguments.
63 * .which_pages is zero-based, so that zero can mean "do
64 * nothing".
66 gup.which_pages[0] = 1;
67 break;
68 case 'F':
69 /* strtol, so you can pass flags in hex form */
70 gup.flags = strtol(optarg, 0, 0);
71 break;
72 case 'm':
73 size = atoi(optarg) * MB;
74 break;
75 case 'r':
76 repeats = atoi(optarg);
77 break;
78 case 'n':
79 nr_pages = atoi(optarg);
80 break;
81 case 't':
82 thp = 1;
83 break;
84 case 'T':
85 thp = 0;
86 break;
87 case 'U':
88 cmd = GUP_BASIC_TEST;
89 break;
90 case 'u':
91 cmd = GUP_FAST_BENCHMARK;
92 break;
93 case 'w':
94 write = 1;
95 break;
96 case 'f':
97 file = optarg;
98 break;
99 case 'S':
100 flags &= ~MAP_PRIVATE;
101 flags |= MAP_SHARED;
102 break;
103 case 'H':
104 flags |= (MAP_HUGETLB | MAP_ANONYMOUS);
105 break;
106 default:
107 return -1;
111 if (optind < argc) {
112 int extra_arg_count = 0;
114 * For example:
116 * ./gup_test -c 0 1 0x1001
118 * ...to dump pages 0, 1, and 4097
121 while ((optind < argc) &&
122 (extra_arg_count < GUP_TEST_MAX_PAGES_TO_DUMP)) {
124 * Do the 1-based indexing here, so that the user can
125 * use normal 0-based indexing on the command line.
127 long page_index = strtol(argv[optind], 0, 0) + 1;
129 gup.which_pages[extra_arg_count] = page_index;
130 extra_arg_count++;
131 optind++;
135 filed = open(file, O_RDWR|O_CREAT);
136 if (filed < 0) {
137 perror("open");
138 exit(filed);
141 gup.nr_pages_per_call = nr_pages;
142 if (write)
143 gup.flags |= FOLL_WRITE;
145 fd = open("/sys/kernel/debug/gup_test", O_RDWR);
146 if (fd == -1) {
147 perror("open");
148 exit(1);
151 p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0);
152 if (p == MAP_FAILED) {
153 perror("mmap");
154 exit(1);
156 gup.addr = (unsigned long)p;
158 if (thp == 1)
159 madvise(p, size, MADV_HUGEPAGE);
160 else if (thp == 0)
161 madvise(p, size, MADV_NOHUGEPAGE);
163 for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE)
164 p[0] = 0;
166 /* Only report timing information on the *_BENCHMARK commands: */
167 if ((cmd == PIN_FAST_BENCHMARK) || (cmd == GUP_FAST_BENCHMARK) ||
168 (cmd == PIN_LONGTERM_BENCHMARK)) {
169 for (i = 0; i < repeats; i++) {
170 gup.size = size;
171 if (ioctl(fd, cmd, &gup))
172 perror("ioctl"), exit(1);
174 printf("%s: Time: get:%lld put:%lld us",
175 cmd_to_str(cmd), gup.get_delta_usec,
176 gup.put_delta_usec);
177 if (gup.size != size)
178 printf(", truncated (size: %lld)", gup.size);
179 printf("\n");
181 } else {
182 gup.size = size;
183 if (ioctl(fd, cmd, &gup)) {
184 perror("ioctl");
185 exit(1);
188 printf("%s: done\n", cmd_to_str(cmd));
189 if (gup.size != size)
190 printf("Truncated (size: %lld)\n", gup.size);
193 return 0;