Skip to content

Commit 0d08b7e

Browse files
rjl493456442mask-pp
authored andcommitted
core: add metrics for state access (ethereum#30353)
This pull request adds a few more performance metrics, specifically: - The average time cost of an account read - The average time cost of a storage read - The rate of account reads - The rate of storage reads
1 parent 7e80276 commit 0d08b7e

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

core/blockchain.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ var (
7676
snapshotStorageReadTimer = metrics.NewRegisteredResettingTimer("chain/snapshot/storage/reads", nil)
7777
snapshotCommitTimer = metrics.NewRegisteredResettingTimer("chain/snapshot/commits", nil)
7878

79+
accountReadSingleTimer = metrics.NewRegisteredResettingTimer("chain/account/single/reads", nil)
80+
storageReadSingleTimer = metrics.NewRegisteredResettingTimer("chain/storage/single/reads", nil)
81+
7982
triedbCommitTimer = metrics.NewRegisteredResettingTimer("chain/triedb/commits", nil)
8083

8184
blockInsertTimer = metrics.NewRegisteredResettingTimer("chain/inserts", nil)
@@ -1947,18 +1950,25 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s
19471950
proctime := time.Since(start) // processing + validation
19481951

19491952
// Update the metrics touched during block processing and validation
1950-
accountReadTimer.Update(statedb.AccountReads) // Account reads are complete(in processing)
1951-
storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete(in processing)
1952-
snapshotAccountReadTimer.Update(statedb.SnapshotAccountReads) // Account reads are complete(in processing)
1953-
snapshotStorageReadTimer.Update(statedb.SnapshotStorageReads) // Storage reads are complete(in processing)
1953+
accountReadTimer.Update(statedb.AccountReads) // Account reads are complete(in processing)
1954+
storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete(in processing)
1955+
snapshotAccountReadTimer.Update(statedb.SnapshotAccountReads) // Account reads are complete(in processing)
1956+
snapshotStorageReadTimer.Update(statedb.SnapshotStorageReads) // Storage reads are complete(in processing)
1957+
1958+
accountRead := statedb.SnapshotAccountReads + statedb.AccountReads // The time spent on account read
1959+
storageRead := statedb.SnapshotStorageReads + statedb.StorageReads // The time spent on storage read
1960+
if statedb.AccountLoaded != 0 {
1961+
accountReadSingleTimer.Update(accountRead / time.Duration(statedb.AccountLoaded))
1962+
}
1963+
if statedb.StorageLoaded != 0 {
1964+
storageReadSingleTimer.Update(storageRead / time.Duration(statedb.StorageLoaded))
1965+
}
19541966
accountUpdateTimer.Update(statedb.AccountUpdates) // Account updates are complete(in validation)
19551967
storageUpdateTimer.Update(statedb.StorageUpdates) // Storage updates are complete(in validation)
19561968
accountHashTimer.Update(statedb.AccountHashes) // Account hashes are complete(in validation)
19571969
triehash := statedb.AccountHashes // The time spent on tries hashing
19581970
trieUpdate := statedb.AccountUpdates + statedb.StorageUpdates // The time spent on tries update
1959-
trieRead := statedb.SnapshotAccountReads + statedb.AccountReads // The time spent on account read
1960-
trieRead += statedb.SnapshotStorageReads + statedb.StorageReads // The time spent on storage read
1961-
blockExecutionTimer.Update(ptime - trieRead) // The time spent on EVM processing
1971+
blockExecutionTimer.Update(ptime - (accountRead + storageRead)) // The time spent on EVM processing
19621972
blockValidationTimer.Update(vtime - (triehash + trieUpdate)) // The time spent on block validation
19631973

19641974
// Write the block to the chain and get the status.

core/state/metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package state
1919
import "github.com/ethereum/go-ethereum/metrics"
2020

2121
var (
22+
accountReadMeters = metrics.NewRegisteredMeter("state/read/accounts", nil)
23+
storageReadMeters = metrics.NewRegisteredMeter("state/read/storage", nil)
2224
accountUpdatedMeter = metrics.NewRegisteredMeter("state/update/account", nil)
2325
storageUpdatedMeter = metrics.NewRegisteredMeter("state/update/storage", nil)
2426
accountDeletedMeter = metrics.NewRegisteredMeter("state/delete/account", nil)

core/state/state_object.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
239239
}
240240
}
241241
s.originStorage[key] = value
242+
s.db.StorageLoaded++
242243
return value
243244
}
244245

core/state/statedb.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,12 @@ type StateDB struct {
163163
SnapshotCommits time.Duration
164164
TrieDBCommits time.Duration
165165

166-
AccountUpdated int
167-
StorageUpdated atomic.Int64
168-
AccountDeleted int
169-
StorageDeleted atomic.Int64
166+
AccountLoaded int // Number of accounts retrieved from the database during the state transition
167+
AccountUpdated int // Number of accounts updated during the state transition
168+
AccountDeleted int // Number of accounts deleted during the state transition
169+
StorageLoaded int // Number of storage slots retrieved from the database during the state transition
170+
StorageUpdated atomic.Int64 // Number of storage slots updated during the state transition
171+
StorageDeleted atomic.Int64 // Number of storage slots deleted during the state transition
170172
}
171173

172174
// New creates a new state from a given trie.
@@ -601,6 +603,7 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject {
601603
s.SnapshotAccountReads += time.Since(start)
602604
if err == nil {
603605
if acc == nil {
606+
s.AccountLoaded++
604607
return nil
605608
}
606609
data = &types.StateAccount{
@@ -629,6 +632,7 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject {
629632
return nil
630633
}
631634
if data == nil {
635+
s.AccountLoaded++
632636
return nil
633637
}
634638
}
@@ -643,6 +647,7 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject {
643647
// Insert into the live set
644648
obj := newObject(s, addr, data)
645649
s.setStateObject(obj)
650+
s.AccountLoaded++
646651
return obj
647652
}
648653

@@ -1286,6 +1291,8 @@ func (s *StateDB) commit(deleteEmptyObjects bool) (*stateUpdate, error) {
12861291
if err := workers.Wait(); err != nil {
12871292
return nil, err
12881293
}
1294+
accountReadMeters.Mark(int64(s.AccountLoaded))
1295+
storageReadMeters.Mark(int64(s.StorageLoaded))
12891296
accountUpdatedMeter.Mark(int64(s.AccountUpdated))
12901297
storageUpdatedMeter.Mark(s.StorageUpdated.Load())
12911298
accountDeletedMeter.Mark(int64(s.AccountDeleted))
@@ -1294,7 +1301,10 @@ func (s *StateDB) commit(deleteEmptyObjects bool) (*stateUpdate, error) {
12941301
accountTrieDeletedMeter.Mark(int64(accountTrieNodesDeleted))
12951302
storageTriesUpdatedMeter.Mark(int64(storageTrieNodesUpdated))
12961303
storageTriesDeletedMeter.Mark(int64(storageTrieNodesDeleted))
1297-
s.AccountUpdated, s.AccountDeleted = 0, 0
1304+
1305+
// Clear the metric markers
1306+
s.AccountLoaded, s.AccountUpdated, s.AccountDeleted = 0, 0, 0
1307+
s.StorageLoaded = 0
12981308
s.StorageUpdated.Store(0)
12991309
s.StorageDeleted.Store(0)
13001310

triedb/pathdb/disklayer.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
139139
dl.cleans.Set(key, blob)
140140
cleanWriteMeter.Mark(int64(len(blob)))
141141
}
142-
143142
return blob, h.hash(blob), &nodeLoc{loc: locDiskLayer, depth: depth}, nil
144143
}
145144

0 commit comments

Comments
 (0)