Module: Mesa
Branch: staging/20.1
Commit: 9dae69fd59a58fed7576cf01479b14b0d569bb93
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=9dae69fd59a58fed7576cf01479b14b0d569bb93

Author: Jonathan Gray <[email protected]>
Date:   Fri Dec  6 01:01:29 2019 +1100

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 <[email protected]>
Reviewed-by: Jason Ekstrand <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6517>
(cherry picked from commit 033dcb2978c6d95d42da944b8147228d239d32f4)

---

 .pick_status.json  |  2 +-
 src/util/os_misc.c | 31 +++++++++++++++++++++++++++++++
 src/util/os_misc.h |  6 ++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/.pick_status.json b/.pick_status.json
index 839d11355f1..a6818204607 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -121,7 +121,7 @@
         "description": "util/os_misc: add os_get_available_system_memory()",
         "nominated": true,
         "nomination_type": 1,
-        "resolution": 0,
+        "resolution": 1,
         "master_sha": null,
         "because_sha": "b80930a6fea075d2ef283ceac5a2a64e65fd7bc4"
     },
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 <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", 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
 }

_______________________________________________
mesa-commit mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/mesa-commit

Reply via email to