Skip to content

Commit

Permalink
Merge branch 'perf_analysis' of https://github.com/forcodedancing/bsc
Browse files Browse the repository at this point in the history
…into perf_analysis
  • Loading branch information
forcodedancing committed Feb 25, 2022
2 parents ecc0fdc + be7b436 commit 3989be3
Show file tree
Hide file tree
Showing 16 changed files with 499 additions and 133 deletions.
41 changes: 41 additions & 0 deletions cachemetrics/get_gid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cachemetrics

import (
"github.com/petermattis/goid"
"sync/atomic"
)

var (
MiningRoutineId int64 // mining main process routine id
SyncingRoutineId int64 // syncing main process routine id
)

func Goid() int64 {
return goid.Get()
}

func UpdateMiningRoutineID(id int64) {
atomic.StoreInt64(&MiningRoutineId, id)
}

// judge if it is main process of mining
func IsMinerMainRoutineID(id int64) bool {
if id == atomic.LoadInt64(&MiningRoutineId) {
return true
}
return false
}

func UpdateSyncingRoutineID(id int64) {
if SyncingRoutineId != id {
SyncingRoutineId = id
}
}

// judge if it is main process of syncing
func IsSyncMainRoutineID(id int64) bool {
if id == SyncingRoutineId {
return true
}
return false
}
114 changes: 114 additions & 0 deletions cachemetrics/mining_cache_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package cachemetrics

import (
"time"

"github.com/ethereum/go-ethereum/metrics"
)

const (
MinerL1ACCOUNT cacheLayerName = "MINER_L1_ACCOUNT"
MinerL2ACCOUNT cacheLayerName = "MINER_L2_ACCOUNT"
MinerL3ACCOUNT cacheLayerName = "MINER_L3_ACCOUNT"
MinerL4ACCOUNT cacheLayerName = "MINER_L4_ACCOUNT"
MinerL1STORAGE cacheLayerName = "MINER_L1_STORAGE"
MinerL2STORAGE cacheLayerName = "MINER_L2_STORAGE"
MinerL3STORAGE cacheLayerName = "MINER_L3_STORAGE"
MinerL4STORAGE cacheLayerName = "MINER_L4_STORAGE"
)

var (
cacheL1MiningAccountTimer = metrics.NewRegisteredTimer("minercache/cost/account/layer1", nil)
cacheL2MiningAccountTimer = metrics.NewRegisteredTimer("minercache/cost/account/layer2", nil)
cacheL3MiningAccountTimer = metrics.NewRegisteredTimer("minercache/cost/account/layer3", nil)
diskL4MiningAccountTimer = metrics.NewRegisteredTimer("minercache/cost/account/layer4", nil)
cacheL1MiningStorageTimer = metrics.NewRegisteredTimer("minercache/cost/storage/layer1", nil)
cacheL2MiningStorageTimer = metrics.NewRegisteredTimer("minercache/cost/storage/layer2", nil)
cacheL3MiningStorageTimer = metrics.NewRegisteredTimer("minercache/cost/storage/layer3", nil)
diskL4MiningStorageTimer = metrics.NewRegisteredTimer("minercache/cost/storage/layer4", nil)

cacheL1MiningAccountCounter = metrics.NewRegisteredCounter("minercache/count/account/layer1", nil)
cacheL2MiningAccountCounter = metrics.NewRegisteredCounter("minercache/count/account/layer2", nil)
cacheL3MiningAccountCounter = metrics.NewRegisteredCounter("minercache/count/account/layer3", nil)
diskL4MiningAccountCounter = metrics.NewRegisteredCounter("minercache/count/account/layer4", nil)
cacheL1MiningStorageCounter = metrics.NewRegisteredCounter("minercache/count/storage/layer1", nil)
cacheL2MiningStorageCounter = metrics.NewRegisteredCounter("minercache/count/storage/layer2", nil)
cacheL3MiningStorageCounter = metrics.NewRegisteredCounter("minercache/count/storage/layer3", nil)
diskL4MiningStorageCounter = metrics.NewRegisteredCounter("minercache/count/storage/layer4", nil)

cacheL1MiningAccountCostCounter = metrics.NewRegisteredCounter("minercache/totalcost/account/layer1", nil)
cacheL2MiningAccountCostCounter = metrics.NewRegisteredCounter("minercache/totalcost/account/layer2", nil)
cacheL3MiningAccountCostCounter = metrics.NewRegisteredCounter("minercache/totalcost/account/layer3", nil)
diskL4MiningAccountCostCounter = metrics.NewRegisteredCounter("minercache/totalcost/account/layer4", nil)
cacheL1MiningStorageCostCounter = metrics.NewRegisteredCounter("minercache/totalcost/storage/layer1", nil)
cacheL2MiningStorageCostCounter = metrics.NewRegisteredCounter("minercache/totalcost/storage/layer2", nil)
cacheL3MiningStorageCostCounter = metrics.NewRegisteredCounter("minercache/totalcost/storage/layer3", nil)
diskL4MiningStorageCostCounter = metrics.NewRegisteredCounter("minercache/totalcost/storage/layer4", nil)
)

// mark the info of total hit counts of each layers
func RecordMinerCacheDepth(metricsName cacheLayerName) {
switch metricsName {
case MinerL1ACCOUNT:
cacheL1MiningAccountCounter.Inc(1)
case MinerL2ACCOUNT:
cacheL2MiningAccountCounter.Inc(1)
case MinerL3ACCOUNT:
cacheL3MiningAccountCounter.Inc(1)
case MinerL4ACCOUNT:
diskL4MiningAccountCounter.Inc(1)
case MinerL1STORAGE:
cacheL1MiningStorageCounter.Inc(1)
case MinerL2STORAGE:
cacheL2MiningStorageCounter.Inc(1)
case MinerL3STORAGE:
cacheL3MiningStorageCounter.Inc(1)
case MinerL4STORAGE:
diskL4MiningStorageCounter.Inc(1)
}
}

// mark the dalays of each layers
func RecordMinerCacheMetrics(metricsName cacheLayerName, start time.Time) {
switch metricsName {
case MinerL1ACCOUNT:
recordCost(cacheL1MiningAccountTimer, start)
case MinerL2ACCOUNT:
recordCost(cacheL2MiningAccountTimer, start)
case MinerL3ACCOUNT:
recordCost(cacheL3MiningAccountTimer, start)
case MinerL4ACCOUNT:
recordCost(diskL4MiningAccountTimer, start)
case MinerL1STORAGE:
recordCost(cacheL1MiningStorageTimer, start)
case MinerL2STORAGE:
recordCost(cacheL2MiningStorageTimer, start)
case MinerL3STORAGE:
recordCost(cacheL3MiningStorageTimer, start)
case MinerL4STORAGE:
recordCost(diskL4MiningStorageTimer, start)

}
}

// accumulate the total dalays of each layers
func RecordMinerTotalCosts(metricsName cacheLayerName, start time.Time) {
switch metricsName {
case MinerL1ACCOUNT:
accumulateCost(cacheL1MiningAccountCostCounter, start)
case MinerL2ACCOUNT:
accumulateCost(cacheL2MiningAccountCostCounter, start)
case MinerL3ACCOUNT:
accumulateCost(cacheL3MiningAccountCostCounter, start)
case MinerL4ACCOUNT:
accumulateCost(diskL4MiningAccountCostCounter, start)
case MinerL1STORAGE:
accumulateCost(cacheL1MiningStorageCostCounter, start)
case MinerL2STORAGE:
accumulateCost(cacheL2MiningStorageCostCounter, start)
case MinerL3STORAGE:
accumulateCost(cacheL3MiningStorageCostCounter, start)
case MinerL4STORAGE:
accumulateCost(diskL4MiningStorageCostCounter, start)
}
}
7 changes: 1 addition & 6 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,7 @@ func applyMetricConfig(ctx *cli.Context, cfg *gethConfig) {
if ctx.GlobalIsSet(utils.MetricsEnabledExpensiveFlag.Name) {
cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledExpensiveFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsEnabledRecordIOFlag.Name) {
cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsEnabledRecordIOFlag.Name)
}
if ctx.GlobalIsSet(utils.MetricsDisablePrefetchFlag.Name) {
cfg.Metrics.EnabledExpensive = ctx.GlobalBool(utils.MetricsDisablePrefetchFlag.Name)
}

if ctx.GlobalIsSet(utils.MetricsHTTPFlag.Name) {
cfg.Metrics.HTTP = ctx.GlobalString(utils.MetricsHTTPFlag.Name)
}
Expand Down
16 changes: 5 additions & 11 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/cachemetrics"

lru "github.com/hashicorp/golang-lru"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -66,9 +68,6 @@ var (

snapshotAccountReadTimer = metrics.NewRegisteredTimer("chain/snapshot/account/reads", nil)
snapshotStorageReadTimer = metrics.NewRegisteredTimer("chain/snapshot/storage/reads", nil)
totalAccountReadTimer = metrics.NewRegisteredTimer("chain/total/account/reads", nil)
totalStorageReadTimer = metrics.NewRegisteredTimer("chain/total/storage/reads", nil)
totalReadTimer = metrics.NewRegisteredTimer("chain/total/cost/reads", nil)
snapshotCommitTimer = metrics.NewRegisteredTimer("chain/snapshot/commits", nil)

blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
Expand Down Expand Up @@ -1951,6 +1950,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
signer := types.MakeSigner(bc.chainConfig, chain[0].Number())
go senderCacher.recoverFromBlocks(signer, chain)

goid := cachemetrics.Goid()
cachemetrics.UpdateSyncingRoutineID(goid)

var (
stats = insertStats{startTime: mclock.Now()}
lastCanon *types.Block
Expand Down Expand Up @@ -2155,14 +2157,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
snapshotAccountReadTimer.Update(statedb.SnapshotAccountReads) // Account reads are complete, we can mark them
snapshotStorageReadTimer.Update(statedb.SnapshotStorageReads) // Storage reads are complete, we can mark them

accountReadCost := statedb.SnapshotAccountReads + statedb.L1CacheAccountReads + statedb.AccountReads
storageReadCost := statedb.SnapshotStorageReads + statedb.L1CacheStorageReads + statedb.StorageReads
// mark the total io process cost in L1-L4 layers of account
totalAccountReadTimer.Update(accountReadCost)
// mark the total io process cost in L1-L4 layers of storage
totalStorageReadTimer.Update(storageReadCost)
// mark the total io process cost in L1-L4 layers
totalReadTimer.Update(storageReadCost + accountReadCost)
blockExecutionTimer.Update(time.Since(substart))

// Validate the state using the default validator
Expand Down
51 changes: 42 additions & 9 deletions core/state/snapshot/difflayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,28 @@ func (dl *diffLayer) AccountRLP(hash common.Hash) ([]byte, error) {
start := time.Now()
hitInDifflayer := false
defer func() {
if hitInDifflayer {
cachemetrics.RecordCacheDepth("CACHE_L2_ACCOUNT")
cachemetrics.RecordCacheMetrics("CACHE_L2_ACCOUNT", start)
cachemetrics.RecordTotalCosts("CACHE_L2_ACCOUNT", start)
routeid := cachemetrics.Goid()
isSyncMainProcess := cachemetrics.IsSyncMainRoutineID(routeid)
isMinerMainProcess := cachemetrics.IsMinerMainRoutineID(routeid)
if isSyncMainProcess {
// l1 miss
syncL1MissAccountMeter.Mark(1)
if hitInDifflayer {
syncL2AccountHitMeter.Mark(1)
cachemetrics.RecordCacheDepth("CACHE_L2_ACCOUNT")
cachemetrics.RecordCacheMetrics("CACHE_L2_ACCOUNT", start)
cachemetrics.RecordTotalCosts("CACHE_L2_ACCOUNT", start)
}
}
if isMinerMainProcess {
// l1 miss
minerL1MissAccountMeter.Mark(1)
if hitInDifflayer {
minerL2AccountHitMeter.Mark(1)
cachemetrics.RecordMinerCacheDepth("MINER_L2_ACCOUNT")
cachemetrics.RecordMinerCacheMetrics("MINER_L2_ACCOUNT", start)
cachemetrics.RecordMinerTotalCosts("MINER_L2_ACCOUNT", start)
}
}
}()
var origin *diskLayer
Expand Down Expand Up @@ -401,15 +419,30 @@ func (dl *diffLayer) Storage(accountHash, storageHash common.Hash) ([]byte, erro
// Check the bloom filter first whether there's even a point in reaching into
// all the maps in all the layers below
start := time.Now()
routeid := cachemetrics.Goid()
hitInDifflayer := false
defer func() {
if hitInDifflayer {
cachemetrics.RecordCacheDepth("CACHE_L2_STORAGE")
cachemetrics.RecordCacheMetrics("CACHE_L2_STORAGE", start)
cachemetrics.RecordTotalCosts("CACHE_L2_STORAGE", start)
isSyncMainProcess := cachemetrics.IsSyncMainRoutineID(routeid)
isMinerMainProcess := cachemetrics.IsMinerMainRoutineID(routeid)
if isSyncMainProcess {
syncL1MissStorageMeter.Mark(1)
if hitInDifflayer {
syncL2StorageHitMeter.Mark(1)
cachemetrics.RecordCacheDepth("CACHE_L2_STORAGE")
cachemetrics.RecordCacheMetrics("CACHE_L2_STORAGE", start)
cachemetrics.RecordTotalCosts("CACHE_L2_STORAGE", start)
}
}
if isMinerMainProcess {
minerL1MissStorageMeter.Mark(1)
if hitInDifflayer {
minerL2StorageHitMeter.Mark(1)
cachemetrics.RecordMinerCacheDepth("MINER_L2_STORAGE")
cachemetrics.RecordMinerCacheMetrics("MINER_L2_STORAGE", start)
cachemetrics.RecordMinerTotalCosts("MINER_L2_STORAGE", start)
}
}
}()

dl.lock.RLock()
hit := dl.diffed.Contains(storageBloomHasher{accountHash, storageHash})
if !hit {
Expand Down
Loading

0 comments on commit 3989be3

Please sign in to comment.