pcpu表示什么以及为什么乘以1000?

时间:2015-04-22 16:59:56

标签: linux process cpu cpu-usage ps

我正在读关于计算进程的cpu使用情况。

seconds = utime / Hertz

total_time = utime + stime

IF include_dead_children
     total_time = total_time + cutime + cstime
ENDIF

seconds = uptime - starttime / Hertz

pcpu = (total_time * 1000 / Hertz) / seconds

print: "%CPU" pcpu / 10 "." pcpu % 10

我没有得到的是,通过'秒',算法意味着计算机花费在感兴趣的过程以外的操作上的时间。因为,正常运行时间是我们的计算机停止运行的时间,而启动时间意味着我们[感兴趣的]进程开始的时间。

那么为什么我们将total_time除以seconds [时间计算机花在做其他事情上]来获得pcpu?这没有意义。

变量的标准含义:

 #  Name      Description
14  utime     CPU time spent in user code, measured in jiffies
15  stime     CPU time spent in kernel code, measured in jiffies
16  cutime    CPU time spent in user code, including time from children
17  cstime    CPU time spent in kernel code, including time from children 
22  starttime Time when the process started, measured in jiffies

/proc/uptime :The uptime of the system (seconds), and the amount of time spent in idle process (seconds).

Hertz        :Number of clock ticks per second

1 个答案:

答案 0 :(得分:0)

现在您已经提供了每个变量所代表的内容,以下是对伪代码的一些评论:

seconds = utime / Hertz

以上一行毫无意义,因为seconds的新值在它被覆盖几行之前从未使用过。

total_time = utime + stime

流程的总运行时间(用户+系统),以jiffies为单位,因为utimestime都是。

IF include_dead_children
     total_time = total_time + cutime + cstime
ENDIF

这应该只是说total_time = cutime + cstime,因为定义似乎表明,例如cutime已包含utime,以及儿童在用户模式下花费的时间。因此,正如所写,这通过将此过程的贡献包括两次来夸大价值。或者,定义是错误的......无论如何,total_time仍然是jiffies。

seconds = uptime - starttime / Hertz

uptime已经过了几秒钟; starttime / Hertzstarttime从jiffies转换为秒,因此seconds基本上变为“自此过程开始以来的秒数。”

pcpu = (total_time * 1000 / Hertz) / seconds

total_time仍处于jiffies状态,因此total_time / Hertz将其转换为秒,即进程消耗的CPU秒数。除以seconds之外,如果它是浮点运算,则会提供自进程启动以来的缩放CPU使用百分比。由于它不是,它的缩放比例为1000,分辨率为1/10%。通过使用括号强制缩放,以保持准确性。

print: "%CPU" pcpu / 10 "." pcpu % 10

这会通过在pcpu除以10时找到被除数和余数来解除缩放,并以看起来像浮点值的格式打印这些值。