Skip to content
Myr

Represent memory usage as f64 as well

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent 27ad0fb · Browse files · 8 files+93 added−79 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+93 added−79 removed

  • other:+93 added−79 removed

8 files+93 added−79 removed

modified:src/benchmark/executor.rs+2 added-2 removed
7 unmodified lines
8use crate::options::{9 CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,10};11use crate::output::progress_bar::get_progress_bar;12use crate::quantity::{byte, second, Information, Time, TimeQuantity};12use crate::quantity::{second, Information, InformationQuantity, Time, TimeQuantity};13use crate::timer::execute_and_measure;14use crate::util::randomized_environment_offset;1516use anyhow::{bail, Context, Result};
330 unmodified lines
347 Ok(Measurement {348 time_wall_clock: Self::extract_time(command.get_command_line()),349 time_user: Time::zero(),350 time_system: Time::zero(),351 peak_memory_usage: Information::new::<byte>(0),351 peak_memory_usage: Information::zero(),352 exit_status,353 })354 }355
19 unmodified lines
modified:src/benchmark/measurement.rs+10 added-14 removed
1use std::process::ExitStatus;23use serde::Serialize;45use crate::outlier_detection::modified_zscores;5use crate::quantity::{7 max, mean, median, min, second, serialize_information, serialize_time, standard_deviation,8 Information, Time, TimeQuantity,6 max, mean, median, min, modified_zscores, serialize_information, serialize_time,7 standard_deviation, Information, Time,8};9use crate::util::exit_code::extract_exit_code;1011fn serialize_exit_status<S>(exit_status: &ExitStatus, serializer: S) -> Result<S::Ok, S::Error>
89 unmodified lines
101 pub fn max(&self) -> Time {102 max(&self.wall_clock_times())103 }104105 /// Compute modified Z-scores for the wall clock times106 pub fn modified_zscores(&self) -> Vec<f64> {107 modified_zscores(&self.wall_clock_times())108 }109110 /// The average user time111 pub fn time_user_mean(&self) -> Time {112 mean(113 &self
15 unmodified lines
129 )130 }131132 pub fn peak_memory_usage_mean(&self) -> Information {129 self.measurements130 .iter()131 .map(|m| m.peak_memory_usage)132 .max() // TODO: should be mean: do we need InformationF64?133 .unwrap_or_default()134 }135136 pub fn modified_zscores(&self) -> Vec<f64> {137 modified_zscores(133 mean(134 &self139 .wall_clock_times()135 .measurements136 .iter()141 .map(|t| t.value_in(second)) // TODO137 .map(|m| m.peak_memory_usage)138 .collect::<Vec<_>>(),139 )140 }141
4 unmodified lines
modified:src/export/csv.rs+6 added-6 removed
83 unmodified lines
84 Measurement {85 time_wall_clock: Time::new::<second>(7.0),86 time_user: Time::new::<second>(7.0),87 time_system: Time::zero(),88 peak_memory_usage: Information::new::<byte>(1024),88 peak_memory_usage: Information::new::<byte>(1024.),89 exit_status: ExitStatus::default(),90 },91 Measurement {92 time_wall_clock: Time::new::<second>(8.0),93 time_user: Time::new::<second>(8.0),94 time_system: Time::zero(),95 peak_memory_usage: Information::new::<byte>(1024),95 peak_memory_usage: Information::new::<byte>(1024.),96 exit_status: ExitStatus::default(),97 },98 Measurement {99 time_wall_clock: Time::new::<second>(12.0),100 time_user: Time::new::<second>(12.0),101 time_system: Time::zero(),102 peak_memory_usage: Information::new::<byte>(1024),102 peak_memory_usage: Information::new::<byte>(1024.),103 exit_status: ExitStatus::default(),104 },105 ]),106 parameters: {
21 unmodified lines
128 Measurement {129 time_wall_clock: Time::new::<second>(17.0),130 time_user: Time::new::<second>(17.0),131 time_system: Time::zero(),132 peak_memory_usage: Information::new::<byte>(1024),132 peak_memory_usage: Information::new::<byte>(1024.),133 exit_status: ExitStatus::default(),134 },135 Measurement {136 time_wall_clock: Time::new::<second>(18.0),137 time_user: Time::new::<second>(18.0),138 time_system: Time::zero(),139 peak_memory_usage: Information::new::<byte>(1024),139 peak_memory_usage: Information::new::<byte>(1024.),140 exit_status: ExitStatus::default(),141 },142 Measurement {143 time_wall_clock: Time::new::<second>(19.0),144 time_user: Time::new::<second>(19.0),145 time_system: Time::zero(),146 peak_memory_usage: Information::new::<byte>(1024),146 peak_memory_usage: Information::new::<byte>(1024.),147 exit_status: ExitStatus::default(),148 },149 ]),150 parameters: {
33 unmodified lines
modified:src/export/tests.rs+24 added-24 removed
31 unmodified lines
32 Measurement {33 time_wall_clock: Time::new::<second>(0.09),34 time_user: Time::new::<second>(0.09),35 time_system: Time::zero(),36 peak_memory_usage: Information::new::<byte>(1024),36 peak_memory_usage: Information::new::<byte>(1024.),37 exit_status: ExitStatus::default(),38 },39 Measurement {40 time_wall_clock: Time::new::<second>(0.10),41 time_user: Time::new::<second>(0.10),42 time_system: Time::zero(),43 peak_memory_usage: Information::new::<byte>(1024),43 peak_memory_usage: Information::new::<byte>(1024.),44 exit_status: ExitStatus::default(),45 },46 Measurement {47 time_wall_clock: Time::new::<second>(0.14),48 time_user: Time::new::<second>(0.14),49 time_system: Time::zero(),50 peak_memory_usage: Information::new::<byte>(1024),50 peak_memory_usage: Information::new::<byte>(1024.),51 exit_status: ExitStatus::default(),52 },53 ]),54 parameters: BTreeMap::new(),
4 unmodified lines
59 Measurement {60 time_wall_clock: Time::new::<second>(2.0),61 time_user: Time::new::<second>(2.0),62 time_system: Time::zero(),63 peak_memory_usage: Information::new::<byte>(1024),63 peak_memory_usage: Information::new::<byte>(1024.),64 exit_status: ExitStatus::default(),65 },66 Measurement {67 time_wall_clock: Time::new::<second>(3.0),68 time_user: Time::new::<second>(3.0),69 time_system: Time::zero(),70 peak_memory_usage: Information::new::<byte>(1024),70 peak_memory_usage: Information::new::<byte>(1024.),71 exit_status: ExitStatus::default(),72 },73 Measurement {74 time_wall_clock: Time::new::<second>(4.0),75 time_user: Time::new::<second>(4.0),76 time_system: Time::zero(),77 peak_memory_usage: Information::new::<byte>(1024),77 peak_memory_usage: Information::new::<byte>(1024.),78 exit_status: ExitStatus::default(),79 },80 ]),81 parameters: BTreeMap::new(),
49 unmodified lines
131 Measurement {132 time_wall_clock: Time::new::<second>(2.1),133 time_user: Time::new::<second>(2.1),134 time_system: Time::zero(),135 peak_memory_usage: Information::new::<byte>(1024),135 peak_memory_usage: Information::new::<byte>(1024.),136 exit_status: ExitStatus::default(),137 },138 Measurement {139 time_wall_clock: Time::new::<second>(2.2),140 time_user: Time::new::<second>(2.2),141 time_system: Time::zero(),142 peak_memory_usage: Information::new::<byte>(1024),142 peak_memory_usage: Information::new::<byte>(1024.),143 exit_status: ExitStatus::default(),144 },145 Measurement {146 time_wall_clock: Time::new::<second>(2.3),147 time_user: Time::new::<second>(2.3),148 time_system: Time::zero(),149 peak_memory_usage: Information::new::<byte>(1024),149 peak_memory_usage: Information::new::<byte>(1024.),150 exit_status: ExitStatus::default(),151 },152 ]),153 parameters: BTreeMap::new(),
4 unmodified lines
158 Measurement {159 time_wall_clock: Time::new::<second>(0.1),160 time_user: Time::new::<second>(0.1),161 time_system: Time::zero(),162 peak_memory_usage: Information::new::<byte>(1024),162 peak_memory_usage: Information::new::<byte>(1024.),163 exit_status: ExitStatus::default(),164 },165 Measurement {166 time_wall_clock: Time::new::<second>(0.2),167 time_user: Time::new::<second>(0.2),168 time_system: Time::zero(),169 peak_memory_usage: Information::new::<byte>(1024),169 peak_memory_usage: Information::new::<byte>(1024.),170 exit_status: ExitStatus::default(),171 },172 Measurement {173 time_wall_clock: Time::new::<second>(0.3),174 time_user: Time::new::<second>(0.3),175 time_system: Time::zero(),176 peak_memory_usage: Information::new::<byte>(1024),176 peak_memory_usage: Information::new::<byte>(1024.),177 exit_status: ExitStatus::default(),178 },179 ]),180 parameters: BTreeMap::new(),
49 unmodified lines
230 Measurement {231 time_wall_clock: Time::new::<second>(2.1),232 time_user: Time::new::<second>(2.1),233 time_system: Time::zero(),234 peak_memory_usage: Information::new::<byte>(1024),234 peak_memory_usage: Information::new::<byte>(1024.),235 exit_status: ExitStatus::default(),236 },237 Measurement {238 time_wall_clock: Time::new::<second>(2.2),239 time_user: Time::new::<second>(2.2),240 time_system: Time::zero(),241 peak_memory_usage: Information::new::<byte>(1024),241 peak_memory_usage: Information::new::<byte>(1024.),242 exit_status: ExitStatus::default(),243 },244 Measurement {245 time_wall_clock: Time::new::<second>(2.3),246 time_user: Time::new::<second>(2.3),247 time_system: Time::zero(),248 peak_memory_usage: Information::new::<byte>(1024),248 peak_memory_usage: Information::new::<byte>(1024.),249 exit_status: ExitStatus::default(),250 },251 ]),252 parameters: BTreeMap::new(),
4 unmodified lines
257 Measurement {258 time_wall_clock: Time::new::<second>(0.1),259 time_user: Time::new::<second>(0.1),260 time_system: Time::zero(),261 peak_memory_usage: Information::new::<byte>(1024),261 peak_memory_usage: Information::new::<byte>(1024.),262 exit_status: ExitStatus::default(),263 },264 Measurement {265 time_wall_clock: Time::new::<second>(0.2),266 time_user: Time::new::<second>(0.2),267 time_system: Time::zero(),268 peak_memory_usage: Information::new::<byte>(1024),268 peak_memory_usage: Information::new::<byte>(1024.),269 exit_status: ExitStatus::default(),270 },271 Measurement {272 time_wall_clock: Time::new::<second>(0.3),273 time_user: Time::new::<second>(0.3),274 time_system: Time::zero(),275 peak_memory_usage: Information::new::<byte>(1024),275 peak_memory_usage: Information::new::<byte>(1024.),276 exit_status: ExitStatus::default(),277 },278 ]),279 parameters: BTreeMap::new(),
48 unmodified lines
328 Measurement {329 time_wall_clock: Time::new::<second>(2.01),330 time_user: Time::new::<second>(2.01),331 time_system: Time::zero(),332 peak_memory_usage: Information::new::<byte>(1024),332 peak_memory_usage: Information::new::<byte>(1024.),333 exit_status: ExitStatus::default(),334 },335 Measurement {336 time_wall_clock: Time::new::<second>(2.02),337 time_user: Time::new::<second>(2.02),338 time_system: Time::zero(),339 peak_memory_usage: Information::new::<byte>(1024),339 peak_memory_usage: Information::new::<byte>(1024.),340 exit_status: ExitStatus::default(),341 },342 Measurement {343 time_wall_clock: Time::new::<second>(2.03),344 time_user: Time::new::<second>(2.03),345 time_system: Time::zero(),346 peak_memory_usage: Information::new::<byte>(1024),346 peak_memory_usage: Information::new::<byte>(1024.),347 exit_status: ExitStatus::default(),348 },349 ]),350 parameters: BTreeMap::new(),
4 unmodified lines
355 Measurement {356 time_wall_clock: Time::new::<second>(0.11),357 time_user: Time::new::<second>(0.11),358 time_system: Time::zero(),359 peak_memory_usage: Information::new::<byte>(1024),359 peak_memory_usage: Information::new::<byte>(1024.),360 exit_status: ExitStatus::default(),361 },362 Measurement {363 time_wall_clock: Time::new::<second>(0.12),364 time_user: Time::new::<second>(0.12),365 time_system: Time::zero(),366 peak_memory_usage: Information::new::<byte>(1024),366 peak_memory_usage: Information::new::<byte>(1024.),367 exit_status: ExitStatus::default(),368 },369 Measurement {370 time_wall_clock: Time::new::<second>(0.13),371 time_user: Time::new::<second>(0.13),372 time_system: Time::zero(),373 peak_memory_usage: Information::new::<byte>(1024),373 peak_memory_usage: Information::new::<byte>(1024.),374 exit_status: ExitStatus::default(),375 },376 ]),377 parameters: BTreeMap::new(),
40 unmodified lines
modified:src/quantity/mod.rs+45 added-17 removed
7 unmodified lines
8pub use uom::si::information::{byte, kibibyte};9pub use uom::si::ratio::ratio;10pub use uom::si::time::{hour, microsecond, millisecond, minute, nanosecond, second};1112pub use si::f64::Ratio;13pub use si::f64::Time;14pub use si::u64::Information;12pub use si::f64::{Information, Ratio, Time};1314pub use si::f64::{Information, Ratio, Time};15pub use si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};16pub use si::ratio::ratio;
72 unmodified lines
89}9091pub trait InformationQuantity {92 fn zero() -> Information {95 Information::new::<byte>(0)93 Information::new::<byte>(0.)94 }9598 fn value_in<U>(&self, u: U) -> u6496 fn value_in<U>(&self, u: U) -> f6497 where100 U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>;98 U: si::information::Unit + Conversion<f64, T = f64>;99100 fn to_string<U>(&self, u: U) -> String101 where104 U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>;102 U: si::information::Unit + Conversion<f64, T = f64>;103}104105impl InformationQuantity for Information {108 fn value_in<U>(&self, _u: U) -> u64106 fn value_in<U>(&self, _u: U) -> f64107 where110 U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>,108 U: si::information::Unit + Conversion<f64, T = f64>,109 {110 self.get::<U>()111 }112113 fn to_string<U>(&self, u: U) -> String114 where117 U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>,115 U: si::information::Unit + Conversion<f64, T = f64>,116 {117 format!(118 "{}",119 self.into_format_args(u, uom::fmt::DisplayStyle::Abbreviation)120 )121 }122}123124pub trait UnsafeRawValue {125 fn unsafe_raw_value(&self) -> f64;126 fn unsafe_from_raw_value(value: f64) -> Self;127}128129impl UnsafeRawValue for Time {130 fn unsafe_raw_value(&self) -> f64 {131 self.value_in(second)132 }133134 fn unsafe_from_raw_value(value: f64) -> Self {135 Time::new::<second>(value)136 }137}138139impl UnsafeRawValue for Information {140 fn unsafe_raw_value(&self) -> f64 {141 self.value_in(byte)142 }143144 fn unsafe_from_raw_value(value: f64) -> Self {145 Information::new::<byte>(value)146 }147}148149pub fn serialize_time<S>(t: &Time, s: S) -> Result<S::Ok, S::Error>150where151 S: Serializer,152{
14 unmodified lines
167}168169macro_rules! quantity_fn {170 ($name:ident, $unwrapped_values:ident, $body:expr) => {148 pub fn $name(values: &[Time]) -> Time {149 let $unwrapped_values: Vec<_> = values.iter().map(|q| q.value_in(second)).collect();171 pub fn $name<Q: UnsafeRawValue>(values: &[Q]) -> Q {172 let $unwrapped_values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();173 let result_value = $body;174152 Time::new::<second>(result_value)175 Q::unsafe_from_raw_value(result_value)176 }177 };178}179
5 unmodified lines
185 let mean_value = statistical::mean(&values);186 statistical::standard_deviation(&values, Some(mean_value))187});188189pub fn modified_zscores<Q: UnsafeRawValue>(values: &[Q]) -> Vec<f64> {190 let values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();191 crate::outlier_detection::modified_zscores(&values)192}193194#[test]195fn test_time() {196 let time = Time::new::<millisecond>(123.4);197 assert_eq!(time.value_in(millisecond), 123.4);
6 unmodified lines
204}205206#[test]207fn test_information() {180 let information = Information::new::<kibibyte>(8);181 assert_eq!(information.value_in(byte), 8192);208 let information = Information::new::<kibibyte>(8.);209 assert_eq!(information.value_in(byte), 8192.);210211 let information_kib = information.value_in(kibibyte);184 assert_eq!(information_kib, 8);212 assert_eq!(information_kib, 8.);213}214215#[test]216fn test_format() {
1 unmodified line
218 assert_eq!(time.format_auto(Some(TimeUnit::Second)), "0.123 s");219 assert_eq!(time.format_auto(Some(TimeUnit::MilliSecond)), "123.4 ms");220 assert_eq!(time.format_auto(Some(TimeUnit::MicroSecond)), "123400.0 µs");221194 let peak_memory_usage = Information::new::<kibibyte>(8);222 let peak_memory_usage = Information::new::<kibibyte>(8.);223 assert_eq!(peak_memory_usage.to_string(byte), "8192 B");224 assert_eq!(peak_memory_usage.to_string(kibibyte), "8 KiB");225}226
43 unmodified lines
modified:src/timer/unix_timer.rs+5 added-3 removed
1#![cfg(not(windows))]23use std::convert::TryFrom;3use std::io;4use std::mem::MaybeUninit;5use std::os::unix::process::ExitStatusExt;6use std::process::{Child, ExitStatus};
38 unmodified lines
45 let rusage = unsafe { rusage.assume_init() };4647 let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {48 // Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes50 Information::new::<byte>(u64::try_from(rusage.ru_maxrss).unwrap_or(0))49 // A f64 can represent integers up to 2^53 exactly, so we can represent50 // all values up to 2^53 bytes = 8 PiB exactly. Beyond that, our precision51 // is larger than 1 byte, but that's considered acceptable.52 Information::new::<byte>(rusage.ru_maxrss as f64)53 } else {52 Information::new::<kibibyte>(u64::try_from(rusage.ru_maxrss).unwrap_or(0))54 Information::new::<kibibyte>(rusage.ru_maxrss as f64)55 };5657 Ok((58 ExitStatus::from_raw(status),
26 unmodified lines
modified:Cargo.lock+1 added-12 removed
513 unmodified lines
514 "num-bigint",515 "num-complex",516 "num-integer",517 "num-iter",518 "num-rational 0.2.4",518 "num-rational",519 "num-traits",520]521522[[package]]
49 unmodified lines
572 "num-traits",573]574575[[package]]576name = "num-rational"577version = "0.4.2"578source = "registry+https://github.com/rust-lang/crates.io-index"579checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"580dependencies = [581 "num-integer",582 "num-traits",583]584585[[package]]576name = "num-traits"577version = "0.2.19"578source = "registry+https://github.com/rust-lang/crates.io-index"579checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
450 unmodified lines
1030version = "0.36.0"1031source = "registry+https://github.com/rust-lang/crates.io-index"1032checksum = "ffd36e5350a65d112584053ee91843955826bf9e56ec0d1351214e01f6d7cd9c"1033dependencies = [1044 "num-rational 0.4.2",1034 "num-traits",1035 "typenum",1036]1037
213 unmodified lines
modified:Cargo.toml-1 removed