4 ms·
Would splice help? http://en.wikipedia.org/wiki/Splice_(system_call) http://en.wikipedia.org/wiki/Splice_(system_call)
by tonfa 16y ago
Would splice help? http://en.wikipedia.org/wiki/Splice_(system_call) http://en.wikipedia.org/wiki/Splice_(system_call)
- FooBarWidget 16y agoUnfortunately not, splice requires that one of the fds is a file. I want to forward data from a socket to another.
- wmf 16y agoThat's not what the man page says. AFAIK HAProxy splices from one socket to another.
- fragmede 16y agoIn 2.6.31, support was added to > Allow splice(2) to work when both the input and the output is a pipe. http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=7c77f0b3f9208c339a4b40737bb2cb0f0319bb8d http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6...
- alexgartrell 16y agoThis goes half way toward solving the problem. What most people want is a pure socket->socket. As evidenced by haproxy, you must still use a pipe intermediary, requiring multiple data copies (on the plus side, it's still fast). ret = splice(fd, NULL, b->pipe->prod, NULL, max, SPLICE_F_MOVE|SPLICE_F_NONBLOCK); edited to add (because I couldn't reply): It almost certainly is faster than read/write, because it's straight memcpy's (which are pretty fast on modern hardware), instead of memcpy's, context switching, special read()/write() logic, and other stuff.
- FooBarWidget 16y agoIs that still faster than userspace forwarding with read() and write()?
- caf 16y agoThe pipe intermediary doesn't necessarily add a copy. You SPLICE_F_MOVE the data from the first fd to the pipe, then SPLICE_F_MOVE it from the pipe to the second fd. If either or both of those can be done zero-copy, they will be. The pipe intermediary is just a way of holding onto the reference-counted pages.
- ithkuil 16y agoThe sendfile manpage says: " Presently (Linux 2.6.9): in_fd, must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket); and out_fd must refer to a socket. " so apparently you cannot use sendfile for your scenario.
- berntb 16y agoI hadn't seen splice, it was cool. But what really blew my mind was the discussion of a system call on Wikipedia. :-) To put it into the wikipedia is obvious I guess, but I just fell in love with the net again. Thank you. Edit: I really should do some low level stuff again.
- gruseom 16y agoI'm glad you posted that. It links to the following superb email thread about slice(), tee(), and other zery-copy magic: http://kerneltrap.org/node/6505 http://kerneltrap.org/node/6505