Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 21 Jan 2016 20:13:08 +0100
From:      Mateusz Guzik <mjguzik@gmail.com>
To:        "Batutis, Ed" <Ed.Batutis@netapp.com>
Cc:        Joerg Sonnenberger <joerg@britannica.bec.de>, Diane Bruce <db@db.net>, "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>
Subject:   Re: get number of open files in a process?
Message-ID:  <20160121191308.GA21869@dft-labs.eu>
In-Reply-To: <d26cc2fcea304ea6a89f61c2ec497322@hioexcmbx08-prd.hq.netapp.com>
References:  <ea9f2ab2d4f543c8ad34fdeb082f8c7a@hioexcmbx08-prd.hq.netapp.com> <20160121153636.GA10961@britannica.bec.de> <20160121161637.GA83843@night.db.net> <20160121162527.GA11600@britannica.bec.de> <d26cc2fcea304ea6a89f61c2ec497322@hioexcmbx08-prd.hq.netapp.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20160121191308.GA21869>