macos 在 Mac OS X 中,如何获得文件描述符使用的准确计数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/795236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Mac OS X, how can I get an accurate count of file descriptor usage?
提问by jfklein
On Linux, ulimit -n
can be used to change or view the limit on the number of file descriptors for a process, and lsof -p nnn | wc -l
seems to consistently report the actual file descriptor usage.
在 Linux 上,ulimit -n
可用于更改或查看进程的文件描述符数量限制,并且lsof -p nnn | wc -l
似乎一致地报告实际文件描述符使用情况。
But on Mac OS X, lsof -p nnn | wc -l
can return a number higher than the limit. I suppose this means lsof
is returning more than just file descriptors, but I can't tell what's what.
但在 Mac OS X 上,lsof -p nnn | wc -l
可以返回高于限制的数字。我想这意味着lsof
返回的不仅仅是文件描述符,但我不知道是什么。
Bottom line: How can I get an accurate count of file descriptor usage in Mac OS X?
底线:如何在 Mac OS X 中准确计算文件描述符的使用情况?
采纳答案by smorgan
lsof can show a lot of things beyond just file descriptors, but most of what is likely inflating your count is the loaded frameworks and libraries for an application. You can look at the "FD" column to see if a line is a file descriptor--in which case it's a number, possibly followed by a letter indicating the mode--or something else (see the description of the FD column in the lsof man page for the full list).
lsof 不仅可以显示文件描述符,还可以显示很多东西,但大部分可能会增加您的计数的是应用程序加载的框架和库。您可以查看“FD”列以查看某行是否是文件描述符——在这种情况下,它是一个数字,可能后跟一个表示模式的字母——或其他内容(请参阅lsof 手册页获取完整列表)。
If you just need a rough approximation adding a 'grep -v " txt "' before your wc will get you a lot closer to an accurate value. If you need an exact value, you probably need to put together a regex to feed the output through that filers precisely by the FD column.
如果您只需要粗略的近似值,那么在 wc 之前添加一个 'grep -v " txt "' 将使您更接近准确的值。如果您需要一个确切的值,您可能需要将一个正则表达式放在一起,以通过 FD 列精确地通过该过滤器提供输出。
回答by PhillipHolmes
I came across the need for identifying this recently - the command I used to count up the total entries (so more than just file handles, but its relative so therefore relevant imo) is:
我最近遇到了识别这一点的需要 - 我用来计算总条目的命令(不仅仅是文件句柄,而是它的相对因此相关的imo)是:
lsof | awk '{print }' | uniq -c | sort -rn | head
This gives something like the following output (your highest used applications may be different!):
这给出了类似于以下输出的内容(您使用最多的应用程序可能会有所不同!):
$lsof | awk '{print }' | uniq -c | sort -rn | head
3271 com.apple
2978 Google
914 Atom\x20H
505 Skype
476 Microsoft
375 Screenher
304 Finder
292 Dock
277 Atom\x20H
270 Atom\x20H
I usually only need to see the top 10 entries, but you can manipulate head
to show as many lines as you like.
我通常只需要查看前 10 个条目,但您可以操作head
以显示任意数量的行。
回答by laike9m
I modified anders' answer, now it only displays the opened fd numbers of a specific process:
我修改了 anders 的答案,现在它只显示特定进程的打开的 fd 号:
FCOUNT=`lsof -p | grep -v " txt " | wc -l`;echo "PID: $FCOUNT" | sort -nk3
Example:
例子:
$ ./fd-count.sh 5926
PID: 5926 97
回答by anders.norgaard
I was looking for which process that had lots of file descriptors - so I guess something like
我正在寻找哪个进程有很多文件描述符 - 所以我想像
for pid in `ps aux | tail -n +2 | awk '{print }'`; do FCOUNT=`lsof -p $pid | grep -v " txt " | wc -l`; echo "PID: $pid $FCOUNT"; done | sort -nk3