我想从 c 计算 Linux 上当前的 CPU 核心利用率或c++ .
平均负载 ( 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/