#include "infrastructure/ProcReader.h" #include #include #include #include uint8_t countCpuCores(void) { uint8_t count = 0; char line[256]; FILE* fp = fopen("/proc/cpuinfo", "r"); if (fp == NULL) { fprintf(stderr, "Unable to open /proc/cpuinfo\n"); return 0; } while (fgets(line, sizeof(line), fp)) { if (strncmp(line, "core id", 7) == 0) { ++count; } } fclose(fp); return count; } void readCpuStats(CpuStats* stats, uint8_t numCores) { // https://www.linuxhowtos.org/System/procstat.htm char line[256]; FILE* fp = fopen("/proc/stat", "r"); if (fp == NULL) { fprintf(stderr, "Unable to open /proc/stat\n"); exit(1); } fgets(line, sizeof(line), fp); // ignore the first line for (int i = 0; i < numCores; i++) { fgets(line, sizeof(line), fp); sscanf(line, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, stats[i].cpuName, &stats[i].user, &stats[i].nice, &stats[i].system, &stats[i].idle); } fclose(fp); }