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>
This commit is contained in:
bors[bot] 2020-12-11 17:25:16 +00:00 committed by GitHub
commit 3df4b8c1fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 37 deletions

View file

@ -10,8 +10,9 @@ mod ssr;
use std::io::Read;
use anyhow::Result;
use ide::Analysis;
use ide::{Analysis, AnalysisHost};
use syntax::{AstNode, SourceFile};
use vfs::Vfs;
pub use self::{
analysis_bench::{BenchCmd, BenchWhat, Position},
@ -82,3 +83,23 @@ fn report_metric(metric: &str, value: u64, unit: &str) {
}
println!("METRIC:{}:{}:{}", metric, value, unit)
}
fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
let mut mem = host.per_query_memory_usage();
let before = profile::memory_usage();
drop(vfs);
let vfs = before.allocated - profile::memory_usage().allocated;
mem.push(("VFS".into(), vfs));
let before = profile::memory_usage();
drop(host);
mem.push(("Unaccounted".into(), before.allocated - profile::memory_usage().allocated));
mem.push(("Remaining".into(), profile::memory_usage().allocated));
for (name, bytes) in mem {
// NOTE: Not a debug print, so avoid going through the `eprintln` defined above.
eprintln!("{:>8} {}", bytes, name);
}
}

View file

@ -12,10 +12,7 @@ use ide_db::base_db::{
};
use vfs::AbsPathBuf;
use crate::{
cli::{load_cargo::load_cargo, Verbosity},
print_memory_usage,
};
use crate::cli::{load_cargo::load_cargo, print_memory_usage, Verbosity};
pub struct BenchCmd {
pub path: PathBuf,

View file

@ -23,11 +23,9 @@ use rustc_hash::FxHashSet;
use stdx::format_to;
use syntax::AstNode;
use crate::{
cli::{
load_cargo::load_cargo, progress_report::ProgressReport, report_metric, Result, Verbosity,
},
print_memory_usage,
use crate::cli::{
load_cargo::load_cargo, print_memory_usage, progress_report::ProgressReport, report_metric,
Result, Verbosity,
};
use profile::StopWatch;

View file

@ -37,10 +37,8 @@ mod document;
pub mod lsp_ext;
pub mod config;
use ide::AnalysisHost;
use serde::de::DeserializeOwned;
use std::fmt;
use vfs::Vfs;
pub use crate::{caps::server_capabilities, main_loop::main_loop};
@ -72,22 +70,3 @@ impl fmt::Display for LspError {
}
impl std::error::Error for LspError {}
fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
let mut mem = host.per_query_memory_usage();
let before = profile::memory_usage();
drop(vfs);
let vfs = before.allocated - profile::memory_usage().allocated;
mem.push(("VFS".into(), vfs));
let before = profile::memory_usage();
drop(host);
mem.push(("Unaccounted".into(), before.allocated - profile::memory_usage().allocated));
mem.push(("Remaining".into(), profile::memory_usage().allocated));
for (name, bytes) in mem {
eprintln!("{:>8} {}", bytes, name);
}
}

View file

@ -3,7 +3,6 @@ use std::{
env,
io::Write as _,
path::Path,
process::{Command, Stdio},
time::{Instant, SystemTime, UNIX_EPOCH},
};
@ -82,11 +81,9 @@ impl Metrics {
}
fn measure_analysis_stats_path(&mut self, name: &str, path: &str) -> Result<()> {
eprintln!("\nMeasuring analysis-stats/{}", name);
let output = Command::new("./target/release/rust-analyzer")
.args(&["analysis-stats", "--quiet", "--memory-usage", path])
.stderr(Stdio::inherit())
.output()?;
let output = String::from_utf8(output.stdout)?;
let output =
cmd!("./target/release/rust-analyzer analysis-stats --quiet --memory-usage {path}")
.read()?;
for (metric, value, unit) in parse_metrics(&output) {
self.report(&format!("analysis-stats/{}/{}", name, metric), value, unit.into());
}