From e7762864ac3a65cfd58edee13f32566833de88f1 Mon Sep 17 00:00:00 2001 From: Pavel Grigorenko Date: Sun, 28 Jul 2024 01:52:01 +0300 Subject: [PATCH] bootstrap: use `nix` crate to get rusage on unix --- src/bootstrap/Cargo.lock | 19 ++++++++++++++++ src/bootstrap/Cargo.toml | 1 + src/bootstrap/src/bin/rustc.rs | 40 +++++++++++++++------------------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/bootstrap/Cargo.lock b/src/bootstrap/Cargo.lock index de0924c0f4236..e3270654705b3 100644 --- a/src/bootstrap/Cargo.lock +++ b/src/bootstrap/Cargo.lock @@ -52,6 +52,7 @@ dependencies = [ "ignore", "junction", "libc", + "nix", "object", "opener", "pretty_assertions", @@ -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" @@ -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" diff --git a/src/bootstrap/Cargo.toml b/src/bootstrap/Cargo.toml index f723407c3ce31..94aaf7d49e20f 100644 --- a/src/bootstrap/Cargo.toml +++ b/src/bootstrap/Cargo.toml @@ -47,6 +47,7 @@ fd-lock = "4.0" home = "0.5" ignore = "0.4" libc = "0.2" +nix = { version = "0.29.0", features = ["resource"] } object = { version = "0.32", default-features = false, features = ["archive", "coff", "read_core", "unaligned"] } opener = "0.5" semver = "1.0" diff --git a/src/bootstrap/src/bin/rustc.rs b/src/bootstrap/src/bin/rustc.rs index 46e845f77ae1b..ea64bdef0cb5b 100644 --- a/src/bootstrap/src/bin/rustc.rs +++ b/src/bootstrap/src/bin/rustc.rs @@ -382,29 +382,25 @@ fn format_rusage_data(child: Child) -> Option { /// 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 { - 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 ); @@ -413,20 +409,20 @@ fn format_rusage_data(_child: Child) -> Option { // 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}"