做过Linux平台性能测试的童鞋平时可能会遇到如下问题:
1、 TCP端口号不够用导致并发上不去(即与服务器端建立新连接失败)
2、 TIME_WAIT状态连接过多导致应用服务器(Nginx、Haproxy、Redis、Tomcat等)性能下降或假死
等等
我们可以通过优化系统内核参数来解决上述问题,优化步骤如下:
Linux 平台
1、 参考附件1中sysctl.conf文件替换或修改系统中/etc/sysctl.conf
意义:
(1) 突破系统最大打开文件描述符数限制(系统级别)
(2) 提高系统网络负载
2、 使用命令“sysctl -p /etc/sysctl.conf”使步骤1修改生效
备注:error: “net.bridge.bridge-nf-call-ip6tables” is an unknown key 解决方法
modprobe bridge
lsmod|grep bridge
3、 修改/etc/security/limits.conf,添加如下内容:
* hard nofile 1024000
* soft nofile 1024000
意义:突破 进程最大打开文件描述符数限制(用户级别)
4、 重新登录服务器使其步骤3修改生效
备注:
(1) 修改过程中需注意:
a. 所有进程打开的文件描述符数不能超过/proc/sys/fs/file-max
b. 单个进程打开的文件描述符数不能超过user limit中nofile的soft limit
c. nofile的soft limit不能超过其hard limit
d. nofile的hard limit不能超过/proc/sys/fs/nr_open
(2) /etc/security/limits.conf里面修改文件描述符数量,建议带上账号,*号代表了所有账户!
1) 新建kdxf运行的账户(一般不使用root账户;现网安全起见一般会禁用root账户直接登录权限,需要从普通账户登录后切换至root)
2) 关闭Linux系统SELINUX和防火墙(需要重启操作系统,SELINUX安全系统控制过严,可能会影响进程访问操作系统某些资源)
修改/etc/selinux/config文件中SELINUX=”“为disabled
关闭防火墙使用命令:
chkconfig iptables off
chkconfig ip6tables off
3) 系统字符集编码配置(系统默认utf-8,在需要的时候可以修改系统字符集,需要重启操作系统)
修改/etc/sysconfig/i18n配置文件中的LANG
LANG="zh_CN.GB18030"
修改/etc/profile,增加两行:
export LANG=zh_CN.GB18030
export LC_ALL=zh_CN.GB18030
4) 产生core文件配置(该配置是应用程序在崩溃时能自动产生一个崩溃core文件,有助于开发定位崩溃原因)
修改/etc/profile,增加一行:
ulimit -c unlimited
修改/etc/sysctl.conf文件,设置
fs.suid_dumpable = 1
运行以下命令使得配置生效
sysctl -p
5) 用户最大进程数配置
Centos5环境修改/etc/security/limits.conf文件
Centos6环境修改/etc/security/limits.d/90-nproc.conf文件
kdxf hard nproc unlimited
kdxf soft nproc unlimited
注:这里kdxf代表运行应用程序的普通系统账户,请根据实际填写!
附件1:sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 12
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 8192
# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
# 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
# Disable IPv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
# Determines how often to check for stale neighbor entries.
net.ipv4.neigh.default.gc_stale_time=120
# Using arp_announce/arp_ignore to solve the ARP Problem
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_announce=2
vm.swappiness = 0
net.ipv4.tcp_max_tw_buckets = 50000
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv4.conf.lo.arp_announce=2
# Controls the application is able to bind to not belong to the local network address
net.ipv4.ip_nonlocal_bind=1
fs.file-max = 1700000
Windows平台
1、 按附件2内容新建tcp.reg注册表文件并执行
意义:修改系统连接数限制
2、 重启系统,使其步骤1修改生效
附件2:tcp.reg
Windows Registry Editor Version 5.00
;修改TCP并发连接数为最大值
[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpipParameters]
"TcpNumConnections"=dword:00fffffe
;修改分页池最大使用值被系统回收
[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerMemory Management]
"PoolUsageMaxium"=dword:00000032
;修复系统支持最大网络吞吐量
[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesLanmanServer]
"MaxFreeConnections"=dword:00001000
"MinFreeConnections"=dword:00000100