`
bachmozart
  • 浏览: 110112 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

TCP Socket Performance issue

阅读更多
1.In general, it is more efficient for the application to write large messages and have them fragmented and reassembled by IP, than to have the application write multiple times.

原因就在于系统调用system_call从应用态转到内核态等系列复杂过程带来的性能的损耗

2.tcp_sendspace

TCP send buffer size can limit how much data the application can send before the application is put to sleep. The TCP socket send buffer is used to buffer the application data in the kernel using mbufs/clusters before it is sent beyond the socket and TCP layer. The default size of this buffer is specified by the parameter tcp_sendspace, but you can use the setsockopt() subroutine to override it.

If the amount of data that the application wants to send is smaller than the send buffer size and also smaller than the maximum segment size and if TCP_NODELAY is not set, then TCP will delay up to 200 ms, until enough data exists to fill the send buffer or the amount of data is greater than or equal to the maximum segment size, before transmitting the packets.

If TCP_NODELAY is set, then the data is sent immediately (useful for request/response type of applications). If the send buffer size is less than or equal to the maximum segment size (ATM and SP switches can have 64 K MTUs), then the application's data will be sent immediately and the application must wait for an ACK before sending another packet (this prevents TCP streaming and could reduce throughput).

TCP发送数据时首先要看发送缓冲区是否有足够的空间存放待发送消息,如果有则复制到发送缓冲区,并且需要等待ack回来才会删除该数据,如果发送缓冲区没有足够空间则会等待或者返回EWOULDBLOCK(非阻塞) ----TCPv2

Note: To maintain a steady stream of packets, increase the socket send buffer size so that it is greater than the MTU (3-10 times the MTU size could be used as a starting point).
If an application does nonblocking I/O (specified O_NDELAY or O_NONBLOCK on the socket), then if the send buffer fills up, the application will return with an EWOULDBLOCK/EAGAIN error rather than being put to sleep. Applications must be coded to handle this error (suggested solution is to sleep for a short while and try to send again).

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics