1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat(runtime): Add an OpenBSD implementation for rss() (#19221)

In case you would consider including specific implementations and not
only build fixes, here's the implementation of the ```rss()``` function
for OpenBSD.
This commit is contained in:
VlkrS 2023-06-05 14:24:19 +02:00 committed by GitHub
parent 08bd23970d
commit 3d582156b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -380,6 +380,53 @@ fn rss() -> usize {
task_info.resident_size as usize
}
#[cfg(target_os = "openbsd")]
fn rss() -> usize {
// Uses OpenBSD's KERN_PROC_PID sysctl(2)
// to retrieve information about the current
// process, part of which is the RSS (p_vm_rssize)
// SAFETY: libc call (get PID of own process)
let pid = unsafe { libc::getpid() };
// SAFETY: libc call (get system page size)
let pagesize = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
// KERN_PROC_PID returns a struct libc::kinfo_proc
let mut kinfoproc = std::mem::MaybeUninit::<libc::kinfo_proc>::uninit();
let mut size = std::mem::size_of_val(&kinfoproc) as libc::size_t;
let mut mib = [
libc::CTL_KERN,
libc::KERN_PROC,
libc::KERN_PROC_PID,
pid,
// mib is an array of integers, size is of type size_t
// conversion is safe, because the size of a libc::kinfo_proc
// structure will not exceed i32::MAX
size.try_into().unwrap(),
1,
];
// SAFETY: libc call, mib has been statically initialized,
// kinfoproc is a valid pointer to a libc::kinfo_proc struct
let res = unsafe {
libc::sysctl(
mib.as_mut_ptr(),
mib.len() as _,
kinfoproc.as_mut_ptr() as *mut libc::c_void,
&mut size,
std::ptr::null_mut(),
0,
)
};
if res == 0 {
// SAFETY: sysctl returns 0 on success and kinfoproc is initialized
// p_vm_rssize contains size in pages -> multiply with pagesize to
// get size in bytes.
pagesize * unsafe { (*kinfoproc.as_mut_ptr()).p_vm_rssize as usize }
} else {
0
}
}
#[cfg(windows)]
fn rss() -> usize {
use winapi::shared::minwindef::DWORD;