Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-06-2004, 01:03 PM   #1
LQ Newbie
Registered: Jul 2004
Posts: 1

Rep: Reputation: 0
Calculating total %cpu usage from /proc/stat


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
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.

Last edited by gaijin; 07-06-2004 at 01:25 PM.
Old 07-07-2004, 02:45 AM   #2
Registered: Jul 2001
Location: Kent, UK
Distribution: /Fedora/Debian/Ubuntu/Xubuntu
Posts: 108

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
/proc/stat file problem for cpu usage arunka Programming 3 04-23-2014 01:08 PM
New cpu values in /proc/stat? eldonz Linux - General 1 10-19-2005 12:16 PM
/proc/stat cpu info GDarkBladE Linux - Software 2 10-11-2005 03:16 PM
/proc/stat cpu ahz Programming 1 04-24-2005 07:24 AM
finding cpu usage with /proc breadcrust Programming 16 06-18-2004 05:53 PM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration