在Linux中以百分比给出的CPU使用率的准确计算? [英] Accurate calculation of CPU usage given in percentage in Linux?

查看:583
本文介绍了在Linux中以百分比给出的CPU使用率的准确计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经问了很多遍了,但是我找不到一个得到很好支持的答案.

It's a question which has been asked many times, however there is no well supported answer I could find.

许多人建议使用top命令,但是如果您只运行一次top(因为您有一个脚本,例如每1秒收集一次Cpu使用情况),它将始终给出相同的Cpu使用情况结果(示例2 ).

Many people suggest the use of top command, but if you run top once (because you have a script for example collecting Cpu usage every 1 second) it will always give the same Cpu usage result (example 1, example 2).

一种更准确的计算CPU使用率的方法是读取/proc/stat中的值,但是大多数答案仅使用/proc/stat中的前4个字段进行计算(一个示例

A more accurate way to calculate CPU usage, is by reading the values from /proc/stat, but most of the answers use only the first 4 fields from /proc/stat to calculate it (one example here).

/proc/stat/每个CPU内核有10个字段!

/proc/stat/ has 10 fields per CPU core as of Linux kernel 2.6.33!

我还发现了在使用以下工具的Linux中准确计算CPU利用率/proc/stat 问题指出了相同的问题-大多数其他问题仅考虑了许多字段中的4个-但此处给出的答案仍以我认为"开头(不确定),除此之外,它只关心前7个字段(/proc/stat/中的10个字段)

I also found this Accurately Calculating CPU Utilization in Linux using /proc/stat question which is pointing out the same issue, -that most other questions only take into consideration 4 out of the many fields- but still the answer given here starts with "I think" (not certain), and except that, it is only concerned about the first 7 fields (out of 10 in /proc/stat/)

perl脚本使用所有字段来计算CPU使用率,我再次这样做经过进一步调查后认为不正确.

This perl script uses all of the fields to calculate the CPU usage, which again I do not think is correct after some further investigation.

快速查看内核代码后

After taking a quick look into the kernel code here, it looks like, for example, guest_nice and guest fields are always increasing together with nice and user (so they should not be included in the cpu usage calculation, since they are included in nice and user fields already)

/*
 * 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使用率的准确方法是什么?在计算中应考虑哪些字段以及如何处理(哪些字段归因于空闲时间,哪些字段归因于非空闲时间)?

So to sum up, what is an accurate way to calculate the CPU usage in Linux and which fields should be considered in the calculations and how (which fields are attributed to the idle time and which fields to non-idle time)?

推荐答案

在撰写本文时,根据 htop 源代码,假设看起来像是有效的:

According the htop source code at the time of writing, my assumptions looks like they are valid:

(请参见 ProcessList.c 上的void ProcessList_scan(ProcessList* this)函数)

// Guest time is already accounted in usertime
usertime = usertime - guest;                     # As you see here, it subtracts guest from user time
nicetime = nicetime - guestnice;                 # and guest_nice from nice time
// Fields existing on kernels >= 2.6
// (and RHEL's patched kernel 2.4...)
idlealltime = idletime + ioWait;                 # ioWait is added in the idleTime
systemalltime = systemtime + irq + softIrq;
virtalltime = guest + guestnice;
totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime;

因此,在/proc/stat第一行中列出的字段中:(请参见文档中的第1.8节)

And so, from fields listed in the first line of /proc/stat: (see section 1.8 at documentation)

     user    nice   system  idle      iowait irq   softirq  steal  guest  guest_nice
cpu  74608   2520   24433   1117073   6176   4054  0        0      0      0

从算法上讲,我们可以像下面这样计算CPU使用率百分比:

Algorithmically, we can calculate the CPU usage percentage like:

PrevIdle = previdle + previowait
Idle = idle + iowait

PrevNonIdle = prevuser + prevnice + prevsystem + previrq + prevsoftirq + prevsteal
NonIdle = user + nice + system + irq + softirq + steal

PrevTotal = PrevIdle + PrevNonIdle
Total = Idle + NonIdle

# differentiate: actual value minus the previous one
totald = Total - PrevTotal
idled = Idle - PrevIdle

CPU_Percentage = (totald - idled)/totald

这篇关于在Linux中以百分比给出的CPU使用率的准确计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆