通过/proc/stat 当前 CPU 核心利用率

标签 c linux cpu cpu-usage

我想从 计算 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 核心负载..
抱歉,如果很明显。

相关帖子: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] = '\0';
    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] = '\0';
        // 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 中到底要使用哪些字段。

关于通过/proc/stat 当前 CPU 核心利用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69242699/

相关文章:

c - 通过打开和读取来检测可用设备

c++ - 编译单个大头文件(VS : C1063)

c - 多个数据类型的单个 malloc

c++ - 如何修复 g++ 内存范围重叠?

cpu - RISCV:如何计算分支指令?

c - SPOJ ADDREV 错误答案

java - 我已经使用 maven 构建了一个 jar 文件,但无法在 linux 中使用 "java"命令执行它

c - 让进程使用所有进程能力

delphi - 如何以编程方式探测CPU的硬件虚拟化能力?

c - sigaction 未初始化 gcc 7