cancel
Showing results for 
Search instead for 
Did you mean: 

CPU idle % in /proc

Claudio Cilloni
Honored Contributor

CPU idle % in /proc

I need to read the CPU % idle state (like the value shown by top) without running any command.
Do you know if it written somewhere /proc? How does commands like top or sar get this value?

tx for your help.
Claudio
6 REPLIES
Balaji N
Honored Contributor

Re: CPU idle % in /proc

hmm..
unfortunately dont have a Linux box around at this momemnt.

in /proc directory if i remember correctly there should be the following files which should give u the info.

1. cpuinfo.
2. loadavg
3. meminfo.


see if they provide the relevant info.

else there is an option to the top command to write the output to a file and u can parse the output from there.
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Claudio Cilloni
Honored Contributor

Re: CPU idle % in /proc

thanks Balaji, but I cannot run an external program as you suggested, and these files don't contain the idle value. I'm using now /proc/loadavg, but sometimes it gives some peaks, passing from 0 to 1 instantly. It could cause my program make bad choices; so I prefer make my own calculation about cpu load.

Ciao
Claudio

Steven E. Protter
Exalted Contributor

Re: CPU idle % in /proc

any user should be able to at least run the top command and identify the offending process.

If you are in X Gnome there is a performance monitor under Programs Utilities.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Stuart Browne
Honored Contributor

Re: CPU idle % in /proc

From what I understand of the 'proc' file system, there is no bare-word processor utilisation % values stored.

You'll need to write your own application to calculate it for you. You could probably be cheeky and just rip the code from the 'top' source.

My best understanding is that it grabs details out of /proc//stat or /proc//status, neither of which are very nice to look at.

.. it uses some very funky stuff, and does uses a proc_t struct as this:
/*
* Calculate percent cpu time for this task.
*/
this->pcpu = (total_time * 10 * 100/Hertz) / elapsed_time;
if (this->pcpu > 999)
this->pcpu = 999;


So it's calculating out the %usage a given process has given how much time has passed in a given period, and how many CPU cycles it's used based on how many it can do..

Combining that with 'man proc' might get you somewhere..
One long-haired git at your service...
Claudio Cilloni
Honored Contributor

Re: CPU idle % in /proc

thanks Stuart, good suggestion. So, top can calculate idle % because it scans the statistics of all processes to gather a lot of other informations it need. Now I don't know if it worth doing the same thing only to get the idle value...

mmmmh... thinking...

Ciao
Claudio
Claudio Cilloni
Honored Contributor

Re: CPU idle % in /proc

I got it! Stuart, you put me on the right track.
/proc/stat is my new friend!
man page helped. I didn't know the existence of the man page for /proc.

Thanks to all!
Claudio