• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BENCHMARK		_IOWR('g', 2, struct gup_benchmark)
19 
20 /* Similar to above, but use FOLL_PIN instead of FOLL_GET. */
21 #define PIN_FAST_BENCHMARK	_IOWR('g', 3, struct gup_benchmark)
22 #define PIN_BENCHMARK		_IOWR('g', 4, struct gup_benchmark)
23 #define PIN_LONGTERM_BENCHMARK	_IOWR('g', 5, struct gup_benchmark)
24 
25 /* Just the flags we need, copied from mm.h: */
26 #define FOLL_WRITE	0x01	/* check pte is writable */
27 
28 struct gup_benchmark {
29 	__u64 get_delta_usec;
30 	__u64 put_delta_usec;
31 	__u64 addr;
32 	__u64 size;
33 	__u32 nr_pages_per_call;
34 	__u32 flags;
35 	__u64 expansion[10];	/* For future use */
36 };
37 
main(int argc,char ** argv)38 int main(int argc, char **argv)
39 {
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";
45 	char *p;
46 
47 	while ((opt = getopt(argc, argv, "m:r:n:f:abtTLUuwSH")) != -1) {
48 		switch (opt) {
49 		case 'a':
50 			cmd = PIN_FAST_BENCHMARK;
51 			break;
52 		case 'b':
53 			cmd = PIN_BENCHMARK;
54 			break;
55 		case 'L':
56 			cmd = PIN_LONGTERM_BENCHMARK;
57 			break;
58 		case 'm':
59 			size = atoi(optarg) * MB;
60 			break;
61 		case 'r':
62 			repeats = atoi(optarg);
63 			break;
64 		case 'n':
65 			nr_pages = atoi(optarg);
66 			break;
67 		case 't':
68 			thp = 1;
69 			break;
70 		case 'T':
71 			thp = 0;
72 			break;
73 		case 'U':
74 			cmd = GUP_BENCHMARK;
75 			break;
76 		case 'u':
77 			cmd = GUP_FAST_BENCHMARK;
78 			break;
79 		case 'w':
80 			write = 1;
81 			break;
82 		case 'f':
83 			file = optarg;
84 			break;
85 		case 'S':
86 			flags &= ~MAP_PRIVATE;
87 			flags |= MAP_SHARED;
88 			break;
89 		case 'H':
90 			flags |= (MAP_HUGETLB | MAP_ANONYMOUS);
91 			break;
92 		default:
93 			return -1;
94 		}
95 	}
96 
97 	filed = open(file, O_RDWR|O_CREAT);
98 	if (filed < 0) {
99 		perror("open");
100 		exit(filed);
101 	}
102 
103 	gup.nr_pages_per_call = nr_pages;
104 	if (write)
105 		gup.flags |= FOLL_WRITE;
106 
107 	fd = open("/sys/kernel/debug/gup_benchmark", O_RDWR);
108 	if (fd == -1) {
109 		perror("open");
110 		exit(1);
111 	}
112 
113 	p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0);
114 	if (p == MAP_FAILED) {
115 		perror("mmap");
116 		exit(1);
117 	}
118 	gup.addr = (unsigned long)p;
119 
120 	if (thp == 1)
121 		madvise(p, size, MADV_HUGEPAGE);
122 	else if (thp == 0)
123 		madvise(p, size, MADV_NOHUGEPAGE);
124 
125 	for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE)
126 		p[0] = 0;
127 
128 	for (i = 0; i < repeats; i++) {
129 		gup.size = size;
130 		if (ioctl(fd, cmd, &gup)) {
131 			perror("ioctl");
132 			exit(1);
133 		}
134 
135 		printf("Time: get:%lld put:%lld us", gup.get_delta_usec,
136 			gup.put_delta_usec);
137 		if (gup.size != size)
138 			printf(", truncated (size: %lld)", gup.size);
139 		printf("\n");
140 	}
141 
142 	return 0;
143 }
144