mod.rs
This file
Path
src/timer/mod.rs
114 lines · 2.6 KiB · Rust
Last changed by
Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
History of this fileTake it
The walk stopped after 200 commits: lines older than f5de548 point at it.
Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
line 1why, one step back1mod wall_clock_timer;Fix clippy suggestionsDavid Peterbefore adoptionf5de548
lines 2–3why, one step back23#[cfg(windows)]Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 4why, one step back4mod windows_timer;Fix clippy suggestionsDavid Peterbefore adoptionf5de548
lines 5–6why, one step back56#[cfg(not(windows))]Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 7why, one step back7mod unix_timer;Fix clippy suggestionsDavid Peterbefore adoptionf5de548
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;15Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 16why, one step back16use crate::util::units::Second;Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
line 17why, one step back17use wall_clock_timer::WallClockTimer;Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 18why, one step back18Address review commentsTavian Barnesbefore adoption3df63ba
line 19why, one step back19use std::io::Read;Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
line 20why, one step back20use std::process::{ChildStdout, Command, ExitStatus};Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 21why, one step back21Further simplify execute_and_measureDavid Peterbefore adoptione1da8ba
line 22why, one step back22use anyhow::Result;Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 23why, one step back23Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 24why, one step back24#[cfg(not(windows))]Fix clippy suggestionsDavid Peterbefore adoptionf5de548
line 25why, one step back25#[derive(Debug, Copy, Clone)]Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 26why, one step back26struct CPUTimes {Fix clippy suggestionsDavid Peterbefore adoptionf5de548
lines 27–33why, one step back27 /// Total amount of time spent executing in user mode28 pub user_usec: i64,2930 /// Total amount of time spent executing in kernel mode31 pub system_usec: i64,32}33Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 34why, one step back34/// Used to indicate the result of running a commandFix clippy suggestionsDavid Peterbefore adoptionf5de548
line 35why, one step back35#[derive(Debug, Copy, Clone)]Rename to TimerResultDavid Peterbefore adoption7fd4aea
line 36why, one step back36pub struct TimerResult {Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
lines 37–39why, one step back37 pub time_real: Second,38 pub time_user: Second,39 pub time_system: Second,Fix clippy suggestionsDavid Peterbefore adoptionf5de548
line 40why, one step back40Move to timer moduleDavid Peterbefore adoption0ffc6d7
lines 41–44why, one step back41 /// The exit status of the process42 pub status: ExitStatus,43}44Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
lines 45–66why, one step back45/// Discard the output of a child process.46fn discard(output: ChildStdout) {47 const CHUNK_SIZE: usize = 64 << 10;4849 #[cfg(target_os = "linux")]50 {51 if let Ok(file) = File::create("/dev/null") {52 while let Ok(bytes) = splice(53 output.as_raw_fd(),54 None,55 file.as_raw_fd(),56 None,57 CHUNK_SIZE,58 SpliceFFlags::empty(),59 ) {60 if bytes == 0 {61 break;62 }63 }64 }65 }66Address review commentsTavian Barnesbefore adoption3df63ba
lines 67–71why, one step back67 let mut output = output;68 let mut buf = [0; CHUNK_SIZE];69 while let Ok(bytes) = output.read(&mut buf) {70 if bytes == 0 {71 break;Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
lines 72–75why, one step back72 }73 }74}75Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 76why, one step back76/// Execute the given command and return a timing summaryRename to TimerResultDavid Peterbefore adoption7fd4aea
line 77why, one step back77pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
lines 78–79why, one step back78 #[cfg(not(windows))]79 let cpu_timer = self::unix_timer::CPUTimer::start();Address review commentsTavian Barnesbefore adoption3df63ba
line 80why, one step back80Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 81–89why, one step back81 #[cfg(windows)]82 {83 use std::os::windows::process::CommandExt;8485 // Create a suspended process86 command.creation_flags(4);87 }8889 let wallclock_timer = WallClockTimer::start();Address review commentsTavian Barnesbefore adoption3df63ba
lines 90–91why, one step back90 let mut child = command.spawn()?;91Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
line 92why, one step back92 #[cfg(windows)]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 93–96why, one step back93 let cpu_timer = {94 // SAFETY: We created a suspended process95 unsafe { self::windows_timer::CPUTimer::start_suspended_process(&child) }96 };Move to timer moduleDavid Peterbefore adoption0ffc6d7
line 97why, one step back97Address review commentsTavian Barnesbefore adoption3df63ba
lines 98–99why, one step back98 if let Some(output) = child.stdout.take() {99 // Handle CommandOutputPolicy::PipeImplement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
lines 100–101why, one step back100 discard(output);101 }Address review commentsTavian Barnesbefore adoption3df63ba
line 102why, one step back102Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c
line 103why, one step back103 let status = child.wait()?;Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
lines 104–105why, one step back104105 let time_real = wallclock_timer.stop();Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 106why, one step back106 let (time_user, time_system) = cpu_timer.stop();Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
line 107why, one step back107Rename to TimerResultDavid Peterbefore adoption7fd4aea
line 108why, one step back108 Ok(TimerResult {Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6
lines 109–111why, one step back109 time_real,110 time_user,111 time_system,Move to timer moduleDavid Peterbefore adoption0ffc6d7
lines 112–113why, one step back112 status,113 })Fix clippy suggestionsDavid Peterbefore adoptionf5de548
line 114why, one step back114}