From dae99b66611759ba48fd164646f077d3e8515dad Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 09:45:20 +0200 Subject: Drop resident from memory usage --- crates/ra_prof/src/memory_usage.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'crates/ra_prof/src/memory_usage.rs') diff --git a/crates/ra_prof/src/memory_usage.rs b/crates/ra_prof/src/memory_usage.rs index 745345fac..857b51321 100644 --- a/crates/ra_prof/src/memory_usage.rs +++ b/crates/ra_prof/src/memory_usage.rs @@ -5,7 +5,6 @@ use cfg_if::cfg_if; pub struct MemoryUsage { pub allocated: Bytes, - pub resident: Bytes, } impl MemoryUsage { @@ -14,9 +13,9 @@ impl MemoryUsage { 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) } + MemoryUsage { allocated: Bytes(alloc) } } else { - MemoryUsage { allocated: Bytes(0), resident: Bytes(0) } + MemoryUsage { allocated: Bytes(0) } } } } @@ -24,7 +23,7 @@ impl MemoryUsage { impl fmt::Display for MemoryUsage { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{} allocated {} resident", self.allocated, self.resident,) + write!(fmt, "{}", self.allocated) } } -- cgit v1.2.3 From afab67e69c39027fb99878751309d4050324beef Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 09:47:16 +0200 Subject: Allow negative bytes Gotta be optimistic about those memory usage optimizations --- crates/ra_prof/src/memory_usage.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'crates/ra_prof/src/memory_usage.rs') diff --git a/crates/ra_prof/src/memory_usage.rs b/crates/ra_prof/src/memory_usage.rs index 857b51321..22b61e4a2 100644 --- a/crates/ra_prof/src/memory_usage.rs +++ b/crates/ra_prof/src/memory_usage.rs @@ -3,16 +3,30 @@ use std::fmt; use cfg_if::cfg_if; +#[derive(Copy, Clone)] pub struct MemoryUsage { pub allocated: Bytes, } +impl fmt::Display for MemoryUsage { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{}", self.allocated) + } +} + +impl std::ops::Sub for MemoryUsage { + type Output = MemoryUsage; + fn sub(self, rhs: MemoryUsage) -> MemoryUsage { + MemoryUsage { allocated: self.allocated - rhs.allocated } + } +} + impl MemoryUsage { pub fn current() -> MemoryUsage { cfg_if! { if #[cfg(target_os = "linux")] { // Note: This is incredibly slow. - let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as usize; + let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as isize; MemoryUsage { allocated: Bytes(alloc) } } else { MemoryUsage { allocated: Bytes(0) } @@ -21,17 +35,11 @@ impl MemoryUsage { } } -impl fmt::Display for MemoryUsage { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{}", self.allocated) - } -} - #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] -pub struct Bytes(usize); +pub struct Bytes(isize); impl Bytes { - pub fn megabytes(self) -> usize { + pub fn megabytes(self) -> isize { self.0 / 1024 / 1024 } } @@ -41,10 +49,10 @@ impl fmt::Display for Bytes { let bytes = self.0; let mut value = bytes; let mut suffix = "b"; - if value > 4096 { + if value.abs() > 4096 { value /= 1024; suffix = "kb"; - if value > 4096 { + if value.abs() > 4096 { value /= 1024; suffix = "mb"; } @@ -55,7 +63,7 @@ impl fmt::Display for Bytes { impl std::ops::AddAssign for Bytes { fn add_assign(&mut self, x: usize) { - self.0 += x; + self.0 += x as isize; } } -- cgit v1.2.3 From 4bab553029da31f3e90e99d8b83d160a34fdf4b2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 10:14:35 +0200 Subject: fix cast --- crates/ra_prof/src/memory_usage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_prof/src/memory_usage.rs') diff --git a/crates/ra_prof/src/memory_usage.rs b/crates/ra_prof/src/memory_usage.rs index 22b61e4a2..c2ecbd33c 100644 --- a/crates/ra_prof/src/memory_usage.rs +++ b/crates/ra_prof/src/memory_usage.rs @@ -26,7 +26,7 @@ impl MemoryUsage { cfg_if! { if #[cfg(target_os = "linux")] { // Note: This is incredibly slow. - let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as isize; + let alloc = unsafe { libc::mallinfo() }.uordblks as isize; MemoryUsage { allocated: Bytes(alloc) } } else { MemoryUsage { allocated: Bytes(0) } -- cgit v1.2.3