mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
phase168: add bounded io_uring executor API
This commit is contained in:
1 parent
bfa8095d76
commit
09bc856d0c
5 files changed
+250
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user