runtime: improve free_memory implementation for OpenBSD, by getting the stats from its UVM system (#24431)

This commit is contained in:
Laurent Cheylus 2025-05-08 11:55:10 +02:00 committed by GitHub
parent bbee42f15e
commit 656afa9d13
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,14 +1,24 @@
module runtime module runtime
#include <sys/sysctl.h>
#include <uvm/uvmexp.h>
struct C.uvmexp {
pagesize int
free int
}
fn free_memory_impl() usize { fn free_memory_impl() usize {
$if cross ? { $if cross ? {
return 1 return 1
} }
$if !cross ? { $if !cross ? {
$if openbsd { $if openbsd {
page_size := usize(C.sysconf(C._SC_PAGESIZE)) mib := [C.CTL_VM, C.VM_UVMEXP]!
av_phys_pages := usize(C.sysconf(C._SC_AVPHYS_PAGES)) mut uvm := C.uvmexp{0, 0}
return page_size * av_phys_pages mut len := sizeof(C.uvmexp)
unsafe { C.sysctl(&mib[0], mib.len, &uvm, &len, C.NULL, 0) }
return usize(uvm.pagesize * uvm.free)
} }
} }
return 1 return 1