Skip to content

Commit d3679c2

Browse files
authored
core/state: export statistics to metrics (#33254)
This PR exposes the state size statistics to the metrics, making them easier to demonstrate. Note that the contract code included in the metrics is not de-duplicated, so the reported size will appear larger than the actual storage footprint.
1 parent 212967d commit d3679c2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

core/state/state_sizer.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/ethereum/go-ethereum/crypto"
3232
"github.com/ethereum/go-ethereum/ethdb"
3333
"github.com/ethereum/go-ethereum/log"
34+
"github.com/ethereum/go-ethereum/metrics"
3435
"github.com/ethereum/go-ethereum/triedb"
3536
"golang.org/x/sync/errgroup"
3637
)
@@ -48,6 +49,21 @@ var (
4849
codeKeySize = int64(len(rawdb.CodePrefix) + common.HashLength)
4950
)
5051

52+
// State size metrics
53+
var (
54+
stateSizeChainHeightGauge = metrics.NewRegisteredGauge("state/height", nil)
55+
stateSizeAccountsCountGauge = metrics.NewRegisteredGauge("state/accounts/count", nil)
56+
stateSizeAccountsBytesGauge = metrics.NewRegisteredGauge("state/accounts/bytes", nil)
57+
stateSizeStoragesCountGauge = metrics.NewRegisteredGauge("state/storages/count", nil)
58+
stateSizeStoragesBytesGauge = metrics.NewRegisteredGauge("state/storages/bytes", nil)
59+
stateSizeAccountTrieNodesCountGauge = metrics.NewRegisteredGauge("state/trienodes/account/count", nil)
60+
stateSizeAccountTrieNodesBytesGauge = metrics.NewRegisteredGauge("state/trienodes/account/bytes", nil)
61+
stateSizeStorageTrieNodesCountGauge = metrics.NewRegisteredGauge("state/trienodes/storage/count", nil)
62+
stateSizeStorageTrieNodesBytesGauge = metrics.NewRegisteredGauge("state/trienodes/storage/bytes", nil)
63+
stateSizeContractsCountGauge = metrics.NewRegisteredGauge("state/contracts/count", nil)
64+
stateSizeContractsBytesGauge = metrics.NewRegisteredGauge("state/contracts/bytes", nil)
65+
)
66+
5167
// SizeStats represents either the current state size statistics or the size
5268
// differences resulting from a state transition.
5369
type SizeStats struct {
@@ -76,6 +92,20 @@ func (s SizeStats) String() string {
7692
)
7793
}
7894

95+
func (s SizeStats) publish() {
96+
stateSizeChainHeightGauge.Update(int64(s.BlockNumber))
97+
stateSizeAccountsCountGauge.Update(s.Accounts)
98+
stateSizeAccountsBytesGauge.Update(s.AccountBytes)
99+
stateSizeStoragesCountGauge.Update(s.Storages)
100+
stateSizeStoragesBytesGauge.Update(s.StorageBytes)
101+
stateSizeAccountTrieNodesCountGauge.Update(s.AccountTrienodes)
102+
stateSizeAccountTrieNodesBytesGauge.Update(s.AccountTrienodeBytes)
103+
stateSizeStorageTrieNodesCountGauge.Update(s.StorageTrienodes)
104+
stateSizeStorageTrieNodesBytesGauge.Update(s.StorageTrienodeBytes)
105+
stateSizeContractsCountGauge.Update(s.ContractCodes)
106+
stateSizeContractsBytesGauge.Update(s.ContractCodeBytes)
107+
}
108+
79109
// add applies the given state diffs and produces a new version of the statistics.
80110
func (s SizeStats) add(diff SizeStats) SizeStats {
81111
s.StateRoot = diff.StateRoot
@@ -309,6 +339,10 @@ func (t *SizeTracker) run() {
309339
stats[u.root] = stat
310340
last = u.root
311341

342+
// Publish statistics to metric system
343+
stat.publish()
344+
345+
// Evict the stale statistics
312346
heap.Push(&h, stats[u.root])
313347
for u.blockNumber-h[0].BlockNumber > statEvictThreshold {
314348
delete(stats, h[0].StateRoot)

0 commit comments

Comments
 (0)