From 18a1e092e9406c6670cd38d17997325bba7bbfdc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 30 Jun 2019 13:30:17 +0300 Subject: Move memory usage statistics to ra_prof --- crates/ra_prof/src/memory_usage.rs | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 crates/ra_prof/src/memory_usage.rs (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 new file mode 100644 index 000000000..2bde8fb5f --- /dev/null +++ b/crates/ra_prof/src/memory_usage.rs @@ -0,0 +1,52 @@ +use std::fmt; + +pub struct MemoryUsage { + pub allocated: Bytes, + pub resident: Bytes, +} + +impl MemoryUsage { + #[cfg(feature = "jemalloc")] + pub fn current() -> MemoryUsage { + jemalloc_ctl::epoch().unwrap(); + MemoryUsage { + allocated: Bytes(jemalloc_ctl::stats::allocated().unwrap()), + resident: Bytes(jemalloc_ctl::stats::resident().unwrap()), + } + } + + #[cfg(not(feature = "jemalloc"))] + pub fn current() -> MemoryUsage { + MemoryUsage { allocated: Bytes(0), resident: Bytes(0) } + } +} + +impl fmt::Display for MemoryUsage { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{} allocated {} resident", self.allocated, self.resident,) + } +} + +#[derive(Default)] +pub struct Bytes(usize); + +impl fmt::Display for Bytes { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let bytes = self.0; + if bytes < 4096 { + return write!(f, "{} bytes", bytes); + } + let kb = bytes / 1024; + if kb < 4096 { + return write!(f, "{}kb", kb); + } + let mb = kb / 1024; + write!(f, "{}mb", mb) + } +} + +impl std::ops::AddAssign for Bytes { + fn add_assign(&mut self, x: usize) { + self.0 += x; + } +} -- cgit v1.2.3