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
3
  • 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, 2013 at 13:18
  • @terdon I updated the original post with additional information.
    – TTT
    Sep 11, 2013 at 13:16
  • 2
    as mentioned by @vy32, good answer over here: unix.stackexchange.com/questions/58539/… Jun 16, 2020 at 11:08

2 Answers 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.

4
  • 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, 2013 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, 2013 at 17:18
  • This answer is factually incorrect or misleading. And shoudl not appear at the top. The 14 plus answer is the correct one. PS is simply taking the total cumulative CPU TIm and dividing it by the elpased time. You can use the two statistics to calculate the ratio they present. So if you have a machine that is running for 1 day and that machine has 10 cores, the max CPU time your process can ever have after 1 day is 10 cores * 100%. So 1000%. The PS command is not good to give the CPu utilization for the RIGHT NOW, just for the overall. With high uptime processes, use TOP to see the right now.
    – 99Sono
    Apr 19, 2023 at 9:14
  • @99Sono how is it factually incorrect? Both what I say here, and what the other answer says is correct, there is no contradiction. And, in this specific case, it seems like the issue was a combination of the two.
    – terdon
    Apr 19, 2023 at 9:22
15

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.

3
  • 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, 2019 at 15:25
  • 1
    There is an excellent answer the covers this in more details over at Unix Stackexchange: unix.stackexchange.com/questions/58539/…
    – vy32
    Jun 17, 2020 at 23:49
  • Your is the correct answer. I have just hat to analyze some collected stats with outputs of the TOP command and the PS command. While the top command shown a beast of 1700% cpu utilization, on a server with more than 18 cores the PS was still showing 124% cpu utilization. The reason was process had started burning CPU through the roof just recently with the java parallel GC. So the PS cumulative stats would take quite a bit of time to picku the huge instantaneous cpu util since the process had been running fine for 21 days, before the heart attack. PS had 27,16 d Actual CPU / 21.3 (elapsed)
    – 99Sono
    Apr 19, 2023 at 9:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .