Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am building a Linux, web-based resource monitor.

For now I am looking forward to use:

  • df for reading HDD usage status
  • /proc/meminfo (through cat) for reading RAM status
  • mpstat (from sysstat package) for reading CPU usage

While the df and /proc/meminfo returns the data almost instantly, mpstat delays for the interval I specify - with the minimum accepted value being 1 (in seconds).

Is there a real-time CPU usage reader, or is there a way to make mpstat return "instantly"?

mpstat output:

[psycketom@stone ~]# mpstat -P ALL 1 1
Linux 3.5.4-1-ARCH (stone)      03/05/2013      _x86_64_        (1 CPU)

11:33:15 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
11:33:16 AM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
11:33:16 AM    0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

Average:     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
Average:     all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
Average:       0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
share|improve this question
I don't know mpstat. Can you provide an example output please? – mpy Mar 5 '13 at 8:35
@mpy, Q updated with example. – joltmode Mar 5 '13 at 9:35
Is top not suited to your purposes for some reason? – Joseph R. Mar 5 '13 at 9:39
I'm looking for a direct output to STDOUT, which I could grep. top continuously polls. – joltmode Mar 5 '13 at 9:46
up vote 4 down vote accepted

You can use top in batch mode and a single iteration:

top -bn 1

I would also use free instead of parsing meminfo.

Another way is to use ps, add the CPU% of each process and divide by the number of cores that your machine has:

ps axo pcpu | 
 gawk -v cores=`grep -m 1 cores /proc/cpuinfo | cut -d ' ' -f 3` \
      -v cpus=`grep -c processor /proc/cpuinfo` \
         '{k+=$1}END{print k/(cores * cpus);}'

If your system has only one core, the above script will fail, producing:

gawk: cmd. line:1: (FILENAME=- FNR=83) fatal: division by zero attempted

In order to make it run, change k/(cores * cpus) to k/(cores==""?1:cores * cpus). This will check whether the cores variable is not empty, if it is, 1 will be used instead.

share|improve this answer
Thanks for the free tip, and looks like I am gonna stick with top then. Accepted! – joltmode Mar 5 '13 at 12:18
Hmm, it seems to me, that top does not reflect the real usage. I made about 60 web-requests to the server, and all of them stayed on 97.7% idle, even though the table showed usage at 5.8% for top. I mean, it showed 97.7% in the header %Cpu(s): 1.5 us, 0.6 sy, 0.1 ni, 97.7 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st. Do I have to read the total usage from somewhere else or should I calculate on my end? – joltmode Mar 5 '13 at 16:04
@psycketom The table showed 5.8% usage of a single core for top. The percentages are for each individual core. So, if foo is taking 40% of a single core of a 2-core machine, top will show 40% for foo, not 20% as you might expect. Also see my updated answer for another way of getting actual usage. – terdon Mar 5 '13 at 16:38
Since my system has only one core, this ended up in a error. Changing to k/(cores==""?1:cores * cpus) fixed the problem. – joltmode Mar 6 '13 at 8:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .