Skip to content
Myr

mod.rs

This file

Path

src/timer/mod.rs

103 lines · 2.4 KiB · Rust

Last changed by

Address review commentsTavian Barnesbefore adoption3df63ba

History of this file

Take it

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

line 1why, one step back
1mod wall_clock_timer;

Remove timer/internal.rsDavid Peterbefore adoption9d61130

line 2why, one step back
2

Remove cfg-if dependencyDavid Peterbefore adoption60e9771

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

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 4why, one step back
4mod windows_timer;

Flat directory/module structureDavid Peterbefore adoption30c381f

line 5why, one step back
5

Remove cfg-if dependencyDavid Peterbefore adoption60e9771

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

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 7why, one step back
7mod unix_timer;

Remove timer/internal.rsDavid Peterbefore adoption9d61130

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

Remove timer/internal.rsDavid Peterbefore adoption9d61130

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

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 25why, one step back
25struct CPUTimes {

Remove timer/internal.rsDavid Peterbefore adoption9d61130

lines 26–32why, one step back
26 /// Total amount of time spent executing in user mode
27 pub user_usec: i64,2829 /// Total amount of time spent executing in kernel mode30 pub system_usec: i64,31}32

Move to timer moduleDavid Peterbefore adoption0ffc6d7

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

Remove timer/internal.rsDavid Peterbefore adoption9d61130

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

Rename to TimerResultDavid Peterbefore adoption7fd4aea

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

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

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

Remove timer/internal.rsDavid Peterbefore adoption9d61130

line 39why, one step back
39

Move to timer moduleDavid Peterbefore adoption0ffc6d7

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

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

lines 44–65why, one step back
44/// Discard the output of a child process.
45fn discard(output: ChildStdout) {46 const CHUNK_SIZE: usize = 64 << 10;4748 #[cfg(target_os = "linux")]49 {50 if let Ok(file) = File::create("/dev/null") {51 while let Ok(bytes) = splice(52 output.as_raw_fd(),53 None,54 file.as_raw_fd(),55 None,56 CHUNK_SIZE,57 SpliceFFlags::empty(),58 ) {59 if bytes == 0 {60 break;61 }62 }63 }64 }65

Address review commentsTavian Barnesbefore adoption3df63ba

lines 66–70why, one step back
66 let mut output = output;
67 let mut buf = [0; CHUNK_SIZE];68 while let Ok(bytes) = output.read(&mut buf) {69 if bytes == 0 {70 break;

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

lines 71–74why, one step back
71 }
72 }73}74

Move to timer moduleDavid Peterbefore adoption0ffc6d7

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

Rename to TimerResultDavid Peterbefore adoption7fd4aea

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

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

line 77why, one step back
77 let wallclock_timer = WallClockTimer::start();

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 78why, one step back
78

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

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

Address review commentsTavian Barnesbefore adoption3df63ba

lines 81–83why, one step back
81
82 let mut child = command.spawn()?;83

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

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

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

line 85why, one step back
85 let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 86why, one step back
86

Address review commentsTavian Barnesbefore adoption3df63ba

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

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

lines 89–90why, one step back
89 discard(output);
90 }

Address review commentsTavian Barnesbefore adoption3df63ba

line 91why, one step back
91

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

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

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

line 93why, one step back
93

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

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

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

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

Rename to TimerResultDavid Peterbefore adoption7fd4aea

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

Move wall clock timer into execute_and_measureDavid Peterbefore adoption55f0ef6

lines 98–100why, one step back
98 time_real,
99 time_user,100 time_system,

Move to timer moduleDavid Peterbefore adoption0ffc6d7

lines 101–102why, one step back
101 status,
102 })

Flat directory/module structureDavid Peterbefore adoption30c381f

line 103why, one step back
103}