xref: /aosp_15_r20/external/igt-gpu-tools/tests/i915/gem_exec_reuse.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
1 /*
2  * Copyright © 2017 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 #include <limits.h>
25 #include <sys/resource.h>
26 
27 #include "igt.h"
28 #include "igt_aux.h"
29 
30 IGT_TEST_DESCRIPTION("Inspect scaling with large number of reused objects");
31 
32 struct noop {
33 	struct drm_i915_gem_exec_object2 *obj;
34 	uint32_t batch;
35 	uint32_t *handles;
36 	unsigned int nhandles;
37 	unsigned int max_age;
38 	int fd;
39 };
40 
noop(struct noop * n,unsigned ring,unsigned ctx,unsigned int count,unsigned int offset)41 static void noop(struct noop *n,
42 		 unsigned ring, unsigned ctx,
43 		 unsigned int count, unsigned int offset)
44 {
45 	struct drm_i915_gem_execbuffer2 execbuf;
46 	unsigned int i;
47 
48 	for (i = 0; i < count; i++)
49 		n->obj[i].handle = n->handles[(i + offset) & (n->nhandles-1)];
50 	n->obj[i].handle = n->batch;
51 
52 	memset(&execbuf, 0, sizeof(execbuf));
53 	execbuf.buffers_ptr = to_user_pointer(n->obj);
54 	execbuf.buffer_count = count + 1;
55 	execbuf.flags = ring | 1 << 12;
56 	execbuf.rsvd1 = ctx;
57 	gem_execbuf(n->fd, &execbuf);
58 }
59 
max_open_files(void)60 static uint64_t max_open_files(void)
61 {
62 	struct rlimit rlim;
63 
64 	if (getrlimit(RLIMIT_NOFILE, &rlim))
65 		rlim.rlim_cur = 64 << 10;
66 
67 	igt_info("Process limit for file descriptors is %lu\n",
68 		 (long)rlim.rlim_cur);
69 	return rlim.rlim_cur;
70 }
71 
max_nfd(void)72 static unsigned int max_nfd(void)
73 {
74 	uint64_t vfs = vfs_file_max();
75 	uint64_t fd = max_open_files();
76 	uint64_t min = fd < vfs ? fd : vfs;
77 	if (min > INT_MAX)
78 		min = INT_MAX;
79 	return min;
80 }
81 
82 igt_main
83 {
84 	struct noop no;
85 	unsigned engines[16];
86 	unsigned nengine;
87 	unsigned n;
88 
89 	igt_fixture {
90 		uint64_t gtt_size, max;
91 		uint32_t bbe = MI_BATCH_BUFFER_END;
92 		unsigned engine;
93 
94 		igt_allow_unlimited_files();
95 
96 		no.fd = drm_open_driver(DRIVER_INTEL);
97 		igt_require_gem(no.fd);
98 
99 		igt_fork_hang_detector(no.fd);
100 
101 		gtt_size = (gem_aperture_size(no.fd) / 2) >> 12;
102 		if (gtt_size > INT_MAX / sizeof(*no.handles))
103 			gtt_size = INT_MAX / sizeof(*no.handles);
104 
105 		max = max_nfd() - 16;
106 		if (max < gtt_size)
107 			gtt_size = max;
108 
109 		no.nhandles = 1 << (igt_fls(gtt_size) - 1);
110 		intel_require_memory(no.nhandles, 4096, CHECK_RAM);
111 
112 		no.max_age = no.nhandles / 2;
113 
114 		no.handles = malloc(sizeof(*no.handles) * no.nhandles);
115 		for (n = 0; n < no.nhandles; n++)
116 			no.handles[n] = gem_create(no.fd, 4096);
117 
118 		no.obj = malloc(sizeof(struct drm_i915_gem_exec_object2) * (no.max_age + 1));
119 
120 		nengine = 0;
121 		for_each_engine(no.fd, engine)
122 			if (engine)
123 				engines[nengine++] = engine;
124 		igt_require(nengine);
125 
126 		no.batch = gem_create(no.fd, 4096);
127 		gem_write(no.fd, no.batch, 0, &bbe, sizeof(bbe));
128 	}
129 
130 	igt_subtest_f("single") {
131 		unsigned int timeout = 5;
132 		unsigned long age = 0;
133 
134 		igt_until_timeout(timeout)
135 			for (n = 0; n < nengine; n++)
136 				noop(&no, engines[n], 0, 0, age++);
137 		gem_sync(no.fd, no.batch);
138 		igt_info("Completed %lu cycles\n", age);
139 	}
140 
141 	igt_subtest_f("baggage") {
142 		unsigned int timeout = 5;
143 		unsigned long age = 0;
144 
145 		igt_until_timeout(timeout)
146 			for (n = 0; n < nengine; n++)
147 				noop(&no, engines[n], 0,
148 				     no.max_age, age++);
149 		gem_sync(no.fd, no.batch);
150 		igt_info("Completed %lu cycles\n", age);
151 	}
152 
153 	igt_subtest_f("contexts") {
154 		unsigned int timeout = 5;
155 		unsigned long ctx_age = 0;
156 		unsigned long obj_age = 0;
157 		const unsigned int ncontexts = 1024;
158 		uint32_t contexts[ncontexts];
159 
160 		gem_require_contexts(no.fd);
161 
162 		for (n = 0; n < ncontexts; n++)
163 			contexts[n] = gem_context_create(no.fd);
164 
igt_until_timeout(timeout)165 		igt_until_timeout(timeout) {
166 			for (n = 0; n < nengine; n++) {
167 				noop(&no, engines[n],
168 				     contexts[ctx_age % ncontexts],
169 				     no.max_age, obj_age);
170 				obj_age++;
171 			}
172 			ctx_age++;
173 		}
174 		gem_sync(no.fd, no.batch);
175 		igt_info("Completed %lu cycles across %lu context switches\n",
176 			 obj_age, ctx_age);
177 
178 		for (n = 0; n < ncontexts; n++)
179 			gem_context_destroy(no.fd, contexts[n]);
180 	}
181 
182 	igt_fixture
183 		igt_stop_hang_detector();
184 }
185