1 /*
2 * Copyright © 2011,2012,2014 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 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 *
27 */
28
29 /*
30 * Testcase: run a couple of big batches to force the eviction code.
31 */
32
33 #include "igt.h"
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <inttypes.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <sys/ioctl.h>
44 #include <sys/time.h>
45 #include "drm.h"
46
47 #include "eviction_common.c"
48
49 IGT_TEST_DESCRIPTION("Run a couple of big batches to force the eviction"
50 " code.");
51
52 #define HEIGHT 256
53 #define WIDTH 1024
54
55 static int
copy(int fd,uint32_t dst,uint32_t src,uint32_t * all_bo,int n_bo)56 copy(int fd, uint32_t dst, uint32_t src, uint32_t *all_bo, int n_bo)
57 {
58 uint32_t batch[12];
59 struct drm_i915_gem_relocation_entry reloc[2];
60 struct drm_i915_gem_exec_object2 *obj;
61 struct drm_i915_gem_execbuffer2 exec;
62 uint32_t handle;
63 int n, ret, i=0;
64
65 batch[i++] = (XY_SRC_COPY_BLT_CMD |
66 XY_SRC_COPY_BLT_WRITE_ALPHA |
67 XY_SRC_COPY_BLT_WRITE_RGB | 6);
68 if (intel_gen(intel_get_drm_devid(fd)) >= 8)
69 batch[i - 1] += 2;
70 batch[i++] = (3 << 24) | /* 32 bits */
71 (0xcc << 16) | /* copy ROP */
72 WIDTH*4;
73 batch[i++] = 0; /* dst x1,y1 */
74 batch[i++] = (HEIGHT << 16) | WIDTH; /* dst x2,y2 */
75 batch[i++] = 0; /* dst reloc */
76 if (intel_gen(intel_get_drm_devid(fd)) >= 8)
77 batch[i++] = 0; /* FIXME */
78 batch[i++] = 0; /* src x1,y1 */
79 batch[i++] = WIDTH*4;
80 batch[i++] = 0; /* src reloc */
81 if (intel_gen(intel_get_drm_devid(fd)) >= 8)
82 batch[i++] = 0; /* FIXME */
83 batch[i++] = MI_BATCH_BUFFER_END;
84 batch[i++] = MI_NOOP;
85
86 handle = gem_create(fd, 4096);
87 gem_write(fd, handle, 0, batch, sizeof(batch));
88
89 reloc[0].target_handle = dst;
90 reloc[0].delta = 0;
91 reloc[0].offset = 4 * sizeof(batch[0]);
92 reloc[0].presumed_offset = 0;
93 reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
94 reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
95
96 reloc[1].target_handle = src;
97 reloc[1].delta = 0;
98 reloc[1].offset = 7 * sizeof(batch[0]);
99 if (intel_gen(intel_get_drm_devid(fd)) >= 8)
100 reloc[1].offset += sizeof(batch[0]);
101 reloc[1].presumed_offset = 0;
102 reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
103 reloc[1].write_domain = 0;
104
105 obj = calloc(n_bo + 1, sizeof(*obj));
106 for (n = 0; n < n_bo; n++)
107 obj[n].handle = all_bo[n];
108 obj[n].handle = handle;
109 obj[n].relocation_count = 2;
110 obj[n].relocs_ptr = to_user_pointer(reloc);
111
112 memset(&exec, 0, sizeof(exec));
113 exec.buffers_ptr = to_user_pointer(obj);
114 exec.buffer_count = n_bo + 1;
115 if (HAS_BLT_RING(intel_get_drm_devid(fd)))
116 exec.flags |= I915_EXEC_BLT;
117 ret = __gem_execbuf(fd, &exec);
118
119 gem_close(fd, handle);
120 free(obj);
121
122 return ret;
123 }
124
clear(int fd,uint32_t handle,uint64_t size)125 static void clear(int fd, uint32_t handle, uint64_t size)
126 {
127 void *base = gem_mmap__cpu(fd, handle, 0, size, PROT_WRITE);
128 memset(base, 0, size);
129 munmap(base, size);
130 }
131
132 static struct igt_eviction_test_ops fault_ops = {
133 .create = gem_create,
134 .close = gem_close,
135 .copy = copy,
136 .clear = clear,
137 };
138
test_forking_evictions(int fd,uint64_t size,uint64_t count,unsigned flags)139 static void test_forking_evictions(int fd, uint64_t size, uint64_t count,
140 unsigned flags)
141 {
142 uint64_t trash_count;
143
144 trash_count = intel_get_total_ram_mb() * 11 / 10;
145 intel_require_memory(trash_count, size, CHECK_RAM | CHECK_SWAP);
146
147 forking_evictions(fd, &fault_ops, size, count, trash_count, flags);
148 }
149
test_mlocked_evictions(int fd,uint64_t size,uint64_t count)150 static void test_mlocked_evictions(int fd, uint64_t size, uint64_t count)
151 {
152 mlocked_evictions(fd, &fault_ops, size, count);
153 }
154
test_swapping_evictions(int fd,uint64_t size,uint64_t count)155 static void test_swapping_evictions(int fd, uint64_t size, uint64_t count)
156 {
157 int trash_count;
158
159 trash_count = intel_get_total_ram_mb() * 11 / 10;
160 intel_require_memory(trash_count, size, CHECK_RAM | CHECK_SWAP);
161
162 swapping_evictions(fd, &fault_ops, size, count, trash_count);
163 }
164
test_minor_evictions(int fd,uint64_t size,uint64_t count)165 static void test_minor_evictions(int fd, uint64_t size, uint64_t count)
166 {
167 minor_evictions(fd, &fault_ops, size, count);
168 }
169
test_major_evictions(int fd,uint64_t size,uint64_t count)170 static void test_major_evictions(int fd, uint64_t size, uint64_t count)
171 {
172 major_evictions(fd, &fault_ops, size, count);
173 }
174
175 #define MAX_32b ((1ull << 32) - 4096)
176
177 igt_main
178 {
179 uint64_t size, count;
180 int fd;
181
182 size = count = 0;
183 fd = -1;
184
185 igt_fixture {
186 fd = drm_open_driver(DRIVER_INTEL);
187 igt_require_gem(fd);
188
189 size = 1024 * 1024;
190 count = gem_aperture_size(fd);
191 if (count >> 32)
192 count = MAX_32b;
193 count = 3 * count / size / 4;
194
195 igt_fork_hang_detector(fd);
196 }
197
198 for (unsigned flags = 0; flags < ALL_FORKING_EVICTIONS + 1; flags++) {
199 igt_subtest_f("forked%s%s%s-%s",
200 flags & FORKING_EVICTIONS_SWAPPING ? "-swapping" : "",
201 flags & FORKING_EVICTIONS_DUP_DRMFD ? "-multifd" : "",
202 flags & FORKING_EVICTIONS_MEMORY_PRESSURE ?
203 "-mempressure" : "",
204 flags & FORKING_EVICTIONS_INTERRUPTIBLE ?
205 "interruptible" : "normal") {
206 test_forking_evictions(fd, size, count, flags);
207 }
208 }
209
210 igt_subtest("mlocked-normal")
211 test_mlocked_evictions(fd, size, count);
212
213 igt_subtest("swapping-normal")
214 test_swapping_evictions(fd, size, count);
215
216 igt_subtest("minor-normal")
217 test_minor_evictions(fd, size, count);
218
219 igt_subtest("major-normal") {
220 size = gem_aperture_size(fd);
221 if (size >> 32)
222 size = MAX_32b;
223 size = 3 * size / 4;
224 count = 4;
225 test_major_evictions(fd, size, count);
226 }
227
228 igt_fixture {
229 size = 1024 * 1024;
230 count = gem_aperture_size(fd);
231 if (count >> 32)
232 count = MAX_32b;
233 count = 3 * count / size / 4;
234 }
235
236 igt_fork_signal_helper();
237
238 igt_subtest("mlocked-interruptible")
239 test_mlocked_evictions(fd, size, count);
240
241 igt_subtest("swapping-interruptible")
242 test_swapping_evictions(fd, size, count);
243
244 igt_subtest("minor-interruptible")
245 test_minor_evictions(fd, size, count);
246
247 igt_subtest("major-interruptible") {
248 size = gem_aperture_size(fd);
249 if (size >> 32)
250 size = MAX_32b;
251 size = 3 * size / 4;
252 count = 4;
253 test_major_evictions(fd, size, count);
254 }
255
256 igt_fixture {
257 igt_stop_hang_detector();
258 igt_fork_hang_helper();
259
260 size = 1024 * 1024;
261 count = gem_aperture_size(fd);
262 if (count >> 32)
263 count = MAX_32b;
264 count = 3 * count / size / 4;
265 }
266
267 igt_subtest("mlocked-hang")
268 test_mlocked_evictions(fd, size, count);
269
270 igt_subtest("swapping-hang")
271 test_swapping_evictions(fd, size, count);
272
273 igt_subtest("minor-hang")
274 test_minor_evictions(fd, size, count);
275
276 igt_subtest("major-hang") {
277 size = gem_aperture_size(fd);
278 if (size >> 32)
279 size = MAX_32b;
280 size = 3 * size / 4;
281 count = 4;
282 test_major_evictions(fd, size, count);
283 }
284
285 igt_stop_signal_helper();
286
287 igt_fixture {
288 igt_stop_hang_helper();
289 close(fd);
290 }
291 }
292