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 b989a2a point at it.

Add new unreleased sectionDavid Peterbefore adoptionb989a2a

lines 1–15why, one step back
1mod 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")]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

Add new unreleased sectionDavid Peterbefore adoptionb989a2a

lines 19–26why, one step back
19use crate::util::units::Second;
20use wall_clock_timer::WallClockTimer;2122use std::io::Read;23use std::process::{ChildStdout, Command, ExitStatus};2425use anyhow::Result;26

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Add new unreleased sectionDavid Peterbefore adoptionb989a2a

lines 28–83why, 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}4748/// 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 }6970 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;75 }76 }77}7879/// Execute the given command and return a timing summary80pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {81 #[cfg(not(windows))]82 let cpu_timer = self::unix_timer::CPUTimer::start();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();

Add new unreleased sectionDavid Peterbefore adoptionb989a2a

lines 93–95why, one step back
93 let mut child = command.spawn()?;
9495 #[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 };

Add new unreleased sectionDavid Peterbefore adoptionb989a2a

lines 100–108why, one step back
100
101 if let Some(output) = child.stdout.take() {102 // Handle CommandOutputPolicy::Pipe103 discard(output);104 }105106 let status = child.wait()?;107108 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();

Add new unreleased sectionDavid Peterbefore adoptionb989a2a

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