unix_timer.rs
This file
Path
src/timer/unix_timer.rs
89 lines · 2.5 KiB · Rust
Last changed by
Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
History of this fileTake it
The walk stopped after 200 commits: lines older than 94322c5 point at it.
Bump versionDavid Peterbefore adoption94322c5
lines 1–2why, one step back1#![cfg(not(windows))]2Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 3–6why, one step back3use std::io;4use std::mem::MaybeUninit;5use std::os::unix::process::ExitStatusExt;6use std::process::{Child, ExitStatus};Bump versionDavid Peterbefore adoption94322c5
line 7why, one step back7Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
line 8why, one step back8use anyhow::Result;Bump versionDavid Peterbefore adoption94322c5
line 9why, one step back9Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 10–11why, one step back10use crate::quantity::{byte, kibibyte, microsecond, second, Information, Time};11Bump versionDavid Peterbefore adoption94322c5
line 12why, one step back12#[derive(Debug, Copy, Clone)]Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
line 13why, one step back13struct ResourceUsage {Bump versionDavid Peterbefore adoption94322c5
line 14why, one step back14 /// Total amount of time spent executing in user modeMerge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
line 15why, one step back15 pub time_user: Time,Bump versionDavid Peterbefore adoption94322c5
lines 16–17why, one step back1617 /// Total amount of time spent executing in kernel modeMerge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
line 18why, one step back18 pub time_system: Time,Bump versionDavid Peterbefore adoption94322c5
line 19why, one step back19Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 20–21why, one step back20 /// Maximum amount of memory used by the process, in bytes21 pub memory_usage: Information,Bump versionDavid Peterbefore adoption94322c5
lines 22–23why, one step back22}23Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 24–27why, one step back24#[allow(clippy::useless_conversion)]25fn convert_timeval(tv: libc::timeval) -> Time {26 let sec = tv.tv_sec as f64;27 let usec = tv.tv_usec as f64;Bump versionDavid Peterbefore adoption94322c5
line 28why, one step back28Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
line 29why, one step back29 Time::new::<second>(sec) + Time::new::<microsecond>(usec)Bump versionDavid Peterbefore adoption94322c5
lines 30–31why, one step back30}31Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 32–34why, one step back32#[allow(clippy::useless_conversion)]33fn wait4(mut child: Child) -> io::Result<(ExitStatus, ResourceUsage)> {34 drop(child.stdin.take());Bump versionDavid Peterbefore adoption94322c5
line 35why, one step back35Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 36–38why, one step back36 let pid = child.id() as i32;37 let mut status = 0;38 let mut rusage = MaybeUninit::zeroed();Bump versionDavid Peterbefore adoption94322c5
line 39why, one step back39Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 40–49why, one step back40 loop {41 let result = unsafe { libc::wait4(pid, &mut status, 0, rusage.as_mut_ptr()) };42 if result >= 0 {43 break;44 }45 let error = io::Error::last_os_error();46 if error.kind() != io::ErrorKind::Interrupted {47 return Err(error);48 }49 }Bump versionDavid Peterbefore adoption94322c5
line 50why, one step back50Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 51–58why, one step back51 let rusage = unsafe { rusage.assume_init() };5253 let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {54 // Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes55 // A f64 can represent integers up to 2^53 exactly, so we can represent56 // all values up to 2^53 bytes = 8 PiB exactly. Beyond that, our precision57 // is larger than 1 byte, but that's considered acceptable.58 Information::new::<byte>(rusage.ru_maxrss as f64)Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 59why, one step back59 } else {Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
line 60why, one step back60 Information::new::<kibibyte>(rusage.ru_maxrss as f64)Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
lines 61–62why, one step back61 };62Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 63–70why, one step back63 Ok((64 ExitStatus::from_raw(status),65 ResourceUsage {66 time_user: convert_timeval(rusage.ru_utime),67 time_system: convert_timeval(rusage.ru_stime),68 memory_usage: memory_usage_byte.into(),69 },70 ))Bump versionDavid Peterbefore adoption94322c5
lines 71–72why, one step back71}72Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 73–77why, one step back73pub struct CPUTimer {}7475impl CPUTimer {76 pub fn start() -> Self {77 Self {}Bump versionDavid Peterbefore adoption94322c5
lines 78–79why, one step back78 }79Merge pull request #965 from sharkdp/codex/wait4-resource-usageDavid Peterbefore adoptione43bf85
lines 80–88why, one step back80 pub fn stop(&self, child: Child) -> Result<(Time, Time, Information, ExitStatus)> {81 let (status, usage) = wait4(child)?;82 Ok((83 usage.time_user,84 usage.time_system,85 usage.memory_usage,86 status,87 ))88 }Bump versionDavid Peterbefore adoption94322c5
line 89why, one step back89}