2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2012-06-25 18:26:19 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// maxSendfileSize is the largest chunk size we ask the kernel to copy
|
|
|
|
// at a time.
|
|
|
|
const maxSendfileSize int = 4 << 20
|
|
|
|
|
|
|
|
// sendFile copies the contents of r to c using the sendfile
|
|
|
|
// system call to minimize copies.
|
|
|
|
//
|
|
|
|
// if handled == true, sendFile returns the number of bytes copied and any
|
|
|
|
// non-EOF error.
|
|
|
|
//
|
|
|
|
// if handled == false, sendFile performed no work.
|
|
|
|
func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
|
|
|
|
// FreeBSD uses 0 as the "until EOF" value. If you pass in more bytes than the
|
2014-05-02 14:17:55 -06:00
|
|
|
// file contains, it will loop back to the beginning ad nauseam until it's sent
|
2012-06-25 18:26:19 -06:00
|
|
|
// exactly the number of bytes told to. As such, we need to know exactly how many
|
|
|
|
// bytes to send.
|
|
|
|
var remain int64 = 0
|
|
|
|
|
|
|
|
lr, ok := r.(*io.LimitedReader)
|
|
|
|
if ok {
|
|
|
|
remain, r = lr.N, lr.R
|
|
|
|
if remain <= 0 {
|
|
|
|
return 0, nil, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f, ok := r.(*os.File)
|
|
|
|
if !ok {
|
|
|
|
return 0, nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if remain == 0 {
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err, false
|
|
|
|
}
|
|
|
|
|
|
|
|
remain = fi.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
// The other quirk with FreeBSD's sendfile implementation is that it doesn't
|
|
|
|
// use the current position of the file -- if you pass it offset 0, it starts
|
|
|
|
// from offset 0. There's no way to tell it "start from current position", so
|
|
|
|
// we have to manage that explicitly.
|
2016-04-12 22:35:37 -06:00
|
|
|
pos, err := f.Seek(0, io.SeekCurrent)
|
2012-06-25 18:26:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return 0, err, false
|
|
|
|
}
|
|
|
|
|
net: add special netFD mutex
The mutex, fdMutex, handles locking and lifetime of sysfd,
and serializes Read and Write methods.
This allows to strip 2 sync.Mutex.Lock calls,
2 sync.Mutex.Unlock calls, 1 defer and some amount
of misc overhead from every network operation.
On linux/amd64, Intel E5-2690:
benchmark old ns/op new ns/op delta
BenchmarkTCP4Persistent 9595 9454 -1.47%
BenchmarkTCP4Persistent-2 8978 8772 -2.29%
BenchmarkTCP4ConcurrentReadWrite 4900 4625 -5.61%
BenchmarkTCP4ConcurrentReadWrite-2 2603 2500 -3.96%
In general it strips 70-500 ns from every network operation depending
on processor model. On my relatively new E5-2690 it accounts to ~5%
of network op cost.
Fixes #6074.
R=golang-dev, bradfitz, alex.brainman, iant, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/12418043
2013-08-09 11:43:00 -06:00
|
|
|
if err := c.writeLock(); err != nil {
|
2012-06-25 18:26:19 -06:00
|
|
|
return 0, err, true
|
|
|
|
}
|
net: add special netFD mutex
The mutex, fdMutex, handles locking and lifetime of sysfd,
and serializes Read and Write methods.
This allows to strip 2 sync.Mutex.Lock calls,
2 sync.Mutex.Unlock calls, 1 defer and some amount
of misc overhead from every network operation.
On linux/amd64, Intel E5-2690:
benchmark old ns/op new ns/op delta
BenchmarkTCP4Persistent 9595 9454 -1.47%
BenchmarkTCP4Persistent-2 8978 8772 -2.29%
BenchmarkTCP4ConcurrentReadWrite 4900 4625 -5.61%
BenchmarkTCP4ConcurrentReadWrite-2 2603 2500 -3.96%
In general it strips 70-500 ns from every network operation depending
on processor model. On my relatively new E5-2690 it accounts to ~5%
of network op cost.
Fixes #6074.
R=golang-dev, bradfitz, alex.brainman, iant, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/12418043
2013-08-09 11:43:00 -06:00
|
|
|
defer c.writeUnlock()
|
2012-06-25 18:26:19 -06:00
|
|
|
|
|
|
|
dst := c.sysfd
|
|
|
|
src := int(f.Fd())
|
|
|
|
for remain > 0 {
|
|
|
|
n := maxSendfileSize
|
|
|
|
if int64(n) > remain {
|
|
|
|
n = int(remain)
|
|
|
|
}
|
2012-06-27 15:02:39 -06:00
|
|
|
pos1 := pos
|
|
|
|
n, err1 := syscall.Sendfile(dst, src, &pos1, n)
|
2012-06-25 18:26:19 -06:00
|
|
|
if n > 0 {
|
|
|
|
pos += int64(n)
|
|
|
|
written += int64(n)
|
|
|
|
remain -= int64(n)
|
|
|
|
}
|
|
|
|
if n == 0 && err1 == nil {
|
|
|
|
break
|
|
|
|
}
|
2012-12-04 21:59:01 -07:00
|
|
|
if err1 == syscall.EAGAIN {
|
2016-03-16 14:33:13 -06:00
|
|
|
if err1 = c.pd.waitWrite(); err1 == nil {
|
2012-06-25 18:26:19 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err1 == syscall.EINTR {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err1 != nil {
|
|
|
|
// This includes syscall.ENOSYS (no kernel
|
|
|
|
// support) and syscall.EINVAL (fd types which
|
2015-03-23 14:33:08 -06:00
|
|
|
// don't implement sendfile)
|
2015-04-16 08:10:56 -06:00
|
|
|
err = err1
|
2012-06-25 18:26:19 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lr != nil {
|
|
|
|
lr.N = remain
|
|
|
|
}
|
net: fix inconsistent errors
These a series of changes fix inconsistent errors on the package net
APIs. Now almost all the APIs return OpError as a common error type
except Lookup, Resolve and Parse APIs. The Lookup, Resolve and Parse
APIs return more specific errors such as DNSError, AddrError or
ParseError.
An OpError may contain nested error information. For example, Dial may
return an OpError containing a DNSError, AddrError, unexposed type/value
or other package's type/value like the following:
OpError{/* dial info */, Err: &DNSError{}}
OpError{/* dial info */, Err: &AddrError{}}
OpError{/* dial info */, Err: <unexposed type or value>}
OpError{/* dial info */, Err: <other package's type or value>}
and Read and Write may return an OpError containing other OpError when
an application uses io.Copy or similar:
OpError{/* for io.Reader */, Err: &OpError{/* for io.Writer */}}
When an endpoint is created for connection-oriented byte-stream
protocols, Read may return an io.EOF when the connection is closed by
remote endpoint.
Fixes #4856.
A series of changes:
- net: fix inconsistent error values on Dial, Listen partially
https://go.googlesource.com/go/+/89b7c66d0d14462fd7893be4290bdfe5f9063ae1
- net: fix inconsistent error values on Read
https://go.googlesource.com/go/+/ec1144423f45e010c72363fe59291d43214b6e31
- net: fix inconsistent error values on Write
https://go.googlesource.com/go/+/11b5f98bf0d5eb8854f735cc332c912725070214
- net: fix inconsistent error values on Close
https://go.googlesource.com/go/+/310db63c5bc121e7bfccb494c01a6b91a257e7fc
- net: fix inconsistent error values on Accept
https://go.googlesource.com/go/+/4540e162b1aefda8157372764ad3d290a414ef1d
- net: fix inconsistent error values on File
https://go.googlesource.com/go/+/885111365ba0a74421059bfbd18f4c57c1e70332
- net: fix inconsistent error values on setters
https://go.googlesource.com/go/+/2173a27903897c481b0a0daf3ca3e0a0685701db
- net: fix inconsistent error values on Interface
https://go.googlesource.com/go/+/456cf0f22c93e1a6654980f4a48a564555f6c8a2
- net: fix inconsistent error values on Lookup
https://go.googlesource.com/go/+/0fc582e87942b2e52bed751b6c56660ba99e9a7d
- net: add Source field to OpError
https://go.googlesource.com/go/+/afd2d2b6df3ebfe99faf347030f15adfdf422fa0
Change-Id: Id678e369088dc9fbe9073cfe7ff8a8754a57d61f
Reviewed-on: https://go-review.googlesource.com/9236
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-21 06:20:15 -06:00
|
|
|
if err != nil {
|
|
|
|
err = os.NewSyscallError("sendfile", err)
|
|
|
|
}
|
2012-06-25 18:26:19 -06:00
|
|
|
return written, err, written > 0
|
|
|
|
}
|