Skip to content
Myr

Use unit system in unix_timer

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent 641451a · Browse files · 1 file+16 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+16 added−14 removed

  • other:+16 added−14 removed

1 file+16 added−14 removed

modified:src/timer/unix_timer.rs+16 added-14 removed
11 unmodified lines
1213#[derive(Debug, Copy, Clone)]14struct ResourceUsage {15 /// Total amount of time spent executing in user mode16 pub user_usec: f64,16 pub time_user: Time,1718 /// Total amount of time spent executing in kernel mode19 pub system_usec: f64,19 pub time_system: Time,2021 /// Maximum amount of memory used by the process, in bytes22 pub memory_usage_byte: i64,22 pub memory_usage: Information,23}2425#[allow(clippy::useless_conversion)]26fn timeval_to_seconds(tv: libc::timeval) -> f64 {27 const MICROSEC_PER_SEC: i64 = 1000 * 1000;28 (i64::from(tv.tv_sec) * MICROSEC_PER_SEC + i64::from(tv.tv_usec)) as f64 * 1e-626fn convert_timeval(tv: libc::timeval) -> Time {27 let sec = tv.tv_sec as f64;28 let usec = tv.tv_usec as f64;2930 Time::from_seconds(sec) + Time::from_microseconds(usec)31}3233#[allow(clippy::useless_conversion)]34fn wait4(mut child: Child) -> io::Result<(ExitStatus, ResourceUsage)> {
11 unmodified lines
46 let rusage = unsafe { rusage.assume_init() };4748 let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {49 // Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes48 rusage.ru_maxrss50 Information::from_bytes(u64::try_from(rusage.ru_maxrss).unwrap_or(0))51 } else {50 rusage.ru_maxrss * 102452 Information::from_kibibytes(u64::try_from(rusage.ru_maxrss).unwrap_or(0))53 };5455 Ok((56 ExitStatus::from_raw(status),57 ResourceUsage {56 user_usec: timeval_to_seconds(rusage.ru_utime),57 system_usec: timeval_to_seconds(rusage.ru_stime),58 memory_usage_byte: memory_usage_byte.into(),58 time_user: convert_timeval(rusage.ru_utime),59 time_system: convert_timeval(rusage.ru_stime),60 memory_usage: memory_usage_byte.into(),61 },62 ))63 }64}
8 unmodified lines
73 pub fn stop(&self, child: Child) -> Result<(ExitStatus, Time, Time, Information)> {74 let (status, usage) = wait4(child)?;75 Ok((76 status,75 Time::from_seconds(usage.user_usec),76 Time::from_seconds(usage.system_usec),77 Information::from_bytes(u64::try_from(usage.memory_usage_byte).unwrap_or(0)),77 usage.time_user,78 usage.time_system,79 usage.memory_usage,80 ))81 }82}