当前 CPU 核心利用率来自 /proc/stat

Current CPU core utilization via /proc/stat

我想根据 or .
计算 Linux 上的当前 CPU 核心利用率 平均负载 (getloadavg()) 不符合我的目的,因为它仅显示具有特定计算的整个 CPU 负载。

根据常识,我了解到当前 CPU 核心负载的字面意思是 0% 或 100%。但我可以用增量时间计算它。

根据 /proc/stat 描述,我看到以下指标:

user: normal processes executing in user mode
nice: niced processes executing in user mode
system: processes executing in kernel mode
idle: twiddling thumbs
iowait: waiting for I/O to complete
irq: servicing interrupts
softirq: servicing softirqs

但我仍然无法弄清楚如何准确计算 CPU 每秒核心负载,例如..
对不起,如果显而易见。

相关post:How can I determine the current CPU utilization from the shell?

以下程序:

#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

struct cpuusage {
    char name[20];
    // Absolute values since last reboot.
    unsigned long long idletime;
    unsigned long long workingtime;
};

struct cpustat {
    char name[20];
    unsigned long long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
};

struct cpuusage cpuusage_from_cpustat(struct cpustat s) {
    struct cpuusage r;
    strncpy(r.name, s.name, sizeof(r.name));
    r.name[sizeof(r.name) - 1] = '[=10=]';
    r.idletime = s.idle + s.iowait;
    r.workingtime = s.user + s.nice + s.system + s.irq + s.softirq;
    return r;
}

void cpuusage_show_diff(struct cpuusage now, struct cpuusage prev) {
    // the number of ticks that passed by since the last measurement
    const unsigned long long workingtime = now.workingtime - prev.workingtime;
    const unsigned long long alltime = workingtime + (now.idletime - prev.idletime);
    // they are divided by themselves - so the unit does not matter.
    printf("Usage: %.0Lf%%\n", (long double)workingtime / alltime * 100.0L);
}

int main() {
    struct cpuusage prev = {0};
    //
    const int stat = open("/proc/stat", O_RDONLY);
    assert(stat != -1);
    fcntl(stat, F_SETFL, O_NONBLOCK);
    while (1) {
        // let's read everything in one call so it's nicely synced.
        int r = lseek(stat, SEEK_SET, 0);
        assert(r != -1);
        char buffer[10001];
        const ssize_t readed = read(stat, buffer, sizeof(buffer) - 1);
        assert(readed != -1);
        buffer[readed] = '[=10=]';
        // Read the values from the readed buffer/
        FILE *f = fmemopen(buffer, readed, "r");
        // Uch, so much borign typing.
        struct cpustat c = {0};
        while (fscanf(f, "%19s %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", c.name, &c.user, &c.nice,
                  &c.system, &c.idle, &c.iowait, &c.irq, &c.softirq, &c.steal, &c.guest,
                  &c.guest_nice) == 11) {
            // Just an example for first cpu core.
            if (strcmp(c.name, "cpu0") == 0) {
                struct cpuusage now = cpuusage_from_cpustat(c);
                cpuusage_show_diff(now, prev);
                prev = now;
                break;
            }
        }
        fclose(f);
        //
        sleep(1);
    }
}

每秒输出第一个核心的使用情况。我可能无法进行计算 - 请咨询此论坛,了解 /dev/stat.

中确切使用哪些字段