aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_prof/src/memory_usage.rs
diff options
context:
space:
mode:
authorZac Pullar-Strecker <[email protected]>2020-07-31 03:12:44 +0100
committerZac Pullar-Strecker <[email protected]>2020-07-31 03:12:44 +0100
commitf05d7b41a719d848844b054a16477b29d0f063c6 (patch)
tree0a8a0946e8aef2ce64d4c13d0035ba41cce2daf3 /crates/ra_prof/src/memory_usage.rs
parent73ff610e41959e3e7c78a2b4b25b086883132956 (diff)
parent6b7cb8b5ab539fc4333ce34bc29bf77c976f232a (diff)
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Hasn't fixed tests yet.
Diffstat (limited to 'crates/ra_prof/src/memory_usage.rs')
-rw-r--r--crates/ra_prof/src/memory_usage.rs52
1 files changed, 32 insertions, 20 deletions
diff --git a/crates/ra_prof/src/memory_usage.rs b/crates/ra_prof/src/memory_usage.rs
index 9768f656c..c2ecbd33c 100644
--- a/crates/ra_prof/src/memory_usage.rs
+++ b/crates/ra_prof/src/memory_usage.rs
@@ -1,46 +1,58 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2
3use std::fmt; 2use std::fmt;
4 3
4use cfg_if::cfg_if;
5
6#[derive(Copy, Clone)]
5pub struct MemoryUsage { 7pub struct MemoryUsage {
6 pub allocated: Bytes, 8 pub allocated: Bytes,
7 pub resident: Bytes,
8} 9}
9 10
10impl MemoryUsage { 11impl fmt::Display for MemoryUsage {
11 #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))] 12 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
12 pub fn current() -> MemoryUsage { 13 write!(fmt, "{}", self.allocated)
13 jemalloc_ctl::epoch::advance().unwrap();
14 MemoryUsage {
15 allocated: Bytes(jemalloc_ctl::stats::allocated::read().unwrap()),
16 resident: Bytes(jemalloc_ctl::stats::resident::read().unwrap()),
17 }
18 } 14 }
15}
19 16
20 #[cfg(any(not(feature = "jemalloc"), target_env = "msvc"))] 17impl std::ops::Sub for MemoryUsage {
21 pub fn current() -> MemoryUsage { 18 type Output = MemoryUsage;
22 MemoryUsage { allocated: Bytes(0), resident: Bytes(0) } 19 fn sub(self, rhs: MemoryUsage) -> MemoryUsage {
20 MemoryUsage { allocated: self.allocated - rhs.allocated }
23 } 21 }
24} 22}
25 23
26impl fmt::Display for MemoryUsage { 24impl MemoryUsage {
27 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 25 pub fn current() -> MemoryUsage {
28 write!(fmt, "{} allocated {} resident", self.allocated, self.resident,) 26 cfg_if! {
27 if #[cfg(target_os = "linux")] {
28 // Note: This is incredibly slow.
29 let alloc = unsafe { libc::mallinfo() }.uordblks as isize;
30 MemoryUsage { allocated: Bytes(alloc) }
31 } else {
32 MemoryUsage { allocated: Bytes(0) }
33 }
34 }
29 } 35 }
30} 36}
31 37
32#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] 38#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
33pub struct Bytes(usize); 39pub struct Bytes(isize);
40
41impl Bytes {
42 pub fn megabytes(self) -> isize {
43 self.0 / 1024 / 1024
44 }
45}
34 46
35impl fmt::Display for Bytes { 47impl fmt::Display for Bytes {
36 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 48 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37 let bytes = self.0; 49 let bytes = self.0;
38 let mut value = bytes; 50 let mut value = bytes;
39 let mut suffix = "b"; 51 let mut suffix = "b";
40 if value > 4096 { 52 if value.abs() > 4096 {
41 value /= 1024; 53 value /= 1024;
42 suffix = "kb"; 54 suffix = "kb";
43 if value > 4096 { 55 if value.abs() > 4096 {
44 value /= 1024; 56 value /= 1024;
45 suffix = "mb"; 57 suffix = "mb";
46 } 58 }
@@ -51,7 +63,7 @@ impl fmt::Display for Bytes {
51 63
52impl std::ops::AddAssign<usize> for Bytes { 64impl std::ops::AddAssign<usize> for Bytes {
53 fn add_assign(&mut self, x: usize) { 65 fn add_assign(&mut self, x: usize) {
54 self.0 += x; 66 self.0 += x as isize;
55 } 67 }
56} 68}
57 69