summaryrefslogtreecommitdiff
path: root/tests/i915/gem_exec_reuse.c
blob: 559047188fbd1872ea2646da9174f75f5ff63731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright © 2017 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <limits.h>
#include <sys/resource.h>

#include "igt.h"

IGT_TEST_DESCRIPTION("Inspect scaling with large number of reused objects");

struct noop {
	struct drm_i915_gem_exec_object2 *obj;
	uint32_t batch;
	uint32_t *handles;
	unsigned int nhandles;
	unsigned int max_age;
	int fd;
};

static void noop(struct noop *n,
		 unsigned ring, unsigned ctx,
		 unsigned int count, unsigned int offset)
{
	struct drm_i915_gem_execbuffer2 execbuf;
	unsigned int i;

	for (i = 0; i < count; i++)
		n->obj[i].handle = n->handles[(i + offset) & (n->nhandles-1)];
	n->obj[i].handle = n->batch;

	memset(&execbuf, 0, sizeof(execbuf));
	execbuf.buffers_ptr = to_user_pointer(n->obj);
	execbuf.buffer_count = count + 1;
	execbuf.flags = ring | 1 << 12;
	execbuf.rsvd1 = ctx;
	gem_execbuf(n->fd, &execbuf);
}

static bool allow_unlimited_files(void)
{
	struct rlimit rlim;
	unsigned nofile_rlim = 1024*1024;

	FILE *file = fopen("/proc/sys/fs/file-max", "r");
	if (file) {
		igt_assert(fscanf(file, "%u", &nofile_rlim) == 1);
		fclose(file);
	}

	if (getrlimit(RLIMIT_NOFILE, &rlim))
		return false;

	rlim.rlim_cur = rlim.rlim_max;
	if (setrlimit(RLIMIT_NOFILE, &rlim))
		return false;

	rlim.rlim_cur = nofile_rlim;
	rlim.rlim_max = nofile_rlim;
	return setrlimit(RLIMIT_NOFILE, &rlim) == 0;
}

static uint64_t vfs_file_max(void)
{
	long long unsigned max = 80000;
	FILE *file = fopen("/proc/sys/fs/file-max", "r");
	if (file) {
		igt_assert(fscanf(file, "%llu", &max) == 1);
		fclose(file);
	}

	igt_info("System limit for open files is %llu\n", max);
	return max;
}

static uint64_t max_open_files(void)
{
	struct rlimit rlim;

	if (getrlimit(RLIMIT_NOFILE, &rlim))
		rlim.rlim_cur = 64 << 10;

	igt_info("Process limit for file descriptors is %lu\n",
		 (long)rlim.rlim_cur);
	return rlim.rlim_cur;
}

static unsigned int max_nfd(void)
{
	uint64_t vfs = vfs_file_max();
	uint64_t fd = max_open_files();
	uint64_t min = fd < vfs ? fd : vfs;
	if (min > INT_MAX)
		min = INT_MAX;
	return min;
}

igt_main
{
	struct noop no;
	unsigned engines[16];
	unsigned nengine;
	unsigned n;

	igt_fixture {
		uint64_t gtt_size, max;
		uint32_t bbe = MI_BATCH_BUFFER_END;
		unsigned engine;

		allow_unlimited_files();

		no.fd = drm_open_driver(DRIVER_INTEL);
		igt_require_gem(no.fd);

		igt_fork_hang_detector(no.fd);

		gtt_size = (gem_aperture_size(no.fd) / 2) >> 12;
		if (gtt_size > INT_MAX / sizeof(*no.handles))
			gtt_size = INT_MAX / sizeof(*no.handles);

		max = max_nfd() - 16;
		if (max < gtt_size)
			gtt_size = max;

		no.nhandles = 1 << (igt_fls(gtt_size) - 1);
		intel_require_memory(no.nhandles, 4096, CHECK_RAM);

		no.max_age = no.nhandles / 2;

		no.handles = malloc(sizeof(*no.handles) * no.nhandles);
		for (n = 0; n < no.nhandles; n++)
			no.handles[n] = gem_create(no.fd, 4096);

		no.obj = malloc(sizeof(struct drm_i915_gem_exec_object2) * (no.max_age + 1));

		nengine = 0;
		for_each_engine(no.fd, engine)
			if (engine)
				engines[nengine++] = engine;
		igt_require(nengine);

		no.batch = gem_create(no.fd, 4096);
		gem_write(no.fd, no.batch, 0, &bbe, sizeof(bbe));
	}

	igt_subtest_f("single") {
		unsigned int timeout = 5;
		unsigned long age = 0;

		igt_until_timeout(timeout)
			for (n = 0; n < nengine; n++)
				noop(&no, engines[n], 0, 0, age++);
		gem_sync(no.fd, no.batch);
		igt_info("Completed %lu cycles\n", age);
	}

	igt_subtest_f("baggage") {
		unsigned int timeout = 5;
		unsigned long age = 0;

		igt_until_timeout(timeout)
			for (n = 0; n < nengine; n++)
				noop(&no, engines[n], 0,
				     no.max_age, age++);
		gem_sync(no.fd, no.batch);
		igt_info("Completed %lu cycles\n", age);
	}

	igt_subtest_f("contexts") {
		unsigned int timeout = 5;
		unsigned long ctx_age = 0;
		unsigned long obj_age = 0;
		const unsigned int ncontexts = 1024;
		uint32_t contexts[ncontexts];

		gem_require_contexts(no.fd);

		for (n = 0; n < ncontexts; n++)
			contexts[n] = gem_context_create(no.fd);

		igt_until_timeout(timeout) {
			for (n = 0; n < nengine; n++) {
				noop(&no, engines[n],
				     contexts[ctx_age % ncontexts],
				     no.max_age, obj_age);
				obj_age++;
			}
			ctx_age++;
		}
		gem_sync(no.fd, no.batch);
		igt_info("Completed %lu cycles across %lu context switches\n",
			 obj_age, ctx_age);

		for (n = 0; n < ncontexts; n++)
			gem_context_destroy(no.fd, contexts[n]);
	}

	igt_fixture
		igt_stop_hang_detector();
}