aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-10 13:09:54 +0100
committerGitHub <[email protected]>2021-06-10 13:09:54 +0100
commitf4da4de7cdd4a7dfe40a417b0100b83ec50d1e1d (patch)
tree15339930fdaf23cf4ce941a99228d9cef1b9a75a /crates
parent660a89620f65225359559ddcaf158bdb9dfe0d4c (diff)
parent2c1ca98abaf6756d3246841cc3cf4ca3f617efca (diff)
Merge #9202
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 <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/profile/Cargo.toml3
-rw-r--r--crates/profile/src/memory_usage.rs16
2 files changed, 19 insertions, 0 deletions
diff --git a/crates/profile/Cargo.toml b/crates/profile/Cargo.toml
index 1a8c8f862..653d3d983 100644
--- a/crates/profile/Cargo.toml
+++ b/crates/profile/Cargo.toml
@@ -20,6 +20,9 @@ jemalloc-ctl = { version = "0.4.1", package = "tikv-jemalloc-ctl", optional = tr
20[target.'cfg(target_os = "linux")'.dependencies] 20[target.'cfg(target_os = "linux")'.dependencies]
21perf-event = "0.4" 21perf-event = "0.4"
22 22
23[target.'cfg(windows)'.dependencies]
24winapi = { version = "0.3.8", features = ["psapi"] }
25
23[features] 26[features]
24cpu_profiler = [] 27cpu_profiler = []
25jemalloc = ["jemalloc-ctl"] 28jemalloc = ["jemalloc-ctl"]
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 {
35 // Note: This is incredibly slow. 35 // Note: This is incredibly slow.
36 let alloc = unsafe { libc::mallinfo() }.uordblks as isize; 36 let alloc = unsafe { libc::mallinfo() }.uordblks as isize;
37 MemoryUsage { allocated: Bytes(alloc) } 37 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) }
38 } else { 54 } else {
39 MemoryUsage { allocated: Bytes(0) } 55 MemoryUsage { allocated: Bytes(0) }
40 } 56 }