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 fileTake it
The walk stopped after 200 commits: lines older than b6ff23a point at it.
Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
line 1why, one step back1mod wall_clock_timer;Update outdated commentDavid Peterbefore adoptionb6ff23a
lines 2–3why, one step back23#[cfg(windows)]Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 4why, one step back4mod windows_timer;Update outdated commentDavid Peterbefore adoptionb6ff23a
lines 5–6why, one step back56#[cfg(not(windows))]Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 7why, one step back7mod unix_timer;Update outdated commentDavid Peterbefore adoptionb6ff23a
line 8why, one step back8Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
lines 9–15why, one step back9#[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;15Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 16–18why, one step back16#[cfg(target_os = "windows")]17use windows_sys::Win32::System::Threading::CREATE_SUSPENDED;18Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 19why, one step back19use crate::util::units::Second;Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
line 20why, one step back20use wall_clock_timer::WallClockTimer;Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 21why, one step back21Address review commentsTavian Barnesbefore adoption3df63ba
line 22why, one step back22use std::io::Read;Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
line 23why, one step back23use std::process::{ChildStdout, Command, ExitStatus};Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 24why, one step back24Further simplify execute_and_measureDavid Peterbefore adoptione1da8ba
line 25why, one step back25use anyhow::Result;Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 26why, one step back26Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 27why, one step back27#[cfg(not(windows))]Update outdated commentDavid Peterbefore adoptionb6ff23a
line 28why, one step back28#[derive(Debug, Copy, Clone)]Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 29why, one step back29struct CPUTimes {Update outdated commentDavid Peterbefore adoptionb6ff23a
lines 30–36why, one step back30 /// 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}36Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 37why, one step back37/// Used to indicate the result of running a commandUpdate outdated commentDavid Peterbefore adoptionb6ff23a
line 38why, one step back38#[derive(Debug, Copy, Clone)]Rename to TimerResultDavid Peterbefore adoption7fd4aea
line 39why, one step back39pub struct TimerResult {Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
lines 40–42why, one step back40 pub time_real: Second,41 pub time_user: Second,42 pub time_system: Second,Update outdated commentDavid Peterbefore adoptionb6ff23a
line 43why, one step back43Move to timer moduleDavid Peterbefore adoption0ffc6d7
lines 44–47why, one step back44 /// The exit status of the process45 pub status: ExitStatus,46}47Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
lines 48–69why, one step back48/// 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 }69Address review commentsTavian Barnesbefore adoption3df63ba
lines 70–74why, one step back70 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;Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
lines 75–78why, one step back75 }76 }77}78Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 79why, one step back79/// Execute the given command and return a timing summaryRename to TimerResultDavid Peterbefore adoption7fd4aea
line 80why, one step back80pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
lines 81–82why, one step back81 #[cfg(not(windows))]82 let cpu_timer = self::unix_timer::CPUTimer::start();Address review commentsTavian Barnesbefore adoption3df63ba
line 83why, one step back83Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 84–87why, one step back84 #[cfg(windows)]85 {86 use std::os::windows::process::CommandExt;87Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 88–89why, one step back88 // 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);Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 90–92why, one step back90 }9192 let wallclock_timer = WallClockTimer::start();Address review commentsTavian Barnesbefore adoption3df63ba
lines 93–94why, one step back93 let mut child = command.spawn()?;94Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
line 95why, one step back95 #[cfg(windows)]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 96–99why, one step back96 let cpu_timer = {97 // SAFETY: We created a suspended process98 unsafe { self::windows_timer::CPUTimer::start_suspended_process(&child) }99 };Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 100why, one step back100Address review commentsTavian Barnesbefore adoption3df63ba
lines 101–102why, one step back101 if let Some(output) = child.stdout.take() {102 // Handle CommandOutputPolicy::PipeImplement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
lines 103–104why, one step back103 discard(output);104 }Address review commentsTavian Barnesbefore adoption3df63ba
line 105why, one step back105Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
line 106why, one step back106 let status = child.wait()?;Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
lines 107–108why, one step back107108 let time_real = wallclock_timer.stop();Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 109why, one step back109 let (time_user, time_system) = cpu_timer.stop();Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
line 110why, one step back110Rename to TimerResultDavid Peterbefore adoption7fd4aea
line 111why, one step back111 Ok(TimerResult {Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
lines 112–114why, one step back112 time_real,113 time_user,114 time_system,Move to timer moduleDavid Peterbefore adoption0ffc6d7
lines 115–116why, one step back115 status,116 })Update outdated commentDavid Peterbefore adoptionb6ff23a
line 117why, one step back117}