Skip to content

Commit 6c5eae3

Browse files
committed
Use unit system in unix_timer
1 parent 2fc989b commit 6c5eae3

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/timer/unix_timer.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ use crate::benchmark::quantity::{Information, InformationQuantity, Time, TimeQua
1313
#[derive(Debug, Copy, Clone)]
1414
struct ResourceUsage {
1515
/// Total amount of time spent executing in user mode
16-
pub user_usec: f64,
16+
pub time_user: Time,
1717

1818
/// Total amount of time spent executing in kernel mode
19-
pub system_usec: f64,
19+
pub time_system: Time,
2020

2121
/// Maximum amount of memory used by the process, in bytes
22-
pub memory_usage_byte: i64,
22+
pub memory_usage: Information,
2323
}
2424

2525
#[allow(clippy::useless_conversion)]
26-
fn 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-6
26+
fn convert_timeval(tv: libc::timeval) -> Time {
27+
let sec = tv.tv_sec as f64;
28+
let usec = tv.tv_usec as f64;
29+
30+
Time::from_seconds(sec) + Time::from_microseconds(usec)
2931
}
3032

3133
#[allow(clippy::useless_conversion)]
@@ -45,17 +47,17 @@ fn wait4(mut child: Child) -> io::Result<(ExitStatus, ResourceUsage)> {
4547

4648
let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
4749
// Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes
48-
rusage.ru_maxrss
50+
Information::from_bytes(u64::try_from(rusage.ru_maxrss).unwrap_or(0))
4951
} else {
50-
rusage.ru_maxrss * 1024
52+
Information::from_kibibytes(u64::try_from(rusage.ru_maxrss).unwrap_or(0))
5153
};
5254

5355
Ok((
5456
ExitStatus::from_raw(status),
5557
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(),
5961
},
6062
))
6163
}
@@ -72,9 +74,9 @@ impl CPUTimer {
7274
let (status, usage) = wait4(child)?;
7375
Ok((
7476
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,
7880
))
7981
}
8082
}

0 commit comments

Comments
 (0)