13

I'm trying to get an accurate read of my used CPU (in percent) from top. This is the command I'm running for testing:

top -n1 | awk '/Cpu\(s\):/ {print $2}'

This returns:

10.7%us,

Which is the proper piece of data I want. However, every time I run the command I get the same output, even though I am applying different loads on my system (and not to mention htop tells me my usage is different). It seems that whenever I start top, my CPU usage is the same. Only after a couple of frames does it give me proper values.

It doesn't seem like I can parse top's output this way, so I'm looking for other reliable applications which will give me an accurate reading from the shell. I really like how htop can give me a per-core reading.

I've tried iostat and mpstat but they seem to give inaccurate and "slow to change" values.

nopcorn
  • 9,559

4 Answers4

13

I use this script (from this thread on the Arch boards):

#!/bin/bash
read cpu a b c previdle rest < /proc/stat
prevtotal=$((a+b+c+previdle))
sleep 0.5
read cpu a b c idle rest < /proc/stat
total=$((a+b+c+idle))
CPU=$((100*( (total-prevtotal) - (idle-previdle) ) / (total-prevtotal) ))
jasonwryan
  • 73,126
  • Linux has some great info in /proc/ -- see http://linux.die.net/man/5/proc for all the goodies. Just beware that these are Linux only. There's really no cross-platform way to get that information unless you use a library like Sigar. – Pat Notz Dec 19 '11 at 17:16
  • The idea is good, but some details are incorrect. The idle value is the 5th column, and you should also add the iowait column (6th column) to get a reasonable percentage. To be fully correct, you'd have to add all the "rest" values as well, but they are often very small. – Peter Eisentraut Feb 07 '13 at 16:44
  • Idle value is the fifth column, if you are counting the "cpu" column. Otherwise, it is the fourth. – SunSparc Dec 30 '14 at 23:08
6

Check out sar, as well. Implementations can vary widely from nix to nix, but it should give you basic system stats, at given snapshots. I'm not sure how accurate the values are at the point at which the command is first initialized, but you might play around to see how it compares to top, iostat, etc.

The output is column-based, like top, so you should be able to pipe output to awk or cut to manipulate the results.

tcdyl
  • 449
  • I've tried sar before with mixed results. I'm going to go with @jasonwryan's answer here because I can easily modify it to represent usage with both of my CPU cores. – nopcorn Dec 17 '11 at 21:11
3

Have you looked at collectl? It's handy because you can tailor the output to your needs. See: http://collectl.sourceforge.net/

ewwhite
  • 1,521
0

What I found is similar to the person who asked the question above, at least on CentOS 6. If I run top in batch mode for just one iteration, it seems to collect the same figure, almost as if its tendency is to start with what it last remembers displaying. Top seems to need to accumulate at least a couple of figures in order to get deltas to give you correct percentages. Having tested over 10 iterations, I found that the second figure that popped up showed sufficient differentiation on each run. So, its easy enough to get this line via

top -b -n 2 | grep Cpu | tail -1

I like incorporating this with uptime for load and a grep filter of select processes from ps, such as postgres queries. A very simple monitor can be expressed with the alias:

alias postgresmon="watch -d 'uptime;top -b -n 2 | grep Cpu | tail -1;ps -ef | grep postgres\: | grep -v idle'"