Skip to content

Commit 9cb6109

Browse files
committed
made changes for tracking depth during block processing
1 parent e2f8854 commit 9cb6109

File tree

5 files changed

+80
-33
lines changed

5 files changed

+80
-33
lines changed

core/blockchain.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,38 +2134,6 @@ func (bc *BlockChain) processBlock(parentRoot common.Hash, block *types.Block, s
21342134
mgasps := float64(res.GasUsed) * 1000 / float64(elapsed)
21352135
chainMgaspsMeter.Update(time.Duration(mgasps))
21362136

2137-
//get state db trie node iterator
2138-
it, error := statedb.GetTrie().NodeIterator(nil)
2139-
if error != nil {
2140-
//log that iterator was not working
2141-
log.Error("Failed to get trie node iterator", "error", error)
2142-
} else {
2143-
var (
2144-
maxDepth int
2145-
sumDepth int
2146-
count int
2147-
)
2148-
2149-
for it.Next(true) {
2150-
// it.Path() is a hex-nibble slice; the “terminator” nibble (0x10)
2151-
// exists only on leaf-nodes, so strip it if present.
2152-
d := len(it.Path())
2153-
if d > 0 && it.Path()[d-1] == 0x10 {
2154-
d--
2155-
}
2156-
if d > maxDepth {
2157-
maxDepth = d
2158-
}
2159-
sumDepth += d
2160-
count++
2161-
}
2162-
2163-
stateMaxDepthGauge.Update(int64(maxDepth))
2164-
if count > 0 {
2165-
stateAvgDepthGauge.Update(int64(sumDepth / count))
2166-
}
2167-
}
2168-
21692137
return &blockProcessingResult{
21702138
usedGas: res.GasUsed,
21712139
procTime: proctime,

core/state/statedb.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,3 +1464,11 @@ func (s *StateDB) Witness() *stateless.Witness {
14641464
func (s *StateDB) AccessEvents() *AccessEvents {
14651465
return s.accessEvents
14661466
}
1467+
1468+
func (s *StateDB) BeginTrieDepthWindow() {
1469+
trie.StateDepthMetricsStartBlock()
1470+
}
1471+
1472+
func (s *StateDB) EndTrieDepthWindow() {
1473+
trie.StateDepthMetricsEndBlock()
1474+
}

core/state_processor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
6464
allLogs []*types.Log
6565
gp = new(GasPool).AddGas(block.GasLimit())
6666
)
67+
// Start the trie depth metrics window
68+
statedb.BeginTrieDepthWindow()
69+
defer statedb.EndTrieDepthWindow()
6770

6871
// Mutate the block and state according to any hard-fork specs
6972
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {

trie/trie.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ func (t *Trie) update(key, value []byte) error {
326326

327327
func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) {
328328
if len(key) == 0 {
329+
if t.owner == (common.Hash{}) {
330+
stateDepthAggregator.record(int64(len(prefix)))
331+
}
332+
329333
if v, ok := n.(valueNode); ok {
330334
return !bytes.Equal(v, value.(valueNode)), value, nil
331335
}
@@ -445,7 +449,9 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
445449
// it in the deletion set. The same the valueNode doesn't
446450
// need to be tracked at all since it's always embedded.
447451
t.tracer.onDelete(prefix)
448-
452+
if t.owner == (common.Hash{}) {
453+
stateDepthAggregator.record(int64(len(prefix)))
454+
}
449455
return true, nil, nil // remove n entirely for whole matches
450456
}
451457
// The key is longer than n.Key. Remove the remaining suffix
@@ -468,6 +474,9 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
468474
// always creates a new slice) instead of append to
469475
// avoid modifying n.Key since it might be shared with
470476
// other nodes.
477+
if t.owner == (common.Hash{}) {
478+
stateDepthAggregator.record(int64(len(prefix) + len(key)))
479+
}
471480
return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil
472481
default:
473482
return true, &shortNode{n.Key, child, t.newFlag()}, nil
@@ -526,6 +535,9 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
526535
// Mark the original short node as deleted since the
527536
// value is embedded into the parent now.
528537
t.tracer.onDelete(append(prefix, byte(pos)))
538+
if t.owner == (common.Hash{}) {
539+
stateDepthAggregator.record(int64(len(prefix) + 1))
540+
}
529541

530542
k := append([]byte{byte(pos)}, cnode.Key...)
531543
return true, &shortNode{k, cnode.Val, t.newFlag()}, nil

trie/trie_metrics.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package trie
2+
3+
import (
4+
"sync"
5+
6+
"github.com/ethereum/go-ethereum/metrics"
7+
)
8+
9+
var (
10+
avgAccessDepthInBlock = metrics.NewRegisteredMeter("trie/access/depth/avg", nil)
11+
minAccessDepthInBlock = metrics.NewRegisteredMeter("trie/access/depth/min", nil)
12+
stateDepthAggregator = &depthAggregator{}
13+
)
14+
15+
// depthAggregator aggregates trie access depth metrics for a block.
16+
type depthAggregator struct {
17+
mu sync.Mutex
18+
sum, cnt int64
19+
min int64
20+
}
21+
22+
// start initializes the aggregator for a new block.
23+
func (d *depthAggregator) start() {
24+
d.mu.Lock()
25+
d.sum, d.cnt, d.min = 0, 0, -1
26+
d.mu.Unlock()
27+
}
28+
29+
// record records the access depth for a trie operation.
30+
func (d *depthAggregator) record(depth int64) {
31+
d.mu.Lock()
32+
v := depth
33+
d.sum += v
34+
d.cnt++
35+
if d.min < 0 || v < d.min {
36+
d.min = v
37+
}
38+
d.mu.Unlock()
39+
}
40+
41+
// end finalizes the metrics for the current block and updates the registered metrics.
42+
func (d *depthAggregator) end() {
43+
d.mu.Lock()
44+
sum, cnt, min := d.sum, d.cnt, d.min
45+
d.mu.Unlock()
46+
if cnt > 0 {
47+
avgAccessDepthInBlock.Mark(sum / cnt)
48+
minAccessDepthInBlock.Mark(min)
49+
}
50+
}
51+
52+
// StateDepthMetricsStartBlock initializes the depth aggregator for a new block.
53+
func StateDepthMetricsStartBlock() { stateDepthAggregator.start() }
54+
55+
// StateDepthMetricsEndBlock finalizes the depth metrics for the current block.
56+
func StateDepthMetricsEndBlock() { stateDepthAggregator.end() }

0 commit comments

Comments
 (0)