From 2c1ca98abaf6756d3246841cc3cf4ca3f617efca Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 9 Jun 2021 22:55:50 +0200 Subject: Make `MemoryUsage` work on Windows --- crates/profile/src/memory_usage.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'crates/profile/src') diff --git a/crates/profile/src/memory_usage.rs b/crates/profile/src/memory_usage.rs index 2917ded60..6ef58c9c1 100644 --- a/crates/profile/src/memory_usage.rs +++ b/crates/profile/src/memory_usage.rs @@ -35,6 +35,22 @@ impl MemoryUsage { // Note: This is incredibly slow. let alloc = unsafe { libc::mallinfo() }.uordblks as isize; MemoryUsage { allocated: Bytes(alloc) } + } else if #[cfg(windows)] { + // There doesn't seem to be an API for determining heap usage, so we try to + // approximate that by using the Commit Charge value. + + use winapi::um::processthreadsapi::*; + use winapi::um::psapi::*; + use std::mem::{MaybeUninit, size_of}; + + let proc = unsafe { GetCurrentProcess() }; + let mut mem_counters = MaybeUninit::uninit(); + let cb = size_of::(); + let ret = unsafe { GetProcessMemoryInfo(proc, mem_counters.as_mut_ptr(), cb as u32) }; + assert!(ret != 0); + + let usage = unsafe { mem_counters.assume_init().PagefileUsage }; + MemoryUsage { allocated: Bytes(usage as isize) } } else { MemoryUsage { allocated: Bytes(0) } } -- cgit v1.2.3