Linux系统内核性能调优

教程发布:风哥 教程分类:ITPUX技术网 更新日期:2022-02-12 浏览学习:725

Linux系统内核性能调优

做过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

----------
linux内核调优参考

对于新部署的机器,需要做一些基本的调优操作,以更改一些默认配置带来的性能问题

1 修改打开文件数

root@mysql:/data/tools/db# vim /etc/security/limits.conf

* soft nofile 65535
* soft nproc 65535
* hard nofile 65535
* hard nproc 65535

root@mysql:/data/tools/db# cd /etc/security/limits.d/

root@mysql:/etc/security/limits.d# vim 90-nofile.conf

* soft nofile 65535
* hard nofile 65535
root soft nofile unlimited

2 内核方面的调优

root@mysql:~# cat /etc/sysctl.conf

net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 32768
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_wmem = 8192 131072 16777216
net.ipv4.tcp_rmem = 32768 131072 16777216
net.ipv4.tcp_mem = 786432 1048576 1572864
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.ip_conntrack_max = 65536
net.ipv4.netfilter.ip_conntrack_max=65536
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=180
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384

vm.swappiness=10

修改完毕后,重启新机器即可。
-------
inux内核调优基本方法
2016-04-01 17:09 100人阅读 评论(0) 收藏 举报
Linux内核调优
网络

调整linux内核参数以便满足高并发访问,解决大量time_wait占用太多本地端口导致的Cannot assign requested address 问题。

客户端与服务端每建立一个连接,客户端一侧都会占用一个本地端口(假设没有启用SO_REUSEADDR选项),本地端口数量是有限制的(默认是net.ipv4.ip_local_port_range=32768 61000),也就是说在没设置socket的SO_REUSEADDR选项时,一台Linux服务器作为客户端(注意是作为客户端)默认只能建立大概3万个TCP连接(服务端没有这个限制),可以更改net.ipv4.ip_local_port_range增大作为客户端可发起的并发连接数,但最多不会超过65535个(服务端没有这个限制)。

当Linux服务器作为客户端频繁建立TCP短连接时,本地会可能会产生很多TIME_WAIT状态的连接,客户端侧的TIME_WAIT状态的连接会占用一个本地端口直到达到2MSL(最长分解生命期)的时间,这样会导致本地端口被暂时占用,当短连接建立速度过快时(例如做压测时),会导致Cannot assign requested address错误,解决办法有几种,比如像下面这样设置端口复用(复用TIME_WAIT状态的连接)。

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

net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1

运行 sysctl -p即可生效

说明:

/etc/sysctl.conf 可设置的选项很多,其它选项可以根据自己的环境需要进行设置
打开文件数

设置系统打开文件数设置,解决高并发下 too many open files 问题。此选项直接影响单个进程容纳的客户端连接数。

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

Soft open files 修改方法:

(1)ulimit -HSn 102400

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

(2)将ulimit -HSn 102400写到/etc/profile中,这样每次登录终端时,都会自动执行/etc/profile。

(3)令修改open files的数值永久生效,则必须修改配置文件:/etc/security/limits.conf. 在这个文件后加上:

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

本文标签:
网站声明:本文由风哥整理发布,转载请保留此段声明,本站所有内容将不对其使用后果做任何承诺,请读者谨慎使用!
【上一篇】
【下一篇】