kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / gem_gtt_speed.c
1 /*
2  * Copyright © 2010 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  *    Eric Anholt <eric@anholt.net>
25  *    Chris Wilson <chris@chris-wilson.co.uk>
26  *
27  */
28
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <sys/time.h>
40 #include "drm.h"
41 #include "ioctl_wrappers.h"
42 #include "drmtest.h"
43
44 #define OBJECT_SIZE 16384
45
46 static double elapsed(const struct timeval *start,
47                       const struct timeval *end,
48                       int loop)
49 {
50         return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec))/loop;
51 }
52
53 int main(int argc, char **argv)
54 {
55         struct timeval start, end;
56         uint8_t *buf;
57         uint32_t handle;
58         int size = OBJECT_SIZE;
59         int loop, i, tiling;
60         int fd;
61
62         igt_simple_init();
63
64         igt_skip_on_simulation();
65
66         if (argc > 1)
67                 size = atoi(argv[1]);
68         if (size == 0) {
69                 igt_warn("Invalid object size specified\n");
70                 return 1;
71         }
72
73         buf = malloc(size);
74         memset(buf, 0, size);
75         fd = drm_open_any();
76
77         handle = gem_create(fd, size);
78         igt_assert(handle);
79
80         for (tiling = I915_TILING_NONE; tiling <= I915_TILING_Y; tiling++) {
81                 if (tiling != I915_TILING_NONE) {
82                         igt_info("\nSetting tiling mode to %s\n",
83                                  tiling == I915_TILING_X ? "X" : "Y");
84                         gem_set_tiling(fd, handle, tiling, 512);
85                 }
86
87                 if (tiling == I915_TILING_NONE) {
88                         gem_set_domain(fd, handle,
89                                        I915_GEM_DOMAIN_CPU,
90                                        I915_GEM_DOMAIN_CPU);
91
92                         {
93                                 uint32_t *base = gem_mmap__cpu(fd, handle, size, PROT_READ | PROT_WRITE);
94                                 volatile uint32_t *ptr = base;
95                                 int x = 0;
96
97                                 for (i = 0; i < size/sizeof(*ptr); i++)
98                                         x += ptr[i];
99
100                                 /* force overtly clever gcc to actually compute x */
101                                 ptr[0] = x;
102
103                                 munmap(base, size);
104
105                                 /* mmap read */
106                                 gettimeofday(&start, NULL);
107                                 for (loop = 0; loop < 1000; loop++) {
108                                         base = gem_mmap__cpu(fd, handle, size, PROT_READ | PROT_WRITE);
109                                         ptr = base;
110                                         x = 0;
111
112                                         for (i = 0; i < size/sizeof(*ptr); i++)
113                                                 x += ptr[i];
114
115                                         /* force overtly clever gcc to actually compute x */
116                                         ptr[0] = x;
117
118                                         munmap(base, size);
119                                 }
120                                 gettimeofday(&end, NULL);
121                                 igt_info("Time to read %dk through a CPU map:           %7.3fµs\n",
122                                          size/1024, elapsed(&start, &end, loop));
123
124                                 /* mmap write */
125                                 gettimeofday(&start, NULL);
126                                 for (loop = 0; loop < 1000; loop++) {
127                                         base = gem_mmap__cpu(fd, handle, size, PROT_READ | PROT_WRITE);
128                                         ptr = base;
129
130                                         for (i = 0; i < size/sizeof(*ptr); i++)
131                                                 ptr[i] = i;
132
133                                         munmap(base, size);
134                                 }
135                                 gettimeofday(&end, NULL);
136                                 igt_info("Time to write %dk through a CPU map:          %7.3fµs\n",
137                                          size/1024, elapsed(&start, &end, loop));
138
139                                 gettimeofday(&start, NULL);
140                                 for (loop = 0; loop < 1000; loop++) {
141                                         base = gem_mmap__cpu(fd, handle, size, PROT_READ | PROT_WRITE);
142                                         memset(base, 0, size);
143                                         munmap(base, size);
144                                 }
145                                 gettimeofday(&end, NULL);
146                                 igt_info("Time to clear %dk through a CPU map:          %7.3fµs\n",
147                                          size/1024, elapsed(&start, &end, loop));
148
149                                 gettimeofday(&start, NULL);
150                                 base = gem_mmap__cpu(fd, handle, size, PROT_READ | PROT_WRITE);
151                                 for (loop = 0; loop < 1000; loop++)
152                                         memset(base, 0, size);
153                                 munmap(base, size);
154                                 gettimeofday(&end, NULL);
155                                 igt_info("Time to clear %dk through a cached CPU map:   %7.3fµs\n",
156                                          size/1024, elapsed(&start, &end, loop));
157                         }
158
159                         /* CPU pwrite */
160                         gettimeofday(&start, NULL);
161                         for (loop = 0; loop < 1000; loop++)
162                                 gem_write(fd, handle, 0, buf, size);
163                         gettimeofday(&end, NULL);
164                         igt_info("Time to pwrite %dk through the CPU:           %7.3fµs\n",
165                                  size/1024, elapsed(&start, &end, loop));
166
167                         /* CPU pread */
168                         gettimeofday(&start, NULL);
169                         for (loop = 0; loop < 1000; loop++)
170                                 gem_read(fd, handle, 0, buf, size);
171                         gettimeofday(&end, NULL);
172                         igt_info("Time to pread %dk through the CPU:            %7.3fµs\n",
173                                  size/1024, elapsed(&start, &end, loop));
174                 }
175
176                 /* prefault into gtt */
177                 {
178                         uint32_t *base = gem_mmap(fd, handle, size, PROT_READ | PROT_WRITE);
179                         volatile uint32_t *ptr = base;
180                         int x = 0;
181
182                         for (i = 0; i < size/sizeof(*ptr); i++)
183                                 x += ptr[i];
184
185                         /* force overtly clever gcc to actually compute x */
186                         ptr[0] = x;
187
188                         munmap(base, size);
189                 }
190                 /* mmap read */
191                 gettimeofday(&start, NULL);
192                 for (loop = 0; loop < 1000; loop++) {
193                         uint32_t *base = gem_mmap(fd, handle, size, PROT_READ | PROT_WRITE);
194                         volatile uint32_t *ptr = base;
195                         int x = 0;
196
197                         for (i = 0; i < size/sizeof(*ptr); i++)
198                                 x += ptr[i];
199
200                         /* force overtly clever gcc to actually compute x */
201                         ptr[0] = x;
202
203                         munmap(base, size);
204                 }
205                 gettimeofday(&end, NULL);
206                 igt_info("Time to read %dk through a GTT map:           %7.3fµs\n",
207                          size/1024, elapsed(&start, &end, loop));
208
209                 /* mmap write */
210                 gettimeofday(&start, NULL);
211                 for (loop = 0; loop < 1000; loop++) {
212                         uint32_t *base = gem_mmap(fd, handle, size, PROT_READ | PROT_WRITE);
213                         volatile uint32_t *ptr = base;
214
215                         for (i = 0; i < size/sizeof(*ptr); i++)
216                                 ptr[i] = i;
217
218                         munmap(base, size);
219                 }
220                 gettimeofday(&end, NULL);
221                 igt_info("Time to write %dk through a GTT map:          %7.3fµs\n",
222                          size/1024, elapsed(&start, &end, loop));
223
224                 /* mmap clear */
225                 gettimeofday(&start, NULL);
226                 for (loop = 0; loop < 1000; loop++) {
227                         uint32_t *base = gem_mmap(fd, handle, size, PROT_READ | PROT_WRITE);
228                         memset(base, 0, size);
229                         munmap(base, size);
230                 }
231                 gettimeofday(&end, NULL);
232                 igt_info("Time to clear %dk through a GTT map:          %7.3fµs\n",
233                          size/1024, elapsed(&start, &end, loop));
234
235                 gettimeofday(&start, NULL);{
236                         uint32_t *base = gem_mmap(fd, handle, size, PROT_READ | PROT_WRITE);
237                         for (loop = 0; loop < 1000; loop++)
238                                 memset(base, 0, size);
239                         munmap(base, size);
240                 } gettimeofday(&end, NULL);
241                 igt_info("Time to clear %dk through a cached GTT map:   %7.3fµs\n",
242                          size/1024, elapsed(&start, &end, loop));
243
244                 /* mmap read */
245                 gettimeofday(&start, NULL);
246                 for (loop = 0; loop < 1000; loop++) {
247                         uint32_t *base = gem_mmap(fd, handle, size, PROT_READ | PROT_WRITE);
248                         volatile uint32_t *ptr = base;
249                         int x = 0;
250
251                         for (i = 0; i < size/sizeof(*ptr); i++)
252                                 x += ptr[i];
253
254                         /* force overtly clever gcc to actually compute x */
255                         ptr[0] = x;
256
257                         munmap(base, size);
258                 }
259                 gettimeofday(&end, NULL);
260                 igt_info("Time to read %dk (again) through a GTT map:   %7.3fµs\n",
261                          size/1024, elapsed(&start, &end, loop));
262
263                 if (tiling == I915_TILING_NONE) {
264                         /* GTT pwrite */
265                         gettimeofday(&start, NULL);
266                         for (loop = 0; loop < 1000; loop++)
267                                 gem_write(fd, handle, 0, buf, size);
268                         gettimeofday(&end, NULL);
269                         igt_info("Time to pwrite %dk through the GTT:           %7.3fµs\n",
270                                  size/1024, elapsed(&start, &end, loop));
271
272                         /* GTT pread */
273                         gettimeofday(&start, NULL);
274                         for (loop = 0; loop < 1000; loop++)
275                                 gem_read(fd, handle, 0, buf, size);
276                         gettimeofday(&end, NULL);
277                         igt_info("Time to pread %dk through the GTT:            %7.3fµs\n",
278                                  size/1024, elapsed(&start, &end, loop));
279
280                         /* GTT pwrite, including clflush */
281                         gettimeofday(&start, NULL);
282                         for (loop = 0; loop < 1000; loop++) {
283                                 gem_write(fd, handle, 0, buf, size);
284                                 gem_sync(fd, handle);
285                         }
286                         gettimeofday(&end, NULL);
287                         igt_info("Time to pwrite %dk through the GTT (clflush): %7.3fµs\n",
288                                  size/1024, elapsed(&start, &end, loop));
289
290                         /* GTT pread, including clflush */
291                         gettimeofday(&start, NULL);
292                         for (loop = 0; loop < 1000; loop++) {
293                                 gem_sync(fd, handle);
294                                 gem_read(fd, handle, 0, buf, size);
295                         }
296                         gettimeofday(&end, NULL);
297                         igt_info("Time to pread %dk through the GTT (clflush):  %7.3fµs\n",
298                                  size/1024, elapsed(&start, &end, loop));
299
300                         /* partial writes */
301                         igt_info("Now partial writes.\n");
302                         size /= 4;
303
304                         /* partial GTT pwrite, including clflush */
305                         gettimeofday(&start, NULL);
306                         for (loop = 0; loop < 1000; loop++) {
307                                 gem_write(fd, handle, 0, buf, size);
308                                 gem_sync(fd, handle);
309                         }
310                         gettimeofday(&end, NULL);
311                         igt_info("Time to pwrite %dk through the GTT (clflush): %7.3fµs\n",
312                                size/1024, elapsed(&start, &end, loop));
313
314                         /* partial GTT pread, including clflush */
315                         gettimeofday(&start, NULL);
316                         for (loop = 0; loop < 1000; loop++) {
317                                 gem_sync(fd, handle);
318                                 gem_read(fd, handle, 0, buf, size);
319                         }
320                         gettimeofday(&end, NULL);
321                         igt_info("Time to pread %dk through the GTT (clflush):  %7.3fµs\n",
322                                size/1024, elapsed(&start, &end, loop));
323
324                         size *= 4;
325                 }
326
327         }
328
329         gem_close(fd, handle);
330         close(fd);
331
332         return 0;
333 }