Move to timer module

This commit is contained in:
David Peter authored and David Peter committed 2022-02-22 13:24:05 +01:00
1 parent 1f3943e7d1
commit 0ffc6d7cc0
6 files changed
+61 -62

No files matched your search

+50 -11
View File
@@ -1,15 +1,19 @@
use crate::util::units::Second;
pub mod wallclocktimer;
pub mod wall_clock_timer;
#[cfg(windows)]
pub mod windows_timer;
mod windows_timer;
#[cfg(not(windows))]
pub mod unix_timer;
mod unix_timer;
use crate::util::units::Second;
use std::process::{Command, ExitStatus};
use anyhow::{Context, Result};
#[derive(Debug, Copy, Clone)]
pub struct CPUTimes {
struct CPUTimes {
/// Total amount of time spent executing in user mode
pub user_usec: i64,
@@ -17,11 +21,46 @@ pub struct CPUTimes {
pub system_usec: i64,
}
/// Used to indicate the result of running a command
#[derive(Debug, Copy, Clone)]
pub struct CPUInterval {
/// Total amount of time spent executing in user mode
pub user: Second,
pub struct ExecuteResult {
/// The amount of user time the process used
pub user_time: Second,
/// Total amount of time spent executing in kernel mode
pub system: Second,
/// The amount of cpu time the process used
pub system_time: Second,
/// The exit status of the process
pub status: ExitStatus,
}
/// Execute the given command and return a timing summary
#[cfg(not(windows))]
pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {
let cpu_timer = self::unix_timer::CPUTimer::start();
let status = command
.status()
.with_context(|| error_message.to_string())?;
let (user_time, system_time) = cpu_timer.stop();
Ok(ExecuteResult {
user_time,
system_time,
status,
})
}
/// Execute the given command and return a timing summary
#[cfg(windows)]
pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {
let mut child = command.spawn().with_context(|| error_message.to_string())?;
let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);
let status = child.wait()?;
let (user_time, system_time) = cpu_timer.stop();
Ok(ExecuteResult {
user_time,
system_time,
status,
})
}