Files
hyperfine/src/timer/mod.rs
T

117 lines
2.8 KiB
Rust
Raw Normal View History

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;
2022-02-22 11:34:30 +01:00
use crate::util::units::Second;
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 {
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(
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
2022-02-22 12:56:56 +01:00
pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {
#[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()?;
#[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-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()?;
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 {
time_real,
time_user,
time_system,
2022-02-22 11:34:30 +01:00
status,
})
2021-08-24 21:48:10 +02:00
}