Skip to content

Commit 2b658e2

Browse files
robertoaloimeta-codesync[bot]
authored andcommitted
Extend memory analysis to lint command
Summary: Move the `print_memory_usage` function to the `reporting` module and use it from the `lint` command. This represents the first step towards a generic argument that can be used by various commands and will be merged with similar options existing for other commands. Reviewed By: alanz Differential Revision: D84347271 fbshipit-source-id: 3c19b87e3c48bc8e790e1a96eb40014deddd87ac
1 parent 747af17 commit 2b658e2

File tree

5 files changed

+50
-24
lines changed

5 files changed

+50
-24
lines changed

crates/elp/src/bin/args.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ pub struct Lint {
323323
/// than one at a time.
324324
pub one_shot: bool,
325325

326+
/// Report system memory usage and other statistics
327+
#[bpaf(long("report-system-stats"))]
328+
pub report_system_stats: bool,
329+
326330
/// Rest of args are space separated list of apps to ignore
327331
#[bpaf(positional("IGNORED_APPS"))]
328332
pub ignore_apps: Vec<String>,

crates/elp/src/bin/elp_parse_cli.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use elp::otp_file_to_ignore;
2626
use elp::server::file_id_to_url;
2727
use elp_eqwalizer::Mode;
2828
use elp_ide::Analysis;
29-
use elp_ide::AnalysisHost;
3029
use elp_ide::diagnostics;
3130
use elp_ide::diagnostics::DiagnosticsConfig;
3231
use elp_ide::diagnostics::LabeledDiagnostics;
@@ -55,6 +54,7 @@ use vfs::AbsPath;
5554

5655
use crate::args::ParseAllElp;
5756
use crate::reporting;
57+
use crate::reporting::print_memory_usage;
5858

5959
#[derive(Debug)]
6060
struct ParseResult {
@@ -63,27 +63,6 @@ struct ParseResult {
6363
diagnostics: DiagnosticCollection,
6464
}
6565

66-
fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs, cli: &mut dyn Cli) -> Result<()> {
67-
let mem = host.per_query_memory_usage();
68-
69-
let before = profile::memory_usage();
70-
drop(vfs);
71-
let vfs = before.allocated - profile::memory_usage().allocated;
72-
73-
let before = profile::memory_usage();
74-
drop(host);
75-
let unaccounted = before.allocated - profile::memory_usage().allocated;
76-
let remaining = profile::memory_usage().allocated;
77-
78-
for (name, bytes, entries) in mem {
79-
writeln!(cli, "{bytes:>8} {entries:>6} {name}")?;
80-
}
81-
writeln!(cli, "{vfs:>8} VFS")?;
82-
writeln!(cli, "{unaccounted:>8} Unaccounted")?;
83-
writeln!(cli, "{remaining:>8} Remaining")?;
84-
Ok(())
85-
}
86-
8766
pub fn parse_all(
8867
args: &ParseAllElp,
8968
cli: &mut dyn Cli,

crates/elp/src/bin/lint_cli.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use elp::build::load;
2323
use elp::build::types::LoadResult;
2424
use elp::cli::Cli;
2525
use elp::convert;
26+
use elp::memory_usage::MemoryUsage;
2627
use elp::otp_file_to_ignore;
2728
use elp::read_lint_config_file;
2829
use elp_eqwalizer::Mode;
@@ -67,12 +68,15 @@ use rayon::prelude::ParallelIterator;
6768

6869
use crate::args::Lint;
6970
use crate::reporting;
71+
use crate::reporting::print_memory_usage;
7072

7173
pub fn run_lint_command(
7274
args: &Lint,
7375
cli: &mut dyn Cli,
7476
query_config: &BuckQueryConfig,
7577
) -> Result<()> {
78+
let memory_start = MemoryUsage::now();
79+
7680
if let Some(to) = &args.to {
7781
fs::create_dir_all(to)?
7882
};
@@ -83,7 +87,18 @@ pub fn run_lint_command(
8387
// errors. No point wasting time if the config is wrong.
8488
let mut loaded = load_project(args, cli, query_config)?;
8589

86-
do_codemod(cli, &mut loaded, &diagnostics_config, args)
90+
let result = do_codemod(cli, &mut loaded, &diagnostics_config, args);
91+
92+
let memory_end = MemoryUsage::now();
93+
let memory_used = memory_end - memory_start;
94+
95+
// Print memory usage at the end if requested and format is normal
96+
if args.is_format_normal() && args.report_system_stats {
97+
print_memory_usage(loaded.analysis_host, loaded.vfs, cli)?;
98+
writeln!(cli, "{}", memory_used)?;
99+
}
100+
101+
result
87102
}
88103

89104
fn get_and_report_diagnostics_config(args: &Lint, cli: &mut dyn Cli) -> Result<DiagnosticsConfig> {

crates/elp/src/bin/reporting.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use elp::cli::Cli;
2929
use elp::convert;
3030
use elp::memory_usage::MemoryUsage;
3131
use elp_ide::Analysis;
32+
use elp_ide::AnalysisHost;
3233
use elp_ide::TextRange;
3334
use elp_ide::elp_ide_db::EqwalizerDiagnostic;
3435
use elp_ide::elp_ide_db::elp_base_db::AbsPath;
@@ -38,6 +39,7 @@ use indicatif::ProgressBar;
3839
use itertools::Itertools;
3940
use lazy_static::lazy_static;
4041
use parking_lot::Mutex;
42+
use vfs::Vfs;
4143

4244
pub trait Reporter {
4345
fn write_eqwalizer_diagnostics(
@@ -372,3 +374,28 @@ pub(crate) fn add_stat(stat: String) {
372374
let mut stats = STATS.lock();
373375
stats.push(stat);
374376
}
377+
378+
pub(crate) fn print_memory_usage(
379+
mut host: AnalysisHost,
380+
vfs: Vfs,
381+
cli: &mut dyn Cli,
382+
) -> Result<()> {
383+
let mem = host.per_query_memory_usage();
384+
385+
let before = profile::memory_usage();
386+
drop(vfs);
387+
let vfs = before.allocated - profile::memory_usage().allocated;
388+
389+
let before = profile::memory_usage();
390+
drop(host);
391+
let unaccounted = before.allocated - profile::memory_usage().allocated;
392+
let remaining = profile::memory_usage().allocated;
393+
394+
for (name, bytes, entries) in mem {
395+
writeln!(cli, "{bytes:>8} {entries:>6} {name}")?;
396+
}
397+
writeln!(cli, "{vfs:>8} VFS")?;
398+
writeln!(cli, "{unaccounted:>8} Unaccounted")?;
399+
writeln!(cli, "{remaining:>8} Remaining")?;
400+
Ok(())
401+
}

crates/elp/src/resources/test/lint_help.stdout

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Usage: [--project PROJECT] [--module MODULE] [--app APP] [--file FILE] [--rebar] [--as PROFILE] [--include-generated] [--include-tests] [--no-diags] [[--format FORMAT]] [--prefix ARG] [--include-erlc-diagnostics] [--include-ct-diagnostics] [--include-edoc-diagnostics] [--include-eqwalizer-diagnostics] [--include-suppressed] [--use-cli-severity] [--diagnostic-ignore CODE] [--diagnostic-filter CODE] [--experimental] [--read-config] [--config-file CONFIG_FILE] [--apply-fix] [--ignore-fix-only] [--in-place] [--to TO] [--recursive] [--with-check] [--check-eqwalize-all] [--one-shot] <IGNORED_APPS>...
1+
Usage: [--project PROJECT] [--module MODULE] [--app APP] [--file FILE] [--rebar] [--as PROFILE] [--include-generated] [--include-tests] [--no-diags] [[--format FORMAT]] [--prefix ARG] [--include-erlc-diagnostics] [--include-ct-diagnostics] [--include-edoc-diagnostics] [--include-eqwalizer-diagnostics] [--include-suppressed] [--use-cli-severity] [--diagnostic-ignore CODE] [--diagnostic-filter CODE] [--experimental] [--read-config] [--config-file CONFIG_FILE] [--apply-fix] [--ignore-fix-only] [--in-place] [--to TO] [--recursive] [--with-check] [--check-eqwalize-all] [--one-shot] [--report-system-stats] <IGNORED_APPS>...
22

33
Available positional items:
44
<IGNORED_APPS> Rest of args are space separated list of apps to ignore
@@ -37,4 +37,5 @@ Available options:
3737
--check-eqwalize-all After applying a fix step, check that all eqwalizer project diagnostics are clear, else roll back
3838
--one-shot Apply to all matching diagnostic occurrences at once, rather
3939
than one at a time.
40+
--report-system-stats Report system memory usage and other statistics
4041
-h, --help Prints help information

0 commit comments

Comments
 (0)