Skip to content

Commit 3df4b8c

Browse files
Merge #6830
6830: Avoid panic when collecting memory metrics r=jonas-schievink a=jonas-schievink This is getting hit during metrics collection. bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents 24731e1 + cb35d3a commit 3df4b8c

File tree

5 files changed

+29
-37
lines changed

5 files changed

+29
-37
lines changed

crates/rust-analyzer/src/cli.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ mod ssr;
1010
use std::io::Read;
1111

1212
use anyhow::Result;
13-
use ide::Analysis;
13+
use ide::{Analysis, AnalysisHost};
1414
use syntax::{AstNode, SourceFile};
15+
use vfs::Vfs;
1516

1617
pub use self::{
1718
analysis_bench::{BenchCmd, BenchWhat, Position},
@@ -82,3 +83,23 @@ fn report_metric(metric: &str, value: u64, unit: &str) {
8283
}
8384
println!("METRIC:{}:{}:{}", metric, value, unit)
8485
}
86+
87+
fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
88+
let mut mem = host.per_query_memory_usage();
89+
90+
let before = profile::memory_usage();
91+
drop(vfs);
92+
let vfs = before.allocated - profile::memory_usage().allocated;
93+
mem.push(("VFS".into(), vfs));
94+
95+
let before = profile::memory_usage();
96+
drop(host);
97+
mem.push(("Unaccounted".into(), before.allocated - profile::memory_usage().allocated));
98+
99+
mem.push(("Remaining".into(), profile::memory_usage().allocated));
100+
101+
for (name, bytes) in mem {
102+
// NOTE: Not a debug print, so avoid going through the `eprintln` defined above.
103+
eprintln!("{:>8} {}", bytes, name);
104+
}
105+
}

crates/rust-analyzer/src/cli/analysis_bench.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ use ide_db::base_db::{
1212
};
1313
use vfs::AbsPathBuf;
1414

15-
use crate::{
16-
cli::{load_cargo::load_cargo, Verbosity},
17-
print_memory_usage,
18-
};
15+
use crate::cli::{load_cargo::load_cargo, print_memory_usage, Verbosity};
1916

2017
pub struct BenchCmd {
2118
pub path: PathBuf,

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ use rustc_hash::FxHashSet;
2323
use stdx::format_to;
2424
use syntax::AstNode;
2525

26-
use crate::{
27-
cli::{
28-
load_cargo::load_cargo, progress_report::ProgressReport, report_metric, Result, Verbosity,
29-
},
30-
print_memory_usage,
26+
use crate::cli::{
27+
load_cargo::load_cargo, print_memory_usage, progress_report::ProgressReport, report_metric,
28+
Result, Verbosity,
3129
};
3230
use profile::StopWatch;
3331

crates/rust-analyzer/src/lib.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ mod document;
3737
pub mod lsp_ext;
3838
pub mod config;
3939

40-
use ide::AnalysisHost;
4140
use serde::de::DeserializeOwned;
4241
use std::fmt;
43-
use vfs::Vfs;
4442

4543
pub use crate::{caps::server_capabilities, main_loop::main_loop};
4644

@@ -72,22 +70,3 @@ impl fmt::Display for LspError {
7270
}
7371

7472
impl std::error::Error for LspError {}
75-
76-
fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
77-
let mut mem = host.per_query_memory_usage();
78-
79-
let before = profile::memory_usage();
80-
drop(vfs);
81-
let vfs = before.allocated - profile::memory_usage().allocated;
82-
mem.push(("VFS".into(), vfs));
83-
84-
let before = profile::memory_usage();
85-
drop(host);
86-
mem.push(("Unaccounted".into(), before.allocated - profile::memory_usage().allocated));
87-
88-
mem.push(("Remaining".into(), profile::memory_usage().allocated));
89-
90-
for (name, bytes) in mem {
91-
eprintln!("{:>8} {}", bytes, name);
92-
}
93-
}

xtask/src/metrics.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{
33
env,
44
io::Write as _,
55
path::Path,
6-
process::{Command, Stdio},
76
time::{Instant, SystemTime, UNIX_EPOCH},
87
};
98

@@ -82,11 +81,9 @@ impl Metrics {
8281
}
8382
fn measure_analysis_stats_path(&mut self, name: &str, path: &str) -> Result<()> {
8483
eprintln!("\nMeasuring analysis-stats/{}", name);
85-
let output = Command::new("./target/release/rust-analyzer")
86-
.args(&["analysis-stats", "--quiet", "--memory-usage", path])
87-
.stderr(Stdio::inherit())
88-
.output()?;
89-
let output = String::from_utf8(output.stdout)?;
84+
let output =
85+
cmd!("./target/release/rust-analyzer analysis-stats --quiet --memory-usage {path}")
86+
.read()?;
9087
for (metric, value, unit) in parse_metrics(&output) {
9188
self.report(&format!("analysis-stats/{}/{}", name, metric), value, unit.into());
9289
}

0 commit comments

Comments
 (0)