Merge #5579
5579: Allow negative bytes r=matklad a=matklad
Gotta be optimistic about those memory usage optimizations
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
402433470a
@ -3,9 +3,22 @@
|
|||||||
|
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
pub struct MemoryUsage {
|
pub struct MemoryUsage {
|
||||||
pub allocated: Bytes,
|
pub allocated: Bytes,
|
||||||
pub resident: Bytes,
|
}
|
||||||
|
|
||||||
|
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 {
|
impl MemoryUsage {
|
||||||
@ -13,26 +26,20 @@ pub fn current() -> MemoryUsage {
|
|||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(target_os = "linux")] {
|
if #[cfg(target_os = "linux")] {
|
||||||
// Note: This is incredibly slow.
|
// Note: This is incredibly slow.
|
||||||
let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as usize;
|
let alloc = unsafe { libc::mallinfo() }.uordblks as u32 as isize;
|
||||||
MemoryUsage { allocated: Bytes(alloc), resident: Bytes(0) }
|
MemoryUsage { allocated: Bytes(alloc) }
|
||||||
} else {
|
} else {
|
||||||
MemoryUsage { allocated: Bytes(0), resident: Bytes(0) }
|
MemoryUsage { allocated: Bytes(0) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for MemoryUsage {
|
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(fmt, "{} allocated {} resident", self.allocated, self.resident,)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
|
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
|
||||||
pub struct Bytes(usize);
|
pub struct Bytes(isize);
|
||||||
|
|
||||||
impl Bytes {
|
impl Bytes {
|
||||||
pub fn megabytes(self) -> usize {
|
pub fn megabytes(self) -> isize {
|
||||||
self.0 / 1024 / 1024
|
self.0 / 1024 / 1024
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,10 +49,10 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|||||||
let bytes = self.0;
|
let bytes = self.0;
|
||||||
let mut value = bytes;
|
let mut value = bytes;
|
||||||
let mut suffix = "b";
|
let mut suffix = "b";
|
||||||
if value > 4096 {
|
if value.abs() > 4096 {
|
||||||
value /= 1024;
|
value /= 1024;
|
||||||
suffix = "kb";
|
suffix = "kb";
|
||||||
if value > 4096 {
|
if value.abs() > 4096 {
|
||||||
value /= 1024;
|
value /= 1024;
|
||||||
suffix = "mb";
|
suffix = "mb";
|
||||||
}
|
}
|
||||||
@ -56,7 +63,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|||||||
|
|
||||||
impl std::ops::AddAssign<usize> for Bytes {
|
impl std::ops::AddAssign<usize> for Bytes {
|
||||||
fn add_assign(&mut self, x: usize) {
|
fn add_assign(&mut self, x: usize) {
|
||||||
self.0 += x;
|
self.0 += x as isize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,11 +111,7 @@ pub fn analysis_stats(
|
|||||||
eprintln!("Total declarations: {}", num_decls);
|
eprintln!("Total declarations: {}", num_decls);
|
||||||
eprintln!("Total functions: {}", funcs.len());
|
eprintln!("Total functions: {}", funcs.len());
|
||||||
let item_collection_memory = ra_prof::memory_usage();
|
let item_collection_memory = ra_prof::memory_usage();
|
||||||
eprintln!(
|
eprintln!("Item Collection: {:?}, {}", analysis_time.elapsed(), item_collection_memory);
|
||||||
"Item Collection: {:?}, {}",
|
|
||||||
analysis_time.elapsed(),
|
|
||||||
item_collection_memory.allocated
|
|
||||||
);
|
|
||||||
|
|
||||||
if randomize {
|
if randomize {
|
||||||
shuffle(&mut rng, &mut funcs);
|
shuffle(&mut rng, &mut funcs);
|
||||||
@ -140,7 +136,7 @@ pub fn analysis_stats(
|
|||||||
eprintln!(
|
eprintln!(
|
||||||
"Parallel Inference: {:?}, {}",
|
"Parallel Inference: {:?}, {}",
|
||||||
inference_time.elapsed(),
|
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 inference_time = inference_time.elapsed();
|
||||||
let total_memory = ra_prof::memory_usage();
|
let total_memory = ra_prof::memory_usage();
|
||||||
eprintln!(
|
eprintln!("Inference: {:?}, {}", inference_time, total_memory - item_collection_memory);
|
||||||
"Inference: {:?}, {}",
|
|
||||||
inference_time,
|
|
||||||
total_memory.allocated - item_collection_memory.allocated
|
|
||||||
);
|
|
||||||
|
|
||||||
let analysis_time = analysis_time.elapsed();
|
let analysis_time = analysis_time.elapsed();
|
||||||
eprintln!("Total: {:?}, {}", analysis_time, total_memory);
|
eprintln!("Total: {:?}, {}", analysis_time, total_memory);
|
||||||
|
Loading…
Reference in New Issue
Block a user