forked from jackpal/Taipei-Torrent
/
hdcache.go
348 lines (315 loc) · 9.93 KB
/
hdcache.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
// hdcache
package torrent
import (
"encoding/hex"
"io"
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"sort"
"strconv"
"sync/atomic"
"time"
)
//This provider creates an HD cache for each torrent.
//Each time a cache is created or closed, all cache
//are recalculated so they total <= capacity (in MiB).
type HdCacheProvider struct {
capacity int
caches map[string]*HdCache
}
func NewHdCacheProvider(capacity int) CacheProvider {
os.Mkdir(filepath.FromSlash(os.TempDir()+"/taipeitorrent"), 0777)
rc := &HdCacheProvider{capacity, make(map[string]*HdCache)}
return rc
}
func (r *HdCacheProvider) NewCache(infohash string, numPieces int, pieceSize int, torrentLength int64) TorrentCache {
i := uint32(1)
rc := &HdCache{pieceSize: pieceSize, atimes: make([]time.Time, numPieces), boxExists: *NewBitset(numPieces),
isBoxFull: *NewBitset(numPieces), isBoxCommit: *NewBitset(numPieces), isByteSet: make([]Bitset, numPieces),
boxPrefix: filepath.FromSlash(os.TempDir() + "/taipeitorrent/" + hex.EncodeToString([]byte(infohash)) + "-"),
torrentLength: torrentLength, cacheProvider: r, capacity: &i, infohash: infohash}
rc.empty() //clear out any detritus from previous runs
r.caches[infohash] = rc
r.rebalance(true)
return rc
}
//Rebalance the cache capacity allocations; has to be called on each cache creation or deletion.
//'shouldTrim', if true, causes trimCommitted() to be called on all the caches. Recommended if a new cache was created
//because otherwise the old caches would stay over the new capacity until their next WriteAt happens.
func (r *HdCacheProvider) rebalance(shouldTrim bool) {
//Cache size is a diminishing return thing:
//The more of it a torrent has, the less of a difference additional cache makes.
//Thus, instead of scaling the distribution lineraly with torrent size, we'll do it by square-root
log.Println("Rebalancing caches...")
var scalingTotal float64
sqrts := make(map[string]float64)
for i, cache := range r.caches {
sqrts[i] = math.Sqrt(float64(cache.torrentLength))
scalingTotal += sqrts[i]
}
scalingFactor := float64(r.capacity*1024*1024) / scalingTotal
for i, cache := range r.caches {
newCap := int(math.Floor(scalingFactor * sqrts[i] / float64(cache.pieceSize)))
if newCap == 0 {
newCap = 1 //Something's better than nothing!
}
log.Printf("Setting cache '%x' to new capacity %v (%v MiB)", cache.infohash, newCap, float32(newCap*cache.pieceSize)/float32(1024*1024))
cache.setCapacity(newCap)
}
if shouldTrim {
for _, cache := range r.caches {
cache.trimCommitted()
}
}
}
func (r *HdCacheProvider) cacheClosed(infohash string) {
delete(r.caches, infohash)
r.rebalance(false)
}
//'pieceSize' is the size of the average piece
//'capacity' is how many pieces the cache can hold
//'actualUsage' is how many pieces the cache has at the moment
//'atime' is an array of access times for each stored box
//'boxExists' indicates if a box is existent in cache
//'isBoxFull' indicates if a box entirely contains written data
//'isBoxCommit' indicates if a box has been committed to storage
//'isByteSet' for [i] indicates for box 'i' if a byte has been written to
//'boxPrefix' is the partial path to the boxes.
//'torrentLength' is the number of bytes in the torrent
//'cacheProvider' is a pointer to the cacheProvider that created this cache
//'infohash' is the infohash of the torrent
type HdCache struct {
pieceSize int
capacity *uint32 //Access only through getter/setter
actualUsage int
atimes []time.Time
boxExists Bitset
isBoxFull Bitset
isBoxCommit Bitset
isByteSet []Bitset
boxPrefix string
torrentLength int64
cacheProvider *HdCacheProvider
infohash string
}
func (r *HdCache) Close() {
r.cacheProvider.cacheClosed(r.infohash)
r.empty()
}
func (r *HdCache) empty() {
for i := 0; i < r.boxExists.Len(); i++ {
os.Remove(r.boxPrefix + strconv.Itoa(i))
}
}
func (r *HdCache) ReadAt(p []byte, off int64) []chunk {
unfulfilled := make([]chunk, 0)
boxI := int(off / int64(r.pieceSize))
boxOff := int(off % int64(r.pieceSize))
for i := 0; i < len(p); {
if !r.boxExists.IsSet(boxI) { //definitely not in cache
end := len(p[i:])
if end > r.pieceSize-boxOff {
end = r.pieceSize - boxOff
}
if len(unfulfilled) > 0 {
last := unfulfilled[len(unfulfilled)-1]
if last.i+int64(len(last.data)) == off+int64(i) {
unfulfilled = unfulfilled[:len(unfulfilled)-1]
i = int(last.i - off)
end += len(last.data)
}
}
unfulfilled = append(unfulfilled, chunk{off + int64(i), p[i : i+end]})
i += end
} else if r.isBoxFull.IsSet(boxI) { //definitely in cache
box, err := os.Open(r.boxPrefix + strconv.Itoa(boxI))
if err != nil {
log.Println("Error opening cache item we thought we had:", r.boxPrefix+strconv.Itoa(boxI), "error:", err)
r.removeBox(boxI)
continue //loop around without incrementing 'i', forget this ever happened
}
copied, err := box.ReadAt(p[i:], int64(boxOff))
box.Close()
i += copied
if err != nil && err != io.EOF {
log.Println("Error while reading cache item:", r.boxPrefix+strconv.Itoa(boxI), "error:", err)
r.removeBox(boxI)
continue //loop around without incrementing 'i', forget this ever happened
}
} else { //Bah, do it byte by byte.
missing := []*inttuple{&inttuple{-1, -1}}
end := len(p[i:]) + boxOff
if end > r.pieceSize {
end = r.pieceSize
}
box, err := ioutil.ReadFile(r.boxPrefix + strconv.Itoa(boxI))
if err != nil {
log.Println("Error reading cache item we thought we had:", r.boxPrefix+strconv.Itoa(boxI), "error:", err)
r.removeBox(boxI)
continue //loop around without incrementing 'i', forget this ever happened
}
for j := boxOff; j < end; j++ {
if r.isByteSet[boxI].IsSet(j) {
p[i] = box[j]
} else {
lastIT := missing[len(missing)-1]
if lastIT.b == i {
lastIT.b = i + 1
} else {
missing = append(missing, &inttuple{i, i + 1})
}
}
i++
}
for _, intt := range missing[1:] {
unfulfilled = append(unfulfilled, chunk{off + int64(intt.a), p[intt.a:intt.b]})
}
}
boxI++
boxOff = 0
}
return unfulfilled
}
func (r *HdCache) WriteAt(p []byte, off int64) []chunk {
boxI := int(off / int64(r.pieceSize))
boxOff := int(off % int64(r.pieceSize))
for i := 0; i < len(p); {
var box *os.File
var err error
if !r.boxExists.IsSet(boxI) { //box doesn't exist, so we'll make one.
box, err = os.Create(r.boxPrefix + strconv.Itoa(boxI))
if err != nil {
log.Panicln("Couldn't create cache file:", err)
return nil
}
r.boxExists.Set(boxI)
box.Truncate(int64(r.pieceSize))
r.actualUsage++
} else { //box exists, so we'll open it
box, err = os.OpenFile(r.boxPrefix+strconv.Itoa(boxI), os.O_WRONLY, 0777)
if err != nil {
log.Println("Error opening cache item we thought we had:", r.boxPrefix+strconv.Itoa(boxI), "error:", err)
r.removeBox(boxI)
continue //loop around without incrementing 'i', forget this ever happened
}
}
end := r.pieceSize - boxOff
if len(p) < end {
end = len(p)
}
copied, err := box.WriteAt(p[i:end], int64(boxOff))
if err != nil {
log.Panicln("Error at write cache box:", box.Name(), "error:", err)
}
i += copied
box.Close()
r.atimes[boxI] = time.Now()
if copied == r.pieceSize {
r.isBoxFull.Set(boxI)
} else {
if r.isByteSet[boxI].n == 0 {
r.isByteSet[boxI] = *NewBitset(r.pieceSize)
}
for j := boxOff; j < boxOff+copied; j++ {
r.isByteSet[boxI].Set(j)
}
}
boxI++
boxOff = 0
}
if r.actualUsage > r.getCapacity() {
return r.trim()
}
return nil
}
func (r *HdCache) MarkCommitted(piece int) {
if r.boxExists.IsSet(piece) {
r.isBoxFull.Set(piece)
r.isBoxCommit.Set(piece)
r.isByteSet[piece] = *NewBitset(0)
}
}
func (r *HdCache) removeBox(boxI int) {
r.isBoxFull.Clear(boxI)
r.boxExists.Clear(boxI)
r.isBoxCommit.Clear(boxI)
r.isByteSet[boxI] = *NewBitset(0)
err := os.Remove(r.boxPrefix + strconv.Itoa(boxI))
if err != nil {
log.Println("Error removing cache box:", err)
} else {
r.actualUsage--
}
}
func (r *HdCache) getCapacity() int {
return int(atomic.LoadUint32(r.capacity))
}
func (r *HdCache) setCapacity(capacity int) {
atomic.StoreUint32(r.capacity, uint32(capacity))
}
//Trim stuff that's already been committed
//Return true if we got underneath capacity, false if not.
func (r *HdCache) trimCommitted() bool {
for i := 0; i < r.isBoxCommit.Len(); i++ {
if r.isBoxCommit.IsSet(i) {
r.removeBox(i)
}
if r.actualUsage <= r.getCapacity() {
return true
}
}
return false
}
//Trim excess data. Returns any uncommitted chunks that were trimmed
func (r *HdCache) trim() []chunk {
if r.trimCommitted() {
return nil
}
retVal := make([]chunk, 0)
//Still need more space? figure out what's oldest
//RawWrite it to storage, and clear that then
tATA := make([]accessTime, 0, r.actualUsage)
for i, atime := range r.atimes {
if r.boxExists.IsSet(i) {
tATA = append(tATA, accessTime{i, atime})
}
}
sort.Sort(byTime(tATA))
deficit := r.actualUsage - r.getCapacity()
for i := 0; i < deficit; i++ {
deadBox := tATA[i].index
data, err := ioutil.ReadFile(r.boxPrefix + strconv.Itoa(deadBox))
if err != nil {
log.Println("Error reading cache box for trimming:", err)
} else {
if r.isBoxFull.IsSet(deadBox) { //Easy, the whole box has to go
retVal = append(retVal, chunk{int64(deadBox) * int64(r.pieceSize), data})
} else { //Ugh, we'll just trim anything unused from the start and the end, and send that.
off := int64(0)
endData := r.pieceSize
//Trim out any unset bytes at the beginning
for j := 0; j < r.pieceSize; j++ {
if !r.isByteSet[deadBox].IsSet(j) {
off++
} else {
break
}
}
//Trim out any unset bytes at the end
for j := r.pieceSize - 1; j > 0; j-- {
if !r.isByteSet[deadBox].IsSet(j) {
endData--
} else {
break
}
}
retVal = append(retVal, chunk{int64(deadBox)*int64(r.pieceSize) + off, data[off:endData]})
}
}
r.removeBox(deadBox)
}
return retVal
}