首先介绍下Linux系统中"一切都是文件"。
1. Linux系统文件句柄数概念
文件句柄(Windows)
文件描述符(Unix/Linux):file discriptor,fd。对于内核而言,所有打开的文件都是通过文件描述符引用,文件描述符是一个非负整数,变化范围是0~(OPEN_MAX-1)。
其中,OPEN_MAX解释为
The maximum number of files that a process can have open at any time. Must not be less than _POSIX_OPEN_MAX(20)
实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。
2. 查询Linux系统文件句柄数
# 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) 7179 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 65535 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 7179 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
3. 配置文件
单一程序(进程)级别的限制
/etc/security/limits.conf * soft nofile 2048 * hard nofile 32768 * soft nofile 2048 * hard nofile 32768 第一列表示域(domain),可以使用用户名(root等),组名(以@开头),通配置*和%,%可以用于%group参数。 第二列表示类型(type),值可以是soft或者hard 第三列表示项目(item),值可以是core, data, fsize, memlock, nofile, rss, stack, cpu, nproc, as, maxlogins, maxsyslogins, priority, locks, msgqueue, nie, rtprio. 第四列表示值.
系统级别的限制
man 5 proc: /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: echo 100000 > /proc/sys/fs/file-max The kernel constant NR_OPEN imposes an upper limit on the value that may be placed in file- max. If you increase /proc/sys/fs/file-max, be sure to increase /proc/sys/fs/inode-max to 3-4 times the new value of /proc/sys/fs/file-max, or you will run out of inodes. /proc/sys/fs/file-nr This (read-only) file gives the number of files presently opened. It contains three num- bers: the number of allocated file handles; the number of free file handles; and the maximum number of file handles. The kernel allocates file handles dynamically, but it doesn’t free them again. If the number of allocated files is close to the maximum, you should consider increasing the maximum. When the number of free file handles is large, you’ve encountered a peak in your usage of file handles and you probably don’t need to increase the maximum.
4. 如何修改最大句柄数
单一进程级别
临时生效: ulimit -n 2048 或者 echo ulimit -n 2048>>/etc/profile
永久生效:修改 /etc/security/limits.conf 配置文件
系统级别
修改 /proc/sys/fs/file-max 文件内容
方法一:直接修改sys文件, echo 100000 > /proc/sys/fs/file-max ;
方法二:直接修改 /etc/sysctl.conf 文件增加或者修改配置项 fs.file-max = 999999 ,输入 #sysctl -p 使之生效;
方法三: sysctl -w fs.nr_open=100000000
如果增加/proc/sys/fs/file-max,需要同步调整/proc/sys/fs/inode-max到新file-max值的3-4倍,否则会用尽inode资源。通常,file-max的值设置为内存(KB)大小的10%左右,即
grep MemTotal /proc/meminfo | awk '{printf("%d\n",$2/10)}'
5. 如何查询当前系统中文件句柄数使用情况
1)统计各进程打开句柄数:lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr
2)统计各用户打开句柄数:lsof -n|awk '{print $3}'|sort|uniq -c|sort -nr
3)统计各命令打开句柄数:lsof -n|awk '{print $1}'|sort|uniq -c|sort -nr