summaryrefslogtreecommitdiff
path: root/lib/intel_os.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2018-06-21 15:14:57 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2018-07-03 11:03:54 +0100
commit199220052af977598033d3810ffb4cc32d377522 (patch)
tree2b6f7954898a440f73bff9724ee0c9f52725ea8e /lib/intel_os.c
parent840d12e2f050b784552197403d6575a57b6e896d (diff)
lib: Report file cache as available system memory
sysinfo() doesn't include all reclaimable memory. In particular it excludes the majority of global_node_page_state(NR_FILE_PAGES), reclaimable pages that are a copy of on-disk files It seems the only way to obtain this counter is by parsing /proc/meminfo. For comparison, check vm_enough_memory() which includes NR_FILE_PAGES as available (sadly there's no way to call vm_enough_memory() directly either!) v2: Pay attention to what one writes. v3: Trim off redundant space, and warn if the requested tags do not match the layout of /proc/meminfo. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Diffstat (limited to 'lib/intel_os.c')
-rw-r--r--lib/intel_os.c44
1 files changed, 39 insertions, 5 deletions
diff --git a/lib/intel_os.c b/lib/intel_os.c
index 885ffdce..29a27272 100644
--- a/lib/intel_os.c
+++ b/lib/intel_os.c
@@ -84,6 +84,19 @@ intel_get_total_ram_mb(void)
return retval / (1024*1024);
}
+static uint64_t get_meminfo(const char *info, const char *tag)
+{
+ const char *str;
+ unsigned long val;
+
+ str = strstr(info, tag);
+ if (str && sscanf(str + strlen(tag), " %lu", &val) == 1)
+ return (uint64_t)val << 10;
+
+ igt_warn("Unrecognised /proc/meminfo field: '%s'\n", tag);
+ return 0;
+}
+
/**
* intel_get_avail_ram_mb:
*
@@ -96,17 +109,38 @@ intel_get_avail_ram_mb(void)
uint64_t retval;
#ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
- struct sysinfo sysinf;
+ char *info;
int fd;
fd = drm_open_driver(DRIVER_INTEL);
intel_purge_vm_caches(fd);
close(fd);
- igt_assert(sysinfo(&sysinf) == 0);
- retval = sysinf.freeram;
- retval += min(sysinf.freeswap, sysinf.bufferram);
- retval *= sysinf.mem_unit;
+ fd = open("/proc", O_RDONLY);
+ info = igt_sysfs_get(fd, "meminfo");
+ close(fd);
+
+ if (info) {
+ retval = get_meminfo(info, "MemAvailable:");
+ retval += get_meminfo(info, "Buffers:");
+ /*
+ * Include the file+swap cache as "available" for the test.
+ * We believe that we can revoke these pages back to their
+ * on disk counterpart, with no loss of functionality while
+ * the test runs using those pages for ourselves without the
+ * test itself being swapped to disk.
+ */
+ retval += get_meminfo(info, "Cached:");
+ retval += get_meminfo(info, "SwapCached:");
+ free(info);
+ } else {
+ struct sysinfo sysinf;
+
+ igt_assert(sysinfo(&sysinf) == 0);
+ retval = sysinf.freeram;
+ retval += min(sysinf.freeswap, sysinf.bufferram);
+ retval *= sysinf.mem_unit;
+ }
#elif defined(_SC_PAGESIZE) && defined(_SC_AVPHYS_PAGES) /* Solaris */
long pagesize, npages;