Skip to content

Commit

Permalink
Add --no-backtrace flag to attempt to control peak memory
Browse files Browse the repository at this point in the history
Issue #20
  • Loading branch information
davidlattimore committed Aug 3, 2024
1 parent 2254edb commit 2c0c7d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,9 @@ impl Checker {
crate::symbol_graph::scan_objects(paths, link_info, self)?;
graph_outputs.apis = self.config.raw.apis.clone();
check_state.graph_outputs = Some(graph_outputs);
self.backtracers
.insert(link_info.output_file.clone(), backtracer);
if let Some(b) = backtracer {
self.backtracers.insert(link_info.output_file.clone(), b);
}
}
let graph_outputs = check_state.graph_outputs.as_ref().unwrap();
let problems = graph_outputs.problems(self)?;
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ struct Args {
#[clap(long, short)]
no_ui: bool,

/// Disable backtraces (may reduce peak memory consumption).
#[clap(long)]
no_backtrace: bool,

#[command(subcommand)]
command: Option<Command>,
}
Expand Down
19 changes: 12 additions & 7 deletions src/symbol_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ enum Filetype {

struct ApiUsageCollector<'input, 'backtracer> {
outputs: ScanOutputs,
backtracer: &'backtracer mut Backtracer,
backtracer: Option<&'backtracer mut Backtracer>,

bin: BinInfo<'input>,
debug_enabled: bool,
Expand Down Expand Up @@ -132,25 +132,28 @@ pub(crate) fn scan_objects(
paths: &[PathBuf],
link_info: &LinkInfo,
checker: &mut Checker,
) -> Result<(ScanOutputs, Backtracer)> {
) -> Result<(ScanOutputs, Option<Backtracer>)> {
log::info!("Scanning {}", link_info.output_file.display());
let start = Instant::now();
let file_bytes = std::fs::read(&link_info.output_file)
.with_context(|| format!("Failed to read `{}`", link_info.output_file.display()))?;
checker.timings.add_timing(start, "Read bin file");

let mut backtracer = Backtracer::new(checker.sysroot.clone());
let backtraces = !checker.args.no_backtrace;
let mut backtracer = backtraces.then(|| Backtracer::new(checker.sysroot.clone()));
let outputs =
scan_object_with_bin_bytes(&file_bytes, checker, &mut backtracer, link_info, paths)?;
scan_object_with_bin_bytes(&file_bytes, checker, backtracer.as_mut(), link_info, paths)?;

backtracer.provide_bin_bytes(file_bytes);
if let Some(b) = backtracer.as_mut() {
b.provide_bin_bytes(file_bytes);
}
Ok((outputs, backtracer))
}

fn scan_object_with_bin_bytes(
bin_file_bytes: &Vec<u8>,
checker: &mut Checker,
backtracer: &mut Backtracer,
backtracer: Option<&mut Backtracer>,
link_info: &LinkInfo,
paths: &[PathBuf],
) -> Result<ScanOutputs> {
Expand Down Expand Up @@ -365,7 +368,9 @@ impl<'input, 'backtracer> ApiUsageCollector<'input, 'backtracer> {
}
for target_symbol in target_symbols {
if let Some(target_address) = self.bin.symbol_addresses.get(&target_symbol) {
self.backtracer.add_reference(bin_location, *target_address);
if let Some(b) = self.backtracer.as_mut() {
b.add_reference(bin_location, *target_address);
}
}
let target = self.bin.get_symbol_and_name(&target_symbol);
self.process_reference(
Expand Down

0 comments on commit 2c0c7d5

Please sign in to comment.