Allow negative bytes

Gotta be optimistic about those memory usage optimizations
This commit is contained in:
Aleksey Kladov 2020-07-30 09:47:16 +02:00
parent dae99b6661
commit afab67e69c
2 changed files with 30 additions and 30 deletions

View file

@ -3,35 +3,43 @@ use std::fmt;
use cfg_if::cfg_if;
#[derive(Copy, Clone)]
pub struct MemoryUsage {
pub allocated: Bytes,
}
impl MemoryUsage {
pub fn current() -> MemoryUsage {
cfg_if! {
if #[cfg(target_os = "linux")] {
// Note: This is incredibly slow.
let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as usize;
MemoryUsage { allocated: Bytes(alloc) }
} else {
MemoryUsage { allocated: Bytes(0) }
}
}
}
}
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 }
}
}
impl MemoryUsage {
pub fn current() -> MemoryUsage {
cfg_if! {
if #[cfg(target_os = "linux")] {
// Note: This is incredibly slow.
let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as isize;
MemoryUsage { allocated: Bytes(alloc) }
} else {
MemoryUsage { allocated: Bytes(0) }
}
}
}
}
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub struct Bytes(usize);
pub struct Bytes(isize);
impl Bytes {
pub fn megabytes(self) -> usize {
pub fn megabytes(self) -> isize {
self.0 / 1024 / 1024
}
}
@ -41,10 +49,10 @@ impl fmt::Display for Bytes {
let bytes = self.0;
let mut value = bytes;
let mut suffix = "b";
if value > 4096 {
if value.abs() > 4096 {
value /= 1024;
suffix = "kb";
if value > 4096 {
if value.abs() > 4096 {
value /= 1024;
suffix = "mb";
}
@ -55,7 +63,7 @@ impl fmt::Display for Bytes {
impl std::ops::AddAssign<usize> for Bytes {
fn add_assign(&mut self, x: usize) {
self.0 += x;
self.0 += x as isize;
}
}

View file

@ -111,11 +111,7 @@ pub fn analysis_stats(
eprintln!("Total declarations: {}", num_decls);
eprintln!("Total functions: {}", funcs.len());
let item_collection_memory = ra_prof::memory_usage();
eprintln!(
"Item Collection: {:?}, {}",
analysis_time.elapsed(),
item_collection_memory.allocated
);
eprintln!("Item Collection: {:?}, {}", analysis_time.elapsed(), item_collection_memory);
if randomize {
shuffle(&mut rng, &mut funcs);
@ -140,7 +136,7 @@ pub fn analysis_stats(
eprintln!(
"Parallel Inference: {:?}, {}",
inference_time.elapsed(),
ra_prof::memory_usage().allocated
ra_prof::memory_usage()
);
}
@ -297,11 +293,7 @@ pub fn analysis_stats(
let inference_time = inference_time.elapsed();
let total_memory = ra_prof::memory_usage();
eprintln!(
"Inference: {:?}, {}",
inference_time,
total_memory.allocated - item_collection_memory.allocated
);
eprintln!("Inference: {:?}, {}", inference_time, total_memory - item_collection_memory);
let analysis_time = analysis_time.elapsed();
eprintln!("Total: {:?}, {}", analysis_time, total_memory);