LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Calculating total %cpu usage from /proc/stat (https://www.linuxquestions.org/questions/programming-9/calculating-total-cpu-usage-from-proc-stat-201794/)

gaijin 07-06-2004 01:03 PM

Calculating total %cpu usage from /proc/stat
 
Hello,

I'm working with a custom distribution of uClinux and I'm tryin to create a performance monitor to show cpu usage over time. The "ps" command with this distribution doesn't display %CPU for each process, so I can't just add up those values. I've been attempting to calculate the value from /proc/stat, but I'm not sure that I'm using the right calculation.

At this point, I know that my /proc/stat file has the following standard information (single cpu):
Code:

cpu <user> <nice> <system> <idle>
Those values are in 'jiffies'.

From what I've found, I should take the values from /proc/stat at two different times and calculate the cpu usage from the difference. However, the way to do it isn't clear.

If someone could post the exact calculation required (preferably with units, to show how the calculation results in %cpu), it would be much appreciated!

Thanks!

edit: found this formula, but it's for a single process, not the overall cpu usage...

1. What is a jiffie ?
Jiffie is a unit used in Unix world to measure CPU activity . it is equal to 1/100 of a second. or that is
10 ms.
2. How to calculate the percentages ?

Lets say you want to calcualate the percentage CPU usage for a process for 1s.

a. Save the utime and stime values for that process.
b. After one second again read the values for utime and stime.
c. Calculate the difference between two values. This will give you the number of jiffies used by the
process in user mode and system mode. Now divide these two numbers with 100 to get the
percentage CPU used by the process for past one second.

Now if you want to calculate the percentage for any two times . Just read the above mentioned two values for thoes times . Take their difference and divide this by total no. of jiffies between two times. to get the percentage. You might want to add the value of user mode time and system mode time to get one value for the process or you might want to leave them as two values to see which processes are making more system calls.

Now for the memory just read the Resident Set Size and divide it with total amount of RAM ,( available from /proc/meminfo ) to get the percentage ram used.

neil 07-07-2004 02:45 AM

have you looked at vmstat -w. The cpu idle in % is displayed at the end.


All times are GMT -5. The time now is 10:28 AM.