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")]
|
2024-11-10 22:37:04 +01:00
|
|
|
use std::os::fd::AsFd;
|
2022-05-14 13:21:41 -04:00
|
|
|
|
2023-04-17 22:12:48 +02:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
use windows_sys::Win32::System::Threading::CREATE_SUSPENDED;
|
|
|
|
|
|
2026-10-05 09:47:09 +02:00
|
|
|
use crate::benchmark::measurement::Measurement;
|
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;
|
2026-10-05 09:47:09 +02:00
|
|
|
use std::process::{ChildStdout, Command};
|
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
|
|
|
|
2026-10-05 14:14:25 +02:00
|
|
|
/// Used to indicate the result of running a command
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
pub struct TimerResult {
|
|
|
|
|
pub time_real: Second,
|
|
|
|
|
pub time_user: Second,
|
|
|
|
|
pub time_system: Second,
|
|
|
|
|
pub memory_usage_byte: u64,
|
|
|
|
|
/// 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(
|
2024-11-10 22:37:04 +01:00
|
|
|
output.as_fd(),
|
2022-05-14 13:21:41 -04:00
|
|
|
None,
|
2024-11-10 22:37:04 +01:00
|
|
|
file.as_fd(),
|
2022-05-14 13:21:41 -04:00
|
|
|
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
|
2026-10-05 09:47:09 +02:00
|
|
|
pub fn execute_and_measure(mut command: Command) -> Result<Measurement> {
|
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;
|
|
|
|
|
|
2023-04-17 22:12:48 +02:00
|
|
|
// Create the process in a suspended state so that we don't miss any cpu time between process creation and `CPUTimer` start.
|
|
|
|
|
command.creation_flags(CREATE_SUSPENDED);
|
2022-08-08 16:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2026-10-05 14:14:25 +02:00
|
|
|
let (status, time_user, time_system, memory_usage_byte) = cpu_timer.stop(child)?;
|
|
|
|
|
let time_real = wallclock_timer.stop();
|
2026-10-05 11:15:39 +02:00
|
|
|
|
2026-10-05 09:47:09 +02:00
|
|
|
Ok(Measurement {
|
2026-10-05 10:09:41 +02:00
|
|
|
time_wall_clock,
|
2022-02-22 12:56:04 +01:00
|
|
|
time_user,
|
|
|
|
|
time_system,
|
2026-10-05 10:09:41 +02:00
|
|
|
peak_memory_usage,
|
|
|
|
|
exit_status,
|
2022-02-22 11:34:30 +01:00
|
|
|
})
|
2021-08-24 21:48:10 +02:00
|
|
|
}
|