Skip to content

Commit f4da4de

Browse files
9202: feat: Make `MemoryUsage` work on Windows r=jonas-schievink a=jonas-schievink Unfortunately there is no convenient API for heap statistics, so this instead uses the Commit Charge value, which is the amount of memory that needs to be allocated either in physical RAM or in the page file. This approximation seems to be good enough to find queries that waste a large amount of memory, but it should generally be expected to be off by several MB. bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents 660a896 + 2c1ca98 commit f4da4de

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/profile/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ jemalloc-ctl = { version = "0.4.1", package = "tikv-jemalloc-ctl", optional = tr
2020
[target.'cfg(target_os = "linux")'.dependencies]
2121
perf-event = "0.4"
2222

23+
[target.'cfg(windows)'.dependencies]
24+
winapi = { version = "0.3.8", features = ["psapi"] }
25+
2326
[features]
2427
cpu_profiler = []
2528
jemalloc = ["jemalloc-ctl"]

crates/profile/src/memory_usage.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ impl MemoryUsage {
3535
// Note: This is incredibly slow.
3636
let alloc = unsafe { libc::mallinfo() }.uordblks as isize;
3737
MemoryUsage { allocated: Bytes(alloc) }
38+
} else if #[cfg(windows)] {
39+
// There doesn't seem to be an API for determining heap usage, so we try to
40+
// approximate that by using the Commit Charge value.
41+
42+
use winapi::um::processthreadsapi::*;
43+
use winapi::um::psapi::*;
44+
use std::mem::{MaybeUninit, size_of};
45+
46+
let proc = unsafe { GetCurrentProcess() };
47+
let mut mem_counters = MaybeUninit::uninit();
48+
let cb = size_of::<PROCESS_MEMORY_COUNTERS>();
49+
let ret = unsafe { GetProcessMemoryInfo(proc, mem_counters.as_mut_ptr(), cb as u32) };
50+
assert!(ret != 0);
51+
52+
let usage = unsafe { mem_counters.assume_init().PagefileUsage };
53+
MemoryUsage { allocated: Bytes(usage as isize) }
3854
} else {
3955
MemoryUsage { allocated: Bytes(0) }
4056
}

0 commit comments

Comments
 (0)