1 use std::{fmt, iter::FromIterator, sync::Arc};
3 use hir::{ExpandResult, MacroFile};
5 salsa::debug::{DebugQueryTable, TableEntry},
6 CrateId, FileId, FileTextQuery, SourceDatabase, SourceRootId,
9 symbol_index::{LibrarySymbolsQuery, SymbolIndex},
12 use itertools::Itertools;
13 use profile::{memory_usage, Bytes};
14 use rustc_hash::FxHashMap;
16 use syntax::{ast, Parse, SyntaxNode};
18 fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
19 ide_db::base_db::ParseQuery.in_db(db).entries::<SyntaxTreeStats>()
21 fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
22 hir::db::ParseMacroExpansionQuery.in_db(db).entries::<SyntaxTreeStats>()
27 // Shows internal statistic about memory usage of rust-analyzer.
30 // | Editor | Action Name
32 // | VS Code | **Rust Analyzer: Status**
34 pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
35 let mut buf = String::new();
36 format_to!(buf, "{}\n", FileTextQuery.in_db(db).entries::<FilesStats>());
37 format_to!(buf, "{}\n", LibrarySymbolsQuery.in_db(db).entries::<LibrarySymbolsStats>());
38 format_to!(buf, "{}\n", syntax_tree_stats(db));
39 format_to!(buf, "{} (macros)\n", macro_syntax_tree_stats(db));
40 format_to!(buf, "{} total\n", memory_usage());
41 format_to!(buf, "\ncounts:\n{}", profile::countme::get_all());
43 if let Some(file_id) = file_id {
44 format_to!(buf, "\nfile info:\n");
45 let krate = crate::parent_module::crate_for(db, file_id).pop();
48 let crate_graph = db.crate_graph();
49 let display_crate = |krate: CrateId| match &crate_graph[krate].display_name {
50 Some(it) => format!("{}({:?})", it, krate),
51 None => format!("{:?}", krate),
53 format_to!(buf, "crate: {}\n", display_crate(krate));
54 let deps = crate_graph[krate]
57 .map(|dep| format!("{}={:?}", dep.name, dep.crate_id))
59 format_to!(buf, "deps: {}\n", deps);
61 None => format_to!(buf, "does not belong to any crate"),
74 impl fmt::Display for FilesStats {
75 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
76 write!(fmt, "{} ({}) files", self.total, self.size)
80 impl FromIterator<TableEntry<FileId, Arc<String>>> for FilesStats {
81 fn from_iter<T>(iter: T) -> FilesStats
83 T: IntoIterator<Item = TableEntry<FileId, Arc<String>>>,
85 let mut res = FilesStats::default();
88 res.size += entry.value.unwrap().len();
95 pub(crate) struct SyntaxTreeStats {
97 pub(crate) retained: usize,
100 impl fmt::Display for SyntaxTreeStats {
101 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
102 write!(fmt, "{} trees, {} retained", self.total, self.retained)
106 impl FromIterator<TableEntry<FileId, Parse<ast::SourceFile>>> for SyntaxTreeStats {
107 fn from_iter<T>(iter: T) -> SyntaxTreeStats
109 T: IntoIterator<Item = TableEntry<FileId, Parse<ast::SourceFile>>>,
111 let mut res = SyntaxTreeStats::default();
114 res.retained += entry.value.is_some() as usize;
120 impl<M> FromIterator<TableEntry<MacroFile, ExpandResult<Option<(Parse<SyntaxNode>, M)>>>>
123 fn from_iter<T>(iter: T) -> SyntaxTreeStats
125 T: IntoIterator<Item = TableEntry<MacroFile, ExpandResult<Option<(Parse<SyntaxNode>, M)>>>>,
127 let mut res = SyntaxTreeStats::default();
130 res.retained += entry.value.is_some() as usize;
137 struct LibrarySymbolsStats {
142 impl fmt::Display for LibrarySymbolsStats {
143 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
144 write!(fmt, "{} ({}) index symbols", self.total, self.size)
148 impl FromIterator<TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>
149 for LibrarySymbolsStats
151 fn from_iter<T>(iter: T) -> LibrarySymbolsStats
153 T: IntoIterator<Item = TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>,
155 let mut res = LibrarySymbolsStats::default();
157 let value = entry.value.unwrap();
158 for symbols in value.values() {
159 res.total += symbols.len();
160 res.size += symbols.memory_size();