Zadania rekrutacyjne i ćwiczeniowe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

50 lines
1.0 KiB

#include "infrastructure/ProcReader.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
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);
}