I can't figure out why lsof on my Mac (10.8.2, MacBook Pro) is so slow.
On my Mac, lsof
takes more than a minute:
$ touch /tmp/testfile
$ time lsof /tmp/testfile
real 1m16.483s
user 0m0.029s
sys 1m15.969s
On a typical Linux box, running Ubuntu 12.04, lsof
takes 20 ms:
$ touch /tmp/testfile
$ time lsof /tmp/testfile
real 0m0.023s
user 0m0.008s
sys 0m0.012s
The problem persists if I run lsof -n
(to avoid DNS lookups). Further, I tried checking which system calls are made by lsof
using dtruss
, and found that it's calling proc_info
tens of thousands of times:
$ sudo dtruss lsof /tmp/testfile 2> /tmp/dump
$ cat /tmp/dump | sort | uniq -c | sort -nr | head
10000 proc_info(0x2, 0x1199, 0x8) = 1272 0
6876 proc_info(0x2, 0x45, 0x8) = 1272 0
2360 proc_info(0x2, 0x190D, 0x8) = 1272 0
1294 proc_info(0x2, 0xFF, 0x8) = 1272 0
1152 proc_info(0x2, 0x474, 0x8) = 1272 0
1079 proc_info(0x2, 0x2F, 0x8) = 1272 0
709 proc_info(0x2, 0xFE, 0x8) = 1272 0
693 proc_info(0x2, 0x1F, 0x8) = 1272 0
623 proc_info(0x2, 0x11A, 0x8) = 1272 0
528 proc_info(0x2, 0xF7, 0x8) = 1272 0
Any ideas? I've run these tests and obtained the same results using both the version of lsof
included with OS X (4.85) as well as the latest version from ftp://sunsite.ualberta.ca/pub/Mirror/lsof/ (4.87).
(For the curious, the reason I'm frustrated by this performance is that when I drag images to Evernote, it runs lsof
in the process of copying the file, causing my system to hang for a full minute every time I try to insert an image in Evernote.)
lsof
with no arguments (to list all files), it hangs for a minute and then prints all the files. But, as I mentioned, it still hangs if I try to list who has a single file open in the /tmp directory, so it's not a particular open file that's the problem. Also, I'm not running any AirServer process.sudo opensnoop -n lsof
.sudo opensnoop -n lsof
andlsof /tmp/testfile
in two tabs, and opensnoop only reported that three files had been opened. So the problem must not be an excessive number of file opens, but something related to excessiveproc_info
calls.