为了达到最大网络吞吐,socket send buffer size(SO_SNDBUF)不应该小于带宽和延迟的乘积。
之前我遇到2个性能问题,都和SO_SNDBUF设置得太小有关。
但是,写程序的时候可能并不知道把SO_SNDBUF设多大合适,而且SO_SNDBUF也不宜设得太大,浪费内存啊。
于是,有OS提供了动态调整缓冲大小的功能,这样应用程序就不用再对SO_SNDBUF调优了。
(接受缓冲SO_RCVBUF也是类似的问题,不应该小于带宽和延迟的乘积)。

On Linux:
Linux从2.4开始支持接收缓冲和发送缓冲的动态调整。
http://www.man7.org/linux/man-pages/man7/tcp.7.html
------------------------------------------------------------
       tcp_rmem (since Linux 2.4)
              This is a vector of 3 integers: [min, default, max].  These
              parameters are used by TCP to regulate receive buffer sizes.
              TCP dynamically adjusts the size of the receive buffer from
              the defaults listed below, in the range of these values,
              depending on memory available in the system.
...
       tcp_wmem (since Linux 2.4)
              This is a vector of 3 integers: [min, default, max].  These
              parameters are used by TCP to regulate send buffer sizes.  TCP
              dynamically adjusts the size of the send buffer from the
              default values listed below, in the range of these values,
              depending on memory available.
------------------------------------------------------------

  1. [root@node2 ~]# cat /proc/sys/net/ipv4/tcp_rmem
  2. 4096 87380 4194304
  3. [root@node2 ~]# cat /proc/sys/net/ipv4/tcp_wmem
  4. 4096 16384 4194304

On Windows:
Windows上其实有类似的机能,但是Windows的文档太糟糕了,我废了不少劲才找到一些旁证。
从Vista开始Windows引入接受窗口的自动调整
http://blogs.msdn.com/b/wndp/archive/2007/07/05/receive-window-auto-tuning-on-vista.aspx

从Win7和Win2008R2开始Windows引入送信缓冲的自动调整
https://technet.microsoft.com/zh-cn/subscriptions/ms740642.aspx
------------------------------------------------------------ 
Updated for Windows 7 and Windows Server 2008 R2
...
Dynamic send buffering for TCP was added on Windows 7 and Windows Server 2008 R2. As a result, the use of the SIO_IDEAL_SEND_BACKLOG_CHANGE andSIO_IDEAL_SEND_BACKLOG_QUERY IOCTLs
are needed only in special circumstances. For more information, see SIO_IDEAL_SEND_BACKLOG_QUERY.
------------------------------------------------------------

需要注意的是,如果应用设置了SO_SNDBUF,Dynamic send buffering会失效 。
https://msdn.microsoft.com/en-us/library/windows/desktop/bb736549(v=vs.85).aspx
------------------------------------------------------------
Dynamic send buffering for TCP was added on Windows 7 and Windows Server 2008 R2. By default,
dynamic send buffering for TCP is enabled unless an application sets the SO_SNDBUF socket
option on the stream socket.
------------------------------------------------------------

我在MSDN上没有找到正式介绍这个功能的页面(也许就没有这样的页面),所以也不知道它的自动调整是怎么个调法,范围是多少。而且,通过对Win7和Windows
Server 2008 R2的测试我也没看到送信缓冲自动调整的效果,这个效果我只在Windows
2012上看到了。

测试:
下面是我的测试,主要针对送信缓冲的。

以下是各OS中送信缓冲的缺省值
OS                      送信缓冲的缺省值(通过getsockopt(SO_SNDBUF)获取) 
Window7:            8k 
Windows2003:     8k 
Windows2008:     8k 
Windows8:          64k 
Windows2012:     64k

测试方法:
1)机器A(Windows)通过TCP socket向机器B发送100MB数据。
2)机器A每次send()向socket写入8K字节。
3)机器A的程序设置不同的SO_SNDBUF,查看总送信时间的变化。

测试环境1:
Host A: Windows 2012(x64)
Host B: RHEL6(x64)
Network:1Gbit LAN

Result(execute time):
default(64K),                  1.118s(送信缓冲的自动调整生效) 
set SO_SNDBUF to 32K,   3.295s 
set SO_SNDBUF to 64K,   2.048s 
set SO_SNDBUF to 128K, 1.404s 
set SO_SNDBUF to 256K, 1.290s

从上面可以看出, Windows 2012中送信缓冲的自动调整还是很有效果的。
注)如果使用Windows而不是Linux作为客户端,效果也是一样的

测试环境2:
Host A: Windows 2008 R2(x64)
Host B: RHEL6(x64)
Network:1Gbit LAN

Result(execute time):
default(8K),                   7.370s 
set SO_SNDBUF to 32K,  4.159s 
set SO_SNDBUF to 64K,  2.875s 
set SO_SNDBUF to 128K, 1.593s 
set SO_SNDBUF to 256K, 1.324s 

对Windows 2008 R2,不知道送信缓冲的自动调整没有生效("netsh
winsock show autotuning"是生效了的),还是8K初始值的起点太低,反正性能不如人意。

结论:
较新的OS都支持socket buffer的自动调整,不需要应用程序去调优。但对Windows 2012(和Win8)以前的Windows,为了达到最大网络吞吐,还是要应用程序操心一下SO_SNDBUF的设置。

关于socket buffer size的调优的更多相关文章

  1. hfile.block.cache.size - hbase调优

    1.一个regionserver上有一个blockcache和N个memstore,它们的大小之和必须小于heapsize* 0.8,否则hbase不能启动,因为仍然要留有一些内存保证其它任务的执行. ...

  2. Linux网卡调优篇-禁用ipv6与优化socket缓冲区大小

    Linux网卡调优篇-禁用ipv6与优化socket缓冲区大小 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一般在内网环境中,我们几乎是用不到IPV6,因此我们没有必要把多不 ...

  3. MapReduce shuffle过程剖析及调优

    MapReduce简介 在Hadoop MapReduce中,框架会确保reduce收到的输入数据是根据key排序过的.数据从Mapper输出到Reducer接收,是一个很复杂的过程,框架处理了所有问 ...

  4. linux tcp调优

    Linux TCP Performance Tuning News Linux Performance Tuning Recommended Books Recommended Links Linux ...

  5. Oracle调优之buffer pool相关

    一个oracle block与data buffer中的一个buffer对应.用户进程(server process)负责读取磁盘上的block到data buffer cache中,DEWn进程负责 ...

  6. mysql 数据库缓存调优之解决The total number of locks exceeds the lock table size错误

    环境: mysql5.6.2  主从同步(备注:需操作主库和从库) 一.InnoDB表执行大批量数据的更新,插入,删除操作时会出现这个问题,需要调整InnoDB全局的innodb_buffer_poo ...

  7. nginx调优buffer参数设置

    内容来自 https://blog.tanteng.me/2016/03/nginx-buffer-params/.有空再详细了解 Nginx性能调优之buffer参数设置 打开Nginx的error ...

  8. Linux内核 TCP/IP、Socket参数调优

    Linux内核 TCP/IP.Socket参数调优 2014-06-06  Harrison....   阅 9611  转 165 转藏到我的图书馆   微信分享:   Doc1: /proc/sy ...

  9. socket programming Max size of tcp/ip socket Buffer?

    TCP data is buffered at both sender and receiver. The size of the receiver's socket receive buffer d ...

随机推荐

  1. redis geo操作

    package club.newtech.qbike.trip.domain.service; import club.newtech.qbike.trip.domain.core.Status;im ...

  2. Luogu P4398 [JSOI2008]Blue Mary的战役地图 矩阵哈希

    其实可以二分矩阵边长但是我太懒了$qwq$. 把每个子矩阵扔到$map$里,然后就没了 #include<cstdio> #include<map> #include<i ...

  3. PHP mysqli_fetch_row() 函数

    定义和用法 mysqli_fetch_row() 函数从结果集中取得一行,并作为枚举数组返回. <?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con= ...

  4. Call JMS Web Service

    The content type application/json of the response message does not match the content type of the bin ...

  5. 并发编程入门(三): 使用C++11实现无锁stack(lock-free stack)

    前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 - 有时候是很多时 ...

  6. vue中用watch监听当前路由

    之前做项目时,特别是后台项目,左边都有侧边栏,我们需要做到点击某个侧边栏的项让这个项高亮,之前采用的是给每个项绑定一个值,点击某个项时,就将这个值付给一个变量,在每一项上判断这个变量是否与每项上的值相 ...

  7. html,css,js实现的一个钟表

    效果如图: 实现代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  8. 5.使用Ribbon实现客户端侧负载均衡

                                Ribbon实现客户端侧负载均衡 5.1. Ribbon简介 Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法 ...

  9. legend3---7、videojs的使用配置的启示是什么

    legend3---7.videojs的使用配置的启示是什么 一.总结 一句话总结: 很多东西网上都有现成的,直接拿来用就好,效果是又快又好 1.用auth认证登录的时候报 "validat ...

  10. HearthBuddy遇奥秘解决方法

    https://tieba.baidu.com/g/5808796816 链接: https://pan.baidu.com/s/1NPQTOfxbN_4alP7J-XWuVw 密码: xfj1