这个问题已经被问过很多次了,但是我找不到一个得到很好支持的答案。
很多人建议使用top命令,但是如果您运行一次top(因为您有一个脚本,例如每1秒收集一次Cpu使用情况),它将始终给出相同的Cpu使用情况结果(示例1,示例2)。
计算CPU使用率的一种更准确的方法是,从中读取值/proc/stat,但是大多数答案仅使用from中的前4个字段/proc/stat进行计算(此处是一个示例)。
/proc/stat/ 从Linux内核2.6.33开始,每个CPU内核有10个字段!
我还发现使用/ proc / stat这个问题在Linux中准确地计算CPU利用率,该问题指出了同样的问题-
大多数其他问题仅考虑了多个字段中的4个-但此处给出的答案仍以“ I思考”(不确定),但仅关注前7个字段(10个中的/proc/stat/)
该
perl脚本使用所有字段来计算CPU使用率,经过进一步研究,我再次认为这是不正确的。
快速浏览一下此处的内核代码后,它看起来像,guest_nice并且guest
fields总是与nice和一起增加user(因此,它们不应包括在niceand user字段中,因此不应该包括在cpu使用率计算中)
/*
* Account guest cpu time to a process.
* @p: the process that the cpu time gets accounted to
* @cputime: the cpu time spent in virtual machine since the last update
* @cputime_scaled: cputime scaled by cpu frequency
*/
static void account_guest_time(struct task_struct *p, cputime_t cputime,
cputime_t cputime_scaled)
{
u64 *cpustat = kcpustat_this_cpu->cpustat;
/* Add guest time to process. */
p->utime += cputime;
p->utimescaled += cputime_scaled;
account_group_user_time(p, cputime);
p->gtime += cputime;
/* Add guest time to cpustat. */
if (task_nice(p) > 0) {
cpustat[CPUTIME_NICE] += (__force u64) cputime;
cpustat[CPUTIME_GUEST_NICE] += (__force u64) cputime;
} else {
cpustat[CPUTIME_USER] += (__force u64) cputime;
cpustat[CPUTIME_GUEST] += (__force u64) cputime;
}
}
因此,总而言之,什么是在Linux中计算CPU使用率的准确方法?计算中应考虑哪些字段以及如何处理(哪些字段属于空闲时间,哪些字段属于非空闲时间)?