Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial work on tracking memory usage #753

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/benchmark/benchmark_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub struct BenchmarkResult {
/// Exit codes of all command invocations
pub exit_codes: Vec<Option<i32>>,

/// All max. memory sizes
#[serde(skip_serializing_if = "Option::is_none")]
pub max_rss_byte: Option<Vec<Second>>,

/// Parameter values for this benchmark
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub parameters: BTreeMap<String, String>,
Expand Down
4 changes: 4 additions & 0 deletions src/benchmark/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl<'a> Executor for RawExecutor<'a> {
time_real: result.time_real,
time_user: result.time_user,
time_system: result.time_system,
max_rss_byte: Some(result.max_rss_byte),
},
result.status,
))
Expand Down Expand Up @@ -167,6 +168,7 @@ impl<'a> Executor for ShellExecutor<'a> {
time_real: result.time_real,
time_user: result.time_user,
time_system: result.time_system,
max_rss_byte: Some(result.max_rss_byte),
},
result.status,
))
Expand Down Expand Up @@ -226,6 +228,7 @@ impl<'a> Executor for ShellExecutor<'a> {
time_real: mean(&times_real),
time_user: mean(&times_user),
time_system: mean(&times_system),
max_rss_byte: None,
});

Ok(())
Expand Down Expand Up @@ -279,6 +282,7 @@ impl Executor for MockExecutor {
time_real: Self::extract_time(command.get_command_line()),
time_user: 0.0,
time_system: 0.0,
max_rss_byte: None,
},
status,
))
Expand Down
3 changes: 3 additions & 0 deletions src/benchmark/timing_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ pub struct TimingResult {

/// Time spent in kernel mode
pub time_system: Second,

/// Amount of memory used
pub max_rss_byte: Option<i64>,
}
8 changes: 6 additions & 2 deletions src/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ struct CPUTimes {

/// Total amount of time spent executing in kernel mode
pub system_usec: i64,

/// Total amount of memory used by the process
pub max_rss_byte: i64,
}

/// Used to indicate the result of running a command
Expand All @@ -40,7 +43,7 @@ pub struct TimerResult {
pub time_real: Second,
pub time_user: Second,
pub time_system: Second,

pub max_rss_byte: i64,
/// The exit status of the process
pub status: ExitStatus,
}
Expand Down Expand Up @@ -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, max_rss_byte) = cpu_timer.stop();

Ok(TimerResult {
time_real,
time_user,
time_system,
max_rss_byte,
status,
})
}
14 changes: 12 additions & 2 deletions src/timer/unix_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ impl CPUTimer {
}
}

pub fn stop(&self) -> (Second, Second) {
pub fn stop(&self) -> (Second, Second, i64) {
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.max_rss_byte)
}
}

Expand All @@ -45,12 +45,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),
max_rss_byte,
}
}

Expand All @@ -70,11 +78,13 @@ fn test_cpu_time_interval() {
let t_a = CPUTimes {
user_usec: 12345,
system_usec: 54321,
max_rss_byte: 0,
};

let t_b = CPUTimes {
user_usec: 20000,
system_usec: 70000,
max_rss_byte: 0,
};

let t_zero = cpu_time_interval(&t_a, &t_a);
Expand Down
Loading