1 /*
2  * Copyright © 2008 Intel Corporation
3  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Eric Anholt <eric@anholt.net>
26  *
27  */
28 
29 #include "config.h"
30 
31 #include <inttypes.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <err.h>
38 #include <assert.h>
39 #include <sys/ioctl.h>
40 #include <fcntl.h>
41 #include <sys/stat.h>
42 #include <sys/mman.h>
43 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM
44 #include <sys/sysinfo.h>
45 #elif defined(HAVE_SWAPCTL) /* Solaris */
46 #include <sys/swap.h>
47 #endif
48 #include <sys/resource.h>
49 
50 #include "intel_io.h"
51 #include "drmtest.h"
52 #include "igt_aux.h"
53 #include "igt_debugfs.h"
54 #include "igt_sysfs.h"
55 
56 /**
57  * intel_get_total_ram_mb:
58  *
59  * Returns:
60  * The total amount of system RAM available in MB.
61  */
62 uint64_t
intel_get_total_ram_mb(void)63 intel_get_total_ram_mb(void)
64 {
65 	uint64_t retval;
66 
67 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
68 	struct sysinfo sysinf;
69 
70 	igt_assert(sysinfo(&sysinf) == 0);
71 	retval = sysinf.totalram;
72 	retval *= sysinf.mem_unit;
73 #elif defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) /* Solaris */
74 	long pagesize, npages;
75 
76 	pagesize = sysconf(_SC_PAGESIZE);
77         npages = sysconf(_SC_PHYS_PAGES);
78 
79 	retval = (uint64_t) pagesize * npages;
80 #else
81 #error "Unknown how to get RAM size for this OS"
82 #endif
83 
84 	return retval / (1024*1024);
85 }
86 
get_meminfo(const char * info,const char * tag)87 static uint64_t get_meminfo(const char *info, const char *tag)
88 {
89 	const char *str;
90 	unsigned long val;
91 
92 	str = strstr(info, tag);
93 	if (str && sscanf(str + strlen(tag), " %lu", &val) == 1)
94 		return (uint64_t)val << 10;
95 
96 	igt_warn("Unrecognised /proc/meminfo field: '%s'\n", tag);
97 	return 0;
98 }
99 
100 /**
101  * intel_get_avail_ram_mb:
102  *
103  * Returns:
104  * The amount of unused system RAM available in MB.
105  */
106 uint64_t
intel_get_avail_ram_mb(void)107 intel_get_avail_ram_mb(void)
108 {
109 	uint64_t retval;
110 
111 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
112 	char *info;
113 	int fd;
114 
115 	fd = drm_open_driver(DRIVER_INTEL);
116 	intel_purge_vm_caches(fd);
117 	close(fd);
118 
119 	fd = open("/proc", O_RDONLY);
120 	info = igt_sysfs_get(fd, "meminfo");
121 	close(fd);
122 
123 	if (info) {
124 		retval  = get_meminfo(info, "MemAvailable:");
125 		retval += get_meminfo(info, "Buffers:");
126 		/*
127 		 * Include the file+swap cache as "available" for the test.
128 		 * We believe that we can revoke these pages back to their
129 		 * on disk counterpart, with no loss of functionality while
130 		 * the test runs using those pages for ourselves without the
131 		 * test itself being swapped to disk.
132 		 */
133 		retval += get_meminfo(info, "Cached:");
134 		retval += get_meminfo(info, "SwapCached:");
135 		free(info);
136 	} else {
137 		struct sysinfo sysinf;
138 
139 		igt_assert(sysinfo(&sysinf) == 0);
140 		retval = sysinf.freeram;
141 		retval += min(sysinf.freeswap, sysinf.bufferram);
142 		retval *= sysinf.mem_unit;
143 	}
144 #elif defined(_SC_PAGESIZE) && defined(_SC_AVPHYS_PAGES) /* Solaris */
145 	long pagesize, npages;
146 
147 	pagesize = sysconf(_SC_PAGESIZE);
148         npages = sysconf(_SC_AVPHYS_PAGES);
149 
150 	retval = (uint64_t) pagesize * npages;
151 #else
152 #error "Unknown how to get available RAM for this OS"
153 #endif
154 
155 	return retval / (1024*1024);
156 }
157 
158 /**
159  * intel_get_total_swap_mb:
160  *
161  * Returns:
162  * The total amount of swap space available in MB.
163  */
164 uint64_t
intel_get_total_swap_mb(void)165 intel_get_total_swap_mb(void)
166 {
167 	uint64_t retval;
168 
169 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
170 	struct sysinfo sysinf;
171 
172 	igt_assert(sysinfo(&sysinf) == 0);
173 	retval = sysinf.freeswap;
174 	retval *= sysinf.mem_unit;
175 #elif defined(HAVE_SWAPCTL) /* Solaris */
176 	long pagesize = sysconf(_SC_PAGESIZE);
177 	uint64_t totalpages = 0;
178 	swaptbl_t *swt;
179 	char *buf;
180 	int n, i;
181 
182 	if ((n = swapctl(SC_GETNSWP, NULL)) == -1) {
183 	    igt_warn("swapctl: GETNSWP");
184 	    return 0;
185 	}
186 	if (n == 0) {
187 	    /* no error, but no swap devices either */
188 	    return 0;
189 	}
190 
191 	swt = malloc(sizeof(struct swaptable) + (n * sizeof(swapent_t)));
192 	buf = malloc(n * MAXPATHLEN);
193 	if (!swt || !buf) {
194 	    igt_warn("malloc");
195 	} else {
196 	    swt->swt_n = n;
197 	    for (i = 0 ; i < n; i++) {
198 		swt->swt_ent[i].ste_path = buf + (i * MAXPATHLEN);
199 	    }
200 
201 	    if ((n = swapctl(SC_LIST, swt)) == -1) {
202 		igt_warn("swapctl: LIST");
203 	    } else {
204 		for (i = 0; i < swt->swt_n; i++) {
205 		    totalpages += swt->swt_ent[i].ste_pages;
206 		}
207 	    }
208 	}
209 	free(swt);
210 	free(buf);
211 
212 	retval = (uint64_t) pagesize * totalpages;
213 #else
214 #warning "Unknown how to get swap size for this OS"
215 	return 0;
216 #endif
217 
218 	return retval / (1024*1024);
219 }
220 
221 /**
222  * intel_get_total_pinnable_mem:
223  *
224  * Compute the amount of memory that we're able to safely lock.
225  * Note that in order to achieve this, we're attempting to repeatedly lock more
226  * and more memory, which is a time consuming process.
227  *
228  * Returns: Amount of memory that can be safely pinned, in bytes.
229  */
intel_get_total_pinnable_mem(size_t * total)230 void *intel_get_total_pinnable_mem(size_t *total)
231 {
232 	uint64_t *can_mlock, pin, avail;
233 
234 	pin = (intel_get_total_ram_mb() + 1) << 20;
235 	avail = (intel_get_avail_ram_mb() + 1) << 20;
236 
237 	can_mlock = mmap(NULL, pin, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
238 	igt_require(can_mlock != MAP_FAILED);
239 
240 	/*
241 	 * We can reasonably assume that we should be able to lock at
242 	 * least 3/4 of available RAM
243 	 */
244 	*can_mlock = (avail >> 1) + (avail >> 2);
245 	if (mlock(can_mlock, *can_mlock)) {
246 		munmap(can_mlock, pin);
247 		return MAP_FAILED;
248 	}
249 
250 	for (uint64_t inc = 1024 << 20; inc >= 4 << 10; inc >>= 2) {
251 		uint64_t locked = *can_mlock;
252 
253 		igt_debug("Testing mlock %'"PRIu64"B (%'"PRIu64"MiB) + %'"PRIu64"B\n",
254 			  locked, locked >> 20, inc);
255 
256 		igt_fork(child, 1) {
257 			uint64_t bytes = *can_mlock;
258 
259 			while (bytes <= pin) {
260 				if (mlock((void *)can_mlock + bytes, inc))
261 					break;
262 
263 				*can_mlock = bytes += inc;
264 				__sync_synchronize();
265 			}
266 		}
267 		__igt_waitchildren();
268 
269 		if (*can_mlock > locked + inc) { /* Weird bit of mm/ lore */
270 			*can_mlock -= inc;
271 			igt_debug("Claiming mlock %'"PRIu64"B (%'"PRIu64"MiB)\n",
272 				  *can_mlock, *can_mlock >> 20);
273 			igt_assert(!mlock((void *)can_mlock + locked,
274 					  *can_mlock - locked));
275 		}
276 	}
277 
278 	*total = pin;
279 	return can_mlock;
280 }
281 
max_open_files(void)282 static unsigned max_open_files(void)
283 {
284 	struct rlimit rlim;
285 
286 	if (getrlimit(RLIMIT_NOFILE, &rlim))
287 		rlim.rlim_cur = 64 << 10;
288 
289 	return rlim.rlim_cur;
290 }
291 
292 /**
293  * intel_require_files:
294  * @count: number of files that will be created
295  *
296  * Does the system support enough file descriptors for the test?
297  */
intel_require_files(uint64_t count)298 void intel_require_files(uint64_t count)
299 {
300 	igt_require_f(count < max_open_files(),
301 		      "Estimated that we need %'llu files, but the process maximum is only %'llu\n",
302 		      (long long)count, (long long)max_open_files());
303 }
304 
__intel_check_memory(uint64_t count,uint64_t size,unsigned mode,uint64_t * out_required,uint64_t * out_total)305 int __intel_check_memory(uint64_t count, uint64_t size, unsigned mode,
306 			 uint64_t *out_required, uint64_t *out_total)
307 {
308 /* rough estimate of how many bytes the kernel requires to track each object */
309 #define KERNEL_BO_OVERHEAD 512
310 	uint64_t required, total;
311 
312 	required = count;
313 	required *= size + KERNEL_BO_OVERHEAD;
314 	required = ALIGN(required, 4096);
315 
316 	igt_debug("Checking %'llu surfaces of size %'llu bytes (total %'llu) against %s%s\n",
317 		  (long long)count, (long long)size, (long long)required,
318 		  mode & (CHECK_RAM | CHECK_SWAP) ? "RAM" : "",
319 		  mode & CHECK_SWAP ? " + swap": "");
320 
321 	total = 0;
322 	if (mode & (CHECK_RAM | CHECK_SWAP))
323 		total += intel_get_avail_ram_mb();
324 	if (mode & CHECK_SWAP)
325 		total += intel_get_total_swap_mb();
326 	total *= 1024 * 1024;
327 
328 	if (out_required)
329 		*out_required = required;
330 
331 	if (out_total)
332 		*out_total = total;
333 
334 	if (count > vfs_file_max())
335 		return false;
336 
337 	return required < total;
338 }
339 
340 /**
341  * intel_require_memory:
342  * @count: number of surfaces that will be created
343  * @size: the size in bytes of each surface
344  * @mode: a bit field declaring whether the test will be run in RAM or in SWAP
345  *
346  * Computes the total amount of memory required to allocate @count surfaces,
347  * each of @size bytes, and includes an estimate for kernel overhead. It then
348  * queries the kernel for the available amount of memory on the system (either
349  * RAM and/or SWAP depending upon @mode) and determines whether there is
350  * sufficient to run the test.
351  *
352  * Most tests should check that there is enough RAM to hold their working set.
353  * The rare swap thrashing tests should check that there is enough RAM + SWAP
354  * for their tests. oom-killer tests should only run if this reports that
355  * there is not enough RAM + SWAP!
356  *
357  * If there is not enough RAM this function calls igt_skip with an appropriate
358  * message. It only ever returns if the requirement is fulfilled. This function
359  * also causes the test to be skipped automatically on simulation under the
360  * assumption that any test that needs to check for memory requirements is a
361  * thrashing test unsuitable for slow simulated systems.
362  */
intel_require_memory(uint64_t count,uint64_t size,unsigned mode)363 void intel_require_memory(uint64_t count, uint64_t size, unsigned mode)
364 {
365 	uint64_t required, total;
366 	bool sufficient_memory;
367 
368 	igt_skip_on_simulation();
369 
370 	sufficient_memory = __intel_check_memory(count, size, mode,
371 						 &required, &total);
372 	if (!sufficient_memory) {
373 		int dir = open("/proc", O_RDONLY);
374 		char *info;
375 
376 		info = igt_sysfs_get(dir, "meminfo");
377 		if (info) {
378 			igt_warn("Insufficient free memory; /proc/meminfo:\n%s",
379 				 info);
380 			free(info);
381 		}
382 
383 		info = igt_sysfs_get(dir, "slabinfo");
384 		if (info) {
385 			igt_warn("Insufficient free memory; /proc/slabinfo:\n%s",
386 				 info);
387 			free(info);
388 		}
389 
390 		close(dir);
391 	}
392 
393 	igt_require_f(sufficient_memory,
394 		      "Estimated that we need %'llu objects and %'llu MiB for the test, but only have %'llu MiB available (%s%s) and a maximum of %'llu objects\n",
395 		      (long long)count,
396 		      (long long)((required + ((1<<20) - 1)) >> 20),
397 		      (long long)(total >> 20),
398 		      mode & (CHECK_RAM | CHECK_SWAP) ? "RAM" : "",
399 		      mode & CHECK_SWAP ? " + swap": "",
400 		      (long long)vfs_file_max());
401 }
402 
intel_purge_vm_caches(int drm_fd)403 void intel_purge_vm_caches(int drm_fd)
404 {
405 	int fd;
406 
407 	fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
408 	if (fd >= 0) {
409 		/*
410 		 * BIT(2): Be quiet. Cannot be combined with other operations,
411 		 * the sysctl has a max value of 4.
412 		 */
413 		igt_ignore_warn(write(fd, "4\n", 2));
414 		close(fd);
415 	}
416 
417 	for (int loop = 0; loop < 2; loop++) {
418 		igt_drop_caches_set(drm_fd,
419 				    DROP_SHRINK_ALL | DROP_IDLE | DROP_FREED);
420 
421 		fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
422 		if (fd < 0)
423 			continue;
424 
425 		/*
426 		 * BIT(0): Drop page cache
427 		 * BIT(1): Drop slab cache
428 		 */
429 		igt_ignore_warn(write(fd, "3\n", 2));
430 		close(fd);
431 	}
432 
433 	errno = 0;
434 }
435 
436 /*
437  * When testing a port to a new platform, create a standalone test binary
438  * by running:
439  * cc -o porttest intel_drm.c -I.. -DSTANDALONE_TEST `pkg-config --cflags libdrm`
440  * and then running the resulting porttest program.
441  */
442 #ifdef STANDALONE_TEST
443 void *mmio;
444 
main(int argc,char ** argv)445 int main(int argc, char **argv)
446 {
447     igt_info("Total RAM:  %"PRIu64" Mb\n", intel_get_total_ram_mb());
448     igt_info("Total Swap: %"PRIu64" Mb\n", intel_get_total_swap_mb());
449 
450     return 0;
451 }
452 #endif /* STANDALONE_TEST */
453