-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagecacheutil.go
170 lines (149 loc) · 3.66 KB
/
pagecacheutil.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
package main
import (
"flag"
"fmt"
"github.com/dotslash/pagecacheutil/oscompat"
. "github.com/dotslash/pagecacheutil/util"
"github.com/fatih/color"
"os"
"syscall"
"unsafe"
)
var verbose = flag.Bool("v", false, "Verbose mode")
var evict = flag.Bool("e", false, "Evict the file")
var touch = flag.Bool("t", false, "Touch the file")
func handleFile(fname string) {
file, err, fstat := openFile(fname)
DieOnErr(err)
defer tryClose(file)
memAddr, err := mmapSyscall(fstat, file)
DieOnErr(err)
defer tryMunmapSyscall(memAddr, fstat, fname)
isPageCached, err := mincoreSyscall(memAddr, fstat)
DieOnErr(err)
printMincoreRes(isPageCached, fstat)
if *touch {
touchFile(fstat, memAddr)
}
if *evict {
oscompat.EvictFile(file, fstat, memAddr)
}
if *touch || *evict {
isPageCached, err = mincoreSyscall(memAddr, fstat)
DieOnErr(err)
printMincoreRes(isPageCached, fstat)
}
}
func printMincoreRes(isPageCached []bool, fstat os.FileInfo) {
inCache := 0
greenBg := color.New(color.BgHiGreen, color.FgBlack)
yellowBg := color.New(color.BgYellow, color.FgBlack)
redBg := color.New(color.BgWhite, color.FgBlack)
reset := color.New(color.Reset)
width := 50
if len(isPageCached) < width {
width = len(isPageCached)
}
groupSize := len(isPageCached) / width
for i := 0; i < len(isPageCached); i += groupSize {
totInGroup := 0
hitsInGroup := 0
for ; totInGroup < groupSize && i+totInGroup < len(isPageCached); totInGroup++ {
if isPageCached[i+totInGroup] {
hitsInGroup++
}
}
inCache += hitsInGroup
if *verbose {
if totInGroup == hitsInGroup {
greenBg.Print("O")
} else if hitsInGroup != 0 {
yellowBg.Print("o")
} else {
redBg.Print("x")
}
}
}
if *verbose {
reset.Println()
}
fmt.Printf("Pages in cache for %v: %v/%v\n", fstat.Name(), inCache, len(isPageCached))
}
func tryMunmapSyscall(memAddr uintptr, fstat os.FileInfo, fname string) {
_, _, err := syscall.Syscall(
syscall.SYS_MUNMAP,
memAddr,
uintptr(fstat.Size()),
0,
)
if err != 0 {
fmt.Printf("MUNMAP failed for %v with err:%v \n", fname, err)
}
}
func mmapSyscall(fstat os.FileInfo, file *os.File) (uintptr, error) {
memAddr, _, err := syscall.Syscall6(
syscall.SYS_MMAP, 0,
uintptr(fstat.Size()),
syscall.PROT_READ,
syscall.MAP_SHARED,
file.Fd(),
0,
)
if err != 0 {
return 0, err
}
return memAddr, nil
}
func mincoreSyscall(mmapAddr uintptr, fstat os.FileInfo) ([]bool, error) {
pageSize := int64(syscall.Getpagesize())
numPages := (fstat.Size() + pageSize - 1) / pageSize
mincoreRes := make([]byte, numPages)
_, _, err := syscall.Syscall(
syscall.SYS_MINCORE,
mmapAddr,
uintptr(fstat.Size()),
uintptr(unsafe.Pointer(&mincoreRes[0])),
)
if err != 0 {
return nil, err
}
isPageCached := make([]bool, numPages)
for i := range mincoreRes {
isPageCached[i] = mincoreRes[i] != 0
}
return isPageCached, nil
}
func openFile(fname string) (*os.File, error, os.FileInfo) {
f, err := os.Open(fname)
DieOnErr(err)
stat, err := f.Stat()
DieOnErr(err)
return f, err, stat
}
func tryClose(f *os.File) {
if err := f.Close(); err != nil {
fmt.Printf("Failed to close %v err:%v\n", f.Name(), err)
}
}
func touchFile(fstat os.FileInfo, memAddr uintptr) {
dummy, mod := 0, (10000*10000)+7
// Slice memory layout
// Taken from syscall/syscall_unix.go
var sl = struct {
addr uintptr
len int
cap int
}{memAddr, int(fstat.Size()), int(fstat.Size())}
membytes := *(*[]byte)(unsafe.Pointer(&sl))
for _, membyte := range membytes {
dummy = (dummy + int(membyte)) % mod
}
fmt.Printf("touched %v contentHash:%v\n", fstat.Name(), dummy)
}
func main() {
flag.Parse()
files := flag.Args()
for _, f := range files {
handleFile(f)
}
}