Skip to content

Commit

Permalink
avoid Go based io.Reader via poll.FD (#11)
Browse files Browse the repository at this point in the history
instead directly deal with 'fd's to get
the real disk performance
  • Loading branch information
harshavardhana authored Nov 3, 2023
1 parent b21eebc commit cc79779
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions pkg/dperf/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand All @@ -38,7 +37,7 @@ import (

// odirectReader - to support O_DIRECT reads for erasure backends.
type odirectReader struct {
File *os.File
fd int
Bufp *[]byte
buf []byte
err error
Expand All @@ -58,22 +57,24 @@ func (o *odirectReader) Read(buf []byte) (n int, err error) {
}
if !o.seenRead {
o.buf = *o.Bufp
n, err = o.File.Read(o.buf)
n, err = syscall.Read(o.fd, o.buf)
if err != nil && err != io.EOF {
if errors.Is(err, syscall.EINVAL) {
if err = disableDirectIO(o.File.Fd()); err != nil {
if err = disableDirectIO(uintptr(o.fd)); err != nil {
o.err = err
return n, err
}
n, err = o.File.Read(o.buf)
n, err = syscall.Read(o.fd, o.buf)
}
if err != nil && err != io.EOF {
o.err = err
return n, err
}
}
if n == 0 {
// err is likely io.EOF
if err == nil {
err = io.EOF
}
o.err = err
return n, err
}
Expand All @@ -95,24 +96,31 @@ func (o *odirectReader) Read(buf []byte) (n int, err error) {
// Close - Release the buffer and close the file.
func (o *odirectReader) Close() error {
o.err = errors.New("internal error: odirectReader Read after Close")
return o.File.Close()
return syscall.Close(o.fd)
}

type nullWriter struct{}

func (n nullWriter) Write(b []byte) (int, error) {
return len(b), nil
}

func (d *DrivePerf) runReadTest(ctx context.Context, path string, data []byte) (uint64, error) {
startTime := time.Now()
f, err := directio.OpenFile(path, os.O_RDONLY, 0400)
fd, err := syscall.Open(path, syscall.O_DIRECT|syscall.O_RDONLY, 0o400)
if err != nil {
return 0, err
}
fadviseSequential(f, int64(d.FileSize))
unix.Fadvise(fd, 0, int64(d.FileSize), unix.FADV_SEQUENTIAL)

of := &odirectReader{
File: f,
fd: fd,
Bufp: &data,
ctx: ctx,
alignment: d.FileSize%4096 == 0,
}
n, err := io.Copy(ioutil.Discard, of)

n, err := io.Copy(&nullWriter{}, of)
of.Close()
if err != nil {
return 0, err
Expand Down Expand Up @@ -175,7 +183,6 @@ func newEncReader(ctx context.Context) io.Reader {
stream := sio.NewStream(gcm, sio.BufSize)

return stream.EncryptReader(&nullReader{ctx: ctx}, randSrc[:stream.NonceSize()], nil)

}

type odirectWriter struct {
Expand Down Expand Up @@ -260,7 +267,6 @@ func copyAligned(fd uintptr, w io.Writer, r io.Reader, alignedBuf []byte, totalS
return written, nil
}
}

}

func (d *DrivePerf) runWriteTest(ctx context.Context, path string, data []byte) (uint64, error) {
Expand All @@ -269,7 +275,7 @@ func (d *DrivePerf) runWriteTest(ctx context.Context, path string, data []byte)
}

startTime := time.Now()
f, err := directio.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
f, err := directio.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit cc79779

Please sign in to comment.