`

通讯系统经验谈【二】解读内核参数 - socket/文件句柄资源限制参数

 
阅读更多

在本系列的第一篇(http://maoyidao.iteye.com/blog/1744277)中介绍了TCP状态以及服务器上常出现的TIME_WAIT和CLOSE_WAIT状态的成因、影响和解决方法。本篇主要解读在一台并发15万连接的HTTP服务上的系统配置

 

Linux系统资源限制

1. 最大文件数

查看进程允许打开的最大文件句柄数:ulimit -n

查看进程所占的文件描述符: lsof -p xxx | wc -l

设置进程能打开的最大文件句柄数:ulimit -n xxx 

 

2. ulimit -n vs. file-max ?

简单的说ulimit -n控制进程级别能够打开的文件句柄的数量max-file表示系统级别的能够打开的文件句柄的数量 

 

ulimit -n的设置在重启机器后会丢失,因此需要修改limits.conf的限制,limits.conf中有两个值soft和hard,soft代表只警告,hard代表真正的限制

 

*               soft    nofile          150000
*               hard    nofile          150000

这里我们把soft和hard设置成一样的。 

 

 

“cat /proc/sys/fs/file-max”,或“sysctl -a | grep fs.file-max”查看系统能打开的最大文件数。查看和设置例如:

 

[root@vm014601 ~]# sysctl -a |grep fs.file-max
fs.file-max = 200592
[root@vm014601 ~]# echo "fs.file-max = 2005920" >> /etc/sysctl.conf 
[root@vm014601 ~]# sysctl -p
[root@vm014601 ~]# cat /proc/sys/fs/file-max                        
2005920

file-nr是只读文件,第一个数代表了目前分配的文件句柄数;第二个数代表了系统分配的最大文件句柄数;比如线上系统查看结果:

 

# cat /proc/sys/fs/file-max
1106537
# cat /proc/sys/fs/file-nr     
1088  0       1106537
# lsof | wc -l
1506

可以看到file-nr和lsof的值不是很一致,但是数量级一致。为什么会不一致?原因如下:

 

 写道
lsof是列出系统所占用的资源,但是这些资源不一定会占用打开文件号的.
比如共享内存,信号量,消息队列,内存映射.等,虽然占用了这些资源,但不占用打开文件号.
 

我曾经在前端机上很长时间都无法得到lsof | wc -l 的结果,这个时候可以通过file-nr粗略的估算一下打开的文件句柄数。

3. sysckernel.threads-max

指定了内核所能使用的线程(所有进程打开线程之和)的最大数目,通过命令 “cat /proc/sys/kernel/threads-max” 查看当前值。查看和设置例如:

 

sysctl -a | grep threads
vm.nr_pdflush_threads = 2
kernel.threads-max = 229376

 

本厂系统配置允许打开的线程数 > 229k

 

如果此值设小了会导致:-bash: fork: Resource temporarily unavailable

 

4. 为什么有限制?

为什么Linux内核对文件句柄数、线程和进程的最大打开数进行了限制?以及如果我们把它调的太大,会产生什么样的后果?

 

原因1 - 资源问题:the operating system needs memory to manage each open file, and memory is a limited resource - especially on embedded systems.

原因2 - 安全问题:if there were no limits, a userland software would be able to create files endlessly until the server goes down.

 

最主要的是资源问题,为防止某一单一进程打开过多文件描述符而耗尽系统资源,对进程打开文件数做了限制。

 

5. 设置成多少比较合适?

网上有朋友给了估算公式:file-max number = RAM size/10k;

I am not a kernel expert, but as far as I can see, the default for file-max seems to be RAM size divided by 10k. As the real memory used per file handler should be much smaller (size of struct file plus some driver dependent memory), this seems a quite conservative limit. – jofel Apr 19 at 16:43

 

那么一个12G RAM 的前端机可以打开接近1M的文件。真的可以打开1百万个文件吗?

为了试验,基于MINA写了一个NIO服务,在一个服务上创建了50万活跃率约为1%的TCP连接。观察内存使用 < 4.5G,可以粗略认为单个socket连接使用内存小于10K。因此可以用上面的公式来简单估算。

 

6. Orphan socket

不属于任何进程的socket叫orphan socket。这里顺便一下讨论orphan socket,因为很多网络资源不足导致的错误都和“孤儿socket”有关。

 

6.1 Orphan socket是怎么产生的呢?

网上没有明确的说明,我们做一个线上调查:

 

[maoyidao@03701 ~]# netstat -nap | awk '/^tcp/  {++S[$NF]} END {for(a in S) print a, S[a]}'
2976/sshd 1
11065/gearman 1
- 2166
32726/java 31455
25554/scribed 4

[maoyidao@03701 ~]# netstat -nap | awk '/^tcp/ {if($NF == "-") {++S[$6]}}  END {for(a in S) print a, S[a]}'
TIME_WAIT 451
FIN_WAIT1 655
ESTABLISHED 118
FIN_WAIT2 102
SYN_RECV 249
CLOSING 2
LAST_ACK 619
 

可以看到任何一个TCP stat情况下都有可能产生“orphan socket”,但多数是在建立过程当中,以及断开连接中的socket。

 

可以通过以下参数减少orphan socket的产生。

sysctl -a | grep orphan

net.ipv4.tcp_orphan_retries = 0

net.ipv4.tcp_max_orphans = 65536

 

6.2 Orphan socket要消耗多少系统资源?

网上的说法是一个orphan socket需要占用64K内存,查到该说法的出路来自这里:http://www.kernel.org/doc/man-pages/online/pages/man7/tcp.7.html

tcp_max_orphans (integer; default: see below; since Linux 2.4)

              The maximum number of orphaned (not attached to any user file handle)

              TCP sockets allowed in the system.  When this number is exceeded, the

              orphaned connection is reset and a warning is printed.  This limit

              exists only to prevent simple denial-of-service attacks.  Lowering this

              limit is not recommended.  Network conditions might require you to

              increase the number of orphans allowed, but note that each orphan can

              eat up to ~64K of unswappable memory.  The default initial value is set

              equal to the kernel parameter NR_FILE.  This initial default is

              adjusted depending on the memory in the system.

 

难道Orphan Socket占用的资源比普通socket多?maoyidao还没有来得及试验,但从理论上讲,占用资源多少和是否是orphan socket是无关的。

 

7. Out of socket memory

谈到Orphan Socket就需要提一下“Out of socket memory”错误。网上已有很清晰的说明:http://jaseywang.me/2012/05/09/%E5%85%B3%E4%BA%8E-out-of-socket-memory-%E7%9A%84%E8%A7%A3%E9%87%8A-2/

摘录一段:

cat /proc/sys/net/ipv4/tcp_mem

69618   92825   139236

第一个数字表示,当 tcp 使用的 page 少于 69618 时,kernel 不对其进行任何的干预

第二个数字表示,当 tcp 使用了超过 92825 的 pages 时,kernel 会进入 “memory pressure”

第三个数字表示,当 tcp 使用的 pages 超过 139236 时,我们就会看到题目中显示的信息

 

maoyidao注:tcp_mem里的数字是page数,转换成字节需要看下系统的page size有多大:

getconf PAGESIZE

4096

 

看看本厂的设置:

cat /proc/sys/net/ipv4/tcp_mem

196608  262144  393216

 

可以看到我们设置的比较大,这是针对前端机的一些优化。再看一下系统当前的情况

[maoyidao@03701 ~]# cat /proc/net/sockstat

sockets: used 30755

TCP: inuse 31684 orphan 1166 tw 600 alloc 31866 mem 5838

UDP: inuse 4 mem 0

RAW: inuse 0

FRAG: inuse 0 memory 0

 

 

下一篇继续讨论其他内核参数。

分享到:
评论
1 楼 qwe1234567890 2014-06-23  

相关推荐

Global site tag (gtag.js) - Google Analytics