linux 网络性能优化

  最近在对程序进行调优,涉及到了网络通信,学习了一下对网络调优的方法,网上的资料很多,针对软件优化方面,大体上主要有两种方式:一是网卡参数,另一个是内核参数。

一. 网卡参数优化

  针对网卡参数优化,需要了解ethtool命令的用法:

# Queries the specified network device for rx/tx ring parameter information.
ethtool -g|--show-ring devname
# Changes the rx/tx ring parameters of the specified network device.
ethtool -G|--set-ring devname [rx N] [rx-mini N] [rx-jumbo N] [tx N]
# Initiates adapter-specific action intended to enable an operator to easily identify the adapter by sight.Typically this involves blinking one or more LEDs on the specific network port.
ethtool -p|--identify devname [N]
# Queries the specified network device for NIC- and driver-specific statistics.
ethtool -S|--statistics devname
# Queries the specified network device for associated driver information.
ethtool -i|--driver devname
# Prints current settings of the specified device.
ethtool devname

比如查看当前网卡的buffer大小:

  

若eth0的rx有丢包问题,则修改buffer大小:

   ethtool -G eth0 rx 2048

 

 二. 内核参数优化

  

#Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536

# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536

# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736

# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296

# 表示socket监听的backlog(监听队列)上限
net.core.somaxconn = 32768
# 内核用于所有类型的连接的默认发送缓冲大小
net.core.wmem_default = 8388608
# 内核用于所有类型的连接的默认接收缓冲大小
net.core.rmem_default = 8388608
# 内核用于所有类型的连接的最大接收缓冲大小
net.core.rmem_max = 16777216
# 内核用于所有类型的连接的最大发送缓冲大小
net.core.wmem_max = 16777216
# tcp报文时间戳
net.ipv4.tcp_timestamps = 0
# 内核放弃连接之前发送SYN+ACK 包的数量
net.ipv4.tcp_synack_retries = 1
# 内核放弃建立连接之前发送SYN包的数量
net.ipv4.tcp_syn_retries = 0
# 是否开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭
net.ipv4.tcp_tw_recycle = 1
# 是否允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭
net.ipv4.tcp_tw_reuse = 1

# 第一个值是为 socket 的发送缓冲区分配的最少字节数。
# 第二个值是默认值(该值会被 wmem_default 覆盖),缓冲区在系统负载不重的情况下可以增长到这个值。
# 第三个值是发送缓冲区空间的最大字节数(该值会被 wmem_max 覆盖)。
net.ipv4.tcp_mem = 94500000 915000000 927000000

# 系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上;如果超过这个数字,孤儿连接将即刻被复位并打印出警告信息;这个限制仅仅是为了防止简单的DoS 攻击,不能过分依靠它或者人为地减小这个值,如果需要修改,在确保有足够内存可用的前提下,应该增大此值。这个数值越大越好,越大对于抗攻击能力越强
net.ipv4.tcp_max_orphans = 3276800

# 用于向外连接的端口范围。缺省情况下很小:32768到61000
net.ipv4.ip_local_port_range = 1024  65535
# 如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间
net.ipv4.tcp_fin_timeout = 10
# 当keepalive起用的时候,TCP发送keepalive消息的频度,默认是是2小时
net.ipv4.tcp_keepalive_time = 100
# 是否开启SYN Cookies,即当SYN等待队列溢出时,是否启用cookies功能
net.ipv4.tcp_syncookies = 1
# 保存的那些尚未收到客户端确认信息的连接请求的最大值;默认为128
net.ipv4.tcp_max_syn_backlog = 8192

# 表示系统同时保持TIME_WAIT的最大数量,如果超过这个数字,TIME_WAIT将立刻被清除并打印警告信息。默 认为180000,改为6000。对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT的最大数量,避免Squid服务器被大量的TIME_WAIT拖死。
net.ipv4.tcp_max_tw_buckets = 20000

常用命令:

  1. 查看当前打开的套接字数量:

    sar -n SOCK

  2. 查看哪个进程的io比较高:

    dstat -top-io

    3. 查看哪个进程占用cpu高:

    dstat -d -r --top-io

 

posted on 2017-02-28 15:47  霏霏暮雨  阅读(892)  评论(0编辑  收藏  举报

导航