Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Src code analyze frame #43

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions collector/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ impl Stats {
pub fn insert(&mut self, stat: String, value: f64) {
self.stats.insert(stat, value);
}

pub fn add_or_insert(&mut self, stat: String, value: f64) {
match self.stats.get_mut(&stat) {
Some(e) => *e += value,
None => self.insert(stat, value),
}
}
}

impl Add for Stats {
Expand Down
18 changes: 17 additions & 1 deletion collector/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
process::{self, Command},
};

use anyhow::{Context, Ok};
use anyhow::{bail, Context, Ok};
use benchmark::scenario::Scenario;
use clap::Parser;
use compile_time::{
Expand All @@ -18,6 +18,7 @@ use compile_time::{
};
use mir_analyze::mir_generate::generate_mir;
use runtime::bench_runtime;
use src_code_analyze::entry::src_code_analyze;
use toolchain::{Cli, Commands, ResultWriter};

use crate::{
Expand All @@ -35,6 +36,7 @@ mod morpheme_miner;
mod pca_analysis;
mod perf_analyze;
mod runtime;
mod src_code_analyze;
mod statistics;
mod toolchain;
mod utils;
Expand Down Expand Up @@ -307,6 +309,20 @@ fn main_result() -> anyhow::Result<i32> {

Ok(0)
}
Commands::SourceCodeAnalyze {
bench_dir,
dependency_dir,
out_path,
} => match src_code_analyze(bench_dir, dependency_dir, out_path) {
anyhow::Result::Ok(p) => {
println!(
"Write analyze result to {}.",
p.as_os_str().to_str().unwrap()
);
Ok(0)
}
Err(e) => bail!("{}", e),
},
}
}

Expand Down
70 changes: 70 additions & 0 deletions collector/src/src_code_analyze/analyzer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::PathBuf;

use crate::execute::Stats;

use crate::{
benchmark::{benchmark::Benchamrk, profile::Profile, scenario::Scenario},
statistics::compile_time_stat::CompileTimeResult,
};

use super::dependancy::read_dependencies;

pub fn analyze_benchmark(
benchmark: &Benchamrk,
dependency_dir: &PathBuf,
ops: &Vec<Box<dyn Fn(&String) -> (String, f64)>>,
) -> CompileTimeResult {
eprintln!(
"analyzing benchmark {} {}",
benchmark.name,
benchmark.path.to_str().unwrap()
);
let stats = analyze_dir(&benchmark.path, dependency_dir, ops, &mut HashSet::new()).unwrap();
CompileTimeResult::new(
benchmark.name.clone(),
0,
Profile::Check,
Scenario::Full,
stats,
)
}

fn analyze_dir(
p: &PathBuf,
dependency_dir: &PathBuf,
ops: &Vec<Box<dyn Fn(&String) -> (String, f64)>>,
analyzed_dependency: &mut HashSet<PathBuf>,
) -> anyhow::Result<Stats> {
let mut stats = Stats::new();
for entry in p.read_dir()? {
let entry = entry?;
if entry.file_type()?.is_dir() {
stats += analyze_dir(&entry.path(), dependency_dir, ops, analyzed_dependency)?;
} else if entry.file_type()?.is_file() {
if entry.file_name().to_str().unwrap().ends_with(".rs") {
let mut reader = BufReader::new(File::open(entry.path())?);
let mut buf = vec![];
reader.read_to_end(&mut buf)?;
if let Ok(buf) = String::from_utf8(buf) {
ops.iter().for_each(|op| {
let t = op(&buf);
stats.add_or_insert(t.0, t.1)
});
}
} else if entry.file_name().to_str().unwrap().eq("Cargo.lock") {
for d in read_dependencies(&entry.path())? {
let path = &d.path(dependency_dir);
if path.exists() && !analyzed_dependency.contains(path) {
println!(" |---analyzing {}", d);
analyzed_dependency.insert(path.clone());
stats += analyze_dir(path, dependency_dir, ops, analyzed_dependency)?;
}
}
}
}
}
Ok(stats)
}
Loading
Loading