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):
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!
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
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.