为什么CPU负载不会改变超过百分之几吗? [英] Why does cpu load not change more than a few hundredths?
问题描述
我在运行此命令
的grep'CPU'的/ proc / STAT | AWK'{使用=($ 2 + $ 4)* 100 /($ 2 + $ 4 + $ 5)} END {打印使用%}'
但它只输出像0.99xxxx%
如果我做了一个易于得到升级或任何过程中,我会想象它会去1%以上。即使在运行压力-c 1不让它改变任何
有一个准确记录CPU使用率的方法吗?服务器有1个vCPU。
需要有这样的日志每5秒。
而睡5;做code>>日志文件; DONE
为什么CPU负载不超过百分之几改变吗?
块引用>由于
的/ proc / STAT
是因为系统返回的汇总CPU负载统计上次引导,而不是实时的。如果你只是在重启后运行脚本,该报告的负荷可能会显著改变,只要CPU负载本身的变化。但随着时间的脚本运行的小负荷的变化将影响显示值,一段时间后,该值将基本保持不变。如果要计算从
负载的/ proc / STAT
,而不是用做的,你需要从两个连续的样本计算的差异已经可用的工具,例如:而睡5;做grep的-w CPU的/ proc / STAT;做| \\
AWK'{
打印(02 + $ o4- $ 2- 4)* 100 /(02 + O4 + o5- $ 2- $ 4- $ 5)%
O2 = $ 2; O4 = $ 4; O5 = $ 5}'否则,简单,但不太精确的方式可以是:
的vmstat -n 5 | \\
AWK'{使用= $ 13 + $ 14;总=使用+ $ 15个
如果(总大于0)打印使用* 100 /总%}'I'm running this command
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
Yet it only outputs something like 0.99xxxx%
If I do an apt-get upgrade or any process, I would imagine it would go above 1%. Even running stress -c 1 doesn't make it change any.
Is there a way to log CPU usage accurately? Server has 1 vCPU.
Need to have this log every 5 seconds.
while sleep 5; do "code" >> logfile; done
解决方案Why does cpu load not change more than a few hundredths?
Because
/proc/stat
is returning aggregated CPU load statistics since the system last booted, not real-time ones. If you run your script just after a reboot, the reported load might significantly change as long as the CPU load itself changes. However, the longer the script runs the lesser load changes will impact the displayed value and after a while, the value will essentially stay constant.If you want to compute the load from
/proc/stat
and not use the already available tools that do it, you need to compute the difference from two consecutive samples, eg :while sleep 5; do grep -w cpu /proc/stat ; done | \ awk '{ print (o2+o4-$2-$4)*100/(o2+o4+o5-$2-$4-$5) "%" o2=$2;o4=$4;o5=$5}'
Otherwise, a simpler but less accurate way might be:
vmstat -n 5 | \ awk '{used=$13+$14;total=used+$15 if(total>0) print used*100/total "%"}'
这篇关于为什么CPU负载不会改变超过百分之几吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!