3

I'm running a quite heavy number crunching thing on a few 4 core servers, all running Ubuntu Precise Pangolin LTS 64 bit, in the cloud (so I suppose in a virtualized environment).

To monitor the cpu usage, i wrote a .sh that uses "top -b -n 1" (that is a single run of top, only the first "frame") and merges it with some other data, to write a small report.

However, top always reported 64% idle on the cpus line, for each server, even if I was quite sure all four cores were 100% busy.

In fact, running top interactively, in the first frame it reports 64% idle time, but as soon as it refresh it reports correct (nearly 0% idle) data.

vmstat too, in the cpu column, always reports 64% idle time on the first line, and then starts reporting (supposedly) real data.

Why is it? Is it a bug in top/vmstat or in the kernel? Or is it a known side effect of how cpu % is measured? Why always 64%?

cpu load is instead always correct (around 4).

2 Answers2

4

This is because top, vmstat, iostat all in their first run collect data since the last reboot time of the system.

And the successive iterations run on the sampling period that you specify. So, in the first run of top, you will see the %idle time because from the time of reboot to the time of running top, it was that much % idle. But in next iterations, since it is busy it doesn't show any %idle.

Exclude the first iteration and try sampling over the interval you want.

Soham Chakraborty
  • 3,534
  • 16
  • 24
  • Nice, always something new to learn. However, how does I tell top to give me the last second and all the life of the server when using -b? I need -b to capture the output of top to a report file. – Simone Gianni Oct 09 '12 at 21:32
  • I don't understand your requirement. Can you elaborate a bit please. Do you want the stats since the last reboot time along with the sampling period you are looking for. If you have specific time requirement between iterations of top use the -d switch, where you can specify delay. – Soham Chakraborty Oct 10 '12 at 06:47
  • Hi Soham, thanks for your answer. I need to capture the output of top and place it in a file, not look at it interactively. That's what the -b (batch) option is there for. I use "top -b -n 1". However, I want, in that file, the current (last second sample) situation, not the situation since the server booted. How can I achieve this? I tried "top -b -n 1 -d 1" but it does not change anything. I could do "top -b -n 2 -d 1" and "filter out" (using awk?) the second "screen", but was searching for a simpler way. – Simone Gianni Oct 10 '12 at 10:22
2

You can do this by grepping the line beginning with "Cpu(s)" and piping the result through tail.

top -b -n2 -d 0.1 |grep "Cpu(s)"|tail -n +1

The tail -n +1 discards the first line (the bad results) and only lets the second line through. The -d 0.1 means a delay of one-tenth of a second between the first and second iteration of top; -b -n2 means run twice in batch mode. The final output of this is the single line containing the "good" results which you can then put to use in a report.

If you need other lines besides the "Cpu(s)" line, rinse and repeat for each one.