From ba96894cfc34d00251198c01393ccee70d7fc1c2 Mon Sep 17 00:00:00 2001 From: Nolan <33241113+nolanxyg@users.noreply.github.com> Date: Wed, 13 Mar 2024 17:57:10 +0800 Subject: [PATCH] add metrics --- core/blockchain.go | 8 ++------ eth/catalyst/api.go | 10 ++++++++++ miner/miner.go | 21 +++++++++++++++++++++ miner/payload_building.go | 2 ++ miner/worker.go | 19 +++++++++++++++++++ 5 files changed, 54 insertions(+), 6 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index d3a7766bec..ad7e2f8eec 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1882,12 +1882,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) storageUpdateTimer.Update(statedb.StorageUpdates) // Storage updates are complete(in validation) accountHashTimer.Update(statedb.AccountHashes) // Account hashes are complete(in validation) storageHashTimer.Update(statedb.StorageHashes) // Storage hashes are complete(in validation) - triehash := statedb.AccountHashes + statedb.StorageHashes // The time spent on tries hashing - trieUpdate := statedb.AccountUpdates + statedb.StorageUpdates // The time spent on tries update - trieRead := statedb.SnapshotAccountReads + statedb.AccountReads // The time spent on account read - trieRead += statedb.SnapshotStorageReads + statedb.StorageReads // The time spent on storage read - blockExecutionTimer.Update(ptime - trieRead) // The time spent on EVM processing - blockValidationTimer.Update(vtime - (triehash + trieUpdate)) // The time spent on block validation + blockExecutionTimer.Update(ptime) // The time spent on block execution + blockValidationTimer.Update(vtime) // The time spent on block validation // Write the block to the chain and get the status. var ( diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 69599d8c6c..eeb60bd6dc 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -32,11 +32,17 @@ import ( "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/rpc" ) +var ( + forkchoiceUpdateAttributesTimer = metrics.NewRegisteredTimer("api/engine/forkchoiceUpdate/attributes", nil) + forkchoiceUpdateHeadsTimer = metrics.NewRegisteredTimer("api/engine/forkchoiceUpdate/heads", nil) +) + // Register adds the engine API to the full node. func Register(stack *node.Node, backend *eth.Ethereum) error { log.Warn("Engine API enabled", "protocol", "eth") @@ -228,6 +234,8 @@ func checkAttribute(active func(*big.Int, uint64) bool, exists bool, block *big. } func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payloadAttributes *engine.PayloadAttributes) (engine.ForkChoiceResponse, error) { + start := time.Now() + api.forkchoiceLock.Lock() defer api.forkchoiceLock.Unlock() @@ -398,8 +406,10 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl return valid(nil), engine.InvalidPayloadAttributes.With(err) } api.localBlocks.put(id, payload) + forkchoiceUpdateAttributesTimer.UpdateSince(start) return valid(&id), nil } + forkchoiceUpdateHeadsTimer.UpdateSince(start) return valid(nil), nil } diff --git a/miner/miner.go b/miner/miner.go index 82d353a336..6d058b4be2 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -35,9 +35,30 @@ import ( "github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/params" ) +var ( + commitDepositTxsTimer = metrics.NewRegisteredTimer("miner/commit/deposit/txs", nil) + packFromTxpoolTimer = metrics.NewRegisteredTimer("miner/pack/txpool/txs", nil) + commitTxpoolTxsTimer = metrics.NewRegisteredTimer("miner/commit/txpool/txs", nil) + assembleBlockTimer = metrics.NewRegisteredTimer("miner/assemble/block", nil) + + accountReadTimer = metrics.NewRegisteredTimer("miner/account/reads", nil) + accountHashTimer = metrics.NewRegisteredTimer("miner/account/hashes", nil) + accountUpdateTimer = metrics.NewRegisteredTimer("miner/account/updates", nil) + + storageReadTimer = metrics.NewRegisteredTimer("miner/storage/reads", nil) + storageHashTimer = metrics.NewRegisteredTimer("miner/storage/hashes", nil) + storageUpdateTimer = metrics.NewRegisteredTimer("miner/storage/updates", nil) + + snapshotAccountReadTimer = metrics.NewRegisteredTimer("miner/snapshot/account/reads", nil) + snapshotStorageReadTimer = metrics.NewRegisteredTimer("miner/snapshot/storage/reads", nil) + + waitPayloadTimer = metrics.NewRegisteredTimer("miner/wait/payload", nil) +) + // Backend wraps all methods required for mining. Only full node is capable // to offer all the functions here. type Backend interface { diff --git a/miner/payload_building.go b/miner/payload_building.go index b7103d70ab..b6638373d9 100644 --- a/miner/payload_building.go +++ b/miner/payload_building.go @@ -186,7 +186,9 @@ func (payload *Payload) ResolveFull() *engine.ExecutionPayloadEnvelope { // Wait the full payload construction. Note it might block // forever if Resolve is called in the meantime which // terminates the background construction process. + start := time.Now() payload.cond.Wait() + waitPayloadTimer.UpdateSince(start) } // Terminate the background payload construction select { diff --git a/miner/worker.go b/miner/worker.go index 35eb738977..2408a1c5f7 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1047,7 +1047,9 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { // into the given sealing block. The transaction selection and ordering strategy can // be customized with the plugin in the future. func (w *worker) fillTransactions(interrupt *atomic.Int32, env *environment) error { + start := time.Now() pending := w.eth.TxPool().Pending(true) + packFromTxpoolTimer.UpdateSince(start) // Split the pending transactions into locals and remotes. localTxs, remoteTxs := make(map[common.Address][]*txpool.LazyTransaction), pending @@ -1059,6 +1061,7 @@ func (w *worker) fillTransactions(interrupt *atomic.Int32, env *environment) err } // Fill the block with all available pending transactions. + start = time.Now() if len(localTxs) > 0 { txs := newTransactionsByPriceAndNonce(env.signer, localTxs, env.header.BaseFee) if err := w.commitTransactions(env, txs, interrupt); err != nil { @@ -1071,6 +1074,7 @@ func (w *worker) fillTransactions(interrupt *atomic.Int32, env *environment) err return err } } + commitTxpoolTxsTimer.UpdateSince(start) return nil } @@ -1092,6 +1096,7 @@ func (w *worker) generateWork(genParams *generateParams) *newPayloadResult { // opBNB no need to hard code this contract via hardfork // misc.EnsureCreate2Deployer(w.chainConfig, work.header.Time, work.state) + start := time.Now() for _, tx := range genParams.txs { from, _ := types.Sender(work.signer, tx) work.state.SetTxContext(tx.Hash(), work.tcount) @@ -1101,6 +1106,7 @@ func (w *worker) generateWork(genParams *generateParams) *newPayloadResult { } work.tcount++ } + commitDepositTxsTimer.UpdateSince(start) // forced transactions done, fill rest of block with transactions if !genParams.noTxs { @@ -1115,10 +1121,23 @@ func (w *worker) generateWork(genParams *generateParams) *newPayloadResult { log.Warn("Block building is interrupted", "allowance", common.PrettyDuration(w.newpayloadTimeout)) } } + + start = time.Now() block, err := w.engine.FinalizeAndAssemble(w.chain, work.header, work.state, work.txs, nil, work.receipts, genParams.withdrawals) if err != nil { return &newPayloadResult{err: err} } + assembleBlockTimer.UpdateSince(start) + + accountReadTimer.Update(work.state.AccountReads) // Account reads are complete(in commit txs) + storageReadTimer.Update(work.state.StorageReads) // Storage reads are complete(in commit txs) + snapshotAccountReadTimer.Update(work.state.SnapshotAccountReads) // Account reads are complete(in commit txs) + snapshotStorageReadTimer.Update(work.state.SnapshotStorageReads) // Storage reads are complete(in commit txs) + accountUpdateTimer.Update(work.state.AccountUpdates) // Account updates are complete(in FinalizeAndAssemble) + storageUpdateTimer.Update(work.state.StorageUpdates) // Storage updates are complete(in FinalizeAndAssemble) + accountHashTimer.Update(work.state.AccountHashes) // Account hashes are complete(in FinalizeAndAssemble) + storageHashTimer.Update(work.state.StorageHashes) // Storage hashes are complete(in FinalizeAndAssemble) + return &newPayloadResult{ block: block, fees: totalFees(block, work.receipts),