在Linux中使用/proc/stat准确计算CPU利用率

20

有很多帖子和参考资料介绍如何使用 /proc/stat 中的统计数据获取 CPU 利用率。然而,大多数只使用了 7+ 个 CPU 统计数据中的四个(user、nice、system 和 idle),而忽略了在 Linux 2.6 中存在的剩余 jiffie CPU 统计数(iowait、irq、softirq)。

例如,请参见 确定 CPU 利用率

我的问题是:iowait/irq/softirq 数字也会计入前四个数字(user/nice/system/idle)之一吗?换句话说,总 jiffie 数是否等于前四个统计数的总和?或者,总 jiffie 数是否等于所有 7 个统计数的总和?如果后者成立,则 CPU 利用率公式应考虑所有数字,如下所示:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  long double a[7],b[7],loadavg;
  FILE *fp;

  for(;;)
  {
    fp = fopen("/proc/stat","r");
    fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]);
    fclose(fp);
    sleep(1);
    fp = fopen("/proc/stat","r");
    fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&b[0],&b[1],&b[2],&b[3],&b[4],&b[5],&b[6]);
    fclose(fp);

    loadavg = ((b[0]+b[1]+b[2]+b[4]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[4]+a[5]+a[6]))
         / ((b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]));
    printf("The current CPU utilization is : %Lf\n",loadavg);

  }

  return(0);
}

1
我也对这个问题很好奇。你在其他来源中找到答案了吗? - justinzane
请参考此答案 - danadam
2个回答

11

我认为iowait/irq/softirq不会被纳入前四个数字的统计之中。您可以查看内核代码中irqtime_account_process_tick的注释以了解更多细节:

(对于Linux kernel 4.1.1)

2815  * Tick demultiplexing follows the order
2816  * - pending hardirq update    <-- this is irq
2817  * - pending softirq update    <-- this is softirq
2818  * - user_time
2819  * - idle_time         <-- iowait is included in here, discuss below
2820  * - system time
2821  *   - check for guest_time
2822  *   - else account as system_time

有关空闲时间处理,请参见account_idle_time函数:

2772 /*
2773  * Account for idle time.
2774  * @cputime: the cpu time spent in idle wait
2775  */
2776 void account_idle_time(cputime_t cputime)
2777 {
2778         u64 *cpustat = kcpustat_this_cpu->cpustat;
2779         struct rq *rq = this_rq();
2780
2781         if (atomic_read(&rq->nr_iowait) > 0)
2782                 cpustat[CPUTIME_IOWAIT] += (__force u64) cputime;
2783         else
2784                 cpustat[CPUTIME_IDLE] += (__force u64) cputime;
2785 }
如果CPU处于空闲状态并且有一些IO正在等待,则计算在CPUTIME_IOWAIT的时间。否则,统计在CPUTIME_IDLE中。
总之,我认为irq/softirq中的jiffies应该被计算为CPU的“繁忙”时间,因为它实际上正在处理某个IRQ或软IRQ。另一方面,“iowait”中的jiffies应该被计算为CPU的“空闲”时间,因为它没有在执行任务而是在等待挂起的IO发生。

3

来自busybox,它的顶部魔法是:

static const char fmt[] ALIGN1 = "cp%*s %llu %llu %llu %llu %llu %llu %llu %llu";

    int ret;

    if (!fgets(line_buf, LINE_BUF_SIZE, fp) || line_buf[0] != 'c' /* not "cpu" */)
        return 0;
    ret = sscanf(line_buf, fmt,
            &p_jif->usr, &p_jif->nic, &p_jif->sys, &p_jif->idle,
            &p_jif->iowait, &p_jif->irq, &p_jif->softirq,
            &p_jif->steal);
    if (ret >= 4) {
        p_jif->total = p_jif->usr + p_jif->nic + p_jif->sys + p_jif->idle
            + p_jif->iowait + p_jif->irq + p_jif->softirq + p_jif->steal;
        /* procps 2.x does not count iowait as busy time */
        p_jif->busy = p_jif->total - p_jif->idle - p_jif->iowait;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接