Skip to content
Myr

mod.rs

This file

Path

src/timer/mod.rs

128 lines · 3.5 KiB · Rust

Last changed by

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

History of this file

Take it

1mod wall_clock_timer;23#[cfg(target_os = "linux")]4mod linux_counters;5#[cfg(target_os = "macos")]6mod macos_counters;78#[cfg(windows)]9mod windows_timer;1011#[cfg(not(windows))]12mod unix_timer;1314#[cfg(target_os = "linux")]15use nix::fcntl::{splice, SpliceFFlags};16#[cfg(target_os = "linux")]17use std::fs::File;18#[cfg(target_os = "linux")]19use std::os::fd::AsFd;2021#[cfg(target_os = "windows")]22use windows_sys::Win32::System::Threading::CREATE_SUSPENDED;2324use crate::benchmark::measurement::Measurement;25use wall_clock_timer::WallClockTimer;2627use std::io::Read;28use std::process::{ChildStdout, Command};2930use anyhow::Result;3132/// Discard the output of a child process.33fn discard(output: ChildStdout) {34    const CHUNK_SIZE: usize = 64 << 10;3536    #[cfg(target_os = "linux")]37    {38        if let Ok(file) = File::create("/dev/null") {39            while let Ok(bytes) = splice(40                output.as_fd(),41                None,42                file.as_fd(),43                None,44                CHUNK_SIZE,45                SpliceFFlags::empty(),46            ) {47                if bytes == 0 {48                    break;49                }50            }51        }52    }5354    let mut output = output;55    let mut buf = [0; CHUNK_SIZE];56    while let Ok(bytes) = output.read(&mut buf) {57        if bytes == 0 {58            break;59        }60    }61}6263/// Execute the given command and return a timing summary64pub fn execute_and_measure(65    mut command: Command,66    _collect_hardware_counters: bool,67) -> Result<Measurement> {68    #[cfg(target_os = "linux")]69    let counters = _collect_hardware_counters.then(linux_counters::CounterGroup::new);70    #[cfg(not(windows))]71    let cpu_timer = self::unix_timer::CPUTimer::start();7273    #[cfg(windows)]74    {75        use std::os::windows::process::CommandExt;7677        // Create the process in a suspended state so that we don't miss any cpu time between process creation and `CPUTimer` start.78        command.creation_flags(CREATE_SUSPENDED);79    }8081    let wallclock_timer = WallClockTimer::start();82    let mut child = command.spawn()?;8384    #[cfg(windows)]85    let cpu_timer = {86        // SAFETY: We created a suspended process87        unsafe { self::windows_timer::CPUTimer::start_suspended_process(&child) }88    };8990    if let Some(output) = child.stdout.take() {91        // Handle CommandOutputPolicy::Pipe92        discard(output);93    }9495    #[cfg(target_os = "macos")]96    let (time_wall_clock, hardware_counters) =97        if _collect_hardware_counters && macos_counters::wait(&mut child).is_ok() {98            // Stop timing before querying counters, but leave the child available for wait4.99            (100                Some(wallclock_timer.stop()),101                macos_counters::read(child.id()),102            )103        } else {104            (None, Default::default())105        };106    let (time_user, time_system, memory_peak_resident, exit_status) = cpu_timer.stop(child)?;107    #[cfg(target_os = "macos")]108    let time_wall_clock = time_wall_clock.unwrap_or_else(|| wallclock_timer.stop());109    #[cfg(not(target_os = "macos"))]110    let time_wall_clock = wallclock_timer.stop();111112    #[cfg(target_os = "linux")]113    let hardware_counters = counters114        .map(linux_counters::CounterGroup::read)115        .unwrap_or_default();116    #[cfg(not(any(target_os = "linux", target_os = "macos")))]117    let hardware_counters = Default::default();118119    Ok(Measurement {120        time_wall_clock,121        time_cpu: time_user + time_system,122        time_user,123        time_system,124        memory_peak_resident,125        hardware_counters,126        exit_status,127    })128}