7

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
TTT
  • 431

2 Answers2

17

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.

trukt
  • 171
  • 1
  • 2
4

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.

terdon
  • 54,564