2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-06-30 05:30:17 -05:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
pub struct MemoryUsage {
|
|
|
|
pub allocated: Bytes,
|
|
|
|
pub resident: Bytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MemoryUsage {
|
2019-11-14 10:47:18 -06:00
|
|
|
#[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
|
2019-06-30 05:30:17 -05:00
|
|
|
pub fn current() -> MemoryUsage {
|
2019-07-17 09:58:17 -05:00
|
|
|
jemalloc_ctl::epoch::advance().unwrap();
|
2019-06-30 05:30:17 -05:00
|
|
|
MemoryUsage {
|
2019-07-17 09:58:17 -05:00
|
|
|
allocated: Bytes(jemalloc_ctl::stats::allocated::read().unwrap()),
|
|
|
|
resident: Bytes(jemalloc_ctl::stats::resident::read().unwrap()),
|
2019-06-30 05:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 10:47:18 -06:00
|
|
|
#[cfg(any(not(feature = "jemalloc"), target_env = "msvc"))]
|
2019-06-30 05:30:17 -05:00
|
|
|
pub fn current() -> MemoryUsage {
|
|
|
|
MemoryUsage { allocated: Bytes(0), resident: Bytes(0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for MemoryUsage {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "{} allocated {} resident", self.allocated, self.resident,)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 06:40:01 -05:00
|
|
|
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
|
2019-06-30 05:30:17 -05:00
|
|
|
pub struct Bytes(usize);
|
|
|
|
|
|
|
|
impl fmt::Display for Bytes {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let bytes = self.0;
|
2019-06-30 06:40:01 -05:00
|
|
|
let mut value = bytes;
|
|
|
|
let mut suffix = "b";
|
|
|
|
if value > 4096 {
|
|
|
|
value /= 1024;
|
|
|
|
suffix = "kb";
|
|
|
|
if value > 4096 {
|
|
|
|
value /= 1024;
|
|
|
|
suffix = "mb";
|
|
|
|
}
|
2019-06-30 05:30:17 -05:00
|
|
|
}
|
2019-06-30 06:40:01 -05:00
|
|
|
f.pad(&format!("{}{}", value, suffix))
|
2019-06-30 05:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::AddAssign<usize> for Bytes {
|
|
|
|
fn add_assign(&mut self, x: usize) {
|
|
|
|
self.0 += x;
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 06:40:01 -05:00
|
|
|
|
|
|
|
impl std::ops::Sub for Bytes {
|
|
|
|
type Output = Bytes;
|
|
|
|
fn sub(self, rhs: Bytes) -> Bytes {
|
|
|
|
Bytes(self.0 - rhs.0)
|
|
|
|
}
|
|
|
|
}
|