top命令的CPU使用率计算 [英] top command's CPU usage calculation
问题描述
我正在尝试使用GNU coreutil top的公式来计算CPU使用率百分比. 但是top使用的是half_total来计算百分比,这是在百分比上加上0.5.
I am trying to use GNU coreutil top's formula for calculating CPU usages in percentage. But top is using some half_total, to calculate the percentage, which is adding 0.5 to the percentage.
在top的utils.c中来源,下一行(在3.8 beta1版中,其行号为459):-
In utils.c of top's source, the following line (at 3.8 beta1, it is in line number: 459): -
* out ++ =(int)((* diffs ++ * 1000 + half_total)/total_change);
*out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
这意味着: ((* diffs ++ * 1000)/total_change)+ 1/2 因此,它总是给出一个数字,即:百分比的10倍,再加上0.5".因此,如果百分比是x,它将返回10x + 0.5.
This translates to : ( (*diffs++ * 1000) / total_change ) + 1/2 So, it always gives a number, which is: "10 times the percentage, plus 0.5". So if the percentage is x, it will return 10x+0.5.
任何人都可以解释该平均值是如何计算的吗?或至少有一些可以从中获得帮助的指针?
Can anyone explain how is this average calculated? or at least some pointer where I can get the help?
PS:
为什么我们不能只使用(*diffs++/total_change) * 100
来获得所需的百分比?
PS:
Why can't we just use (*diffs++/total_change) * 100
to get the required percentage?
Top的源代码位于:- http://downloads.sourceforge.net/unixtop/top-3.8beta1.tar.gz?modtime=1210117842&big_mirror=0
Top's source code is located at: - http://downloads.sourceforge.net/unixtop/top-3.8beta1.tar.gz?modtime=1210117842&big_mirror=0
推荐答案
这是对整数值进行舍入的方法,因为除法会舍弃小数部分.
This is the way to do rounding for integer values, because the division discards the fractional part.
当您将除数增加一半时,这等于浮点除法,如果小数部分为0.5或更大,则四舍五入.
When you add half the divisor this is equivalent to a floating point division and rounding up if the fractional part is 0.5 or greater.
这篇关于top命令的CPU使用率计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!