From 56c090d0d0ad68c0dd195684e4d8180ea149692f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 21 Jul 2020 19:30:17 +0200 Subject: Allow gathering memory stats on non-jemalloc Linux --- Cargo.lock | 2 ++ crates/ra_prof/Cargo.toml | 2 ++ crates/ra_prof/src/memory_usage.rs | 25 +++++++++++++++---------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf4de8bbe..fbe4cd666 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1198,8 +1198,10 @@ name = "ra_prof" version = "0.1.0" dependencies = [ "backtrace", + "cfg-if", "jemalloc-ctl", "jemallocator", + "libc", "mimalloc", "once_cell", "ra_arena", diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml index b3d52985a..2e60858f1 100644 --- a/crates/ra_prof/Cargo.toml +++ b/crates/ra_prof/Cargo.toml @@ -14,6 +14,8 @@ ra_arena = { path = "../ra_arena" } once_cell = "1.3.1" backtrace = { version = "0.3.44", optional = true } mimalloc = { version = "0.1.19", default-features = false, optional = true } +cfg-if = "0.1.10" +libc = "0.2.73" [target.'cfg(not(target_env = "msvc"))'.dependencies] jemallocator = { version = "0.3.2", optional = true } diff --git a/crates/ra_prof/src/memory_usage.rs b/crates/ra_prof/src/memory_usage.rs index 9768f656c..b1858b06f 100644 --- a/crates/ra_prof/src/memory_usage.rs +++ b/crates/ra_prof/src/memory_usage.rs @@ -1,5 +1,6 @@ //! FIXME: write short doc here +use cfg_if::cfg_if; use std::fmt; pub struct MemoryUsage { @@ -8,19 +9,23 @@ pub struct MemoryUsage { } impl MemoryUsage { - #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))] pub fn current() -> MemoryUsage { - jemalloc_ctl::epoch::advance().unwrap(); - MemoryUsage { - allocated: Bytes(jemalloc_ctl::stats::allocated::read().unwrap()), - resident: Bytes(jemalloc_ctl::stats::resident::read().unwrap()), + cfg_if! { + if #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))] { + jemalloc_ctl::epoch::advance().unwrap(); + MemoryUsage { + allocated: Bytes(jemalloc_ctl::stats::allocated::read().unwrap()), + resident: Bytes(jemalloc_ctl::stats::resident::read().unwrap()), + } + } else if #[cfg(target_os = "linux")] { + // Note: This is incredibly slow. + let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as usize; + MemoryUsage { allocated: Bytes(alloc), resident: Bytes(0) } + } else { + MemoryUsage { allocated: Bytes(0), resident: Bytes(0) } + } } } - - #[cfg(any(not(feature = "jemalloc"), target_env = "msvc"))] - pub fn current() -> MemoryUsage { - MemoryUsage { allocated: Bytes(0), resident: Bytes(0) } - } } impl fmt::Display for MemoryUsage { -- cgit v1.2.3