Skip to content

Commit fa86416

Browse files
metrics: add chain/mgasps to track gas usage rate (ethereum#31753)
This adds a metric called `chain/mgasps`, which records how many million gas per second are being used during block insertion. The value is calculated as `usedGas * 1000 / elapsed`, and it's updated in the `insertStats.report` method. Also cleaned up the log output to reuse the same value instead of recalculating it. Useful for monitoring block processing throughput. --------- Co-authored-by: Gary Rong <[email protected]>
1 parent 0db99f4 commit fa86416

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

core/blockchain.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ var (
6565
headFinalizedBlockGauge = metrics.NewRegisteredGauge("chain/head/finalized", nil)
6666
headSafeBlockGauge = metrics.NewRegisteredGauge("chain/head/safe", nil)
6767

68-
chainInfoGauge = metrics.NewRegisteredGaugeInfo("chain/info", nil)
68+
chainInfoGauge = metrics.NewRegisteredGaugeInfo("chain/info", nil)
69+
chainMgaspsGauge = metrics.NewRegisteredGauge("chain/mgasps", nil)
6970

7071
accountReadTimer = metrics.NewRegisteredResettingTimer("chain/account/reads", nil)
7172
accountHashTimer = metrics.NewRegisteredResettingTimer("chain/account/hashes", nil)

core/blockchain_insert.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ func (st *insertStats) report(chain []*types.Block, index int, snapDiffItems, sn
4343
// Fetch the timings for the batch
4444
var (
4545
now = mclock.Now()
46-
elapsed = now.Sub(st.startTime)
46+
elapsed = now.Sub(st.startTime) + 1 // prevent zero division
47+
mgasps = float64(st.usedGas) * 1000 / float64(elapsed)
4748
)
49+
// Update the Mgas per second gauge
50+
chainMgaspsGauge.Update(int64(mgasps))
51+
4852
// If we're at the last block of the batch or report period reached, log
4953
if index == len(chain)-1 || elapsed >= statsReportLimit {
5054
// Count the number of transactions in this segment
@@ -58,7 +62,7 @@ func (st *insertStats) report(chain []*types.Block, index int, snapDiffItems, sn
5862
context := []interface{}{
5963
"number", end.Number(), "hash", end.Hash(),
6064
"blocks", st.processed, "txs", txs, "mgas", float64(st.usedGas) / 1000000,
61-
"elapsed", common.PrettyDuration(elapsed), "mgasps", float64(st.usedGas) * 1000 / float64(elapsed),
65+
"elapsed", common.PrettyDuration(elapsed), "mgasps", mgasps,
6266
}
6367
if timestamp := time.Unix(int64(end.Time()), 0); time.Since(timestamp) > time.Minute {
6468
context = append(context, []interface{}{"age", common.PrettyAge(timestamp)}...)

0 commit comments

Comments
 (0)