rust/crates/ra_ide_api/src/status.rs

187 lines
4.6 KiB
Rust
Raw Normal View History

2019-01-25 08:20:52 -06:00
use std::{
fmt,
iter::FromIterator,
sync::Arc,
};
2019-01-25 07:10:34 -06:00
2019-01-25 08:20:52 -06:00
use ra_syntax::{AstNode, TreeArc, SourceFile};
2019-01-22 15:15:03 -06:00
use ra_db::{
2019-01-26 02:51:36 -06:00
ParseQuery, FileTextQuery, SourceRootId,
2019-01-25 08:20:52 -06:00
salsa::{Database, debug::{DebugQueryTable, TableEntry}},
2019-01-22 15:15:03 -06:00
};
2019-01-25 08:20:52 -06:00
use crate::{
FileId, db::RootDatabase,
symbol_index::{SymbolIndex, LibrarySymbolsQuery},
};
2019-01-22 15:15:03 -06:00
2019-01-26 11:33:33 -06:00
pub(crate) fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
db.query(ParseQuery).entries::<SyntaxTreeStats>()
}
2019-01-22 15:15:03 -06:00
pub(crate) fn status(db: &RootDatabase) -> String {
2019-01-25 08:20:52 -06:00
let files_stats = db.query(FileTextQuery).entries::<FilesStats>();
2019-01-26 11:33:33 -06:00
let syntax_tree_stats = syntax_tree_stats(db);
2019-01-25 08:20:52 -06:00
let symbols_stats = db
.query(LibrarySymbolsQuery)
.entries::<LibrarySymbolsStats>();
2019-01-22 15:15:03 -06:00
let n_defs = {
2019-01-24 03:41:08 -06:00
let interner: &hir::HirInterner = db.as_ref();
2019-01-22 15:15:03 -06:00
interner.len()
};
2019-01-25 08:20:52 -06:00
format!(
2019-01-26 12:28:32 -06:00
"{}\n{}\n{}\n{} defs\n\nmemory:\n{}\ngc {:?} seconds ago",
2019-01-26 11:33:33 -06:00
files_stats,
symbols_stats,
syntax_tree_stats,
n_defs,
2019-01-26 12:12:16 -06:00
MemoryStats::current(),
2019-01-26 11:33:33 -06:00
db.last_gc.elapsed().as_secs(),
2019-01-25 08:20:52 -06:00
)
2019-01-25 07:10:34 -06:00
}
#[derive(Default)]
struct FilesStats {
total: usize,
2019-01-25 08:20:52 -06:00
size: Bytes,
2019-01-25 07:10:34 -06:00
}
impl fmt::Display for FilesStats {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
2019-01-25 08:20:52 -06:00
write!(fmt, "{} ({}) files", self.total, self.size)
}
}
impl FromIterator<TableEntry<FileId, Arc<String>>> for FilesStats {
fn from_iter<T>(iter: T) -> FilesStats
where
T: IntoIterator<Item = TableEntry<FileId, Arc<String>>>,
{
let mut res = FilesStats::default();
for entry in iter {
res.total += 1;
res.size += entry.value.unwrap().len();
}
res
}
}
#[derive(Default)]
2019-01-26 11:33:33 -06:00
pub(crate) struct SyntaxTreeStats {
2019-01-25 08:20:52 -06:00
total: usize,
2019-01-26 11:33:33 -06:00
pub(crate) retained: usize,
2019-01-25 08:20:52 -06:00
retained_size: Bytes,
}
impl fmt::Display for SyntaxTreeStats {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
2019-01-25 07:10:34 -06:00
write!(
fmt,
2019-01-25 08:20:52 -06:00
"{} trees, {} ({}) retained",
self.total, self.retained, self.retained_size,
2019-01-25 07:10:34 -06:00
)
}
}
2019-01-25 08:20:52 -06:00
impl FromIterator<TableEntry<FileId, TreeArc<SourceFile>>> for SyntaxTreeStats {
fn from_iter<T>(iter: T) -> SyntaxTreeStats
where
T: IntoIterator<Item = TableEntry<FileId, TreeArc<SourceFile>>>,
{
let mut res = SyntaxTreeStats::default();
for entry in iter {
res.total += 1;
if let Some(value) = entry.value {
res.retained += 1;
res.retained_size += value.syntax().memory_size_of_subtree();
}
}
res
2019-01-25 07:10:34 -06:00
}
2019-01-25 08:20:52 -06:00
}
#[derive(Default)]
struct LibrarySymbolsStats {
total: usize,
2019-01-25 12:10:28 -06:00
size: Bytes,
2019-01-25 08:20:52 -06:00
}
impl fmt::Display for LibrarySymbolsStats {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
2019-01-25 12:10:28 -06:00
write!(fmt, "{} ({}) symbols", self.total, self.size,)
2019-01-25 08:20:52 -06:00
}
}
impl FromIterator<TableEntry<SourceRootId, Arc<SymbolIndex>>> for LibrarySymbolsStats {
fn from_iter<T>(iter: T) -> LibrarySymbolsStats
where
T: IntoIterator<Item = TableEntry<SourceRootId, Arc<SymbolIndex>>>,
{
let mut res = LibrarySymbolsStats::default();
for entry in iter {
let value = entry.value.unwrap();
res.total += value.len();
2019-01-25 12:10:28 -06:00
res.size += value.memory_size();
2019-01-25 08:20:52 -06:00
}
res
}
}
2019-01-26 12:12:16 -06:00
struct MemoryStats {
allocated: Bytes,
resident: Bytes,
}
impl MemoryStats {
2019-01-28 06:52:21 -06:00
#[cfg(feature = "jemalloc")]
2019-01-26 12:12:16 -06:00
fn current() -> MemoryStats {
jemalloc_ctl::epoch().unwrap();
MemoryStats {
allocated: Bytes(jemalloc_ctl::stats::allocated().unwrap()),
resident: Bytes(jemalloc_ctl::stats::resident().unwrap()),
}
}
2019-01-28 06:52:21 -06:00
#[cfg(not(feature = "jemalloc"))]
fn current() -> MemoryStats {
MemoryStats {
allocated: Bytes(0),
resident: Bytes(0),
}
}
2019-01-26 12:12:16 -06:00
}
impl fmt::Display for MemoryStats {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"{} allocated {} resident",
self.allocated, self.resident,
)
}
}
2019-01-25 08:20:52 -06:00
#[derive(Default)]
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;
2019-01-25 07:10:34 -06:00
}
2019-01-22 15:15:03 -06:00
}