2022-02-22 11:34:30 +01:00
|
|
|
pub mod wall_clock_timer;
|
2018-02-05 18:22:59 -05:00
|
|
|
|
2021-08-24 21:36:37 +02:00
|
|
|
#[cfg(windows)]
|
2022-02-22 11:34:30 +01:00
|
|
|
mod windows_timer;
|
2019-05-07 11:55:38 +02:00
|
|
|
|
2021-08-24 21:36:37 +02:00
|
|
|
#[cfg(not(windows))]
|
2022-02-22 11:34:30 +01:00
|
|
|
mod unix_timer;
|
|
|
|
|
|
|
|
|
|
use crate::util::units::Second;
|
|
|
|
|
|
|
|
|
|
use std::process::{Command, ExitStatus};
|
|
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
2021-08-24 21:48:10 +02:00
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
2022-02-22 11:34:30 +01:00
|
|
|
struct CPUTimes {
|
2021-08-24 21:48:10 +02:00
|
|
|
/// Total amount of time spent executing in user mode
|
|
|
|
|
pub user_usec: i64,
|
|
|
|
|
|
|
|
|
|
/// Total amount of time spent executing in kernel mode
|
|
|
|
|
pub system_usec: i64,
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-22 11:34:30 +01:00
|
|
|
/// Used to indicate the result of running a command
|
2021-08-24 21:48:10 +02:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2022-02-22 11:34:30 +01:00
|
|
|
pub struct ExecuteResult {
|
|
|
|
|
/// The amount of user time the process used
|
|
|
|
|
pub user_time: Second,
|
2021-08-24 21:48:10 +02:00
|
|
|
|
2022-02-22 11:34:30 +01:00
|
|
|
/// The amount of cpu time the process used
|
|
|
|
|
pub system_time: Second,
|
|
|
|
|
|
|
|
|
|
/// The exit status of the process
|
|
|
|
|
pub status: ExitStatus,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Execute the given command and return a timing summary
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {
|
|
|
|
|
let cpu_timer = self::unix_timer::CPUTimer::start();
|
|
|
|
|
let status = command
|
|
|
|
|
.status()
|
|
|
|
|
.with_context(|| error_message.to_string())?;
|
|
|
|
|
let (user_time, system_time) = cpu_timer.stop();
|
|
|
|
|
|
|
|
|
|
Ok(ExecuteResult {
|
|
|
|
|
user_time,
|
|
|
|
|
system_time,
|
|
|
|
|
status,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Execute the given command and return a timing summary
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {
|
|
|
|
|
let mut child = command.spawn().with_context(|| error_message.to_string())?;
|
|
|
|
|
let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);
|
|
|
|
|
let status = child.wait()?;
|
|
|
|
|
|
|
|
|
|
let (user_time, system_time) = cpu_timer.stop();
|
|
|
|
|
Ok(ExecuteResult {
|
|
|
|
|
user_time,
|
|
|
|
|
system_time,
|
|
|
|
|
status,
|
|
|
|
|
})
|
2021-08-24 21:48:10 +02:00
|
|
|
}
|