forked from kahing/goofys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_pool.go
493 lines (399 loc) · 9.27 KB
/
buffer_pool.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright 2015 - 2017 Ka-Hing Cheung
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
. "github.com/kahing/goofys/api/common"
"io"
"runtime"
"runtime/debug"
"sync"
"github.com/jacobsa/fuse"
"github.com/shirou/gopsutil/mem"
)
type BufferPool struct {
mu sync.Mutex
cond *sync.Cond
numBuffers uint64
maxBuffers uint64
totalBuffers uint64
computedMaxbuffers uint64
pool *sync.Pool
}
const BUF_SIZE = 5 * 1024 * 1024
func maxMemToUse(buffersNow uint64) uint64 {
m, err := mem.VirtualMemory()
if err != nil {
panic(err)
}
availableMem, err := getCgroupAvailableMem()
if err != nil {
log.Debugf("amount of available memory from cgroup is: %v", availableMem/1024/1024)
}
if err != nil || availableMem < 0 || availableMem > m.Available {
availableMem = m.Available
}
log.Debugf("amount of available memory: %v", availableMem/1024/1024)
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
log.Debugf("amount of allocated memory: %v %v", ms.Sys/1024/1024, ms.Alloc/1024/1024)
max := uint64(availableMem+ms.Sys) / 2
maxbuffers := MaxUInt64(max/BUF_SIZE, 1)
log.Debugf("using up to %v %vMB buffers, now is %v", maxbuffers, BUF_SIZE/1024/1024, buffersNow)
return maxbuffers
}
func rounduUp(size uint64, pageSize int) int {
return pages(size, pageSize) * pageSize
}
func pages(size uint64, pageSize int) int {
return int((size + uint64(pageSize) - 1) / uint64(pageSize))
}
func (pool BufferPool) Init() *BufferPool {
pool.cond = sync.NewCond(&pool.mu)
pool.computedMaxbuffers = pool.maxBuffers
pool.pool = &sync.Pool{New: func() interface{} {
return make([]byte, 0, BUF_SIZE)
}}
return &pool
}
// for testing
func NewBufferPool(maxSizeGlobal uint64) *BufferPool {
pool := BufferPool{maxBuffers: maxSizeGlobal / BUF_SIZE}.Init()
return pool
}
func (pool *BufferPool) RequestBuffer() (buf []byte) {
return pool.RequestMultiple(BUF_SIZE, true)[0]
}
func (pool *BufferPool) recomputeBufferLimit() {
if pool.maxBuffers == 0 {
pool.computedMaxbuffers = maxMemToUse(pool.numBuffers)
if pool.computedMaxbuffers == 0 {
panic("OOM")
}
}
}
func (pool *BufferPool) RequestMultiple(size uint64, block bool) (buffers [][]byte) {
nPages := pages(size, BUF_SIZE)
pool.mu.Lock()
defer pool.mu.Unlock()
if pool.totalBuffers%10 == 0 {
pool.recomputeBufferLimit()
}
bufferLog.Debugf("requesting %v", size)
for pool.numBuffers+uint64(nPages) > pool.computedMaxbuffers {
if block {
if pool.numBuffers == 0 {
pool.MaybeGC()
pool.recomputeBufferLimit()
if pool.numBuffers+uint64(nPages) > pool.computedMaxbuffers {
// we don't have any in use buffers, and we've made attempts to
// free memory AND correct our limits, yet we still can't allocate.
// it's likely that we are simply asking for too much
log.Errorf("Unable to allocate %d bytes, limit is %d bytes",
nPages*BUF_SIZE, pool.computedMaxbuffers*BUF_SIZE)
panic("OOM")
}
}
pool.cond.Wait()
} else {
return
}
}
for i := 0; i < nPages; i++ {
pool.numBuffers++
pool.totalBuffers++
buf := pool.pool.Get()
buffers = append(buffers, buf.([]byte))
}
return
}
func (pool *BufferPool) MaybeGC() {
if pool.numBuffers == 0 {
debug.FreeOSMemory()
}
}
func (pool *BufferPool) Free(buf []byte) {
bufferLog.Debugf("returning %v", len(buf))
pool.mu.Lock()
defer pool.mu.Unlock()
buf = buf[:0]
pool.pool.Put(buf)
pool.numBuffers--
pool.cond.Signal()
}
var mbufLog = GetLogger("mbuf")
type MBuf struct {
pool *BufferPool
buffers [][]byte
rbuf int
wbuf int
rp int
wp int
}
func (mb MBuf) Init(h *BufferPool, size uint64, block bool) *MBuf {
mb.pool = h
if size != 0 {
mb.buffers = h.RequestMultiple(size, block)
if mb.buffers == nil {
return nil
}
}
return &mb
}
func (mb *MBuf) Len() (length int) {
for i := mb.rbuf; i < int(len(mb.buffers)); i++ {
var bufSize int
var start int
if i == mb.wbuf {
bufSize = mb.wp
} else {
bufSize = int(len(mb.buffers[i]))
}
if i == mb.rbuf {
start = mb.rp
} else {
start = 0
}
length += bufSize - start
}
return
}
// seek only seeks the reader
func (mb *MBuf) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0: // relative to beginning
if offset == 0 {
mb.rbuf = 0
mb.rp = 0
return 0, nil
}
case 1: // relative to current position
if offset == 0 {
for i := 0; i < mb.rbuf; i++ {
offset += int64(len(mb.buffers[i]))
}
offset += int64(mb.rp)
return offset, nil
}
case 2: // relative to the end
if offset == 0 {
for i := 0; i < len(mb.buffers); i++ {
offset += int64(len(mb.buffers[i]))
}
mb.rbuf = len(mb.buffers)
mb.rp = 0
return offset, nil
}
}
log.Errorf("Seek %d %d", offset, whence)
panic(fuse.EINVAL)
return 0, fuse.EINVAL
}
func (mb *MBuf) Read(p []byte) (n int, err error) {
if mb.rbuf == mb.wbuf && mb.rp == mb.wp {
err = io.EOF
return
}
if mb.rp == cap(mb.buffers[mb.rbuf]) {
mb.rbuf++
mb.rp = 0
}
if mb.rbuf == len(mb.buffers) {
err = io.EOF
return
} else if mb.rbuf > len(mb.buffers) {
panic("mb.cur > len(mb.buffers)")
}
n = copy(p, mb.buffers[mb.rbuf][mb.rp:])
mb.rp += n
return
}
func (mb *MBuf) Full() bool {
return mb.buffers == nil || (mb.wp == cap(mb.buffers[mb.wbuf]) && mb.wbuf+1 == len(mb.buffers))
}
func (mb *MBuf) Write(p []byte) (n int, err error) {
b := mb.buffers[mb.wbuf]
if mb.wp == cap(b) {
if mb.wbuf+1 == len(mb.buffers) {
return
}
mb.wbuf++
b = mb.buffers[mb.wbuf]
mb.wp = 0
} else if mb.wp > cap(b) {
panic("mb.wp > cap(b)")
}
n = copy(b[mb.wp:cap(b)], p)
mb.wp += n
// resize the buffer to account for what we just read
mb.buffers[mb.wbuf] = mb.buffers[mb.wbuf][:mb.wp]
return
}
func (mb *MBuf) WriteFrom(r io.Reader) (n int, err error) {
b := mb.buffers[mb.wbuf]
if mb.wp == cap(b) {
if mb.wbuf+1 == len(mb.buffers) {
return
}
mb.wbuf++
b = mb.buffers[mb.wbuf]
mb.wp = 0
} else if mb.wp > cap(b) {
panic("mb.wp > cap(b)")
}
n, err = r.Read(b[mb.wp:cap(b)])
mb.wp += n
// resize the buffer to account for what we just read
mb.buffers[mb.wbuf] = mb.buffers[mb.wbuf][:mb.wp]
return
}
func (mb *MBuf) Reset() {
mb.rbuf = 0
mb.wbuf = 0
mb.rp = 0
mb.wp = 0
}
func (mb *MBuf) Close() error {
mb.Free()
return nil
}
func (mb *MBuf) Free() {
for _, b := range mb.buffers {
mb.pool.Free(b)
}
mb.buffers = nil
}
var bufferLog = GetLogger("buffer")
type Buffer struct {
mu sync.Mutex
cond *sync.Cond
buf *MBuf
reader io.ReadCloser
err error
}
type ReaderProvider func() (io.ReadCloser, error)
func (b Buffer) Init(buf *MBuf, r ReaderProvider) *Buffer {
b.buf = buf
b.cond = sync.NewCond(&b.mu)
go func() {
b.readLoop(r)
}()
return &b
}
func (b *Buffer) readLoop(r ReaderProvider) {
for {
b.mu.Lock()
if b.reader == nil {
b.reader, b.err = r()
b.cond.Broadcast()
if b.err != nil {
b.mu.Unlock()
break
}
}
if b.buf == nil {
// buffer was drained
b.mu.Unlock()
break
}
nread, err := b.buf.WriteFrom(b.reader)
if err != nil {
b.err = err
b.mu.Unlock()
break
}
bufferLog.Debugf("wrote %v into buffer", nread)
if nread == 0 {
b.reader.Close()
b.mu.Unlock()
break
}
b.mu.Unlock()
// if we get here we've read _something_, bounce this goroutine
// to allow another one to read
runtime.Gosched()
}
bufferLog.Debugf("<-- readLoop()")
}
func (b *Buffer) readFromStream(p []byte) (n int, err error) {
bufferLog.Debugf("reading %v from stream", len(p))
n, err = b.reader.Read(p)
if n != 0 && err == io.ErrUnexpectedEOF {
err = nil
} else {
bufferLog.Debugf("read %v from stream", n)
}
return
}
func (b *Buffer) Read(p []byte) (n int, err error) {
bufferLog.Debugf("Buffer.Read(%v)", len(p))
b.mu.Lock()
defer b.mu.Unlock()
for b.reader == nil && b.err == nil {
bufferLog.Debugf("waiting for stream")
b.cond.Wait()
}
// we could have received the err before Read was called
if b.reader == nil {
if b.err == nil {
panic("reader and err are both nil")
}
err = b.err
return
}
if b.buf != nil {
bufferLog.Debugf("reading %v from buffer", len(p))
n, err = b.buf.Read(p)
if n == 0 {
b.buf.Free()
b.buf = nil
bufferLog.Debugf("drained buffer")
n, err = b.readFromStream(p)
} else {
bufferLog.Debugf("read %v from buffer", n)
}
} else if b.err != nil {
err = b.err
} else {
n, err = b.readFromStream(p)
}
return
}
func (b *Buffer) ReInit(r ReaderProvider) {
b.mu.Lock()
defer b.mu.Unlock()
if b.reader != nil {
b.reader.Close()
b.reader = nil
}
if b.buf != nil {
b.buf.Reset()
}
b.err = nil
go func() {
b.readLoop(r)
}()
}
func (b *Buffer) Close() (err error) {
bufferLog.Debugf("Buffer.Close()")
b.mu.Lock()
defer b.mu.Unlock()
if b.reader != nil {
err = b.reader.Close()
}
if b.buf != nil {
b.buf.Free()
b.buf = nil
}
return
}