2022-02-22 12:56:04 +01:00
|
|
|
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;
|
|
|
|
|
|
2022-05-14 13:21:41 -04:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
use nix::fcntl::{splice, SpliceFFlags};
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
|
|
2022-02-22 11:34:30 +01:00
|
|
|
use crate::util::units::Second;
|
2022-02-22 12:56:04 +01:00
|
|
|
use wall_clock_timer::WallClockTimer;
|
2022-02-22 11:34:30 +01:00
|
|
|
|
2022-05-17 14:21:16 -04:00
|
|
|
use std::io::Read;
|
2022-05-14 13:21:41 -04:00
|
|
|
use std::process::{ChildStdout, Command, ExitStatus};
|
2022-02-22 11:34:30 +01:00
|
|
|
|
2022-02-22 11:45:46 +01:00
|
|
|
use anyhow::Result;
|
2021-08-24 21:48:10 +02:00
|
|
|
|
2022-08-08 16:37:39 +02:00
|
|
|
#[cfg(not(windows))]
|
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 12:56:56 +01:00
|
|
|
pub struct TimerResult {
|
2022-02-22 12:56:04 +01:00
|
|
|
pub time_real: Second,
|
|
|
|
|
pub time_user: Second,
|
|
|
|
|
pub time_system: Second,
|
2022-02-22 11:34:30 +01:00
|
|
|
|
|
|
|
|
/// The exit status of the process
|
|
|
|
|
pub status: ExitStatus,
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-14 13:21:41 -04:00
|
|
|
/// Discard the output of a child process.
|
|
|
|
|
fn discard(output: ChildStdout) {
|
|
|
|
|
const CHUNK_SIZE: usize = 64 << 10;
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
{
|
|
|
|
|
if let Ok(file) = File::create("/dev/null") {
|
|
|
|
|
while let Ok(bytes) = splice(
|
|
|
|
|
output.as_raw_fd(),
|
|
|
|
|
None,
|
|
|
|
|
file.as_raw_fd(),
|
|
|
|
|
None,
|
|
|
|
|
CHUNK_SIZE,
|
|
|
|
|
SpliceFFlags::empty(),
|
|
|
|
|
) {
|
|
|
|
|
if bytes == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-17 14:21:16 -04:00
|
|
|
let mut output = output;
|
|
|
|
|
let mut buf = [0; CHUNK_SIZE];
|
|
|
|
|
while let Ok(bytes) = output.read(&mut buf) {
|
|
|
|
|
if bytes == 0 {
|
|
|
|
|
break;
|
2022-05-14 13:21:41 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-22 11:34:30 +01:00
|
|
|
/// Execute the given command and return a timing summary
|
2022-02-22 12:56:56 +01:00
|
|
|
pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {
|
2022-02-22 12:56:04 +01:00
|
|
|
#[cfg(not(windows))]
|
2022-05-14 13:21:41 -04:00
|
|
|
let cpu_timer = self::unix_timer::CPUTimer::start();
|
2022-05-17 14:21:16 -04:00
|
|
|
|
2022-08-08 16:37:39 +02:00
|
|
|
#[cfg(windows)]
|
|
|
|
|
{
|
|
|
|
|
use std::os::windows::process::CommandExt;
|
|
|
|
|
|
|
|
|
|
// Create a suspended process
|
|
|
|
|
command.creation_flags(4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let wallclock_timer = WallClockTimer::start();
|
2022-05-17 14:21:16 -04:00
|
|
|
let mut child = command.spawn()?;
|
|
|
|
|
|
2022-02-22 12:56:04 +01:00
|
|
|
#[cfg(windows)]
|
2022-08-08 16:37:39 +02:00
|
|
|
let cpu_timer = {
|
|
|
|
|
// SAFETY: We created a suspended process
|
|
|
|
|
unsafe { self::windows_timer::CPUTimer::start_suspended_process(&child) }
|
|
|
|
|
};
|
2022-02-22 12:56:04 +01:00
|
|
|
|
2022-05-17 14:21:16 -04:00
|
|
|
if let Some(output) = child.stdout.take() {
|
|
|
|
|
// Handle CommandOutputPolicy::Pipe
|
2022-05-14 13:21:41 -04:00
|
|
|
discard(output);
|
|
|
|
|
}
|
2022-05-17 14:21:16 -04:00
|
|
|
|
2022-05-14 13:21:41 -04:00
|
|
|
let status = child.wait()?;
|
2022-02-22 12:56:04 +01:00
|
|
|
|
|
|
|
|
let time_real = wallclock_timer.stop();
|
2022-08-08 16:37:39 +02:00
|
|
|
let (time_user, time_system) = cpu_timer.stop();
|
2022-02-22 11:34:30 +01:00
|
|
|
|
2022-02-22 12:56:56 +01:00
|
|
|
Ok(TimerResult {
|
2022-02-22 12:56:04 +01:00
|
|
|
time_real,
|
|
|
|
|
time_user,
|
|
|
|
|
time_system,
|
2022-02-22 11:34:30 +01:00
|
|
|
status,
|
|
|
|
|
})
|
2021-08-24 21:48:10 +02:00
|
|
|
}
|