Skip to content

Commit

Permalink
csv-format: dominators
Browse files Browse the repository at this point in the history
  • Loading branch information
csmoe committed Apr 26, 2018
1 parent 9a70888 commit cbf7474
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
46 changes: 44 additions & 2 deletions analyze/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,50 @@ impl traits::Emit for DominatorTree {
recursive_add_children(items, &self.opts, &self.tree, items.meta_root(), &mut obj)
}

fn emit_csv(&self, _items: &ir::Items, _dest: &mut io::Write) -> Result<(), traits::Error> {
unimplemented!();
fn emit_csv(&self, items: &ir::Items, dest: &mut io::Write) -> Result<(), traits::Error> {
let mut wtr = csv_sys::Writer::from_writer(dest);
fn recursive_add_children<'a>(
items: &ir::Items,
opts: &opt::Dominators,
dominator_tree: &BTreeMap<ir::Id, Vec<ir::Id>>,
id: ir::Id,
wtr: &mut csv_sys::Writer<&mut io::Write>
) -> Result<(), traits::Error> {
let item = &items[id];
let (size, size_percent) = (
item.size(),
(item.size() as f64) / (items.size() as f64) * 100.0,
);
let (retained_size, retained_size_percent) = (
items.retained_size(id),
(items.retained_size(id) as f64) / (items.size() as f64) * 100.0,
);

let rc = csv::CsvRecord {
id: Some(item.id().0),
name: item.name().to_string(),
shallow_size: size,
shallow_size_percent: size_percent,
retained_size: Some(retained_size),
retained_size_percent: Some(retained_size_percent),
immediate_dominator: Some(id.0),
..Default::default()
};

wtr.serialize(rc)?;
wtr.flush()?;
if let Some(children) = dominator_tree.get(&id) {
let mut children: Vec<_> = children.iter().cloned().collect();
children.sort_by(|a, b| items.retained_size(*b).cmp(&items.retained_size(*a)));
for child in children {
recursive_add_children(items, opts, dominator_tree, child, wtr)?;
}
}
Ok(())
}

recursive_add_children(items, &self.opts, &self.tree, items.meta_root(), &mut wtr)?;
Ok(())
}
}

Expand Down
6 changes: 3 additions & 3 deletions analyze/csv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ir::Id;

#[derive(Debug, Default, Serialize)]
pub struct CsvRecord {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<u32>,
pub name: String,
pub shallow_size: u32,
pub shallow_size_percent: f64,
Expand All @@ -10,7 +10,7 @@ pub struct CsvRecord {
#[serde(skip_serializing_if = "Option::is_none")]
pub retained_size_percent: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub next_dominator: Option<Id>,
pub immediate_dominator: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<String>
}
5 changes: 2 additions & 3 deletions ir/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extern crate cpp_demangle;
extern crate frozen;
extern crate petgraph;
extern crate rustc_demangle;
#[macro_use] extern crate serde_derive;

mod graph_impl;

Expand Down Expand Up @@ -358,8 +357,8 @@ impl<'a> Iterator for Iter<'a> {
}

/// An item's unique identifier.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
pub struct Id(u32, u32);
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(pub u32, u32);

impl Id {
/// Create an `Id` for a the given section.
Expand Down
19 changes: 19 additions & 0 deletions twiggy/tests/expectations/dominators_wee_alloc_csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
id,name,shallow_size,shallow_size_percent,retained_size,retained_size_percent,immediate_dominator
4294967295,<meta root>,0,0,1417,50.30173943911963,4294967295
8,"""function names"" subsection",774,27.47603833865815,774,27.47603833865815,8
4,"export ""hello""",8,0.2839900603478878,573,20.340788072417464,4
1,func[8],1,0.03549875754348598,565,20.056798012069578,1
6,hello,164,5.8217962371317,560,19.879304224352147,6
1,func[2],1,0.03549875754348598,387,13.738019169329075,1
6,wee_alloc::alloc_with_refill::hb32c1bbce9ebda8e,152,5.395811146609868,378,13.418530351437699,6
1,func[3],1,0.03549875754348598,226,8.022719204827832,1
6,wee_alloc::alloc_first_fit::h9a72de3af77ef93f,225,7.987220447284344,225,7.987220447284344,6
0,type[4],8,0.2839900603478878,8,0.2839900603478878,0
7,data[0],9,0.3194888178913738,9,0.3194888178913738,7
0,type[5],4,0.1419950301739439,4,0.1419950301739439,0
4,"export ""goodbye""",10,0.3549875754348598,59,2.0944266950656725,4
1,func[9],1,0.03549875754348598,49,1.7394391196308128,1
6,goodbye,44,1.5619453319133831,44,1.5619453319133831,6
0,type[3],4,0.1419950301739439,4,0.1419950301739439,0
4,"export ""memory""",9,0.3194888178913738,11,0.3904863329783458,4
3,memory[0],2,0.07099751508697195,2,0.07099751508697195,3
10 changes: 5 additions & 5 deletions twiggy/tests/expectations/top_2_csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name,shallow_size,shallow_size_percent,retained_size,retained_size_percent
data[3],1034,36.7057152999645,,
"""function names"" subsection",774,27.47603833865815,,
wee_alloc::alloc_first_fit::h9a72de3af77ef93f,225,7.987220447284344,,
hello,164,5.8217962371317,,
id,name,shallow_size,shallow_size_percent
0,data[3],1034,36.7057152999645
0,"""function names"" subsection",774,27.47603833865815
0,wee_alloc::alloc_first_fit::h9a72de3af77ef93f,225,7.987220447284344
0,hello,164,5.8217962371317
8 changes: 8 additions & 0 deletions twiggy/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ test!(
"./fixtures/wee_alloc.wasm"
);

test!(
dominators_wee_alloc_csv,
"dominators",
"./fixtures/wee_alloc.wasm",
"-f",
"csv"
);

test!(
dominators_wee_alloc_with_depth_and_row,
"dominators",
Expand Down

0 comments on commit cbf7474

Please sign in to comment.