From owner-freebsd-hackers@FreeBSD.ORG Thu Dec 18 08:48:15 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id E190150F for ; Thu, 18 Dec 2014 08:48:14 +0000 (UTC) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 83C2C1A75 for ; Thu, 18 Dec 2014 08:48:14 +0000 (UTC) Received: from tom.home (kostik@localhost [127.0.0.1]) by kib.kiev.ua (8.14.9/8.14.9) with ESMTP id sBI8m4h2034397 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 18 Dec 2014 10:48:04 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.9.2 kib.kiev.ua sBI8m4h2034397 Received: (from kostik@localhost) by tom.home (8.14.9/8.14.9/Submit) id sBI8m3eN034396; Thu, 18 Dec 2014 10:48:03 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Thu, 18 Dec 2014 10:48:03 +0200 From: Konstantin Belousov To: Robert Bonomi Subject: Re: getting 'load average' info from inside a kernel module Message-ID: <20141218084803.GP2148@kib.kiev.ua> References: <201412172006.sBHK6Q5E031660@host203.r-bonomi.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <201412172006.sBHK6Q5E031660@host203.r-bonomi.com> User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on tom.home Cc: freebsd-hackers@freebsd.org X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Dec 2014 08:48:15 -0000 On Wed, Dec 17, 2014 at 02:06:26PM -0600, Robert Bonomi wrote: > > originally sent to -questions, where it was suggested that -hackers was more > likely to produce some useful info. > > > Subject: getting 'load averages' (or something similar) from inside a _kernel_ > > > > I'm trying to get the current 'runnable processes' count from inside a > > kernel loadable module for BSD 8.4, so I can tweak it's behavior depending > > on the current activity level. All I've found so far is what's in 'man 9 > > runqueue', and it is *badly* out-of-sync with the 8.4 kernel code. > > e.g., the external arrays at the beginning of the synopisis > > are shown as being of type 'struct rq' -- but there *ISN"T* any defined > > struct 'rq'; it seems to be named 'runq', at least in /usr/include/sys/runq.h > > > > Then the cr*p gets deeper -- trying to make heads or tails out of how to > > get a count of runnable processes from the _arrays_ (of unknown size) > > described on the 'man 9 runqueue' page info has me defeated. > > Looking at '/usr/include/sys/runq.h', it appears that 'struct runq' is > > a single item with an array of queues (by 'nice' level), and a bitmap of > > which array elements have non-zero length queues. > > > > I'm perfectly willing to brute-force the data out of the queue lists, *IF* > > there's some reasonable, _current_, descriptive info of the format/usage > > of those kernel structures. > > if 'man 9 runqueue' is 'mostly' correct -- reality just being 'runq' instead > of 'rq' -- then 'how to find out' how mamy elements are in those 'unknown size' > arrays is the missing element. *OR* if those four items are _not_ arrays, > but simple structs -- given the array of queueheads in the 'runq' struct, > I can probably decipher the rest. > > > > Alternatively, is there a way to directly access the 'sysctl' MIB data in > > kernel memory. sysctl(3) is out -- it's in 'libc', and not available to > > kernel modules. Pointers to descriptions of the kernel in-memory data for > > that MIB would be a big help. Or 'where to ask', if there's a better place. > > I'm willing to 'count processes' in the run queues each time I query -- an > 'instantenous' value is fine for my purposes, I can derive whatever 'avrage > over time' I need. :)) You are writing a kernel module, so why did not you looked at the kernel sources ? Look at the implementation of the sysctl for which you want the kernel analog, and do the same. AFAIU, you want vm.loadavg, which is floating-point recoded representation of the fixed-point values from averunnable array. Look at sys/kern/kern_synch.c and sys/vm/vm_meter.c (honestly, I only checked HEAD).