diff --git a/seaweed-block/internal/iouring/execution.go b/seaweed-block/internal/iouring/execution.go new file mode 100644 index 0000000000..209c53a768 --- /dev/null +++ b/seaweed-block/internal/iouring/execution.go @@ -0,0 +1,51 @@ +package iouring + +import "errors" + +var ErrUnsupported = errors.New("io_uring execution is unsupported on this platform") + +type operationKind uint8 + +const ( + operationWrite operationKind = iota + 1 + operationFsync +) + +type Operation struct { + kind operationKind + fd int + offset int64 + data []byte + userData uint64 +} + +func Write(fd int, offset int64, data []byte, userData uint64) Operation { + return Operation{ + kind: operationWrite, + fd: fd, + offset: offset, + data: data, + userData: userData, + } +} + +func Fsync(fd int, userData uint64) Operation { + return Operation{ + kind: operationFsync, + fd: fd, + userData: userData, + } +} + +type Completion struct { + UserData uint64 + Result int32 + Flags uint32 +} + +type ExecutionStats struct { + QueueDepth uint32 + SubmittedOps uint64 + SubmitSyscalls uint64 + CompletionCount uint64 +} diff --git a/seaweed-block/internal/iouring/execution_linux_amd64.go b/seaweed-block/internal/iouring/execution_linux_amd64.go new file mode 100644 index 0000000000..62fdc5cde6 --- /dev/null +++ b/seaweed-block/internal/iouring/execution_linux_amd64.go @@ -0,0 +1,110 @@ +//go:build linux && amd64 + +package iouring + +import ( + "errors" + "fmt" + "sync" +) + +type Executor struct { + mu sync.Mutex + ring *ioUring + closed bool + stats ExecutionStats +} + +func New(depth uint32) (*Executor, error) { + ring, err := newIOUring(depth) + if err != nil { + return nil, fmt.Errorf("%w: setup: %v", ErrUnsupported, err) + } + supported, err := ring.supportedOpcodes() + if err != nil { + ring.close() + return nil, fmt.Errorf("%w: opcode probe: %v", ErrUnsupported, err) + } + if !supported[ioUringOpWrite] || !supported[ioUringOpFsync] { + ring.close() + return nil, fmt.Errorf( + "%w: required opcodes write=%t fsync=%t", + ErrUnsupported, + supported[ioUringOpWrite], + supported[ioUringOpFsync], + ) + } + return &Executor{ + ring: ring, + stats: ExecutionStats{ + QueueDepth: *ring.sqEntries, + }, + }, nil +} + +func (executor *Executor) SubmitAndWait(operations []Operation) ([]Completion, error) { + executor.mu.Lock() + defer executor.mu.Unlock() + if executor.closed { + return nil, errors.New("io_uring executor is closed") + } + + raw := make([]ringOperation, len(operations)) + for i, operation := range operations { + var opcode uint8 + switch operation.kind { + case operationWrite: + if len(operation.data) == 0 { + return nil, fmt.Errorf("write operation %d has an empty buffer", i) + } + opcode = ioUringOpWrite + case operationFsync: + if len(operation.data) != 0 { + return nil, fmt.Errorf("fsync operation %d unexpectedly owns data", i) + } + opcode = ioUringOpFsync + default: + return nil, fmt.Errorf("operation %d has unknown kind %d", i, operation.kind) + } + raw[i] = ringOperation{ + opcode: opcode, + fd: int32(operation.fd), + offset: uint64(operation.offset), + data: operation.data, + userData: operation.userData, + } + } + + beforeSyscalls := executor.ring.submitSyscalls + cqes, submitted, err := executor.ring.submitAndWait(raw) + executor.stats.SubmittedOps += uint64(submitted) + executor.stats.SubmitSyscalls += uint64(executor.ring.submitSyscalls - beforeSyscalls) + executor.stats.CompletionCount += uint64(len(cqes)) + + completions := make([]Completion, len(cqes)) + for i, cqe := range cqes { + completions[i] = Completion{ + UserData: cqe.UserData, + Result: cqe.Result, + Flags: cqe.Flags, + } + } + return completions, err +} + +func (executor *Executor) Stats() ExecutionStats { + executor.mu.Lock() + defer executor.mu.Unlock() + return executor.stats +} + +func (executor *Executor) Close() error { + executor.mu.Lock() + defer executor.mu.Unlock() + if executor.closed { + return nil + } + executor.closed = true + executor.ring.close() + return nil +} diff --git a/seaweed-block/internal/iouring/execution_linux_amd64_test.go b/seaweed-block/internal/iouring/execution_linux_amd64_test.go new file mode 100644 index 0000000000..e35cb7a95e --- /dev/null +++ b/seaweed-block/internal/iouring/execution_linux_amd64_test.go @@ -0,0 +1,53 @@ +//go:build linux && amd64 + +package iouring + +import ( + "bytes" + "os" + "path/filepath" + "testing" +) + +func TestExecutorSubmitsNonContiguousWritesAndFsync(t *testing.T) { + executor, err := New(8) + if err != nil { + t.Fatal(err) + } + defer executor.Close() + + path := filepath.Join(t.TempDir(), "executor.dat") + file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + defer file.Close() + if err := file.Truncate(4 * probeBlockSize); err != nil { + t.Fatal(err) + } + + first := bytes.Repeat([]byte{0x19}, probeBlockSize) + second := bytes.Repeat([]byte{0x73}, probeBlockSize) + completions, err := executor.SubmitAndWait([]Operation{ + Write(int(file.Fd()), 2*probeBlockSize, first, 1), + Write(int(file.Fd()), 0, second, 2), + }) + if err != nil { + t.Fatal(err) + } + if len(completions) != 2 { + t.Fatalf("write completions=%d want=2", len(completions)) + } + completions, err = executor.SubmitAndWait([]Operation{Fsync(int(file.Fd()), 3)}) + if err != nil { + t.Fatal(err) + } + if len(completions) != 1 || completions[0].UserData != 3 || completions[0].Result != 0 { + t.Fatalf("fsync completions=%+v", completions) + } + + stats := executor.Stats() + if stats.QueueDepth < 4 || stats.SubmittedOps != 3 || stats.CompletionCount != 3 { + t.Fatalf("stats=%+v", stats) + } +} diff --git a/seaweed-block/internal/iouring/execution_unsupported.go b/seaweed-block/internal/iouring/execution_unsupported.go new file mode 100644 index 0000000000..5d6094df69 --- /dev/null +++ b/seaweed-block/internal/iouring/execution_unsupported.go @@ -0,0 +1,21 @@ +//go:build !linux || !amd64 + +package iouring + +type Executor struct{} + +func New(uint32) (*Executor, error) { + return nil, ErrUnsupported +} + +func (*Executor) SubmitAndWait([]Operation) ([]Completion, error) { + return nil, ErrUnsupported +} + +func (*Executor) Stats() ExecutionStats { + return ExecutionStats{} +} + +func (*Executor) Close() error { + return nil +} diff --git a/seaweed-block/internal/iouring/execution_unsupported_test.go b/seaweed-block/internal/iouring/execution_unsupported_test.go new file mode 100644 index 0000000000..816126a367 --- /dev/null +++ b/seaweed-block/internal/iouring/execution_unsupported_test.go @@ -0,0 +1,15 @@ +//go:build !linux || !amd64 + +package iouring + +import ( + "errors" + "testing" +) + +func TestExecutorIsExplicitlyUnsupported(t *testing.T) { + executor, err := New(8) + if executor != nil || !errors.Is(err, ErrUnsupported) { + t.Fatalf("New=(%v,%v) want=(nil,ErrUnsupported)", executor, err) + } +}