forked from shibukawa/git4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mwindow.go
190 lines (167 loc) · 4.22 KB
/
mwindow.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
package git4go
import (
"errors"
"github.com/edsrzf/mmap-go"
"os"
"runtime"
"strings"
"sync"
)
var mwindow_windowSize = 1024 * 1024 * SelectByArch(32, 1024)
var mwindow_mappedLimit = 1024 * 1024 * SelectByArch(256, 8192)
var memCtl MWindowCtl = MWindowCtl{}
var packCache map[string]*PackFile = map[string]*PackFile{}
var mwindowMutex sync.Mutex
type MWindow struct {
windowMap mmap.MMap
offset uint64
lastUsed uint64
}
func mwindowFinalizer(w *MWindow) {
mwindowMutex.Lock()
defer mwindowMutex.Unlock()
w.windowMap.Unmap()
}
func (w *MWindow) contains(offset uint64) bool {
return w.offset <= offset && offset <= (w.offset+uint64(len(w.windowMap)))
}
type MWindowFile struct {
windows []*MWindow
file *os.File
size uint64
}
func (mwf *MWindowFile) Open(offset, extra uint64) ([]byte, error) {
mwindowMutex.Lock()
defer mwindowMutex.Unlock()
var w *MWindow
for _, existingWindow := range mwf.windows {
if existingWindow.contains(offset) && existingWindow.contains(offset+extra) {
w = existingWindow
break
}
}
if w == nil {
var err error
w, err = mwf.newWindow(offset)
if err != nil {
return nil, err
}
mwf.windows = append([]*MWindow{w}, mwf.windows...)
}
w.lastUsed = memCtl.usedCtr
memCtl.usedCtr++
offset -= w.offset
return w.windowMap[offset:], nil
}
func (mwf *MWindowFile) freeAll() {
mwindowMutex.Lock()
defer mwindowMutex.Unlock()
mwf.freeAllLocked()
}
func (mwf *MWindowFile) newWindow(offset uint64) (*MWindow, error) {
wAlign := mwindow_windowSize / 2
w := &MWindow{
offset: (offset / wAlign) * wAlign,
}
length := mwf.size - w.offset
if length > mwindow_windowSize {
length = mwindow_windowSize
}
memCtl.mapped += length
for mwindow_mappedLimit < memCtl.mapped && mwf.closeLru() == nil {
/* nop */
}
mmapObj, err := mmap.MapRegion(mwf.file, int(mwf.size), 0, mmap.RDONLY, int64(w.offset))
if err != nil {
return nil, err
}
w.windowMap = mmapObj
runtime.SetFinalizer(w, mwindowFinalizer)
memCtl.mmapCalls++
memCtl.openWindow++
if memCtl.mapped > memCtl.peakMapped {
memCtl.peakMapped = memCtl.mapped
}
if memCtl.openWindow > memCtl.peakOpenWindows {
memCtl.peakOpenWindows = memCtl.openWindow
}
return w, nil
}
func (mwf *MWindowFile) freeAllLocked() {
for i, w := range memCtl.windowFiles {
if w == mwf {
newFiles := memCtl.windowFiles[:i]
if i != len(memCtl.windowFiles)-1 {
newFiles = append(newFiles, memCtl.windowFiles[i+1:]...)
}
memCtl.windowFiles = newFiles
break
}
}
for _, window := range mwf.windows {
memCtl.mapped -= uint64(len(window.windowMap))
memCtl.openWindow--
window.windowMap.Unmap()
}
}
func (mwf *MWindowFile) register() {
mwindowMutex.Lock()
defer mwindowMutex.Unlock()
memCtl.windowFiles = append(memCtl.windowFiles)
}
func (mwf *MWindowFile) unregister() {
mwindowMutex.Lock()
defer mwindowMutex.Unlock()
for i, w := range memCtl.windowFiles {
if w == mwf {
newFiles := memCtl.windowFiles[:i]
if i != len(memCtl.windowFiles)-1 {
newFiles = append(newFiles, memCtl.windowFiles[i+1:]...)
}
break
}
}
}
func (mwf *MWindowFile) scanLru(lruWindow **MWindow, lruFile **MWindowFile, lruIndex *int) {
for i, window := range mwf.windows {
if (*lruWindow) == nil || window.lastUsed < (*lruWindow).lastUsed {
*lruWindow = window
*lruFile = mwf
*lruIndex = i
}
}
}
func (mwf *MWindowFile) closeLru() error {
var lruWindow *MWindow
var lruFile *MWindowFile
var lruIndex int
if len(mwf.windows) != 0 {
mwf.scanLru(&lruWindow, &lruFile, &lruIndex)
}
var currentWindowFile *MWindowFile
for _, currentWindowFile = range memCtl.windowFiles {
currentWindowFile.scanLru(&lruWindow, &lruFile, &lruIndex)
}
if lruWindow == nil {
return errors.New("Failed to close memory window. Couldn't find LRU")
}
memCtl.mapped -= uint64(len(lruWindow.windowMap))
lruFile.windows = append(lruFile.windows[:lruIndex], lruFile.windows[lruIndex+1:]...)
return nil
}
type MWindowCtl struct {
mapped uint64
openWindow uint64
mmapCalls uint64
peakOpenWindows uint64
peakMapped uint64
usedCtr uint64
windowFiles []*MWindowFile
}
func SelectByArch(for32, for64 uint64) uint64 {
if strings.HasSuffix(runtime.GOARCH, "64") {
return for64
} else {
return for32
}
}