-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
tracker.go
109 lines (97 loc) · 3.42 KB
/
tracker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2022 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 tracers
import (
"fmt"
"sync"
)
// stateTracker is an auxiliary tool used to cache the release functions of all
// used trace states, and to determine whether the creation of trace state needs
// to be paused in case there are too many states waiting for tracing.
type stateTracker struct {
limit int // Maximum number of states allowed waiting for tracing
oldest uint64 // The number of the oldest state which is still using for trace
used []bool // List of flags indicating whether the trace state has been used up
releases []StateReleaseFunc // List of trace state release functions waiting to be called
cond *sync.Cond
lock *sync.RWMutex
}
// newStateTracker initializes the tracker with provided state limits and
// the number of the first state that will be used.
func newStateTracker(limit int, oldest uint64) *stateTracker {
lock := new(sync.RWMutex)
return &stateTracker{
limit: limit,
oldest: oldest,
used: make([]bool, limit),
cond: sync.NewCond(lock),
lock: lock,
}
}
// releaseState marks the state specified by the number as released and caches
// the corresponding release functions internally.
func (t *stateTracker) releaseState(number uint64, release StateReleaseFunc) {
t.lock.Lock()
defer t.lock.Unlock()
// Set the state as used, the corresponding flag is indexed by
// the distance between the specified state and the oldest state
// which is still using for trace.
t.used[int(number-t.oldest)] = true
// If the oldest state is used up, update the oldest marker by moving
// it to the next state which is not used up.
if number == t.oldest {
var count int
for _, used := range t.used {
if !used {
break
}
count += 1
}
t.oldest += uint64(count)
copy(t.used, t.used[count:])
// Clean up the array tail since they are useless now.
for i := t.limit - count; i < t.limit; i++ {
t.used[i] = false
}
// Fire the signal to all waiters that oldest marker is updated.
t.cond.Broadcast()
}
t.releases = append(t.releases, release)
}
// callReleases invokes all cached release functions.
func (t *stateTracker) callReleases() {
t.lock.Lock()
defer t.lock.Unlock()
for _, release := range t.releases {
release()
}
t.releases = t.releases[:0]
}
// wait blocks until the accumulated trace states are less than the limit.
func (t *stateTracker) wait(number uint64) error {
t.lock.Lock()
defer t.lock.Unlock()
for {
if number < t.oldest {
return fmt.Errorf("invalid state number %d head %d", number, t.oldest)
}
if number < t.oldest+uint64(t.limit) {
// number is now within limit, wait over
return nil
}
t.cond.Wait()
}
}