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 
47 #define OBJECT_SIZE (1<<23)
48 
elapsed(const struct timespec * start,const struct timespec * end)49 static uint64_t elapsed(const struct timespec *start,
50                         const struct timespec *end)
51 {
52 	return 1000000000ULL*(end->tv_sec - start->tv_sec) + (end->tv_nsec - start->tv_nsec);
53 }
54 
main(int argc,char ** argv)55 int main(int argc, char **argv)
56 {
57 	int fd = drm_open_driver(DRIVER_INTEL);
58 	int domain = I915_GEM_DOMAIN_GTT;
59 	enum dir { READ, WRITE } dir = READ;
60 	void *buf = malloc(OBJECT_SIZE);
61 	uint32_t handle;
62 	int reps = 13;
63 	int c, size;
64 
65 	while ((c = getopt (argc, argv, "D:d:r:")) != -1) {
66 		switch (c) {
67 		case 'd':
68 			if (strcmp(optarg, "cpu") == 0)
69 				domain = I915_GEM_DOMAIN_CPU;
70 			else if (strcmp(optarg, "gtt") == 0)
71 				domain = I915_GEM_DOMAIN_GTT;
72 			break;
73 		case 'D':
74 			if (strcmp(optarg, "read") == 0)
75 				dir = READ;
76 			else if (strcmp(optarg, "write") == 0)
77 				dir = WRITE;
78 			else
79 				abort();
80 			break;
81 
82 		case 'r':
83 			reps = atoi(optarg);
84 			if (reps < 1)
85 				reps = 1;
86 			break;
87 
88 		default:
89 			break;
90 		}
91 	}
92 
93 	handle = gem_create(fd, OBJECT_SIZE);
94 	for (size = 1; size <= OBJECT_SIZE; size <<= 1) {
95 		igt_stats_t stats;
96 		int n;
97 
98 		igt_stats_init_with_size(&stats, reps);
99 
100 		for (n = 0; n < reps; n++) {
101 			struct timespec start, end;
102 
103 			gem_set_domain(fd, handle, domain, domain);
104 
105 			clock_gettime(CLOCK_MONOTONIC, &start);
106 			if (dir == READ)
107 				gem_read(fd, handle, 0, buf, size);
108 			else
109 				gem_write(fd, handle, 0, buf, size);
110 			clock_gettime(CLOCK_MONOTONIC, &end);
111 
112 			igt_stats_push(&stats, elapsed(&start, &end));
113 		}
114 
115 		printf("%7.3f\n", igt_stats_get_trimean(&stats)/1000);
116 		igt_stats_fini(&stats);
117 	}
118 
119 	return 0;
120 }
121