2022-08-09 01:16:32 +02:00
|
|
|
use std::{fmt, sync::Arc};
|
2019-01-25 16:10:34 +03:00
|
|
|
|
2020-11-26 16:48:17 +01:00
|
|
|
use hir::{ExpandResult, MacroFile};
|
2020-10-24 11:39:57 +03:00
|
|
|
use ide_db::base_db::{
|
2020-07-07 10:14:48 +02:00
|
|
|
salsa::debug::{DebugQueryTable, TableEntry},
|
2020-09-29 22:05:18 +02:00
|
|
|
CrateId, FileId, FileTextQuery, SourceDatabase, SourceRootId,
|
2019-01-23 00:15:03 +03:00
|
|
|
};
|
2020-08-13 16:39:16 +02:00
|
|
|
use ide_db::{
|
2020-02-06 12:52:32 +01:00
|
|
|
symbol_index::{LibrarySymbolsQuery, SymbolIndex},
|
|
|
|
RootDatabase,
|
|
|
|
};
|
2020-09-29 22:05:18 +02:00
|
|
|
use itertools::Itertools;
|
2020-08-13 16:39:16 +02:00
|
|
|
use profile::{memory_usage, Bytes};
|
2021-06-28 17:50:24 +01:00
|
|
|
use std::env;
|
2020-09-29 22:05:18 +02:00
|
|
|
use stdx::format_to;
|
2020-08-12 18:26:51 +02:00
|
|
|
use syntax::{ast, Parse, SyntaxNode};
|
2019-01-23 00:15:03 +03:00
|
|
|
|
2019-09-26 12:31:16 +03:00
|
|
|
fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
|
2020-10-24 11:39:57 +03:00
|
|
|
ide_db::base_db::ParseQuery.in_db(db).entries::<SyntaxTreeStats>()
|
2019-06-02 20:15:10 +03:00
|
|
|
}
|
2019-09-26 12:31:16 +03:00
|
|
|
fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
|
2020-11-24 21:57:51 +01:00
|
|
|
hir::db::ParseMacroExpansionQuery.in_db(db).entries::<SyntaxTreeStats>()
|
2019-01-26 20:33:33 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 10:14:36 +02:00
|
|
|
// Feature: Status
|
|
|
|
//
|
|
|
|
// Shows internal statistic about memory usage of rust-analyzer.
|
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Action Name
|
|
|
|
//
|
2022-08-01 13:47:09 +02:00
|
|
|
// | VS Code | **rust-analyzer: Status**
|
2020-05-31 10:14:36 +02:00
|
|
|
// |===
|
2021-03-31 00:08:10 +01:00
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113065584-05f34500-91b1-11eb-98cc-5c196f76be7f.gif[]
|
2020-09-29 22:05:18 +02: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));
|
2021-06-18 19:22:03 +01:00
|
|
|
format_to!(buf, "{} (Macros)\n", macro_syntax_tree_stats(db));
|
2021-06-19 07:51:44 +01:00
|
|
|
format_to!(buf, "{} in total\n", memory_usage());
|
2021-06-28 17:50:24 +01:00
|
|
|
if env::var("RA_COUNT").is_ok() {
|
2021-06-29 20:34:52 +01:00
|
|
|
format_to!(buf, "\nCounts:\n{}", profile::countme::get_all());
|
2021-06-28 17:50:24 +01:00
|
|
|
}
|
2020-09-29 22:05:18 +02:00
|
|
|
|
|
|
|
if let Some(file_id) = file_id {
|
2021-06-18 19:22:03 +01:00
|
|
|
format_to!(buf, "\nFile info:\n");
|
2022-10-17 17:53:50 +02:00
|
|
|
let crates = crate::parent_module::crates_for(db, file_id);
|
2022-01-17 18:10:01 +01:00
|
|
|
if crates.is_empty() {
|
|
|
|
format_to!(buf, "Does not belong to any crate");
|
|
|
|
}
|
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
for krate in crates {
|
|
|
|
let display_crate = |krate: CrateId| match &crate_graph[krate].display_name {
|
2022-12-23 13:42:58 -05:00
|
|
|
Some(it) => format!("{it}({krate:?})"),
|
|
|
|
None => format!("{krate:?}"),
|
2022-01-17 18:10:01 +01:00
|
|
|
};
|
|
|
|
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, "Dependencies: {}\n", deps);
|
2020-09-29 22:05:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-21 19:04:50 +03:00
|
|
|
|
2021-06-18 23:34:00 +01:00
|
|
|
buf.trim().to_string()
|
2019-01-25 16:10:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct FilesStats {
|
|
|
|
total: usize,
|
2019-01-25 17:20:52 +03:00
|
|
|
size: Bytes,
|
2019-01-25 16:10:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for FilesStats {
|
2022-07-20 15:02:08 +02:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-06-19 07:51:44 +01:00
|
|
|
write!(fmt, "{} of files", self.size)
|
2019-01-25 17:20:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 20:33:33 +03:00
|
|
|
pub(crate) struct SyntaxTreeStats {
|
2019-01-25 17:20:52 +03:00
|
|
|
total: usize,
|
2019-01-26 20:33:33 +03:00
|
|
|
pub(crate) retained: usize,
|
2019-01-25 17:20:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SyntaxTreeStats {
|
2022-07-20 15:02:08 +02:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-06-19 10:14:15 +01:00
|
|
|
write!(fmt, "{} trees, {} preserved", self.total, self.retained)
|
2019-01-25 16:10:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 22:29:20 +03:00
|
|
|
impl FromIterator<TableEntry<FileId, Parse<ast::SourceFile>>> for SyntaxTreeStats {
|
2019-06-02 20:15:10 +03:00
|
|
|
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
|
|
|
where
|
2019-07-18 22:29:20 +03:00
|
|
|
T: IntoIterator<Item = TableEntry<FileId, Parse<ast::SourceFile>>>,
|
2019-06-02 20:15:10 +03:00
|
|
|
{
|
|
|
|
let mut res = SyntaxTreeStats::default();
|
|
|
|
for entry in iter {
|
|
|
|
res.total += 1;
|
2019-07-19 19:53:42 +03:00
|
|
|
res.retained += entry.value.is_some() as usize;
|
2019-06-02 20:15:10 +03:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:48:17 +01:00
|
|
|
impl<M> FromIterator<TableEntry<MacroFile, ExpandResult<Option<(Parse<SyntaxNode>, M)>>>>
|
2020-11-24 19:00:23 +01:00
|
|
|
for SyntaxTreeStats
|
|
|
|
{
|
2019-01-25 17:20:52 +03:00
|
|
|
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
|
|
|
where
|
2020-11-26 16:48:17 +01:00
|
|
|
T: IntoIterator<Item = TableEntry<MacroFile, ExpandResult<Option<(Parse<SyntaxNode>, M)>>>>,
|
2019-01-25 17:20:52 +03:00
|
|
|
{
|
|
|
|
let mut res = SyntaxTreeStats::default();
|
|
|
|
for entry in iter {
|
|
|
|
res.total += 1;
|
2019-07-19 19:53:42 +03:00
|
|
|
res.retained += entry.value.is_some() as usize;
|
2019-01-25 17:20:52 +03:00
|
|
|
}
|
|
|
|
res
|
2019-01-25 16:10:34 +03:00
|
|
|
}
|
2019-01-25 17:20:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct LibrarySymbolsStats {
|
|
|
|
total: usize,
|
2019-01-25 21:10:28 +03:00
|
|
|
size: Bytes,
|
2019-01-25 17:20:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for LibrarySymbolsStats {
|
2022-07-20 15:02:08 +02:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-06-28 07:31:54 +01:00
|
|
|
write!(fmt, "{} of index symbols ({})", self.size, self.total)
|
2019-01-25 17:20:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 09:36:00 +00:00
|
|
|
impl FromIterator<TableEntry<SourceRootId, Arc<SymbolIndex>>> for LibrarySymbolsStats {
|
2019-01-25 17:20:52 +03:00
|
|
|
fn from_iter<T>(iter: T) -> LibrarySymbolsStats
|
|
|
|
where
|
2021-11-29 09:36:00 +00:00
|
|
|
T: IntoIterator<Item = TableEntry<SourceRootId, Arc<SymbolIndex>>>,
|
2019-01-25 17:20:52 +03:00
|
|
|
{
|
|
|
|
let mut res = LibrarySymbolsStats::default();
|
|
|
|
for entry in iter {
|
2021-11-29 09:36:00 +00:00
|
|
|
let symbols = entry.value.unwrap();
|
|
|
|
res.total += symbols.len();
|
|
|
|
res.size += symbols.memory_size();
|
2019-01-25 17:20:52 +03:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|