From: Chris Wilson <chris@chris-wilson.co.uk> To: igt-dev@lists.freedesktop.org Cc: intel-gfx@lists.freedesktop.org Subject: [PATCH i-g-t] lib: Report file cache as available system memory Date: Thu, 21 Jun 2018 16:01:03 +0100 [thread overview] Message-ID: <20180621150103.14924-1-chris@chris-wilson.co.uk> (raw) In-Reply-To: <20180621141928.3116-1-chris@chris-wilson.co.uk> 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. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> --- lib/intel_os.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/intel_os.c b/lib/intel_os.c index 885ffdcec..5bd18fc52 100644 --- a/lib/intel_os.c +++ b/lib/intel_os.c @@ -84,6 +84,18 @@ 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; + + return 0; +} + /** * intel_get_avail_ram_mb: * @@ -96,17 +108,31 @@ 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: "); + 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; -- 2.18.0.rc2 _______________________________________________ Intel-gfx mailing list Intel-gfx@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/intel-gfx
WARNING: multiple messages have this Message-ID (diff)
From: Chris Wilson <chris@chris-wilson.co.uk> To: igt-dev@lists.freedesktop.org Cc: intel-gfx@lists.freedesktop.org Subject: [igt-dev] [PATCH i-g-t] lib: Report file cache as available system memory Date: Thu, 21 Jun 2018 16:01:03 +0100 [thread overview] Message-ID: <20180621150103.14924-1-chris@chris-wilson.co.uk> (raw) In-Reply-To: <20180621141928.3116-1-chris@chris-wilson.co.uk> 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. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> --- lib/intel_os.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/intel_os.c b/lib/intel_os.c index 885ffdcec..5bd18fc52 100644 --- a/lib/intel_os.c +++ b/lib/intel_os.c @@ -84,6 +84,18 @@ 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; + + return 0; +} + /** * intel_get_avail_ram_mb: * @@ -96,17 +108,31 @@ 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: "); + 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; -- 2.18.0.rc2 _______________________________________________ igt-dev mailing list igt-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/igt-dev
next prev parent reply other threads:[~2018-06-21 15:01 UTC|newest] Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top 2018-06-21 14:19 [PATCH i-g-t] lib: Report file cache as available system memory Chris Wilson 2018-06-21 14:19 ` [igt-dev] " Chris Wilson 2018-06-21 14:43 ` [igt-dev] ✓ Fi.CI.BAT: success for " Patchwork 2018-06-21 15:01 ` Chris Wilson [this message] 2018-06-21 15:01 ` [igt-dev] [PATCH i-g-t] " Chris Wilson 2018-06-21 15:23 ` [igt-dev] ✓ Fi.CI.BAT: success for lib: Report file cache as available system memory (rev2) Patchwork 2018-06-21 20:06 ` [igt-dev] ✓ Fi.CI.IGT: success for lib: Report file cache as available system memory Patchwork 2018-06-21 21:36 ` [igt-dev] ✓ Fi.CI.IGT: success for lib: Report file cache as available system memory (rev2) Patchwork
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20180621150103.14924-1-chris@chris-wilson.co.uk \ --to=chris@chris-wilson.co.uk \ --cc=igt-dev@lists.freedesktop.org \ --cc=intel-gfx@lists.freedesktop.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.