Skip to content

Commit

Permalink
Add trace timecost log for ChainService
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <[email protected]>
  • Loading branch information
eval-exec committed Nov 2, 2023
1 parent 0679b11 commit 6696d53
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
52 changes: 47 additions & 5 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ use ckb_verification::{BlockVerifier, InvalidParentError, NonContextualBlockTxsV
use ckb_verification_contextual::{ContextualBlockVerifier, VerifyContext};
use ckb_verification_traits::{Switch, Verifier};
use std::collections::{HashSet, VecDeque};
use std::ops::AddAssign;
use std::sync::Arc;
use std::{cmp, thread};
use std::{cmp, thread, time::Duration};

type ProcessBlockRequest = Request<(Arc<BlockView>, Switch), Result<bool, Error>>;
type TruncateRequest = Request<Byte32, Result<(), Error>>;
Expand Down Expand Up @@ -196,12 +197,25 @@ impl GlobalIndex {
}
}

#[derive(Default, Debug)]
struct TimeCost {
wait_process_block: Duration,
process_block: Duration,

db_commit: Duration,
resolve_block_transactions: Duration,
non_contextual_block_verifier: Duration,
contextual_block_verifier: Duration,
verify_script: Duration,
}

/// Chain background service
///
/// The ChainService provides a single-threaded background executor.
pub struct ChainService {
shared: Shared,
proposal_table: ProposalTable,
time_cost: TimeCost,
}

impl ChainService {
Expand All @@ -210,6 +224,7 @@ impl ChainService {
ChainService {
shared,
proposal_table,
time_cost: TimeCost::default(),
}
}

Expand All @@ -228,14 +243,17 @@ impl ChainService {

let thread = thread_builder
.spawn(move || loop {
let trace_start = std::time::Instant::now();
select! {
recv(signal_receiver) -> _ => {
break;
},
recv(process_block_receiver) -> msg => match msg {
Ok(Request { responder, arguments: (block, verify) }) => {
let _ = tx_control.suspend_chunk_process();
self.time_cost.wait_process_block.add_assign(trace_start.elapsed());
let _ = responder.send(self.process_block(block, verify));
self.time_cost.process_block.add_assign(trace_start.elapsed());
let _ = tx_control.continue_chunk_process();
},
_ => {
Expand Down Expand Up @@ -347,10 +365,16 @@ impl ChainService {
warn!("receive 0 number block: 0-{}", block_hash);
}

self.insert_block(block, switch).map(|ret| {
let result = self.insert_block(block, switch).map(|ret| {
debug!("finish processing block");
ret
})
});

if block_number == 1 || block_number % 100_000 == 0 {
info!("block:{}, time_cost {:?}", block_number, self.time_cost);
}

result
}

fn non_contextual_verify(&self, block: &BlockView) -> Result<(), Error> {
Expand All @@ -373,6 +397,7 @@ impl ChainService {
}

fn insert_block(&mut self, block: Arc<BlockView>, switch: Switch) -> Result<bool, Error> {
let start_time = std::time::Instant::now();
let db_txn = Arc::new(self.shared.store().begin_transaction());
let txn_snapshot = db_txn.get_snapshot();
let _snapshot_tip_hash = db_txn.get_update_for_tip_hash(&txn_snapshot);
Expand All @@ -385,6 +410,9 @@ impl ChainService {
if !switch.disable_non_contextual() {
self.non_contextual_verify(&block)?;
}
self.time_cost
.non_contextual_block_verifier
.add_assign(start_time.elapsed());

let mut total_difficulty = U256::zero();
let mut fork = ForkChanges::default();
Expand Down Expand Up @@ -472,6 +500,8 @@ impl ChainService {
}
db_txn.commit()?;

self.time_cost.db_commit.add_assign(start_time.elapsed());

if new_best_block {
let tip_header = block.header();
info!(
Expand Down Expand Up @@ -719,7 +749,7 @@ impl ChainService {

// we found new best_block
pub(crate) fn reconcile_main_chain(
&self,
&mut self,
txn: Arc<StoreTransaction>,
fork: &mut ForkChanges,
switch: Switch,
Expand Down Expand Up @@ -753,9 +783,13 @@ impl ChainService {
.iter()
.zip(fork.attached_blocks.iter().skip(verified_len))
{
let start_time = std::time::Instant::now();
if !switch.disable_all() {
if found_error.is_none() {
let resolved = self.resolve_block_transactions(&txn, b, &verify_context);
self.time_cost
.resolve_block_transactions
.add_assign(start_time.elapsed());
match resolved {
Ok(resolved) => {
let verified = {
Expand All @@ -766,7 +800,15 @@ impl ChainService {
Arc::clone(&txs_verify_cache),
&mmr,
);
contextual_block_verifier.verify(&resolved, b)
let result = contextual_block_verifier.verify(
&resolved,
b,
&mut self.time_cost.verify_script,
);
self.time_cost
.contextual_block_verifier
.add_assign(start_time.elapsed());
result
};
match verified {
Ok((cycles, cache_entries)) => {
Expand Down
4 changes: 4 additions & 0 deletions verification/contextual/src/contextual_block_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use ckb_verification::{BlockTransactionsError, EpochError, TxVerifyEnv};
use ckb_verification_traits::Switch;
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
use std::collections::{HashMap, HashSet};
use std::ops::AddAssign;
use std::sync::Arc;
use tokio::sync::{oneshot, RwLock};

Expand Down Expand Up @@ -644,6 +645,7 @@ impl<'a, CS: ChainStore + VersionbitsIndexer + 'static, MS: MMRStore<HeaderDiges
&'a self,
resolved: &'a [Arc<ResolvedTransaction>],
block: &'a BlockView,
verify_script_cost: &mut std::time::Duration,
) -> Result<(Cycle, Vec<Completed>), Error> {
let parent_hash = block.data().header().raw().parent_hash();
let header = block.header();
Expand Down Expand Up @@ -690,13 +692,15 @@ impl<'a, CS: ChainStore + VersionbitsIndexer + 'static, MS: MMRStore<HeaderDiges

BlockExtensionVerifier::new(&self.context, self.chain_root_mmr, &parent).verify(block)?;

let start_time = std::time::Instant::now();
let ret = BlockTxsVerifier::new(
self.context.clone(),
header,
self.handle,
&self.txs_verify_cache,
)
.verify(resolved, self.switch.disable_script())?;
verify_script_cost.add_assign(start_time.elapsed());
Ok(ret)
}
}

0 comments on commit 6696d53

Please sign in to comment.