[Mesa-dev] [PATCH 2/4] util/os_misc: add os_get_available_system_memory()
Jonathan Gray
jsg at jsg.id.au
Thu Dec 5 14:54:14 UTC 2019
Add os_get_available_system_memory() derived from
src/intel/vulkan/anv_device.c get_available_system_memory()
Signed-off-by: Jonathan Gray <jsg at jsg.id.au>
---
src/util/os_misc.c | 31 +++++++++++++++++++++++++++++++
src/util/os_misc.h | 6 ++++++
2 files changed, 37 insertions(+)
diff --git a/src/util/os_misc.c b/src/util/os_misc.c
index e6894731b63..581e15f3934 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 <stdarg.h>
@@ -44,6 +45,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
#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");
+ 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
}
--
2.24.0
More information about the mesa-dev
mailing list