2022-09-21 10:47:15 +08:00
|
|
|
#include "fastfetch.h"
|
2023-03-17 17:56:31 +08:00
|
|
|
#include "detection/cpuusage/cpuusage.h"
|
2026-01-06 09:48:38 +08:00
|
|
|
#include "common/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
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
const char* ffGetCpuUsageInfo(FFlist* cpuTimes) {
|
2023-12-04 17:36:45 +09:00
|
|
|
char buf[PROC_FILE_BUFFSIZ];
|
2024-10-18 16:15:48 +08:00
|
|
|
ssize_t nRead = ffReadFileData("/proc/stat", ARRAY_SIZE(buf) - 1, buf);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (nRead < 0) {
|
|
|
|
|
#ifdef __ANDROID__
|
2022-10-27 22:31:57 +08:00
|
|
|
return "Accessing \"/proc/stat\" is restricted on Android O+";
|
2026-03-29 09:28:49 +08:00
|
|
|
#else
|
2024-10-18 16:15:48 +08:00
|
|
|
return "ffReadFileData(\"/proc/stat\", ARRAY_SIZE(buf) - 1, buf) failed";
|
2026-03-29 09:28:49 +08:00
|
|
|
#endif
|
2022-10-27 22:31:57 +08:00
|
|
|
}
|
2023-12-04 17:36:45 +09:00
|
|
|
buf[nRead] = '\0';
|
|
|
|
|
|
2023-10-10 11:18:22 +08:00
|
|
|
// Skip first line
|
2026-07-10 18:08:04 +08:00
|
|
|
char* start = nullptr;
|
|
|
|
|
if ((start = strchr(buf, '\n')) == nullptr) {
|
2023-12-04 17:36:45 +09:00
|
|
|
return "skip first line failed";
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-12-04 17:36:45 +09:00
|
|
|
++start;
|
2023-10-10 11:18:22 +08:00
|
|
|
|
|
|
|
|
uint64_t user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0;
|
2026-07-10 18:08:04 +08:00
|
|
|
char* token = nullptr;
|
2026-03-29 09:28:49 +08:00
|
|
|
while ((token = strchr(start, '\n'))) {
|
|
|
|
|
if (sscanf(start, "cpu%*d%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%*[^\n]\n", &user, &nice, &system, &idle, &iowait, &irq, &softirq) == 7) {
|
2025-06-30 19:36:46 +08:00
|
|
|
uint64_t inUse = user + nice + system + irq + softirq;
|
|
|
|
|
uint64_t total = inUse + idle + iowait;
|
2023-10-10 16:00:23 +08:00
|
|
|
|
2026-04-14 10:41:18 +08:00
|
|
|
FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes);
|
2023-12-04 17:36:45 +09:00
|
|
|
*info = (FFCpuUsageInfo) {
|
2024-06-17 22:51:02 +08:00
|
|
|
.inUseAll = inUse,
|
|
|
|
|
.totalAll = total,
|
2023-12-04 17:36:45 +09:00
|
|
|
};
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2023-12-04 20:34:41 +09:00
|
|
|
break;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-12-04 17:36:45 +09:00
|
|
|
start = token + 1;
|
2022-09-21 10:47:15 +08:00
|
|
|
}
|
2022-09-27 22:06:53 +08:00
|
|
|
|
2026-07-10 18:08:04 +08:00
|
|
|
return nullptr;
|
2022-09-21 10:47:15 +08:00
|
|
|
}
|