Skip to content
Myr

mod.rs

This file

Path

src/timer/mod.rs

117 lines · 2.8 KiB · Rust

Last changed by

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

History of this file

Take it

1mod wall_clock_timer;23#[cfg(windows)]4mod windows_timer;56#[cfg(not(windows))]7mod unix_timer;89#[cfg(target_os = "linux")]10use nix::fcntl::{splice, SpliceFFlags};11#[cfg(target_os = "linux")]12use std::fs::File;13#[cfg(target_os = "linux")]14use std::os::unix::io::AsRawFd;1516#[cfg(target_os = "windows")]17use windows_sys::Win32::System::Threading::CREATE_SUSPENDED;1819use crate::util::units::Second;20use wall_clock_timer::WallClockTimer;2122use std::io::Read;23use std::process::{ChildStdout, Command, ExitStatus};2425use anyhow::Result;2627#[cfg(not(windows))]28#[derive(Debug, Copy, Clone)]29struct CPUTimes {30    /// Total amount of time spent executing in user mode31    pub user_usec: i64,3233    /// Total amount of time spent executing in kernel mode34    pub system_usec: i64,35}3637/// Used to indicate the result of running a command38#[derive(Debug, Copy, Clone)]39pub struct TimerResult {40    pub time_real: Second,41    pub time_user: Second,42    pub time_system: Second,4344    /// The exit status of the process45    pub status: ExitStatus,46}4748/// Discard the output of a child process.49fn discard(output: ChildStdout) {50    const CHUNK_SIZE: usize = 64 << 10;5152    #[cfg(target_os = "linux")]53    {54        if let Ok(file) = File::create("/dev/null") {55            while let Ok(bytes) = splice(56                output.as_raw_fd(),57                None,58                file.as_raw_fd(),59                None,60                CHUNK_SIZE,61                SpliceFFlags::empty(),62            ) {63                if bytes == 0 {64                    break;65                }66            }67        }68    }6970    let mut output = output;71    let mut buf = [0; CHUNK_SIZE];72    while let Ok(bytes) = output.read(&mut buf) {73        if bytes == 0 {74            break;75        }76    }77}7879/// Execute the given command and return a timing summary80pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {81    #[cfg(not(windows))]82    let cpu_timer = self::unix_timer::CPUTimer::start();8384    #[cfg(windows)]85    {86        use std::os::windows::process::CommandExt;8788        // Create the process in a suspended state so that we don't miss any cpu time between process creation and `CPUTimer` start.89        command.creation_flags(CREATE_SUSPENDED);90    }9192    let wallclock_timer = WallClockTimer::start();93    let mut child = command.spawn()?;9495    #[cfg(windows)]96    let cpu_timer = {97        // SAFETY: We created a suspended process98        unsafe { self::windows_timer::CPUTimer::start_suspended_process(&child) }99    };100101    if let Some(output) = child.stdout.take() {102        // Handle CommandOutputPolicy::Pipe103        discard(output);104    }105106    let status = child.wait()?;107108    let time_real = wallclock_timer.stop();109    let (time_user, time_system) = cpu_timer.stop();110111    Ok(TimerResult {112        time_real,113        time_user,114        time_system,115        status,116    })117}