Finding total Percentage CPU usage

Hi,

How can I find total CPU usage in percentage? e.g. if my system has 8 CPUs and I want to list total usage for all of them, is it possible through a command?

I have tried some of the commands like top, mpstat, sar. The output of those commands has to be manipulated to derive the percentage CPU utilization. I am looking for some other command.

Thanks in advance.

What hardware?
Which Operating System?
Which shell?

If your OS has mpstat, it is very likely to have vmstat too. vmstat last columns output are the CPU percentages you are looking for.

OS: Red Hat Enterprise Linux AS release 3
Shell: ksh

Top command output..

CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle
           total    8.9%    0.0%    4.3%   0.1%     0.8%   19.3%   66.3%
           cpu00    6.6%    0.0%    4.4%   0.5%     2.4%   20.8%   65.0%
           cpu01    4.0%    0.0%    2.3%   0.0%     0.6%   30.4%   62.4%
           cpu02   13.4%    0.0%    0.7%   0.0%     0.0%   16.7%   68.9%
           cpu03    2.1%    0.0%    1.2%   0.0%     0.0%   17.1%   79.2%
           cpu04    6.4%    0.0%    6.1%   0.0%     0.3%   19.3%   67.6%
           cpu05   16.5%    0.0%    4.5%   0.0%     0.0%   32.8%   45.9%
           cpu06    7.1%    0.0%    6.3%   0.0%     0.6%   19.9%   65.9%
           cpu07   12.7%    0.0%   12.0%   0.0%     0.2%   24.7%   50.1%
           cpu08    9.8%    0.0%    9.5%   1.0%     1.1%   13.1%   65.1%
           cpu09    8.8%    0.0%    5.7%   0.5%     6.4%   17.3%   61.0%
           cpu10   16.9%    0.0%    3.0%   0.0%     0.1%    7.1%   72.6%
           cpu11    7.7%    0.0%    3.6%   0.0%     0.2%   15.0%   73.2%
           cpu12    4.4%    0.0%    2.8%   0.0%     0.1%   15.9%   76.6%
           cpu13    9.0%    0.0%    3.0%   0.0%     0.0%   47.5%   40.2%
           cpu14    3.5%    0.0%    0.7%   0.0%     0.3%    0.8%   94.4%
           cpu15   83.5%    0.0%   16.4%   0.0%     0.0%    0.0%    0.0%

And when press the C .. (Collapsed SMP CPU stats)
the output

CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle
           total   14.2%    0.0%    5.3%   0.1%     0.9%   20.6%   58.7%

Dou you want like this?

You might find that a bit of a setback, but Unix is a general-purpose operating system and not specifically designed to meet your personal needs. The Bell Labs. people, in short-sighted ignorance, only implemented the tools you need to build what you want and did not bother at all to anticipate what especially you might want a few decades later.

With this in mind: write a small script which converts the data you get from the system into the form you want and be done. Its the way things are.

bakunin

$ vmstat
procs                      memory      swap          io     system         cpu
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 0  0      0 10982104 232072 2817792    0    0     0     0    2     2  0  0  0  0

Can you please help me understand the output above?

 
man vmstat

This output is broken. The last four numbers should sum to 100.
Can you please clarify what you are exactly looking for beyond "total percentage CPU usage" ?

In most versions of "vmstat" the first line of statistics are garbage and can be discarded. On most version the output from "vmstat" with no parameters is meaningless.

All of the basic statistics commands like "sar" "mpstat" and "vmstat" take parameters from the command line for the sample period.

For example to take samples every 10 seconds for one minute:

sar 10 6
sar 10 6 | grep "Average"   #  Just show the average line

Many sites set up the "sadc" suite of statistics data collection crons to record statistics for the machine on an hour-by hour basis. This enables the full use of "sar" to provide historical analysis data which you can manipulate as you see fit. Use of trend data is much more useful in server monitoring than trying to do a spot check after there is a problem.

Really depends on what you are trying to achieve and how you will be presenting the information and the time periods involved.

That would be a bug. All vmstat implementations I'm aware of (Unix derivatives, BSD, Gnu/Linux) document the first line to show average values since last reboot.

ditto.

I much prefer a properly set up "sar" system even thought the statistics files use disc space. I usually keep 3-months worth of raw data for rolling trend analysis.

Example of "vmstat" from a HP-UX development system: Note that the first line is garbage. The scripts which I have ported over many years ignore the first line of any "vmstat" or "iostat" output. This is not a new issue at all.

vmstat
         procs           memory                   page                          
    faults       cpu
    r     b     w      avm    free   re   at    pi   po    fr   de    sr     in     sy    cs  us sy id
    1     0     0   522676  907141   10    2     0    0     0    0     0   1168   5757   663  31 79 4231861895168


vmstat 10 6
         procs           memory                   page                          
    faults       cpu
    r     b     w      avm    free   re   at    pi   po    fr   de    sr     in     sy    cs  us sy id
    1     0     0   518736  907250   10    2     0    0     0    0     0   1168   5757   663  31 79 4232048017408
    1     0     0   515225  907224    0    0     0    0     0    0     0    976   9249   668   0  0 100
    1     0     0   520765  907224    0    0     0    0     0    0     0    984   1979   659   0  0 100
    1     0     0   520373  907224    0    0     0    0     0    0     0    986   4095   826   0  0 100
    1     0     0   523508  907224    6    0     0    0     0    0     0   1022   1590   762   0  0 100
    1     0     0   522832  907172    0    0     0    0     0    0     0    990   2188   657   0  0 100

There is indeed a well known issue with implementations where 32 bit counters are still used to store these values and then overflow when the OS hasn't been rebooted for a while. I would say that's a documentation bug as this limitation should be mentioned:

Even with that bug, I wouldn't expect all CPU values to be 0 like the OP sent though.

Just looked at a machine which booted less than a day ago.

us sy id
0   1 99

Plausible? No. When it wasn't running a backup it was running an active database.
Ignoring the field overflow issues (which were and extreme example) I've always found the first line to be garbage.

Well, HP-UX might be that buggy after all if you are sure significant CPU was used on this machine. It's close source so who knows how did their developers messed if you are true ...

On the other hand, a backup use no that much CPU cycles at it's mostly I/O. A database, if really busy should use more. Did you check with sar or similar ?

In any case, I haven't had that kind of issues with Solaris since many years.