4

I've seen a few posts similar to this, most notably here, but wasn't quite satisfied with the answers. I'm comparing top and ps results on a specific process and see huge discrepancies in CPU usage. top varies between <1% and 100% from interval to interval including periods of sustained highs (>50% for 3-4 intervals), while ps is steady at 2.2%. The process I'm watching doesn't have any children or anything, so I'm not quite sure what to make of it. Since there are sustained high periods in top, I feel I can rule out sampling interval.

Is this really just a discrepancy in how these two tools handle I/O wait time, as suggested by the question I linked to above?

EDIT:
I've seen it fluctuate to 2.1% in ps, but that's it so far. Output from top -p 4522:

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND  
4522 root      16   0  340m 316m 4732 R 54.7  1.3 508:57.46 maui

Output from ps u -p 4522:

 USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND  
root      4522  2.2  1.3 348764 324456 ?       Ss   Aug25 509:25 /usr/local/maui/sbin/maui
  • You mean ps never ever changes and is always 2.2? Could you post the output of ps and top for the command in question so we can have a look? – terdon Sep 10 '13 at 13:18
  • @terdon I updated the original post with additional information. – TTT Sep 11 '13 at 13:16
  • 1
    as mentioned by @vy32, good answer over here: unix.stackexchange.com/questions/58539/… – Tom Carchrae Jun 16 at 11:08
3

The likeliest reason is that top shows the percentage values as a percentage of a single CPU while ps shows the percentage of total available CPU power. Try running top and hitting ShiftI while it's running to show the percentage of all cores.

If this is a server cluster with a lot of CPUs, what you describe is normal behavior. Also see here.

| improve this answer | |
  • Yes, I'm running on 12 cores. That does reduce the % levels that it reaches in top such that the average value is below that of ps, so that may be the difference. The process is a lot less active now, though, so it's hard to say... – TTT Sep 11 '13 at 15:48
  • 1
    @TTT this is a classic issue, it used to drive me up the wall before I discovered the shift+I thing. – terdon Sep 11 '13 at 17:18
7

This question is old, but in my opinion the answer is incorrect. ps and top calculates CPU usage using different methods.

from man top:

  1. %CPU -- CPU Usage The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.

from man ps:

CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process.

So, lets say you have a process that was started a week ago and during that time it used 2.2% of CPU time on average. If suddenly it would became CPU intensive ( constantly consuming 100%) - looking at ps you would observe same 2.2% for the first few hours.

| improve this answer | |
  • 1
    You're half-right in this case, since maui runs continuously in the background. But, even if ps and top averaged over the same time period, there'd still be a 12x difference because of the issue noted by the accepted answer. So, both answers are partially correct. – TTT Jan 12 '19 at 15:25
  • There is an excellent answer the covers this in more details over at Unix Stackexchange: unix.stackexchange.com/questions/58539/… – vy32 Jun 17 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.