如何扩展C程序以获得所有核心的CPU使用率

时间:2014-09-23 17:48:09

标签: c cpu cpu-usage

我有计算所有核心的平均CPU使用率的程序。我想扩展程序以单独获取所有内核的CPU使用率。我无法弄清楚如何为每个核心单独做到这一点。 感谢。

/ proc / stat就像这样开始:

cpu  3698413 14728645 5722454 10134230 69449 0 1223 0 0 0
cpu0 976719 3443648 1547164 2603386 19834 0 411 0 0 0
cpu1 919933 3875010 1438785 2355286 17272 0 373 0 0 0
cpu2 989581 3082865 1559116 2922304 18621 0 169 0 0 0
cpu3 812180 4327122 1177389 2253254 13722 0 270 0 0 0

我的计划平均值:

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

#define PROCSTATFILE "/proc/stat"

void eprintf(const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    exit(EXIT_FAILURE);
}

double cpuusage(void) {
    char buf[BUFSIZ];
    static unsigned long long lastuser, lastnice, lastsystem, lastidle;
    unsigned long long user, nice, system, idle;
    unsigned long long total;
    double percent;
    FILE *fp;

    if (lastuser && lastnice && lastsystem && lastidle) {
        fp = fopen(PROCSTATFILE, "r");
        if (!fp)
            eprintf("failed to open %s\n", PROCSTATFILE);
        fgets(buf, BUFSIZ, fp);
        sscanf(buf, "cpu %llu %llu %llu %llu", &user, &nice, &system, &idle);
        fclose(fp);

        percent = 0;
        total = 0;
        total += (user - lastuser);
        total += (nice - lastnice);
        total += (system - lastsystem);
        percent = total;
        total += (idle - lastidle);
        percent /= total;
        percent *= 100;
    }

    fp = fopen(PROCSTATFILE, "r");
    if (!fp)
        eprintf("failed to open %s\n", PROCSTATFILE);
    fgets(buf, BUFSIZ, fp);
    sscanf(buf, "cpu %llu %llu %llu %llu", &lastuser, &lastnice, &lastsystem, &lastidle);
    fclose(fp);

    return percent;
}

int main(void) {
    while (1) {
        printf("cpu usage:%f\n", cpuusage());
        sleep(1);
    }

    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:0)

这是解决方案:

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysinfo.h>

#define PROCSTATFILE "/proc/stat"

void eprintf(const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    exit(EXIT_FAILURE);
}

void *emalloc(size_t size) {
    void *p;

    p = malloc(size);
    if (!p)
        eprintf("out of memory\n");
    return p;
}

int cpucount(void) {
    int i;
    FILE *fp;
    char buf[BUFSIZ];

    fp = fopen(PROCSTATFILE, "r");
    if (!fp)
        eprintf("can't open %s\n", PROCSTATFILE);
    fgets(buf, BUFSIZ, fp);
    for (i = 0; fgets(buf, BUFSIZ, fp); i++)
        if (!!strncmp("cpu", buf, 3))
            break;

    fclose(fp);

    return i;
}

double *cpuusage(void) {
    int i;
    char buf[BUFSIZ];
    int cpus;
    int cpuid;
    int r;
    static unsigned long long *lastuser, *lastnice, *lastsystem, *lastidle;
    unsigned long long *user, *nice, *system, *idle;
    unsigned long long total;
    static double *percent;
    FILE *fp;

    cpus = cpucount();

    if (!lastuser)
        lastuser = emalloc(cpus * sizeof(long long));
    if (!lastnice)
        lastnice = emalloc(cpus * sizeof(long long));
    if (!lastsystem)
        lastsystem = emalloc(cpus * sizeof(long long));
    if (!lastidle)
        lastidle = emalloc(cpus * sizeof(long long));

    user = emalloc(cpus * sizeof(long long));
    nice = emalloc(cpus * sizeof(long long));
    system = emalloc(cpus * sizeof(long long));
    idle = emalloc(cpus * sizeof(long long));

    if (!percent)
        percent = calloc((cpus + 1), sizeof(double));

    fp = fopen(PROCSTATFILE, "r");
    if (!fp)
        eprintf("can't open %s\n", PROCSTATFILE);
    fgets(buf, BUFSIZ, fp);
    for (i = 0; i < cpus; i++) {
        if (lastuser[i] && lastnice[i] && lastsystem[i] && lastidle[i]) {
            fgets(buf, BUFSIZ, fp);
            r = sscanf(buf, "cpu%d %llu %llu %llu %llu",
                    &cpuid, &user[i], &nice[i], &system[i], &idle[i]);
            if (r < 5)
                break;

            percent[i] = i;
            total = i;
            total += (user[i] - lastuser[i]);
            total += (nice[i] - lastnice[i]);
            total += (system[i] - lastsystem[i]);
            percent[i] = total;
            total += (idle[i] - lastidle[i]);
            percent[i] /= total;
            percent[i] *= 100;
        }
    }
    free(user);
    free(nice);
    free(system);
    free(idle);
    fclose(fp);

    fp = fopen(PROCSTATFILE, "r");
    if (!fp)
        eprintf("can't open %s\n", PROCSTATFILE);
    fgets(buf, BUFSIZ, fp);
    for (i = 0; i < 4; i++) {
        fgets(buf, BUFSIZ, fp);
        r = sscanf(buf, "cpu%d %llu %llu %llu %llu",
                &cpuid, &lastuser[i], &lastnice[i], &lastsystem[i], &lastidle[i]);
        if (r < 5)
            break;
    }
    fclose(fp);

    return percent;
}

int main(void) {
    int i;
    double *percent;

    while (1) {
        percent = cpuusage();

        for (i = 0; percent[i]; i++)
            printf("cpu%d:%.2f%%\n", i, percent[i]);
        sleep(1);
    }

    return EXIT_SUCCESS;
}