2021-05-22 08:53:47 -05:00
|
|
|
//! Like [`std::time::Instant`], but for memory.
|
|
|
|
//!
|
|
|
|
//! Measures the total size of all currently allocated objects.
|
2020-07-22 06:40:45 -05:00
|
|
|
use std::fmt;
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2020-07-21 12:30:17 -05:00
|
|
|
use cfg_if::cfg_if;
|
2019-06-30 05:30:17 -05:00
|
|
|
|
2020-07-30 02:47:16 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2019-06-30 05:30:17 -05:00
|
|
|
pub struct MemoryUsage {
|
|
|
|
pub allocated: Bytes,
|
|
|
|
}
|
|
|
|
|
2020-07-30 02:47:16 -05:00
|
|
|
impl fmt::Display for MemoryUsage {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-03-21 03:43:36 -05:00
|
|
|
self.allocated.fmt(f)
|
2020-07-30 02:47:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Sub for MemoryUsage {
|
|
|
|
type Output = MemoryUsage;
|
|
|
|
fn sub(self, rhs: MemoryUsage) -> MemoryUsage {
|
|
|
|
MemoryUsage { allocated: self.allocated - rhs.allocated }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:30:17 -05:00
|
|
|
impl MemoryUsage {
|
2021-05-22 08:53:47 -05:00
|
|
|
pub fn now() -> MemoryUsage {
|
2020-07-21 12:30:17 -05:00
|
|
|
cfg_if! {
|
2021-01-18 12:25:55 -06: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"))] {
|
2021-06-10 07:58:52 -05:00
|
|
|
memusage_linux()
|
2021-06-09 15:55:50 -05: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 12:30:17 -05:00
|
|
|
} else {
|
2020-07-30 02:45:20 -05:00
|
|
|
MemoryUsage { allocated: Bytes(0) }
|
2020-07-21 12:30:17 -05:00
|
|
|
}
|
2019-06-30 05:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 07:10:07 -05:00
|
|
|
#[cfg(all(target_os = "linux", target_env = "gnu", not(feature = "jemalloc")))]
|
2021-06-10 07:58:52 -05:00
|
|
|
fn memusage_linux() -> MemoryUsage {
|
|
|
|
// Linux/glibc has 2 APIs for allocator introspection that we can use: mallinfo and mallinfo2.
|
|
|
|
// mallinfo uses `int` fields and cannot handle memory usage exceeding 2 GB.
|
|
|
|
// mallinfo2 is very recent, so its presence needs to be detected at runtime.
|
|
|
|
// Both are abysmally slow.
|
|
|
|
|
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
|
|
|
|
static MALLINFO2: AtomicUsize = AtomicUsize::new(1);
|
|
|
|
|
|
|
|
let mut mallinfo2 = MALLINFO2.load(Ordering::Relaxed);
|
|
|
|
if mallinfo2 == 1 {
|
|
|
|
let cstr = CStr::from_bytes_with_nul(b"mallinfo2\0").unwrap();
|
|
|
|
mallinfo2 = unsafe { libc::dlsym(libc::RTLD_DEFAULT, cstr.as_ptr()) } as usize;
|
|
|
|
// NB: races don't matter here, since they'll always store the same value
|
|
|
|
MALLINFO2.store(mallinfo2, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if mallinfo2 == 0 {
|
|
|
|
// mallinfo2 does not exist, use mallinfo.
|
|
|
|
let alloc = unsafe { libc::mallinfo() }.uordblks as isize;
|
|
|
|
MemoryUsage { allocated: Bytes(alloc) }
|
|
|
|
} else {
|
|
|
|
let mallinfo2: fn() -> libc::mallinfo2 = unsafe { std::mem::transmute(mallinfo2) };
|
|
|
|
let alloc = mallinfo2().uordblks as isize;
|
|
|
|
MemoryUsage { allocated: Bytes(alloc) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 06:40:01 -05:00
|
|
|
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
|
2020-07-30 02:47:16 -05:00
|
|
|
pub struct Bytes(isize);
|
2019-06-30 05:30:17 -05:00
|
|
|
|
2023-04-22 02:34:47 -05:00
|
|
|
impl Bytes {
|
|
|
|
pub fn new(bytes: isize) -> Bytes {
|
|
|
|
Bytes(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 03:35:45 -05:00
|
|
|
impl Bytes {
|
2020-07-30 02:47:16 -05:00
|
|
|
pub fn megabytes(self) -> isize {
|
2020-07-25 03:35:45 -05:00
|
|
|
self.0 / 1024 / 1024
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:30:17 -05:00
|
|
|
impl fmt::Display for Bytes {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-06-30 05:30:17 -05:00
|
|
|
let bytes = self.0;
|
2019-06-30 06:40:01 -05:00
|
|
|
let mut value = bytes;
|
|
|
|
let mut suffix = "b";
|
2020-07-30 02:47:16 -05:00
|
|
|
if value.abs() > 4096 {
|
2019-06-30 06:40:01 -05:00
|
|
|
value /= 1024;
|
|
|
|
suffix = "kb";
|
2020-07-30 02:47:16 -05:00
|
|
|
if value.abs() > 4096 {
|
2019-06-30 06:40:01 -05:00
|
|
|
value /= 1024;
|
|
|
|
suffix = "mb";
|
|
|
|
}
|
2019-06-30 05:30:17 -05:00
|
|
|
}
|
2022-12-23 12:42:58 -06: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) {
|
2020-07-30 02:47:16 -05:00
|
|
|
self.0 += x as isize;
|
2019-06-30 05:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|