Use a freelist for outgoing messages.

This eliminates about half of the allocations by bytes for gcsfuse's sequential
read benchmark.
geesefs-0-30-9
Aaron Jacobs 2015-07-28 16:36:05 +10:00
commit 314b93c7c0
8 changed files with 281 additions and 109 deletions

View File

@ -79,7 +79,8 @@ type Connection struct {
cancelFuncs map[uint64]func() cancelFuncs map[uint64]func()
// Freelists, serviced by freelists.go. // Freelists, serviced by freelists.go.
inMessages freelist.Freelist // GUARDED_BY(mu) inMessages freelist.Freelist // GUARDED_BY(mu)
outMessages freelist.Freelist // GUARDED_BY(mu)
} }
// State that is maintained for each in-flight op. This is stuffed into the // State that is maintained for each in-flight op. This is stuffed into the
@ -448,17 +449,15 @@ func (c *Connection) Reply(ctx context.Context, opErr error) {
c.errorLogger.Printf("%T error: %v", op, opErr) c.errorLogger.Printf("%T error: %v", op, opErr)
} }
// Send the reply to the kernel. // Send the reply to the kernel, if one is required.
replyMsg := kernelResponse(m.Header().Unique, op, opErr, c.protocol) outMsg := c.kernelResponse(m.Header().Unique, op, opErr)
if replyMsg != nil { if outMsg != nil {
if err := c.writeMessage(replyMsg); err != nil { err := c.writeMessage(outMsg.Bytes())
if c.errorLogger != nil { c.putOutMessage(outMsg)
c.errorLogger.Printf("writeMessage: %v", err)
}
return if err != nil && c.errorLogger != nil {
c.errorLogger.Printf("writeMessage: %v", err)
} }
} }
} }

View File

@ -387,35 +387,31 @@ func convertInMessage(
// Outgoing messages // Outgoing messages
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Return the response that should be sent to the kernel. If the op requires no // Return the response that should be sent to the kernel, or nil if the op
// response, return a nil response. // requires no response.
func kernelResponse( func (c *Connection) kernelResponse(
fuseID uint64, fuseID uint64,
op interface{}, op interface{},
opErr error, opErr error) (m *buffer.OutMessage) {
protocol fusekernel.Protocol) (msg []byte) { // If the user replied with an error, create a response containing just the
// If the user replied with an error, create room enough just for the result // result header with the error filled in. Otherwise create an appropriate
// header and fill it in with an error. Otherwise create an appropriate
// response. // response.
var b buffer.OutMessage
if opErr != nil { if opErr != nil {
b = buffer.NewOutMessage(0) m = c.getOutMessage()
if errno, ok := opErr.(syscall.Errno); ok { if errno, ok := opErr.(syscall.Errno); ok {
b.OutHeader().Error = -int32(errno) m.OutHeader().Error = -int32(errno)
} else { } else {
b.OutHeader().Error = -int32(syscall.EIO) m.OutHeader().Error = -int32(syscall.EIO)
} }
} else { } else {
b = kernelResponseForOp(op, protocol) m = c.kernelResponseForOp(op)
} }
msg = b.Bytes()
// Fill in the rest of the header, if a response is required. // Fill in the rest of the header, if a response is required.
if msg != nil { if m != nil {
h := b.OutHeader() h := m.OutHeader()
h.Unique = fuseID h.Unique = fuseID
h.Len = uint32(len(msg)) h.Len = uint32(m.Len())
} }
return return
@ -423,29 +419,28 @@ func kernelResponse(
// Like kernelResponse, but assumes the user replied with a nil error to the // Like kernelResponse, but assumes the user replied with a nil error to the
// op. Returns a nil response if no response is required. // op. Returns a nil response if no response is required.
func kernelResponseForOp( func (c *Connection) kernelResponseForOp(
op interface{}, op interface{}) (m *buffer.OutMessage) {
protocol fusekernel.Protocol) (b buffer.OutMessage) {
// Create the appropriate output message // Create the appropriate output message
switch o := op.(type) { switch o := op.(type) {
case *fuseops.LookUpInodeOp: case *fuseops.LookUpInodeOp:
size := fusekernel.EntryOutSize(protocol) size := fusekernel.EntryOutSize(c.protocol)
b = buffer.NewOutMessage(size) m = c.getOutMessage()
out := (*fusekernel.EntryOut)(b.Grow(size)) out := (*fusekernel.EntryOut)(m.Grow(size))
convertChildInodeEntry(&o.Entry, out) convertChildInodeEntry(&o.Entry, out)
case *fuseops.GetInodeAttributesOp: case *fuseops.GetInodeAttributesOp:
size := fusekernel.AttrOutSize(protocol) size := fusekernel.AttrOutSize(c.protocol)
b = buffer.NewOutMessage(size) m = c.getOutMessage()
out := (*fusekernel.AttrOut)(b.Grow(size)) out := (*fusekernel.AttrOut)(m.Grow(size))
out.AttrValid, out.AttrValidNsec = convertExpirationTime( out.AttrValid, out.AttrValidNsec = convertExpirationTime(
o.AttributesExpiration) o.AttributesExpiration)
convertAttributes(o.Inode, &o.Attributes, &out.Attr) convertAttributes(o.Inode, &o.Attributes, &out.Attr)
case *fuseops.SetInodeAttributesOp: case *fuseops.SetInodeAttributesOp:
size := fusekernel.AttrOutSize(protocol) size := fusekernel.AttrOutSize(c.protocol)
b = buffer.NewOutMessage(size) m = c.getOutMessage()
out := (*fusekernel.AttrOut)(b.Grow(size)) out := (*fusekernel.AttrOut)(m.Grow(size))
out.AttrValid, out.AttrValidNsec = convertExpirationTime( out.AttrValid, out.AttrValidNsec = convertExpirationTime(
o.AttributesExpiration) o.AttributesExpiration)
convertAttributes(o.Inode, &o.Attributes, &out.Attr) convertAttributes(o.Inode, &o.Attributes, &out.Attr)
@ -454,85 +449,85 @@ func kernelResponseForOp(
// No response. // No response.
case *fuseops.MkDirOp: case *fuseops.MkDirOp:
size := fusekernel.EntryOutSize(protocol) size := fusekernel.EntryOutSize(c.protocol)
b = buffer.NewOutMessage(size) m = c.getOutMessage()
out := (*fusekernel.EntryOut)(b.Grow(size)) out := (*fusekernel.EntryOut)(m.Grow(size))
convertChildInodeEntry(&o.Entry, out) convertChildInodeEntry(&o.Entry, out)
case *fuseops.CreateFileOp: case *fuseops.CreateFileOp:
eSize := fusekernel.EntryOutSize(protocol) eSize := fusekernel.EntryOutSize(c.protocol)
b = buffer.NewOutMessage(eSize + unsafe.Sizeof(fusekernel.OpenOut{})) m = c.getOutMessage()
e := (*fusekernel.EntryOut)(b.Grow(eSize)) e := (*fusekernel.EntryOut)(m.Grow(eSize))
convertChildInodeEntry(&o.Entry, e) convertChildInodeEntry(&o.Entry, e)
oo := (*fusekernel.OpenOut)(b.Grow(unsafe.Sizeof(fusekernel.OpenOut{}))) oo := (*fusekernel.OpenOut)(m.Grow(unsafe.Sizeof(fusekernel.OpenOut{})))
oo.Fh = uint64(o.Handle) oo.Fh = uint64(o.Handle)
case *fuseops.CreateSymlinkOp: case *fuseops.CreateSymlinkOp:
size := fusekernel.EntryOutSize(protocol) size := fusekernel.EntryOutSize(c.protocol)
b = buffer.NewOutMessage(size) m = c.getOutMessage()
out := (*fusekernel.EntryOut)(b.Grow(size)) out := (*fusekernel.EntryOut)(m.Grow(size))
convertChildInodeEntry(&o.Entry, out) convertChildInodeEntry(&o.Entry, out)
case *fuseops.RenameOp: case *fuseops.RenameOp:
b = buffer.NewOutMessage(0) m = c.getOutMessage()
case *fuseops.RmDirOp: case *fuseops.RmDirOp:
b = buffer.NewOutMessage(0) m = c.getOutMessage()
case *fuseops.UnlinkOp: case *fuseops.UnlinkOp:
b = buffer.NewOutMessage(0) m = c.getOutMessage()
case *fuseops.OpenDirOp: case *fuseops.OpenDirOp:
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.OpenOut{})) m = c.getOutMessage()
out := (*fusekernel.OpenOut)(b.Grow(unsafe.Sizeof(fusekernel.OpenOut{}))) out := (*fusekernel.OpenOut)(m.Grow(unsafe.Sizeof(fusekernel.OpenOut{})))
out.Fh = uint64(o.Handle) out.Fh = uint64(o.Handle)
case *fuseops.ReadDirOp: case *fuseops.ReadDirOp:
b = buffer.NewOutMessage(uintptr(len(o.Data))) m = c.getOutMessage()
b.Append(o.Data) m.Append(o.Data)
case *fuseops.ReleaseDirHandleOp: case *fuseops.ReleaseDirHandleOp:
b = buffer.NewOutMessage(0) m = c.getOutMessage()
case *fuseops.OpenFileOp: case *fuseops.OpenFileOp:
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.OpenOut{})) m = c.getOutMessage()
out := (*fusekernel.OpenOut)(b.Grow(unsafe.Sizeof(fusekernel.OpenOut{}))) out := (*fusekernel.OpenOut)(m.Grow(unsafe.Sizeof(fusekernel.OpenOut{})))
out.Fh = uint64(o.Handle) out.Fh = uint64(o.Handle)
case *fuseops.ReadFileOp: case *fuseops.ReadFileOp:
b = buffer.NewOutMessage(uintptr(len(o.Data))) m = c.getOutMessage()
b.Append(o.Data) m.Append(o.Data)
case *fuseops.WriteFileOp: case *fuseops.WriteFileOp:
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.WriteOut{})) m = c.getOutMessage()
out := (*fusekernel.WriteOut)(b.Grow(unsafe.Sizeof(fusekernel.WriteOut{}))) out := (*fusekernel.WriteOut)(m.Grow(unsafe.Sizeof(fusekernel.WriteOut{})))
out.Size = uint32(len(o.Data)) out.Size = uint32(len(o.Data))
case *fuseops.SyncFileOp: case *fuseops.SyncFileOp:
b = buffer.NewOutMessage(0) m = c.getOutMessage()
case *fuseops.FlushFileOp: case *fuseops.FlushFileOp:
b = buffer.NewOutMessage(0) m = c.getOutMessage()
case *fuseops.ReleaseFileHandleOp: case *fuseops.ReleaseFileHandleOp:
b = buffer.NewOutMessage(0) m = c.getOutMessage()
case *fuseops.ReadSymlinkOp: case *fuseops.ReadSymlinkOp:
b = buffer.NewOutMessage(uintptr(len(o.Target))) m = c.getOutMessage()
b.AppendString(o.Target) m.AppendString(o.Target)
case *statFSOp: case *statFSOp:
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.StatfsOut{})) m = c.getOutMessage()
b.Grow(unsafe.Sizeof(fusekernel.StatfsOut{})) m.Grow(unsafe.Sizeof(fusekernel.StatfsOut{}))
case *interruptOp: case *interruptOp:
// No response. // No response.
case *initOp: case *initOp:
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.InitOut{})) m = c.getOutMessage()
out := (*fusekernel.InitOut)(b.Grow(unsafe.Sizeof(fusekernel.InitOut{}))) out := (*fusekernel.InitOut)(m.Grow(unsafe.Sizeof(fusekernel.InitOut{})))
out.Major = o.Library.Major out.Major = o.Library.Major
out.Minor = o.Library.Minor out.Minor = o.Library.Minor

View File

@ -20,14 +20,20 @@ import (
"github.com/jacobsa/fuse/internal/buffer" "github.com/jacobsa/fuse/internal/buffer"
) )
////////////////////////////////////////////////////////////////////////
// buffer.InMessage
////////////////////////////////////////////////////////////////////////
// LOCKS_EXCLUDED(c.mu) // LOCKS_EXCLUDED(c.mu)
func (c *Connection) getInMessage() (m *buffer.InMessage) { func (c *Connection) getInMessage() (x *buffer.InMessage) {
c.mu.Lock() c.mu.Lock()
m = (*buffer.InMessage)(c.inMessages.Get()) x = (*buffer.InMessage)(c.inMessages.Get())
if m == nil {
m = new(buffer.InMessage)
}
c.mu.Unlock() c.mu.Unlock()
if x == nil {
x = new(buffer.InMessage)
}
return return
} }
@ -37,3 +43,28 @@ func (c *Connection) putInMessage(x *buffer.InMessage) {
c.inMessages.Put(unsafe.Pointer(x)) c.inMessages.Put(unsafe.Pointer(x))
c.mu.Unlock() c.mu.Unlock()
} }
////////////////////////////////////////////////////////////////////////
// buffer.OutMessage
////////////////////////////////////////////////////////////////////////
// LOCKS_EXCLUDED(c.mu)
func (c *Connection) getOutMessage() (x *buffer.OutMessage) {
c.mu.Lock()
x = (*buffer.OutMessage)(c.outMessages.Get())
c.mu.Unlock()
if x == nil {
x = new(buffer.OutMessage)
}
x.Reset()
return
}
// LOCKS_EXCLUDED(c.mu)
func (c *Connection) putOutMessage(x *buffer.OutMessage) {
c.mu.Lock()
c.outMessages.Put(unsafe.Pointer(x))
c.mu.Unlock()
}

View File

@ -15,70 +15,112 @@
package buffer package buffer
import ( import (
"fmt"
"log"
"reflect" "reflect"
"unsafe" "unsafe"
"github.com/jacobsa/fuse/internal/fusekernel" "github.com/jacobsa/fuse/internal/fusekernel"
) )
const outHeaderSize = unsafe.Sizeof(fusekernel.OutHeader{})
// We size out messages to be large enough to hold a header for the response
// plus the largest read that may come in.
const outMessageSize = outHeaderSize + MaxReadSize
// OutMessage provides a mechanism for constructing a single contiguous fuse // OutMessage provides a mechanism for constructing a single contiguous fuse
// message from multiple segments, where the first segment is always a // message from multiple segments, where the first segment is always a
// fusekernel.OutHeader message. // fusekernel.OutHeader message.
// //
// Must be created with NewOutMessage. Exception: the zero value has // Must be initialized with Reset.
// Bytes() == nil.
type OutMessage struct { type OutMessage struct {
slice []byte offset uintptr
storage [outMessageSize]byte
} }
// Create a new buffer whose initial contents are a zeroed fusekernel.OutHeader // Make sure alignment works out correctly, at least for the header.
// message, and with room enough to grow by extra bytes. func init() {
func NewOutMessage(extra uintptr) (b OutMessage) { a := unsafe.Alignof(OutMessage{})
const headerSize = unsafe.Sizeof(fusekernel.OutHeader{}) o := unsafe.Offsetof(OutMessage{}.storage)
b.slice = make([]byte, headerSize, headerSize+extra) e := unsafe.Alignof(fusekernel.OutHeader{})
return
if a%e != 0 || o%e != 0 {
log.Panicf("Bad alignment or offset: %d, %d, need %d", a, o, e)
}
} }
// Return a pointer to the header at the start of the buffer. // Reset the message so that it is ready to be used again. Afterward, the
// contents are solely a zeroed header.
func (m *OutMessage) Reset() {
m.offset = outHeaderSize
memclr(unsafe.Pointer(&m.storage), outHeaderSize)
}
// Return a pointer to the header at the start of the message.
func (b *OutMessage) OutHeader() (h *fusekernel.OutHeader) { func (b *OutMessage) OutHeader() (h *fusekernel.OutHeader) {
sh := (*reflect.SliceHeader)(unsafe.Pointer(&b.slice)) h = (*fusekernel.OutHeader)(unsafe.Pointer(&b.storage))
h = (*fusekernel.OutHeader)(unsafe.Pointer(sh.Data))
return return
} }
// Grow the buffer by the supplied number of bytes, returning a pointer to the // Grow the buffer by the supplied number of bytes, returning a pointer to the
// start of the new segment. The sum of the arguments given to Grow must not // start of the new segment, which is zeroed. If there is no space left, return
// exceed the argument given to New when creating the buffer. // the nil pointer.
func (b *OutMessage) Grow(size uintptr) (p unsafe.Pointer) { func (b *OutMessage) Grow(size uintptr) (p unsafe.Pointer) {
sh := (*reflect.SliceHeader)(unsafe.Pointer(&b.slice)) p = b.GrowNoZero(size)
p = unsafe.Pointer(sh.Data + uintptr(sh.Len)) if p != nil {
b.slice = b.slice[:len(b.slice)+int(size)] memclr(p, size)
}
return return
} }
// Equivalent to growing by the length of p, then copying p into the new segment. // Equivalent to Grow, except the new segment is not zeroed. Use with caution!
func (b *OutMessage) Append(p []byte) { func (b *OutMessage) GrowNoZero(size uintptr) (p unsafe.Pointer) {
sh := reflect.SliceHeader{ if outMessageSize-b.offset < size {
Data: uintptr(b.Grow(uintptr(len(p)))), return
Len: len(p),
Cap: len(p),
} }
copy(*(*[]byte)(unsafe.Pointer(&sh)), p) p = unsafe.Pointer(uintptr(unsafe.Pointer(&b.storage)) + b.offset)
b.offset += size
return
} }
// Equivalent to growing by the length of s, then copying s into the new segment. // Equivalent to growing by the length of p, then copying p over the new
func (b *OutMessage) AppendString(s string) { // segment. Panics if there is not enough room available.
sh := reflect.SliceHeader{ func (b *OutMessage) Append(src []byte) {
Data: uintptr(b.Grow(uintptr(len(s)))), p := b.GrowNoZero(uintptr(len(src)))
Len: len(s), if p == nil {
Cap: len(s), panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
} }
copy(*(*[]byte)(unsafe.Pointer(&sh)), s) sh := (*reflect.SliceHeader)(unsafe.Pointer(&src))
memmove(p, unsafe.Pointer(sh.Data), uintptr(sh.Len))
return
}
// Equivalent to growing by the length of s, then copying s over the new
// segment. Panics if there is not enough room available.
func (b *OutMessage) AppendString(src string) {
p := b.GrowNoZero(uintptr(len(src)))
if p == nil {
panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
}
sh := (*reflect.StringHeader)(unsafe.Pointer(&src))
memmove(p, unsafe.Pointer(sh.Data), uintptr(sh.Len))
return
}
// Return the current size of the buffer.
func (b *OutMessage) Len() int {
return int(b.offset)
} }
// Return a reference to the current contents of the buffer. // Return a reference to the current contents of the buffer.
func (b *OutMessage) Bytes() []byte { func (b *OutMessage) Bytes() []byte {
return b.slice return b.storage[:int(b.offset)]
} }

View File

@ -0,0 +1,21 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 buffer
// The maximum read size that we expect to ever see from the kernel, used for
// calculating the size of out messages.
//
// Experimentally determined on OS X.
const MaxReadSize = 1 << 20

View File

@ -0,0 +1,21 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 buffer
// The maximum read size that we expect to ever see from the kernel, used for
// calculating the size of out messages.
//
// For 4 KiB pages, this is 128 KiB (cf. https://goo.gl/HOiEYo)
const MaxReadSize = 1 << 17

View File

@ -0,0 +1,27 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 buffer
import "unsafe"
//go:noescape
// Zero the n bytes starting at p.
func memclr(p unsafe.Pointer, n uintptr)
//go:noescape
// Copy from src to dst, allowing overlap.
func memmove(dst unsafe.Pointer, src unsafe.Pointer, n uintptr)

36
internal/buffer/runtime.s Normal file
View File

@ -0,0 +1,36 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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.
// +build amd64 arm64 ppc64 ppc64le
// Assembly code isn't subject to visibility restrictions, so we can jump
// directly into package runtime.
//
// Technique copied from here:
// https://github.com/golang/go/blob/d8c6dac/src/os/signal/sig.s
#include "textflag.h"
#ifdef GOARCH_ppc64
#define JMP BR
#endif
#ifdef GOARCH_ppc64le
#define JMP BR
#endif
TEXT ·memclr(SB),NOSPLIT,$0-16
JMP runtime·memclr(SB)
TEXT ·memmove(SB),NOSPLIT,$0-24
JMP runtime·memmove(SB)