xref: /aosp_15_r20/external/igt-gpu-tools/benchmarks/vgem_mmap.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
1 /*
2  * Copyright ©2016 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  */
24 
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <inttypes.h>
32 #include <errno.h>
33 #include <sys/stat.h>
34 #include <sys/ioctl.h>
35 #include <sys/time.h>
36 #include <time.h>
37 
38 #include "igt.h"
39 #include "igt_vgem.h"
40 
elapsed(const struct timespec * start,const struct timespec * end)41 static double elapsed(const struct timespec *start,
42 		      const struct timespec *end)
43 {
44 	return (end->tv_sec - start->tv_sec) + 1e-9*(end->tv_nsec - start->tv_nsec);
45 }
46 
main(int argc,char ** argv)47 int main(int argc, char **argv)
48 {
49 	enum dir {READ, WRITE, CLEAR, FAULT} dir = READ;
50 	struct timespec start, end;
51 	struct vgem_bo bo;
52 	void *buf;
53 	void *ptr, *src, *dst;
54 	int reps = 1;
55 	int loops;
56 	int vgem;
57 	int c;
58 
59 	while ((c = getopt (argc, argv, "d:r:")) != -1) {
60 		switch (c) {
61 		case 'd':
62 			if (strcmp(optarg, "read") == 0)
63 				dir = READ;
64 			else if (strcmp(optarg, "write") == 0)
65 				dir = WRITE;
66 			else if (strcmp(optarg, "clear") == 0)
67 				dir = CLEAR;
68 			else if (strcmp(optarg, "fault") == 0)
69 				dir = FAULT;
70 			else
71 				abort();
72 			break;
73 
74 		case 'r':
75 			reps = atoi(optarg);
76 			if (reps < 1)
77 				reps = 1;
78 			break;
79 
80 		default:
81 			break;
82 		}
83 	}
84 
85 	vgem = drm_open_driver(DRIVER_VGEM);
86 
87 	bo.width = 2024;
88 	bo.height = 2024;
89 	bo.bpp = 4;
90 	vgem_create(vgem, &bo);
91 	ptr = vgem_mmap(vgem, &bo, PROT_WRITE);
92 	buf = malloc(bo.size);
93 
94 	if (dir == READ) {
95 		src = ptr;
96 		dst = buf;
97 	} else {
98 		src = buf;
99 		dst = ptr;
100 	}
101 
102 	clock_gettime(CLOCK_MONOTONIC, &start);
103 	switch (dir) {
104 	case CLEAR:
105 	case FAULT:
106 		memset(dst, 0, bo.size);
107 		break;
108 	default:
109 		memcpy(dst, src, bo.size);
110 		break;
111 	}
112 	clock_gettime(CLOCK_MONOTONIC, &end);
113 
114 	loops = 2 / elapsed(&start, &end);
115 	while (reps--) {
116 		clock_gettime(CLOCK_MONOTONIC, &start);
117 		for (c = 0; c < loops; c++) {
118 			int page;
119 
120 			switch (dir) {
121 			case CLEAR:
122 				memset(dst, 0, bo.size);
123 				break;
124 			case FAULT:
125 				munmap(ptr, bo.size);
126 				ptr = vgem_mmap(vgem, &bo, PROT_WRITE);
127 				for (page = 0; page < bo.size; page += 4096) {
128 					uint32_t *x = (uint32_t *)ptr + page/4;
129 					__asm__ __volatile__("": : :"memory");
130 					page += *x; /* should be zero! */
131 				}
132 				break;
133 			default:
134 				memcpy(dst, src, bo.size);
135 				break;
136 			}
137 		}
138 		clock_gettime(CLOCK_MONOTONIC, &end);
139 		printf("%7.3f\n", bo.size / elapsed(&start, &end) * loops / (1024*1024));
140 	}
141 
142 	return 0;
143 }
144