aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_prof/src/memory_usage.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-06-30 11:42:23 +0100
committerbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-06-30 11:42:23 +0100
commit2ad8220f58675193860337a00fed87162a98dc1a (patch)
treee39c335b575481fb40b183c82db25a222076c96b /crates/ra_prof/src/memory_usage.rs
parentbb70d18a0a51a2321780e8eaa0262ec61a659b05 (diff)
parent18a1e092e9406c6670cd38d17997325bba7bbfdc (diff)
Merge #1462
1462: Move memory usage statistics to ra_prof r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
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}