LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-15-2012, 10:00 AM   #1
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
ps and top Commands are giving Different %CPU Utilization for the Same Job


Hi Guys,


I am getting two different %CPU values for a process / PID when I issue top and ps commands:

Command: top -p 11859
Output:

Code:
  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
11859 xyzprod   25   0  246M 246M 24084 S     0.0  4.9   0:02 fiaccad
Command: ps -up 11859
Output:

Code:
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
xyzprod  11859  104  4.9 561088 252780 ?     S    09:05 109:02 /app/fiaccad
As you can see above the "top" displays 0.0 in the %CPU column whereas "ps" reports "104" in its %CPU column for the same process / command running.

Our monitoring tool (may be, Netcool) is generating alerts whose alarm value (as seen in the ps' output above) matches with the output of the "ps" command but not with that of "top" command.

So, which one is correct? Which output shall I consider / disregard? Besdies why the difference is there?

Please Note: I checked the same job on some other boxes where it is running and found the same behaviour with respect to "top" and "ps" commands' output.

Last edited by devUnix; 05-15-2012 at 11:42 AM.
 
Old 05-15-2012, 02:51 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
that is interesting, I can't give you the 100% answer as I don't remember the info of the top of my head and a google search didn't help. I remember dealing with something similar but different in regards to utilization being read incorrectly. After dealing with Red Hat support it came down to the way the ps and top read the /proc/pid/ files to determine the utilization, they determine it in a different way.

I hope it gets you in the right direction, sorry I couldn't be of more help but I'll keep doing some googling to see if I can track it down.
 
Old 05-15-2012, 02:54 PM   #3
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Found it! Man page FTW.

Excerpt from ps man page
Code:
CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is unlikely to add up to exactly 100%.
Top is the more accurate reading.
 
Old 05-15-2012, 07:37 PM   #4
grim76
Member
 
Registered: Jun 2007
Distribution: Debian, SLES, Ubuntu
Posts: 308

Rep: Reputation: 50
Top is not necessarily more accurate. Depends on what you are looking for regarding process information. PS is a good way to tell what a process has been doing over its life. Top is the point in time good for looking short term, or at short running processes.

PS is good for looking at longer running processes and getting an idea of how the process is impacting the server long term.

Just depends on the view of the processes you need to have.
 
Old 05-16-2012, 04:31 AM   #5
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
I must clear my point here. Netcool, for example, sends me an alert saying that "Job xyz PID 1234 is utilizing 85% of CPU" and I look up the PID 1234 by running "top" and do not see it on the top of the top's output. (I hit C to sort on CPU Utilisation.) I run "top -p 1234" and see "%CPU 0.0". ps tells me "90%".

I observe the trends shown by top and see CPU Utilisation upto 80% and 95% (fluctuating around those figures).

Now put yourself in my situation. Would you go ahead and close the ticket by logging the output from top or would you go ahead and ask the job's owner to kill the job or otherwise look into it?

As pointed out by Kustom42 with respect to ps: "CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process." In that case, a particular job should not be the culprit which is escalating the CPU utilisation because ps is not telling us that the job in question is eating up 85% of CPU's brain at this moment. Though it has done so during the las couple of minutes or hours as the case may be. Whereas top reports that the job is innocent. As pointed out by grim76: "Top is not necessarily more accurate."
 
Old 05-16-2012, 04:49 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
it was already discussed here:
http://www.linuxquestions.org/questi...and-ps-940393/
 
Old 05-16-2012, 08:59 AM   #7
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by pan64 View Post
Well, that post does not lead us to any conclusion. I have seen your contribution to it, though.

We want to see the Current Status of CPU Utilisation By a Given Process. We need an exact answer to that problem. I have run the top command in batch mode:

Code:
top -b -n 1
and the output is "%CPU 0.0" for that particular PID. In batch mode, the top command should not be showing the commulative differences.
 
Old 05-16-2012, 09:18 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
top -b -n 1 will measure the current load and it may depend even on the fact: which task is currently on a CPU (the top itself). In this case a sleeping process may have 0.0 load.
ps will give you something like an average, it measured from the start of the process. The higher average means the real higher load. Also, on a system with 8 CPUs (for example) the 104% load is not a blocking issue.

So if I were you I should run top several times, and check the max, min and average values for the given process.
 
Old 05-16-2012, 09:44 AM   #9
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by pan64 View Post
ps will give you something like an average, it measured from the start of the process. The higher average means the real higher load. Also, on a system with 8 CPUs (for example) the 104% load is not a blocking issue.

So if I were you I should run top several times, and check the max, min and average values for the given process.
Agreed!

I just noticed one more interesting thing that when I run the top command as:

Code:
top -p 1234
(where 1234 is the PID of the process) top shows "5.4" (for example) for the same process / PID whereas running top without any options, top is showing "24.5". Interestingly this time I do not see any output by running ps as:

Code:
ps -up 1234
(only the header is output not the values)

whereas the process is still running.
 
  


Reply



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
Confusing CPU utilization from top output sumedh.inamdar Linux - Newbie 2 01-12-2011 06:06 PM
top utility: initial display shows incorrect CPU utilization data texas_danrob Linux - Software 3 09-01-2009 08:34 AM
Total CPU utilization returned by "top" command sanpnair Linux - Newbie 1 05-15-2009 12:19 PM
top, ps, sar which gives accurate data for CPU utilization makam.sreekanth Linux - Newbie 6 03-20-2009 09:39 AM
high cpu utilization 'top' analysis saarb16 Linux - General 2 03-09-2006 09:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:18 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration