-
Notifications
You must be signed in to change notification settings - Fork 21.2k
core, miner, trie: add metrics tracking state trie depth #32388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f6f3931
Update blockchain.go
shantichanal e869586
made changes for tracking depth during block processing
shantichanal 0f09a8c
small fix on metering function
shantichanal d4d1467
quick fix on previous update to make sure no code change in blockchai…
shantichanal 765c380
reset
shantichanal 80bc2f3
add metrics for depth of state trie access
shantichanal 97519e0
changes with witness tracking
shantichanal f986915
Changes to keep track of writes and account trie statistics. Also red…
shantichanal 23d24f3
small fix
shantichanal 3b35457
core,trie: rework the witness stats
rjl493456442 ca8c82e
core, trie: polish
rjl493456442 d273d81
Apply suggestion from @gballet
rjl493456442 94a0320
trie: rebase
rjl493456442 b07c9bb
core/state: fix nil trie
rjl493456442 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2025 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package stateless | ||
|
||
import ( | ||
"maps" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/metrics" | ||
) | ||
|
||
var ( | ||
accountTrieDepthAvg = metrics.NewRegisteredGauge("witness/trie/account/depth/avg", nil) | ||
accountTrieDepthMin = metrics.NewRegisteredGauge("witness/trie/account/depth/min", nil) | ||
accountTrieDepthMax = metrics.NewRegisteredGauge("witness/trie/account/depth/max", nil) | ||
|
||
storageTrieDepthAvg = metrics.NewRegisteredGauge("witness/trie/storage/depth/avg", nil) | ||
storageTrieDepthMin = metrics.NewRegisteredGauge("witness/trie/storage/depth/min", nil) | ||
storageTrieDepthMax = metrics.NewRegisteredGauge("witness/trie/storage/depth/max", nil) | ||
) | ||
|
||
// depthStats tracks min/avg/max statistics for trie access depths. | ||
type depthStats struct { | ||
totalDepth int64 | ||
samples int64 | ||
minDepth int64 | ||
maxDepth int64 | ||
} | ||
|
||
// newDepthStats creates a new depthStats with default values. | ||
func newDepthStats() *depthStats { | ||
return &depthStats{minDepth: -1} | ||
} | ||
|
||
// add records a new depth sample. | ||
func (d *depthStats) add(n int64) { | ||
if n < 0 { | ||
return | ||
} | ||
d.totalDepth += n | ||
d.samples++ | ||
|
||
if d.minDepth == -1 || n < d.minDepth { | ||
d.minDepth = n | ||
} | ||
if n > d.maxDepth { | ||
d.maxDepth = n | ||
} | ||
} | ||
|
||
// report uploads the collected statistics into the provided gauges. | ||
func (d *depthStats) report(maxGauge, minGauge, avgGauge *metrics.Gauge) { | ||
if d.samples == 0 { | ||
return | ||
} | ||
maxGauge.Update(d.maxDepth) | ||
minGauge.Update(d.minDepth) | ||
avgGauge.Update(d.totalDepth / d.samples) | ||
} | ||
|
||
// WitnessStats aggregates statistics for account and storage trie accesses. | ||
type WitnessStats struct { | ||
accountTrie *depthStats | ||
storageTrie *depthStats | ||
} | ||
|
||
// NewWitnessStats creates a new WitnessStats collector. | ||
func NewWitnessStats() *WitnessStats { | ||
return &WitnessStats{ | ||
accountTrie: newDepthStats(), | ||
storageTrie: newDepthStats(), | ||
} | ||
} | ||
|
||
// Add records trie access depths from the given node paths. | ||
// If `owner` is the zero hash, accesses are attributed to the account trie; | ||
// otherwise, they are attributed to the storage trie of that account. | ||
func (s *WitnessStats) Add(nodes map[string][]byte, owner common.Hash) { | ||
if owner == (common.Hash{}) { | ||
for path := range maps.Keys(nodes) { | ||
s.accountTrie.add(int64(len(path))) | ||
} | ||
} else { | ||
for path := range maps.Keys(nodes) { | ||
s.storageTrie.add(int64(len(path))) | ||
} | ||
} | ||
} | ||
|
||
// ReportMetrics reports the collected statistics to the global metrics registry. | ||
func (s *WitnessStats) ReportMetrics() { | ||
s.accountTrie.report(accountTrieDepthMax, accountTrieDepthMin, accountTrieDepthAvg) | ||
s.storageTrie.report(storageTrieDepthMax, storageTrieDepthMin, storageTrieDepthAvg) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume that Gary okay'd this. I wonder what the ram usage impact will be though. We'll have to check that... with metrics 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe Gary changed this so that everything is tracked in stats now, but would be curious to see how much it impacts memory.