Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking for a tool that outputs very simple usage information on the CPU at certain intervals. I think I've used a tool like this before a few years ago but cannot remember the name. I want to be able to plot a graph of the how the CPU rises and falls during load tests.

Thanks in advance.

share|improve this question
    
How many CPUs do you have? Do you want a breakdown for each CPU or the overall, combined CPU usage? –  terdon May 28 '13 at 17:28
    
I guess it would be combined, but getting an idea of each would also be helpful. lscpu tells me I have 8 CPU's. –  SutureSelf May 28 '13 at 17:34

2 Answers 2

up vote 2 down vote accepted

The easiest way I know to do this is to use top in batch mode and single iteration:

$ top -bn 1| grep Cpu
 %Cpu(s):  5.0 us,  2.8 sy,  0.0 ni, 91.4 id,  0.5 wa,  0.0 hi,  0.2 si,  0.0 st

See my answer here for an explanation of the fields. Suffice it to say you want the sum of us, sy and ni so some parsing is required:

$ top -bn 1| grep Cpu | gawk '{print $2+$4+$6}' > logfile

The above command returns 7.8, meaning that 7.8% of available CPU power is in use at that particular moment. You can use cron to run that command, for example, every minute:

$ crontab -e

This will open an editor window (of whichever editor you have set as the $EDITOR shell variable), in that window paste this line and then save and close it:

* * * * * top -bn 1| grep Cpu | gawk '{print $2+$4+$6}' >> ~/logfile

That will cause the CPU percentage to be written to ~/logfile every two minutes.


If you want a breakdown for each CPU, you could parse the output of mpstat:

$ mpstat -P ALL | tail -n +4 | gawk '{print "CPU:"$3,$4+$5+$6}'
CPU:all 7.87
CPU:0 10.73
CPU:1 10.75
CPU:2 4.97
CPU:3 5.09
share|improve this answer
    
Perfect. This is exactly what I'm after. The level of detail you provided is above and beyond, thanks a lot! –  SutureSelf May 29 '13 at 10:16
    
Just noticed, in the cronjob, should it be */2 * * * * ... for every 2 minutes? –  SutureSelf May 29 '13 at 10:23
    
@NomNomNom yes, what I wrote will actually run every minute, I'd said two because I thought that was the minimum interval cron deals with. I just tested and it is in fact every minute. Answer corrected. –  terdon May 29 '13 at 11:53

Depends what you want:

  • /usr/bin/top returns info, given a number of iterations or other specifiers, that will help you;
  • /proc/cpuinfo shows what number of cpus you have, to help your tool figure out usage stats;
  • For interactive use in Ubuntu, there are desktop widgets to display what you want graphically. For instance: https://launchpad.net/indicator-applet

-- Edit --

Questioner requests writing to logfile. I would recommend using:

 vmstat -s

this outputs:

    99052720  total memory
    60854068  used memory
     3565184  active memory
    19739432  inactive memory
    38198648  free memory
     3221080  buffer memory
    20045176  swap cache
     2097144  total swap
       10836  used swap
     2086308  free swap
   124233445 non-nice user cpu ticks
        3023 nice user cpu ticks
    45760329 system cpu ticks
 21762927158 idle cpu ticks
      124003 IO-wait cpu ticks
        9748 IRQ cpu ticks
     1040367 softirq cpu ticks
           0 stolen cpu ticks
     4503955 pages paged in
  1180587414 pages paged out
        1637 pages swapped in
       11093 pages swapped out
      708030 interrupts
  2814409153 CPU context switches
  1377801399 boot time
    40693435 forks

So, run this command and pipe to a file or read it in with python. Then, repeat, and diff the results to find cpu usage.

Note, the term 'CPU TICKS' is SERIOUSLY MISLEADING. Do NOT try to convert ticks to seconds, you will fail and search the web for why and pound your head until the physical pain matches your mental pain.

Only use these ticks as RELATIVE measures. That means, add up all the ticks, and divide the proportion of them spent doing one specific thing. Something like:

 idlePercent = (idle / (idle+kernel+user+nonNiceUser+softirq+irq+iowait+stolen)) * 100
share|improve this answer
    
Thanks for your answer. Sorry I probably should've mentioned in my question that I want it to write to a log file. Like, at the first interval, print CPU usage at that point, then at the next interval, print the CPU usage at that point, etc. Then I can take the log file and plot a graph of the usage over a several hour load testing period. Unless I can do that with top...? Also, I don't seem to have /proc/cpuinfo on my system. –  SutureSelf May 28 '13 at 16:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.