Skip to content
Myr

Use wait4 instead of getrusage

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 50232fe · Browse files · 9 files+143 added−98 removed
This commit

State

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

Diff stats

9 files+143 added−98 removed

  • other:+143 added−98 removed

9 files+143 added−98 removed

modified:src/benchmark/benchmark_result.rs+6 added-7 removed
16 unmodified lines
17 pub user_time: Second,1819 /// Time spent in kernel mode20 pub system_time: Second,2122 /// Maximum memory usage of the process, in bytes23 pub memory_usage_byte: u64,2425 /// Exit codes of the process26 pub exit_code: Option<i32>,27}2829use crate::benchmark::measurement::Measurements;30use crate::quantity::{byte, second, Time};
19 unmodified lines
5051 /// All run time measurements52 pub runs: Vec<BenchmarkRun>,5348 /// Maximum memory usage of the process, in bytes49 #[serde(skip_serializing_if = "Option::is_none")]50 pub memory_usage_byte: Option<Vec<u64>>,5152 /// Exit codes of all command invocations53 pub exit_codes: Vec<Option<i32>>,5454 /// Parameter values for this benchmark55 pub parameters: BTreeMap<String, Parameter>,56}57
121 unmodified lines
modified:src/benchmark/mod.rs+18 added-8 removed
490 unmodified lines
491 command: self.command.get_name(),492 command_with_unused_parameters: self.command.get_name_with_unused_parameters(),493 runs: times_real494 .iter()495 .zip(times_user.iter().zip(times_system.iter()))496 .map(|(wall_clock_time, (user_time, system_time))| BenchmarkRun {497 wall_clock_time: *wall_clock_time,498 user_time: *user_time,499 system_time: *system_time,500 })495 .zip(times_user.iter())496 .zip(times_system.iter())497 .zip(memory_usage_byte.iter())498 .zip(exit_codes.iter())499 .map(500 |(501 (((wall_clock_time, user_time), system_time), memory_usage_byte),502 exit_code,503 )| {504 BenchmarkRun {505 wall_clock_time: *wall_clock_time,506 user_time: *user_time,507 system_time: *system_time,508 memory_usage_byte: *memory_usage_byte,509 exit_code: *exit_code,510 }511 },512 )513 .collect(),502 memory_usage_byte: Some(memory_usage_byte),503 exit_codes,514 parameters: self515 .command516 .get_parameters()517 .iter()
13 unmodified lines
modified:src/benchmark/relative_speed.rs+2 added-2 removed
154 unmodified lines
155 runs: vec![BenchmarkRun {156 wall_clock_time: mean,157 user_time: mean,158 system_time: 0.,159 memory_usage_byte: 1024,160 exit_code: Some(0),161 }],160 memory_usage_byte: None,161 exit_codes: Vec::new(),162 parameters: BTreeMap::new(),163 }164}165
64 unmodified lines
modified:src/benchmark/scheduler.rs+8 added-12 removed
207 unmodified lines
208 runs:209 - wall_clock_time: 0.123210 user_time: 0211 system_time: 0212 memory_usage_byte: 0213 exit_code: 0214 - wall_clock_time: 0.123215 user_time: 0216 system_time: 0215 memory_usage_byte:216 - 0217 - 0218 exit_codes:219 - 0220 - 0217 memory_usage_byte: 0218 exit_code: 0219 - command: sleep 0.456220 runs:221 - wall_clock_time: 0.456222 user_time: 0223 system_time: 0224 memory_usage_byte: 0225 exit_code: 0226 - wall_clock_time: 0.456227 user_time: 0228 system_time: 0229 memory_usage_byte:230 - 0231 - 0232 exit_codes:233 - 0234 - 0229 memory_usage_byte: 0230 exit_code: 0231 "#);232233 Ok(())234}
modified:src/export/csv.rs+12 added-4 removed
88 unmodified lines
89 BenchmarkRun {90 wall_clock_time: 7.0,91 user_time: 7.0,92 system_time: 0.0,93 memory_usage_byte: 1024,94 exit_code: Some(0),95 },96 BenchmarkRun {97 wall_clock_time: 8.0,98 user_time: 8.0,99 system_time: 0.0,100 memory_usage_byte: 1024,101 exit_code: Some(0),102 },103 BenchmarkRun {104 wall_clock_time: 12.0,105 user_time: 12.0,106 system_time: 0.0,107 memory_usage_byte: 1024,108 exit_code: Some(0),109 },110 ],105 memory_usage_byte: None,106 exit_codes: vec![Some(0), Some(0), Some(0)],111 parameters: {112 let mut params = BTreeMap::new();113 params.insert(114 "foo".into(),
19 unmodified lines
134 BenchmarkRun {135 wall_clock_time: 17.0,136 user_time: 17.0,137 system_time: 0.0,138 memory_usage_byte: 1024,139 exit_code: Some(0),140 },141 BenchmarkRun {142 wall_clock_time: 18.0,143 user_time: 18.0,144 system_time: 0.0,145 memory_usage_byte: 1024,146 exit_code: Some(0),147 },148 BenchmarkRun {149 wall_clock_time: 19.0,150 user_time: 19.0,151 system_time: 0.0,152 memory_usage_byte: 1024,153 exit_code: Some(0),154 },155 ],146 memory_usage_byte: None,147 exit_codes: vec![Some(0), Some(0), Some(0)],156 parameters: {157 let mut params = BTreeMap::new();158 params.insert(159 "foo".into(),
30 unmodified lines
modified:src/export/tests.rs+48 added-16 removed
31 unmodified lines
32 BenchmarkRun {33 wall_clock_time: 0.09,34 user_time: 0.09,35 system_time: 0.,36 memory_usage_byte: 1024,37 exit_code: Some(0),38 },39 BenchmarkRun {40 wall_clock_time: 0.10,41 user_time: 0.10,42 system_time: 0.,43 memory_usage_byte: 1024,44 exit_code: Some(0),45 },46 BenchmarkRun {47 wall_clock_time: 0.14,48 user_time: 0.14,49 system_time: 0.,50 memory_usage_byte: 1024,51 exit_code: Some(0),52 },53 ],48 memory_usage_byte: None,49 exit_codes: vec![Some(0), Some(0), Some(0)],54 parameters: BTreeMap::new(),55 },56 BenchmarkResult {57 command: String::from("sleep 2"),
2 unmodified lines
60 BenchmarkRun {61 wall_clock_time: 2.0,62 user_time: 2.0,63 system_time: 0.,64 memory_usage_byte: 1024,65 exit_code: Some(0),66 },67 BenchmarkRun {68 wall_clock_time: 3.0,69 user_time: 3.0,70 system_time: 0.,71 memory_usage_byte: 1024,72 exit_code: Some(0),73 },74 BenchmarkRun {75 wall_clock_time: 4.0,76 user_time: 4.0,77 system_time: 0.,78 memory_usage_byte: 1024,79 exit_code: Some(0),80 },81 ],72 memory_usage_byte: None,73 exit_codes: vec![Some(0), Some(0), Some(0)],82 parameters: BTreeMap::new(),83 },84 ];85
47 unmodified lines
133 BenchmarkRun {134 wall_clock_time: 2.1,135 user_time: 2.1,136 system_time: 0.,137 memory_usage_byte: 1024,138 exit_code: Some(0),139 },140 BenchmarkRun {141 wall_clock_time: 2.2,142 user_time: 2.2,143 system_time: 0.,144 memory_usage_byte: 1024,145 exit_code: Some(0),146 },147 BenchmarkRun {148 wall_clock_time: 2.3,149 user_time: 2.3,150 system_time: 0.,151 memory_usage_byte: 1024,152 exit_code: Some(0),153 },154 ],141 memory_usage_byte: None,142 exit_codes: vec![Some(0), Some(0), Some(0)],155 parameters: BTreeMap::new(),156 },157 BenchmarkResult {158 command: String::from("sleep 0.1"),
2 unmodified lines
161 BenchmarkRun {162 wall_clock_time: 0.1,163 user_time: 0.1,164 system_time: 0.,165 memory_usage_byte: 1024,166 exit_code: Some(0),167 },168 BenchmarkRun {169 wall_clock_time: 0.2,170 user_time: 0.2,171 system_time: 0.,172 memory_usage_byte: 1024,173 exit_code: Some(0),174 },175 BenchmarkRun {176 wall_clock_time: 0.3,177 user_time: 0.3,178 system_time: 0.,179 memory_usage_byte: 1024,180 exit_code: Some(0),181 },182 ],165 memory_usage_byte: None,166 exit_codes: vec![Some(0), Some(0), Some(0)],183 parameters: BTreeMap::new(),184 },185 ];186
47 unmodified lines
234 BenchmarkRun {235 wall_clock_time: 2.1,236 user_time: 2.1,237 system_time: 0.,238 memory_usage_byte: 1024,239 exit_code: Some(0),240 },241 BenchmarkRun {242 wall_clock_time: 2.2,243 user_time: 2.2,244 system_time: 0.,245 memory_usage_byte: 1024,246 exit_code: Some(0),247 },248 BenchmarkRun {249 wall_clock_time: 2.3,250 user_time: 2.3,251 system_time: 0.,252 memory_usage_byte: 1024,253 exit_code: Some(0),254 },255 ],234 memory_usage_byte: None,235 exit_codes: vec![Some(0), Some(0), Some(0)],256 parameters: BTreeMap::new(),257 },258 BenchmarkResult {259 command: String::from("sleep 0.1"),
2 unmodified lines
262 BenchmarkRun {263 wall_clock_time: 0.1,264 user_time: 0.1,265 system_time: 0.,266 memory_usage_byte: 1024,267 exit_code: Some(0),268 },269 BenchmarkRun {270 wall_clock_time: 0.2,271 user_time: 0.2,272 system_time: 0.,273 memory_usage_byte: 1024,274 exit_code: Some(0),275 },276 BenchmarkRun {277 wall_clock_time: 0.3,278 user_time: 0.3,279 system_time: 0.,280 memory_usage_byte: 1024,281 exit_code: Some(0),282 },283 ],258 memory_usage_byte: None,259 exit_codes: vec![Some(0), Some(0), Some(0)],284 parameters: BTreeMap::new(),285 },286 ];287
46 unmodified lines
334 BenchmarkRun {335 wall_clock_time: 2.01,336 user_time: 2.01,337 system_time: 0.,338 memory_usage_byte: 1024,339 exit_code: Some(0),340 },341 BenchmarkRun {342 wall_clock_time: 2.02,343 user_time: 2.02,344 system_time: 0.,345 memory_usage_byte: 1024,346 exit_code: Some(0),347 },348 BenchmarkRun {349 wall_clock_time: 2.03,350 user_time: 2.03,351 system_time: 0.,352 memory_usage_byte: 1024,353 exit_code: Some(0),354 },355 ],326 memory_usage_byte: None,327 exit_codes: vec![Some(0), Some(0), Some(0)],356 parameters: BTreeMap::new(),357 },358 BenchmarkResult {359 command: String::from("sleep 0.1"),
2 unmodified lines
362 BenchmarkRun {363 wall_clock_time: 0.11,364 user_time: 0.11,365 system_time: 0.,366 memory_usage_byte: 1024,367 exit_code: Some(0),368 },369 BenchmarkRun {370 wall_clock_time: 0.12,371 user_time: 0.12,372 system_time: 0.,373 memory_usage_byte: 1024,374 exit_code: Some(0),375 },376 BenchmarkRun {377 wall_clock_time: 0.13,378 user_time: 0.13,379 system_time: 0.,380 memory_usage_byte: 1024,381 exit_code: Some(0),382 },383 ],350 memory_usage_byte: None,351 exit_codes: vec![Some(0), Some(0), Some(0)],384 parameters: BTreeMap::new(),385 },386 ];387
37 unmodified lines
modified:src/timer/mod.rs+13 added-2 removed
22 unmodified lines
23use std::process::{ChildStdout, Command};2425use anyhow::Result;2627/// Used to indicate the result of running a command28#[derive(Debug, Copy, Clone)]29pub struct TimerResult {30 pub time_real: Second,31 pub time_user: Second,32 pub time_system: Second,33 pub memory_usage_byte: u64,34 /// The exit status of the process35 pub status: ExitStatus,36}3738/// Discard the output of a child process.39fn discard(output: ChildStdout) {40 const CHUNK_SIZE: usize = 64 << 10;41
50 unmodified lines
92 // Handle CommandOutputPolicy::Pipe93 discard(output);94 }9585 let (time_user, time_system, peak_memory_usage, exit_status) = cpu_timer.stop(child)?;86 let time_wall_clock = wallclock_timer.stop();96 let (status, time_user, time_system, memory_usage_byte) = cpu_timer.stop(child)?;97 let time_real = wallclock_timer.stop();9899 Ok(Measurement {100 time_wall_clock,101 time_user,
5 unmodified lines
modified:src/timer/unix_timer.rs+32 added-41 removed
1#![cfg(not(windows))]23use std::convert::TryFrom;4use std::io;5use std::mem::MaybeUninit;6use std::os::unix::process::ExitStatusExt;7use std::process::{Child, ExitStatus};89use anyhow::Result;1010use crate::quantity::{byte, kibibyte, microsecond, second, Information, Time};11use crate::util::units::Second;1213#[derive(Debug, Copy, Clone)]14struct ResourceUsage {15 /// Total amount of time spent executing in user mode15 pub time_user: Time,16 pub user_usec: Second,1718 /// Total amount of time spent executing in kernel mode18 pub time_system: Time,19 pub system_usec: Second,2021 /// Maximum amount of memory used by the process, in bytes21 pub memory_usage: Information,22 pub memory_usage_byte: i64,23}2425#[allow(clippy::useless_conversion)]25fn convert_timeval(tv: libc::timeval) -> Time {26 let sec = tv.tv_sec as f64;27 let usec = tv.tv_usec as f64;2829 Time::new::<second>(sec) + Time::new::<microsecond>(usec)26fn timeval_to_seconds(tv: libc::timeval) -> Second {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-629}3031#[allow(clippy::useless_conversion)]32fn wait4(mut child: Child) -> io::Result<(ExitStatus, ResourceUsage)> {
2 unmodified lines
35 let pid = child.id() as i32;36 let mut status = 0;37 let mut rusage = MaybeUninit::zeroed();3840 loop {41 let result = unsafe { libc::wait4(pid, &mut status, 0, rusage.as_mut_ptr()) };39 let result = unsafe { libc::wait4(pid, &mut status, 0, rusage.as_mut_ptr()) };42 if result >= 0 {43 break;44 }45 let error = io::Error::last_os_error();46 if error.kind() != io::ErrorKind::Interrupted {47 return Err(error);48 }49 }4051 let rusage = unsafe { rusage.assume_init() };5253 let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {54 // Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes55 // A f64 can represent integers up to 2^53 exactly, so we can represent56 // all values up to 2^53 bytes = 8 PiB exactly. Beyond that, our precision57 // is larger than 1 byte, but that's considered acceptable.58 Information::new::<byte>(rusage.ru_maxrss as f64)41 if result < 0 {42 Err(io::Error::last_os_error())43 } else {60 Information::new::<kibibyte>(rusage.ru_maxrss as f64)61 };44 let rusage = unsafe { rusage.assume_init() };4546 let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {47 // Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes48 rusage.ru_maxrss49 } else {50 rusage.ru_maxrss * 102451 };5263 Ok((64 ExitStatus::from_raw(status),65 ResourceUsage {66 time_user: convert_timeval(rusage.ru_utime),67 time_system: convert_timeval(rusage.ru_stime),68 memory_usage: memory_usage_byte.into(),69 },70 ))53 Ok((54 ExitStatus::from_raw(status),55 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(),59 },60 ))61 }62}6364pub struct CPUTimer {}65
1 unmodified line
67 pub fn start() -> Self {68 Self {}69 }7080 pub fn stop(&self, child: Child) -> Result<(Time, Time, Information, ExitStatus)> {71 pub fn stop(&self, child: Child) -> Result<(ExitStatus, Second, Second, u64)> {72 let (status, usage) = wait4(child)?;73 Ok((83 usage.time_user,84 usage.time_system,85 usage.memory_usage,74 status,75 usage.user_usec,76 usage.system_usec,77 u64::try_from(usage.memory_usage_byte).unwrap_or(0),78 ))79 }80}
modified:src/timer/windows_timer.rs+4 added-6 removed
82 unmodified lines
8384 Self { job_object }85 }8687 pub fn stop(&self, mut child: Child) -> Result<(Time, Time, Information, ExitStatus)> {87 pub fn stop(&self, mut child: Child) -> Result<(ExitStatus, Second, Second, u64)> {88 let status = child.wait()?;8990 let mut job_object_info =91 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();
20 unmodified lines
112113 // The `TotalKernelTime` is "The total amount of kernel-mode execution time114 // for all active processes associated with the job, as well as all terminated115 // processes no longer associated with the job, in 100-nanosecond ticks."116 let system_time =117 Time::new::<nanosecond>((job_object_info.TotalKernelTime as f64) * 100.0);118119 Ok((user_time, system_time, Information::zero(), status))116 let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS;117 Ok((status, user as f64 * 1e-6, kernel as f64 * 1e-6, 0))118 } else {121 Ok((Time::zero(), Time::zero(), Information::zero(), status))119 Ok((status, 0.0, 0.0, 0))120 }121 }122}123
6 unmodified lines