use std::time::Duration;
pub fn process_cpu_time() -> Duration {
#[cfg(windows)]
{
crate::windows_live::process_cpu_time()
}
#[cfg(unix)]
{
let mut r: libc::rusage = unsafe { std::mem::zeroed() };
if unsafe { libc::getrusage(libc::RUSAGE_SELF, &mut r) } != 0 {
return Duration::ZERO;
}
let tv = |t: libc::timeval| {
Duration::from_secs(t.tv_sec.max(0) as u64)
+ Duration::from_micros(t.tv_usec.max(0) as u64)
};
tv(r.ru_utime) + tv(r.ru_stime)
}
#[cfg(not(any(windows, unix)))]
{
Duration::ZERO
}
}
pub fn process_memory() -> u64 {
#[cfg(windows)]
{
crate::windows_live::process_memory()
}
#[cfg(target_os = "macos")]
{
let mut info: libc::rusage_info_v2 = unsafe { std::mem::zeroed() };
let ok = unsafe {
libc::proc_pid_rusage(
libc::getpid(),
libc::RUSAGE_INFO_V2,
(&mut info as *mut libc::rusage_info_v2).cast(),
)
} == 0;
if ok { info.ri_phys_footprint } else { 0 }
}
#[cfg(target_os = "linux")]
{
let pages = std::fs::read_to_string("/proc/self/statm")
.ok()
.and_then(|s| s.split_whitespace().nth(1)?.parse::<u64>().ok())
.unwrap_or(0);
let page = unsafe { libc::sysconf(libc::_SC_PAGESIZE) }.max(0) as u64;
pages * page
}
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
{
0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn this_process_uses_something() {
let mut x = 0u64;
for i in 0..2_000_000u64 {
x = x.wrapping_mul(31).wrapping_add(i);
}
assert!(x != 1);
assert!(process_cpu_time() > Duration::ZERO);
assert!(process_memory() > 0);
}
}