Skip to content

Commit

Permalink
chore: add more metrics for perf
Browse files Browse the repository at this point in the history
  • Loading branch information
will@2012 committed Feb 21, 2024
1 parent 16731f9 commit dc5338b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ var (
errChainStopped = errors.New("blockchain is stopped")
errInvalidOldChain = errors.New("invalid old chain")
errInvalidNewChain = errors.New("invalid new chain")

// perf writeBlockWithState
perfCommitPhase1Timer = metrics.NewRegisteredTimer("perf/commit/phase1/time", nil)
perfCommitPhase2Timer = metrics.NewRegisteredTimer("perf/commit/phase2/time", nil)
perfCommitPhase3Timer = metrics.NewRegisteredTimer("perf/commit/phase3/time", nil)
perfCommitPhase4Timer = metrics.NewRegisteredTimer("perf/commit/phase4/time", nil)
)

const (
Expand Down Expand Up @@ -1420,6 +1426,7 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error {
// writeBlockWithState writes block, metadata and corresponding state data to the
// database.
func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) error {
startPhase1 := time.Now()
// Calculate the total difficulty of the block
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
if ptd == nil {
Expand All @@ -1440,20 +1447,30 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
if err := blockBatch.Write(); err != nil {
log.Crit("Failed to write block into disk", "err", err)
}
perfCommitPhase1Timer.UpdateSince(startPhase1)

startPhase2 := time.Now()
// Commit all cached state changes into underlying memory database.
root, err := state.Commit(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()))
if err != nil {
return err
}
perfCommitPhase2Timer.UpdateSince(startPhase2)

// If node is running in path mode, skip explicit gc operation
// which is unnecessary in this mode.
if bc.triedb.Scheme() == rawdb.PathScheme {
return nil
}
// If we're running an archive node, always flush
if bc.cacheConfig.TrieDirtyDisabled {
return bc.triedb.Commit(root, false)
startPhase3 := time.Now()
err = bc.triedb.Commit(root, false)
perfCommitPhase3Timer.UpdateSince(startPhase3)
return err
}
startPhase4 := time.Now()
perfCommitPhase4Timer.UpdateSince(startPhase4)
// Full but not archive node, do proper garbage collection
bc.triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive
bc.triegc.Push(root, -int64(block.NumberU64()))
Expand Down

0 comments on commit dc5338b

Please sign in to comment.