@@ -13,19 +13,21 @@ use crate::benchmark::quantity::{Information, InformationQuantity, Time, TimeQua
13
13
#[ derive( Debug , Copy , Clone ) ]
14
14
struct ResourceUsage {
15
15
/// Total amount of time spent executing in user mode
16
- pub user_usec : f64 ,
16
+ pub time_user : Time ,
17
17
18
18
/// Total amount of time spent executing in kernel mode
19
- pub system_usec : f64 ,
19
+ pub time_system : Time ,
20
20
21
21
/// Maximum amount of memory used by the process, in bytes
22
- pub memory_usage_byte : i64 ,
22
+ pub memory_usage : Information ,
23
23
}
24
24
25
25
#[ 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)
29
31
}
30
32
31
33
#[ allow( clippy:: useless_conversion) ]
@@ -45,17 +47,17 @@ fn wait4(mut child: Child) -> io::Result<(ExitStatus, ResourceUsage)> {
45
47
46
48
let memory_usage_byte = if cfg ! ( target_os = "macos" ) || cfg ! ( target_os = "ios" ) {
47
49
// 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 ) )
49
51
} else {
50
- rusage. ru_maxrss * 1024
52
+ Information :: from_kibibytes ( u64 :: try_from ( rusage. ru_maxrss ) . unwrap_or ( 0 ) )
51
53
} ;
52
54
53
55
Ok ( (
54
56
ExitStatus :: from_raw ( status) ,
55
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 ( ) ,
59
61
} ,
60
62
) )
61
63
}
@@ -72,9 +74,9 @@ impl CPUTimer {
72
74
let ( status, usage) = wait4 ( child) ?;
73
75
Ok ( (
74
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 ,
78
80
) )
79
81
}
80
82
}
0 commit comments