From 6556b2bd4605f7058c058a7697b29ff44e148797 Mon Sep 17 00:00:00 2001 From: David Peter Date: Sun, 23 Jun 2024 13:29:05 +0200 Subject: [PATCH] Initial work on tracking memory usage --- src/benchmark/benchmark_result.rs | 4 ++++ src/benchmark/executor.rs | 4 ++++ src/benchmark/mod.rs | 4 ++++ src/benchmark/relative_speed.rs | 1 + src/benchmark/timing_result.rs | 3 +++ src/export/asciidoc.rs | 4 ++++ src/export/csv.rs | 2 ++ src/export/markdown.rs | 8 ++++++++ src/export/markup.rs | 8 ++++++++ src/export/orgmode.rs | 4 ++++ src/timer/mod.rs | 8 ++++++-- src/timer/unix_timer.rs | 19 +++++++++++++++++-- src/timer/windows_timer.rs | 6 +++--- 13 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/benchmark/benchmark_result.rs b/src/benchmark/benchmark_result.rs index b37f725..287c73b 100644 --- a/src/benchmark/benchmark_result.rs +++ b/src/benchmark/benchmark_result.rs @@ -42,6 +42,10 @@ pub struct BenchmarkResult { #[serde(skip_serializing_if = "Option::is_none")] pub times: Option>, + /// Maximum memory usage of the process, in bytes + #[serde(skip_serializing_if = "Option::is_none")] + pub memory_usage_byte: Option>, + /// Exit codes of all command invocations pub exit_codes: Vec>, diff --git a/src/benchmark/executor.rs b/src/benchmark/executor.rs index 42af622..03e2b6a 100644 --- a/src/benchmark/executor.rs +++ b/src/benchmark/executor.rs @@ -133,6 +133,7 @@ impl Executor for RawExecutor<'_> { time_real: result.time_real, time_user: result.time_user, time_system: result.time_system, + memory_usage_byte: result.memory_usage_byte, }, result.status, )) @@ -204,6 +205,7 @@ impl Executor for ShellExecutor<'_> { time_real: result.time_real, time_user: result.time_user, time_system: result.time_system, + memory_usage_byte: result.memory_usage_byte, }, result.status, )) @@ -268,6 +270,7 @@ impl Executor for ShellExecutor<'_> { time_real: mean(×_real), time_user: mean(×_user), time_system: mean(×_system), + memory_usage_byte: 0, }); Ok(()) @@ -323,6 +326,7 @@ impl Executor for MockExecutor { time_real: Self::extract_time(command.get_command_line()), time_user: 0.0, time_system: 0.0, + memory_usage_byte: 0, }, status, )) diff --git a/src/benchmark/mod.rs b/src/benchmark/mod.rs index 2cca857..e3534a7 100644 --- a/src/benchmark/mod.rs +++ b/src/benchmark/mod.rs @@ -151,6 +151,7 @@ impl<'a> Benchmark<'a> { let mut times_real: Vec = vec![]; let mut times_user: Vec = vec![]; let mut times_system: Vec = vec![]; + let mut memory_usage_byte: Vec = vec![]; let mut exit_codes: Vec> = vec![]; let mut all_succeeded = true; @@ -279,6 +280,7 @@ impl<'a> Benchmark<'a> { times_real.push(res.time_real); times_user.push(res.time_user); times_system.push(res.time_system); + memory_usage_byte.push(res.memory_usage_byte); exit_codes.push(extract_exit_code(status)); all_succeeded = all_succeeded && success; @@ -315,6 +317,7 @@ impl<'a> Benchmark<'a> { times_real.push(res.time_real); times_user.push(res.time_user); times_system.push(res.time_system); + memory_usage_byte.push(res.memory_usage_byte); exit_codes.push(extract_exit_code(status)); all_succeeded = all_succeeded && success; @@ -451,6 +454,7 @@ impl<'a> Benchmark<'a> { min: t_min, max: t_max, times: Some(times_real), + memory_usage_byte: Some(memory_usage_byte), exit_codes, parameters: self .command diff --git a/src/benchmark/relative_speed.rs b/src/benchmark/relative_speed.rs index 7449168..8e87cc9 100644 --- a/src/benchmark/relative_speed.rs +++ b/src/benchmark/relative_speed.rs @@ -133,6 +133,7 @@ fn create_result(name: &str, mean: Scalar) -> BenchmarkResult { min: mean, max: mean, times: None, + memory_usage_byte: None, exit_codes: Vec::new(), parameters: BTreeMap::new(), } diff --git a/src/benchmark/timing_result.rs b/src/benchmark/timing_result.rs index d4b8490..f7aac92 100644 --- a/src/benchmark/timing_result.rs +++ b/src/benchmark/timing_result.rs @@ -11,4 +11,7 @@ pub struct TimingResult { /// Time spent in kernel mode pub time_system: Second, + + /// Maximum amount of memory used, in bytes + pub memory_usage_byte: u64, } diff --git a/src/export/asciidoc.rs b/src/export/asciidoc.rs index f1323fc..0e8401b 100644 --- a/src/export/asciidoc.rs +++ b/src/export/asciidoc.rs @@ -105,6 +105,7 @@ fn test_asciidoc_format_s() { min: 5.0, max: 6.0, times: Some(vec![7.0, 8.0, 9.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: { let mut params = BTreeMap::new(); @@ -124,6 +125,7 @@ fn test_asciidoc_format_s() { min: 15.0, max: 16.0, times: Some(vec![17.0, 18.0, 19.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: { let mut params = BTreeMap::new(); @@ -180,6 +182,7 @@ fn test_asciidoc_format_ms() { min: 0.015, max: 0.016, times: Some(vec![0.017, 0.018, 0.019]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: { let mut params = BTreeMap::new(); @@ -199,6 +202,7 @@ fn test_asciidoc_format_ms() { min: 5.0, max: 6.0, times: Some(vec![7.0, 8.0, 9.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: { let mut params = BTreeMap::new(); diff --git a/src/export/csv.rs b/src/export/csv.rs index 7c7d0ee..16c4189 100644 --- a/src/export/csv.rs +++ b/src/export/csv.rs @@ -77,6 +77,7 @@ fn test_csv() { min: 5.0, max: 6.0, times: Some(vec![7.0, 8.0, 9.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: { let mut params = BTreeMap::new(); @@ -96,6 +97,7 @@ fn test_csv() { min: 15.0, max: 16.5, times: Some(vec![17.0, 18.0, 19.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: { let mut params = BTreeMap::new(); diff --git a/src/export/markdown.rs b/src/export/markdown.rs index c4d5fd1..05afcca 100644 --- a/src/export/markdown.rs +++ b/src/export/markdown.rs @@ -82,6 +82,7 @@ fn test_markdown_format_ms() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -96,6 +97,7 @@ fn test_markdown_format_ms() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -139,6 +141,7 @@ fn test_markdown_format_s() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -153,6 +156,7 @@ fn test_markdown_format_s() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -196,6 +200,7 @@ fn test_markdown_format_time_unit_s() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -210,6 +215,7 @@ fn test_markdown_format_time_unit_s() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -274,6 +280,7 @@ fn test_markdown_format_time_unit_ms() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -288,6 +295,7 @@ fn test_markdown_format_time_unit_ms() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, diff --git a/src/export/markup.rs b/src/export/markup.rs index eeeeb61..9ca2bfa 100644 --- a/src/export/markup.rs +++ b/src/export/markup.rs @@ -136,6 +136,7 @@ fn test_determine_unit_from_results_unit_given_s() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -150,6 +151,7 @@ fn test_determine_unit_from_results_unit_given_s() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -178,6 +180,7 @@ fn test_determine_unit_from_results_unit_given_ms() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -192,6 +195,7 @@ fn test_determine_unit_from_results_unit_given_ms() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -220,6 +224,7 @@ fn test_determine_unit_from_results_unit_first_s() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -234,6 +239,7 @@ fn test_determine_unit_from_results_unit_first_s() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -262,6 +268,7 @@ fn test_determine_unit_from_results_unit_first_ms() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -276,6 +283,7 @@ fn test_determine_unit_from_results_unit_first_ms() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, diff --git a/src/export/orgmode.rs b/src/export/orgmode.rs index 65ff63f..6a256b8 100644 --- a/src/export/orgmode.rs +++ b/src/export/orgmode.rs @@ -88,6 +88,7 @@ fn test_orgmode_format_ms() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -102,6 +103,7 @@ fn test_orgmode_format_ms() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -147,6 +149,7 @@ fn test_orgmode_format_s() { min: 2.0020, max: 2.0080, times: Some(vec![2.0, 2.0, 2.0]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, @@ -161,6 +164,7 @@ fn test_orgmode_format_s() { min: 0.1023, max: 0.1080, times: Some(vec![0.1, 0.1, 0.1]), + memory_usage_byte: None, exit_codes: vec![Some(0), Some(0), Some(0)], parameters: BTreeMap::new(), }, diff --git a/src/timer/mod.rs b/src/timer/mod.rs index 675f287..b2cd265 100644 --- a/src/timer/mod.rs +++ b/src/timer/mod.rs @@ -32,6 +32,9 @@ struct CPUTimes { /// 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, } /// Used to indicate the result of running a command @@ -40,7 +43,7 @@ pub struct TimerResult { pub time_real: Second, pub time_user: Second, pub time_system: Second, - + pub memory_usage_byte: u64, /// The exit status of the process pub status: ExitStatus, } @@ -106,12 +109,13 @@ pub fn execute_and_measure(mut command: Command) -> Result { let status = child.wait()?; let time_real = wallclock_timer.stop(); - let (time_user, time_system) = cpu_timer.stop(); + let (time_user, time_system, memory_usage_byte) = cpu_timer.stop(); Ok(TimerResult { time_real, time_user, time_system, + memory_usage_byte, status, }) } diff --git a/src/timer/unix_timer.rs b/src/timer/unix_timer.rs index 1b812b3..34e5325 100644 --- a/src/timer/unix_timer.rs +++ b/src/timer/unix_timer.rs @@ -1,5 +1,6 @@ #![cfg(not(windows))] +use std::convert::TryFrom; use std::mem; use crate::timer::CPUTimes; @@ -25,10 +26,14 @@ impl CPUTimer { } } - pub fn stop(&self) -> (Second, Second) { + pub fn stop(&self) -> (Second, Second, u64) { let end_cpu = get_cpu_times(); let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu); - (cpu_interval.user, cpu_interval.system) + ( + cpu_interval.user, + cpu_interval.system, + end_cpu.memory_usage_byte, + ) } } @@ -45,12 +50,20 @@ fn get_cpu_times() -> CPUTimes { 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), } } @@ -70,11 +83,13 @@ 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); diff --git a/src/timer/windows_timer.rs b/src/timer/windows_timer.rs index 6b3d333..04dc9fc 100644 --- a/src/timer/windows_timer.rs +++ b/src/timer/windows_timer.rs @@ -86,7 +86,7 @@ impl CPUTimer { Self { job_object } } - pub fn stop(&self) -> (Second, Second) { + pub fn stop(&self) -> (Second, Second, u64) { let mut job_object_info = mem::MaybeUninit::::uninit(); @@ -114,9 +114,9 @@ impl CPUTimer { // for all active processes associated with the job, as well as all terminated // processes no longer associated with the job, in 100-nanosecond ticks." let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS; - (user as f64 * 1e-6, kernel as f64 * 1e-6) + (user as f64 * 1e-6, kernel as f64 * 1e-6, 0) } else { - (0.0, 0.0) + (0.0, 0.0, 0) } } }