2013-07-15 16:40:55 -06:00
|
|
|
// Copyright 2009 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 net
|
|
|
|
|
|
|
|
import (
|
2014-01-09 20:33:54 -07:00
|
|
|
"os"
|
|
|
|
"syscall"
|
2013-07-15 16:40:55 -06:00
|
|
|
"time"
|
2014-01-09 20:33:54 -07:00
|
|
|
"unsafe"
|
2013-07-15 16:40:55 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
|
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 := fd.incref(); err != nil {
|
2013-07-15 16:40:55 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.decref()
|
2014-09-11 02:56:58 -06:00
|
|
|
// The kernel expects milliseconds so round to next highest
|
|
|
|
// millisecond.
|
2014-01-09 20:33:54 -07:00
|
|
|
d += (time.Millisecond - time.Nanosecond)
|
2014-09-11 02:56:58 -06:00
|
|
|
msecs := uint32(d / time.Millisecond)
|
2014-01-09 20:33:54 -07:00
|
|
|
ka := syscall.TCPKeepalive{
|
|
|
|
OnOff: 1,
|
2014-09-11 02:56:58 -06:00
|
|
|
Time: msecs,
|
|
|
|
Interval: msecs,
|
2014-01-09 20:33:54 -07:00
|
|
|
}
|
|
|
|
ret := uint32(0)
|
|
|
|
size := uint32(unsafe.Sizeof(ka))
|
|
|
|
err := syscall.WSAIoctl(fd.sysfd, syscall.SIO_KEEPALIVE_VALS, (*byte)(unsafe.Pointer(&ka)), size, nil, 0, &ret, nil, 0)
|
|
|
|
return os.NewSyscallError("WSAIoctl", err)
|
2013-07-15 16:40:55 -06:00
|
|
|
}
|