socket fd 泄露分析

前提:

首先,了解: /proc/net/sockstat , 和 ss -s 命令

cat /proc/net/sockstat
sockets: used 294
TCP: inuse 35 orphan 0 tw 0 alloc 45 mem 1
UDP: inuse 13 mem 2
UDPLITE: inuse 0
RAW: inuse 4
FRAG: inuse 0 memory 0
sockets: used:已使用的所有协议套接字总量
TCP: inuse:正在使用(正在侦听)的TCP套接字数量。其值≤ netstat –lnt | grep ^tcp | wc –l
TCP: orphan:无主(不属于任何进程)的TCP连接数(无用、待销毁的TCP socket数)
TCP: tw:等待关闭的TCP连接数。其值等于netstat –ant | grep TIME_WAIT | wc –l
TCP:alloc(allocated):已分配(已建立、已申请到sk_buff)的TCP套接字数量。其值等于netstat –ant | grep ^tcp | wc –l
TCP:mem:套接字缓冲区使用量(单位不详。用scp实测,速度在4803.9kB/s时:其值=11,netstat –ant 中相应的22端口的Recv-Q=0,Send-Q≈400)
UDP:inuse:正在使用的UDP套接字数量
RAW:
FRAG:使用的IP段数量

问题

大量的没有关闭的socket fd

如何找到异常的进程?

 lsof -n|awk '{print $2}'| sort | uniq -c | sort -nr | head

但是,当问题很严重的时候,这个命令几乎也执行不出来。

可以直接试试:

ll /proc/*/fd/

看看卡在这里。

需要注意:

fd资源的限制:

系统层面的限制: /proc/sys/fs/file-max

man proc,可得到file-max的描述:
/proc/sys/fs/file-max
              This  file defines a system-wide limit on the number of open files for all processes.  (See
              also setrlimit(2),  which  can  be  used  by  a  process  to  set  the  per-process  limit,
              RLIMIT_NOFILE,  on  the  number  of  files it may open.)  If you get lots of error messages
              about running out of file handles, try increasing this value:

单个进程的限制:

ulimit -n 
muahao@aliyun.com
原文地址:https://www.cnblogs.com/muahao/p/15190174.html