From: Jonathan Gray Date: Thu, 5 Dec 2019 14:01:29 +0000 (+1100) Subject: util/os_misc: add os_get_available_system_memory() X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=033dcb2978c6d95d42da944b8147228d239d32f4;p=mesa.git util/os_misc: add os_get_available_system_memory() Add os_get_available_system_memory() derived from src/intel/vulkan/anv_device.c get_available_system_memory() Fixes: b80930a6fea ("anv: add support for VK_EXT_memory_budget") Signed-off-by: Jonathan Gray Reviewed-by: Jason Ekstrand Part-of: --- diff --git a/src/util/os_misc.c b/src/util/os_misc.c index e6894731b63..954900e3c5d 100644 --- a/src/util/os_misc.c +++ b/src/util/os_misc.c @@ -27,6 +27,7 @@ #include "os_misc.h" +#include "os_file.h" #include @@ -44,6 +45,8 @@ #include #include +#include +#include #endif @@ -182,3 +185,31 @@ os_get_total_physical_memory(uint64_t *size) return false; #endif } + +bool +os_get_available_system_memory(uint64_t *size) +{ +#if DETECT_OS_LINUX + char *meminfo = os_read_file("/proc/meminfo", NULL); + if (!meminfo) + return false; + + char *str = strstr(meminfo, "MemAvailable:"); + if (!str) { + free(meminfo); + return false; + } + + uint64_t kb_mem_available; + if (sscanf(str, "MemAvailable: %" PRIx64, &kb_mem_available) == 1) { + free(meminfo); + *size = kb_mem_available << 10; + return true; + } + + free(meminfo); + return false; +#else + return false; +#endif +} diff --git a/src/util/os_misc.h b/src/util/os_misc.h index 19c8962d5d8..60811faf5a4 100644 --- a/src/util/os_misc.h +++ b/src/util/os_misc.h @@ -95,6 +95,12 @@ os_get_option(const char *name); bool os_get_total_physical_memory(uint64_t *size); +/* + * Amount of physical memory available to a process + */ +bool +os_get_available_system_memory(uint64_t *size); + #ifdef __cplusplus }