From the mpstat manpage:
The interval parameter specifies the
amount of time in seconds between each
report. A value of 0 (or no
parameters at all) indicates that
processors statistics are to be
reported for the time since system
startup (boot)
Running mpstat once only gives you a log-term estimate of usage. Running mpstat with an interval argument will give you more precise numbers.
This discrepancy is an accumulation of rounding errors, which grows with system activity and uptime. The actual statistics dispayed are collected from /proc/stat
and /proc/uptime
. The statistics are counted in USER_HZ (usually 100Hz, getconf CLK_TCK
will confirm this), so your resolution is only 1/100th of a second; and a CPU does a lot in .01 seconds.
As an example:
$ cat /proc/stat; cat /proc/uptime
cpu 414821 51578 226720 66535103 73932 0 4548 0 0 0
cpu0 205014 22950 114302 33188492 36369 0 2071 0 0 0
cpu1 209807 28628 112418 33346611 37563 0 2477 0 0 0
<...>
335694.91 665351.03
On my workstation, which has only been up for a few days, we get the above numbers. If we calculate the percentage of the total time for the cpu stats compared to uptime, we get
(414821 + 51578 + 226720 + 66535103 + 73932 + 4548) / 335694.91 / 2
100.25%
There's a (not very helpful) note about process statistics in the ps
man page as well.
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%