Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 07-15-2011, 05:20 AM   #1
LQ Newbie
Registered: Jul 2011
Posts: 4

Rep: Reputation: Disabled
CPU Usage of a process

Hi guys,

I am currently writing a JAVA script to monitor certain unix processes through JConsole.

Upon having lots of trouble with runtime.exec, i decided to bypass the top/ps command call and just get the information straight from /proc/*pid*/whatever.

Now i can pull back any information from any of the files I want, and the current way i determine the CPU usage of a process is as follows:

Add the UTime and STime of that process from /proc/pid/stat then divide my pidCpu by UTime + STime + NTime from /proc/stat, then multiply that by 100, should give me the % cpu usage a process is using, right?

Theory being if I get the jiffies assigned to my process, I can divide that by the total jiffies the cpu assigns.

However, my results seem to vary from the ones gathered from top and ps. What am I doing wrong?

Any input is appreciated.
Old 07-15-2011, 06:05 AM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,244

Rep: Reputation: 1286Reputation: 1286Reputation: 1286Reputation: 1286Reputation: 1286Reputation: 1286Reputation: 1286Reputation: 1286Reputation: 1286
Every time you read a "file" from /proc, its value(s) are recalculated - at that point in time. So nothing ever correlates.
Same applies to "top" and "ps" BTW.

That means some (FSVO "some") variance is expected.
Old 07-15-2011, 07:48 AM   #3
LQ Newbie
Registered: Jul 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
So the method of adding the Utime and Stime of a process, diving that by the Utime + Stime + Ntime of the /proc/stat file at that time and multiplying that by 100 should give you the %cpu that that process is using?


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
process cpu usage ahmedb72 Linux - General 9 09-07-2010 02:22 PM
How to get %CPU usage of a process devershetty Linux - Newbie 1 08-18-2008 01:52 AM
How to get %CPU usage of a process devershetty Linux - Newbie 13 08-16-2008 11:51 AM
process cpu usage hamedrafi Programming 5 06-11-2007 06:32 PM
How to get %CPU usage of a process romydmisc Linux - Newbie 2 05-24-2006 01:02 PM

All times are GMT -5. The time now is 01:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration