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 --- crates/ra_prof/src/memory_usage.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'crates/ra_prof/src') 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