get number of open files in a process?

Mateusz Guzik mjguzik at gmail.com
Thu Jan 21 19:13:13 UTC 2016


On Thu, Jan 21, 2016 at 06:39:44PM +0000, Batutis, Ed wrote:
> > > > Use getdtablesize() to get the current largest known fd. If you
> > > > want, you can afterwards loop and use fcntl with F_GETFD to check if
> > > > the descriptor is still open.
> 
> Since I need an open handle count I would have to do 2048 fcntl() calls in my case. I'm afraid this doesn't look like a good idea, since it might happen a lot under certain circumstances.
> 
> Looking at the source for kinfo_getfile, it seems like using this is not a good idea because, according to a comment in bug 197695:
> 
> https://www.mail-archive.com/freebsd-bugs@freebsd.org/msg21569.html
> 
> the function uses a sysctl with KERN_PROC_FILEDESC which "might be slow". This function also does two memory allocations for data I don't need to look at.
> 
> So, I guess the answer is, for me anyway, that there is no sufficiently efficient way to get the number of open file handles from the system for my user-space application.
> 
> But, if anyone has any more ideas, I'd be happy to hear them!
> 

Well, OpenBSD has getdtablecount syscall giving a cheap way to obtain
this information. FreeBSD gained a sysctl providing an equivalent
functionality for compatiblity reasons, but the feature is only in head.

https://svnweb.freebsd.org/base/head/sys/kern/kern_descrip.c?r1=287540&r2=290473

It is trivial to backport to 10.x, but I have no plans doing so (I'm not
going to prevent anyone from doing it either).

However, the real question is what do you need this for.

-- 
Mateusz Guzik <mjguzik gmail.com>


More information about the freebsd-hackers mailing list