kms_rotation_crc: Allow the sprite test to run even without universal planes
[platform/upstream/intel-gpu-tools.git] / tests / gem_pread.c
1 /*
2  * Copyright © 2011 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 "drm.h"
40 #include "ioctl_wrappers.h"
41 #include "drmtest.h"
42
43 #define OBJECT_SIZE 16384
44
45 static void do_gem_read(int fd, uint32_t handle, void *buf, int len, int loops)
46 {
47         while (loops--)
48                 gem_read(fd, handle, 0, buf, len);
49 }
50
51 static double elapsed(const struct timeval *start,
52                       const struct timeval *end,
53                       int loop)
54 {
55         return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec))/loop;
56 }
57
58 static const char *bytes_per_sec(char *buf, double v)
59 {
60         const char *order[] = {
61                 "",
62                 "KiB",
63                 "MiB",
64                 "GiB",
65                 "TiB",
66                 NULL,
67         }, **o = order;
68
69         while (v > 1000 && o[1]) {
70                 v /= 1000;
71                 o++;
72         }
73         sprintf(buf, "%.1f%s/s", v, *o);
74         return buf;
75 }
76
77
78 uint32_t *src, dst;
79 int fd, count;
80
81 int main(int argc, char **argv)
82 {
83         int object_size = 0;
84         uint32_t buf[20];
85         const struct {
86                 int level;
87                 const char *name;
88         } cache[] = {
89                 { 0, "uncached" },
90                 { 1, "snoop" },
91                 { 2, "display" },
92                 { -1 },
93         }, *c;
94
95         igt_subtest_init(argc, argv);
96         igt_skip_on_simulation();
97
98         if (argc > 1 && atoi(argv[1]))
99                 object_size = atoi(argv[1]);
100         if (object_size == 0)
101                 object_size = OBJECT_SIZE;
102         object_size = (object_size + 3) & -4;
103
104         igt_fixture {
105                 fd = drm_open_any();
106
107                 dst = gem_create(fd, object_size);
108                 src = malloc(object_size);
109         }
110
111         igt_subtest("normal") {
112                 for (count = 1; count <= 1<<17; count <<= 1) {
113                         struct timeval start, end;
114
115                         gettimeofday(&start, NULL);
116                         do_gem_read(fd, dst, src, object_size, count);
117                         gettimeofday(&end, NULL);
118                         igt_info("Time to pread %d bytes x %6d: %7.3fµs, %s\n",
119                                  object_size, count,
120                                  elapsed(&start, &end, count),
121                                  bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
122                         fflush(stdout);
123                 }
124         }
125
126         for (c = cache; c->level != -1; c++) {
127                 igt_subtest(c->name) {
128                         gem_set_caching(fd, dst, c->level);
129
130                         for (count = 1; count <= 1<<17; count <<= 1) {
131                                 struct timeval start, end;
132
133                                 gettimeofday(&start, NULL);
134                                 do_gem_read(fd, dst, src, object_size, count);
135                                 gettimeofday(&end, NULL);
136                                 igt_info("Time to %s pread %d bytes x %6d:      %7.3fµs, %s\n",
137                                          c->name, object_size, count,
138                                          elapsed(&start, &end, count),
139                                          bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
140                                 fflush(stdout);
141                         }
142                 }
143         }
144
145         igt_fixture {
146                 free(src);
147                 gem_close(fd, dst);
148
149                 close(fd);
150         }
151
152         igt_exit();
153 }