Initial work on tracking memory usage

This commit is contained in:
David Peter authored and David Peter committed 2024-12-29 12:18:55 +01:00
1 parent f78f482d8f
commit 6556b2bd46
13 files changed
+68 -7

No files matched your search

+6 -2
View File
@@ -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<TimerResult> {
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,
})
}
+17 -2
View File
@@ -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);
+3 -3
View File
@@ -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::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::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)
}
}
}