Commit b55cc8ad authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

fuse: use sync.Pool for the buffers that read from the kernel.

parent 9d459bc5
...@@ -39,9 +39,12 @@ type Server struct { ...@@ -39,9 +39,12 @@ type Server struct {
started chan struct{} started chan struct{}
reqPool sync.Pool // Pool for request structs.
reqPool sync.Pool
// Pool for raw requests data
readPool sync.Pool
reqMu sync.Mutex reqMu sync.Mutex
readPool [][]byte
reqReaders int reqReaders int
outstandingReadBufs int outstandingReadBufs int
kernelSettings InitIn kernelSettings InitIn
...@@ -140,6 +143,7 @@ func NewServer(fs RawFileSystem, mountPoint string, opts *MountOptions) (*Server ...@@ -140,6 +143,7 @@ func NewServer(fs RawFileSystem, mountPoint string, opts *MountOptions) (*Server
opts: &o, opts: &o,
} }
ms.reqPool.New = func() interface{} { return new(request) } ms.reqPool.New = func() interface{} { return new(request) }
ms.readPool.New = func() interface{} { return make([]byte, o.MaxWrite+PAGESIZE) }
optStrs := opts.Options optStrs := opts.Options
if opts.AllowOther { if opts.AllowOther {
optStrs = append(optStrs, "allow_other") optStrs = append(optStrs, "allow_other")
...@@ -182,11 +186,10 @@ func (ms *Server) DebugData() string { ...@@ -182,11 +186,10 @@ func (ms *Server) DebugData() string {
var r int var r int
ms.reqMu.Lock() ms.reqMu.Lock()
r = len(ms.readPool) + ms.reqReaders r = ms.reqReaders
ms.reqMu.Unlock() ms.reqMu.Unlock()
s += fmt.Sprintf(" read buffers: %d (sz %d )", s += fmt.Sprintf(" readers: %d", r)
r, ms.opts.MaxWrite/PAGESIZE+1)
return s return s
} }
...@@ -196,21 +199,13 @@ const _MAX_READERS = 2 ...@@ -196,21 +199,13 @@ const _MAX_READERS = 2
// Returns a new request, or error. In case exitIdle is given, returns // Returns a new request, or error. In case exitIdle is given, returns
// nil, OK if we have too many readers already. // nil, OK if we have too many readers already.
func (ms *Server) readRequest(exitIdle bool) (req *request, code Status) { func (ms *Server) readRequest(exitIdle bool) (req *request, code Status) {
var dest []byte
ms.reqMu.Lock() ms.reqMu.Lock()
if ms.reqReaders > _MAX_READERS { if ms.reqReaders > _MAX_READERS {
ms.reqMu.Unlock() ms.reqMu.Unlock()
return nil, OK return nil, OK
} }
req = ms.reqPool.Get().(*request) req = ms.reqPool.Get().(*request)
l := len(ms.readPool) dest := ms.readPool.Get().([]byte)
if l > 0 {
dest = ms.readPool[l-1]
ms.readPool = ms.readPool[:l-1]
} else {
dest = make([]byte, ms.opts.MaxWrite+PAGESIZE)
}
ms.outstandingReadBufs++ ms.outstandingReadBufs++
ms.reqReaders++ ms.reqReaders++
ms.reqMu.Unlock() ms.reqMu.Unlock()
...@@ -233,7 +228,7 @@ func (ms *Server) readRequest(exitIdle bool) (req *request, code Status) { ...@@ -233,7 +228,7 @@ func (ms *Server) readRequest(exitIdle bool) (req *request, code Status) {
ms.reqMu.Lock() ms.reqMu.Lock()
if !gobbled { if !gobbled {
ms.outstandingReadBufs-- ms.outstandingReadBufs--
ms.readPool = append(ms.readPool, dest) ms.readPool.Put(dest)
dest = nil dest = nil
} }
ms.reqReaders-- ms.reqReaders--
...@@ -258,9 +253,9 @@ func (ms *Server) returnRequest(req *request) { ...@@ -258,9 +253,9 @@ func (ms *Server) returnRequest(req *request) {
req.clear() req.clear()
ms.reqMu.Lock() ms.reqMu.Lock()
if req.bufferPoolOutputBuf != nil { if req.bufferPoolOutputBuf != nil {
ms.readPool = append(ms.readPool, req.bufferPoolInputBuf) ms.readPool.Put(req.bufferPoolInputBuf)
ms.outstandingReadBufs--
req.bufferPoolInputBuf = nil req.bufferPoolInputBuf = nil
ms.outstandingReadBufs--
} }
ms.reqPool.Put(req) ms.reqPool.Put(req)
ms.reqMu.Unlock() ms.reqMu.Unlock()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment