forked from rfyiamcool/pgcacher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
320 lines (261 loc) · 6.27 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"io/fs"
"log"
"os"
"path"
"runtime"
"sort"
"strings"
"sync"
"github.com/rfyiamcool/pgcacher/pkg/psutils"
pcstat "github.com/tobert/pcstat/pkg"
)
var (
pidFlag, topFlag, workerFlag int
terseFlag, nohdrFlag, jsonFlag, unicodeFlag bool
plainFlag, ppsFlag, bnameFlag bool
)
func init() {
flag.IntVar(&pidFlag, "pid", 0, "show all open maps for the given pid")
flag.IntVar(&topFlag, "top", 0, "show top x cached files in descending order")
flag.IntVar(&workerFlag, "worker", 2, "concurrency workers")
flag.BoolVar(&terseFlag, "terse", false, "show terse output")
flag.BoolVar(&nohdrFlag, "nohdr", false, "omit the header from terse & text output")
flag.BoolVar(&jsonFlag, "json", false, "return data in JSON format")
flag.BoolVar(&unicodeFlag, "unicode", false, "return data with unicode box characters")
flag.BoolVar(&plainFlag, "plain", false, "return data with no box characters")
flag.BoolVar(&ppsFlag, "pps", false, "include the per-page status in JSON output")
flag.BoolVar(&bnameFlag, "bname", false, "convert paths to basename to narrow the output")
}
func main() {
if runtime.GOOS != "linux" {
log.Fatalf("pgcacher only support running on Linux !!!")
}
flag.Parse()
files := flag.Args()
pg := pgcacher{files: files}
if topFlag != 0 {
pg.handleTop(topFlag)
os.Exit(0)
}
if pidFlag != 0 {
pg.appendProcessFiles(pidFlag)
}
if len(pg.files) == 0 {
fmt.Println("files is null ?")
flag.Usage()
os.Exit(1)
}
pg.filterFiles()
stats := pg.getPageCacheStats()
pg.output(stats)
}
type emptyNull struct{}
type pgcacher struct {
files []string
}
func (pg *pgcacher) filterFiles() {
sset := make(map[string]emptyNull, len(pg.files))
for _, file := range pg.files {
file = strings.Trim(file, " ")
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.
pcstat.SwitchMountNs(pidFlag)
// 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 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 < workerFlag; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for file := range queue {
readlink(file)
}
}()
}
wg.Wait()
return out
}
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)
analyse := func(fname string) {
status, err := pcstat.GetPcStatus(fname)
if err != nil {
log.Printf("skipping %q: %v", fname, err)
return
}
// only get filename, trim full dir path of the file.
if bnameFlag {
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 < workerFlag; 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) {
if jsonFlag {
stats.FormatJson(!ppsFlag)
} else if terseFlag {
stats.FormatTerse()
} else if unicodeFlag {
stats.FormatUnicode()
} else if plainFlag {
stats.FormatPlain()
} else {
stats.FormatText()
}
}
func (pg *pgcacher) handleTop(top int) {
// 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 < workerFlag; 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()
stats := pg.getPageCacheStats()
top = min(len(stats), top)
pg.output(stats[:top])
}
func min(x, y int) int {
if x < y {
return x
}
return y
}