lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / gem_pwrite.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 #define COPY_BLT_CMD            (2<<29|0x53<<22|0x6)
46 #define BLT_WRITE_ALPHA         (1<<21)
47 #define BLT_WRITE_RGB           (1<<20)
48 #define BLT_SRC_TILED           (1<<15)
49 #define BLT_DST_TILED           (1<<11)
50
51 static void do_gem_write(int fd, uint32_t handle, void *buf, int len, int loops)
52 {
53         while (loops--)
54                 gem_write(fd, handle, 0, buf, len);
55 }
56
57 static double elapsed(const struct timeval *start,
58                       const struct timeval *end,
59                       int loop)
60 {
61         return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec))/loop;
62 }
63
64 static const char *bytes_per_sec(char *buf, double v)
65 {
66         const char *order[] = {
67                 "",
68                 "KiB",
69                 "MiB",
70                 "GiB",
71                 "TiB",
72                 NULL,
73         }, **o = order;
74
75         while (v > 1000 && o[1]) {
76                 v /= 1000;
77                 o++;
78         }
79         sprintf(buf, "%.1f%s/s", v, *o);
80         return buf;
81 }
82
83
84 uint32_t *src, dst;
85 int fd;
86
87 int main(int argc, char **argv)
88 {
89         int object_size = 0;
90         uint32_t buf[20];
91         int count;
92         const struct {
93                 int level;
94                 const char *name;
95         } cache[] = {
96                 { 0, "uncached" },
97                 { 1, "snoop" },
98                 { 2, "display" },
99                 { -1 },
100         }, *c;
101
102         igt_skip_on_simulation();
103
104         igt_subtest_init(argc, argv);
105
106         if (argc > 1 && atoi(argv[1]))
107                 object_size = atoi(argv[1]);
108         if (object_size == 0)
109                 object_size = OBJECT_SIZE;
110         object_size = (object_size + 3) & -4;
111
112         igt_fixture {
113                 fd = drm_open_any();
114
115                 dst = gem_create(fd, object_size);
116                 src = malloc(object_size);
117         }
118
119         igt_subtest("normal") {
120                 for (count = 1; count <= 1<<17; count <<= 1) {
121                         struct timeval start, end;
122
123                         gettimeofday(&start, NULL);
124                         do_gem_write(fd, dst, src, object_size, count);
125                         gettimeofday(&end, NULL);
126                         igt_info("Time to pwrite %d bytes x %6d:        %7.3fµs, %s\n",
127                                  object_size, count,
128                                  elapsed(&start, &end, count),
129                                  bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
130                         fflush(stdout);
131                 }
132         }
133
134         for (c = cache; c->level != -1; c++) {
135                 igt_subtest(c->name) {
136                         gem_set_caching(fd, dst, c->level);
137
138                         for (count = 1; count <= 1<<17; count <<= 1) {
139                                 struct timeval start, end;
140
141                                 gettimeofday(&start, NULL);
142                                 do_gem_write(fd, dst, src, object_size, count);
143                                 gettimeofday(&end, NULL);
144                                 igt_info("Time to %s pwrite %d bytes x %6d:     %7.3fµs, %s\n",
145                                          c->name, object_size, count,
146                                          elapsed(&start, &end, count),
147                                          bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
148                                 fflush(stdout);
149                         }
150                 }
151         }
152
153         igt_fixture {
154                 free(src);
155                 gem_close(fd, dst);
156
157                 close(fd);
158         }
159
160         igt_exit();
161 }