[Mesa-dev] [PATCH 2/2] anv: implement OpenBSD get_available_system_memory()

Jonathan Gray jsg at jsg.id.au
Sun Dec 1 07:38:42 UTC 2019


On Sun, Dec 01, 2019 at 03:22:01AM +1100, Jonathan Gray wrote:
> On Sun, Dec 01, 2019 at 02:21:49AM +1100, Jonathan Gray wrote:
> > map linux /proc/meminfo "MemAvailable" to uvm free pages
> 
> On second thought HW_USERMEM64 may be a better fit here.
> 
> "The amount of available non-kernel memory in bytes"
> 
> Which ends up being physmem - uvmexp.wired (memory that can't be paged
> out) instead of the current amount of free memory.

Along the lines of this:

commit e9d96571d3670de169595c2009e7a49febd7cd2d
Author: Jonathan Gray <jsg at jsg.id.au>
Date:   Wed Nov 27 01:01:44 2019 +1100

    anv: implement OpenBSD get_available_system_memory()
    
    Determine a value for how much memory a process can allocate
    without failing not exceeding amount of physical memory
    available to userspace.
    
    v2: use the smallest value of available non-kernel
        physical memory and per process data size limit.
    
    Signed-off-by: Jonathan Gray <jsg at jsg.id.au>

diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index 2a2041535c5..eddcf727f8a 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -28,6 +28,7 @@
 #ifdef __OpenBSD__
 #include <sys/types.h>
 #include <sys/sysctl.h>
+#include <sys/resource.h>
 #else
 #include <sys/sysinfo.h>
 #endif
@@ -369,6 +370,22 @@ anv_physical_device_free_disk_cache(struct anv_physical_device *device)
 static uint64_t
 get_available_system_memory()
 {
+#ifdef __OpenBSD__
+   struct rlimit rl;
+   int mib[] = { CTL_HW, HW_USERMEM64 };
+   int64_t mem_available;
+   size_t size = sizeof(mem_available);
+
+   /* physmem - wired */
+   if (sysctl(mib, 2, &mem_available, &size, NULL, 0) == -1)
+      return 0;
+
+   /* fixed login.conf limit */
+   if (getrlimit(RLIMIT_DATA, &rl) == -1)
+      return 0;
+
+   return MIN2(mem_available, rl.rlim_cur);
+#else
    char *meminfo = os_read_file("/proc/meminfo");
    if (!meminfo)
       return 0;
@@ -387,6 +404,7 @@ get_available_system_memory()
 
    free(meminfo);
    return 0;
+#endif
 }
 
 static VkResult


More information about the mesa-dev mailing list