Repository navigation
Expand file tree
/
Copy pathderived_idle.go
More file actions
148 lines (138 loc) · 4.12 KB
/
Copy pathderived_idle.go
File metadata and controls
148 lines (138 loc) · 4.12 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package usage
import (
"runtime/debug"
"sync/atomic"
"testing"
"time"
)
// derivedIdle is how long what is kept of each chunk by its serial (its
// message keys, match hints, first time and heat before a cut) stays
// after the last pairing or heatmap that read it. Each is read again from
// its chunk on the next ask. They were kept by turns, and with no turn
// while nobody looked they stayed for the life of the process: ~4 MB of
// message keys alone in a real history's idle heap.
const derivedIdle = 3 * time.Minute
var derivedKept struct {
last atomic.Int64
armed atomic.Bool
off bool
}
func init() { derivedKept.off = testing.Testing() }
// derivedUsed notes a read of the per-chunk caches and arms the idle
// check.
func derivedUsed() {
if derivedKept.off {
return
}
derivedKept.last.Store(time.Now().UnixNano())
if derivedKept.armed.CompareAndSwap(false, true) {
time.AfterFunc(derivedIdle, derivedIdleCheck)
}
}
// derivedIdleCheck lets the per-chunk caches go once nothing has read
// them for derivedIdle, or looks again when that will be.
func derivedIdleCheck() {
if derivedKept.off { // a test's timer, after it ended
derivedKept.armed.Store(false)
return
}
if left := derivedIdle - time.Since(time.Unix(0, derivedKept.last.Load())); left > 0 {
time.AfterFunc(left, derivedIdleCheck)
return
}
at := time.Now().UnixNano()
n := dropDerived()
derivedKept.armed.Store(false)
if derivedKept.last.Load() > at && derivedKept.armed.CompareAndSwap(false, true) {
time.AfterFunc(derivedIdle, derivedIdleCheck) // read again meanwhile
}
if n > 0 {
debug.FreeOSMemory()
}
}
// dropDerived lets go of what is kept of every chunk by its serial and
// says how many were kept.
func dropDerived() int {
n := 0
msgKeys.Lock()
n += len(msgKeys.by)
msgKeys.by = nil
msgKeys.Unlock()
matchHints.Lock()
n += len(matchHints.by)
matchHints.by = nil
matchHints.Unlock()
firsts.Lock()
n += len(firsts.by)
firsts.by = map[uint64]chunkFirst{} // written below an unlock, never nil
firsts.Unlock()
heatBefores.Lock()
n += len(heatBefores.by)
heatBefores.by = nil
heatBefores.Unlock()
n += dropEffortPeriods()
n += dropDigestTurns()
return n
}
// dropDigestTurns lets go of the serials sourceDigest met this turn and
// the one before (577 KB of a real history's idle heap). They only order
// which digests heat-digests.bin keeps first when there are more than it
// holds; what was made is written out before they go.
func dropDigestTurns() int {
sourceDigestsSave()
d := &sourceDigests
d.Lock()
defer d.Unlock()
if len(d.made) != 0 { // made meanwhile: written with them next time
return 0
}
n := len(d.by) + len(d.last)
d.by, d.last = nil, nil
return n
}
// dropEffortPeriods lets go of every period's kept CodexModelEffort answer
// but one being built: each thread's consumption by model, ~7 MB of a real
// history's idle heap after one visit to the Sessions page. The next ask
// builds it again, waiting for it as a first ask does. The effort before
// the last few days (effortOlds, ~2 MB more) goes too when no build is
// under way: it is in effort-v1.json, read back by the next build.
func dropEffortPeriods() int {
n := 0
if codexEffortBuilding.TryLock() {
n += len(effortOlds)
clear(effortOlds)
codexEffortBuilding.Unlock()
}
codexEffort.Lock()
defer codexEffort.Unlock()
for p, e := range codexEffort.periods {
if e.building == nil {
delete(codexEffort.periods, p)
n++
}
}
return n
}
// ForgetPages lets go at once of what the Requests page and its heatmap
// keep for their next ask: the chunks (as requestForget does) and what is
// kept of each by its serial (as derivedIdle does). The app calls it once
// its page has gone away, closed or hidden, so that what a visit read
// goes in seconds, not the 7 minutes the idle timers take; the next visit
// reads the chunks back from the file of them. Says whether anything went.
func ForgetPages() bool {
idx := &requestCache
idx.Lock()
n := len(idx.chunks)
if idx.idle != nil {
idx.idle.Stop()
idx.idle = nil
}
if idx.forget != nil {
idx.forget.Stop()
idx.forget = nil
}
idx.trim(0)
idx.Unlock()
n += dropDerived()
return n > 0
}