Skip to content

Commit

Permalink
Use RUSAGE_SELF for getrusage; enable max_rss metric for MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnski committed Feb 2, 2023
1 parent 3865da1 commit becf7a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion node/core/pvf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ sp-wasm-interface = { git = "https://github.com/paritytech/substrate", branch =
sp-maybe-compressed-blob = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" }

[target.'cfg(target_os = "linux")'.dependencies]
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
libc = "0.2.139"

[dev-dependencies]
Expand Down
25 changes: 15 additions & 10 deletions node/core/pvf/src/prepare/memory_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use std::{
use tikv_jemalloc_ctl::{epoch, stats, Error};
use tokio::task::JoinHandle;

#[cfg(target_os = "linux")]
use libc::{getrusage, rusage, timeval, RUSAGE_THREAD};
#[cfg(any(target_os = "linux", target_os = "macos"))]
use libc::{getrusage, rusage, timeval, RUSAGE_SELF};

/// Helper struct to contain all the memory stats, including [`MemoryAllocationStats`] and, if
/// supported by the OS, `ru_maxrss`.
Expand Down Expand Up @@ -87,9 +87,9 @@ impl MemoryAllocationTracker {
}
}

/// Get the rusage stats for the current thread.
#[cfg(target_os = "linux")]
fn getrusage_thread() -> io::Result<rusage> {
/// Get the rusage stats for all threads in the current process.
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn getrusage_process() -> io::Result<rusage> {
let mut result = rusage {
ru_utime: timeval { tv_sec: 0, tv_usec: 0 },
ru_stime: timeval { tv_sec: 0, tv_usec: 0 },
Expand All @@ -108,19 +108,24 @@ fn getrusage_thread() -> io::Result<rusage> {
ru_nvcsw: 0,
ru_nivcsw: 0,
};
if unsafe { getrusage(RUSAGE_THREAD, &mut result) } == -1 {
if unsafe { getrusage(RUSAGE_SELF, &mut result) } == -1 {
return Err(io::Error::last_os_error())
}
Ok(result)
}

/// Gets the `ru_maxrss` for the current thread if the OS supports `getrusage`. Otherwise, just
/// Gets the `ru_maxrss` for the current process if the OS supports `getrusage`. Otherwise, just
/// returns `None`.
pub fn get_max_rss_thread() -> Option<io::Result<i64>> {
///
/// The returned value is always in kilobytes.
pub fn get_max_rss_process() -> Option<io::Result<i64>> {
// `c_long` is either `i32` or `i64` depending on architecture. `i64::from` always works.
#[cfg(target_os = "linux")]
let max_rss = Some(getrusage_thread().map(|rusage| i64::from(rusage.ru_maxrss)));
#[cfg(not(target_os = "linux"))]
let max_rss = Some(getrusage_process().map(|rusage| i64::from(rusage.ru_maxrss)));
// Macos returns this in bytes, so convert to kilobytes.
#[cfg(target_os = "macos")]
let max_rss = Some(getrusage_process().map(|rusage| i64::from(rusage.ru_maxrss) / 1024));
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
let max_rss = None;
max_rss
}
Expand Down
8 changes: 4 additions & 4 deletions node/core/pvf/src/prepare/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use super::memory_stats::{
get_max_rss_thread, get_memory_tracker_loop_stats, memory_tracker_loop, observe_memory_metrics,
MemoryStats,
get_max_rss_process, get_memory_tracker_loop_stats, memory_tracker_loop,
observe_memory_metrics, MemoryStats,
};
use crate::{
artifacts::CompiledArtifact,
Expand Down Expand Up @@ -389,8 +389,8 @@ pub fn worker_entrypoint(socket_path: &str) {
.spawn_blocking(move || {
let prepare_result = prepare_artifact(&code);

// Get the `ru_maxrss` stat. If supported, call getrusage for the thread.
let max_rss = get_max_rss_thread();
// Get the `ru_maxrss` stat. If supported, call getrusage for the process.
let max_rss = get_max_rss_process();

(prepare_result, max_rss)
})
Expand Down

0 comments on commit becf7a8

Please sign in to comment.