[TAG] Counting total and maximum opened file descriptors or a program
Benjamin A. Okopnik
ben at linuxgazette.net
Fri Mar 11 21:00:09 MSK 2005
On Thu, Mar 10, 2005 at 02:37:48PM -0700, Mike Orr wrote:
> Bastian Kleineidam wrote:
> > I am wondering if there is a tool to count the overall and maximum
> > number of opened files from a program. Just like `time <program>`
> > reports processor resources, I'd like to do `count_files <program>`
> > to report file descriptor resources.
> > Ideally, count_files should execute the program, and after it finishes
> > report for example:
> > Total number of opened files: 774
> > Total number of closed files: 774
> > Maximum number of open files: 230
> > I want to use this to test CGI scripts for an environment where the
> > number of open files is restricted.
> I don't know about counting files, but you can limit the number of open
> file descriptors available to the process:
> ulimit -n 100
> This is a bash command so you'd have to run it before the program starts.
To be a little more precise, it's a Bourne shell ('sh') function that is
available in all the shells derived from 'sh' - e.g., Bash, KSH, and zsh
all support it.
> You could keep lowering the number till the program aborts or raises an
> error. "man bash" says most OSes don't allow it to be set, but my Gentoo
> at least reports the new number back, although I don't know if the kernel
> honors it. The default on my system is 1024.
Note that you can _decrease_ the number of available filehandles easily,
while increasing them usually requires root privileges (at least in Bash
and KSH; zsh doesn't seem to care.)
> If the program is something
> like a web server, insufficient file descriptors can also cause
> "Connection Refused" errors, so keep that in mind if your users start
> reporting an increase in those.
> Are these scripts you wrote or scripts you can't edit? Why not have the
> script itself count the number of files it opens?
I suspect that it's an administration issue - e.g., Bastian is running a
server and wants to know how many filehandles his people are keeping
open. The problem becomes more... interesting, in that case. :)
There's also 'lsof', which reports on processes and the files that they
have open; Bastian may well find that to be of use. In any case, 'lsof'
is one of those fine Geek Tools that's well worth knowing.
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://linuxgazette.net *
More information about the TAG