Skip to content

Commit e3bc95f

Browse files
191220029191220029
andauthored
data-manage: impl MergeTableMetrics & MergeCompileTimeStatsToTableBase; Mir-analyze: Fix div 0 error in oop_pattern (#40)
* Mir-analyze: reorgnize mir generate Signed-off-by: 191220029 <[email protected]> MirGenerate: take cargo opts when needed Signed-off-by: 191220029 <[email protected]> impl mir_analyze Signed-off-by: 191220029 <[email protected]> * Mir-analyze: Fix div 0 error in oop_pattern Signed-off-by: 191220029 <[email protected]> * data-manage: impl MergeTableMetrics & MergeCompileTimeStatsToTable Signed-off-by: 191220029 <[email protected]> --------- Signed-off-by: 191220029 <[email protected]> Co-authored-by: 191220029 <[email protected]>
1 parent fff9bdc commit e3bc95f

File tree

10 files changed

+5819
-6
lines changed

10 files changed

+5819
-6
lines changed

collector/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub mod benchmark;
22
mod compile_time;
33
mod csv_transfer;
44
mod execute;
5-
mod mir_analyze;
5+
pub mod mir_analyze;
66
mod morpheme_miner;
77
mod pca_analysis;
88
mod perf_analyze;

collector/src/mir_analyze/data/tex_writer.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use std::{
2-
collections::HashMap,
32
fmt::Display,
43
fs::File,
54
io::{BufWriter, Write},
65
path::Path,
76
};
87

9-
// type TableData<U, V> = HashMap<U, Vec<V>>;
10-
11-
pub type TableDatas<X, Y, T> = HashMap<X, HashMap<Y, T>>;
8+
use super::table_data::TableDatas;
129

1310
/// Transform 2-D data into a 2-D tex table.
1411
pub fn write_tex_table<X: Display + Ord, Y: Display + Ord, T: Display + Default>(

collector/src/mir_analyze/mir/oop_pattern.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ pub fn dfc(mir_file: &MIRs) -> i32 {
187187
let depths_count = depths.len();
188188
let depths_sum: usize = depths.values().sum();
189189

190+
if funcname_call_count == 0 {
191+
return 0;
192+
}
193+
190194
let result = funcname_call_count - depths_count + depths_sum / funcname_call_count;
191195
// println!("Result: {}", result);
192196
result as i32

collector/src/mir_analyze/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub(crate) mod data;
1+
pub mod data;
22
mod mir;
33
pub(crate) mod mir_analyze;
44
pub(crate) mod mir_generate;

data_manage/src/commannds.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,36 @@ pub enum Commands {
8888
#[clap(long = "out-path", default_value = "results")]
8989
out_path: PathBuf,
9090
},
91+
92+
/// Merge several metrics into a new metric of a table data fmt file.
93+
MergeTableMetrics {
94+
/// The path of table data fmt file.
95+
#[clap(long = "table-data")]
96+
table_data_path: PathBuf,
97+
/// The path of output file.
98+
#[clap(long = "out-path")]
99+
out_path: PathBuf,
100+
/// Metrics need to be merged.
101+
#[clap(long = "old-metrics")]
102+
old_metrics: Vec<String>,
103+
/// New metric merged from old-metrics.
104+
#[clap(long = "merged-metric")]
105+
merged_metric: String,
106+
},
107+
108+
/// Merge compile-time stats into a table data fmt file.
109+
MergeCompileTimeStatsToTable {
110+
/// The path of table data fmt file.
111+
#[clap(long = "table-data")]
112+
table_data_path: PathBuf,
113+
/// The path of compile-time stats fmt file.
114+
#[clap(long = "stats")]
115+
stats_path: PathBuf,
116+
/// The path of output file.
117+
#[clap(long = "out-path")]
118+
out_path: PathBuf,
119+
/// Metrics merged from stats fmt file.
120+
#[clap(long = "new_metrics")]
121+
new_metrics: Vec<String>,
122+
},
91123
}

data_manage/src/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ use clap::Parser;
22
use commannds::Cli;
33
use compare_stats::{compare_stat::compare_stat, compare_stat_2d::compare_stat_2d};
44
use merge_stats::{merge_runtime_stat::merge_runtime_stats, merge_stat::merge_compile_time_stats};
5+
use table_data::merge_metrics::{
6+
merge_metrics_from_compile_time_stats_to_table_data, merge_metrics_on_table_data,
7+
};
58

69
mod commannds;
710
mod compare_stats;
811
mod merge_stats;
12+
mod table_data;
913

1014
fn main() {
1115
let cli = Cli::parse();
@@ -47,5 +51,33 @@ fn main() {
4751
Ok(p) => println!("Write merged stats to {}", p.to_str().unwrap()),
4852
Err(e) => eprintln!("{}", e),
4953
},
54+
commannds::Commands::MergeTableMetrics {
55+
table_data_path,
56+
out_path,
57+
old_metrics,
58+
merged_metric,
59+
} => match merge_metrics_on_table_data(
60+
&table_data_path,
61+
&out_path,
62+
&old_metrics,
63+
&merged_metric,
64+
) {
65+
Ok(p) => println!("Write merged table data to {}", p.to_str().unwrap()),
66+
Err(e) => eprintln!("{}", e),
67+
},
68+
commannds::Commands::MergeCompileTimeStatsToTable {
69+
table_data_path,
70+
stats_path,
71+
out_path,
72+
new_metrics,
73+
} => match merge_metrics_from_compile_time_stats_to_table_data(
74+
&table_data_path,
75+
&stats_path,
76+
out_path.as_path(),
77+
new_metrics,
78+
) {
79+
Ok(p) => println!("Write merged table data to {}", p.to_str().unwrap()),
80+
Err(e) => eprintln!("{}", e),
81+
},
5082
}
5183
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
use std::{
2+
collections::HashMap,
3+
fs::File,
4+
io::{BufReader, BufWriter},
5+
path::{Path, PathBuf},
6+
};
7+
8+
use collector::{
9+
mir_analyze::data::table_data::TableDatas, statistics::compile_time_stat::CompileTimeStatistics,
10+
};
11+
12+
pub fn merge_metrics_on_table_data(
13+
table_data_path: &Path,
14+
out_path: &Path,
15+
old_metrics: &Vec<String>,
16+
merged_metric: &String,
17+
) -> anyhow::Result<PathBuf> {
18+
serde_json::to_writer(
19+
BufWriter::new(File::create(out_path)?),
20+
&merge_metrics(
21+
serde_json::from_reader(BufReader::new(File::open(table_data_path)?))?,
22+
old_metrics,
23+
merged_metric,
24+
),
25+
)?;
26+
27+
Ok(out_path.to_path_buf())
28+
}
29+
30+
pub fn merge_metrics_from_compile_time_stats_to_table_data(
31+
table_data_path: &Path,
32+
stats_path: &Path,
33+
out_path: &Path,
34+
new_metrics: Vec<String>,
35+
) -> anyhow::Result<PathBuf> {
36+
serde_json::to_writer(
37+
BufWriter::new(File::create(out_path)?),
38+
&merge_metrics_from_compile_time_stats(
39+
serde_json::from_reader(BufReader::new(File::open(table_data_path)?))?,
40+
serde_json::from_reader(BufReader::new(File::open(stats_path)?))?,
41+
new_metrics,
42+
),
43+
)?;
44+
45+
Ok(out_path.to_path_buf())
46+
}
47+
48+
fn merge_metrics(
49+
data: TableDatas<String, String, f64>,
50+
old_metrics: &Vec<String>,
51+
merged_metric: &String,
52+
) -> TableDatas<String, String, f64> {
53+
let mut merged_metrics = TableDatas::new();
54+
55+
data.into_iter().for_each(|(m, d)| {
56+
if old_metrics.contains(&m) {
57+
match merged_metrics.get_mut(merged_metric) {
58+
Some(v) => {
59+
v.iter_mut().for_each(|(k, v)| {
60+
*v += d.get(k).unwrap();
61+
});
62+
}
63+
None => {
64+
merged_metrics.insert(merged_metric.clone(), d);
65+
}
66+
}
67+
} else {
68+
merged_metrics.insert(m, d);
69+
}
70+
});
71+
merged_metrics
72+
}
73+
74+
fn merge_metrics_from_compile_time_stats(
75+
mut data: TableDatas<String, String, f64>,
76+
stats: CompileTimeStatistics,
77+
new_metrics: Vec<String>,
78+
) -> TableDatas<String, String, f64> {
79+
let stats: HashMap<String, HashMap<String, _>> = stats
80+
.into_iter()
81+
.map(|s| {
82+
(
83+
s.name,
84+
s.statistic_vec
85+
.into_iter()
86+
.map(|(name, stats)| (name, stats))
87+
.collect(),
88+
)
89+
})
90+
.collect();
91+
92+
new_metrics.into_iter().for_each(|m| {
93+
data.insert(
94+
m.clone(),
95+
stats
96+
.iter()
97+
.map(|(b, stats_map)| (b.clone(), stats_map.get(&m).unwrap().geometric_mean))
98+
.collect(),
99+
);
100+
});
101+
102+
data
103+
}
104+
105+
#[cfg(test)]
106+
mod test_merge_metrics {
107+
use std::{
108+
fs::{remove_file, File},
109+
io::BufReader,
110+
path::PathBuf,
111+
};
112+
113+
use collector::mir_analyze::data::table_data::TableDatas;
114+
115+
use crate::table_data::merge_metrics::merge_metrics_from_compile_time_stats_to_table_data;
116+
117+
use super::merge_metrics_on_table_data;
118+
119+
#[test]
120+
fn test_merge_metrics_on_table_data() {
121+
let table_data_path = PathBuf::from("test/table_data/data/mir-analysis.json");
122+
let out_data_path = PathBuf::from("test/table_data/merged_metrics.json");
123+
let old_metrics = vec![
124+
String::from("oop_noc"),
125+
String::from("oop_pbf"),
126+
String::from("oop_dfc"),
127+
];
128+
let merged_metric = String::from("oop_pattern");
129+
130+
assert_eq!(
131+
merge_metrics_on_table_data(
132+
table_data_path.as_path(),
133+
out_data_path.as_path(),
134+
&old_metrics,
135+
&merged_metric
136+
)
137+
.unwrap(),
138+
out_data_path
139+
);
140+
141+
let merged_data: TableDatas<String, String, f64> =
142+
serde_json::from_reader(BufReader::new(File::open(&out_data_path).unwrap())).unwrap();
143+
assert!(merged_data.contains_key(&merged_metric));
144+
old_metrics.iter().for_each(|m| {
145+
assert!(!merged_data.contains_key(m));
146+
});
147+
148+
remove_file(out_data_path).unwrap();
149+
}
150+
151+
#[test]
152+
fn test_merge_metrics_from_compile_time_stats_to_table_data() {
153+
let table_data_path = PathBuf::from("test/table_data/data/mir-analysis.json");
154+
let stats_path = PathBuf::from("test/table_data/data/merged_statstics_current.json");
155+
let out_data_path = PathBuf::from("test/table_data/merged_stats_metrics.json");
156+
let new_metrics = vec![
157+
String::from("instructions:u"),
158+
String::from("branch-misses"),
159+
String::from("cache-misses"),
160+
];
161+
162+
assert_eq!(
163+
merge_metrics_from_compile_time_stats_to_table_data(
164+
table_data_path.as_path(),
165+
stats_path.as_path(),
166+
out_data_path.as_path(),
167+
new_metrics.clone()
168+
)
169+
.unwrap(),
170+
out_data_path
171+
);
172+
173+
let merged_data: TableDatas<String, String, f64> =
174+
serde_json::from_reader(BufReader::new(File::open(&out_data_path).unwrap())).unwrap();
175+
new_metrics.iter().for_each(|m| {
176+
assert!(merged_data.contains_key(m));
177+
});
178+
179+
remove_file(out_data_path).unwrap();
180+
}
181+
}

data_manage/src/table_data/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod merge_metrics;

0 commit comments

Comments
 (0)