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

377 lines
14 KiB
Rust
Raw Normal View History

2020-02-18 12:11:32 +01:00
//! Fully type-check project and print various stats, like the number of type
//! errors.
2020-07-29 19:49:10 +02:00
use std::{
env,
path::PathBuf,
time::{SystemTime, UNIX_EPOCH},
2020-07-29 19:49:10 +02:00
};
2020-06-29 18:07:52 +03:00
2019-12-08 12:44:14 +01:00
use hir::{
db::{AstDatabase, DefDatabase, HirDatabase},
AssocItem, Crate, Function, HasSource, HirDisplay, ModuleDef,
2019-12-08 12:44:14 +01:00
};
use hir_def::{body::BodySourceMap, expr::ExprId, FunctionId};
2021-04-06 12:36:12 +02:00
use hir_ty::{TyExt, TypeWalk};
use ide::{Analysis, AnalysisHost, LineCol, RootDatabase};
2020-10-24 11:39:57 +03:00
use ide_db::base_db::{
salsa::{self, ParallelDatabase},
SourceDatabaseExt,
};
2020-07-25 10:35:45 +02:00
use itertools::Itertools;
2020-07-29 19:49:10 +02:00
use oorandom::Rand32;
2021-03-14 18:31:14 +08:00
use project_model::CargoConfig;
2020-07-25 10:35:45 +02:00
use rayon::prelude::*;
use rustc_hash::FxHashSet;
2020-03-28 11:20:34 +01:00
use stdx::format_to;
2020-08-12 18:26:51 +02:00
use syntax::AstNode;
use vfs::{Vfs, VfsPath};
2020-12-11 18:24:27 +01:00
use crate::cli::{
load_cargo::{load_workspace_at, LoadCargoConfig},
print_memory_usage,
progress_report::ProgressReport,
report_metric, Result, Verbosity,
2020-07-15 12:14:51 +02:00
};
2020-08-12 16:32:36 +02:00
use profile::StopWatch;
/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
struct Snap<DB>(DB);
impl<DB: ParallelDatabase> Clone for Snap<salsa::Snapshot<DB>> {
fn clone(&self) -> Snap<salsa::Snapshot<DB>> {
Snap(self.0.snapshot())
}
}
pub struct AnalysisStatsCmd {
pub randomize: bool,
pub parallel: bool,
pub memory_usage: bool,
pub only: Option<String>,
pub with_deps: bool,
2021-03-14 18:31:14 +08:00
pub no_sysroot: bool,
pub path: PathBuf,
pub enable_build_scripts: bool,
pub enable_proc_macros: bool,
pub skip_inference: bool,
}
2020-07-29 19:49:10 +02:00
impl AnalysisStatsCmd {
pub fn run(self, verbosity: Verbosity) -> Result<()> {
let mut rng = {
let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
Rand32::new(seed)
};
let mut db_load_sw = self.stop_watch();
2021-03-14 18:31:14 +08:00
let mut cargo_config = CargoConfig::default();
cargo_config.no_sysroot = self.no_sysroot;
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: self.enable_build_scripts,
with_proc_macro: self.enable_proc_macros,
prefill_caches: false,
};
let (host, vfs, _proc_macro) =
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
let db = host.raw_database();
2021-01-11 22:17:30 +03:00
eprintln!("{:<20} {}", "Database loaded:", db_load_sw.elapsed());
let mut analysis_sw = self.stop_watch();
let mut num_crates = 0;
let mut visited_modules = FxHashSet::default();
let mut visit_queue = Vec::new();
let mut krates = Crate::all(db);
if self.randomize {
shuffle(&mut rng, &mut krates);
}
for krate in krates {
let module = krate.root_module(db);
let file_id = module.definition_source(db).file_id;
let file_id = file_id.original_file(db);
let source_root = db.file_source_root(file_id);
let source_root = db.source_root(source_root);
if !source_root.is_library || self.with_deps {
num_crates += 1;
visit_queue.push(module);
}
}
if self.randomize {
shuffle(&mut rng, &mut visit_queue);
}
2021-01-11 22:17:30 +03:00
eprint!(" crates: {}", num_crates);
let mut num_decls = 0;
let mut funcs = Vec::new();
while let Some(module) = visit_queue.pop() {
if visited_modules.insert(module) {
visit_queue.extend(module.children(db));
for decl in module.declarations(db) {
num_decls += 1;
if let ModuleDef::Function(f) = decl {
2019-06-03 10:01:10 -04:00
funcs.push(f);
}
}
for impl_def in module.impl_defs(db) {
for item in impl_def.items(db) {
num_decls += 1;
if let AssocItem::Function(f) = item {
funcs.push(f);
}
}
}
}
}
2021-01-11 22:17:30 +03:00
eprintln!(", mods: {}, decls: {}, fns: {}", visited_modules.len(), num_decls, funcs.len());
eprintln!("{:<20} {}", "Item Collection:", analysis_sw.elapsed());
if self.randomize {
shuffle(&mut rng, &mut funcs);
}
2019-09-12 11:45:33 +03:00
if !self.skip_inference {
self.run_inference(&host, db, &vfs, &funcs, verbosity);
}
let total_span = analysis_sw.elapsed();
eprintln!("{:<20} {}", "Total:", total_span);
report_metric("total time", total_span.time.as_millis() as u64, "ms");
if let Some(instructions) = total_span.instructions {
report_metric("total instructions", instructions, "#instr");
}
if let Some(memory) = total_span.memory {
report_metric("total memory", memory.allocated.megabytes() as u64, "MB");
}
if env::var("RA_COUNT").is_ok() {
eprintln!("{}", profile::countme::get_all());
}
if self.memory_usage && verbosity.is_verbose() {
print_memory_usage(host, vfs);
}
Ok(())
}
fn run_inference(
&self,
host: &AnalysisHost,
db: &RootDatabase,
vfs: &Vfs,
funcs: &[Function],
verbosity: Verbosity,
) {
let mut bar = match verbosity {
Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(),
_ if self.parallel => ProgressReport::hidden(),
_ => ProgressReport::new(funcs.len() as u64),
};
if self.parallel {
let mut inference_sw = self.stop_watch();
let snap = Snap(db.snapshot());
funcs
.par_iter()
.map_with(snap, |snap, &f| {
let f_id = FunctionId::from(f);
snap.0.body(f_id.into());
snap.0.infer(f_id.into());
})
.count();
2021-01-11 22:17:30 +03:00
eprintln!("{:<20} {}", "Parallel Inference:", inference_sw.elapsed());
}
let mut inference_sw = self.stop_watch();
bar.tick();
let mut num_exprs = 0;
let mut num_exprs_unknown = 0;
let mut num_exprs_partially_unknown = 0;
let mut num_type_mismatches = 0;
let analysis = host.analysis();
for f in funcs.iter().copied() {
let name = f.name(db);
let full_name = f
.module(db)
.path_to_root(db)
.into_iter()
.rev()
.filter_map(|it| it.name(db))
.chain(Some(f.name(db)))
.join("::");
if let Some(only_name) = self.only.as_deref() {
if name.to_string() != only_name && full_name != only_name {
continue;
}
}
let mut msg = format!("processing: {}", full_name);
if verbosity.is_verbose() {
if let Some(src) = f.source(db) {
let original_file = src.file_id.original_file(db);
let path = vfs.file_path(original_file);
let syntax_range = src.value.syntax().text_range();
format_to!(msg, " ({} {:?})", path, syntax_range);
}
}
if verbosity.is_spammy() {
bar.println(msg.to_string());
}
bar.set_message(&msg);
let f_id = FunctionId::from(f);
let (body, sm) = db.body_with_source_map(f_id.into());
let inference_result = db.infer(f_id.into());
let (previous_exprs, previous_unknown, previous_partially_unknown) =
(num_exprs, num_exprs_unknown, num_exprs_partially_unknown);
for (expr_id, _) in body.exprs.iter() {
let ty = &inference_result[expr_id];
num_exprs += 1;
if ty.is_unknown() {
num_exprs_unknown += 1;
if verbosity.is_spammy() {
if let Some((path, start, end)) =
expr_syntax_range(db, &analysis, vfs, &sm, expr_id)
{
bar.println(format!(
"{} {}:{}-{}:{}: Unknown type",
path,
start.line + 1,
start.col,
end.line + 1,
end.col,
));
} else {
bar.println(format!("{}: Unknown type", name,));
}
}
} else {
let mut is_partially_unknown = false;
ty.walk(&mut |ty| {
if ty.is_unknown() {
is_partially_unknown = true;
}
});
if is_partially_unknown {
num_exprs_partially_unknown += 1;
}
}
if self.only.is_some() && verbosity.is_spammy() {
// in super-verbose mode for just one function, we print every single expression
if let Some((_, start, end)) =
expr_syntax_range(db, &analysis, vfs, &sm, expr_id)
{
bar.println(format!(
"{}:{}-{}:{}: {}",
start.line + 1,
2021-02-12 21:24:10 +03:00
start.col,
end.line + 1,
2021-02-12 21:24:10 +03:00
end.col,
ty.display(db)
));
} else {
bar.println(format!("unknown location: {}", ty.display(db)));
}
}
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) {
num_type_mismatches += 1;
if verbosity.is_verbose() {
if let Some((path, start, end)) =
expr_syntax_range(db, &analysis, vfs, &sm, expr_id)
{
bar.println(format!(
"{} {}:{}-{}:{}: Expected {}, got {}",
path,
start.line + 1,
2021-02-12 21:24:10 +03:00
start.col,
end.line + 1,
2021-02-12 21:24:10 +03:00
end.col,
mismatch.expected.display(db),
mismatch.actual.display(db)
));
} else {
bar.println(format!(
"{}: Expected {}, got {}",
name,
mismatch.expected.display(db),
mismatch.actual.display(db)
));
}
}
}
}
if verbosity.is_spammy() {
bar.println(format!(
"In {}: {} exprs, {} unknown, {} partial",
full_name,
num_exprs - previous_exprs,
num_exprs_unknown - previous_unknown,
num_exprs_partially_unknown - previous_partially_unknown
));
}
bar.inc(1);
}
bar.finish_and_clear();
eprintln!(
2021-01-11 22:17:30 +03:00
" exprs: {}, ??ty: {} ({}%), ?ty: {} ({}%), !ty: {}",
num_exprs,
num_exprs_unknown,
2021-01-11 22:17:30 +03:00
percentage(num_exprs_unknown, num_exprs),
num_exprs_partially_unknown,
2021-01-11 22:17:30 +03:00
percentage(num_exprs_partially_unknown, num_exprs),
num_type_mismatches
);
2021-01-11 22:17:30 +03:00
report_metric("unknown type", num_exprs_unknown, "#");
report_metric("type mismatches", num_type_mismatches, "#");
2020-07-25 10:35:45 +02:00
2021-01-11 22:17:30 +03:00
eprintln!("{:<20} {}", "Inference:", inference_sw.elapsed());
2019-06-30 14:40:01 +03:00
}
fn stop_watch(&self) -> StopWatch {
StopWatch::start().memory(self.memory_usage)
}
}
2020-07-29 19:49:10 +02:00
fn expr_syntax_range(
db: &RootDatabase,
analysis: &Analysis,
vfs: &Vfs,
sm: &BodySourceMap,
expr_id: ExprId,
) -> Option<(VfsPath, LineCol, LineCol)> {
let src = sm.expr_syntax(expr_id);
if let Ok(src) = src {
// FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly
// But also, we should just turn the type mismatches into diagnostics and provide these
let root = db.parse_or_expand(src.file_id).unwrap();
let node = src.map(|e| e.to_node(&root).syntax().clone());
let original_range = node.as_ref().original_file_range(db);
let path = vfs.file_path(original_range.file_id);
let line_index = analysis.file_line_index(original_range.file_id).unwrap();
let text_range = original_range.range;
let (start, end) =
(line_index.line_col(text_range.start()), line_index.line_col(text_range.end()));
Some((path, start, end))
} else {
None
}
}
2020-07-29 19:49:10 +02:00
fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {
2020-07-30 15:40:15 +02:00
for i in 0..slice.len() {
randomize_first(rng, &mut slice[i..]);
}
fn randomize_first<T>(rng: &mut Rand32, slice: &mut [T]) {
assert!(!slice.is_empty());
let idx = rng.rand_range(0..slice.len() as u32) as usize;
slice.swap(0, idx);
2020-07-29 19:49:10 +02:00
}
}
2021-01-11 22:17:30 +03:00
fn percentage(n: u64, total: u64) -> u64 {
(n * 100).checked_div(total).unwrap_or(100)
}