Skip to content

Commit f2316c2

Browse files
acln0ianlancetaylor
authored andcommitted
net: add support for splice(2) in (*TCPConn).ReadFrom on Linux
This change adds support for the splice system call on Linux, for the purpose of optimizing (*TCPConn).ReadFrom by reducing copies of data from and to userspace. It does so by creating a temporary pipe and splicing data from the source connection to the pipe, then from the pipe to the destination connection. The pipe serves as an in-kernel buffer for the data transfer. No new API is added to package net, but a new Splice function is added to package internal/poll, because using splice requires help from the network poller. Users of the net package should benefit from the change transparently. This change only enables the optimization if the Reader in ReadFrom is a TCP connection. Since splice is a more general interface, it could, in theory, also be enabled if the Reader were a unix socket, or the read half of a pipe. However, benchmarks show that enabling it for unix sockets is most likely not a net performance gain. The tcp <- unix case is also fairly unlikely to be used very much by users of package net. Enabling the optimization for pipes is also problematic from an implementation perspective, since package net cannot easily get at the *poll.FD of an *os.File. A possible solution to this would be to dup the pipe file descriptor, register the duped descriptor with the network poller, and work on that *poll.FD instead of the original. However, this seems too intrusive, so it has not been done. If there was a clean way to do it, it would probably be worth doing, since splicing from a pipe to a socket can be done directly. Therefore, this patch only enables the optimization for what is likely the most common use case: tcp <- tcp. The following benchmark compares the performance of the previous userspace genericReadFrom code path to the new optimized code path. The sub-benchmarks represent chunk sizes used by the writer on the other end of the Reader passed to ReadFrom. benchmark old ns/op new ns/op delta BenchmarkTCPReadFrom/1024-4 4727 4954 +4.80% BenchmarkTCPReadFrom/2048-4 4389 4301 -2.01% BenchmarkTCPReadFrom/4096-4 4606 4534 -1.56% BenchmarkTCPReadFrom/8192-4 5219 4779 -8.43% BenchmarkTCPReadFrom/16384-4 8708 8008 -8.04% BenchmarkTCPReadFrom/32768-4 16349 14973 -8.42% BenchmarkTCPReadFrom/65536-4 35246 27406 -22.24% BenchmarkTCPReadFrom/131072-4 72920 52382 -28.17% BenchmarkTCPReadFrom/262144-4 149311 95094 -36.31% BenchmarkTCPReadFrom/524288-4 306704 181856 -40.71% BenchmarkTCPReadFrom/1048576-4 674174 357406 -46.99% benchmark old MB/s new MB/s speedup BenchmarkTCPReadFrom/1024-4 216.62 206.69 0.95x BenchmarkTCPReadFrom/2048-4 466.61 476.08 1.02x BenchmarkTCPReadFrom/4096-4 889.09 903.31 1.02x BenchmarkTCPReadFrom/8192-4 1569.40 1714.06 1.09x BenchmarkTCPReadFrom/16384-4 1881.42 2045.84 1.09x BenchmarkTCPReadFrom/32768-4 2004.18 2188.41 1.09x BenchmarkTCPReadFrom/65536-4 1859.38 2391.25 1.29x BenchmarkTCPReadFrom/131072-4 1797.46 2502.21 1.39x BenchmarkTCPReadFrom/262144-4 1755.69 2756.68 1.57x BenchmarkTCPReadFrom/524288-4 1709.42 2882.98 1.69x BenchmarkTCPReadFrom/1048576-4 1555.35 2933.84 1.89x Fixes #10948 Change-Id: I3ce27f21f7adda8b696afdc48a91149998ae16a5 Reviewed-on: https://go-review.googlesource.com/107715 Run-TryBot: Brad Fitzpatrick <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent cc88092 commit f2316c2

File tree

5 files changed

+661
-0
lines changed

5 files changed

+661
-0
lines changed

src/internal/poll/splice_linux.go

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package poll
6+
7+
import "syscall"
8+
9+
const (
10+
// spliceNonblock makes calls to splice(2) non-blocking.
11+
spliceNonblock = 0x2
12+
13+
// maxSpliceSize is the maximum amount of data Splice asks
14+
// the kernel to move in a single call to splice(2).
15+
maxSpliceSize = 4 << 20
16+
)
17+
18+
// Splice transfers at most remain bytes of data from src to dst, using the
19+
// splice system call to minimize copies of data from and to userspace.
20+
//
21+
// Splice creates a temporary pipe, to serve as a buffer for the data transfer.
22+
// src and dst must both be stream-oriented sockets.
23+
//
24+
// If err != nil, sc is the system call which caused the error.
25+
func Splice(dst, src *FD, remain int64) (written int64, handled bool, sc string, err error) {
26+
prfd, pwfd, sc, err := newTempPipe()
27+
if err != nil {
28+
return 0, false, sc, err
29+
}
30+
defer destroyTempPipe(prfd, pwfd)
31+
// From here on, the operation should be considered handled,
32+
// even if Splice doesn't transfer any data.
33+
if err := src.readLock(); err != nil {
34+
return 0, true, "splice", err
35+
}
36+
defer src.readUnlock()
37+
if err := dst.writeLock(); err != nil {
38+
return 0, true, "splice", err
39+
}
40+
defer dst.writeUnlock()
41+
if err := src.pd.prepareRead(src.isFile); err != nil {
42+
return 0, true, "splice", err
43+
}
44+
if err := dst.pd.prepareWrite(dst.isFile); err != nil {
45+
return 0, true, "splice", err
46+
}
47+
var inPipe, n int
48+
for err == nil && remain > 0 {
49+
max := maxSpliceSize
50+
if int64(max) > remain {
51+
max = int(remain)
52+
}
53+
inPipe, err = spliceDrain(pwfd, src, max)
54+
// spliceDrain should never return EAGAIN, so if err != nil,
55+
// Splice cannot continue. If inPipe == 0 && err == nil,
56+
// src is at EOF, and the transfer is complete.
57+
if err != nil || (inPipe == 0 && err == nil) {
58+
break
59+
}
60+
n, err = splicePump(dst, prfd, inPipe)
61+
if n > 0 {
62+
written += int64(n)
63+
remain -= int64(n)
64+
}
65+
}
66+
if err != nil {
67+
return written, true, "splice", err
68+
}
69+
return written, true, "", nil
70+
}
71+
72+
// spliceDrain moves data from a socket to a pipe.
73+
//
74+
// Invariant: when entering spliceDrain, the pipe is empty. It is either in its
75+
// initial state, or splicePump has emptied it previously.
76+
//
77+
// Given this, spliceDrain can reasonably assume that the pipe is ready for
78+
// writing, so if splice returns EAGAIN, it must be because the socket is not
79+
// ready for reading.
80+
//
81+
// If spliceDrain returns (0, nil), src is at EOF.
82+
func spliceDrain(pipefd int, sock *FD, max int) (int, error) {
83+
for {
84+
n, err := splice(pipefd, sock.Sysfd, max, spliceNonblock)
85+
if err != syscall.EAGAIN {
86+
return n, err
87+
}
88+
if err := sock.pd.waitRead(sock.isFile); err != nil {
89+
return n, err
90+
}
91+
}
92+
}
93+
94+
// splicePump moves all the buffered data from a pipe to a socket.
95+
//
96+
// Invariant: when entering splicePump, there are exactly inPipe
97+
// bytes of data in the pipe, from a previous call to spliceDrain.
98+
//
99+
// By analogy to the condition from spliceDrain, splicePump
100+
// only needs to poll the socket for readiness, if splice returns
101+
// EAGAIN.
102+
//
103+
// If splicePump cannot move all the data in a single call to
104+
// splice(2), it loops over the buffered data until it has written
105+
// all of it to the socket. This behavior is similar to the Write
106+
// step of an io.Copy in userspace.
107+
func splicePump(sock *FD, pipefd int, inPipe int) (int, error) {
108+
written := 0
109+
for inPipe > 0 {
110+
n, err := splice(sock.Sysfd, pipefd, inPipe, spliceNonblock)
111+
// Here, the condition n == 0 && err == nil should never be
112+
// observed, since Splice controls the write side of the pipe.
113+
if n > 0 {
114+
inPipe -= n
115+
written += n
116+
continue
117+
}
118+
if err != syscall.EAGAIN {
119+
return written, err
120+
}
121+
if err := sock.pd.waitWrite(sock.isFile); err != nil {
122+
return written, err
123+
}
124+
}
125+
return written, nil
126+
}
127+
128+
// splice wraps the splice system call. Since the current implementation
129+
// only uses splice on sockets and pipes, the offset arguments are unused.
130+
// splice returns int instead of int64, because callers never ask it to
131+
// move more data in a single call than can fit in an int32.
132+
func splice(out int, in int, max int, flags int) (int, error) {
133+
n, err := syscall.Splice(in, nil, out, nil, max, flags)
134+
return int(n), err
135+
}
136+
137+
// newTempPipe sets up a temporary pipe for a splice operation.
138+
func newTempPipe() (prfd, pwfd int, sc string, err error) {
139+
var fds [2]int
140+
const flags = syscall.O_CLOEXEC | syscall.O_NONBLOCK
141+
if err := syscall.Pipe2(fds[:], flags); err != nil {
142+
// pipe2 was added in 2.6.27 and our minimum requirement
143+
// is 2.6.23, so it might not be implemented.
144+
if err == syscall.ENOSYS {
145+
return newTempPipeFallback(fds[:])
146+
}
147+
return -1, -1, "pipe2", err
148+
}
149+
return fds[0], fds[1], "", nil
150+
}
151+
152+
// newTempPipeFallback is a fallback for newTempPipe, for systems
153+
// which do not support pipe2.
154+
func newTempPipeFallback(fds []int) (prfd, pwfd int, sc string, err error) {
155+
syscall.ForkLock.RLock()
156+
defer syscall.ForkLock.RUnlock()
157+
if err := syscall.Pipe(fds); err != nil {
158+
return -1, -1, "pipe", err
159+
}
160+
prfd, pwfd = fds[0], fds[1]
161+
syscall.CloseOnExec(prfd)
162+
syscall.CloseOnExec(pwfd)
163+
if err := syscall.SetNonblock(prfd, true); err != nil {
164+
CloseFunc(prfd)
165+
CloseFunc(pwfd)
166+
return -1, -1, "setnonblock", err
167+
}
168+
if err := syscall.SetNonblock(pwfd, true); err != nil {
169+
CloseFunc(prfd)
170+
CloseFunc(pwfd)
171+
return -1, -1, "setnonblock", err
172+
}
173+
return prfd, pwfd, "", nil
174+
}
175+
176+
// destroyTempPipe destroys a temporary pipe.
177+
func destroyTempPipe(prfd, pwfd int) error {
178+
err := CloseFunc(prfd)
179+
err1 := CloseFunc(pwfd)
180+
if err == nil {
181+
return err1
182+
}
183+
return err
184+
}

src/net/splice_linux.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package net
6+
7+
import (
8+
"internal/poll"
9+
"io"
10+
)
11+
12+
// splice transfers data from r to c using the splice system call to minimize
13+
// copies from and to userspace. c must be a TCP connection. Currently, splice
14+
// is only enabled if r is also a TCP connection.
15+
//
16+
// If splice returns handled == false, it has performed no work.
17+
func splice(c *netFD, r io.Reader) (written int64, err error, handled bool) {
18+
var remain int64 = 1 << 62 // by default, copy until EOF
19+
lr, ok := r.(*io.LimitedReader)
20+
if ok {
21+
remain, r = lr.N, lr.R
22+
if remain <= 0 {
23+
return 0, nil, true
24+
}
25+
}
26+
s, ok := r.(*TCPConn)
27+
if !ok {
28+
return 0, nil, false
29+
}
30+
written, handled, sc, err := poll.Splice(&c.pfd, &s.fd.pfd, remain)
31+
if lr != nil {
32+
lr.N -= written
33+
}
34+
return written, wrapSyscallError(sc, err), handled
35+
}

src/net/splice_stub.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !linux
6+
7+
package net
8+
9+
import "io"
10+
11+
func splice(c *netFD, r io.Reader) (int64, error, bool) {
12+
return 0, nil, false
13+
}

0 commit comments

Comments
 (0)