runtime: add used_memory implementation for OpenBSD (#24918)

This commit is contained in:
Laurent Cheylus 2025-07-17 16:51:07 +02:00 committed by GitHub
parent c3dfe62d7b
commit 54e8ed5ec5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 2 deletions

View File

@ -1,8 +1,8 @@
module runtime
// used_memory retrieves the current physical memory usage of the process.
// Note: implementation available only on FreeBSD, macOS, Linux and Windows. Otherwise,
// returns 'used_memory: not implemented'.
// Note: implementation available only on FreeBSD, macOS, Linux, OpenBSD and
// Windows. Otherwise, returns 'used_memory: not implemented'.
pub fn used_memory() !u64 {
return error('used_memory: not implemented')
}

View File

@ -0,0 +1,26 @@
module runtime
#include <sys/resource.h>
struct C.rusage {
ru_maxrss int
}
fn C.getrusage(who int, usage &C.rusage) int
// used_memory retrieves the current physical memory usage of the process.
pub fn used_memory() !u64 {
page_size := usize(C.sysconf(C._SC_PAGESIZE))
c_errno_1 := C.errno
if page_size == usize(-1) {
return error('used_memory: C.sysconf() return error code = ${c_errno_1}')
}
mut usage := C.rusage{}
ret := C.getrusage(C.RUSAGE_SELF, &usage)
if ret == -1 {
c_errno_2 := C.errno
return error('used_memory: C.getrusage() return error code = ${c_errno_2}')
}
return u64(int_max(1, usage.ru_maxrss)) * 1024
}