brush_core/sys/unix/
resource.rs

1//! Resource utilities
2
3use crate::error;
4
5/// Returns the user and system CPU time used by the current process;
6/// expressed as a tuple containing user time and system time, in that order.
7pub fn get_self_user_and_system_time()
8-> Result<(std::time::Duration, std::time::Duration), error::Error> {
9    let usage = nix::sys::resource::getrusage(nix::sys::resource::UsageWho::RUSAGE_SELF)?;
10    Ok((
11        convert_rusage_time(usage.user_time()),
12        convert_rusage_time(usage.system_time()),
13    ))
14}
15
16/// Returns the user and system CPU time used by child processes; expressed
17/// as a tuple containing user time and system time, in that order.
18pub fn get_children_user_and_system_time()
19-> Result<(std::time::Duration, std::time::Duration), error::Error> {
20    let usage = nix::sys::resource::getrusage(nix::sys::resource::UsageWho::RUSAGE_CHILDREN)?;
21    Ok((
22        convert_rusage_time(usage.user_time()),
23        convert_rusage_time(usage.system_time()),
24    ))
25}
26
27const fn convert_rusage_time(time: nix::sys::time::TimeVal) -> std::time::Duration {
28    #[allow(clippy::cast_sign_loss)]
29    #[allow(clippy::cast_possible_truncation)]
30    std::time::Duration::new(time.tv_sec() as u64, time.tv_usec() as u32 * 1000)
31}