-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: net: TCPConn supports Writev #13451
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
Comments
I want to explain why writev is important for media streaming server:
I have use cpu and mem profile to fix the bottleneck, but the memory copy and syscall by use netFD.Write hurts the performance. |
if has writev, it's easy for us to implement zero copy service.... |
@ianlancetaylor // reader & writer goroutine call write concurrency, must lock.
lock.Lock()
w.Write(head)
w.Write(data)
lock.Unlock() but the inner netFD has fd.writeLock(), so caller must do a wasting lock.Lock(). // Write is atomic, and goroutine safe
w.Write(head, data)
func (w *Writer) Write(data ...[]byte) (n int, err error) |
For streaming service, for example, to send a video packet to RTMP(over TCP) client, the video maybe:
Then we need to add some small header every some video bytes, for instance, 10k:
RIght now, we send data in a very very slow way:
Because the syscall is expensive than copy buffer:
When golang support writev, we can send in a syscall and without copy to a big buffer.
|
h0, p0, etc all derive from a single []byte slice, why do you slice them up, then copy them back together? Sending the original slice would avoid the copy, and avoid extending the net.Conn interface. |
@davecheney Nop, only the p0,p1,...,pN is slice from video, the h0,h1,...,hN is another slice. That is, we slice the video payload:
But the headers is another slice:
We should interleave the header and payload to a big buffer:
Because each header is belong to its payload, for instance, hN is only for pN. When use c/c++ to write to socket, we can use writev:
Does that make sense? |
I'm fine with adding a Writev call to the net package. I'm not aware of anybody actually working on it. |
@ianlancetaylor Great~ I create SRS, and rewrite it by golang go-oryx, and the benchmark tool srs-bench for streaming server. SRS can serve 7.5K clients, use 1CPU(about 80% usage); the bandwidth is about 3.75Gbps. SRS is really very high efficient for use writev to avoid copy and use little syscall. But SRS is single process model, for steaming server is too complex. Nginx-RTMP which is a plugin in nginx, which only support 2k clients per CPU, but nginx-rtmp support multiple-processes. I create go-oryx, because I want to use the multile processes feature of golang. Right now, after lots of performance refine, the go-oryx can support 8k by 4CPU. If golang support writev, I think the performance can improve 200% to 400%, that is about 16k to 32k clients use 4CPU. It's really awesome. |
@davecheney @ianlancetaylor What's the state of this issue now? Accept or postpone? Any plan? |
@winlinvip, are you sure your problem is Writev and not, say, allocations? Some of your examples above look very allocation-happy. Before accepting this proposal, I'd want to see before & after programs with numbers and profiles to show that it helps enough. There will definitely be pushback about expanding the net package's API, which many feel is already too large. That is, convince us with data perhaps. Implement this in the net package and then show us numbers with how much it helped. We have to be able to reproduce the numbers. |
@bradfitz I try the no-copy version, to send the []byte |
@bradfitz And I think the writev is not a new API or language feature, it exists on all linux, unix and unix-like os. It's really very useful for high performance server, I found nginx also use writev:
|
Seems writev was introduced at 2001, https://en.wikipedia.org/wiki/Vectored_I/O
|
@winlinvip, can you try to modify variant 6 to avoid allocations of group buffers by populating buf (https://github.com/winlinvip/go-writev/blob/master/golang/server.go#L123) directly. I think this will be roughly equivalent to calling writev on multiple buffers. What your benchmark shows for this program? |
@kostya-sh Did u compare the c++ version? The allocation is not the bottle-neck, but the memory copy and syscall. |
It's really a very basic problem for server. Eventhough the golang is modern language, but it's compile to binary code and execute on linux, the memory copy and syscall is always the bottle-neck for server, I have profile it. |
Please post a profile here. |
@winlinvip, by removing allocations from your test program (variant 6) you will end up with a single syscall per write. This way you can estimate how fast would be your Go program if it used writev. If improvement of Go application is significant then it could be a pro argument for adding writev. if you can implement writev in your local version of Go standard library, test the performance and post numbers here it would be even better. |
@kostya-sh I will try to add my writev version to golang stadard library, and give the result. |
I will test the real streaming server go-oryx later. |
@winlinvip It seems syscalls grow, I guess there are other side effect syscalls arisen. From the diff maybe some of these calls ?
|
@ggaaooppeenngg, selectGoImpl is about |
A similar implemnetation for writev by coroutine:
|
@winlinvip your C++ version |
@bradfitz Bellow is my research result by go-oryx:
Conclude:
|
Will golang accept the proposal to support netFD.Writev? |
@winlinvip, thanks for prototyping. Can you make a small stand-alone program for profiling that can do either multiple writes or writev (controlled by a flag) and link to the code so others can reproduce your results and investigate more? I also have an API idea. Instead of adding API like Then we can add the writev optimization gradually to different OS/conn types over time. |
@minux ah. I misread it as writing one buffer to multiple fd's (and I wasn't suggesting modifying io.MultiWriter itself -- but rather adding a specialized function [which is now moot] to the net package) |
And, should add new API method to support sending multiple WSABuf with WSASend/WSASendto (similar to writev() and struct iovec) for windows. // src/net/fd_windows.go:L502 Single WSABuf sending
func (fd *netFD) Write(buf []byte) (int, error) {
if err := fd.writeLock(); err != nil {
return 0, err
}
defer fd.writeUnlock()
if raceenabled {
raceReleaseMerge(unsafe.Pointer(&ioSync))
}
o := &fd.wop
o.InitBuf(buf) // should init multiple WSABuf
n, err := wsrv.ExecIO(o, "WSASend", func(o *operation) error {
// #### here arg 2,3 should be *WSAbuf, count ###
return syscall.WSASend(o.fd.sysfd, &o.buf, 1, &o.qty, 0, &o.o, nil)
})
if _, ok := err.(syscall.Errno); ok {
err = os.NewSyscallError("wsasend", err)
}
return n, err
} |
@bradfitz ping. Thank you! |
@mbeacom, sorry, this also will not make Go 1.7. The 1.7 development freeze is May 1st and this isn't ready. |
ping @bradfitz |
@bradfitz What is the likelihood of this moving into the Go 1.8 Maybe milestone? |
@ianlancetaylor and I are happy with this. API-wise, though, I'm thinking: package net
// Buffers contains zero or more runs of bytes to write.
//
// On certain machines, for certain types of connections, this is optimized
// into an OS-specific batch write operation.
type Buffers [][]byte
func (v Buffers) WriteTo(w io.Writer) (n int64, err error) |
@bradfitz would we do (Your proposed docstring says that Buffers is "bytes to write".) |
Perhaps. But let's leave readv for another day. |
@bradfitz: What is the expected behavior if WriteTo is called with zero runs of bytes to write? I would prefer that it is documented. (Both "does nothing" and "calls write with a zero-length buffer" are acceptable responses, I think.) |
Why would a user care? I'd prefer the future implementation freedom and documentation clarity from leaving it unspecified. Also, do you mean zero overall or an entry with zero length? Because currently (for ease of implementation), I do nothing if the overall vector is zero, but if one of the vector entries is empty, I still include it in the set, but zero length. I'd prefer to not get into that level of detail in docs. |
I sent out https://golang.org/cl/29951 It works, but the thing I don't like about it is But I can't make |
CL https://golang.org/cl/29951 mentions this issue. |
I think a user would care because we might have the behavior that a Conn that is closed or otherwise unusable might or might not return an error if you try to write zero bytes. That seems confusing to me. I guess it looks like we currently also don't promise what will happen if Write is called with a zero-length slice? |
The initial validity check in the CL (
Exactly. Zero-length reads and writes have historically been very ill-defined in Go. In general, we promise nothing. |
👍 Nice writev~ |
CL https://golang.org/cl/30102 mentions this issue. |
CL https://golang.org/cl/32371 mentions this issue. |
Updates #13451 Change-Id: I2c3c66d9532c16e616c476e2afe31b3ddc0a8d79 Reviewed-on: https://go-review.googlesource.com/32371 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]>
I have search go-nuts and google about the writev for unix, seems go not support it yet. And I found a project vectorio which support writev but not work.
I am rewriting the srs to go-oryx. SRS can support 7.5k clients per CPU, while golang version only support 2k, for the media streaming server need to delivery the video or audio packet to different parts, then use writev to send to client to avoid bytes copy.
I try to use reflect to implements the writev, but I found it's possible for the pollDesc is not exported, the commit is here.
Does go plan to support writev(netFD.Writev)?
The text was updated successfully, but these errors were encountered: