You are not logged in.

#1 2012-03-04 02:25:09

konos5
Member
Registered: 2011-03-16
Posts: 56

ps reporting erroneous CPU usage..

Hi everyone,

I came across a very useful command last night which displays the top 10 processes with the largest CPU utilization.

ps -eo pcpu,pid,user,args | sort -n -k 1 -r | head -10

So far so good.

However at some point I fired up google chrome and began to watch a video on youtube. The gnome system monitor indicator was reporting 20-30% CPU usage and for the sake of correctness I thought it would be a good idea to verify that number using my newly found command/script.

Unfortunately it turned out that the script was reporting only 5% CPU usage! Even if did

ps aux

and scrolled through the entire process list (just to rule out a script's fault) the result was the same.

I finally also ran the htop command to confirm which like gnome system monitor also reported a CPU utilization of  ~28%.

Could someone explain to me why is this happening?

Thank you in advance.

P.S. I double checked the PID numbers against both top and htop output and I am absolutely certain it's the exact same process.

Last edited by konos5 (2012-03-04 02:27:50)

Offline

#2 2012-03-04 07:30:16

Gcool
Member
Registered: 2011-08-16
Posts: 1,456

Re: ps reporting erroneous CPU usage..

Extract from the ps manpage:

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 on the other hand gives you a "realtime" view on the situation.


Burninate!

Offline

#3 2012-03-04 11:52:24

konos5
Member
Registered: 2011-03-16
Posts: 56

Re: ps reporting erroneous CPU usage..

So...are you suggesting that the 5% I saw earlier was the time the flashplugin spent in running (R) state while chrome was "active"..?

EDIT: which still doesn't make sense to me cause if that was the case the percentage would never decrease but only increase until we kill the process.

Last edited by konos5 (2012-03-04 12:12:32)

Offline

#4 2012-03-04 12:18:28

Gcool
Member
Registered: 2011-08-16
Posts: 1,456

Re: ps reporting erroneous CPU usage..

The way I always understood it is as follows (might have been very vague my previous post):

* %CPU output in top: the current percentage of cpu that a specific process is using.
* %CPU in ps: the average cpu percentage that a process has been consuming during its life.

So in practice, lets say the flashplugin has been active for several minutes. During those several minutes; there might have been times where the cpu usage was 28% and other times where it was barely using any cpu at all. The %CPU output in "ps aux" will be the average over the whole of the several minutes (which could perfectly be 5%).


Burninate!

Offline

#5 2012-03-04 12:49:06

konos5
Member
Registered: 2011-03-16
Posts: 56

Re: ps reporting erroneous CPU usage..

I think I understand what you are saying now.
Thanks again.

Offline

Board footer

Powered by FluxBB