diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:19:53 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:20:13 +0100 |
commit | 7bbca7a1b3f9293d2f5cc5745199bc5f8396f2f0 (patch) | |
tree | bdb47765991cb973b2cd5481a088fac636bd326c /crates/profile/src/memory_usage.rs | |
parent | ca464650eeaca6195891199a93f4f76cf3e7e697 (diff) | |
parent | e65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff) |
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Diffstat (limited to 'crates/profile/src/memory_usage.rs')
-rw-r--r-- | crates/profile/src/memory_usage.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/crates/profile/src/memory_usage.rs b/crates/profile/src/memory_usage.rs new file mode 100644 index 000000000..83390212a --- /dev/null +++ b/crates/profile/src/memory_usage.rs | |||
@@ -0,0 +1,75 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | use std::fmt; | ||
3 | |||
4 | use cfg_if::cfg_if; | ||
5 | |||
6 | #[derive(Copy, Clone)] | ||
7 | pub struct MemoryUsage { | ||
8 | pub allocated: Bytes, | ||
9 | } | ||
10 | |||
11 | impl fmt::Display for MemoryUsage { | ||
12 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
13 | write!(fmt, "{}", self.allocated) | ||
14 | } | ||
15 | } | ||
16 | |||
17 | impl std::ops::Sub for MemoryUsage { | ||
18 | type Output = MemoryUsage; | ||
19 | fn sub(self, rhs: MemoryUsage) -> MemoryUsage { | ||
20 | MemoryUsage { allocated: self.allocated - rhs.allocated } | ||
21 | } | ||
22 | } | ||
23 | |||
24 | impl MemoryUsage { | ||
25 | pub fn current() -> MemoryUsage { | ||
26 | cfg_if! { | ||
27 | if #[cfg(all(target_os = "linux", target_env = "gnu"))] { | ||
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 | } | ||
35 | } | ||
36 | } | ||
37 | |||
38 | #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] | ||
39 | pub struct Bytes(isize); | ||
40 | |||
41 | impl Bytes { | ||
42 | pub fn megabytes(self) -> isize { | ||
43 | self.0 / 1024 / 1024 | ||
44 | } | ||
45 | } | ||
46 | |||
47 | impl fmt::Display for Bytes { | ||
48 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
49 | let bytes = self.0; | ||
50 | let mut value = bytes; | ||
51 | let mut suffix = "b"; | ||
52 | if value.abs() > 4096 { | ||
53 | value /= 1024; | ||
54 | suffix = "kb"; | ||
55 | if value.abs() > 4096 { | ||
56 | value /= 1024; | ||
57 | suffix = "mb"; | ||
58 | } | ||
59 | } | ||
60 | f.pad(&format!("{}{}", value, suffix)) | ||
61 | } | ||
62 | } | ||
63 | |||
64 | impl std::ops::AddAssign<usize> for Bytes { | ||
65 | fn add_assign(&mut self, x: usize) { | ||
66 | self.0 += x as isize; | ||
67 | } | ||
68 | } | ||
69 | |||
70 | impl std::ops::Sub for Bytes { | ||
71 | type Output = Bytes; | ||
72 | fn sub(self, rhs: Bytes) -> Bytes { | ||
73 | Bytes(self.0 - rhs.0) | ||
74 | } | ||
75 | } | ||