From owner-freebsd-hackers@freebsd.org Thu Jan 21 19:13:13 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 35E8CA8BD9A for ; Thu, 21 Jan 2016 19:13:13 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: from mail-wm0-x233.google.com (mail-wm0-x233.google.com [IPv6:2a00:1450:400c:c09::233]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B7FDD1B58 for ; Thu, 21 Jan 2016 19:13:12 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: by mail-wm0-x233.google.com with SMTP id r129so185116627wmr.0 for ; Thu, 21 Jan 2016 11:13:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=niMcAM/h5apNnrDeqouhe4HANJwxmIGzdK3ZugQUm8I=; b=gT7OcNDCTphDyVjP1HK2xhcLolbSXkAYJQq5q2quy4VLcfkySFyMptvDUgc3RWw/QX rSDx/h9w9uMCdsU79lfY6Yn7Tkq5Qfv31ond/rshZJdzaMRfmww+GgOKWmfZqb2N/DSk qytKXxXgEOcUNTFH6v0zWd3e1JFFS/ZhxDpGs02CVqafQ052yJ2aJZQwg9wRXzx4iQI2 jzmm+QA3TrJYcZCQMCIPRgnrJQ3zGtiG+tUxnvhabb+CoWYQ8i8gb5Tz9gP7P2jA6xlz MAo+Dv54Li8Q1ZLUhWLHkeGtfim1XJIHTZCOLDLeIyJdjJypT3LzsqTeb1A+34Hu4Rdr U9fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-type :content-disposition:in-reply-to:user-agent; bh=niMcAM/h5apNnrDeqouhe4HANJwxmIGzdK3ZugQUm8I=; b=PD3IRZBZR5/+OY4MTjcWWnFsO4klpwSBdBDi2eNcDeD9etu9Nnb9RgeH4fxUdPS6ge BKBf8FwzCJ7EHM/0IWc+pwunpxepQE13Bxe04NAn+/baI3DsIhi9rxehYGz35JVQH/KH OB3+WD/CpPAK1ZvgD/kITP3XijiNrqX29Uib3Rpfr3qdGaaX2YGmrdmSKE2NyEAVh0uW eJmGpR6zLVUVb3xAVgRQk+PpY76n+fMCuPXDQGqqV9v7o5bYZ5Vtb8Jy9dySY6hZGo6b HmXFkGqCUUpbPwFqsjZOst3Ar5Pr6SN+SajcRkiDB4nFZaEzkHVSZwoyyhIC9SdWuS9o bJEw== X-Gm-Message-State: ALoCoQn/TaVAzJpmdsxeRnLP7D7dqk2+vfOWelQQtEjGLRP2yXyQ9pJegf98iYVKoVxOzZZoQH2s91vHzb0cuwgS3ZpGKfJCbA== X-Received: by 10.194.117.68 with SMTP id kc4mr43570438wjb.111.1453403591293; Thu, 21 Jan 2016 11:13:11 -0800 (PST) Received: from dft-labs.eu (n1x0n-1-pt.tunnel.tserv5.lon1.ipv6.he.net. [2001:470:1f08:1f7::2]) by smtp.gmail.com with ESMTPSA id k130sm4230779wmg.6.2016.01.21.11.13.10 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 21 Jan 2016 11:13:10 -0800 (PST) Date: Thu, 21 Jan 2016 20:13:08 +0100 From: Mateusz Guzik To: "Batutis, Ed" Cc: Joerg Sonnenberger , Diane Bruce , "freebsd-hackers@freebsd.org" Subject: Re: get number of open files in a process? Message-ID: <20160121191308.GA21869@dft-labs.eu> Mail-Followup-To: Mateusz Guzik , "Batutis, Ed" , Joerg Sonnenberger , Diane Bruce , "freebsd-hackers@freebsd.org" References: <20160121153636.GA10961@britannica.bec.de> <20160121161637.GA83843@night.db.net> <20160121162527.GA11600@britannica.bec.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Jan 2016 19:13:13 -0000 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