Files
fastfetch/src/detection/cpuusage/cpuusage_linux.c
T

37 lines
1.1 KiB
C
Raw Normal View History

2022-09-21 10:47:15 +08:00
#include "fastfetch.h"
2023-03-17 17:56:31 +08:00
#include "detection/cpuusage/cpuusage.h"
#include "common/io/io.h"
2022-09-21 10:47:15 +08:00
#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
{
FF_AUTO_CLOSE_FILE FILE* procStat = fopen("/proc/stat", "r");
2022-09-27 22:06:53 +08:00
if(procStat == NULL)
{
#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";
#endif
}
// Skip first line
if (fscanf(procStat, "cpu%*[^\n]\n") < 0)
return "fscanf() first line failed";
*inUseAll = 0;
*totalAll = 0;
2022-09-27 22:06:53 +08:00
uint64_t user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0;
while (fscanf(procStat, "cpu%*d%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64, &user, &nice, &system, &idle, &iowait, &irq, &softirq) == 7)
2022-09-21 10:47:15 +08:00
{
uint64_t inUse = user + nice + system;
uint64_t total = inUse + idle + iowait + irq + softirq;
*inUseAll += inUse;
*totalAll += total;
2022-09-21 10:47:15 +08:00
}
2022-09-27 22:06:53 +08:00
return NULL;
2022-09-21 10:47:15 +08:00
}