Initial work on tracking memory usage
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 f78f482 · Browse files · 13 files·+68 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
13 files+68 added−7 removed
- other:+68 added−7 removed
Provenance
- Commit
6556b2b- Tree
d6c366a- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
f78f482
13 files changed+68 added−7 removed
modified:src/benchmark/benchmark_result.rs+4 added
40 unmodified lines41 /// All run time measurements42 #[serde(skip_serializing_if = "Option::is_none")]43 pub times: Option<Vec<Second>>,44 45+ /// Maximum memory usage of the process, in bytes46+ #[serde(skip_serializing_if = "Option::is_none")]47+ pub memory_usage_byte: Option<Vec<u64>>,48+49 /// Exit codes of all command invocations50 pub exit_codes: Vec<Option<i32>>,51 52 /// Parameter values for this benchmark3 unmodified linesmodified:src/benchmark/executor.rs+4 added
131 unmodified lines132 TimingResult {133 time_real: result.time_real,134 time_user: result.time_user,135 time_system: result.time_system,136+ memory_usage_byte: result.memory_usage_byte,137 },138 result.status,139 ))140 }63 unmodified lines204 TimingResult {205 time_real: result.time_real,206 time_user: result.time_user,207 time_system: result.time_system,208+ memory_usage_byte: result.memory_usage_byte,209 },210 result.status,211 ))212 }56 unmodified lines269 self.shell_spawning_time = Some(TimingResult {270 time_real: mean(×_real),271 time_user: mean(×_user),272 time_system: mean(×_system),273+ memory_usage_byte: 0,274 });275 276 Ok(())277 }47 unmodified lines325 TimingResult {326 time_real: Self::extract_time(command.get_command_line()),327 time_user: 0.0,328 time_system: 0.0,329+ memory_usage_byte: 0,330 },331 status,332 ))333 }17 unmodified linesmodified:src/benchmark/mod.rs+4 added
149 unmodified lines150 151 let mut times_real: Vec<Second> = vec![];152 let mut times_user: Vec<Second> = vec![];153 let mut times_system: Vec<Second> = vec![];154+ let mut memory_usage_byte: Vec<u64> = vec![];155 let mut exit_codes: Vec<Option<i32>> = vec![];156 let mut all_succeeded = true;157 158 let output_policy = &self.options.command_output_policies[self.number];120 unmodified lines279 // Save the first result280 times_real.push(res.time_real);281 times_user.push(res.time_user);282 times_system.push(res.time_system);283+ memory_usage_byte.push(res.memory_usage_byte);284 exit_codes.push(extract_exit_code(status));285 286 all_succeeded = all_succeeded && success;287 28 unmodified lines316 317 times_real.push(res.time_real);318 times_user.push(res.time_user);319 times_system.push(res.time_system);320+ memory_usage_byte.push(res.memory_usage_byte);321 exit_codes.push(extract_exit_code(status));322 323 all_succeeded = all_succeeded && success;324 128 unmodified lines453 system: system_mean,454 min: t_min,455 max: t_max,456 times: Some(times_real),457+ memory_usage_byte: Some(memory_usage_byte),458 exit_codes,459 parameters: self460 .command461 .get_parameters()6 unmodified linesmodified:src/benchmark/relative_speed.rs+1 added
131 unmodified lines132 system: 0.0,133 min: mean,134 max: mean,135 times: None,136+ memory_usage_byte: None,137 exit_codes: Vec::new(),138 parameters: BTreeMap::new(),139 }140 }40 unmodified linesmodified:src/benchmark/timing_result.rs+3 added
9 unmodified lines10 pub time_user: Second,11 12 /// Time spent in kernel mode13 pub time_system: Second,14+15+ /// Maximum amount of memory used, in bytes16+ pub memory_usage_byte: u64,17 }modified:src/export/asciidoc.rs+4 added
103 unmodified lines104 system: 4.0,105 min: 5.0,106 max: 6.0,107 times: Some(vec![7.0, 8.0, 9.0]),108+ memory_usage_byte: None,109 exit_codes: vec![Some(0), Some(0), Some(0)],110 parameters: {111 let mut params = BTreeMap::new();112 params.insert("foo".into(), "1".into());11 unmodified lines124 system: 14.0,125 min: 15.0,126 max: 16.0,127 times: Some(vec![17.0, 18.0, 19.0]),128+ memory_usage_byte: None,129 exit_codes: vec![Some(0), Some(0), Some(0)],130 parameters: {131 let mut params = BTreeMap::new();132 params.insert("foo".into(), "1".into());48 unmodified lines181 system: 0.014,182 min: 0.015,183 max: 0.016,184 times: Some(vec![0.017, 0.018, 0.019]),185+ memory_usage_byte: None,186 exit_codes: vec![Some(0), Some(0), Some(0)],187 parameters: {188 let mut params = BTreeMap::new();189 params.insert("foo".into(), "1".into());11 unmodified lines201 system: 4.0,202 min: 5.0,203 max: 6.0,204 times: Some(vec![7.0, 8.0, 9.0]),205+ memory_usage_byte: None,206 exit_codes: vec![Some(0), Some(0), Some(0)],207 parameters: {208 let mut params = BTreeMap::new();209 params.insert("foo".into(), "1".into());32 unmodified linesmodified:src/export/csv.rs+2 added
75 unmodified lines76 system: 4.0,77 min: 5.0,78 max: 6.0,79 times: Some(vec![7.0, 8.0, 9.0]),80+ memory_usage_byte: None,81 exit_codes: vec![Some(0), Some(0), Some(0)],82 parameters: {83 let mut params = BTreeMap::new();84 params.insert("foo".into(), "one".into());11 unmodified lines96 system: 14.0,97 min: 15.0,98 max: 16.5,99 times: Some(vec![17.0, 18.0, 19.0]),100+ memory_usage_byte: None,101 exit_codes: vec![Some(0), Some(0), Some(0)],102 parameters: {103 let mut params = BTreeMap::new();104 params.insert("foo".into(), "one".into());20 unmodified linesmodified:src/export/markdown.rs+8 added
80 unmodified lines81 system: 0.0011,82 min: 0.1023,83 max: 0.1080,84 times: Some(vec![0.1, 0.1, 0.1]),85+ memory_usage_byte: None,86 exit_codes: vec![Some(0), Some(0), Some(0)],87 parameters: BTreeMap::new(),88 },89 BenchmarkResult {6 unmodified lines96 system: 0.0012,97 min: 2.0020,98 max: 2.0080,99 times: Some(vec![2.0, 2.0, 2.0]),100+ memory_usage_byte: None,101 exit_codes: vec![Some(0), Some(0), Some(0)],102 parameters: BTreeMap::new(),103 },104 ];35 unmodified lines140 system: 0.0012,141 min: 2.0020,142 max: 2.0080,143 times: Some(vec![2.0, 2.0, 2.0]),144+ memory_usage_byte: None,145 exit_codes: vec![Some(0), Some(0), Some(0)],146 parameters: BTreeMap::new(),147 },148 BenchmarkResult {6 unmodified lines155 system: 0.0011,156 min: 0.1023,157 max: 0.1080,158 times: Some(vec![0.1, 0.1, 0.1]),159+ memory_usage_byte: None,160 exit_codes: vec![Some(0), Some(0), Some(0)],161 parameters: BTreeMap::new(),162 },163 ];35 unmodified lines199 system: 0.0012,200 min: 2.0020,201 max: 2.0080,202 times: Some(vec![2.0, 2.0, 2.0]),203+ memory_usage_byte: None,204 exit_codes: vec![Some(0), Some(0), Some(0)],205 parameters: BTreeMap::new(),206 },207 BenchmarkResult {6 unmodified lines214 system: 0.0011,215 min: 0.1023,216 max: 0.1080,217 times: Some(vec![0.1, 0.1, 0.1]),218+ memory_usage_byte: None,219 exit_codes: vec![Some(0), Some(0), Some(0)],220 parameters: BTreeMap::new(),221 },222 ];56 unmodified lines279 system: 0.0012,280 min: 2.0020,281 max: 2.0080,282 times: Some(vec![2.0, 2.0, 2.0]),283+ memory_usage_byte: None,284 exit_codes: vec![Some(0), Some(0), Some(0)],285 parameters: BTreeMap::new(),286 },287 BenchmarkResult {6 unmodified lines294 system: 0.0011,295 min: 0.1023,296 max: 0.1080,297 times: Some(vec![0.1, 0.1, 0.1]),298+ memory_usage_byte: None,299 exit_codes: vec![Some(0), Some(0), Some(0)],300 parameters: BTreeMap::new(),301 },302 ];17 unmodified linesmodified:src/export/markup.rs+8 added
134 unmodified lines135 system: 0.0012,136 min: 2.0020,137 max: 2.0080,138 times: Some(vec![2.0, 2.0, 2.0]),139+ memory_usage_byte: None,140 exit_codes: vec![Some(0), Some(0), Some(0)],141 parameters: BTreeMap::new(),142 },143 BenchmarkResult {6 unmodified lines150 system: 0.0011,151 min: 0.1023,152 max: 0.1080,153 times: Some(vec![0.1, 0.1, 0.1]),154+ memory_usage_byte: None,155 exit_codes: vec![Some(0), Some(0), Some(0)],156 parameters: BTreeMap::new(),157 },158 ];20 unmodified lines179 system: 0.0012,180 min: 2.0020,181 max: 2.0080,182 times: Some(vec![2.0, 2.0, 2.0]),183+ memory_usage_byte: None,184 exit_codes: vec![Some(0), Some(0), Some(0)],185 parameters: BTreeMap::new(),186 },187 BenchmarkResult {6 unmodified lines194 system: 0.0011,195 min: 0.1023,196 max: 0.1080,197 times: Some(vec![0.1, 0.1, 0.1]),198+ memory_usage_byte: None,199 exit_codes: vec![Some(0), Some(0), Some(0)],200 parameters: BTreeMap::new(),201 },202 ];20 unmodified lines223 system: 0.0012,224 min: 2.0020,225 max: 2.0080,226 times: Some(vec![2.0, 2.0, 2.0]),227+ memory_usage_byte: None,228 exit_codes: vec![Some(0), Some(0), Some(0)],229 parameters: BTreeMap::new(),230 },231 BenchmarkResult {6 unmodified lines238 system: 0.0011,239 min: 0.1023,240 max: 0.1080,241 times: Some(vec![0.1, 0.1, 0.1]),242+ memory_usage_byte: None,243 exit_codes: vec![Some(0), Some(0), Some(0)],244 parameters: BTreeMap::new(),245 },246 ];20 unmodified lines267 system: 0.0011,268 min: 0.1023,269 max: 0.1080,270 times: Some(vec![0.1, 0.1, 0.1]),271+ memory_usage_byte: None,272 exit_codes: vec![Some(0), Some(0), Some(0)],273 parameters: BTreeMap::new(),274 },275 BenchmarkResult {6 unmodified lines282 system: 0.0012,283 min: 2.0020,284 max: 2.0080,285 times: Some(vec![2.0, 2.0, 2.0]),286+ memory_usage_byte: None,287 exit_codes: vec![Some(0), Some(0), Some(0)],288 parameters: BTreeMap::new(),289 },290 ];19 unmodified linesmodified:src/export/orgmode.rs+4 added
86 unmodified lines87 system: 0.0011,88 min: 0.1023,89 max: 0.1080,90 times: Some(vec![0.1, 0.1, 0.1]),91+ memory_usage_byte: None,92 exit_codes: vec![Some(0), Some(0), Some(0)],93 parameters: BTreeMap::new(),94 },95 BenchmarkResult {6 unmodified lines102 system: 0.0012,103 min: 2.0020,104 max: 2.0080,105 times: Some(vec![2.0, 2.0, 2.0]),106+ memory_usage_byte: None,107 exit_codes: vec![Some(0), Some(0), Some(0)],108 parameters: BTreeMap::new(),109 },110 ];37 unmodified lines148 system: 0.0012,149 min: 2.0020,150 max: 2.0080,151 times: Some(vec![2.0, 2.0, 2.0]),152+ memory_usage_byte: None,153 exit_codes: vec![Some(0), Some(0), Some(0)],154 parameters: BTreeMap::new(),155 },156 BenchmarkResult {6 unmodified lines163 system: 0.0011,164 min: 0.1023,165 max: 0.1080,166 times: Some(vec![0.1, 0.1, 0.1]),167+ memory_usage_byte: None,168 exit_codes: vec![Some(0), Some(0), Some(0)],169 parameters: BTreeMap::new(),170 },171 ];17 unmodified linesmodified:src/timer/mod.rs+6 added-2 removed
30 unmodified lines31 pub user_usec: i64,32 33 /// Total amount of time spent executing in kernel mode34 pub system_usec: i64,35+36+ /// Maximum amount of memory used by the process, in bytes37+ pub memory_usage_byte: u64,38 }39 40 /// Used to indicate the result of running a command41 #[derive(Debug, Copy, Clone)]42 pub struct TimerResult {43 pub time_real: Second,44 pub time_user: Second,45 pub time_system: Second,43-46+ pub memory_usage_byte: u64,47 /// The exit status of the process48 pub status: ExitStatus,49 }50 57 unmodified lines108 109 let status = child.wait()?;110 111 let time_real = wallclock_timer.stop();109- let (time_user, time_system) = cpu_timer.stop();112+ let (time_user, time_system, memory_usage_byte) = cpu_timer.stop();113 114 Ok(TimerResult {115 time_real,116 time_user,117 time_system,118+ memory_usage_byte,119 status,120 })121 }modified:src/timer/unix_timer.rs+17 added-2 removed
1 #![cfg(not(windows))]2 3+use std::convert::TryFrom;4 use std::mem;5 6 use crate::timer::CPUTimes;7 use crate::util::units::Second;17 unmodified lines25 start_cpu: get_cpu_times(),26 }27 }28 28- pub fn stop(&self) -> (Second, Second) {29+ pub fn stop(&self) -> (Second, Second, u64) {30 let end_cpu = get_cpu_times();31 let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);31- (cpu_interval.user, cpu_interval.system)32+ (33+ cpu_interval.user,34+ cpu_interval.system,35+ end_cpu.memory_usage_byte,36+ )37 }38 }39 40 /// Read CPU execution times ('user' and 'system')8 unmodified lines49 };50 51 const MICROSEC_PER_SEC: i64 = 1000 * 1000;52 53+ // Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes54+ let max_rss_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {55+ result.ru_maxrss56+ } else {57+ result.ru_maxrss * 102458+ };59+60 #[allow(clippy::useless_conversion)]61 CPUTimes {62 user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC63 + i64::from(result.ru_utime.tv_usec),64 system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC65 + i64::from(result.ru_stime.tv_usec),66+ memory_usage_byte: u64::try_from(max_rss_byte).unwrap_or(0),67 }68 }69 70 /// Compute the time intervals in between two `CPUTimes` snapshots11 unmodified lines82 fn test_cpu_time_interval() {83 let t_a = CPUTimes {84 user_usec: 12345,85 system_usec: 54321,86+ memory_usage_byte: 0,87 };88 89 let t_b = CPUTimes {90 user_usec: 20000,91 system_usec: 70000,92+ memory_usage_byte: 0,93 };94 95 let t_zero = cpu_time_interval(&t_a, &t_a);96 assert!(t_zero.user.abs() < f64::EPSILON);10 unmodified linesmodified:src/timer/windows_timer.rs+3 added-3 removed
84 unmodified lines85 86 Self { job_object }87 }88 89- pub fn stop(&self) -> (Second, Second) {89+ pub fn stop(&self) -> (Second, Second, u64) {90 let mut job_object_info =91 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();92 93 // SAFETY: A valid job object got created in `start_suspended_process`19 unmodified lines113 // The `TotalKernelTime` is "The total amount of kernel-mode execution time114 // for all active processes associated with the job, as well as all terminated115 // processes no longer associated with the job, in 100-nanosecond ticks."116 let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS;117- (user as f64 * 1e-6, kernel as f64 * 1e-6)117+ (user as f64 * 1e-6, kernel as f64 * 1e-6, 0)118 } else {119- (0.0, 0.0)119+ (0.0, 0.0, 0)120 }121 }122 }123 6 unmodified lines