Skip to content

Commit

Permalink
add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanxyg authored and andyzhang2023 committed Apr 2, 2024
1 parent ceb83f4 commit ba96894
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
8 changes: 2 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
10 changes: 10 additions & 0 deletions eth/catalyst/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
}

Expand Down
21 changes: 21 additions & 0 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions miner/payload_building.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -1071,6 +1074,7 @@ func (w *worker) fillTransactions(interrupt *atomic.Int32, env *environment) err
return err
}
}
commitTxpoolTxsTimer.UpdateSince(start)
return nil
}

Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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),
Expand Down

0 comments on commit ba96894

Please sign in to comment.