Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 23d9fe2 point at it.

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 1–8why, one step back
1mod wall_clock_timer;
23#[cfg(windows)]4mod windows_timer;56#[cfg(not(windows))]7mod unix_timer;8

Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c

lines 9–15why, one step back
9#[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;15

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 16–18why, one step back
16#[cfg(target_os = "windows")]
17use windows_sys::Win32::System::Threading::CREATE_SUSPENDED;18

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 19–21why, one step back
19use crate::util::units::Second;
20use wall_clock_timer::WallClockTimer;21

Address review commentsTavian Barnesbefore adoption3df63ba

line 22why, one step back
22use std::io::Read;

Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c

line 23why, one step back
23use std::process::{ChildStdout, Command, ExitStatus};

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 24–26why, one step back
24
25use anyhow::Result;26

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 27why, one step back
27#[cfg(not(windows))]

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 28–47why, one step back
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,35}3637/// Used to indicate the result of running a command38#[derive(Debug, Copy, Clone)]39pub struct TimerResult {40 pub time_real: Second,41 pub time_user: Second,42 pub time_system: Second,4344 /// The exit status of the process45 pub status: ExitStatus,46}47

Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c

lines 48–69why, one step back
48/// 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 }69

Address review commentsTavian Barnesbefore adoption3df63ba

lines 70–74why, one step back
70 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 back
75 }
76 }77}78

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 79–80why, one step back
79/// Execute the given command and return a timing summary
80pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {

Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c

lines 81–82why, one step back
81 #[cfg(not(windows))]
82 let cpu_timer = self::unix_timer::CPUTimer::start();

Address review commentsTavian Barnesbefore adoption3df63ba

line 83why, one step back
83

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 84–87why, one step back
84 #[cfg(windows)]
85 {86 use std::os::windows::process::CommandExt;87

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 88–89why, one step back
88 // 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 back
90 }
9192 let wallclock_timer = WallClockTimer::start();

Address review commentsTavian Barnesbefore adoption3df63ba

lines 93–94why, one step back
93 let mut child = command.spawn()?;
94

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

line 95why, one step back
95 #[cfg(windows)]

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 96–99why, one step back
96 let cpu_timer = {
97 // SAFETY: We created a suspended process98 unsafe { self::windows_timer::CPUTimer::start_suspended_process(&child) }99 };

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

line 100why, one step back
100

Address review commentsTavian Barnesbefore adoption3df63ba

lines 101–102why, one step back
101 if let Some(output) = child.stdout.take() {
102 // Handle CommandOutputPolicy::Pipe

Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c

lines 103–104why, one step back
103 discard(output);
104 }

Address review commentsTavian Barnesbefore adoption3df63ba

line 105why, one step back
105

Implement --output={null,pipe,<FILE>}Tavian Barnesbefore adoption277807c

line 106why, one step back
106 let status = child.wait()?;

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 107–108why, one step back
107
108 let time_real = wallclock_timer.stop();

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 109why, one step back
109 let (time_user, time_system) = cpu_timer.stop();

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 110–117why, one step back
110
111 Ok(TimerResult {112 time_real,113 time_user,114 time_system,115 status,116 })117}