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