One thing that mystify unix newbies is the result difference between top
and ps
commands about CPU usage:
# ps -o %cpu 21162
%CPU
5.5
but a top -p 21162
give us 18%. There is something wrong right ?
Nope. But the confusion rise from the fact that ps
and top
don’t have the same definition of what constitute CPU usage.
top
give you an average value of CPU consumption per core on a short period of time (by default 3 seconds). A value of 200% means that the process “hogged” in average two core during the last 3 seconds.
On the other hand ps
calculate it value on the process lifetime, and don’t take into account how many core have been hog. So a for example a value of 15.3% means that since the process is running 15.3% of it lifetime it has been bugging on the CPU. The other 84.7% of the time the process was doing nothing, probably waiting for some input to append.
So as you can see this two commands have a very different definition of what CPU usage is, and both value are relevant in their own.