1

I am running a test on Ubuntu 14.04. When I check my CPU usage using 'ps aux|grep service' then CPU usage is 0.1 of a process, while in htop for the same process the CPU% is 12.3.

Can anyone tell me the reason? or which value should I consider the right one?

Thanks

anonymous255
  • 108
  • 10

1 Answers1

6

They are measuring different things.

From the ps man-page:

   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%.   

From the htop man-page (I am the author of htop):

   PERCENT_CPU (CPU%)
        The  percentage  of  the  CPU  time  that the process is currently
        using.

So, in htop this is the percentage of total CPU time used by the program between the last refresh of the screen and now.

PercentageInHtop = (non-idle CPU time used by process during the last 1.5s) / 1.5s

In ps this is the percentage of CPU time used by the program relative to the total time it exists (ie, since it was launched).

PercentageInPs = (non-idle CPU time used by process since process startup) / (time elapsed since process startup)

That is, in your reading it means that htop is saying that the service is taking 12.3% of your CPU now, while ps is saying that your service has spent 99.9% of its total life idle.

Hisham H M
  • 6,398
  • 1
  • 29
  • 30
  • I didn't get it. I collected above reading at the same time. If ps is showing me the cpu% usage of the process since the process started, it should be approximately equal or logically greater than the htop, as htop is showing the reading since I wrote htop in my terminal right? – anonymous255 Mar 10 '16 at 16:31
  • No. htop this is the percentage of total CPU time used by the program between the last *refresh of the screen* (the last time it recalculated values) and now (in other words, during the last 1.5 second, by default). – Hisham H M Mar 13 '16 at 20:07
  • Shouldn't it be less than ps?? – anonymous255 Mar 14 '16 at 04:34
  • No, they are really totally different things! The usage in htop may be high because of a temporary spike, and in total the lifetime use of the process (reflected by ps) may still be low. I added equations in the answer above to explain it more clearly. – Hisham H M Mar 15 '16 at 04:07