LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   CPU Usage of a process (https://www.linuxquestions.org/questions/linux-general-1/cpu-usage-of-a-process-891853/)

matt9949 07-15-2011 05:20 AM

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.

syg00 07-15-2011 06:05 AM

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.

matt9949 07-15-2011 07:48 AM

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?


All times are GMT -5. The time now is 10:23 PM.