Skip to content

Commit

Permalink
trySplice: use one pair instead of two; add move flag to splice
Browse files Browse the repository at this point in the history
  • Loading branch information
SandyXSD committed Oct 30, 2024
1 parent 6a2a137 commit 7d1b622
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 39 deletions.
45 changes: 9 additions & 36 deletions fuse/splice_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,25 @@ func (s *Server) setSplice() {
// This dance is neccessary because header and payload cannot be split across
// two splices and we cannot seek in a pipe buffer.
func (ms *Server) trySplice(header []byte, req *request, fdData *readResultFd) error {
var err error

// Get a pair of connected pipes
pair1, err := splice.Get()
if err != nil {
return err
}
defer splice.Done(pair1)

// Grow buffer pipe to requested size + one extra page
// Without the extra page the kernel will block once the pipe is almost full
pair1Sz := fdData.Size() + os.Getpagesize()
if err := pair1.Grow(pair1Sz); err != nil {
return err
}

// Read data from file
payloadLen, err := pair1.LoadFromAt(fdData.Fd, fdData.Size(), fdData.Off)

if err != nil {
// TODO - extract the data from splice.
return err
}

// Get another pair of connected pipes
pair2, err := splice.Get()
pair, err := splice.Get()
if err != nil {
return err
}
defer splice.Done(pair2)
defer splice.Done(pair)

// Grow pipe to header + actually read size + one extra page
// Without the extra page the kernel will block once the pipe is almost full
payloadLen := fdData.Size()
header = req.serializeHeader(payloadLen)
total := len(header) + payloadLen
pair2Sz := total + os.Getpagesize()
if err := pair2.Grow(pair2Sz); err != nil {
// FIXME: use pipes big enough to skip the Grow()?
if err := pair.Grow(total + os.Getpagesize()); err != nil {
return err
}

// Write header into pair2
n, err := pair2.Write(header)
n, err := pair.Write(header)
if err != nil {
return err
}
Expand All @@ -79,7 +56,7 @@ func (ms *Server) trySplice(header []byte, req *request, fdData *readResultFd) e
}

// Write data into pair2
n, err = pair2.LoadFrom(pair1.ReadFd(), payloadLen)
n, err = pair.LoadFrom(fdData.Fd, payloadLen)
if err != nil {
return err
}
Expand All @@ -88,10 +65,6 @@ func (ms *Server) trySplice(header []byte, req *request, fdData *readResultFd) e
}

// Write header + data to /dev/fuse
_, err = pair2.WriteTo(uintptr(ms.mountFd), total)
if err != nil {
return err
}

return nil
_, err = pair.WriteTo(uintptr(ms.mountFd), total)
return err
}
7 changes: 4 additions & 3 deletions splice/pair_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"syscall"
)

const _SPLICE_F_MOVE = 0x1
const _SPLICE_F_NONBLOCK = 0x2

func (p *Pair) LoadFromAt(fd uintptr, sz int, off int64) (int, error) {
n, err := syscall.Splice(int(fd), &off, p.w, nil, sz, 0)
return int(n), err
Expand All @@ -30,15 +33,13 @@ func (p *Pair) LoadFrom(fd uintptr, sz int) (int, error) {
}

func (p *Pair) WriteTo(fd uintptr, n int) (int, error) {
m, err := syscall.Splice(p.r, nil, int(fd), nil, int(n), 0)
m, err := syscall.Splice(p.r, nil, int(fd), nil, int(n), _SPLICE_F_MOVE)
if err != nil {
err = os.NewSyscallError("Splice write", err)
}
return int(m), err
}

const _SPLICE_F_NONBLOCK = 0x2

func (p *Pair) discard() {
_, err := syscall.Splice(p.r, nil, int(devNullFD), nil, int(p.size), _SPLICE_F_NONBLOCK)
if err == syscall.EAGAIN {
Expand Down

0 comments on commit 7d1b622

Please sign in to comment.