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

查看:26
本文介绍了准确计算 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使用结果(示例 1示例 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 个字段/code> 来计算它(一个例子 这里).

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/ 从 Linux 内核 2.6.33 开始,每个 CPU 内核有 10 个字段!

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

我还发现了这个 Accurately Calculating CPU Utilization in Linux using/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.

快速浏览内核代码后 这里,比如guest_niceguest字段总是一起递增使用 niceuser(因此它们不应包含在 cpu 使用率计算中,因为它们包含在 niceuser 字段已经)

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, my assumptions looks like they are valid:

(参见 static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) 函数在 LinuxProcessList.c)

// 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...)
unsigned long long int idlealltime = idletime + ioWait;  # ioWait is added in the idleTime
unsigned long long int systemalltime = systemtime + irq + softIrq;
unsigned long long int virtalltime = guest + guestnice;
unsigned long long int totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime;

因此,来自 /proc/stat 第一行中列出的字段:(请参阅 文档)

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天全站免登陆