/
lcache.go
211 lines (186 loc) · 4.29 KB
/
lcache.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Package core provides core metadata and in-cluster API
/*
* Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
*/
package core
import (
"sync"
"time"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/atomic"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/NVIDIA/aistore/cmn/nlog"
"github.com/NVIDIA/aistore/core/meta"
"github.com/NVIDIA/aistore/fs"
"github.com/NVIDIA/aistore/hk"
"github.com/NVIDIA/aistore/memsys"
"github.com/NVIDIA/aistore/sys"
)
const (
oomEvictAtime = time.Minute * 5 // OOM
mpeEvictAtime = time.Minute * 10 // extreme
mphEvictAtime = time.Minute * 20 // high
mpnEvictAtime = time.Hour // normal
iniEvictAtime = mpnEvictAtime / 2 // initial
maxEvictAtime = mpnEvictAtime * 2 // maximum
)
type lchk struct {
cache *sync.Map
// runtime
now time.Time
d time.Duration
totalCnt int64
// stats
evictedCnt int64
flushColdCnt int64
// single entry
running atomic.Bool
}
func regLomCacheWithHK() {
g.lchk.running.Store(false)
hk.Reg("lcache"+hk.NameSuffix, g.lchk.housekeep, iniEvictAtime)
}
//
// evictions
//
func UncacheBck(b *meta.Bck) {
var (
caches = lomCaches()
n = max(sys.NumCPU()/4, 4)
wg = cos.NewLimitedWaitGroup(n, len(caches))
)
for _, lcache := range caches {
wg.Add(1)
go func(cache *sync.Map) {
cache.Range(func(hkey, value any) bool {
lmd := value.(*lmeta)
bck, _ := cmn.ParseUname(lmd.uname)
if bck.Equal((*cmn.Bck)(b)) {
cache.Delete(hkey)
}
return true
})
wg.Done()
}(lcache)
}
wg.Wait()
}
// NOTE: watch https://github.com/golang/go/pull/61702 for `sync.Map.Clear`, likely Go 22
func UncacheMountpath(mi *fs.Mountpath) {
for idx := 0; idx < cos.MultiSyncMapCount; idx++ {
cache := mi.LomCache(idx)
cache.Range(func(hkey any, _ any) bool {
cache.Delete(hkey)
return true
})
}
}
//////////
// lchk //
//////////
func (lchk *lchk) housekeep() (d time.Duration) {
var tag string
d, tag = lchk.mp()
if !lchk.running.CAS(false, true) {
if tag != "" {
nlog.Infof("running now: memory pressure %q, next sched %v", tag, d)
}
return
}
go lchk.evictAll(d /*evict older than*/)
return
}
const termDuration = time.Duration(-1)
func (lchk *lchk) terminating() bool { return lchk.d == termDuration }
func (*lchk) mp() (d time.Duration, tag string) {
p := g.pmm.Pressure()
switch p {
case memsys.OOM:
d = oomEvictAtime
tag = "OOM"
case memsys.PressureExtreme:
d = mpeEvictAtime
tag = "extreme"
case memsys.PressureHigh:
d = mphEvictAtime
tag = "high"
default:
d = mpnEvictAtime
}
return
}
func (lchk *lchk) evictAll(d time.Duration) {
lchk.now = time.Now()
lchk.d = d
lchk.evictedCnt, lchk.flushColdCnt, lchk.totalCnt = 0, 0, 0
// single-threaded: one cache at a time
caches := lomCaches()
if lchk.terminating() {
for _, cache := range caches {
lchk.cache = cache
cache.Range(lchk.fterm)
}
return
}
for _, cache := range caches {
lchk.cache = cache
cache.Range(lchk.frun)
}
if _, tag := lchk.mp(); tag != "" {
nlog.Infof("memory pressure %q, total %d, evicted %d", tag, lchk.totalCnt, lchk.evictedCnt)
}
// stats
g.tstats.Add(LcacheEvictedCount, lchk.evictedCnt)
g.tstats.Add(LcacheFlushColdCount, lchk.flushColdCnt)
lchk.running.Store(false)
}
func (lchk *lchk) fterm(_, value any) bool {
md := value.(*lmeta)
if md.Atime < 0 {
// prefetched, not yet accessed
lchk.flush(md, time.Unix(0, -md.Atime))
return true
}
if md.isDirty() || md.atimefs != uint64(md.Atime) {
lchk.flush(md, time.Unix(0, md.Atime))
}
return true
}
func (lchk *lchk) frun(hkey, value any) bool {
var (
md = value.(*lmeta)
mdTime = md.Atime
)
lchk.totalCnt++
if mdTime < 0 {
// prefetched, not yet accessed
mdTime = -mdTime
}
atime := time.Unix(0, mdTime)
elapsed := lchk.now.Sub(atime)
if elapsed < lchk.d {
return true
}
if md.isDirty() {
if lchk.d > mphEvictAtime && elapsed < maxEvictAtime {
return true
}
lchk.flush(md, atime)
} else if md.atimefs != uint64(md.Atime) {
lchk.flush(md, atime)
}
lchk.cache.Delete(hkey)
lchk.evictedCnt++
return true
}
func (lchk *lchk) flush(md *lmeta, atime time.Time) {
lif := LIF{Uname: md.uname, BID: md.bckID}
lom, err := lif.LOM()
if err == nil {
lom.Lock(true)
lom.flushCold(md, atime)
lom.Unlock(true)
FreeLOM(lom)
lchk.flushColdCnt++
}
}