Linux系统内核性能调优详细说明

系统内核优化

ulimit

设置系统打开文件数设置,解决高并发下 too many open files 问题。此选项直接影响单个进程容纳的客户端连接数。 Soft open files 是Linux系统参数,影响系统单个进程能够打开最大的文件句柄数量,这个值会影响到长连接应用如聊天中单个进程能够维持的用户连接数, 运行ulimit -n能看到这个参数值,如果是1024,就是代表单个进程只能同时最多只能维持1024甚至更少(因为有其它文件的句柄被打开)。如果开启4个进程维持用户连接,那么整个应用能够同时维持的连接数不会超过4*1024个,也就是说最多只能支持4x1024个用户在线可以增大这个设置以便服务能够维持更多的TCP连接。 Soft open files 修改方法:

ulimit -HSn 102400

这只是在当前终端有效,退出之后,open files 又变为默认值。

将ulimit -HSn 102400写到/etc/profile中

这样每次登录终端时,都会自动执行/etc/profile。

令修改open files的数值永久生效。

修改配置文件:/etc/security/limits.conf. 在这个文件后加上:

soft nofile 1024000
hard nofile 1024000
root soft nofile 1024000
root hard nofile 1024000

注意,修改limits.conf文件后,需要重启系统生效

sysctl.conf

打开文件 /etc/sysctl.conf,增加以下设置

#该参数设置系统的TIME_WAIT的数量,如果超过默认值则会被立即清除
net.ipv4.tcp_max_tw_buckets = 20000
#定义了系统中每一个端口最大的监听队列的长度,这是个全局的参数
net.core.somaxconn = 65535
#对于还未获得对方确认的连接请求,可保存在队列中的最大数目
net.ipv4.tcp_max_syn_backlog = 262144
#在每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目
net.core.netdev_max_backlog = 30000
#能够更快地回收TIME-WAIT套接字。此选项会导致处于NAT网络的客户端超时,建议为0
net.ipv4.tcp_tw_recycle = 0
#系统所有进程一共可以打开的文件数量
fs.file-max = 6815744
#防火墙跟踪表的大小。注意:如果防火墙没开则会提示error: "net.netfilter.nf_conntrack_max" is an unknown key,忽略即可

net.netfilter.nf_conntrack_max = 2621440 
运行 sysctl -p即可生效。

说明:

/etc/sysctl.conf 可设置的选项很多,其它选项可以根据自己的环境需要进行设置 net.unix.max_dgram_qlen = 100

swoole使用unix socket dgram来做进程间通信,如果请求量很大,需要调整此参数。系统默认为10,可以设置为100或者更大。 或者增加worker进程的数量,减少单个worker进程分配的请求量。 net.core.wmem_max

修改此参数增加socket缓存区的内存大小

net.ipv4.tcp_mem  =   379008       505344  758016
net.ipv4.tcp_wmem = 4096        16384   4194304
net.ipv4.tcp_rmem = 4096          87380   4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_tw_reuse

是否socket reuse,此函数的作用是Server重启时可以快速重新使用监听的端口。如果没有设置此参数,会导致server重启时发生端口未及时释放而启动失败 net.ipv4.tcp_tw_recycle

使用socket快速回收,短连接Server需要开启此参数。此参数表示开启TCP连接中TIME-WAIT sockets的快速回收,Linux系统中默认为0,表示关闭。打开此参数可能会造成NAT用户连接不稳定,请谨慎测试后再开启。 其他重要配置

net.ipv4.tcp_syncookies=1
net.ipv4.tcp_max_syn_backlog=81920
net.ipv4.tcp_synack_retries=3
net.ipv4.tcp_syn_retries=3
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 20000 65000
net.ipv4.tcp_max_tw_buckets = 200000
net.ipv4.route.max_size = 5242880

开启CoreDump

设置内核参数

kernel.core_pattern = /data/core_files/core-%e-%p-%t 
通过ulimit -c命令查看当前coredump文件的限制

ulimit -c 
如果为0,需要修改/etc/security/limits.conf,进行limit设置。

开启core-dump后,一旦程序发生异常,会将进程导出到文件。对于调查程序问题有很大的帮助 查看配置是否生效

如:修改net.unix.max_dgram_qlen = 100后,通过

cat /proc/sys/net/unix/max_dgram_qlen 
如果修改成功,这里是新设置的值。

————————————————————————————————————————————————————————————————————————————————————————————————————————————————

做过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_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
"TcpNumConnections"=dword:00fffffe
;修改分页池最大使用值被系统回收
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management]
"PoolUsageMaxium"=dword:00000032
;修复系统支持最大网络吞吐量
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer]
"MaxFreeConnections"=dword:00001000
"MinFreeConnections"=dword:00000100

原文参考:https://blog.csdn.net/hexieshangwang/article/details/47253241 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值