File: msm_shrink.c

package info (click to toggle)
intel-gpu-tools 2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 63,360 kB
  • sloc: xml: 781,458; ansic: 360,567; python: 8,336; yacc: 2,781; perl: 1,196; sh: 1,177; lex: 487; asm: 227; lisp: 35; makefile: 30
file content (312 lines) | stat: -rw-r--r-- 8,543 bytes parent folder | download | duplicates (2)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright © 2022 Google, Inc.
 * Copyright © 2016 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 <fcntl.h>
#include <sys/mman.h>

#include "igt.h"
#include "igt_msm.h"
#include "igt_os.h"
#include "igt_sysfs.h"

static void leak(uint64_t alloc)
{
	char *ptr;

	ptr = mmap(NULL, alloc, PROT_READ | PROT_WRITE,
		   MAP_ANON | MAP_PRIVATE | MAP_POPULATE,
		   -1, 0);
	if (ptr == MAP_FAILED)
		return;

	while (alloc > 4096) {
		alloc -= 4096;
		ptr[alloc] = 0;
	}
}

static void madvise_dontneed(struct msm_bo *bo)
{
	struct drm_msm_gem_madvise req = {
		.handle = bo->handle,
		.madv = MSM_MADV_DONTNEED,
	};
	do_ioctl(bo->dev->fd, DRM_IOCTL_MSM_GEM_MADVISE, &req);
}

static struct msm_cmd *cmd_copy_gpu(struct msm_pipe *pipe, unsigned num_bos, struct msm_bo **bos)
{
	struct msm_cmd *cmd = igt_msm_cmd_new(pipe, 0x1000);

	assert((num_bos % 2) == 0);

	for (unsigned i = 0; i < (num_bos / 2); i++) {
		struct msm_bo *dst = bos[2*i];
		struct msm_bo *src = bos[2*i+1];

		unsigned dwords = min(0x2000u, dst->size / 4);

		msm_cmd_pkt7(cmd, CP_MEMCPY, 5);
		msm_cmd_emit(cmd, dwords);          /* DWORDS */
		msm_cmd_bo  (cmd, src, 0);          /* SRC_LO/HI */
		msm_cmd_bo  (cmd, dst, 0);          /* DST_LO/HI */
		msm_cmd_pkt7(cmd, CP_WAIT_MEM_WRITES, 0);
		msm_cmd_pkt7(cmd, CP_WAIT_FOR_IDLE, 0);
		msm_cmd_pkt7(cmd, CP_WAIT_FOR_ME, 0);
	}

	return cmd;
}

static void *map_dmabuf(struct msm_bo *bo)
{
	int fd, ret;
	void *ptr;

	ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC | DRM_RDWR, &fd);
	igt_assert_eq(ret, 0);

	ptr = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	igt_assert(ptr != MAP_FAILED);

	close(fd);

	return ptr;
}

struct test {
	const char *name;
	struct msm_cmd *(*cmd)(struct msm_pipe *pipe, unsigned num_bos, struct msm_bo **bos);
	void *(*map)(struct msm_bo *bo);
} tests[] = {
	{ "copy-gpu", cmd_copy_gpu },
	{ "copy-mmap", cmd_copy_gpu, igt_msm_bo_map },
	{ "copy-mmap-dmabuf", cmd_copy_gpu, map_dmabuf },
	{},
};

enum testmode {
	SANITY_CHECK	= BIT(0),
	SINGLE_THREAD	= BIT(1),
	MADVISE		= BIT(2),
	OOM		= BIT(3),
};

static const struct mode {
	const char *suffix;
	unsigned flags;
} modes[] = {
	{ "-sanitycheck", SANITY_CHECK },
/* Disable by default to keep test runtime down:
	{ "-singlethread", SINGLE_THREAD },
 */
	{ "", 0 },
	{ "-madvise", MADVISE },
	{ "-oom", OOM },
	{ NULL },
};

static void do_test(int num_submits, uint64_t alloc_size_kb, int num_bos,
		    unsigned timeout, bool do_madvise, const struct test *test)
{
	struct msm_device *dev = igt_msm_dev_open();
	struct msm_pipe *pipe = igt_msm_pipe_open(dev, 0);
	struct msm_bo *bo[num_submits][num_bos];
	struct msm_cmd *cmd[num_submits];
	void *map[num_submits][num_bos];
	int fence_fd = -1;

	/* Allocate the buffer objects and prepare the cmds: */
	for (int i = 0; i < num_submits; i++) {
		for (int j = 0; j < num_bos; j++) {
			bo[i][j] = igt_msm_bo_new(dev, alloc_size_kb * 1024, MSM_BO_WC);
		}
		cmd[i] = test->cmd(pipe, num_bos, bo[i]);
	}

	/* Prepare the CPU maps, if necessary: */
	if (test->map) {
		for (int i = 0; i < num_submits; i++) {
			for (int j = 0; j < num_bos; j++) {
				map[i][j] = test->map(bo[i][j]);
				memset(map[i][j], 0xde, bo[i][j]->size);
			}
		}
	}

	igt_until_timeout(timeout) {
		for (int i = 0; i < num_submits / 2; i++) {
			if (fence_fd > 0)
				close(fence_fd);
			fence_fd = igt_msm_cmd_submit(cmd[i]);
		}

		igt_wait_and_close(fence_fd);
		fence_fd = -1;

		if (test->map) {
			for (int i = 0; i < num_submits; i++) {
				for (int j = 0; j < num_bos; j++) {
					memset(map[i][j], 0xde, bo[i][j]->size);
				}
			}
		}

		for (int i = num_submits / 2; i < num_submits; i++) {
			if (fence_fd > 0)
				close(fence_fd);
			fence_fd = igt_msm_cmd_submit(cmd[i]);
		}
		igt_wait_and_close(fence_fd);
		fence_fd = -1;
	}

	if (do_madvise) {
		for (int i = 0; i < num_submits; i++) {
			if (fence_fd > 0)
				close(fence_fd);
			fence_fd = igt_msm_cmd_submit(cmd[i]);
			for (int j = 0; j < num_bos; j++) {
				madvise_dontneed(bo[i][j]);
			}
		}
		igt_wait_and_close(fence_fd);
		fence_fd = -1;
	}
}

static void run_test(int nchildren, uint64_t alloc_size_mb, unsigned num_bos,
		     const struct test *test, unsigned flags)
{
	const int timeout = (test->map) || (flags & (SANITY_CHECK | MADVISE)) ? 1 : 10;
	bool madvise = !!(flags & MADVISE);
	uint64_t alloc_size_kb;

	/* We are trying to use more GEM buffers that will fit in
	 * memory, but less than 2x avail RAM.  Split across at
	 * least two submits so we aren't getting into a scenario
	 * where all the children are trying to pin all the memory
	 * at the same time and get into a situation where no one
	 * can make forward progress.
	 */
	unsigned num_submits = 8;

	if (flags & SANITY_CHECK)
		nchildren = 1;

	alloc_size_kb = DIV_ROUND_UP(alloc_size_mb * 1024, num_bos * num_submits);

	if (flags & SINGLE_THREAD) {
		num_submits *= nchildren;
		nchildren = 1;
	}

	igt_info("%s, %d submits, %d processes, and %d x %'"PRIu64"KiB bos per submit for total size of %'"PRIu64"KiB\n",
		 test->name, num_submits, nchildren, num_bos, alloc_size_kb,
		 num_bos * num_submits * nchildren * alloc_size_kb);

	/* Background load */
	if (flags & OOM) {
		igt_fork(child, nchildren) {
			for (int pass = 0; pass < nchildren; pass++)
				leak(alloc_size_kb / 1024);
		}
	}

	/* Exercise major ioctls */
	igt_fork(child, nchildren) {
		do_test(num_submits, alloc_size_kb, num_bos, timeout, madvise, test);
	}
	igt_waitchildren();
}

static const unsigned num_bos[] = { 8, 32 };

igt_main
{
	struct msm_device *dev = NULL;
	uint64_t alloc_size_mb = 0;
	int num_processes = 0;

	igt_fixture {
		int params, ncpus;
		uint64_t mem_size;
		uint64_t swap_size;

		/* Make sure we are running on the right hw: */
		dev = igt_msm_dev_open();

		igt_require(dev->gen >= 6);

		/* Ensure that eviction is enabled: */
		params = igt_params_open(dev->fd);
		igt_sysfs_set(params, "enable_eviction", "1");
		igt_sysfs_set(params, "address_space_size", "0x400000000");
		close(params);

		/* Figure out # of processes and allocation size: */
		ncpus = sysconf(_SC_NPROCESSORS_ONLN);
		mem_size = igt_get_total_ram_mb();
		swap_size = igt_get_total_swap_mb();

		igt_require(swap_size > 0);

		/*
		 * Spawn enough processes to use all memory, but each only
		 * uses a fraction of the available per-cpu memory.
		 * Individually the processes would be ok, but en masse
		 * we expect the shrinker to start purging objects,
		 * and possibly fail.
		 *
		 * Note, consider only a fraction of avail swap when
		 * calculating target size, as we have no good way to
		 * determine if it is zram swap (which will consume an
		 * increasing portion of RAM as it is filled)
		 */
		mem_size += swap_size / 4;
		alloc_size_mb = (mem_size + ncpus - 1) / ncpus / 8;
		num_processes = ncpus + (mem_size / alloc_size_mb);

		igt_info("Using %d processes and %'"PRIu64"MiB per process for total size of %'"PRIu64"MiB\n",
			 num_processes, alloc_size_mb, num_processes * alloc_size_mb);

		igt_require_memory(num_processes, alloc_size_mb,
				   CHECK_SWAP | CHECK_RAM);
	}

	for(const struct test *t = tests; t->name; t++) {
		for(const struct mode *m = modes; m->suffix; m++) {
			for (int i = 0; i < ARRAY_SIZE(num_bos); i++) {
				igt_subtest_f("%s%s-%u", t->name, m->suffix, num_bos[i]) {
					run_test(num_processes, alloc_size_mb,
						 num_bos[i], t, m->flags);
				}
			}
		}
	}

	igt_fixture {
		igt_msm_dev_close(dev);
	}
}