Skip to content

Commit

Permalink
draw dag
Browse files Browse the repository at this point in the history
  • Loading branch information
AshinGau committed Dec 5, 2024
1 parent 7c2a9e9 commit 23b4cb7
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/tx_dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::{
cmp::{max, min, Reverse},
collections::{BTreeMap, BTreeSet, BinaryHeap, VecDeque},
};
use std::fs::File;
use std::io::{BufWriter, Write};

pub(crate) type DependentTxsVec = SmallVec<[TxId; 1]>;

Expand Down Expand Up @@ -35,6 +37,7 @@ pub(crate) struct TxDependency {

pub round: Option<usize>,
pub block_height: u64,
tx_states: SharedTxStates,
}

impl TxDependency {
Expand All @@ -46,6 +49,7 @@ impl TxDependency {
tx_weight: None,
round: None,
block_height: 0,
tx_states: Default::default(),
}
}

Expand All @@ -64,6 +68,7 @@ impl TxDependency {
last_write_tx.insert(location.clone(), txid);
}
}
self.tx_states = tx_states;
}

/// Retrieve the optimal partitions based on the dependency relationships between transactions.
Expand Down Expand Up @@ -224,12 +229,14 @@ impl TxDependency {
counter!("grevm.total_block_cnt").increment(1);
}
let mut subgraph = BTreeSet::new();
let mut two = false;
if let Some((_, groups)) = weighted_group.last_key_value() {
if self.round.is_none() {
let largest_ratio = groups[0].len() as f64 / num_remaining as f64;
histogram!("grevm.large_graph_ratio").record(largest_ratio);
if groups[0].len() >= num_remaining / 2 {
counter!("grevm.low_parallelism_cnt").increment(1);
two = true;
}
}
if groups[0].len() >= num_remaining / 3 {
Expand All @@ -240,16 +247,19 @@ impl TxDependency {
return;
}

let mut longest_chain: Vec<TxId> = vec![];
// ChainLength -> ChainNumber
let mut chains = BTreeMap::new();
let mut visited = HashSet::new();
for txid in subgraph.iter().rev() {
if !visited.contains(txid) {
let mut txid = *txid;
let mut chain_len = 0;
let mut curr_chain = vec![];
while !visited.contains(&txid) {
chain_len += 1;
visited.insert(txid);
curr_chain.push(txid);
let dep: BTreeSet<TxId> =
self.tx_dependency[txid - num_finality_txs].clone().into_iter().collect();
for dep_id in dep.into_iter().rev() {
Expand All @@ -259,6 +269,9 @@ impl TxDependency {
}
}
}
if curr_chain.len() > longest_chain.len() {
longest_chain = curr_chain;
}
let chain_num = chains.get(&chain_len).cloned().unwrap_or(0) + 1;
chains.insert(chain_len, chain_num);
}
Expand All @@ -282,6 +295,47 @@ impl TxDependency {
counter!("grevm.large_graph", "type" => chain_type, "tip" => tip.clone()).increment(1);
if self.round.is_none() {
info!("Block({}) has large subgraph, type={}", self.block_height, chain_type);
if two && chain_type == "chain" {
self.draw_dag(weighted_group, longest_chain);
}
}
}
}

fn draw_dag(&self, weighted_group: &BTreeMap<usize, Vec<DependentTxsVec>>, longest_chain: Vec<TxId>) {
let file = File::create(format!("dag/block{}.info", self.block_height)).unwrap();
let mut writer = BufWriter::new(file);
writeln!(writer, "[dag]").unwrap();

for txid in (0..self.tx_dependency.len()).rev() {
let deps: BTreeSet<TxId> = self.tx_dependency[txid].clone().into_iter().collect();
for dep_id in deps.into_iter().rev() {
writeln!(writer, "tx{}->tx{}", txid, dep_id).unwrap();
}
}
writeln!(writer, "").unwrap();

let mut locations: HashSet<LocationAndType> = HashSet::new();
info!("Block({}) has large subgraph, longest chain: {:?}", self.block_height, longest_chain);
for index in 0..longest_chain.len() - 1 {
let txid = longest_chain[index];
let dep_id = longest_chain[index + 1];
let rs = self.tx_states[txid].read_set.clone();
let ws = self.tx_states[dep_id].write_set.clone();
let mut intersection = HashSet::new();
for l in ws {
if rs.contains_key(&l) {
intersection.insert(l);
}
}
if locations.is_empty() {
locations = intersection;
} else {
let join: HashSet<LocationAndType> = locations.intersection(&intersection).cloned().collect();
if join.is_empty() {
info!("Block({}) has large subgraph, ==> tx{}: {:?}", self.block_height, txid, locations);
}
locations = join;
}
}
}
Expand Down

0 comments on commit 23b4cb7

Please sign in to comment.