【问题标题】:Current CPU core utilization via /proc/stat当前 CPU 核心利用率通过 /proc/stat
【发布时间】:2021-09-19 11:12:32
【问题描述】:

我想从 计算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?

【问题讨论】:

  • @AvalSarri 不,这不是。我提到的每个指标只有差异(对于整个 CPU 字符串,而不是内核)。但是这些差异如何表征当前的 CPU 核心利用率?
  • the current CPU core utilization? 获取利用率。等待 1 秒。得到利用。减去。 getloadavg() 的作用完全相同,但在内核内部,周期为 1 分钟/5 分钟/15 分钟。
  • @KamilCuk getloadavg() 有复杂的计算并且没有按核心计算。我需要利用每个 CPU 内核。 "获取利用率。减去。" 我怎样才能获得利用率?似乎/proc/stat 不提供“利用”。对不起,我只是一个初学者
  • 总时间-空闲时间-iowait时间是活动CPU时间。这就是你想要的吗?

标签: c c++ c linux cpu cpu-usage


【解决方案1】:

以下程序:

#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 中确切使用哪些字段。

【讨论】:

    猜你喜欢
    • 2021-03-13
    • 2011-07-27
    • 1970-01-01
    • 2020-08-30
    • 2023-03-30
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    相关资源
    最近更新 更多