Skip to content

Commit

Permalink
posix: Use sync.Pool buffers to copy in large buffers. (minio#3106)
Browse files Browse the repository at this point in the history
These fixes are borrowed from the fixes required for GlusterFS i/o throughput.
  • Loading branch information
harshavardhana authored Oct 27, 2016
1 parent 8871eb8 commit e9c4510
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ language: go

os:
- linux
- osx

osx_image: xcode7.2

env:
- ARCH=x86_64
Expand Down
2 changes: 1 addition & 1 deletion cmd/object-common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
blockSizeV1 = 10 * 1024 * 1024 // 10MiB.

// Staging buffer read size for all internal operations version 1.
readSizeV1 = 128 * 1024 // 128KiB.
readSizeV1 = 1 * 1024 * 1024 // 1MiB.

// Buckets meta prefix.
bucketMetaPrefix = "buckets"
Expand Down
17 changes: 14 additions & 3 deletions cmd/posix-list-dir-nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ import (
"path"
"runtime"
"strings"
"sync"
"syscall"
"unsafe"
)

const (
// readDirentBufSize for syscall.ReadDirent() to hold multiple
// directory entries in one buffer. golang source uses 4096 as
// buffer size whereas we want 25 times larger to save lots of
// buffer size whereas we want 64 times larger to save lots of
// entries to avoid multiple syscall.ReadDirent() call.
readDirentBufSize = 4096 * 25
readDirentBufSize = 4096 * 64
)

// actual length of the byte array from the c - world.
Expand Down Expand Up @@ -106,9 +107,19 @@ func parseDirents(dirPath string, buf []byte) (entries []string, err error) {
return entries, nil
}

var readDirBufPool = sync.Pool{
New: func() interface{} {
b := make([]byte, readDirentBufSize)
return &b
},
}

// Return all the entries at the directory dirPath.
func readDir(dirPath string) (entries []string, err error) {
buf := make([]byte, readDirentBufSize)
bufp := readDirBufPool.Get().(*[]byte)
buf := *bufp
defer readDirBufPool.Put(bufp)

d, err := os.Open(dirPath)
if err != nil {
// File is really not found.
Expand Down
2 changes: 1 addition & 1 deletion cmd/posix-utils_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestUNCPaths(t *testing.T) {
{string(bytes.Repeat([]byte("界"), 85)), true},
// Each path component must be <= 255 bytes long.
{string(bytes.Repeat([]byte("界"), 100)), false},
{`\\p\q\r\s\t`, true},
{`/p/q/r/s/t`, true},
}
// Instantiate posix object to manage a disk
var err error
Expand Down
20 changes: 17 additions & 3 deletions cmd/posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
"syscall"

Expand All @@ -45,6 +46,7 @@ type posix struct {
suppliedDiskPath string
minFreeSpace int64
minFreeInodes int64
pool sync.Pool
}

var errFaultyDisk = errors.New("Faulty disk")
Expand Down Expand Up @@ -114,6 +116,13 @@ func newPosix(diskPath string) (StorageAPI, error) {
diskPath: diskPath,
minFreeSpace: fsMinFreeSpace,
minFreeInodes: fsMinFreeInodesPercent,
// 1MiB buffer pool for posix internal operations.
pool: sync.Pool{
New: func() interface{} {
b := make([]byte, readSizeV1)
return &b
},
},
}
st, err := os.Stat(preparePath(diskPath))
if err != nil {
Expand Down Expand Up @@ -144,7 +153,7 @@ func getDiskInfo(diskPath string) (di disk.Info, err error) {
}

// checkDiskFree verifies if disk path has sufficient minimum free disk space and files.
func (s posix) checkDiskFree() (err error) {
func (s *posix) checkDiskFree() (err error) {
di, err := getDiskInfo(s.diskPath)
if err != nil {
return err
Expand Down Expand Up @@ -584,7 +593,7 @@ func (s *posix) AppendFile(volume, path string, buf []byte) (err error) {
}
// Create top level directories if they don't exist.
// with mode 0777 mkdir honors system umask.
if err = mkdirAll(filepath.Dir(filePath), 0777); err != nil {
if err = mkdirAll(preparePath(slashpath.Dir(filePath)), 0777); err != nil {
// File path cannot be verified since one of the parents is a file.
if isSysErrNotDir(err) {
return errFileAccessDenied
Expand All @@ -609,8 +618,13 @@ func (s *posix) AppendFile(volume, path string, buf []byte) (err error) {
// Close upon return.
defer w.Close()

bufp := s.pool.Get().(*[]byte)

// Reuse buffer.
defer s.pool.Put(bufp)

// Return io.Copy
_, err = io.Copy(w, bytes.NewReader(buf))
_, err = io.CopyBuffer(w, bytes.NewReader(buf), *bufp)
return err
}

Expand Down

0 comments on commit e9c4510

Please sign in to comment.