LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 07-15-2011, 06:20 AM   #1
matt9949
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, 07:05 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,445

Rep: Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068
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, 08:48 AM   #3
matt9949
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?
 
  


Reply


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 03:22 PM
How to get %CPU usage of a process devershetty Linux - Newbie 1 08-18-2008 02:52 AM
How to get %CPU usage of a process devershetty Linux - Newbie 13 08-16-2008 12:51 PM
process cpu usage hamedrafi Programming 5 06-11-2007 07:32 PM
How to get %CPU usage of a process romydmisc Linux - Newbie 2 05-24-2006 02:02 PM


All times are GMT -5. The time now is 08:58 PM.

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