Skip to content

Commit c86a215

Browse files
feat(metrics):migrate metrics from develop branch (#910)
migrate metrics from develop Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
1 parent e6705ce commit c86a215

File tree

6 files changed

+134
-21
lines changed

6 files changed

+134
-21
lines changed

core/block_validator.go

+24
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,28 @@ import (
2020
"errors"
2121
"fmt"
2222
"sync"
23+
"time"
2324

2425
"github.com/scroll-tech/go-ethereum/consensus"
2526
"github.com/scroll-tech/go-ethereum/core/rawdb"
2627
"github.com/scroll-tech/go-ethereum/core/state"
2728
"github.com/scroll-tech/go-ethereum/core/types"
2829
"github.com/scroll-tech/go-ethereum/ethdb"
2930
"github.com/scroll-tech/go-ethereum/log"
31+
"github.com/scroll-tech/go-ethereum/metrics"
3032
"github.com/scroll-tech/go-ethereum/params"
3133
"github.com/scroll-tech/go-ethereum/rollup/circuitcapacitychecker"
3234
"github.com/scroll-tech/go-ethereum/trie"
3335
)
3436

37+
var (
38+
validateL1MessagesTimer = metrics.NewRegisteredTimer("validator/l1msg", nil)
39+
validateRowConsumptionTimer = metrics.NewRegisteredTimer("validator/rowconsumption", nil)
40+
validateTraceTimer = metrics.NewRegisteredTimer("validator/trace", nil)
41+
validateLockTimer = metrics.NewRegisteredTimer("validator/lock", nil)
42+
validateCccTimer = metrics.NewRegisteredTimer("validator/ccc", nil)
43+
)
44+
3545
// BlockValidator is responsible for validating block headers, uncles and
3646
// processed state.
3747
//
@@ -182,6 +192,10 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
182192
// - L1 messages follow the QueueIndex order.
183193
// - The L1 messages included in the block match the node's view of the L1 ledger.
184194
func (v *BlockValidator) ValidateL1Messages(block *types.Block) error {
195+
defer func(t0 time.Time) {
196+
validateL1MessagesTimer.Update(time.Since(t0))
197+
}(time.Now())
198+
185199
// skip DB read if the block contains no L1 messages
186200
if !block.ContainsL1Messages() {
187201
return nil
@@ -334,6 +348,10 @@ func (v *BlockValidator) createTraceEnvAndGetBlockTrace(block *types.Block) (*ty
334348
}
335349

336350
func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*types.RowConsumption, error) {
351+
defer func(t0 time.Time) {
352+
validateRowConsumptionTimer.Update(time.Since(t0))
353+
}(time.Now())
354+
337355
log.Trace(
338356
"Validator apply ccc for block",
339357
"id", v.circuitCapacityChecker.ID,
@@ -342,17 +360,23 @@ func (v *BlockValidator) validateCircuitRowConsumption(block *types.Block) (*typ
342360
"len(txs)", block.Transactions().Len(),
343361
)
344362

363+
traceStartTime := time.Now()
345364
traces, err := v.createTraceEnvAndGetBlockTrace(block)
346365
if err != nil {
347366
return nil, err
348367
}
368+
validateTraceTimer.Update(time.Since(traceStartTime))
349369

370+
lockStartTime := time.Now()
350371
v.cMu.Lock()
351372
defer v.cMu.Unlock()
373+
validateLockTimer.Update(time.Since(lockStartTime))
352374

375+
cccStartTime := time.Now()
353376
v.circuitCapacityChecker.Reset()
354377
log.Trace("Validator reset ccc", "id", v.circuitCapacityChecker.ID)
355378
rc, err := v.circuitCapacityChecker.ApplyBlock(traces)
379+
validateCccTimer.Update(time.Since(cccStartTime))
356380

357381
log.Trace(
358382
"Validator apply ccc for block result",

core/blockchain.go

+16
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ var (
5959
headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil)
6060
headFinalizedBlockGauge = metrics.NewRegisteredGauge("chain/head/finalized", nil)
6161
headSafeBlockGauge = metrics.NewRegisteredGauge("chain/head/safe", nil)
62+
headTimeGapGauge = metrics.NewRegisteredGauge("chain/head/timegap", nil)
63+
64+
l2BaseFeeGauge = metrics.NewRegisteredGauge("chain/fees/l2basefee", nil)
6265

6366
chainInfoGauge = metrics.NewRegisteredGaugeInfo("chain/info", nil)
6467

@@ -1434,6 +1437,19 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error {
14341437
// writeBlockWithState writes block, metadata and corresponding state data to the
14351438
// database.
14361439
func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) error {
1440+
// Note latest seen L2 base fee
1441+
if block.BaseFee() != nil {
1442+
l2BaseFeeGauge.Update(block.BaseFee().Int64())
1443+
} else {
1444+
l2BaseFeeGauge.Update(0)
1445+
}
1446+
1447+
parent := bc.GetHeaderByHash(block.ParentHash())
1448+
// block.Time is guaranteed to be larger than parent.Time,
1449+
// and the time gap should fit into int64.
1450+
gap := int64(block.Time() - parent.Time)
1451+
headTimeGapGauge.Update(gap)
1452+
14371453
// Calculate the total difficulty of the block
14381454
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
14391455
if ptd == nil {

core/state_processor.go

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222
"math/big"
23+
"time"
2324

2425
"github.com/scroll-tech/go-ethereum/common"
2526
"github.com/scroll-tech/go-ethereum/consensus"
@@ -28,10 +29,20 @@ import (
2829
"github.com/scroll-tech/go-ethereum/core/types"
2930
"github.com/scroll-tech/go-ethereum/core/vm"
3031
"github.com/scroll-tech/go-ethereum/crypto"
32+
"github.com/scroll-tech/go-ethereum/metrics"
3133
"github.com/scroll-tech/go-ethereum/params"
3234
"github.com/scroll-tech/go-ethereum/rollup/fees"
3335
)
3436

37+
var (
38+
processorBlockTransactionGauge = metrics.NewRegisteredGauge("processor/block/transactions", nil)
39+
processBlockTimer = metrics.NewRegisteredTimer("processor/block/process", nil)
40+
finalizeBlockTimer = metrics.NewRegisteredTimer("processor/block/finalize", nil)
41+
applyTransactionTimer = metrics.NewRegisteredTimer("processor/tx/apply", nil)
42+
applyMessageTimer = metrics.NewRegisteredTimer("processor/tx/msg/apply", nil)
43+
updateStatedbTimer = metrics.NewRegisteredTimer("processor/tx/statedb/update", nil)
44+
)
45+
3546
// StateProcessor is a basic Processor, which takes care of transitioning
3647
// state from one point to another.
3748
//
@@ -59,6 +70,10 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen
5970
// returns the amount of gas that was used in the process. If any of the
6071
// transactions failed to execute due to insufficient gas it will return an error.
6172
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
73+
defer func(t0 time.Time) {
74+
processBlockTimer.Update(time.Since(t0))
75+
}(time.Now())
76+
6277
var (
6378
receipts types.Receipts
6479
usedGas = new(uint64)
@@ -84,6 +99,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
8499
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
85100
ProcessBeaconBlockRoot(*beaconRoot, vmenv, statedb)
86101
}
102+
processorBlockTransactionGauge.Update(int64(block.Transactions().Len()))
87103
// Iterate over and process the individual transactions
88104
for i, tx := range block.Transactions() {
89105
msg, err := TransactionToMessage(tx, signer, header.BaseFee)
@@ -104,12 +120,18 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
104120
return nil, nil, 0, errors.New("withdrawals before shanghai")
105121
}
106122
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
123+
finalizeBlockStartTime := time.Now()
107124
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), withdrawals)
125+
finalizeBlockTimer.Update(time.Since(finalizeBlockStartTime))
108126

109127
return receipts, allLogs, *usedGas, nil
110128
}
111129

112130
func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) {
131+
defer func(t0 time.Time) {
132+
applyTransactionTimer.Update(time.Since(t0))
133+
}(time.Now())
134+
113135
// Create a new context to be used in the EVM environment.
114136
txContext := NewEVMTxContext(msg)
115137
evm.Reset(txContext, statedb)
@@ -120,18 +142,22 @@ func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, sta
120142
}
121143

122144
// Apply the transaction to the current state (included in the env).
145+
applyMessageStartTime := time.Now()
123146
result, err := ApplyMessage(evm, msg, gp, l1DataFee)
147+
applyMessageTimer.Update(time.Since(applyMessageStartTime))
124148
if err != nil {
125149
return nil, err
126150
}
127151

128152
// Update the state with pending changes.
129153
var root []byte
154+
updateStatedbStartTime := time.Now()
130155
if config.IsByzantium(blockNumber) {
131156
statedb.Finalise(true)
132157
} else {
133158
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
134159
}
160+
updateStatedbTimer.Update(time.Since(updateStatedbStartTime))
135161
*usedGas += result.UsedGas
136162

137163
// Create a new receipt for the transaction, storing the intermediate root and gas used

core/state_transition.go

+13
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ import (
2121
"fmt"
2222
"math"
2323
"math/big"
24+
"time"
2425

2526
"github.com/scroll-tech/go-ethereum/common"
2627
cmath "github.com/scroll-tech/go-ethereum/common/math"
2728
"github.com/scroll-tech/go-ethereum/core/types"
2829
"github.com/scroll-tech/go-ethereum/core/vm"
2930
"github.com/scroll-tech/go-ethereum/log"
31+
"github.com/scroll-tech/go-ethereum/metrics"
3032
"github.com/scroll-tech/go-ethereum/params"
3133
)
3234

35+
var (
36+
stateTransitionEvmCallExecutionTimer = metrics.NewRegisteredTimer("state/transition/call_execution", nil)
37+
stateTransitionApplyMessageTimer = metrics.NewRegisteredTimer("state/transition/apply_message", nil)
38+
)
39+
3340
// ExecutionResult includes all output after executing given evm
3441
// message no matter the execution itself is successful or not.
3542
type ExecutionResult struct {
@@ -196,6 +203,10 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In
196203
// indicates a core error meaning that the message would always fail for that particular
197204
// state and would never be accepted within a block.
198205
func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool, l1DataFee *big.Int) (*ExecutionResult, error) {
206+
defer func(t time.Time) {
207+
stateTransitionApplyMessageTimer.Update(time.Since(t))
208+
}(time.Now())
209+
199210
return NewStateTransition(evm, msg, gp, l1DataFee).TransitionDb()
200211
}
201212

@@ -463,7 +474,9 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
463474
} else {
464475
// Increment the nonce for the next transaction
465476
st.state.SetNonce(msg.From, st.state.GetNonce(sender.Address())+1)
477+
evmCallStart := time.Now()
466478
ret, st.gasRemaining, vmerr = st.evm.Call(sender, st.to(), msg.Data, st.gasRemaining, msg.Value)
479+
stateTransitionEvmCallExecutionTimer.Update(time.Since(evmCallStart))
467480
}
468481

469482
// no refunds for l1 messages

rollup/sync_service/sync_service.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/scroll-tech/go-ethereum/ethdb"
1212
"github.com/scroll-tech/go-ethereum/event"
1313
"github.com/scroll-tech/go-ethereum/log"
14+
"github.com/scroll-tech/go-ethereum/metrics"
1415
"github.com/scroll-tech/go-ethereum/node"
1516
"github.com/scroll-tech/go-ethereum/params"
1617
)
@@ -35,6 +36,10 @@ const (
3536
DbWriteThresholdBlocks = 1000
3637
)
3738

39+
var (
40+
l1MessageTotalCounter = metrics.NewRegisteredCounter("rollup/l1/message", nil)
41+
)
42+
3843
// SyncService collects all L1 messages and stores them in a local database.
3944
type SyncService struct {
4045
ctx context.Context
@@ -172,6 +177,7 @@ func (s *SyncService) fetchMessages() {
172177
numBlocksPendingDbWrite = 0
173178

174179
if numMessagesPendingDbWrite > 0 {
180+
l1MessageTotalCounter.Inc(int64(numMessagesPendingDbWrite))
175181
s.msgCountFeed.Send(core.NewL1MsgsEvent{Count: numMessagesPendingDbWrite})
176182
numMessagesPendingDbWrite = 0
177183
}

0 commit comments

Comments
 (0)