Linux上的套接字性能 [英] Socket performance on linux

查看:98
本文介绍了Linux上的套接字性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继我上一期提问之后:

使用带有套接字的Javas对象流的性能问题

我正在研究Linux上的套接字性能。通过上面的例子,我的往返时间约为65μsec。如果我在文件系统上制作两个fifo,则会降低到~45μsec。使用localhost套接字的额外时间必须是因为我正在访问网络堆栈。

I'm looking at socket performance on Linux. With the above example I get a round trip time of ~65μsec. If I make two fifos on the file system this goes down to ~45μsec. The extra time using localhost sockets must be because I'm hitting the network stack.

是否有一些操作系统配置可以使localhost套接字与命名管道一样快?

Is there some OS configuration that can make a localhost socket go as fast as a named pipe?

uname -a
Linux fiatpap1d 2.4.21-63.ELhugemem #1 SMP Wed Oct 28 23:12:58 EDT 2009 i686 athlon i386 GNU/Linux

提前致谢!

推荐答案


通过上面的例子,我的往返时间约为65μsec。如果我在文件系统上制作两个fifo,则会降低到~45μsec。使用localhost套接字的额外时间必须是因为我正在访问网络堆栈。

With the above example I get a round trip time of ~65μsec. If I make two fifos on the file system this goes down to ~45μsec. The extra time using localhost sockets must be because I'm hitting the network stack.

是的,这是预期的。

FIFO是相当原始的通信方法。他们的状态基本上是一个bool变量。读取和写入通过固定大小的相同预分配缓冲区。因此,OS可以并且确实优化了操作。

FIFOs are rather primitive communication method. Their state is essentially a bool variable. Reads and writes go through the same pre-allocated buffer of fixed size. Thus the OS can and does optimize the operations.

套接字更复杂。他们拥有完整的TCP状态机。缓冲是动态和双向的(recv,send是单独缓冲的)。这意味着当你在本地套接字中写入内容时,你几乎总是涉及某种动态内存管理。 Linux试图尽可能地避免这种情况:在整个地方实施零拷贝/单拷贝技巧。然而很明显,因为调用必须通过更多的代码,所以它们会更慢。

Sockets are more complex. Their have full fledged TCP's state machine. The buffering is dynamical and bidirectional (recv, send are buffered separately). That means when you write something into local socket, you pretty much always have some sort of dynamic memory management involved. Linux tries to avoid that as much as possible: zero-copy/single-copy tricks are implemented all over the place. Yet obviously since the calls have to go through more code they would be slower.

最后,考虑到与FIFO比较多少套接字,坦率地说,20us的区别是关于Linux'套接字性能有多好的说法。

In the end, considering how much more sockets are compared to FIFOs, 20us difference frankly is a statement about how good the Linux' socket performance is.

PS 65us rtt =〜35us在一个方向。 1s / 35us =每秒30K包。对于没有使用单一连接进行优化的网络代码,听起来不错。

P.S. 65us rtt = ~35us in one direction. 1s/35us =~ 30K packets per second. For network code without optimizations using single connection that sounds about right.

这篇关于Linux上的套接字性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆