Skip to content

Commit e776286

Browse files
committed
bootstrap: use nix crate to get rusage on unix
1 parent fbccf50 commit e776286

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/bootstrap/Cargo.lock

+19
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ dependencies = [
5252
"ignore",
5353
"junction",
5454
"libc",
55+
"nix",
5556
"object",
5657
"opener",
5758
"pretty_assertions",
@@ -100,6 +101,12 @@ version = "1.0.0"
100101
source = "registry+https://github.com/rust-lang/crates.io-index"
101102
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
102103

104+
[[package]]
105+
name = "cfg_aliases"
106+
version = "0.2.1"
107+
source = "registry+https://github.com/rust-lang/crates.io-index"
108+
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
109+
103110
[[package]]
104111
name = "clap"
105112
version = "4.4.13"
@@ -360,6 +367,18 @@ version = "2.7.1"
360367
source = "registry+https://github.com/rust-lang/crates.io-index"
361368
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
362369

370+
[[package]]
371+
name = "nix"
372+
version = "0.29.0"
373+
source = "registry+https://github.com/rust-lang/crates.io-index"
374+
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
375+
dependencies = [
376+
"bitflags 2.4.1",
377+
"cfg-if",
378+
"cfg_aliases",
379+
"libc",
380+
]
381+
363382
[[package]]
364383
name = "ntapi"
365384
version = "0.4.1"

src/bootstrap/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fd-lock = "4.0"
4747
home = "0.5"
4848
ignore = "0.4"
4949
libc = "0.2"
50+
nix = { version = "0.29.0", features = ["resource"] }
5051
object = { version = "0.32", default-features = false, features = ["archive", "coff", "read_core", "unaligned"] }
5152
opener = "0.5"
5253
semver = "1.0"

src/bootstrap/src/bin/rustc.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -382,29 +382,25 @@ fn format_rusage_data(child: Child) -> Option<String> {
382382
/// supplied on Linux (the `rusage` struct has other fields in it but they are
383383
/// currently unsupported by Linux).
384384
fn format_rusage_data(_child: Child) -> Option<String> {
385-
let rusage: libc::rusage = unsafe {
386-
let mut recv = std::mem::zeroed();
387-
// -1 is RUSAGE_CHILDREN, which means to get the rusage for all children
388-
// (and grandchildren, etc) processes that have respectively terminated
389-
// and been waited for.
390-
let retval = libc::getrusage(-1, &mut recv);
391-
if retval != 0 {
392-
return None;
393-
}
394-
recv
395-
};
385+
use nix::sys::resource::{getrusage, UsageWho};
386+
387+
// RUSAGE_CHILDREN (= -1) means to get the rusage for all children
388+
// (and grandchildren, etc) processes that have respectively terminated
389+
// and been waited for.
390+
let rusage = getrusage(UsageWho::RUSAGE_CHILDREN).ok()?;
391+
396392
// Mac OS X reports the maxrss in bytes, not kb.
397393
let divisor = if env::consts::OS == "macos" { 1024 } else { 1 };
398-
let maxrss = (rusage.ru_maxrss + (divisor - 1)) / divisor;
394+
let maxrss = (rusage.max_rss() + (divisor - 1)) / divisor;
399395

400396
let mut init_str = format!(
401397
"user: {USER_SEC}.{USER_USEC:03} \
402398
sys: {SYS_SEC}.{SYS_USEC:03} \
403399
max rss (kb): {MAXRSS}",
404-
USER_SEC = rusage.ru_utime.tv_sec,
405-
USER_USEC = rusage.ru_utime.tv_usec,
406-
SYS_SEC = rusage.ru_stime.tv_sec,
407-
SYS_USEC = rusage.ru_stime.tv_usec,
400+
USER_SEC = rusage.user_time().tv_sec(),
401+
USER_USEC = rusage.user_time().tv_usec(),
402+
SYS_SEC = rusage.system_time().tv_sec(),
403+
SYS_USEC = rusage.system_time().tv_usec(),
408404
MAXRSS = maxrss
409405
);
410406

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

416-
let minflt = rusage.ru_minflt;
417-
let majflt = rusage.ru_majflt;
412+
let minflt = rusage.minor_page_faults();
413+
let majflt = rusage.major_page_faults();
418414
if minflt != 0 || majflt != 0 {
419415
init_str.push_str(&format!(" page reclaims: {minflt} page faults: {majflt}"));
420416
}
421417

422-
let inblock = rusage.ru_inblock;
423-
let oublock = rusage.ru_oublock;
418+
let inblock = rusage.block_reads();
419+
let oublock = rusage.block_writes();
424420
if inblock != 0 || oublock != 0 {
425421
init_str.push_str(&format!(" fs block inputs: {inblock} fs block outputs: {oublock}"));
426422
}
427423

428-
let nvcsw = rusage.ru_nvcsw;
429-
let nivcsw = rusage.ru_nivcsw;
424+
let nvcsw = rusage.voluntary_context_switches();
425+
let nivcsw = rusage.involuntary_context_switches();
430426
if nvcsw != 0 || nivcsw != 0 {
431427
init_str.push_str(&format!(
432428
" voluntary ctxt switches: {nvcsw} involuntary ctxt switches: {nivcsw}"

0 commit comments

Comments
 (0)