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

50 lines
1.4 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
2023-10-10 16:00:23 +08:00
const char* ffGetCpuUsageInfo(FFlist* cpuTimes)
2022-09-21 10:47:15 +08:00
{
char buf[PROC_FILE_BUFFSIZ];
ssize_t nRead = ffReadFileData("/proc/stat", sizeof(buf) - 1, buf);
if(nRead < 0)
{
#ifdef __ANDROID__
return "Accessing \"/proc/stat\" is restricted on Android O+";
#else
return "ffReadFileData(\"/proc/stat\", sizeof(buf) - 1, buf) failed";
#endif
}
buf[nRead] = '\0';
// Skip first line
char *start = NULL;
if((start = strchr(buf, '\n')) == NULL)
return "skip first line failed";
++start;
uint64_t user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0;
char *token = NULL;
while ((token = strchr(start, '\n')))
2022-09-21 10:47:15 +08:00
{
if(sscanf(start, "cpu%*d%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%*[^\n]\n", &user, &nice, &system, &idle, &iowait, &irq, &softirq) == 7)
{
uint64_t inUse = user + nice + system;
uint64_t total = inUse + idle + iowait + irq + softirq;
2023-10-10 16:00:23 +08:00
FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes);
*info = (FFCpuUsageInfo) {
.inUseAll = (uint64_t)inUse,
.totalAll = (uint64_t)total,
};
}
else
2023-12-04 20:34:41 +09:00
break;
start = token + 1;
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
}