10 #include <sys/types.h>
12 #include <linux/types.h>
14 #define MB (1UL << 20)
15 #define PAGE_SIZE sysconf(_SC_PAGESIZE)
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)
21 /* Similar to above, but use FOLL_PIN instead of FOLL_GET. */
22 #define PIN_FAST_BENCHMARK _IOWR('g', 4, struct gup_benchmark)
23 #define PIN_BENCHMARK _IOWR('g', 5, struct gup_benchmark)
25 /* Just the flags we need, copied from mm.h: */
26 #define FOLL_WRITE 0x01 /* check pte is writable */
28 struct gup_benchmark
{
33 __u32 nr_pages_per_call
;
35 __u64 expansion
[10]; /* For future use */
38 int main(int argc
, char **argv
)
40 struct gup_benchmark gup
;
41 unsigned long size
= 128 * MB
;
42 int i
, fd
, filed
, opt
, nr_pages
= 1, thp
= -1, repeats
= 1, write
= 0;
43 int cmd
= GUP_FAST_BENCHMARK
, flags
= MAP_PRIVATE
;
44 char *file
= "/dev/zero";
47 while ((opt
= getopt(argc
, argv
, "m:r:n:f:abtTLUuwSH")) != -1) {
50 cmd
= PIN_FAST_BENCHMARK
;
56 size
= atoi(optarg
) * MB
;
59 repeats
= atoi(optarg
);
62 nr_pages
= atoi(optarg
);
71 cmd
= GUP_LONGTERM_BENCHMARK
;
77 cmd
= GUP_FAST_BENCHMARK
;
86 flags
&= ~MAP_PRIVATE
;
90 flags
|= (MAP_HUGETLB
| MAP_ANONYMOUS
);
97 filed
= open(file
, O_RDWR
|O_CREAT
);
103 gup
.nr_pages_per_call
= nr_pages
;
105 gup
.flags
|= FOLL_WRITE
;
107 fd
= open("/sys/kernel/debug/gup_benchmark", O_RDWR
);
109 perror("open"), exit(1);
111 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, flags
, filed
, 0);
113 perror("mmap"), exit(1);
114 gup
.addr
= (unsigned long)p
;
117 madvise(p
, size
, MADV_HUGEPAGE
);
119 madvise(p
, size
, MADV_NOHUGEPAGE
);
121 for (; (unsigned long)p
< gup
.addr
+ size
; p
+= PAGE_SIZE
)
124 for (i
= 0; i
< repeats
; i
++) {
126 if (ioctl(fd
, cmd
, &gup
))
127 perror("ioctl"), exit(1);
129 printf("Time: get:%lld put:%lld us", gup
.get_delta_usec
,
131 if (gup
.size
!= size
)
132 printf(", truncated (size: %lld)", gup
.size
);