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