Skip to content
Myr

mod.rs

This file

Path

src/timer/mod.rs

64 lines · 1.5 KiB · Rust

Last changed by

Rename to TimerResultDavid Peterbefore adoption7fd4aea

History of this file

Take it

1mod wall_clock_timer;23#[cfg(windows)]4mod windows_timer;56#[cfg(not(windows))]7mod unix_timer;89use crate::util::units::Second;10use wall_clock_timer::WallClockTimer;1112use std::process::{Command, ExitStatus};1314use anyhow::Result;1516#[derive(Debug, Copy, Clone)]17struct CPUTimes {18    /// Total amount of time spent executing in user mode19    pub user_usec: i64,2021    /// Total amount of time spent executing in kernel mode22    pub system_usec: i64,23}2425/// Used to indicate the result of running a command26#[derive(Debug, Copy, Clone)]27pub struct TimerResult {28    pub time_real: Second,29    pub time_user: Second,30    pub time_system: Second,3132    /// The exit status of the process33    pub status: ExitStatus,34}3536/// Execute the given command and return a timing summary37pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {38    let wallclock_timer = WallClockTimer::start();3940    #[cfg(not(windows))]41    let ((time_user, time_system), status) = {42        let cpu_timer = self::unix_timer::CPUTimer::start();43        let status = command.status()?;44        (cpu_timer.stop(), status)45    };4647    #[cfg(windows)]48    let ((time_user, time_system), status) = {49        let mut child = command.spawn()?;50        let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);51        let status = child.wait()?;5253        (cpu_timer.stop(), status)54    };5556    let time_real = wallclock_timer.stop();5758    Ok(TimerResult {59        time_real,60        time_user,61        time_system,62        status,63    })64}