1 /*
2  * Copyright © 2011-2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Chris Wilson <chris@chris-wilson.co.uk>
25  *
26  */
27 
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include <sys/time.h>
39 #include <time.h>
40 
41 #include "drm.h"
42 #include "ioctl_wrappers.h"
43 #include "drmtest.h"
44 #include "igt_aux.h"
45 #include "igt_stats.h"
46 #include "intel_reg.h"
47 
48 #define OBJECT_SIZE (1<<23)
49 
50 #define LOCAL_I915_EXEC_NO_RELOC (1<<11)
51 #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
52 
elapsed(const struct timespec * start,const struct timespec * end)53 static double elapsed(const struct timespec *start,
54                         const struct timespec *end)
55 {
56 	return (end->tv_sec - start->tv_sec) + 1e-9*(end->tv_nsec - start->tv_nsec);
57 }
58 
make_busy(int fd,uint32_t handle)59 static void make_busy(int fd, uint32_t handle)
60 {
61 	struct drm_i915_gem_execbuffer2 execbuf;
62 	struct drm_i915_gem_exec_object2 gem_exec;
63 
64 	const uint32_t buf[] = {MI_BATCH_BUFFER_END};
65 	gem_write(fd, handle, 0, buf, sizeof(buf));
66 
67 	memset(&gem_exec, 0, sizeof(gem_exec));
68 	gem_exec.handle = handle;
69 
70 	memset(&execbuf, 0, sizeof(execbuf));
71 	execbuf.buffers_ptr = (uintptr_t)&gem_exec;
72 	execbuf.buffer_count = 1;
73 	execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
74 	execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
75 	if (__gem_execbuf(fd, &execbuf)) {
76 		execbuf.flags = 0;
77 		gem_execbuf(fd, &execbuf);
78 	}
79 }
80 
main(int argc,char ** argv)81 int main(int argc, char **argv)
82 {
83 	int fd = drm_open_driver(DRIVER_INTEL);
84 	int size = 0;
85 	int busy = 0;
86 	int reps = 13;
87 	int ncpus = 1;
88 	int c, n, s;
89 
90 	while ((c = getopt (argc, argv, "bs:r:f")) != -1) {
91 		switch (c) {
92 		case 's':
93 			size = atoi(optarg);
94 			break;
95 
96 		case 'r':
97 			reps = atoi(optarg);
98 			if (reps < 1)
99 				reps = 1;
100 			break;
101 
102 		case 'f':
103 			ncpus = sysconf(_SC_NPROCESSORS_ONLN);
104 			break;
105 
106 		case 'b':
107 			busy = true;
108 			break;
109 
110 		default:
111 			break;
112 		}
113 	}
114 
115 	if (size == 0) {
116 		for (s = 4096; s <=  OBJECT_SIZE; s <<= 1) {
117 			igt_stats_t stats;
118 
119 			igt_stats_init_with_size(&stats, reps);
120 			for (n = 0; n < reps; n++) {
121 				struct timespec start, end;
122 				uint64_t count = 0;
123 
124 				clock_gettime(CLOCK_MONOTONIC, &start);
125 				do {
126 					for (c = 0; c < 1000; c++) {
127 						uint32_t handle;
128 
129 						handle = gem_create(fd, s);
130 						gem_set_domain(fd, handle,
131 							       I915_GEM_DOMAIN_GTT,
132 							       I915_GEM_DOMAIN_GTT);
133 						if (busy)
134 							make_busy(fd, handle);
135 						gem_close(fd, handle);
136 					}
137 					count += c;
138 					clock_gettime(CLOCK_MONOTONIC, &end);
139 				} while (end.tv_sec - start.tv_sec < 2);
140 
141 				igt_stats_push_float(&stats, count / elapsed(&start, &end));
142 			}
143 			printf("%f\n", igt_stats_get_trimean(&stats));
144 			igt_stats_fini(&stats);
145 		}
146 	} else {
147 		double *shared;
148 
149 		shared = mmap(0, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
150 		for (n = 0; n < reps; n++) {
151 			memset(shared, 0, 4096);
152 
153 			igt_fork(child, ncpus) {
154 				struct timespec start, end;
155 				uint64_t count = 0;
156 
157 				clock_gettime(CLOCK_MONOTONIC, &start);
158 				do {
159 					for (c = 0; c < 1000; c++) {
160 						uint32_t handle;
161 
162 						handle = gem_create(fd, size);
163 						gem_set_domain(fd, handle,
164 								I915_GEM_DOMAIN_GTT,
165 								I915_GEM_DOMAIN_GTT);
166 						if (busy)
167 							make_busy(fd, handle);
168 						gem_close(fd, handle);
169 					}
170 					count += c;
171 					clock_gettime(CLOCK_MONOTONIC, &end);
172 				} while (end.tv_sec - start.tv_sec < 2);
173 
174 				shared[child] = count / elapsed(&start, &end);
175 			}
176 			igt_waitchildren();
177 
178 			for (int child = 0; child < ncpus; child++)
179 				shared[ncpus] += shared[child];
180 
181 			printf("%7.3f\n", shared[ncpus]);
182 		}
183 	}
184 
185 	return 0;
186 }
187