Skip to content

Commit

Permalink
Merge #5244
Browse files Browse the repository at this point in the history
5244: Add a command to compute memory usage statistics r=matklad a=jonas-schievink

This allows inspecting memory usage on a live rust-analyzer instance after it has been used interactively.

This will only work with `--features jemalloc`, so maybe it should print something more useful when that's not available? Right now it will just print 0 Bytes for every query.

Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
  • Loading branch information
bors[bot] and Jonas Schievink authored Jul 7, 2020
2 parents 0719f28 + f44c4b6 commit 73e972a
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions crates/ra_ide_db/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ impl RootDatabase {
hir::db::BodyQuery.in_db(self).sweep(sweep);
}

// Feature: Memory Usage
//
// Clears rust-analyzer's internal database and prints memory usage statistics.
//
// |===
// | Editor | Action Name
//
// | VS Code | **Rust Analyzer: Memory Usage (Clears Database)**
// |===
pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
let mut acc: Vec<(String, Bytes)> = vec![];
let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
Expand Down
13 changes: 12 additions & 1 deletion crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
cargo_target_spec::CargoTargetSpec,
config::RustfmtConfig,
from_json, from_proto,
global_state::GlobalStateSnapshot,
global_state::{GlobalState, GlobalStateSnapshot},
lsp_ext::{self, InlayHint, InlayHintsParams},
to_proto, LspError, Result,
};
Expand Down Expand Up @@ -62,6 +62,17 @@ pub(crate) fn handle_analyzer_status(snap: GlobalStateSnapshot, _: ()) -> Result
Ok(buf)
}

pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<String> {
let _p = profile("handle_memory_usage");
let mem = state.analysis_host.per_query_memory_usage();

let mut out = String::new();
for (name, bytes) in mem {
format_to!(out, "{:>8} {}\n", bytes, name);
}
Ok(out)
}

pub(crate) fn handle_syntax_tree(
snap: GlobalStateSnapshot,
params: lsp_ext::SyntaxTreeParams,
Expand Down
8 changes: 8 additions & 0 deletions crates/rust-analyzer/src/lsp_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ impl Request for AnalyzerStatus {
const METHOD: &'static str = "rust-analyzer/analyzerStatus";
}

pub enum MemoryUsage {}

impl Request for MemoryUsage {
type Params = ();
type Result = String;
const METHOD: &'static str = "rust-analyzer/memoryUsage";
}

pub enum ReloadWorkspace {}

impl Request for ReloadWorkspace {
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ impl GlobalState {
.on_sync::<lsp_ext::MatchingBrace>(|s, p| {
handlers::handle_matching_brace(s.snapshot(), p)
})?
.on_sync::<lsp_ext::MemoryUsage>(|s, p| handlers::handle_memory_usage(s, p))?
.on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)?
.on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)?
.on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)?
Expand Down
10 changes: 10 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"activationEvents": [
"onLanguage:rust",
"onCommand:rust-analyzer.analyzerStatus",
"onCommand:rust-analyzer.memoryUsage",
"onCommand:rust-analyzer.reloadWorkspace",
"workspaceContains:**/Cargo.toml"
],
Expand Down Expand Up @@ -142,6 +143,11 @@
"title": "Status",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.memoryUsage",
"title": "Memory Usage (Clears Database)",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.reloadWorkspace",
"title": "Reload workspace",
Expand Down Expand Up @@ -843,6 +849,10 @@
"command": "rust-analyzer.analyzerStatus",
"when": "inRustProject"
},
{
"command": "rust-analyzer.memoryUsage",
"when": "inRustProject"
},
{
"command": "rust-analyzer.reloadWorkspace",
"when": "inRustProject"
Expand Down
32 changes: 32 additions & 0 deletions editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,38 @@ export function analyzerStatus(ctx: Ctx): Cmd {
};
}

export function memoryUsage(ctx: Ctx): Cmd {
const tdcp = new class implements vscode.TextDocumentContentProvider {
readonly uri = vscode.Uri.parse('rust-analyzer-memory://memory');
readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();

provideTextDocumentContent(_uri: vscode.Uri): vscode.ProviderResult<string> {
if (!vscode.window.activeTextEditor) return '';

return ctx.client.sendRequest(ra.memoryUsage, null).then((mem) => {
return 'Per-query memory usage:\n' + mem + '\n(note: database has been cleared)';
});
}

get onDidChange(): vscode.Event<vscode.Uri> {
return this.eventEmitter.event;
}
}();

ctx.pushCleanup(
vscode.workspace.registerTextDocumentContentProvider(
'rust-analyzer-memory',
tdcp,
),
);

return async () => {
tdcp.eventEmitter.fire(tdcp.uri);
const document = await vscode.workspace.openTextDocument(tdcp.uri);
return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true);
};
}

export function matchingBrace(ctx: Ctx): Cmd {
return async () => {
const editor = ctx.activeRustEditor;
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/lsp_ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as lc from "vscode-languageclient";

export const analyzerStatus = new lc.RequestType<null, string, void>("rust-analyzer/analyzerStatus");
export const memoryUsage = new lc.RequestType<null, string, void>("rust-analyzer/memoryUsage");

export type Status = "loading" | "ready" | "invalid" | "needsReload";
export const status = new lc.NotificationType<Status>("rust-analyzer/status");
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
});

ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
ctx.registerCommand('memoryUsage', commands.memoryUsage);
ctx.registerCommand('reloadWorkspace', commands.reloadWorkspace);
ctx.registerCommand('matchingBrace', commands.matchingBrace);
ctx.registerCommand('joinLines', commands.joinLines);
Expand Down

0 comments on commit 73e972a

Please sign in to comment.