forked from hanwen/go-fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
splice: optional SPLICE_F_MOVE on fuse replies #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AliRamberg
wants to merge
2
commits into
master
Choose a base branch
from
splice-f-move
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ import ( | |
| "os" | ||
| "syscall" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
|
|
||
| "github.com/hanwen/go-fuse/v2/splice" | ||
| ) | ||
|
|
||
|
|
@@ -35,6 +37,12 @@ func (r *fuseFD) trySplice(req *request, readResult ReadResult) error { | |
| // readResult.Size(), so req.outHeaderBuf is correct for the optimistic case. | ||
| total := len(req.outHeaderBuf) + len(req.outDataBuf) + readResult.Size() | ||
|
|
||
| // The filesystem decides per reply whether its pages may be moved. | ||
| spliceFlags := 0 | ||
| if m, ok := readResult.(movableResult); ok && m.SpliceMove() { | ||
| spliceFlags = unix.SPLICE_F_MOVE | ||
| } | ||
|
|
||
| pair, err := splice.Get() | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -90,12 +98,12 @@ func (r *fuseFD) trySplice(req *request, readResult ReadResult) error { | |
| // New length. | ||
| req.serializeHeader(payloadLen) | ||
|
|
||
| return r.trySplice(req, ReadResultPipe(pair, payloadLen)) | ||
| return r.trySplice(req, &pipeReadResult{pair: pair, size: payloadLen, move: spliceFlags != 0}) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| // Write header + payload to /dev/fuse. | ||
| if cerr := r.withFD(func(fd int) { | ||
| _, err = pair.WriteTo(uintptr(fd), total) | ||
| _, err = pair.WriteToFlags(uintptr(fd), total, spliceFlags) | ||
| }); cerr != nil { | ||
| return cerr | ||
| } | ||
|
|
@@ -105,6 +113,7 @@ func (r *fuseFD) trySplice(req *request, readResult ReadResult) error { | |
| type pipeReadResult struct { | ||
| pair *splice.Pair | ||
| size int | ||
| move bool | ||
| } | ||
|
|
||
| func (r *pipeReadResult) Done() { | ||
|
|
@@ -128,9 +137,21 @@ func (r *pipeReadResult) Stateful() (fd uintptr, sz int) { | |
| return r.pair.ReadFd(), r.size | ||
| } | ||
|
|
||
| func (r *pipeReadResult) SpliceMove() bool { return r.move } | ||
|
|
||
| // ReadResultPipe returns a [ReadResult] of `size` bytes that was preloaded | ||
| // into the given pipe. The pipe is discarded with splice.Done() | ||
| // after the read completes. | ||
| func ReadResultPipe(pipe *splice.Pair, size int) ReadResult { | ||
| return &pipeReadResult{pipe, size} | ||
| return &pipeReadResult{pair: pipe, size: size} | ||
| } | ||
|
|
||
| // ReadResultPipeMove is [ReadResultPipe] for a pipe whose pages the kernel may | ||
| // move into the inode's page cache instead of copying into it. A move removes | ||
| // each page from the page cache of the file it came from and waits on its | ||
| // writeback, so use it only for pages the filesystem can lose. Only readahead | ||
| // reads are eligible. A ReadResult can also opt in by implementing | ||
| // SpliceMove() bool. | ||
| func ReadResultPipeMove(pipe *splice.Pair, size int) ReadResult { | ||
| return &pipeReadResult{pair: pipe, size: size, move: true} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package fuse | ||
|
|
||
| import "testing" | ||
|
|
||
| // trySplice reaches the zero-copy path only for a result satisfying | ||
| // statefulResult, so the type carrying the move flag must keep satisfying it: | ||
| // wrapping a ReadResult hides Stateful and falls back to copying. | ||
| func TestReadResultPipeSpliceMove(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| r ReadResult | ||
| want bool | ||
| }{ | ||
| {"ReadResultPipe", ReadResultPipe(nil, 4096), false}, | ||
| {"ReadResultPipeMove", ReadResultPipeMove(nil, 4096), true}, | ||
| } { | ||
| if _, ok := tc.r.(statefulResult); !ok { | ||
| t.Errorf("%s: not a statefulResult; trySplice would copy instead of splice", tc.name) | ||
| } | ||
| m, ok := tc.r.(movableResult) | ||
| if !ok { | ||
| t.Fatalf("%s: not a movableResult", tc.name) | ||
| } | ||
| if got := m.SpliceMove(); got != tc.want { | ||
| t.Errorf("%s: SpliceMove() = %v, want %v", tc.name, got, tc.want) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In future we might add more splice flags, so this is brittle.