Skip to content

Commit

Permalink
bootstrap: use nix crate to get rusage on unix
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigorenkoPV committed Jul 27, 2024
1 parent fbccf50 commit 787d59c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
19 changes: 19 additions & 0 deletions src/bootstrap/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies = [
"ignore",
"junction",
"libc",
"nix",
"object",
"opener",
"pretty_assertions",
Expand Down Expand Up @@ -100,6 +101,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"

[[package]]
name = "clap"
version = "4.4.13"
Expand Down Expand Up @@ -360,6 +367,18 @@ version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"

[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.4.1",
"cfg-if",
"cfg_aliases",
"libc",
]

[[package]]
name = "ntapi"
version = "0.4.1"
Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ xz2 = "0.1"
# Dependencies needed by the build-metrics feature
sysinfo = { version = "0.30", default-features = false, optional = true }

[target.'cfg(unix)'.dependencies.nix]
version = "0.29.0"
features = ["resource"]

[target.'cfg(windows)'.dependencies.junction]
version = "1.0.0"

Expand Down
40 changes: 18 additions & 22 deletions src/bootstrap/src/bin/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,29 +382,25 @@ fn format_rusage_data(child: Child) -> Option<String> {
/// supplied on Linux (the `rusage` struct has other fields in it but they are
/// currently unsupported by Linux).
fn format_rusage_data(_child: Child) -> Option<String> {
let rusage: libc::rusage = unsafe {
let mut recv = std::mem::zeroed();
// -1 is RUSAGE_CHILDREN, which means to get the rusage for all children
// (and grandchildren, etc) processes that have respectively terminated
// and been waited for.
let retval = libc::getrusage(-1, &mut recv);
if retval != 0 {
return None;
}
recv
};
use nix::sys::resource::{getrusage, UsageWho};

// RUSAGE_CHILDREN (= -1) means to get the rusage for all children
// (and grandchildren, etc) processes that have respectively terminated
// and been waited for.
let rusage = getrusage(UsageWho::RUSAGE_CHILDREN).ok()?;

// Mac OS X reports the maxrss in bytes, not kb.
let divisor = if env::consts::OS == "macos" { 1024 } else { 1 };
let maxrss = (rusage.ru_maxrss + (divisor - 1)) / divisor;
let maxrss = (rusage.max_rss() + (divisor - 1)) / divisor;

let mut init_str = format!(
"user: {USER_SEC}.{USER_USEC:03} \
sys: {SYS_SEC}.{SYS_USEC:03} \
max rss (kb): {MAXRSS}",
USER_SEC = rusage.ru_utime.tv_sec,
USER_USEC = rusage.ru_utime.tv_usec,
SYS_SEC = rusage.ru_stime.tv_sec,
SYS_USEC = rusage.ru_stime.tv_usec,
USER_SEC = rusage.user_time().tv_sec(),
USER_USEC = rusage.user_time().tv_usec(),
SYS_SEC = rusage.system_time().tv_sec(),
SYS_USEC = rusage.system_time().tv_usec(),
MAXRSS = maxrss
);

Expand All @@ -413,20 +409,20 @@ fn format_rusage_data(_child: Child) -> Option<String> {
// either means no events of that type occurred, or that the platform
// does not support it.

let minflt = rusage.ru_minflt;
let majflt = rusage.ru_majflt;
let minflt = rusage.minor_page_faults();
let majflt = rusage.major_page_faults();
if minflt != 0 || majflt != 0 {
init_str.push_str(&format!(" page reclaims: {minflt} page faults: {majflt}"));
}

let inblock = rusage.ru_inblock;
let oublock = rusage.ru_oublock;
let inblock = rusage.block_reads();
let oublock = rusage.block_writes();
if inblock != 0 || oublock != 0 {
init_str.push_str(&format!(" fs block inputs: {inblock} fs block outputs: {oublock}"));
}

let nvcsw = rusage.ru_nvcsw;
let nivcsw = rusage.ru_nivcsw;
let nvcsw = rusage.voluntary_context_switches();
let nivcsw = rusage.involuntary_context_switches();
if nvcsw != 0 || nivcsw != 0 {
init_str.push_str(&format!(
" voluntary ctxt switches: {nvcsw} involuntary ctxt switches: {nivcsw}"
Expand Down

0 comments on commit 787d59c

Please sign in to comment.