2021-05-22 13:53:47 +00:00
|
|
|
//! Like [`std::time::Instant`], but for memory.
|
|
|
|
//!
|
|
|
|
//! Measures the total size of all currently allocated objects.
|
2020-07-22 11:40:45 +00:00
|
|
|
use std::fmt;
|
2019-09-30 08:58:53 +00:00
|
|
|
|
2020-07-21 17:30:17 +00:00
|
|
|
use cfg_if::cfg_if;
|
2019-06-30 10:30:17 +00:00
|
|
|
|
2020-07-30 07:47:16 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2019-06-30 10:30:17 +00:00
|
|
|
pub struct MemoryUsage {
|
|
|
|
pub allocated: Bytes,
|
|
|
|
}
|
|
|
|
|
2020-07-30 07:47:16 +00:00
|
|
|
impl fmt::Display for MemoryUsage {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "{}", self.allocated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Sub for MemoryUsage {
|
|
|
|
type Output = MemoryUsage;
|
|
|
|
fn sub(self, rhs: MemoryUsage) -> MemoryUsage {
|
|
|
|
MemoryUsage { allocated: self.allocated - rhs.allocated }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 10:30:17 +00:00
|
|
|
impl MemoryUsage {
|
2021-05-22 13:53:47 +00:00
|
|
|
pub fn now() -> MemoryUsage {
|
2020-07-21 17:30:17 +00:00
|
|
|
cfg_if! {
|
2021-01-18 18:25:55 +00:00
|
|
|
if #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))] {
|
|
|
|
jemalloc_ctl::epoch::advance().unwrap();
|
|
|
|
MemoryUsage {
|
|
|
|
allocated: Bytes(jemalloc_ctl::stats::allocated::read().unwrap() as isize),
|
|
|
|
}
|
|
|
|
} else if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
|
2020-07-21 17:30:17 +00:00
|
|
|
// Note: This is incredibly slow.
|
2020-07-30 08:14:35 +00:00
|
|
|
let alloc = unsafe { libc::mallinfo() }.uordblks as isize;
|
2020-07-30 07:45:20 +00:00
|
|
|
MemoryUsage { allocated: Bytes(alloc) }
|
2021-06-09 20:55:50 +00:00
|
|
|
} else if #[cfg(windows)] {
|
|
|
|
// There doesn't seem to be an API for determining heap usage, so we try to
|
|
|
|
// approximate that by using the Commit Charge value.
|
|
|
|
|
|
|
|
use winapi::um::processthreadsapi::*;
|
|
|
|
use winapi::um::psapi::*;
|
|
|
|
use std::mem::{MaybeUninit, size_of};
|
|
|
|
|
|
|
|
let proc = unsafe { GetCurrentProcess() };
|
|
|
|
let mut mem_counters = MaybeUninit::uninit();
|
|
|
|
let cb = size_of::<PROCESS_MEMORY_COUNTERS>();
|
|
|
|
let ret = unsafe { GetProcessMemoryInfo(proc, mem_counters.as_mut_ptr(), cb as u32) };
|
|
|
|
assert!(ret != 0);
|
|
|
|
|
|
|
|
let usage = unsafe { mem_counters.assume_init().PagefileUsage };
|
|
|
|
MemoryUsage { allocated: Bytes(usage as isize) }
|
2020-07-21 17:30:17 +00:00
|
|
|
} else {
|
2020-07-30 07:45:20 +00:00
|
|
|
MemoryUsage { allocated: Bytes(0) }
|
2020-07-21 17:30:17 +00:00
|
|
|
}
|
2019-06-30 10:30:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 11:40:01 +00:00
|
|
|
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
|
2020-07-30 07:47:16 +00:00
|
|
|
pub struct Bytes(isize);
|
2019-06-30 10:30:17 +00:00
|
|
|
|
2020-07-25 08:35:45 +00:00
|
|
|
impl Bytes {
|
2020-07-30 07:47:16 +00:00
|
|
|
pub fn megabytes(self) -> isize {
|
2020-07-25 08:35:45 +00:00
|
|
|
self.0 / 1024 / 1024
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 10:30:17 +00:00
|
|
|
impl fmt::Display for Bytes {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let bytes = self.0;
|
2019-06-30 11:40:01 +00:00
|
|
|
let mut value = bytes;
|
|
|
|
let mut suffix = "b";
|
2020-07-30 07:47:16 +00:00
|
|
|
if value.abs() > 4096 {
|
2019-06-30 11:40:01 +00:00
|
|
|
value /= 1024;
|
|
|
|
suffix = "kb";
|
2020-07-30 07:47:16 +00:00
|
|
|
if value.abs() > 4096 {
|
2019-06-30 11:40:01 +00:00
|
|
|
value /= 1024;
|
|
|
|
suffix = "mb";
|
|
|
|
}
|
2019-06-30 10:30:17 +00:00
|
|
|
}
|
2019-06-30 11:40:01 +00:00
|
|
|
f.pad(&format!("{}{}", value, suffix))
|
2019-06-30 10:30:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::AddAssign<usize> for Bytes {
|
|
|
|
fn add_assign(&mut self, x: usize) {
|
2020-07-30 07:47:16 +00:00
|
|
|
self.0 += x as isize;
|
2019-06-30 10:30:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-30 11:40:01 +00:00
|
|
|
|
|
|
|
impl std::ops::Sub for Bytes {
|
|
|
|
type Output = Bytes;
|
|
|
|
fn sub(self, rhs: Bytes) -> Bytes {
|
|
|
|
Bytes(self.0 - rhs.0)
|
|
|
|
}
|
|
|
|
}
|