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 fileTake it
The walk stopped after 200 commits: lines older than 3013c9d point at it.
Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 1–13why, one step back1mod 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")]Update dependenciesDavid Peterbefore adoption2efc633
line 14why, one step back14use std::os::fd::AsFd;Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
line 15why, one step back15Use 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;18Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 19–26why, one step back19use crate::util::units::Second;20use wall_clock_timer::WallClockTimer;2122use std::io::Read;23use std::process::{ChildStdout, Command, ExitStatus};2425use anyhow::Result;26Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 27why, one step back27#[cfg(not(windows))]Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 28–34why, one step back28#[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,Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
lines 35–37why, one step back3536 /// Maximum amount of memory used by the process, in bytes37 pub memory_usage_byte: u64,Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 38–45why, one step back38}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,Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 46why, one step back46 pub memory_usage_byte: u64,Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 47–58why, one step back47 /// 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(Update dependenciesDavid Peterbefore adoption2efc633
line 59why, one step back59 output.as_fd(),Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
line 60why, one step back60 None,Update dependenciesDavid Peterbefore adoption2efc633
line 61why, one step back61 file.as_fd(),Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 62–86why, one step back62 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();86Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 87–90why, one step back87 #[cfg(windows)]88 {89 use std::os::windows::process::CommandExt;90Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 91–92why, one step back91 // 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);Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 93–95why, one step back93 }9495 let wallclock_timer = WallClockTimer::start();Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 96–98why, one step back96 let mut child = command.spawn()?;9798 #[cfg(windows)]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 99–102why, one step back99 let cpu_timer = {100 // SAFETY: We created a suspended process101 unsafe { self::windows_timer::CPUTimer::start_suspended_process(&child) }102 };Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 103–111why, one step back103104 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();Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 112why, one step back112 let (time_user, time_system, memory_usage_byte) = cpu_timer.stop();Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 113–117why, one step back113114 Ok(TimerResult {115 time_real,116 time_user,117 time_system,Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 118why, one step back118 memory_usage_byte,Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 119–121why, one step back119 status,120 })121}