mpstat/vmstat disagree with top

When I run mpstat or vmstat, the CPU is shown to be mostly idle (95+% in most cases). When I run top, though, it shows my CPU as being 40% idle (mostly in use). What's up with that?

Using vmstat 1, it will slowly adjust to the correct numbers, but I need to get that data on the fly from the command line. Any way I could just cut the crap and skip right to the "real" numbers?

3 Replies

Each vmstat line displays the average CPU percentages since the last line it displayed; the first line will be the average since your last boot. This is because the kernel only keeps track of how much cumulative time has elapsed in various states (via /proc/stat)… you can't know a "right now" percentage, you can only take two samples and compare them.

munin has a "cpu" plugin that handles all this for you.

@bastawhiz:

…I need to get that data on the fly from the command line. Any way I could just cut the crap and skip right to the "real" numbers?

As I recall, top displays the same load average numbers found in the file /proc/loadavg. On the command line, you might use:

cat /proc/loadavg

James

Load average is not the same thing as CPU percentage. As hoopycat explained, there is no "current" CPU percentage being maintained by the kernel - userspace tools have to figure it out by comparing cumulative CPU counters over time.

If you can wait a second for the result you can use something like this:

$ vmstat 1 2 |tail -1

Otherwise you need a daemon like munin watching it for you all the time.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct