Top command the first iteration always returns the same result

When you run top -b -n 1, the command always returns the same CPU values. Consider the following test run 5 times in a row:

[ user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)" Cpu(s): 18.5%us, 10.0%sy, 0.0%ni, 67.0%id, 4.2%wa, 0.0%hi, 0.2%si, 0.1%st Cpu(s): 39.8%us, 27.7%sy, 0.0%ni, 31.3%id, 0.0%wa, 0.0%hi, 1.2%si, 0.0%st Cpu(s): 39.0%us, 35.4%sy, 0.0%ni, 23.2%id, 0.0%wa, 0.0%hi, 1.2%si, 1.2%st Cpu(s): 41.2%us, 34.1%sy, 0.0%ni, 15.3%id, 1.2%wa, 0.0%hi, 2.4%si, 5.9%st Cpu(s): 59.0%us, 30.1%sy, 0.0%ni, 4.8%id, 0.0%wa, 0.0%hi, 3.6%si, 2.4%st [ user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)" Cpu(s): 18.5%us, 10.0%sy, 0.0%ni, 67.0%id, 4.2%wa, 0.0%hi, 0.2%si, 0.1%st Cpu(s): 18.9%us, 17.8%sy, 0.0%ni, 63.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu(s): 18.8%us, 21.2%sy, 0.0%ni, 55.3%id, 2.4%wa, 0.0%hi, 1.2%si, 1.2%st Cpu(s): 29.4%us, 24.7%sy, 0.0%ni, 45.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu(s): 60.5%us, 24.4%sy, 0.0%ni, 11.6%id, 1.2%wa, 0.0%hi, 1.2%si, 1.2%st [ user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)" Cpu(s): 18.5%us, 10.0%sy, 0.0%ni, 67.0%id, 4.2%wa, 0.0%hi, 0.2%si, 0.1%st Cpu(s): 43.4%us, 38.6%sy, 0.0%ni, 15.7%id, 0.0%wa, 0.0%hi, 1.2%si, 1.2%st Cpu(s): 55.3%us, 40.0%sy, 0.0%ni, 4.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu(s): 39.5%us, 48.8%sy, 0.0%ni, 5.8%id, 1.2%wa, 0.0%hi, 1.2%si, 3.5%st Cpu(s): 40.7%us, 55.6%sy, 0.0%ni, 2.5%id, 0.0%wa, 0.0%hi, 0.0%si, 1.2%st [ user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)" Cpu(s): 18.5%us, 10.0%sy, 0.0%ni, 67.0%id, 4.2%wa, 0.0%hi, 0.2%si, 0.1%st Cpu(s): 27.1%us, 10.6%sy, 0.0%ni, 61.2%id, 1.2%wa, 0.0%hi, 0.0%si, 0.0%st Cpu(s): 25.3%us, 5.7%sy, 0.0%ni, 67.8%id, 0.0%wa, 0.0%hi, 0.0%si, 1.1%st Cpu(s): 15.5%us, 16.7%sy, 0.0%ni, 64.3%id, 0.0%wa, 0.0%hi, 0.0%si, 3.6%st Cpu(s): 57.3%us, 11.2%sy, 0.0%ni, 30.3%id, 0.0%wa, 0.0%hi, 0.0%si, 1.1%st [ user@server ~]$ top -b -n 5 -d.2 | grep "Cpu(s)" Cpu(s): 18.5%us, 10.0%sy, 0.0%ni, 67.0%id, 4.2%wa, 0.0%hi, 0.2%si, 0.1%st Cpu(s): 44.0%us, 6.0%sy, 0.0%ni, 42.9%id, 0.0%wa, 0.0%hi, 3.6%si, 3.6%st Cpu(s): 45.8%us, 9.6%sy, 0.0%ni, 44.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu(s): 34.5%us, 8.3%sy, 0.0%ni, 57.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Cpu(s): 38.6%us, 14.5%sy, 0.0%ni, 45.8%id, 0.0%wa, 0.0%hi, 0.0%si, 1.2%st 

Any idea what could be the problem here?

+4
source share
5 answers

CPU utilization is calculated as the average over a period of time. For the first iteration, this time interval is "from system boot to now"; for subsequent iterations, the time interval "from the last iteration until now."

+3
source

If someone is looking for a single line, get the cpu value, try this

top -b -n 5 -d.2 | grep "Cpu" | tail -n1 | awk '{print($2)}' | cut -d'%' -f 1

+8
source

Top does not know how long the process was before it started, so it makes an assumption in the first pass, based on many factors, such as the average load level and the waiting threads.

You can see the same effect if you start from the top in interactive mode and quickly look through the first batch of results that it returns.

Simple solution: top -b -n 5 -d.2 | grep "Cpu(s)" | tail -n+2 top -b -n 5 -d.2 | grep "Cpu(s)" | tail -n+2

+4
source

Here is an example of calculating the total CPU utilization for ALL processes at the top. I use the -d flag to increase the default interval to smooth out the value. Do not use tail because the header may be specific to the Linux distribution.

 top -b -d 5 -n 2 | awk '$1 == "PID" {block_num++; next} block_num == 2 {sum += $9;} END {print sum}' 
+2
source

I ran into this problem and made some jerks.

If you are a โ€œman on topโ€ and scroll down waaaay, you will find the following from section 7.,.

The top command computes Cpu (s) by looking at the change in processor time between samples. When you start it first, it does not have a previous sample for comparison, so these initial values โ€‹โ€‹are percentages from the time of loading. This means that you need at least two loops, or you must ignore the final output from the first loop. This is a problem, for example, for batch mode. There is a possible workaround if you define the environment variable CPULOOP = 1. The top command will run one additional hidden cycle for the CPU data before standard output.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1338960/


All Articles