2022-09-21 10:47:15 +08:00
|
|
|
#include "fastfetch.h"
|
|
|
|
|
#include "cpuUsage.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2022-10-07 16:52:52 +08:00
|
|
|
#include <inttypes.h>
|
2022-09-21 10:47:15 +08:00
|
|
|
|
2022-10-07 16:52:52 +08:00
|
|
|
const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll)
|
2022-09-21 10:47:15 +08:00
|
|
|
{
|
2022-10-07 16:52:52 +08:00
|
|
|
uint64_t user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0;
|
2022-09-21 10:47:15 +08:00
|
|
|
|
2022-09-27 22:06:53 +08:00
|
|
|
FILE* procStat = fopen("/proc/stat", "r");
|
|
|
|
|
if(procStat == NULL)
|
2022-10-27 22:31:57 +08:00
|
|
|
{
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
return "Accessing \"/proc/stat\" is restricted on Android O+";
|
|
|
|
|
#else
|
2022-09-27 22:06:53 +08:00
|
|
|
return "fopen(\"""/proc/stat\", \"r\") == NULL";
|
2022-10-27 22:31:57 +08:00
|
|
|
#endif
|
|
|
|
|
}
|
2022-09-27 22:06:53 +08:00
|
|
|
|
2022-10-07 16:52:52 +08:00
|
|
|
if (fscanf(procStat, "cpu%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64, &user, &nice, &system, &idle, &iowait, &irq, &softirq) < 0)
|
2022-09-21 10:47:15 +08:00
|
|
|
{
|
|
|
|
|
fclose(procStat);
|
|
|
|
|
return "fscanf() failed";
|
|
|
|
|
}
|
|
|
|
|
*inUseAll = user + nice + system;
|
|
|
|
|
*totalAll = *inUseAll + idle + iowait + irq + softirq;
|
|
|
|
|
|
|
|
|
|
fclose(procStat);
|
2022-09-27 22:06:53 +08:00
|
|
|
|
|
|
|
|
return NULL;
|
2022-09-21 10:47:15 +08:00
|
|
|
}
|