aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-07-07 11:10:14 +0100
committerJonas Schievink <[email protected]>2020-07-07 11:10:14 +0100
commitf44c4b61e131284287b24dea6da6324cbe9cb252 (patch)
treed05b6d48374da0097d359b16115da959b57048ce
parent0f5d62a3f3b33edadea50ea93c725ac36460c0d7 (diff)
Add a command to compute memory usage statistics
-rw-r--r--crates/ra_ide_db/src/change.rs9
-rw-r--r--crates/rust-analyzer/src/handlers.rs13
-rw-r--r--crates/rust-analyzer/src/lsp_ext.rs8
-rw-r--r--crates/rust-analyzer/src/main_loop.rs1
-rw-r--r--editors/code/package.json10
-rw-r--r--editors/code/src/commands.ts32
-rw-r--r--editors/code/src/lsp_ext.ts1
-rw-r--r--editors/code/src/main.ts1
8 files changed, 74 insertions, 1 deletions
diff --git a/crates/ra_ide_db/src/change.rs b/crates/ra_ide_db/src/change.rs
index d8da3f949..84c6f40ff 100644
--- a/crates/ra_ide_db/src/change.rs
+++ b/crates/ra_ide_db/src/change.rs
@@ -164,6 +164,15 @@ impl RootDatabase {
164 hir::db::BodyQuery.in_db(self).sweep(sweep); 164 hir::db::BodyQuery.in_db(self).sweep(sweep);
165 } 165 }
166 166
167 // Feature: Memory Usage
168 //
169 // Clears rust-analyzer's internal database and prints memory usage statistics.
170 //
171 // |===
172 // | Editor | Action Name
173 //
174 // | VS Code | **Rust Analyzer: Memory Usage (Clears Database)**
175 // |===
167 pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> { 176 pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
168 let mut acc: Vec<(String, Bytes)> = vec![]; 177 let mut acc: Vec<(String, Bytes)> = vec![];
169 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); 178 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index cc83a1406..f374334fe 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -32,7 +32,7 @@ use crate::{
32 cargo_target_spec::CargoTargetSpec, 32 cargo_target_spec::CargoTargetSpec,
33 config::RustfmtConfig, 33 config::RustfmtConfig,
34 from_json, from_proto, 34 from_json, from_proto,
35 global_state::GlobalStateSnapshot, 35 global_state::{GlobalState, GlobalStateSnapshot},
36 lsp_ext::{self, InlayHint, InlayHintsParams}, 36 lsp_ext::{self, InlayHint, InlayHintsParams},
37 to_proto, LspError, Result, 37 to_proto, LspError, Result,
38}; 38};
@@ -62,6 +62,17 @@ pub(crate) fn handle_analyzer_status(snap: GlobalStateSnapshot, _: ()) -> Result
62 Ok(buf) 62 Ok(buf)
63} 63}
64 64
65pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<String> {
66 let _p = profile("handle_memory_usage");
67 let mem = state.analysis_host.per_query_memory_usage();
68
69 let mut out = String::new();
70 for (name, bytes) in mem {
71 format_to!(out, "{:>8} {}\n", bytes, name);
72 }
73 Ok(out)
74}
75
65pub(crate) fn handle_syntax_tree( 76pub(crate) fn handle_syntax_tree(
66 snap: GlobalStateSnapshot, 77 snap: GlobalStateSnapshot,
67 params: lsp_ext::SyntaxTreeParams, 78 params: lsp_ext::SyntaxTreeParams,
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs
index d225ad5ff..ba8a0231f 100644
--- a/crates/rust-analyzer/src/lsp_ext.rs
+++ b/crates/rust-analyzer/src/lsp_ext.rs
@@ -14,6 +14,14 @@ impl Request for AnalyzerStatus {
14 const METHOD: &'static str = "rust-analyzer/analyzerStatus"; 14 const METHOD: &'static str = "rust-analyzer/analyzerStatus";
15} 15}
16 16
17pub enum MemoryUsage {}
18
19impl Request for MemoryUsage {
20 type Params = ();
21 type Result = String;
22 const METHOD: &'static str = "rust-analyzer/memoryUsage";
23}
24
17pub enum ReloadWorkspace {} 25pub enum ReloadWorkspace {}
18 26
19impl Request for ReloadWorkspace { 27impl Request for ReloadWorkspace {
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index cfde55431..5900886e7 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -341,6 +341,7 @@ impl GlobalState {
341 .on_sync::<lsp_ext::MatchingBrace>(|s, p| { 341 .on_sync::<lsp_ext::MatchingBrace>(|s, p| {
342 handlers::handle_matching_brace(s.snapshot(), p) 342 handlers::handle_matching_brace(s.snapshot(), p)
343 })? 343 })?
344 .on_sync::<lsp_ext::MemoryUsage>(|s, p| handlers::handle_memory_usage(s, p))?
344 .on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)? 345 .on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)?
345 .on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)? 346 .on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)?
346 .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)? 347 .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)?
diff --git a/editors/code/package.json b/editors/code/package.json
index 4b47fc9d3..743a2290c 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -61,6 +61,7 @@
61 "activationEvents": [ 61 "activationEvents": [
62 "onLanguage:rust", 62 "onLanguage:rust",
63 "onCommand:rust-analyzer.analyzerStatus", 63 "onCommand:rust-analyzer.analyzerStatus",
64 "onCommand:rust-analyzer.memoryUsage",
64 "onCommand:rust-analyzer.reloadWorkspace", 65 "onCommand:rust-analyzer.reloadWorkspace",
65 "workspaceContains:**/Cargo.toml" 66 "workspaceContains:**/Cargo.toml"
66 ], 67 ],
@@ -143,6 +144,11 @@
143 "category": "Rust Analyzer" 144 "category": "Rust Analyzer"
144 }, 145 },
145 { 146 {
147 "command": "rust-analyzer.memoryUsage",
148 "title": "Memory Usage (Clears Database)",
149 "category": "Rust Analyzer"
150 },
151 {
146 "command": "rust-analyzer.reloadWorkspace", 152 "command": "rust-analyzer.reloadWorkspace",
147 "title": "Reload workspace", 153 "title": "Reload workspace",
148 "category": "Rust Analyzer" 154 "category": "Rust Analyzer"
@@ -844,6 +850,10 @@
844 "when": "inRustProject" 850 "when": "inRustProject"
845 }, 851 },
846 { 852 {
853 "command": "rust-analyzer.memoryUsage",
854 "when": "inRustProject"
855 },
856 {
847 "command": "rust-analyzer.reloadWorkspace", 857 "command": "rust-analyzer.reloadWorkspace",
848 "when": "inRustProject" 858 "when": "inRustProject"
849 }, 859 },
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 19a9c2a0d..1f3a7cf7e 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -55,6 +55,38 @@ export function analyzerStatus(ctx: Ctx): Cmd {
55 }; 55 };
56} 56}
57 57
58export function memoryUsage(ctx: Ctx): Cmd {
59 const tdcp = new class implements vscode.TextDocumentContentProvider {
60 readonly uri = vscode.Uri.parse('rust-analyzer-memory://memory');
61 readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
62
63 provideTextDocumentContent(_uri: vscode.Uri): vscode.ProviderResult<string> {
64 if (!vscode.window.activeTextEditor) return '';
65
66 return ctx.client.sendRequest(ra.memoryUsage, null).then((mem) => {
67 return 'Per-query memory usage:\n' + mem + '\n(note: database has been cleared)';
68 });
69 }
70
71 get onDidChange(): vscode.Event<vscode.Uri> {
72 return this.eventEmitter.event;
73 }
74 }();
75
76 ctx.pushCleanup(
77 vscode.workspace.registerTextDocumentContentProvider(
78 'rust-analyzer-memory',
79 tdcp,
80 ),
81 );
82
83 return async () => {
84 tdcp.eventEmitter.fire(tdcp.uri);
85 const document = await vscode.workspace.openTextDocument(tdcp.uri);
86 return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true);
87 };
88}
89
58export function matchingBrace(ctx: Ctx): Cmd { 90export function matchingBrace(ctx: Ctx): Cmd {
59 return async () => { 91 return async () => {
60 const editor = ctx.activeRustEditor; 92 const editor = ctx.activeRustEditor;
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts
index bf4703239..5f32cb40e 100644
--- a/editors/code/src/lsp_ext.ts
+++ b/editors/code/src/lsp_ext.ts
@@ -5,6 +5,7 @@
5import * as lc from "vscode-languageclient"; 5import * as lc from "vscode-languageclient";
6 6
7export const analyzerStatus = new lc.RequestType<null, string, void>("rust-analyzer/analyzerStatus"); 7export const analyzerStatus = new lc.RequestType<null, string, void>("rust-analyzer/analyzerStatus");
8export const memoryUsage = new lc.RequestType<null, string, void>("rust-analyzer/memoryUsage");
8 9
9export type Status = "loading" | "ready" | "invalid" | "needsReload"; 10export type Status = "loading" | "ready" | "invalid" | "needsReload";
10export const status = new lc.NotificationType<Status>("rust-analyzer/status"); 11export const status = new lc.NotificationType<Status>("rust-analyzer/status");
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 4b990afa1..eda95ae5c 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -96,6 +96,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
96 }); 96 });
97 97
98 ctx.registerCommand('analyzerStatus', commands.analyzerStatus); 98 ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
99 ctx.registerCommand('memoryUsage', commands.memoryUsage);
99 ctx.registerCommand('reloadWorkspace', commands.reloadWorkspace); 100 ctx.registerCommand('reloadWorkspace', commands.reloadWorkspace);
100 ctx.registerCommand('matchingBrace', commands.matchingBrace); 101 ctx.registerCommand('matchingBrace', commands.matchingBrace);
101 ctx.registerCommand('joinLines', commands.joinLines); 102 ctx.registerCommand('joinLines', commands.joinLines);