Skip to content
Myr

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 file

Take it

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

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

line 1why, one step back
1mod wall_clock_timer;

chore: add funtoo linux instructionsinvakid404before adoptionf5db783

lines 2–3why, one step back
2
3#[cfg(windows)]

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 4why, one step back
4mod windows_timer;

chore: add funtoo linux instructionsinvakid404before adoptionf5db783

lines 5–6why, one step back
5
6#[cfg(not(windows))]

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 7why, one step back
7mod unix_timer;

chore: add funtoo linux instructionsinvakid404before adoptionf5db783

line 8why, one step back
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

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 16why, one step back
16use crate::util::units::Second;

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

line 17why, one step back
17use wall_clock_timer::WallClockTimer;

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 18why, one step back
18

Address review commentsTavian Barnesbefore adoption3df63ba

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

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

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

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 21why, one step back
21

Further simplify execute_and_measureDavid Peterbefore adoptione1da8ba

line 22why, one step back
22use anyhow::Result;

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 23why, one step back
23

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

chore: add funtoo linux instructionsinvakid404before adoptionf5db783

line 25why, one step back
25#[derive(Debug, Copy, Clone)]

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 26why, one step back
26struct CPUTimes {

chore: add funtoo linux instructionsinvakid404before adoptionf5db783

lines 27–33why, one step back
27 /// Total amount of time spent executing in user mode
28 pub user_usec: i64,2930 /// Total amount of time spent executing in kernel mode31 pub system_usec: i64,32}33

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 34why, one step back
34/// Used to indicate the result of running a command

chore: add funtoo linux instructionsinvakid404before adoptionf5db783

line 35why, one step back
35#[derive(Debug, Copy, Clone)]

Rename to TimerResultDavid Peterbefore adoption7fd4aea

line 36why, one step back
36pub struct TimerResult {

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

lines 37–39why, one step back
37 pub time_real: Second,
38 pub time_user: Second,39 pub time_system: Second,

chore: add funtoo linux instructionsinvakid404before adoptionf5db783

line 40why, one step back
40

Move to timer moduleDavid Peterbefore adoption0ffc6d7

lines 41–44why, one step back
41 /// The exit status of the process
42 pub status: ExitStatus,43}44

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

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

Address review commentsTavian Barnesbefore adoption3df63ba

lines 67–71why, one step back
67 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 back
72 }
73 }74}75

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 76why, one step back
76/// Execute the given command and return a timing summary

Rename to TimerResultDavid Peterbefore adoption7fd4aea

line 77why, one step back
77pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {

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

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

Address review commentsTavian Barnesbefore adoption3df63ba

line 80why, one step back
80

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 81–89why, one step back
81 #[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 back
90 let mut child = command.spawn()?;
91

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 93–96why, one step back
93 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 back
97

Address review commentsTavian Barnesbefore adoption3df63ba

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

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

lines 100–101why, one step back
100 discard(output);
101 }

Address review commentsTavian Barnesbefore adoption3df63ba

line 102why, one step back
102

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

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

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

line 107why, one step back
107

Rename to TimerResultDavid Peterbefore adoption7fd4aea

line 108why, one step back
108 Ok(TimerResult {

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

lines 109–111why, one step back
109 time_real,
110 time_user,111 time_system,

Move to timer moduleDavid Peterbefore adoption0ffc6d7

lines 112–113why, one step back
112 status,
113 })

chore: add funtoo linux instructionsinvakid404before adoptionf5db783

line 114why, one step back
114}