Skip to content

Commit 48aaff6

Browse files
andyzhang2023andyzhang2023
authored andcommitted
Fix pevm 20per performance issue (bnb-chain#256)
Co-authored-by: andyzhang2023 <andyzhang2023@gmail.com>
1 parent 921afc5 commit 48aaff6

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

cmd/utils/flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,8 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
20652065

20662066
if ctx.IsSet(ParallelTxDATMaxDepthRatioFlag.Name) {
20672067
cfg.ParallelTxDAGMaxDepthRatio = ctx.Float64(ParallelTxDATMaxDepthRatioFlag.Name)
2068+
} else {
2069+
cfg.ParallelTxDAGMaxDepthRatio = ParallelTxDATMaxDepthRatioFlag.Value
20682070
}
20692071

20702072
if ctx.IsSet(ParallelTxDAGSenderPrivFlag.Name) {

core/blockchain.go

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ var (
107107

108108
// metrics to identify whether a block is processed in parallel EVM or serial EVM
109109
parallelInSequencial = metrics.NewRegisteredMeter("chain/parallel/sequencial", nil)
110-
parallelTxDepth = metrics.NewRegisteredMeter("chain/parallel/txdepth", nil)
110+
parallelTxDepth = metrics.NewRegisteredGauge("chain/parallel/txdepth", nil)
111111
// TxDepthRatio = TxDepth / TxNum * 100
112112
parallelTxDepthRatio = metrics.NewRegisteredGauge("chain/parallel/txdepth/ratio", nil)
113113

@@ -582,7 +582,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
582582
if bc.vmConfig.EnableParallelExec {
583583
bc.processor = newPEVMProcessor(chainConfig, bc, engine)
584584
bc.serialProcessor = NewStateProcessor(chainConfig, bc, engine)
585-
log.Info("Parallel V2 enabled", "parallelNum", ParallelNum())
585+
log.Info("Parallel V2 enabled", "parallelNum", ParallelNum(), "TxDepthRation", bc.vmConfig.TxDAGMaxDepthRatio)
586586
} else {
587587
bc.processor = NewStateProcessor(chainConfig, bc, engine)
588588
bc.serialProcessor = bc.processor
@@ -1773,27 +1773,26 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
17731773
return bc.insertChain(chain, true)
17741774
}
17751775

1776-
func (bc *BlockChain) useSerialProcessor(block *types.Block) (bool, bool) {
1777-
// findout whether or not the dependencies of the block are too deep to be processed
1778-
// if the dependencies are too deep, we will fallback to serial processing
1779-
txCount := len(block.Transactions())
1780-
_, depth := BuildTxLevels(txCount, bc.vmConfig.TxDAG)
1781-
var depthRatio float64
1782-
1783-
if txCount > 0 {
1784-
depthRatio = float64(depth) / float64(txCount)
1785-
} else {
1786-
depthRatio = 0
1787-
}
1788-
tooDeep := depthRatio > bc.vmConfig.TxDAGMaxDepthRatio
1789-
isByzantium := bc.chainConfig.IsByzantium(block.Number())
1790-
1791-
txDAGMissButNecessary := bc.vmConfig.TxDAG == nil && (bc.vmConfig.EnableParallelUnorderedMerge || bc.vmConfig.EnableTxParallelMerge)
1792-
useSerialProcessor := !bc.vmConfig.EnableParallelExec || txDAGMissButNecessary || tooDeep || !isByzantium
1793-
1776+
// userSerialProcessor decides whether or not to use serial processor for the block
1777+
// these are the reasons to use serial processor:
1778+
// 0. the parallel flag is not enabled
1779+
// 1. the block is empty
1780+
// 2. the block is not byzantium
1781+
// 3. the block is too deep in the dependency graph
1782+
// 4. the TxDAG is nil and parallel merge is enabled
1783+
func (bc *BlockChain) useSerialProcessor(block *types.Block) (useSerialProcessor bool) {
1784+
var (
1785+
enableParallel bool
1786+
txCount int
1787+
depth int
1788+
depthRatio float64
1789+
tooDeep bool
1790+
isByzantium bool
1791+
txDAGMissButNecessary bool
1792+
)
17941793
// mark the metrics
17951794
defer func() {
1796-
parallelTxDepth.Mark(int64(depth))
1795+
parallelTxDepth.Update(int64(depth))
17971796
parallelTxDepthRatio.Update(int64(depthRatio * 100))
17981797
// put reasons in expensive metrics
17991798
if metrics.EnabledExpensive {
@@ -1807,8 +1806,38 @@ func (bc *BlockChain) useSerialProcessor(block *types.Block) (bool, bool) {
18071806
parallelConditionByzantium.Mark(1)
18081807
}
18091808
}
1809+
log.Info("run in parallel or sequencial", "block", block.Number(), "useSerialProcessor", useSerialProcessor, "depthRatio", depthRatio, "depth", depth, "txCount", txCount, "txDAG", bc.vmConfig.TxDAG != nil, "byz", isByzantium, "tooDeep", tooDeep,
1810+
"enableParallel", enableParallel, "enableParallelExec", bc.vmConfig.EnableParallelExec)
18101811
}()
1811-
return useSerialProcessor, tooDeep
1812+
1813+
txCount = len(block.Transactions())
1814+
_, depth = BuildTxLevels(txCount, bc.vmConfig.TxDAG)
1815+
if txCount > 0 {
1816+
depthRatio = float64(depth) / float64(txCount)
1817+
} else {
1818+
depthRatio = 0
1819+
}
1820+
// switch to serial processor if the block is empty
1821+
if txCount <= 1 {
1822+
return true
1823+
}
1824+
// switch to serial processor if parallel flag not enabled
1825+
if enableParallel = bc.vmConfig.EnableParallelExec; !enableParallel {
1826+
return true
1827+
}
1828+
// switch to serial processor if the block is not byzantium
1829+
if isByzantium = bc.chainConfig.IsByzantium(block.Number()); !isByzantium {
1830+
return true
1831+
}
1832+
// switch to serial processor if the block is too deep in the dependency graph
1833+
if tooDeep = (depthRatio > bc.vmConfig.TxDAGMaxDepthRatio); tooDeep {
1834+
return true
1835+
}
1836+
// switch to serial processor if the TxDAG is nil and parallel merge is enabled
1837+
if txDAGMissButNecessary = (bc.vmConfig.TxDAG == nil && (bc.vmConfig.EnableParallelUnorderedMerge || bc.vmConfig.EnableTxParallelMerge)); txDAGMissButNecessary {
1838+
return true
1839+
}
1840+
return false
18121841
}
18131842

18141843
// insertChain is the internal implementation of InsertChain, which assumes that
@@ -2023,8 +2052,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
20232052

20242053
blockProcessedInParallel := false
20252054
var (
2026-
tooDeep, useSerialProcessor bool
2027-
depth int
2055+
useSerialProcessor bool
20282056
)
20292057
// skip block process if we already have the state, receipts and logs from mining work
20302058
if !(receiptExist && logExist && stateExist) {
@@ -2038,8 +2066,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
20382066
bc.parseTxDAG(block)
20392067
}
20402068

2041-
useSerialProcessor, tooDeep = bc.useSerialProcessor(block)
2042-
if !useSerialProcessor {
2069+
useSerialProcessor = bc.useSerialProcessor(block)
2070+
if !useSerialProcessor && bc.vmConfig.TxDAG != nil && bc.vmConfig.EnableTxParallelMerge {
2071+
//ParallelStateDB is used for parallel merge, and it works only when the block's TxDAG is not nil
20432072
statedb, err = state.NewParallel(parent.Root, bc.stateCache, bc.snaps)
20442073
} else {
20452074
statedb, err = state.New(parent.Root, bc.stateCache, bc.snaps)
@@ -2177,7 +2206,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
21772206
"storageUpdates", common.PrettyDuration(timers.StorageUpdates),
21782207
"accountHashes", common.PrettyDuration(timers.AccountHashes),
21792208
"storageHashes", common.PrettyDuration(timers.StorageHashes),
2180-
"tooDeep", tooDeep, "depth", depth,
21812209
)
21822210

21832211
// Write the block to the chain and get the status.

0 commit comments

Comments
 (0)