From patchwork Thu Jun 21 15:01:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Wilson X-Patchwork-Id: 10480001 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C0A3A604D3 for ; Thu, 21 Jun 2018 15:01:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B00512921D for ; Thu, 21 Jun 2018 15:01:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A3AFC2921E; Thu, 21 Jun 2018 15:01:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3B5E5291F0 for ; Thu, 21 Jun 2018 15:01:21 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1A3186E8DF; Thu, 21 Jun 2018 15:01:19 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from fireflyinternet.com (mail.fireflyinternet.com [109.228.58.192]) by gabe.freedesktop.org (Postfix) with ESMTPS id F26986E8C6; Thu, 21 Jun 2018 15:01:16 +0000 (UTC) X-Default-Received-SPF: pass (skip=forwardok (res=PASS)) x-ip-name=78.156.65.138; Received: from haswell.alporthouse.com (unverified [78.156.65.138]) by fireflyinternet.com (Firefly Internet (M1)) with ESMTP id 12118030-1500050 for multiple; Thu, 21 Jun 2018 16:01:04 +0100 Received: by haswell.alporthouse.com (sSMTP sendmail emulation); Thu, 21 Jun 2018 16:01:04 +0100 From: Chris Wilson To: igt-dev@lists.freedesktop.org Date: Thu, 21 Jun 2018 16:01:03 +0100 Message-Id: <20180621150103.14924-1-chris@chris-wilson.co.uk> X-Mailer: git-send-email 2.18.0.rc2 In-Reply-To: <20180621141928.3116-1-chris@chris-wilson.co.uk> References: <20180621141928.3116-1-chris@chris-wilson.co.uk> X-Originating-IP: 78.156.65.138 X-Country: code=GB country="United Kingdom" ip=78.156.65.138 Subject: [Intel-gfx] [PATCH i-g-t] lib: Report file cache as available system memory X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: intel-gfx@lists.freedesktop.org MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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;