mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Move memory usage statistics to ra_prof
This commit is contained in:
parent
e18389d268
commit
18a1e092e9
9 changed files with 78 additions and 68 deletions
|
@ -1,3 +1,5 @@
|
|||
mod memory_usage;
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
time::{Duration, Instant},
|
||||
|
@ -11,6 +13,14 @@ use std::{
|
|||
use once_cell::sync::Lazy;
|
||||
use itertools::Itertools;
|
||||
|
||||
pub use crate::memory_usage::{MemoryUsage, Bytes};
|
||||
|
||||
// We use jemalloc mainly to get heap usage statistics, actual performance
|
||||
// difference is not measures.
|
||||
#[cfg(feature = "jemalloc")]
|
||||
#[global_allocator]
|
||||
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
||||
|
||||
/// Set profiling filter. It specifies descriptions allowed to profile.
|
||||
/// This is helpful when call stack has too many nested profiling scopes.
|
||||
/// Additionally filter can specify maximum depth of profiling scopes nesting.
|
||||
|
@ -288,6 +298,10 @@ impl Drop for CpuProfiler {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn memory_usage() -> MemoryUsage {
|
||||
MemoryUsage::current()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
52
crates/ra_prof/src/memory_usage.rs
Normal file
52
crates/ra_prof/src/memory_usage.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use std::fmt;
|
||||
|
||||
pub struct MemoryUsage {
|
||||
pub allocated: Bytes,
|
||||
pub resident: Bytes,
|
||||
}
|
||||
|
||||
impl MemoryUsage {
|
||||
#[cfg(feature = "jemalloc")]
|
||||
pub fn current() -> MemoryUsage {
|
||||
jemalloc_ctl::epoch().unwrap();
|
||||
MemoryUsage {
|
||||
allocated: Bytes(jemalloc_ctl::stats::allocated().unwrap()),
|
||||
resident: Bytes(jemalloc_ctl::stats::resident().unwrap()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "jemalloc"))]
|
||||
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,)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Bytes(usize);
|
||||
|
||||
impl fmt::Display for Bytes {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let bytes = self.0;
|
||||
if bytes < 4096 {
|
||||
return write!(f, "{} bytes", bytes);
|
||||
}
|
||||
let kb = bytes / 1024;
|
||||
if kb < 4096 {
|
||||
return write!(f, "{}kb", kb);
|
||||
}
|
||||
let mb = kb / 1024;
|
||||
write!(f, "{}mb", mb)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::AddAssign<usize> for Bytes {
|
||||
fn add_assign(&mut self, x: usize) {
|
||||
self.0 += x;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue