10 #include <sys/types.h>
13 #include <mm/gup_test.h>
14 #include "../kselftest.h"
18 #define MB (1UL << 20)
20 /* Just the flags we need, copied from mm.h: */
21 #define FOLL_WRITE 0x01 /* check pte is writable */
22 #define FOLL_TOUCH 0x02 /* mark page accessed */
24 #define GUP_TEST_FILE "/sys/kernel/debug/gup_test"
26 static unsigned long cmd = GUP_FAST_BENCHMARK;
27 static int gup_fd, repeats = 1;
28 static unsigned long size = 128 * MB;
29 /* Serialize prints */
30 static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
32 static char *cmd_to_str(unsigned long cmd)
35 case GUP_FAST_BENCHMARK:
36 return "GUP_FAST_BENCHMARK";
37 case PIN_FAST_BENCHMARK:
38 return "PIN_FAST_BENCHMARK";
39 case PIN_LONGTERM_BENCHMARK:
40 return "PIN_LONGTERM_BENCHMARK";
42 return "GUP_BASIC_TEST";
44 return "PIN_BASIC_TEST";
45 case DUMP_USER_PAGES_TEST:
46 return "DUMP_USER_PAGES_TEST";
48 return "Unknown command";
51 void *gup_thread(void *data)
53 struct gup_test gup = *(struct gup_test *)data;
56 /* Only report timing information on the *_BENCHMARK commands: */
57 if ((cmd == PIN_FAST_BENCHMARK) || (cmd == GUP_FAST_BENCHMARK) ||
58 (cmd == PIN_LONGTERM_BENCHMARK)) {
59 for (i = 0; i < repeats; i++) {
61 if (ioctl(gup_fd, cmd, &gup))
62 perror("ioctl"), exit(1);
64 pthread_mutex_lock(&print_mutex);
65 printf("%s: Time: get:%lld put:%lld us",
66 cmd_to_str(cmd), gup.get_delta_usec,
69 printf(", truncated (size: %lld)", gup.size);
71 pthread_mutex_unlock(&print_mutex);
75 if (ioctl(gup_fd, cmd, &gup)) {
80 pthread_mutex_lock(&print_mutex);
81 printf("%s: done\n", cmd_to_str(cmd));
83 printf("Truncated (size: %lld)\n", gup.size);
84 pthread_mutex_unlock(&print_mutex);
90 int main(int argc, char **argv)
92 struct gup_test gup = { 0 };
93 int filed, i, opt, nr_pages = 1, thp = -1, write = 1, nthreads = 1, ret;
94 int flags = MAP_PRIVATE, touch = 0;
95 char *file = "/dev/zero";
99 while ((opt = getopt(argc, argv, "m:r:n:F:f:abcj:tTLUuwWSHpz")) != -1) {
102 cmd = PIN_FAST_BENCHMARK;
105 cmd = PIN_BASIC_TEST;
108 cmd = PIN_LONGTERM_BENCHMARK;
111 cmd = DUMP_USER_PAGES_TEST;
113 * Dump page 0 (index 1). May be overridden later, by
114 * user's non-option arguments.
116 * .which_pages is zero-based, so that zero can mean "do
119 gup.which_pages[0] = 1;
122 /* works only with DUMP_USER_PAGES_TEST */
123 gup.test_flags |= GUP_TEST_FLAG_DUMP_PAGES_USE_PIN;
126 /* strtol, so you can pass flags in hex form */
127 gup.gup_flags = strtol(optarg, 0, 0);
130 nthreads = atoi(optarg);
133 size = atoi(optarg) * MB;
136 repeats = atoi(optarg);
139 nr_pages = atoi(optarg);
148 cmd = GUP_BASIC_TEST;
151 cmd = GUP_FAST_BENCHMARK;
163 flags &= ~MAP_PRIVATE;
167 flags |= (MAP_HUGETLB | MAP_ANONYMOUS);
170 /* fault pages in gup, do not fault in userland */
179 int extra_arg_count = 0;
183 * ./gup_test -c 0 1 0x1001
185 * ...to dump pages 0, 1, and 4097
188 while ((optind < argc) &&
189 (extra_arg_count < GUP_TEST_MAX_PAGES_TO_DUMP)) {
191 * Do the 1-based indexing here, so that the user can
192 * use normal 0-based indexing on the command line.
194 long page_index = strtol(argv[optind], 0, 0) + 1;
196 gup.which_pages[extra_arg_count] = page_index;
202 filed = open(file, O_RDWR|O_CREAT);
208 gup.nr_pages_per_call = nr_pages;
210 gup.gup_flags |= FOLL_WRITE;
212 gup_fd = open(GUP_TEST_FILE, O_RDWR);
217 printf("Please run this test as root\n");
220 if (opendir("/sys/kernel/debug") == NULL) {
221 printf("mount debugfs at /sys/kernel/debug\n");
224 printf("check if CONFIG_GUP_TEST is enabled in kernel config\n");
227 perror("failed to open " GUP_TEST_FILE);
233 p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0);
234 if (p == MAP_FAILED) {
238 gup.addr = (unsigned long)p;
241 madvise(p, size, MADV_HUGEPAGE);
243 madvise(p, size, MADV_NOHUGEPAGE);
246 * FOLL_TOUCH, in gup_test, is used as an either/or case: either
247 * fault pages in from the kernel via FOLL_TOUCH, or fault them
248 * in here, from user space. This allows comparison of performance
249 * between those two cases.
252 gup.gup_flags |= FOLL_TOUCH;
254 for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE)
258 tid = malloc(sizeof(pthread_t) * nthreads);
260 for (i = 0; i < nthreads; i++) {
261 ret = pthread_create(&tid[i], NULL, gup_thread, &gup);
264 for (i = 0; i < nthreads; i++) {
265 ret = pthread_join(tid[i], NULL);