mod wall_clock_timer;
#[cfg(windows)]
mod windows_timer;
#[cfg(not(windows))]
mod unix_timer;
#[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;
use crate::util::units::Second;
use wall_clock_timer::WallClockTimer;
use std::io::Read;
use std::process::{ChildStdout, Command, ExitStatus};
use anyhow::Result;
#[cfg(not(windows))]
#[derive(Debug, Copy, Clone)]
struct CPUTimes {
pub user_usec: i64,
pub system_usec: i64,
}
#[derive(Debug, Copy, Clone)]
pub struct TimerResult {
pub time_real: Second,
pub time_user: Second,
pub time_system: Second,
pub status: ExitStatus,
}
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;
}
}
}
}
let mut output = output;
let mut buf = [0; CHUNK_SIZE];
while let Ok(bytes) = output.read(&mut buf) {
if bytes == 0 {
break;
}
}
}
pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {
#[cfg(not(windows))]
let cpu_timer = self::unix_timer::CPUTimer::start();
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
command.creation_flags(4);
}
let wallclock_timer = WallClockTimer::start();
let mut child = command.spawn()?;
#[cfg(windows)]
let cpu_timer = {
unsafe { self::windows_timer::CPUTimer::start_suspended_process(&child) }
};
if let Some(output) = child.stdout.take() {
discard(output);
}
let status = child.wait()?;
let time_real = wallclock_timer.stop();
let (time_user, time_system) = cpu_timer.stop();
Ok(TimerResult {
time_real,
time_user,
time_system,
status,
})
}