-
Notifications
You must be signed in to change notification settings - Fork 11
/
pgcacher.go
417 lines (338 loc) · 7.65 KB
/
pgcacher.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main
import (
"bufio"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path"
"sort"
"strings"
"sync"
"github.com/rfyiamcool/pgcacher/pkg/pcstats"
"github.com/rfyiamcool/pgcacher/pkg/psutils"
)
type emptyNull struct{}
type pgcacher struct {
files []string
leastSize int64
option *option
}
func (pg *pgcacher) ignoreFile(file string) bool {
if pg.option.excludeFiles != "" && wildcardMatch(file, pg.option.excludeFiles) {
return true
}
if pg.option.includeFiles != "" && !wildcardMatch(file, pg.option.includeFiles) {
return true
}
return false
}
func (pg *pgcacher) filterFiles() {
sset := make(map[string]emptyNull, len(pg.files))
for _, file := range pg.files {
file = strings.Trim(file, " ")
if pg.ignoreFile(file) {
continue
}
sset[file] = emptyNull{}
}
// remove duplication.
dups := make([]string, 0, len(sset))
for fname := range sset {
dups = append(dups, fname)
}
pg.files = dups
}
func (pg *pgcacher) appendProcessFiles(pid int) {
pg.files = append(pg.files, pg.getProcessFiles(pid)...)
}
func (pg *pgcacher) getProcessFiles(pid int) []string {
// switch mount namespace for container.
pcstats.SwitchMountNs(pg.option.pid)
// get files of `/proc/{pid}/fd` and `/proc/{pid}/maps`
processFiles := pg.getProcessFdFiles(pid)
processMapFiles := pg.getProcessMaps(pid)
// append
var files []string
files = append(files, processFiles...)
files = append(files, processMapFiles...)
return files
}
func (pg *pgcacher) getProcessMaps(pid int) []string {
fname := fmt.Sprintf("/proc/%d/maps", pid)
f, err := os.Open(fname)
if err != nil {
log.Printf("could not read dir %s, err: %s", fname, err.Error())
return nil
}
defer f.Close()
scanner := bufio.NewScanner(f)
out := make([]string, 0, 20)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
if len(parts) == 6 && strings.HasPrefix(parts[5], "/") {
// found something that looks like a file
out = append(out, parts[5])
}
}
if err := scanner.Err(); err != nil {
log.Fatalf("reading '%s' failed: %s", fname, err)
}
return out
}
func (pg *pgcacher) getProcessFdFiles(pid int) []string {
dpath := fmt.Sprintf("/proc/%d/fd", pid)
files, err := os.ReadDir(dpath)
if err != nil {
log.Printf("could not read dir %s, err: %s", dpath, err.Error())
return nil
}
var (
out = make([]string, 0, len(files))
mu = sync.Mutex{}
)
readlink := func(file fs.DirEntry) {
fpath := fmt.Sprintf("%s/%s", dpath, file.Name())
target, err := os.Readlink(fpath)
if !strings.HasPrefix(target, "/") { // ignore socket or pipe.
return
}
if strings.HasPrefix(target, "/dev") { // ignore devices
return
}
if pg.ignoreFile(target) {
return
}
if err != nil {
log.Printf("can not read link '%s', err: %v\n", fpath, err.Error())
return
}
mu.Lock()
out = append(out, target)
mu.Unlock()
}
// fill files to channel.
queue := make(chan fs.DirEntry, len(files))
for _, file := range files {
queue <- file
}
close(queue)
// handle files concurrently.
wg := sync.WaitGroup{}
for i := 0; i < pg.option.worker; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for file := range queue {
readlink(file)
}
}()
}
wg.Wait()
return out
}
var errLessThanSize = errors.New("the file size is less than the leastSize")
func (pg *pgcacher) getPageCacheStats() PcStatusList {
var (
mu = sync.Mutex{}
wg = sync.WaitGroup{}
stats = make(PcStatusList, 0, len(pg.files))
)
// fill files to queue.
queue := make(chan string, len(pg.files))
for _, fname := range pg.files {
queue <- fname
}
close(queue)
ignoreFunc := func(file *os.File) error {
fs, err := file.Stat()
if err != nil {
return err
}
if pg.leastSize != 0 && fs.Size() < pg.leastSize {
return errLessThanSize
}
return nil
}
analyse := func(fname string) {
status, err := pcstats.GetPcStatus(fname, ignoreFunc)
if err == errLessThanSize {
return
}
if err != nil {
log.Printf("skipping %q: %v", fname, err)
return
}
// only get filename, trim full dir path of the file.
if pg.option.bname {
status.Name = path.Base(fname)
}
// append
mu.Lock()
stats = append(stats, status)
mu.Unlock()
}
// analyse page cache stats of files concurrently.
for i := 0; i < pg.option.worker; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for fname := range queue {
analyse(fname)
}
}()
}
wg.Wait()
sort.Sort(PcStatusList(stats))
return stats
}
func (pg *pgcacher) output(stats PcStatusList, limit int) {
limit = min(len(stats), limit)
stats = stats[:limit]
if pg.option.json {
stats.FormatJson()
} else if pg.option.terse {
stats.FormatTerse()
} else if pg.option.unicode {
stats.FormatUnicode()
} else if pg.option.plain {
stats.FormatPlain()
} else {
stats.FormatText()
}
}
func (pg *pgcacher) handleTop() {
// get all active process.
procs, err := psutils.Processes()
if err != nil || len(procs) == 0 {
log.Fatalf("failed to get processes, err: %v", err)
}
ps := make([]psutils.Process, 0, 50)
for _, proc := range procs {
if proc.RSS() == 0 {
continue
}
ps = append(ps, proc)
}
var (
wg = sync.WaitGroup{}
mu = sync.Mutex{}
queue = make(chan psutils.Process, len(ps))
)
for _, process := range ps {
queue <- process
}
close(queue)
// append open fd of each process.
for i := 0; i < pg.option.worker; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for process := range queue {
files := pg.getProcessFiles(process.Pid())
mu.Lock()
pg.files = append(pg.files, files...)
mu.Unlock()
}
}()
}
wg.Wait()
// filter files
pg.filterFiles()
// get page cache stats of files.
stats := pg.getPageCacheStats()
// print
pg.output(stats, pg.option.limit)
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
func wildcardMatch(s string, p string) bool {
if strings.Contains(s, p) {
return true
}
runeInput := []rune(s)
runePattern := []rune(p)
lenInput := len(runeInput)
lenPattern := len(runePattern)
isMatchingMatrix := make([][]bool, lenInput+1)
for i := range isMatchingMatrix {
isMatchingMatrix[i] = make([]bool, lenPattern+1)
}
isMatchingMatrix[0][0] = true
for i := 1; i < lenInput; i++ {
isMatchingMatrix[i][0] = false
}
if lenPattern > 0 {
if runePattern[0] == '*' {
isMatchingMatrix[0][1] = true
}
}
for j := 2; j <= lenPattern; j++ {
if runePattern[j-1] == '*' {
isMatchingMatrix[0][j] = isMatchingMatrix[0][j-1]
}
}
for i := 1; i <= lenInput; i++ {
for j := 1; j <= lenPattern; j++ {
if runePattern[j-1] == '*' {
isMatchingMatrix[i][j] = isMatchingMatrix[i-1][j] || isMatchingMatrix[i][j-1]
}
if runePattern[j-1] == '?' || runeInput[i-1] == runePattern[j-1] {
isMatchingMatrix[i][j] = isMatchingMatrix[i-1][j-1]
}
}
}
return isMatchingMatrix[lenInput][lenPattern]
}
func walkDirs(dirs []string, maxDepth int) []string {
if len(dirs) == 0 {
return dirs
}
var files []string
for _, dir := range dirs {
fi, err := os.Open(dir)
if err != nil {
files = append(files, dir)
continue
}
fs, err := fi.Stat()
if err != nil {
files = append(files, dir)
continue
}
// is dir
if fs.IsDir() {
files = append(files, walkDir(dir, 0, maxDepth)...)
continue
}
// is file
files = append(files, dir)
}
return files
}
func walkDir(dir string, depth int, maxDepth int) []string {
if depth >= maxDepth {
return nil
}
var files []string
ofiles, err := ioutil.ReadDir(dir)
if err != nil {
return nil
}
for _, file := range ofiles {
curdir := path.Join(dir, file.Name())
if file.IsDir() {
files = append(files, walkDir(curdir, depth+1, maxDepth)...)
continue
}
files = append(files, curdir)
}
return files
}