Skip to content
Myr

mod.rs

This file

Path

src/timer/mod.rs

64 lines · 1.5 KiB · Rust

Last changed by

Further simplify execute_and_measureDavid Peterbefore adoptione1da8ba

History of this file

Take it

1pub mod wall_clock_timer;23#[cfg(windows)]4mod windows_timer;56#[cfg(not(windows))]7mod unix_timer;89use crate::util::units::Second;1011use std::process::{Command, ExitStatus};1213use anyhow::Result;1415#[derive(Debug, Copy, Clone)]16struct CPUTimes {17    /// Total amount of time spent executing in user mode18    pub user_usec: i64,1920    /// Total amount of time spent executing in kernel mode21    pub system_usec: i64,22}2324/// Used to indicate the result of running a command25#[derive(Debug, Copy, Clone)]26pub struct ExecuteResult {27    /// The amount of user time the process used28    pub user_time: Second,2930    /// The amount of cpu time the process used31    pub system_time: Second,3233    /// The exit status of the process34    pub status: ExitStatus,35}3637/// Execute the given command and return a timing summary38#[cfg(not(windows))]39pub fn execute_and_measure(mut command: Command) -> Result<ExecuteResult> {40    let cpu_timer = self::unix_timer::CPUTimer::start();41    let status = command.status()?;42    let (user_time, system_time) = cpu_timer.stop();4344    Ok(ExecuteResult {45        user_time,46        system_time,47        status,48    })49}5051/// Execute the given command and return a timing summary52#[cfg(windows)]53pub fn execute_and_measure(mut command: Command) -> Result<ExecuteResult> {54    let mut child = command.spawn()?;55    let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);56    let status = child.wait()?;5758    let (user_time, system_time) = cpu_timer.stop();59    Ok(ExecuteResult {60        user_time,61        system_time,62        status,63    })64}