Separate io tests to read and write mode with separate latencies (#2)

This commit is contained in:
T.v.Dein
2025-10-22 18:02:26 +02:00
committed by GitHub
parent 8b3cf64e96
commit 5184c3a03e
6 changed files with 178 additions and 74 deletions

View File

@@ -1,7 +1,6 @@
package cmd
import (
"bytes"
"context"
"errors"
"io"
@@ -12,7 +11,7 @@ import (
"github.com/ncw/directio"
)
func die(err error, fd *os.File) bool {
func report(err error, fd *os.File) bool {
slog.Debug("failed to check io", "error", err)
if fd != nil {
@@ -24,8 +23,8 @@ func die(err error, fd *os.File) bool {
return false
}
// Calls runcheck() with timeout
func runExporter(file string, alloc *Alloc, timeout time.Duration) bool {
// Calls runcheck* with timeout
func runExporter(file string, alloc *Alloc, timeout time.Duration, op int) bool {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
@@ -34,14 +33,19 @@ func runExporter(file string, alloc *Alloc, timeout time.Duration) bool {
var res bool
go func() {
res = runcheck(file, alloc)
switch op {
case O_R:
res = runcheck_r(file, alloc)
case O_W:
res = runcheck_w(file, alloc)
}
run <- struct{}{}
}()
for {
select {
case <-ctx.Done():
return die(ctx.Err(), nil)
return report(ctx.Err(), nil)
case <-run:
return res
}
@@ -50,24 +54,49 @@ func runExporter(file string, alloc *Alloc, timeout time.Duration) bool {
// Checks file io on the specified path:
//
// - open the file (create if it doesnt exist)
// - truncate it if it already exists
// - write some data to it
// - closes the file
// - re-opens it for reading
// - opens it for reading
// - reads the block
// - compares if written block is equal to read block
// - closes file again
//
// Returns false if anything failed during that sequence,
// true otherwise.
func runcheck(file string, alloc *Alloc) bool {
alloc.Clean()
func runcheck_r(file string, alloc *Alloc) bool {
// read
in, err := directio.OpenFile(file, os.O_RDONLY, 0640)
if err != nil {
report(err, nil)
}
n, err := io.ReadFull(in, alloc.readBlock)
if err != nil {
return report(err, in)
}
if n != len(alloc.writeBlock) {
return report(errors.New("failed to read block"), in)
}
if err := in.Close(); err != nil {
return report(err, nil)
}
return true
}
// Checks file io on the specified path:
//
// - open the file (create if it doesnt exist)
// - truncate it if it already exists
// - write some data to it
// - closes the file
//
// Returns false if anything failed during that sequence,
// true otherwise.
func runcheck_w(file string, alloc *Alloc) bool {
// write
fd, err := directio.OpenFile(file, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0640)
if err != nil {
die(err, nil)
report(err, nil)
}
for i := 0; i < len(alloc.writeBlock); i++ {
@@ -76,39 +105,15 @@ func runcheck(file string, alloc *Alloc) bool {
n, err := fd.Write(alloc.writeBlock)
if err != nil {
return die(err, fd)
return report(err, fd)
}
if n != len(alloc.writeBlock) {
return die(errors.New("failed to write block"), fd)
return report(errors.New("failed to write block"), fd)
}
if err := fd.Close(); err != nil {
return die(err, nil)
}
// read
in, err := directio.OpenFile(file, os.O_RDONLY, 0640)
if err != nil {
die(err, nil)
}
n, err = io.ReadFull(in, alloc.readBlock)
if err != nil {
return die(err, in)
}
if n != len(alloc.writeBlock) {
return die(errors.New("failed to read block"), fd)
}
if err := in.Close(); err != nil {
return die(err, nil)
}
// compare
if !bytes.Equal(alloc.writeBlock, alloc.readBlock) {
return die(errors.New("read not the same as written"), nil)
return report(err, nil)
}
return true