blog » Work / 努力工作 » 你的系统打开了多少文件?
你的系统打开了多少文件?
研究这个问题的起因是我有两台机器,“打开文件数目”的统计出现了错误。到现在也没能找到问题的原因,借这个问题,正好把这方面的概念搞清楚了。同时发现,中文搜到的内容里面,讲清楚这个的很少。
在 Linux 系统中,有一个参数控制了整个系统可打开的文件数量。可以通过 /proc 来查看、修改这个数量:
cat /proc/sys/fs/file-max
829200
如果你想修改的话,用 echo 就可以了:
echo "104854" > /proc/sys/fs/file-max
接着,还有一个地方能让我们看到系统打开了多少个文件:
[root@srv-4 proc]# cat /proc/sys/fs/file-nr
3391 969 52427
| | |
| | |
| | 最大打开文件个数
| 空闲的文件句柄数
已经打开的文件句柄数
(本次开机所使用的句柄数量)
说到这里,一定有很多人想起了 lsof ,在 Linux 一切皆文件,lsof 也可以显示文件打开数量的。但问题在于 lsof 显示出来的数量与 file-nr 是不同的。
什么样的文件才是被打开的文件?
一个被打开的文件就是在使用的文件么?打开一个文件,他就会被描述为“打开文件”的状态么?一个文件的状态是由程序抓取文件句柄时的数据结构控制的,典型的 stdout 一类的东西。lsof 只是罗列的所有打开的文件,并不是通过程序获取文件句柄的文件,这里面其实是个包容关系。比如说:所在的工作目录,内存中映射的库文件等等 lsof 都会显示出来,但是 file-nr 并不会显示。
再来做一个实验,打开一个文件,看看 lsof 和 /proc/pid 下的显示有什么不同:
usr-4 2381 0.0 0.5 5168 2748 pts/14 S 14:42 0:01 vim openfiles.html
[root@srv-4 usr-4]# lsof | grep 2381
vim 2381 usr-4 cwd DIR 3,8 4096 2621517 /n
vim 2381 usr-4 rtd DIR 3,5 4096 2 /
vim 2381 usr-4 txt REG 3,2 2160520 34239 /usr/bin/vim
vim 2381 usr-4 mem REG 3,5 85420 144496 /lib/ld-2.2.5.so
vim 2381 usr-4 mem REG 3,2 371 20974 /usr/lib/locale/LC_IDENTIFICATION
vim 2381 usr-4 mem REG 3,2 20666 192622 /usr/lib/gconv/gconv-modules.cache
vim 2381 usr-4 mem REG 3,2 29 20975 /usr/lib/locale/LC_MEASUREMENT
vim 2381 usr-4 mem REG 3,2 65 20979 /usr/lib/locale/LC_TELEPHONE
vim 2381 usr-4 mem REG 3,2 161 19742 /usr/lib/locale/LC_ADDRESS
vim 2381 usr-4 mem REG 3,2 83 20977 /usr/lib/locale/LC_NAME
vim 2381 usr-4 mem REG 3,2 40 20978 /usr/lib/locale/LC_PAPER
vim 2381 usr-4 mem REG 3,2 58 51851 /usr/lib/locale/LC_MESSAGES/SYSL
vim 2381 usr-4 mem REG 3,2 292 20976 /usr/lib/locale/LC_MONETARY
vim 2381 usr-4 mem REG 3,2 22592 99819 /usr/lib/locale/LC_COLLATE
vim 2381 usr-4 mem REG 3,2 2457 20980 /usr/lib/locale/LC_TIME
vim 2381 usr-4 mem REG 3,2 60 35062 /usr/lib/locale/LC_NUMERIC
vim 2381 usr-4 mem REG 3,2 290511 64237 /usr/lib/libncurses.so.5.2
vim 2381 usr-4 mem REG 3,2 24565 64273 /usr/lib/libgpm.so.1.18.0
vim 2381 usr-4 mem REG 3,5 11728 144511 /lib/libdl-2.2.5.so
vim 2381 usr-4 mem REG 3,5 22645 144299 /lib/libcrypt-2.2.5.so
vim 2381 usr-4 mem REG 3,5 10982 144339 /lib/libutil-2.2.5.so
vim 2381 usr-4 mem REG 3,5 105945 144516 /lib/libpthread-0.9.so
vim 2381 usr-4 mem REG 3,5 169581 144512 /lib/libm-2.2.5.so
vim 2381 usr-4 mem REG 3,5 1344152 144297 /lib/libc-2.2.5.so
vim 2381 usr-4 mem REG 3,2 173680 112269 /usr/lib/locale/LC_CTYPE
vim 2381 usr-4 mem REG 3,5 42897 144321 /lib/libnss_files-2.2.5.so
vim 2381 usr-4 0u CHR 136,14 16 /dev/pts/14
vim 2381 usr-4 1u CHR 136,14 16 /dev/pts/14
vim 2381 usr-4 2u CHR 136,14 16 /dev/pts/14
vim 2381 usr-4 4u REG 3,8 12288 2621444 /n/.openfiles.html.swp
[root@srv-4 usr-4]# lsof | grep 2381 | wc -l
30
[root@srv-4 fd]# ls -l /proc/2381/fd/
total 0
lrwx------ 1 usr-4 usr-4 64 Jul 30 15:16 0 -> /dev/pts/14
lrwx------ 1 usr-4 usr-4 64 Jul 30 15:16 1 -> /dev/pts/14
lrwx------ 1 usr-4 usr-4 64 Jul 30 15:16 2 -> /dev/pts/14
lrwx------ 1 usr-4 usr-4 64 Jul 30 15:16 4 -> /n/.openfiles.html.swp
lsof 显示我们打开了30个文件,但是对于系统来说被标识为“打开文件(open files)”的只有4个。
到这里基本明了了。
写本文的缘由是因为我有一台机器 file-nr 的状态打开文件一直为 0。这显然是一个不正常的状态,但还没有找到原因。Bug?或许吧。
发表评论