aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_prof/src/memory_usage.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-06-30 11:30:17 +0100
committerAleksey Kladov <[email protected]>2019-06-30 11:30:17 +0100
commit18a1e092e9406c6670cd38d17997325bba7bbfdc (patch)
tree65284e826535842d1a28db8713881151000da874 /crates/ra_prof/src/memory_usage.rs
parente18389d2684a8da9937fe37f9598fabf67c65fee (diff)
Move memory usage statistics to ra_prof
Diffstat (limited to 'crates/ra_prof/src/memory_usage.rs')
-rw-r--r--crates/ra_prof/src/memory_usage.rs52
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 @@
1use std::fmt;
2
3pub struct MemoryUsage {
4 pub allocated: Bytes,
5 pub resident: Bytes,
6}
7
8impl 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
24impl 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)]
31pub struct Bytes(usize);
32
33impl 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
48impl std::ops::AddAssign<usize> for Bytes {
49 fn add_assign(&mut self, x: usize) {
50 self.0 += x;
51 }
52}