簡體   English   中英

如何使用linux top命令計算CPU負載

[英]How to calculate CPU load using linux top command

我正在運行top -b -n2 -d 1 | grep Cpu top -b -n2 -d 1 | grep Cpu循環執行,並注意到它在每次迭代中都返回兩個條目...

1)每個循環有兩行結果...我應該使用第一行還是第二行...兩者之間有什么區別?

2)要計算CPU利用率,是否要添加%us,%sy,%ni,%hi和%si?

Cpu(s):  1.6%us,  1.7%sy,  0.0%ni, 96.6%id,  0.0%wa,  0.0%hi,  0.1%si,  0.0%st
Cpu(s):  8.7%us,  9.4%sy,  0.0%ni, 81.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st


Cpu(s):  1.6%us,  1.7%sy,  0.0%ni, 96.6%id,  0.0%wa,  0.0%hi,  0.1%si,  0.0%st
Cpu(s):  9.7%us,  8.9%sy,  0.0%ni, 81.5%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

我需要第二項...

command = "top -bn 2 -d 0.01 | grep '^Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'" 


The 1st iteration of top -b returns the percentages since boot, 
We need at least two iterations (-n 2) to get the current percentage. 
To speed things up, you can set the delay between iterations to 0.01. 
top splits CPU usage between user, system processes and nice processes, we want the sum of the three. 
Finally, you grep the line containing the CPU percentages and then use gawk to sum user, system and nice processes:

top -bn 2 -d 0.01 | grep '^Cpu' | tail -n 1 | gawk '{print $2+$4+$6}'
        -----  ------   -----------    ---------   ----------------------
        |      |           |             |             |------> add the values
        |      |           |             |--> keep only the 2nd iteration
        |      |           |----------------> keep only the CPU use lines
        |      |----------------------------> set the delay between runs
        |-----------------------------------> run twice in batch mode

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM