Skip to content

Commit b97fd3e

Browse files
committed
Auto merge of #82754 - rylev:rusage-windows, r=pnkfelix
Attempt to gather similar stats as rusage on Windows A follow up to #82532. This is a bit hacked in because I think we need to discuss this before merging, but this is an attempt to gather similar metrics as `libc::rusage` on Windows. Some comments on differences: * Currently, we're passing `RUSAGE_CHILDREN` to `rusage` which collects statistics on all children that have been waited on and terminated. I believe this is currently just the invocation of the real `rustc` that the shim is wrapping. Does `rustc` itself spawn children processes? The windows version gets the child processes handle when spawning it, and uses that to collect the statistics. For maxrss, `rusage` will return "the resident set size of the largest child, not the maximum resident set size of the process tree.", the Windows version will only collect statistics on the wrapped `rustc` child process directly even if some theoretical sub process has a larger memory footprint. * There might be subtle differences between `rusage`'s "resident set" and Window's "working set". The "working set" and "resident set" should both be the number of pages that are in memory and which would not cause a page fault when accessed. * I'm not yet sure how best to get the same information that `ru_minflt`, `ru_inblock`, `ru_oublock`, `ru_nivcsw ` and `ru_nvcsw` provide. r? `@pnkfelix`
2 parents eb95ace + 302867c commit b97fd3e

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

src/bootstrap/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ merge = "0.1.0"
5353

5454
[target.'cfg(windows)'.dependencies.winapi]
5555
version = "0.3"
56-
features = ["fileapi", "ioapiset", "jobapi2", "handleapi", "winioctl"]
56+
features = ["fileapi", "ioapiset", "jobapi2", "handleapi", "winioctl", "psapi", "impl-default"]
5757

5858
[dev-dependencies]
5959
pretty_assertions = "0.6"

src/bootstrap/bin/rustc.rs

+75-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
use std::env;
1919
use std::path::PathBuf;
20-
use std::process::Command;
20+
use std::process::{Child, Command};
2121
use std::str::FromStr;
2222
use std::time::Instant;
2323

@@ -163,9 +163,11 @@ fn main() {
163163
}
164164

165165
let start = Instant::now();
166-
let status = {
166+
let (child, status) = {
167167
let errmsg = format!("\nFailed to run:\n{:?}\n-------------", cmd);
168-
cmd.status().expect(&errmsg)
168+
let mut child = cmd.spawn().expect(&errmsg);
169+
let status = child.wait().expect(&errmsg);
170+
(child, status)
169171
};
170172

171173
if env::var_os("RUSTC_PRINT_STEP_TIMINGS").is_some()
@@ -176,7 +178,7 @@ fn main() {
176178
// If the user requested resource usage data, then
177179
// include that in addition to the timing output.
178180
let rusage_data =
179-
env::var_os("RUSTC_PRINT_STEP_RUSAGE").and_then(|_| format_rusage_data());
181+
env::var_os("RUSTC_PRINT_STEP_RUSAGE").and_then(|_| format_rusage_data(child));
180182
eprintln!(
181183
"[RUSTC-TIMING] {} test:{} {}.{:03}{}{}",
182184
crate_name,
@@ -213,19 +215,83 @@ fn main() {
213215
}
214216
}
215217

216-
#[cfg(not(unix))]
217-
/// getrusage is not available on non-unix platforms. So for now, we do not
218-
/// bother trying to make a shim for it.
219-
fn format_rusage_data() -> Option<String> {
218+
#[cfg(all(not(unix), not(windows)))]
219+
// In the future we can add this for more platforms
220+
fn format_rusage_data(_child: Child) -> Option<String> {
220221
None
221222
}
222223

224+
#[cfg(windows)]
225+
fn format_rusage_data(child: Child) -> Option<String> {
226+
use std::os::windows::io::AsRawHandle;
227+
use winapi::um::{processthreadsapi, psapi, timezoneapi};
228+
let handle = child.as_raw_handle();
229+
macro_rules! try_bool {
230+
($e:expr) => {
231+
if $e != 1 {
232+
return None;
233+
}
234+
};
235+
}
236+
237+
let mut user_filetime = Default::default();
238+
let mut user_time = Default::default();
239+
let mut kernel_filetime = Default::default();
240+
let mut kernel_time = Default::default();
241+
let mut memory_counters = psapi::PROCESS_MEMORY_COUNTERS::default();
242+
243+
unsafe {
244+
try_bool!(processthreadsapi::GetProcessTimes(
245+
handle,
246+
&mut Default::default(),
247+
&mut Default::default(),
248+
&mut kernel_filetime,
249+
&mut user_filetime,
250+
));
251+
try_bool!(timezoneapi::FileTimeToSystemTime(&user_filetime, &mut user_time));
252+
try_bool!(timezoneapi::FileTimeToSystemTime(&kernel_filetime, &mut kernel_time));
253+
254+
// Unlike on Linux with RUSAGE_CHILDREN, this will only return memory information for the process
255+
// with the given handle and none of that process's children.
256+
try_bool!(psapi::GetProcessMemoryInfo(
257+
handle as _,
258+
&mut memory_counters as *mut _ as _,
259+
std::mem::size_of::<psapi::PROCESS_MEMORY_COUNTERS_EX>() as u32,
260+
));
261+
}
262+
263+
// Guide on interpreting these numbers:
264+
// https://docs.microsoft.com/en-us/windows/win32/psapi/process-memory-usage-information
265+
let peak_working_set = memory_counters.PeakWorkingSetSize / 1024;
266+
let peak_page_file = memory_counters.PeakPagefileUsage / 1024;
267+
let peak_paged_pool = memory_counters.QuotaPeakPagedPoolUsage / 1024;
268+
let peak_nonpaged_pool = memory_counters.QuotaPeakNonPagedPoolUsage / 1024;
269+
Some(format!(
270+
"user: {USER_SEC}.{USER_USEC:03} \
271+
sys: {SYS_SEC}.{SYS_USEC:03} \
272+
peak working set (kb): {PEAK_WORKING_SET} \
273+
peak page file usage (kb): {PEAK_PAGE_FILE} \
274+
peak paged pool usage (kb): {PEAK_PAGED_POOL} \
275+
peak non-paged pool usage (kb): {PEAK_NONPAGED_POOL} \
276+
page faults: {PAGE_FAULTS}",
277+
USER_SEC = user_time.wSecond + (user_time.wMinute * 60),
278+
USER_USEC = user_time.wMilliseconds,
279+
SYS_SEC = kernel_time.wSecond + (kernel_time.wMinute * 60),
280+
SYS_USEC = kernel_time.wMilliseconds,
281+
PEAK_WORKING_SET = peak_working_set,
282+
PEAK_PAGE_FILE = peak_page_file,
283+
PEAK_PAGED_POOL = peak_paged_pool,
284+
PEAK_NONPAGED_POOL = peak_nonpaged_pool,
285+
PAGE_FAULTS = memory_counters.PageFaultCount,
286+
))
287+
}
288+
223289
#[cfg(unix)]
224290
/// Tries to build a string with human readable data for several of the rusage
225291
/// fields. Note that we are focusing mainly on data that we believe to be
226292
/// supplied on Linux (the `rusage` struct has other fields in it but they are
227293
/// currently unsupported by Linux).
228-
fn format_rusage_data() -> Option<String> {
294+
fn format_rusage_data(_child: Child) -> Option<String> {
229295
let rusage: libc::rusage = unsafe {
230296
let mut recv = std::mem::zeroed();
231297
// -1 is RUSAGE_CHILDREN, which means to get the rusage for all children

0 commit comments

Comments
 (0)