上海isp 发表于 2019-1-27 07:08:50

系统调优:如何解决系统报错too many open files瓶颈

  一、检查系统版本是否手工升级
# uname -r   linux的内核版本号
2.6.32-358.el6.x86_64
# cat /proc/version
Linux version 2.6.32-358.el6.x86_64
# cat /etc/issue系统安装时默认的发行版本
CentOS release 6.4 (Final)
# cat /etc/redhat-release
CentOS release 6.4 (Final)
# yum -y install redhat-lsb
# lsb_release -a
LSB Version::base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID:CentOS
Description:CentOS release 6.4 (Final)
Release:6.4
Codename:Final  关于lsb_release -a和/etc/issue显示的发行版本号不同,原因只有一个:系统内核手动升级了。
  

  对于高并发高http连接的应用程序例如www或Java,会遇上Socket/File: Can’t open so many files或too many open files的问题.
  最好通过使用ulimit -n xx 修改每个进程可打开的文件数,缺省值是1024,
  

  二、如何增大用户打开最大进程数
  ulimit -a 来显示当前的各种用户进程限制
# ulimit -a
core file size          (blocks, -c) 0
data seg size         (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals               (-i) 7777
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues   (bytes, -q) 819200
real-time priority            (-r) 0
stack size            (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes            (-u) 7777
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited  

  系统原始文件
  ulimit命令有分软硬设置,加-H硬限制是实际的限制,加-S软限制是warnning限制,只会做出warning;
# grep '^*' /etc/security/limits.conf
*    soft    65535
*    hard    65535  

  修改方法:
方法一:
1在/etc/rc.local中增加ulimit -SHn 65535
# echo "ulimit -SHn 65535" >> /etc/rc.local
2在/etc/profile中增加ulimit -SHu 65535
# echo "ulimit -SHu 65535" >> /etc/profile
然后source /etc/profile让参数即时生效
方法二:修改limits.conf配置文件生效
nofile(可打开的文件描述符的最大数)和nproc(单个用户可用的最大进程数量)
# grep '^*' /etc/security/limits.conf
*    soft    nproc    65535
*    hard    nproc    65535
*    soft    nofile    65535
*    hard    nofile    65535
退出用户终端重新登录,使配置文件生效
# ulimit -a
open files                      (-n) 65535
max user processes            (-u) 65535  

  用户打开最大进程数还受此文件影响
  limits.conf中的nproc受文件/etc/security/limits.d/90-nproc.conf中nproc值大小制约
  # egrep -v "^#|^$" /etc/security/limits.d/90-nproc.conf
  *          soft    nproc   1024
  root       soft    nproc   unlimited
  修改所有用户打开最大进程数
  限制用户最多打开1024个文件,修改root无用,修改瓶颈进程例如www用户限制才有用。
  # vim /etc/security/limits.d/90-nproc.conf
  *          soft    nproc   65535
  #root       soft    nproc   unlimited
  

  查看对应进程的资源限制#cat /proc/pid号/limits,
# netstat -nutlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp      0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1389/sshd               
tcp      0      0 :::22                     :::*                        LISTEN      1389/sshd            
# cat /proc/1389/limits
Limit                     Soft Limit         Hard Limit         Units   
Max processes             7777               7777               processes
Max open files            1024               4096               files  三、系统总限制
  内核支持进程打开最大文件数
  # cat /proc/sys/fs/file-max
  98309
  内核支持系统目前使用的文件数,
  # cat /proc/sys/fs/file-nr
  480    0    98309
  第一个数表示当前系统已分配使用的打开文件数,第二个数为分配后已释放的(目前已不再使用),第三个数等于file-max。
  一个进程最多使用文件数,在内核2.6.5之前没有nr_open需更改内核中file.fs.max
  # cat /proc/sys/fs/nr_open
  1048576
  # sysctl -a | egrep "fs.file|fs.nr"
  fs.file-nr = 480    0    98309
  fs.file-max = 98309
  fs.nr_open = 1048576
  

  总结:
  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
  1.应对高并发环境高性能大内存环境64G内存,修改nr_open和file_max可以设置超过65535的102400,即100W
  2.修改系统最大打开进程数/文件数,使用此参数的程序配置文件也需要增加对应参数
     如果你使用squid的话,你要在/etc/init.d/squid的文件加入ulimit -SHn 65535.另外,在squid.conf中也要加入max_filedesc 16384
  

  四、查找文件句柄问题的时候,还有一个很实用的程序lsof。可以很方便看到某个进程开了那些句柄,也可以看到某个文件/目录被什么进程占用了。
  # lsof            #sshd进程打开哪些句柄
  COMMAND   PID    USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
  sshd       1389    rootcwd       DIR                8,2   4096          2 /
  sshd       1389    rootrtd       DIR                8,2   4096          2 /
  sshd       1389    roottxt       REG                8,2   526008      23317 /usr/sbin/sshd
  sshd       1389    rootmem       REG                8,2    65928       4222 /lib64/libnss_files-2.12.so
  sshd       1389    rootmem       REG                8,2   240592       4713 /lib64/libnspr4.so
  sshd       1389    rootmem       REG                8,2    14560       4724 /lib64/libplds4.so
  sshd       1389    rootmem       REG                8,2    18720       4723 /lib64/libplc4.so
  # lsof -i:22      #端口22被哪些进程占用
  COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
  sshd   1389 root    3uIPv4   9121      0t0TCP *:ssh (LISTEN)
  sshd   1389 root    4uIPv6   9123      0t0TCP *:ssh (LISTEN)
  sshd    11687 root    3rIPv421241      0t0TCP station253:ssh->NULL:hyperscsi-port (ESTABLISHED)
  #lsof+d /usr/sbin   #/usr/sbin目录下进程正打开的文件
  COMMAND   PID USERFD   TYPE DEVICE SIZE/OFFNODE NAME
  sshd   1389 root txt    REG    8,2   526008 23317 /usr/sbin/sshd
  crond    1474 root txt    REG    8,2    59832 20300 /usr/sbin/crond
  sshd    11687 root txt    REG    8,2   526008 23317 /usr/sbin/sshd
  lsof    11790 root txt    REG    8,2   141048 29342 /usr/sbin/lsof
  lsof    11791 root txt    REG    8,2   141048 29342 /usr/sbin/lsof
  




页: [1]
查看完整版本: 系统调优:如何解决系统报错too many open files瓶颈