Skip to content
Myr

mod.rs

This file

Path

src/timer/mod.rs

121 lines · 3.0 KiB · Rust

Last changed by

Initial work on tracking memory usageDavid Peterbefore adoption6556b2b

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::fd::AsFd;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,3536    /// Maximum amount of memory used by the process, in bytes37    pub memory_usage_byte: u64,38}3940/// Used to indicate the result of running a command41#[derive(Debug, Copy, Clone)]42pub struct TimerResult {43    pub time_real: Second,44    pub time_user: Second,45    pub time_system: Second,46    pub memory_usage_byte: u64,47    /// The exit status of the process48    pub status: ExitStatus,49}5051/// Discard the output of a child process.52fn discard(output: ChildStdout) {53    const CHUNK_SIZE: usize = 64 << 10;5455    #[cfg(target_os = "linux")]56    {57        if let Ok(file) = File::create("/dev/null") {58            while let Ok(bytes) = splice(59                output.as_fd(),60                None,61                file.as_fd(),62                None,63                CHUNK_SIZE,64                SpliceFFlags::empty(),65            ) {66                if bytes == 0 {67                    break;68                }69            }70        }71    }7273    let mut output = output;74    let mut buf = [0; CHUNK_SIZE];75    while let Ok(bytes) = output.read(&mut buf) {76        if bytes == 0 {77            break;78        }79    }80}8182/// Execute the given command and return a timing summary83pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {84    #[cfg(not(windows))]85    let cpu_timer = self::unix_timer::CPUTimer::start();8687    #[cfg(windows)]88    {89        use std::os::windows::process::CommandExt;9091        // Create the process in a suspended state so that we don't miss any cpu time between process creation and `CPUTimer` start.92        command.creation_flags(CREATE_SUSPENDED);93    }9495    let wallclock_timer = WallClockTimer::start();96    let mut child = command.spawn()?;9798    #[cfg(windows)]99    let cpu_timer = {100        // SAFETY: We created a suspended process101        unsafe { self::windows_timer::CPUTimer::start_suspended_process(&child) }102    };103104    if let Some(output) = child.stdout.take() {105        // Handle CommandOutputPolicy::Pipe106        discard(output);107    }108109    let status = child.wait()?;110111    let time_real = wallclock_timer.stop();112    let (time_user, time_system, memory_usage_byte) = cpu_timer.stop();113114    Ok(TimerResult {115        time_real,116        time_user,117        time_system,118        memory_usage_byte,119        status,120    })121}