From owner-freebsd-hackers@freebsd.org Thu Jan 21 22:51:15 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 D78C5A8CC29 for ; Thu, 21 Jan 2016 22:51:15 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: from mail-wm0-x235.google.com (mail-wm0-x235.google.com [IPv6:2a00:1450:400c:c09::235]) (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 6AD101C4C for ; Thu, 21 Jan 2016 22:51:15 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: by mail-wm0-x235.google.com with SMTP id u188so245504783wmu.1 for ; Thu, 21 Jan 2016 14:51:15 -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=nIEDS3W87apFCt4Kse2RM30jgZ0MbFKEIQK2KvtKmug=; b=TgZlUWjy4xyCo/IvKlyF/UqLbCwlRUyAGio/qYMfslaSXI/fDSgn8y0FakHT4HZ5UC 9t+DjHkVnpePJNC++HIVhO6qdjuPj1p1GEPG00b1I4tKXH/3jHrAwjnYqjUq0UiU9KhJ uptuWSc3UN0B+hIMq1WVGUYT1dm/20SUNXqBbgda5cOwKR6KBd5FUoM3Oxi976waiS/6 TaGvEMef0+p+V2OvpFB4wQyjnYrFh/rZBI/q8tD5/Den6Cf/lXXNek56r25jZ0zkrO75 sxXIABwYz85bV7Pj3CzMIcB9um8iFLSd6t5Js4RvJEUH9GSMxl/hx1lES8/5ASAeU1rh IMAg== 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=nIEDS3W87apFCt4Kse2RM30jgZ0MbFKEIQK2KvtKmug=; b=NDwsBUEzFCl9nJOHiwXnL7yYoGpXy41T9E0G4OLKYZvztnG/JGlJwReS8/H9/9oLjR JPhM+FyQtjHdnIimPNh6hke2tBhFAsx5p9sOqxWLmQNivXyz58upPdX2jyEkni0k2L3k fn2BVR2QQkwsVddx8i3jRGDxA2UDzeETeV6GWtNfE18/ox9KaLF9fXAdlZogumumSKFf xACT/XIt/y0eK4nCU/jlO53SjnGIqdn9Qs0UFU4+Kzd/4C8JfK88t6sUGF40kKQb32aN toEaCpZsjc0tqOjRhQtIAlSocAPVFh9Pr8PcOgiH+oYPxpUUAfcVHcOZtyQjcJCz53mZ CSOg== X-Gm-Message-State: ALoCoQmxf/T0b0Z/u6QY3kHDJZRar0QmUmet3Opt83P2zBfPaPrU8I6phbVu9sWrX7eZXxuqBeNnjygtXE9VO0CbFVBxP9YcXQ== X-Received: by 10.194.184.210 with SMTP id ew18mr44395954wjc.79.1453416673721; Thu, 21 Jan 2016 14:51:13 -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 s2sm3312212wjs.43.2016.01.21.14.51.12 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 21 Jan 2016 14:51:13 -0800 (PST) Date: Thu, 21 Jan 2016 23:51:11 +0100 From: Mateusz Guzik To: Jilles Tjoelker Cc: "Batutis, Ed" , "freebsd-hackers@freebsd.org" , Diane Bruce , Joerg Sonnenberger Subject: Re: get number of open files in a process? Message-ID: <20160121225111.GB21869@dft-labs.eu> Mail-Followup-To: Mateusz Guzik , Jilles Tjoelker , "Batutis, Ed" , "freebsd-hackers@freebsd.org" , Diane Bruce , Joerg Sonnenberger References: <20160121153636.GA10961@britannica.bec.de> <20160121161637.GA83843@night.db.net> <20160121162527.GA11600@britannica.bec.de> <20160121191308.GA21869@dft-labs.eu> <31126442b7d347c5976a57aabc2e5792@hioexcmbx08-prd.hq.netapp.com> <20160121224511.GA74045@stack.nl> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20160121224511.GA74045@stack.nl> 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 22:51:15 -0000 On Thu, Jan 21, 2016 at 11:45:11PM +0100, Jilles Tjoelker wrote: > On Thu, Jan 21, 2016 at 07:44:01PM +0000, Batutis, Ed wrote: > > > However, the real question is what do you need this for. > > > I would like to know when I am getting close to hitting the file > > handle limit. This is useful for two reasons: 1) if I run out of > > available file handles this will most likely cause my process to > > crash. If I start logging when the count begins to get dangerously > > high, then I might after the fact be able to determine what was going > > on to cause that. 2) Some handles are used by a user interface. It > > would be nice if the server could reject connections when the open > > handle count hit a critical threshold so the user interface doesn't > > become part of the problem - obscuring the actual underlying cause. > > You can check the integer file descriptor that was just created, since > if the descriptor is N, at least N other descriptors must be active at > the same time. For example, if a new client's file descriptor is higher > than the limit minus some safety margin, reject the connection. > This cannot work. POSIX mandates the lowest free fd is returned, so the N you see here can be extremely low, while the process is running out of fds. The sysctl I mentioned in my mail can be trivially backported to other FreeBSD versions, if this kind of feature is really needed. I'm still not convinced the approach here (of checking fds) is the right one to take - you have to have a very big reserve to not suddenly run out of fds, one would just limit amount of clients and, well, handle errors as they can pop up for reasons other than running out of fd slots. -- Mateusz Guzik