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 <jsg@jsg.id.au>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6517>
#include "os_misc.h"
+#include "os_file.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
#endif
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
+}
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
}