2019-07-04 20:05:17 +00:00
|
|
|
use std::{fmt, iter::FromIterator, sync::Arc};
|
2019-01-25 13:10:34 +00:00
|
|
|
|
2019-07-04 20:05:17 +00:00
|
|
|
use hir::MacroFile;
|
2019-01-22 21:15:03 +00:00
|
|
|
use ra_db::{
|
2019-07-04 20:05:17 +00:00
|
|
|
salsa::{
|
|
|
|
debug::{DebugQueryTable, TableEntry},
|
|
|
|
Database,
|
|
|
|
},
|
2019-06-01 19:11:38 +00:00
|
|
|
FileTextQuery, SourceRootId,
|
2019-01-22 21:15:03 +00:00
|
|
|
};
|
2020-02-06 11:52:32 +00:00
|
|
|
use ra_ide_db::{
|
|
|
|
symbol_index::{LibrarySymbolsQuery, SymbolIndex},
|
|
|
|
RootDatabase,
|
|
|
|
};
|
2019-07-04 20:05:17 +00:00
|
|
|
use ra_prof::{memory_usage, Bytes};
|
2019-07-19 16:53:42 +00:00
|
|
|
use ra_syntax::{ast, Parse, SyntaxNode};
|
2019-01-22 21:15:03 +00:00
|
|
|
|
2020-02-06 11:52:32 +00:00
|
|
|
use crate::FileId;
|
2020-06-18 06:29:34 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2019-01-22 21:15:03 +00:00
|
|
|
|
2019-09-26 09:31:16 +00:00
|
|
|
fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
|
2019-06-02 17:15:10 +00:00
|
|
|
db.query(ra_db::ParseQuery).entries::<SyntaxTreeStats>()
|
|
|
|
}
|
2019-09-26 09:31:16 +00:00
|
|
|
fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
|
2019-06-02 17:15:10 +00:00
|
|
|
db.query(hir::db::ParseMacroQuery).entries::<SyntaxTreeStats>()
|
2019-01-26 17:33:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 08:14:36 +00:00
|
|
|
// Feature: Status
|
|
|
|
//
|
|
|
|
// Shows internal statistic about memory usage of rust-analyzer.
|
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Action Name
|
|
|
|
//
|
|
|
|
// | VS Code | **Rust Analyzer: Status**
|
|
|
|
// |===
|
2019-01-22 21:15:03 +00:00
|
|
|
pub(crate) fn status(db: &RootDatabase) -> String {
|
2019-01-25 14:20:52 +00:00
|
|
|
let files_stats = db.query(FileTextQuery).entries::<FilesStats>();
|
2019-01-26 17:33:33 +00:00
|
|
|
let syntax_tree_stats = syntax_tree_stats(db);
|
2019-06-02 17:15:10 +00:00
|
|
|
let macro_syntax_tree_stats = macro_syntax_tree_stats(db);
|
2019-02-08 11:49:43 +00:00
|
|
|
let symbols_stats = db.query(LibrarySymbolsQuery).entries::<LibrarySymbolsStats>();
|
2019-01-25 14:20:52 +00:00
|
|
|
format!(
|
2019-06-02 17:15:10 +00:00
|
|
|
"{}\n{}\n{}\n{} (macros)\n\n\nmemory:\n{}\ngc {:?} seconds ago",
|
2019-01-26 17:33:33 +00:00
|
|
|
files_stats,
|
|
|
|
symbols_stats,
|
|
|
|
syntax_tree_stats,
|
2019-06-02 17:15:10 +00:00
|
|
|
macro_syntax_tree_stats,
|
2019-06-30 10:30:17 +00:00
|
|
|
memory_usage(),
|
2019-01-26 17:33:33 +00:00
|
|
|
db.last_gc.elapsed().as_secs(),
|
2019-01-25 14:20:52 +00:00
|
|
|
)
|
2019-01-25 13:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct FilesStats {
|
|
|
|
total: usize,
|
2019-01-25 14:20:52 +00:00
|
|
|
size: Bytes,
|
2019-01-25 13:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for FilesStats {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2019-01-25 14:20:52 +00: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 17:33:33 +00:00
|
|
|
pub(crate) struct SyntaxTreeStats {
|
2019-01-25 14:20:52 +00:00
|
|
|
total: usize,
|
2019-01-26 17:33:33 +00:00
|
|
|
pub(crate) retained: usize,
|
2019-01-25 14:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SyntaxTreeStats {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2019-07-19 16:53:42 +00:00
|
|
|
write!(fmt, "{} trees, {} retained", self.total, self.retained)
|
2019-01-25 13:10:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 19:29:20 +00:00
|
|
|
impl FromIterator<TableEntry<FileId, Parse<ast::SourceFile>>> for SyntaxTreeStats {
|
2019-06-02 17:15:10 +00:00
|
|
|
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
|
|
|
where
|
2019-07-18 19:29:20 +00:00
|
|
|
T: IntoIterator<Item = TableEntry<FileId, Parse<ast::SourceFile>>>,
|
2019-06-02 17:15:10 +00:00
|
|
|
{
|
|
|
|
let mut res = SyntaxTreeStats::default();
|
|
|
|
for entry in iter {
|
|
|
|
res.total += 1;
|
2019-07-19 16:53:42 +00:00
|
|
|
res.retained += entry.value.is_some() as usize;
|
2019-06-02 17:15:10 +00:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 20:00:27 +00:00
|
|
|
impl<M> FromIterator<TableEntry<MacroFile, Option<(Parse<SyntaxNode>, M)>>> for SyntaxTreeStats {
|
2019-01-25 14:20:52 +00:00
|
|
|
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
|
|
|
where
|
2019-11-08 20:00:27 +00:00
|
|
|
T: IntoIterator<Item = TableEntry<MacroFile, Option<(Parse<SyntaxNode>, M)>>>,
|
2019-01-25 14:20:52 +00:00
|
|
|
{
|
|
|
|
let mut res = SyntaxTreeStats::default();
|
|
|
|
for entry in iter {
|
|
|
|
res.total += 1;
|
2019-07-19 16:53:42 +00:00
|
|
|
res.retained += entry.value.is_some() as usize;
|
2019-01-25 14:20:52 +00:00
|
|
|
}
|
|
|
|
res
|
2019-01-25 13:10:34 +00:00
|
|
|
}
|
2019-01-25 14:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct LibrarySymbolsStats {
|
|
|
|
total: usize,
|
2019-01-25 18:10:28 +00:00
|
|
|
size: Bytes,
|
2019-01-25 14:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for LibrarySymbolsStats {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2020-06-18 06:29:34 +00:00
|
|
|
write!(fmt, "{} ({}) symbols", self.total, self.size)
|
2019-01-25 14:20:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 06:29:34 +00:00
|
|
|
impl FromIterator<TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>
|
|
|
|
for LibrarySymbolsStats
|
|
|
|
{
|
2019-01-25 14:20:52 +00:00
|
|
|
fn from_iter<T>(iter: T) -> LibrarySymbolsStats
|
|
|
|
where
|
2020-06-18 06:29:34 +00:00
|
|
|
T: IntoIterator<Item = TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>,
|
2019-01-25 14:20:52 +00:00
|
|
|
{
|
|
|
|
let mut res = LibrarySymbolsStats::default();
|
|
|
|
for entry in iter {
|
|
|
|
let value = entry.value.unwrap();
|
2020-06-18 06:29:34 +00:00
|
|
|
for symbols in value.values() {
|
|
|
|
res.total += symbols.len();
|
|
|
|
res.size += symbols.memory_size();
|
|
|
|
}
|
2019-01-25 14:20:52 +00:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|