diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-10 13:09:54 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-10 13:09:54 +0100 |
commit | f4da4de7cdd4a7dfe40a417b0100b83ec50d1e1d (patch) | |
tree | 15339930fdaf23cf4ce941a99228d9cef1b9a75a | |
parent | 660a89620f65225359559ddcaf158bdb9dfe0d4c (diff) | |
parent | 2c1ca98abaf6756d3246841cc3cf4ca3f617efca (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]>
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | crates/profile/Cargo.toml | 3 | ||||
-rw-r--r-- | crates/profile/src/memory_usage.rs | 16 |
3 files changed, 20 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock index 505263c64..2bd8897f0 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -1170,6 +1170,7 @@ dependencies = [ | |||
1170 | "once_cell", | 1170 | "once_cell", |
1171 | "perf-event", | 1171 | "perf-event", |
1172 | "tikv-jemalloc-ctl", | 1172 | "tikv-jemalloc-ctl", |
1173 | "winapi", | ||
1173 | ] | 1174 | ] |
1174 | 1175 | ||
1175 | [[package]] | 1176 | [[package]] |
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] |
21 | perf-event = "0.4" | 21 | perf-event = "0.4" |
22 | 22 | ||
23 | [target.'cfg(windows)'.dependencies] | ||
24 | winapi = { version = "0.3.8", features = ["psapi"] } | ||
25 | |||
23 | [features] | 26 | [features] |
24 | cpu_profiler = [] | 27 | cpu_profiler = [] |
25 | jemalloc = ["jemalloc-ctl"] | 28 | jemalloc = ["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 | } |