[geeks] getting CPU status in FreeBSD

Charles Shannon Hendrix shannon at widomaker.com
Thu Sep 13 13:39:44 CDT 2007


On Thu, 13 Sep 2007 14:12:25 -0400
Sridhar Ayengar <ploopster at gmail.com> wrote:

> Charles Shannon Hendrix wrote:
> > Does anyone know of a decent routine to get the CPU stats under FreeBSD?
> > 
> > I basically just want a function that returns 0-100 for each CPU in the
> > system so I can print the results to an LCD.
> > 
> > I'm trying to dig this information out of top sources, but it's a real
> > hairball, so I'm looking for something easier if I can find it.
> 
> I don't suppose you could write a little awk-ism to read it right out of 
> top's console output?

No, this is an embedded LCD in an appliance system.

I should have explained: this is a semi-realtime process that manages an
event loop for an LCD, to process keypad input and show system and software
statistics.

It currently runs a 150ms cycle and I don't want anything heavy, so C
routines I can embed in my code are preferred.

For now, I'm writing my own routine based on getsysctl to get the
kern.cp_time array and use those numbers to calculate CPU percentages.

I've never used sysctl calls in a C program, and I don't know much about that
system.  In fact, I didn't even know you could use sysctl outside of the
shell until two days ago.  I had no idea that's how top got sysinfo on
FreeBSD, for example.


-- 
shannon /     Trouble rather the tiger in his lair than the sage amongst 
-------'      his books For to you kingdoms and their armies are
              mighty and enduring,  but to him they are but toys of
              the moment to be overturned by the flicking of a finger. 



More information about the geeks mailing list