Skip to content

Commit

Permalink
core/state: move metrics out of state objects (#29665)
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Apr 26, 2024
1 parent 8d42e11 commit 4253030
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
4 changes: 1 addition & 3 deletions core/blockchain.go
Expand Up @@ -68,7 +68,6 @@ var (
accountCommitTimer = metrics.NewRegisteredResettingTimer("chain/account/commits", nil)

storageReadTimer = metrics.NewRegisteredResettingTimer("chain/storage/reads", nil)
storageHashTimer = metrics.NewRegisteredResettingTimer("chain/storage/hashes", nil)
storageUpdateTimer = metrics.NewRegisteredResettingTimer("chain/storage/updates", nil)
storageCommitTimer = metrics.NewRegisteredResettingTimer("chain/storage/commits", nil)

Expand Down Expand Up @@ -1937,8 +1936,7 @@ func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, s
accountUpdateTimer.Update(statedb.AccountUpdates) // Account updates are complete(in validation)
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
triehash := statedb.AccountHashes // 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
Expand Down
9 changes: 0 additions & 9 deletions core/state/state_object.go
Expand Up @@ -294,9 +294,6 @@ func (s *stateObject) updateTrie() (Trie, error) {
if len(s.pendingStorage) == 0 {
return s.trie, nil
}
// Track the amount of time wasted on updating the storage trie
defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now())

// The snapshot storage map for the object
var (
storage map[common.Hash][]byte
Expand Down Expand Up @@ -400,9 +397,6 @@ func (s *stateObject) updateRoot() {
if err != nil || tr == nil {
return
}
// Track the amount of time wasted on hashing the storage trie
defer func(start time.Time) { s.db.StorageHashes += time.Since(start) }(time.Now())

s.data.Root = tr.Hash()
}

Expand All @@ -415,9 +409,6 @@ func (s *stateObject) commit() (*trienode.NodeSet, error) {
s.origin = s.data.Copy()
return nil, nil
}
// Track the amount of time wasted on committing the storage trie
defer func(start time.Time) { s.db.StorageCommits += time.Since(start) }(time.Now())

// The trie is currently in an open state and could potentially contain
// cached mutations. Call commit to acquire a set of nodes that have been
// modified, the set can be nil if nothing to commit.
Expand Down
9 changes: 7 additions & 2 deletions core/state/statedb.go
Expand Up @@ -151,7 +151,6 @@ type StateDB struct {
AccountUpdates time.Duration
AccountCommits time.Duration
StorageReads time.Duration
StorageHashes time.Duration
StorageUpdates time.Duration
StorageCommits time.Duration
SnapshotAccountReads time.Duration
Expand Down Expand Up @@ -856,6 +855,7 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
// the account prefetcher. Instead, let's process all the storage updates
// first, giving the account prefetches just a few more milliseconds of time
// to pull useful data from disk.
start := time.Now()
for addr, op := range s.mutations {
if op.applied {
continue
Expand All @@ -865,6 +865,8 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
}
s.stateObjects[addr].updateRoot()
}
s.StorageUpdates += time.Since(start)

// Now we're about to start to write changes to the trie. The trie is so far
// _untouched_. We can check with the prefetcher, if it can give us a trie
// which has the same root, but also has some content loaded into it.
Expand Down Expand Up @@ -1151,6 +1153,7 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er
return common.Hash{}, err
}
// Handle all state updates afterwards
start := time.Now()
for addr, op := range s.mutations {
if op.isDelete() {
continue
Expand Down Expand Up @@ -1179,13 +1182,15 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er
storageTrieNodesDeleted += deleted
}
}
s.StorageCommits += time.Since(start)

if codeWriter.ValueSize() > 0 {
if err := codeWriter.Write(); err != nil {
log.Crit("Failed to commit dirty codes", "error", err)
}
}
// Write the account trie changes, measuring the amount of wasted time
start := time.Now()
start = time.Now()

root, set, err := s.trie.Commit(true)
if err != nil {
Expand Down

0 comments on commit 4253030

Please sign in to comment.