%Cpu Column and Load in Top

%CPU column and load in top

%CPU shown in for example top is intantaneous, i.e. what is being used at that time, while the load shown by uptime is an average:

It conventionally appears in the form of three numbers which represent
the system load during the last one-, five-, and fifteen-minute
periods.

from Wikipedia.

Over time these should converge, but while %CPU is 100% maximum, load can be higher than 1 when though there are processes waiting. For example:

For example, one can interpret a load average of "1.73 0.50 7.98" on a
single-CPU system as:

during the last minute, the CPU was overloaded by 73% (1 CPU with 1.73 runnable processes, so that 0.73 processes had to wait for a turn)

during the last 5 minutes, the CPU was underloaded 50% (no processes had to wait for a turn)

during the last 15 minutes, the CPU was overloaded 698% (1 CPU with 7.98 runnable processes, so that 6.98 processes had to wait for a
turn)

Update:

I only now noticed that your process was using 28% CPU while top reported both CPUs to be >99% idle. I was not able to reproduce this on my Linux system (Ubuntu 12.04, Intel i7-3770K quadcore HT), see screenshots below.

Normal setup, no load, showing CPU usage averaged over all cores:

top - 18:10:04 up  7:50,  2 users,  load average: 0.00, 0.05, 0.15
Tasks: 157 total, 1 running, 156 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 16528224k total, 1124956k used, 15403268k free, 148772k buffers
Swap: 15624188k total, 0k used, 15624188k free, 670460k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7022 root 20 0 0 0 0 S 0 0.0 0:00.14 kworker/0:1
1 root 20 0 3640 2060 1324 S 0 0.0 0:01.23 init
2 root 20 0 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0 0.0 0:00.14 ksoftirqd/0
6 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/0

Full load on one core (running yes > /dev/null), showing CPU usage averaged over all cores:

top - 18:11:58 up  7:52,  2 users,  load average: 0.11, 0.07, 0.15
Tasks: 157 total, 2 running, 155 sleeping, 0 stopped, 0 zombie
Cpu(s): 12.5%us, 0.0%sy, 0.0%ni, 87.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 16528224k total, 1124880k used, 15403344k free, 148824k buffers
Swap: 15624188k total, 0k used, 15624188k free, 670472k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8254 tim 20 0 4188 280 228 R 100 0.0 0:04.88 yes
1 root 20 0 3640 2060 1324 S 0 0.0 0:01.23 init
2 root 20 0 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0 0.0 0:00.15 ksoftirqd/0
6 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/0

Same as 2), but showing per-core CPU usage:

top - 18:13:47 up  7:54,  2 users,  load average: 0.86, 0.36, 0.24
Tasks: 157 total, 2 running, 155 sleeping, 0 stopped, 0 zombie
Cpu0 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 0.3%us, 0.0%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu4 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu5 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu6 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu7 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 16528224k total, 1124756k used, 15403468k free, 148840k buffers
Swap: 15624188k total, 0k used, 15624188k free, 670472k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8254 tim 20 0 4188 280 228 R 100 0.0 1:53.98 yes
1 root 20 0 3640 2060 1324 S 0 0.0 0:01.23 init
2 root 20 0 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0 0.0 0:00.15 ksoftirqd/0
6 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/0

N.B.: I did note that the first statistics top shows do not show the 100% cpu usage for yes, similar to what you have. Perhaps that was the problem?

How do you use top to extract only the CPU usage and the process names (command) columns?

From the command line or from within top?

If you're already in top, press f and toggle the columns you want to see.

Alternatively, you can use ps:

ps -eo %cpu,pid --sort -%cpu

Parsing values from TOP command and then add them up

if your 9th column in /home/user/top_VM.txt containts number like this :

10

12.5

0

0.5

...

you can do :

awk '{ sum+=$9} END {print sum}' /home/user/top_VM.txt

if your 9th column dont contains only number you can do :

#The first awk is to get only number part (12.3 from 12.3%cpu)
awk '{printf ("%f\n",$9)} ' /home/user/top_VM.txt | awk '{ sum+=$1} END {print sum} '

in order to calculate cpu % for each one of your snapshots you can use the following script :

#!/bin/bash
count="0"
VM="VM cpu usage : "
SUM="ONSTART"

while read line ;
do
#extract the first word in each line
TMP=$(echo $line | awk '{printf $1}')

#if its equal to PID then we have a new snapshot
#SUM variable is to prevent writing to result file before calculating cpu% the first time we have a snapshot
if [[ "$TMP" == "PID" && "$SUM" == "NEWVM" ]] ; then
#increment snapshot number
let count++
echo "$count $VM $NUM" >> result
NUM="0"
fi

#if we have a number so its the pid of a process
if [[ $TMP =~ ^[0-9]+$ ]] ; then
#get cpu value
NEWNUM=$(echo $line | awk '{printf ("%f\n",$9)}')
#add new value to previous ones
NUM=$( echo "$NUM $NEWNUM" | awk '{print $1 + $2}')
SUM="NEWVM"
fi

done < /home/user/top_VM.txt
#Save cpu usage for the last snapshot
let count++
echo "$count $VM $NUM" >> result

uptime VS. top CPU usage : What should I believe, why this difference?

Load is not just a measure of how many processes in the R state (runnable, could use CPU time), but also processes in the D state (uninterruptable sleep, usually waiting for IO). You likely have a process in the D state which is contributing to load, but not using cpu. This command would show you all the current processes which are contributing to load:

ps aux | awk '$8~/[RD]/'

Have a look at that output and see if you have commands in the D state (in the 8th column)

What do top %cpu abbreviations mean?

In "top" what are us, sy, ni, id, wa, hi, si and st (for CPU usage)?

  • us - user cpu time (or) % CPU time spent in user space
  • sy - system cpu time (or) % CPU time spent in kernel space
  • ni - user nice cpu time (or) % CPU time spent on low priority processes
  • id - idle cpu time (or) % CPU time spent idle
  • wa - io wait cpu time (or) % CPU time spent in wait (on disk)
  • hi - hardware irq (or) % CPU time spent servicing/handling hardware interrupts
  • si - software irq (or) % CPU time spent servicing/handling software interrupts
  • st - steal time % CPU time in involuntary wait by virtual cpu while hypervisor is servicing another processor (or) % CPU time stolen from a virtual machine

Source: In Linux "top" command what are us, sy, ni, id, wa, hi, si and st (for CPU usage)?

CPU usage in top batch mode

On the first iteration, it is showing you the average CPU usage since system startup.

(Note that this is no longer the case for newer versions of top).

How to suppress the general information for top command

It's known as the "Summary Area" and i don't think there is a way at top initialization to disable those.

But while top is running, you can disable those by pressing l, t, m.

From man top:

 Summary-Area-defaults
'l' - Load Avg/Uptime On (thus program name)
't' - Task/Cpu states On (1+1 lines, see '1')
'm' - Mem/Swap usage On (2 lines worth)
'1' - Single Cpu On (thus 1 line if smp)


Related Topics



Leave a reply



Submit