forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
os, internal/poll, internal/syscall/unix: use copy_file_range on Linux
Linux 4.5 introduced (and Linux 5.3 refined) the copy_file_range system call, which allows file systems the opportunity to implement copy acceleration techniques. This commit adds support for copy_file_range(2) to the os package. Introduce a new ReadFrom method on *os.File, which makes *os.File implement the io.ReaderFrom interface. If dst and src are both files, this enables io.Copy(dst, src) to call dst.ReadFrom(src), which, in turn, will call copy_file_range(2) if possible. If copy_file_range(2) is not supported by the host kernel, or if either of dst or src refers to a non-regular file, ReadFrom falls back to the regular io.Copy code path. Add internal/poll.CopyFileRange, which acquires locks on the appropriate poll.FDs and performs the actual work, as well as internal/syscall/unix.CopyFileRange, which wraps the copy_file_range system call itself at the lowest level. Rework file layout in internal/syscall/unix to accomodate the additional system call numbers needed for copy_file_range. Merge these definitions with the ones used by getrandom(2) into sysnum_linux_$GOARCH.go files. A note on additional optimizations: if dst and src both refer to pipes in the invocation dst.ReadFrom(src), we could, in theory, use the existing splice(2) code in package internal/poll to splice directly from src to dst. Attempting this runs into trouble with the poller, however. If we call splice(src, dst) and see EAGAIN, we cannot know if it came from src not being ready for reading or dst not being ready for writing. The write end of src and the read end of dst are not under our control, so we cannot reliably use the poller to wait for readiness. Therefore, it seems infeasible to use the new ReadFrom method to splice between pipes directly. In conclusion, for now, the only optimization enabled by the new ReadFrom method on *os.File is the copy_file_range optimization. Fixes golang#36817. Change-Id: I696372639fa0cdf704e3f65414f7321fc7d30adb Reviewed-on: https://go-review.googlesource.com/c/go/+/229101 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
- Loading branch information
1 parent
5ea8976
commit 6c68fc0
Showing
16 changed files
with
568 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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,93 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package poll | ||
|
||
import ( | ||
"internal/syscall/unix" | ||
"sync/atomic" | ||
"syscall" | ||
) | ||
|
||
var copyFileRangeSupported int32 = 1 // accessed atomically | ||
|
||
const maxCopyFileRangeRound = 1 << 30 | ||
|
||
// CopyFileRange copies at most remain bytes of data from src to dst, using | ||
// the copy_file_range system call. dst and src must refer to regular files. | ||
func CopyFileRange(dst, src *FD, remain int64) (written int64, handled bool, err error) { | ||
if atomic.LoadInt32(©FileRangeSupported) == 0 { | ||
return 0, false, nil | ||
} | ||
for remain > 0 { | ||
max := remain | ||
if max > maxCopyFileRangeRound { | ||
max = maxCopyFileRangeRound | ||
} | ||
n, err := copyFileRange(dst, src, int(max)) | ||
switch err { | ||
case syscall.ENOSYS: | ||
// copy_file_range(2) was introduced in Linux 4.5. | ||
// Go supports Linux >= 2.6.33, so the system call | ||
// may not be present. | ||
// | ||
// If we see ENOSYS, we have certainly not transfered | ||
// any data, so we can tell the caller that we | ||
// couldn't handle the transfer and let them fall | ||
// back to more generic code. | ||
// | ||
// Seeing ENOSYS also means that we will not try to | ||
// use copy_file_range(2) again. | ||
atomic.StoreInt32(©FileRangeSupported, 0) | ||
return 0, false, nil | ||
case syscall.EXDEV, syscall.EINVAL: | ||
// Prior to Linux 5.3, it was not possible to | ||
// copy_file_range across file systems. Similarly to | ||
// the ENOSYS case above, if we see EXDEV, we have | ||
// not transfered any data, and we can let the caller | ||
// fall back to generic code. | ||
// | ||
// As for EINVAL, that is what we see if, for example, | ||
// dst or src refer to a pipe rather than a regular | ||
// file. This is another case where no data has been | ||
// transfered, so we consider it unhandled. | ||
return 0, false, nil | ||
case nil: | ||
if n == 0 { | ||
// src is at EOF, which means we are done. | ||
return written, true, nil | ||
} | ||
remain -= n | ||
written += n | ||
default: | ||
return written, true, err | ||
} | ||
} | ||
return written, true, nil | ||
} | ||
|
||
// copyFileRange performs one round of copy_file_range(2). | ||
func copyFileRange(dst, src *FD, max int) (written int64, err error) { | ||
// The signature of copy_file_range(2) is: | ||
// | ||
// ssize_t copy_file_range(int fd_in, loff_t *off_in, | ||
// int fd_out, loff_t *off_out, | ||
// size_t len, unsigned int flags); | ||
// | ||
// Note that in the call to unix.CopyFileRange below, we use nil | ||
// values for off_in and off_out. For the system call, this means | ||
// "use and update the file offsets". That is why we must acquire | ||
// locks for both file descriptors (and why this whole machinery is | ||
// in the internal/poll package to begin with). | ||
if err := dst.writeLock(); err != nil { | ||
return 0, err | ||
} | ||
defer dst.writeUnlock() | ||
if err := src.readLock(); err != nil { | ||
return 0, err | ||
} | ||
defer src.readUnlock() | ||
n, err := unix.CopyFileRange(src.Sysfd, nil, dst.Sysfd, nil, max, 0) | ||
return int64(n), err | ||
} |
This file contains 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,26 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package unix | ||
|
||
import ( | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
func CopyFileRange(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error) { | ||
r1, _, errno := syscall.Syscall6(copyFileRangeTrap, | ||
uintptr(rfd), | ||
uintptr(unsafe.Pointer(roff)), | ||
uintptr(wfd), | ||
uintptr(unsafe.Pointer(woff)), | ||
uintptr(len), | ||
uintptr(flags), | ||
) | ||
n = int(r1) | ||
if errno != 0 { | ||
err = errno | ||
} | ||
return | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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,7 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package os | ||
|
||
var PollCopyFileRangeP = &pollCopyFileRange |
This file contains 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 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,41 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package os | ||
|
||
import ( | ||
"internal/poll" | ||
"io" | ||
) | ||
|
||
var pollCopyFileRange = poll.CopyFileRange | ||
|
||
func (f *File) readFrom(r io.Reader) (written int64, handled bool, err error) { | ||
// copy_file_range(2) does not support destinations opened with | ||
// O_APPEND, so don't even try. | ||
if f.appendMode { | ||
return 0, false, nil | ||
} | ||
|
||
remain := int64(1 << 62) | ||
|
||
lr, ok := r.(*io.LimitedReader) | ||
if ok { | ||
remain, r = lr.N, lr.R | ||
if remain <= 0 { | ||
return 0, true, nil | ||
} | ||
} | ||
|
||
src, ok := r.(*File) | ||
if !ok { | ||
return 0, false, nil | ||
} | ||
|
||
written, handled, err = pollCopyFileRange(&f.pfd, &src.pfd, remain) | ||
if lr != nil { | ||
lr.N -= written | ||
} | ||
return written, handled, NewSyscallError("copy_file_range", err) | ||
} |
Oops, something went wrong.