Files
hyperfine/src/timer/mod.rs
T

110 lines
2.7 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;
use crate::benchmark::measurement::Measurement;
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;
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
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,
2024-06-23 13:29:05 +02:00
/// Maximum amount of memory used by the process, in bytes
pub memory_usage_byte: u64,
2021-08-24 21:48:10 +02:00
}
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
pub fn execute_and_measure(mut command: Command) -> Result<Measurement> {
#[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
2026-10-05 10:09:41 +02:00
let exit_status = child.wait()?;
2026-10-05 10:09:41 +02:00
let time_wall_clock = wallclock_timer.stop();
let (time_user, time_system, peak_memory_usage) = cpu_timer.stop();
2022-02-22 11:34:30 +01:00
Ok(Measurement {
2026-10-05 10:09:41 +02:00
time_wall_clock,
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
}