2019-07-04 15:05:17 -05:00
|
|
|
use std::{fmt, iter::FromIterator, sync::Arc};
|
2019-01-25 07:10:34 -06:00
|
|
|
|
2020-11-24 12:00:23 -06:00
|
|
|
use hir::{MacroFile, MacroResult};
|
2020-10-24 03:39:57 -05:00
|
|
|
use ide_db::base_db::{
|
2020-07-07 03:14:48 -05:00
|
|
|
salsa::debug::{DebugQueryTable, TableEntry},
|
2020-09-29 15:05:18 -05:00
|
|
|
CrateId, FileId, FileTextQuery, SourceDatabase, SourceRootId,
|
2019-01-22 15:15:03 -06:00
|
|
|
};
|
2020-08-13 09:39:16 -05:00
|
|
|
use ide_db::{
|
2020-02-06 05:52:32 -06:00
|
|
|
symbol_index::{LibrarySymbolsQuery, SymbolIndex},
|
|
|
|
RootDatabase,
|
|
|
|
};
|
2020-09-29 15:05:18 -05:00
|
|
|
use itertools::Itertools;
|
2020-08-13 09:39:16 -05:00
|
|
|
use profile::{memory_usage, Bytes};
|
2020-07-07 03:14:48 -05:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-09-29 15:05:18 -05:00
|
|
|
use stdx::format_to;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{ast, Parse, SyntaxNode};
|
2019-01-22 15:15:03 -06:00
|
|
|
|
2019-09-26 04:31:16 -05:00
|
|
|
fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
|
2020-10-24 03:39:57 -05:00
|
|
|
ide_db::base_db::ParseQuery.in_db(db).entries::<SyntaxTreeStats>()
|
2019-06-02 12:15:10 -05:00
|
|
|
}
|
2019-09-26 04:31:16 -05:00
|
|
|
fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
|
2020-11-24 14:57:51 -06:00
|
|
|
hir::db::ParseMacroExpansionQuery.in_db(db).entries::<SyntaxTreeStats>()
|
2019-01-26 11:33:33 -06:00
|
|
|
}
|
|
|
|
|
2020-05-31 03:14:36 -05:00
|
|
|
// Feature: Status
|
|
|
|
//
|
|
|
|
// Shows internal statistic about memory usage of rust-analyzer.
|
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Action Name
|
|
|
|
//
|
|
|
|
// | VS Code | **Rust Analyzer: Status**
|
|
|
|
// |===
|
2020-09-29 15:05:18 -05:00
|
|
|
pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
format_to!(buf, "{}\n", FileTextQuery.in_db(db).entries::<FilesStats>());
|
|
|
|
format_to!(buf, "{}\n", LibrarySymbolsQuery.in_db(db).entries::<LibrarySymbolsStats>());
|
|
|
|
format_to!(buf, "{}\n", syntax_tree_stats(db));
|
|
|
|
format_to!(buf, "{} (macros)\n", macro_syntax_tree_stats(db));
|
|
|
|
format_to!(buf, "{} total\n", memory_usage());
|
|
|
|
|
|
|
|
if let Some(file_id) = file_id {
|
|
|
|
format_to!(buf, "\nfile info:\n");
|
|
|
|
let krate = crate::parent_module::crate_for(db, file_id).pop();
|
|
|
|
match krate {
|
|
|
|
Some(krate) => {
|
|
|
|
let crate_graph = db.crate_graph();
|
2020-10-20 08:38:11 -05:00
|
|
|
let display_crate = |krate: CrateId| match &crate_graph[krate].display_name {
|
2020-09-29 15:05:18 -05:00
|
|
|
Some(it) => format!("{}({:?})", it, krate),
|
|
|
|
None => format!("{:?}", krate),
|
|
|
|
};
|
|
|
|
format_to!(buf, "crate: {}\n", display_crate(krate));
|
|
|
|
let deps = crate_graph[krate]
|
|
|
|
.dependencies
|
|
|
|
.iter()
|
|
|
|
.map(|dep| format!("{}={:?}", dep.name, dep.crate_id))
|
|
|
|
.format(", ");
|
|
|
|
format_to!(buf, "deps: {}\n", deps);
|
|
|
|
}
|
|
|
|
None => format_to!(buf, "does not belong to any crate"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SyntaxTreeStats {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2019-07-19 11:53:42 -05:00
|
|
|
write!(fmt, "{} trees, {} retained", self.total, self.retained)
|
2019-01-25 07:10:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 14:29:20 -05:00
|
|
|
impl FromIterator<TableEntry<FileId, Parse<ast::SourceFile>>> for SyntaxTreeStats {
|
2019-06-02 12:15:10 -05:00
|
|
|
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
|
|
|
where
|
2019-07-18 14:29:20 -05:00
|
|
|
T: IntoIterator<Item = TableEntry<FileId, Parse<ast::SourceFile>>>,
|
2019-06-02 12:15:10 -05:00
|
|
|
{
|
|
|
|
let mut res = SyntaxTreeStats::default();
|
|
|
|
for entry in iter {
|
|
|
|
res.total += 1;
|
2019-07-19 11:53:42 -05:00
|
|
|
res.retained += entry.value.is_some() as usize;
|
2019-06-02 12:15:10 -05:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 12:00:23 -06:00
|
|
|
impl<M> FromIterator<TableEntry<MacroFile, MacroResult<(Parse<SyntaxNode>, M)>>>
|
|
|
|
for SyntaxTreeStats
|
|
|
|
{
|
2019-01-25 08:20:52 -06:00
|
|
|
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
|
|
|
where
|
2020-11-24 12:00:23 -06:00
|
|
|
T: IntoIterator<Item = TableEntry<MacroFile, MacroResult<(Parse<SyntaxNode>, M)>>>,
|
2019-01-25 08:20:52 -06:00
|
|
|
{
|
|
|
|
let mut res = SyntaxTreeStats::default();
|
|
|
|
for entry in iter {
|
|
|
|
res.total += 1;
|
2019-07-19 11:53:42 -05:00
|
|
|
res.retained += entry.value.is_some() as usize;
|
2019-01-25 08:20:52 -06:00
|
|
|
}
|
|
|
|
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 {
|
2020-09-29 14:13:58 -05:00
|
|
|
write!(fmt, "{} ({}) index symbols", self.total, self.size)
|
2019-01-25 08:20:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 01:29:34 -05:00
|
|
|
impl FromIterator<TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>
|
|
|
|
for LibrarySymbolsStats
|
|
|
|
{
|
2019-01-25 08:20:52 -06:00
|
|
|
fn from_iter<T>(iter: T) -> LibrarySymbolsStats
|
|
|
|
where
|
2020-06-18 01:29:34 -05:00
|
|
|
T: IntoIterator<Item = TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>,
|
2019-01-25 08:20:52 -06:00
|
|
|
{
|
|
|
|
let mut res = LibrarySymbolsStats::default();
|
|
|
|
for entry in iter {
|
|
|
|
let value = entry.value.unwrap();
|
2020-06-18 01:29:34 -05:00
|
|
|
for symbols in value.values() {
|
|
|
|
res.total += symbols.len();
|
|
|
|
res.size += symbols.memory_size();
|
|
|
|
}
|
2019-01-25 08:20:52 -06:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|