diff options
Diffstat (limited to 'crates/ra_prof/src/memory_usage.rs')
-rw-r--r-- | crates/ra_prof/src/memory_usage.rs | 52 |
1 files changed, 52 insertions, 0 deletions
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 @@ | |||
1 | use std::fmt; | ||
2 | |||
3 | pub struct MemoryUsage { | ||
4 | pub allocated: Bytes, | ||
5 | pub resident: Bytes, | ||
6 | } | ||
7 | |||
8 | impl MemoryUsage { | ||
9 | #[cfg(feature = "jemalloc")] | ||
10 | pub fn current() -> MemoryUsage { | ||
11 | jemalloc_ctl::epoch().unwrap(); | ||
12 | MemoryUsage { | ||
13 | allocated: Bytes(jemalloc_ctl::stats::allocated().unwrap()), | ||
14 | resident: Bytes(jemalloc_ctl::stats::resident().unwrap()), | ||
15 | } | ||
16 | } | ||
17 | |||
18 | #[cfg(not(feature = "jemalloc"))] | ||
19 | pub fn current() -> MemoryUsage { | ||
20 | MemoryUsage { allocated: Bytes(0), resident: Bytes(0) } | ||
21 | } | ||
22 | } | ||
23 | |||
24 | impl fmt::Display for MemoryUsage { | ||
25 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
26 | write!(fmt, "{} allocated {} resident", self.allocated, self.resident,) | ||
27 | } | ||
28 | } | ||
29 | |||
30 | #[derive(Default)] | ||
31 | pub struct Bytes(usize); | ||
32 | |||
33 | impl fmt::Display for Bytes { | ||
34 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
35 | let bytes = self.0; | ||
36 | if bytes < 4096 { | ||
37 | return write!(f, "{} bytes", bytes); | ||
38 | } | ||
39 | let kb = bytes / 1024; | ||
40 | if kb < 4096 { | ||
41 | return write!(f, "{}kb", kb); | ||
42 | } | ||
43 | let mb = kb / 1024; | ||
44 | write!(f, "{}mb", mb) | ||
45 | } | ||
46 | } | ||
47 | |||
48 | impl std::ops::AddAssign<usize> for Bytes { | ||
49 | fn add_assign(&mut self, x: usize) { | ||
50 | self.0 += x; | ||
51 | } | ||
52 | } | ||