Compare commits

..
Author SHA1 Message Date
David Peter b5f58651a1 Extract quantity and command helpers 2026-10-05 11:14:09 +02:00
8 changed files with 124 additions and 92 deletions

No files matched your search

+1 -1
View File
@@ -266,7 +266,7 @@ impl Executor for ShellExecutor<'_> {
time_wall_clock: measurements.time_wall_clock_mean(), time_wall_clock: measurements.time_wall_clock_mean(),
time_user: measurements.time_user_mean(), time_user: measurements.time_user_mean(),
time_system: measurements.time_system_mean(), time_system: measurements.time_system_mean(),
peak_memory_usage: Information::zero(), peak_memory_usage: measurements.peak_memory_usage_mean(),
exit_status: ExitStatus::default(), exit_status: ExitStatus::default(),
}); });
+6 -7
View File
@@ -1,7 +1,7 @@
use std::process::ExitStatus; use std::process::ExitStatus;
use crate::quantity::statistics::{max, mean, median, min, modified_zscores, standard_deviation}; use crate::quantity::statistics::{max, mean, median, min, modified_zscores, standard_deviation};
use crate::quantity::{second, Information, Time}; use crate::quantity::{Information, Time};
/// Performance measurements and exit status from running a single command /// Performance measurements and exit status from running a single command
#[derive(Debug, Default, Copy, Clone)] #[derive(Debug, Default, Copy, Clone)]
@@ -77,12 +77,7 @@ impl Measurements {
/// Compute modified Z-scores for the wall clock times. /// Compute modified Z-scores for the wall clock times.
pub fn modified_zscores(&self) -> Vec<f64> { pub fn modified_zscores(&self) -> Vec<f64> {
modified_zscores( modified_zscores(&self.wall_clock_times().collect::<Vec<_>>())
&self
.wall_clock_times()
.map(|t| t.get::<second>())
.collect::<Vec<_>>(),
)
} }
/// The average user time. /// The average user time.
@@ -94,4 +89,8 @@ impl Measurements {
pub fn time_system_mean(&self) -> Time { pub fn time_system_mean(&self) -> Time {
mean(self.measurements.iter().map(|m| m.time_system)) mean(self.measurements.iter().map(|m| m.time_system))
} }
pub fn peak_memory_usage_mean(&self) -> Information {
mean(self.measurements.iter().map(|m| m.peak_memory_usage))
}
} }
+5 -1
View File
@@ -97,10 +97,14 @@ impl<'a> Command<'a> {
&self.parameters &self.parameters
} }
pub fn is_parameter_unused(&self, parameter: &str) -> bool {
!self.expression.contains(&format!("{{{parameter}}}"))
}
pub fn get_unused_parameters(&self) -> impl Iterator<Item = &(&'a str, ParameterValue)> { pub fn get_unused_parameters(&self) -> impl Iterator<Item = &(&'a str, ParameterValue)> {
self.parameters self.parameters
.iter() .iter()
.filter(move |(parameter, _)| !self.expression.contains(&format!("{{{parameter}}}"))) .filter(move |(parameter, _)| self.is_parameter_unused(parameter))
} }
fn replace_parameters_in(&self, original: &str) -> String { fn replace_parameters_in(&self, original: &str) -> String {
+2 -2
View File
@@ -14,13 +14,13 @@ pub const OUTLIER_THRESHOLD: f64 = 1.4826 * 10.0;
/// modified Z-score that is larger than `OUTLIER_THRESHOLD`. /// modified Z-score that is larger than `OUTLIER_THRESHOLD`.
#[cfg(test)] #[cfg(test)]
pub fn num_outliers(xs: &[f64]) -> usize { pub fn num_outliers(xs: &[f64]) -> usize {
use crate::quantity::statistics::modified_zscores; use crate::quantity::statistics::modified_zscores_f64;
if xs.is_empty() { if xs.is_empty() {
return 0; return 0;
} }
let scores = modified_zscores(xs); let scores = modified_zscores_f64(xs);
scores scores
.iter() .iter()
.filter(|&&s| s.abs() > OUTLIER_THRESHOLD) .filter(|&&s| s.abs() > OUTLIER_THRESHOLD)
+1 -3
View File
@@ -3,8 +3,6 @@ use std::marker::PhantomData;
pub use uom::num_traits::Zero; pub use uom::num_traits::Zero;
pub use uom::si::f64::{Information, Ratio, Time}; pub use uom::si::f64::{Information, Ratio, Time};
pub use uom::si::information::byte; pub use uom::si::information::byte;
#[cfg(any(not(windows), test))]
pub use uom::si::information::kibibyte;
pub use uom::si::ratio::ratio; pub use uom::si::ratio::ratio;
pub use uom::si::time::{microsecond, nanosecond, second}; pub use uom::si::time::{microsecond, nanosecond, second};
@@ -55,7 +53,7 @@ pub fn format_duration_value(duration: f64, unit: Option<TimeUnit>) -> (String,
} }
#[cfg(test)] #[cfg(test)]
use uom::si::time::millisecond; use uom::si::{information::kibibyte, time::millisecond};
#[test] #[test]
fn test_time() { fn test_time() {
+6 -1
View File
@@ -112,7 +112,7 @@ pub fn standard_deviation<Q: UnsafeRawValue>(values: &[Q], mean: Q) -> Q {
/// ///
/// References: /// References:
/// - <https://en.wikipedia.org/wiki/Median_absolute_deviation> /// - <https://en.wikipedia.org/wiki/Median_absolute_deviation>
pub fn modified_zscores(xs: &[f64]) -> Vec<f64> { pub fn modified_zscores_f64(xs: &[f64]) -> Vec<f64> {
assert!(!xs.is_empty()); assert!(!xs.is_empty());
// Compute sample median: // Compute sample median:
@@ -131,6 +131,11 @@ pub fn modified_zscores(xs: &[f64]) -> Vec<f64> {
xs.iter().map(|&x| (x - x_median) / mad).collect() xs.iter().map(|&x| (x - x_median) / mad).collect()
} }
pub fn modified_zscores<Q: UnsafeRawValue>(values: &[Q]) -> Vec<f64> {
let values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();
modified_zscores_f64(&values)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
+13 -6
View File
@@ -24,6 +24,19 @@ use std::process::{ChildStdout, Command};
use anyhow::Result; use anyhow::Result;
#[cfg(not(windows))]
#[derive(Debug, Copy, Clone)]
struct CPUTimes {
/// Total amount of time spent executing in user mode
pub user_usec: i64,
/// Total amount of time spent executing in kernel mode
pub system_usec: i64,
/// Maximum amount of memory used by the process, in bytes
pub memory_usage_byte: u64,
}
/// Discard the output of a child process. /// Discard the output of a child process.
fn discard(output: ChildStdout) { fn discard(output: ChildStdout) {
const CHUNK_SIZE: usize = 64 << 10; const CHUNK_SIZE: usize = 64 << 10;
@@ -82,15 +95,9 @@ pub fn execute_and_measure(mut command: Command) -> Result<Measurement> {
discard(output); discard(output);
} }
#[cfg(not(windows))]
let (time_user, time_system, peak_memory_usage, exit_status) = cpu_timer.stop(child)?;
#[cfg(windows)]
let exit_status = child.wait()?; let exit_status = child.wait()?;
let time_wall_clock = wallclock_timer.stop(); let time_wall_clock = wallclock_timer.stop();
#[cfg(windows)]
let (time_user, time_system, peak_memory_usage) = cpu_timer.stop(); let (time_user, time_system, peak_memory_usage) = cpu_timer.stop();
Ok(Measurement { Ok(Measurement {
+90 -71
View File
@@ -1,89 +1,108 @@
#![cfg(not(windows))] #![cfg(not(windows))]
use std::io; use std::convert::TryFrom;
use std::mem::MaybeUninit; use std::mem;
use std::os::unix::process::ExitStatusExt;
use std::process::{Child, ExitStatus};
use anyhow::Result; use crate::quantity::{byte, microsecond, Information, Time};
use crate::timer::CPUTimes;
use crate::quantity::{byte, kibibyte, microsecond, second, Information, Time};
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
struct ResourceUsage { pub struct CPUInterval {
/// Total amount of time spent executing in user mode /// Total amount of time spent executing in user mode
pub time_user: Time, pub user: Time,
/// Total amount of time spent executing in kernel mode /// Total amount of time spent executing in kernel mode
pub time_system: Time, pub system: Time,
/// Maximum amount of memory used by the process, in bytes
pub memory_usage: Information,
} }
#[allow(clippy::useless_conversion)] pub struct CPUTimer {
fn convert_timeval(tv: libc::timeval) -> Time { start_cpu: CPUTimes,
let sec = tv.tv_sec as f64;
let usec = tv.tv_usec as f64;
Time::new::<second>(sec) + Time::new::<microsecond>(usec)
} }
#[allow(clippy::useless_conversion)]
fn wait4(mut child: Child) -> io::Result<(ExitStatus, ResourceUsage)> {
drop(child.stdin.take());
let pid = child.id() as i32;
let mut status = 0;
let mut rusage = MaybeUninit::zeroed();
loop {
let result = unsafe { libc::wait4(pid, &mut status, 0, rusage.as_mut_ptr()) };
if result >= 0 {
break;
}
let error = io::Error::last_os_error();
if error.kind() != io::ErrorKind::Interrupted {
return Err(error);
}
}
let rusage = unsafe { rusage.assume_init() };
let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
// Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes
// A f64 can represent integers up to 2^53 exactly, so we can represent
// all values up to 2^53 bytes = 8 PiB exactly. Beyond that, our precision
// is larger than 1 byte, but that's considered acceptable.
Information::new::<byte>(rusage.ru_maxrss as f64)
} else {
Information::new::<kibibyte>(rusage.ru_maxrss as f64)
};
Ok((
ExitStatus::from_raw(status),
ResourceUsage {
time_user: convert_timeval(rusage.ru_utime),
time_system: convert_timeval(rusage.ru_stime),
memory_usage: memory_usage_byte.into(),
},
))
}
pub struct CPUTimer {}
impl CPUTimer { impl CPUTimer {
pub fn start() -> Self { pub fn start() -> Self {
Self {} CPUTimer {
start_cpu: get_cpu_times(),
}
} }
pub fn stop(&self, child: Child) -> Result<(Time, Time, Information, ExitStatus)> { pub fn stop(&self) -> (Time, Time, Information) {
let (status, usage) = wait4(child)?; let end_cpu = get_cpu_times();
Ok(( let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);
usage.time_user, (
usage.time_system, cpu_interval.user,
usage.memory_usage, cpu_interval.system,
status, Information::new::<byte>(end_cpu.memory_usage_byte as f64),
)) )
} }
} }
/// Read CPU execution times ('user' and 'system')
fn get_cpu_times() -> CPUTimes {
use libc::{getrusage, rusage, RUSAGE_CHILDREN};
let result: rusage = unsafe {
let mut buf = mem::zeroed();
let success = getrusage(RUSAGE_CHILDREN, &mut buf);
assert_eq!(0, success);
buf
};
const MICROSEC_PER_SEC: i64 = 1000 * 1000;
// Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes
let max_rss_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
result.ru_maxrss
} else {
result.ru_maxrss * 1024
};
#[allow(clippy::useless_conversion)]
CPUTimes {
user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC
+ i64::from(result.ru_utime.tv_usec),
system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC
+ i64::from(result.ru_stime.tv_usec),
memory_usage_byte: u64::try_from(max_rss_byte).unwrap_or(0),
}
}
/// Compute the time intervals in between two `CPUTimes` snapshots
fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {
CPUInterval {
user: Time::new::<microsecond>((end.user_usec - start.user_usec) as f64),
system: Time::new::<microsecond>((end.system_usec - start.system_usec) as f64),
}
}
#[cfg(test)]
use crate::quantity::second;
#[cfg(test)]
use approx::assert_relative_eq;
#[test]
fn test_cpu_time_interval() {
let t_a = CPUTimes {
user_usec: 12345,
system_usec: 54321,
memory_usage_byte: 0,
};
let t_b = CPUTimes {
user_usec: 20000,
system_usec: 70000,
memory_usage_byte: 0,
};
let t_zero = cpu_time_interval(&t_a, &t_a);
assert!(t_zero.user.get::<second>().abs() < f64::EPSILON);
assert!(t_zero.system.get::<second>().abs() < f64::EPSILON);
let t_ab = cpu_time_interval(&t_a, &t_b);
assert_relative_eq!(0.007655, t_ab.user.get::<second>());
assert_relative_eq!(0.015679, t_ab.system.get::<second>());
let t_ba = cpu_time_interval(&t_b, &t_a);
assert_relative_eq!(-0.007655, t_ba.user.get::<second>());
assert_relative_eq!(-0.015679, t_ba.system.get::<second>());
}