xref: /aosp_15_r20/external/igt-gpu-tools/tests/i915/gem_exec_create.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
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 <[email protected]>
25  *
26  */
27 
28 #include "igt.h"
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.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/time.h>
40 #include <time.h>
41 #include "drm.h"
42 
43 #define LOCAL_I915_EXEC_NO_RELOC (1<<11)
44 #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
45 
46 #define LOCAL_I915_EXEC_BSD_SHIFT      (13)
47 #define LOCAL_I915_EXEC_BSD_MASK       (3 << LOCAL_I915_EXEC_BSD_SHIFT)
48 
49 #define ENGINE_FLAGS  (I915_EXEC_RING_MASK | LOCAL_I915_EXEC_BSD_MASK)
50 
elapsed(const struct timespec * start,const struct timespec * end)51 static double elapsed(const struct timespec *start, const struct timespec *end)
52 {
53 	return ((end->tv_sec - start->tv_sec) +
54 		(end->tv_nsec - start->tv_nsec)*1e-9);
55 }
56 
57 #define LEAK 0x1
58 
all(int fd,unsigned flags,int timeout,int ncpus)59 static void all(int fd, unsigned flags, int timeout, int ncpus)
60 {
61 	const uint32_t bbe = MI_BATCH_BUFFER_END;
62 	struct drm_i915_gem_execbuffer2 execbuf;
63 	struct drm_i915_gem_exec_object2 obj;
64 	unsigned engines[16];
65 	unsigned nengine;
66 	unsigned engine;
67 
68 	nengine = 0;
69 	for_each_physical_engine(fd, engine)
70 		engines[nengine++] = engine;
71 	igt_require(nengine);
72 
73 	memset(&obj, 0, sizeof(obj));
74 	obj.handle =  gem_create(fd, 4096);
75 	gem_write(fd, obj.handle, 0, &bbe, sizeof(bbe));
76 
77 	memset(&execbuf, 0, sizeof(execbuf));
78 	execbuf.buffers_ptr = to_user_pointer(&obj);
79 	execbuf.buffer_count = 1;
80 	execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
81 	execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
82 	if (__gem_execbuf(fd, &execbuf)) {
83 		execbuf.flags = 0;
84 		gem_execbuf(fd, &execbuf);
85 	}
86 	gem_sync(fd, obj.handle);
87 	gem_close(fd, obj.handle);
88 
89 	intel_detect_and_clear_missed_interrupts(fd);
90 	igt_fork(child, ncpus) {
91 		struct timespec start, now;
92 		unsigned long count;
93 		double time;
94 
95 		count = 0;
96 		clock_gettime(CLOCK_MONOTONIC, &start);
97 		do {
98 			for (int loop = 0; loop < 1024; loop++) {
99 				for (int n = 0; n < nengine; n++) {
100 					obj.handle =  gem_create(fd, 4096);
101 					gem_write(fd, obj.handle, 0, &bbe, sizeof(bbe));
102 					execbuf.flags &= ~ENGINE_FLAGS;
103 					execbuf.flags |= engines[n];
104 					gem_execbuf(fd, &execbuf);
105 					if (flags & LEAK)
106 						gem_madvise(fd, obj.handle, I915_MADV_DONTNEED);
107 					else
108 						gem_close(fd, obj.handle);
109 				}
110 			}
111 			count += nengine * 1024;
112 			clock_gettime(CLOCK_MONOTONIC, &now);
113 		} while (elapsed(&start, &now) < timeout); /* Hang detection ~120s */
114 		obj.handle =  gem_create(fd, 4096);
115 		gem_write(fd, obj.handle, 0, &bbe, sizeof(bbe));
116 		for (int n = 0; n < nengine; n++) {
117 			execbuf.flags &= ~ENGINE_FLAGS;
118 			execbuf.flags |= engines[n];
119 			gem_execbuf(fd, &execbuf);
120 		}
121 		gem_sync(fd, obj.handle);
122 		gem_close(fd, obj.handle);
123 		clock_gettime(CLOCK_MONOTONIC, &now);
124 
125 		time = elapsed(&start, &now) / count;
126 		igt_info("[%d] All (%d engines): %'lu cycles, average %.3fus per cycle\n",
127 			 child, nengine, count, 1e6*time);
128 	}
129 	igt_waitchildren();
130 	igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
131 }
132 
133 igt_main
134 {
135 	const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
136 	int device = -1;
137 
138 	igt_fixture {
139 		device = drm_open_driver(DRIVER_INTEL);
140 		igt_require_gem(device);
141 
142 		igt_fork_hang_detector(device);
143 	}
144 
145 	igt_subtest("basic")
146 		all(device, 0, 5, 1);
147 	igt_subtest("forked")
148 		all(device, 0, 150, ncpus);
149 
150 	igt_subtest("madvise")
151 		all(device, LEAK, 20, 1);
152 
153 
154 	igt_fixture {
155 		igt_stop_hang_detector();
156 		close(device);
157 	}
158 }
159