Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Mac OS X, how can I get an accurate count of file descriptor usage?

Tags:

unix

macos

lsof

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.

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.

Bottom line: How can I get an accurate count of file descriptor usage in Mac OS X?

like image 730
jfklein Avatar asked Apr 27 '09 20:04

jfklein


People also ask

How do I check open descriptors on Mac?

At least on OSX 10.10 (Yosemite, didn't check on Mavericks), you can get the list of open files by process via the default activity monitor application. Just double click on the relevant process on the list and select "Open Files and Ports" tab on the popup.

How many file descriptors can Mac open?

PROTIP: On MacOS, the maximum number that can be specified is 12288.

How do I check file descriptor?

In the /proc pseudo filesystem, we can find the open file descriptors under /proc/<pid>/fd/ where <pid> is the PID of a given process. Thus, we have to determine the process identification number (PID) of a process to look at its open file descriptors.


2 Answers

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:

lsof | awk '{print $1}' | uniq -c | sort -rn | head 

This gives something like the following output (your highest used applications may be different!):

$lsof | awk '{print $1}' | 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.

like image 192
keba Avatar answered Oct 07 '22 10:10

keba


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).

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.

like image 44
smorgan Avatar answered Oct 07 '22 10:10

smorgan