summaryrefslogtreecommitdiffstats
path: root/src/detection/cpuusage/cpuusage_linux.c
diff options
context:
space:
mode:
authorsumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
committersumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
commit76424950e373d3b04ac3dd13019151bfba3e8423 (patch)
tree4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/detection/cpuusage/cpuusage_linux.c
Add the files
Diffstat (limited to 'src/detection/cpuusage/cpuusage_linux.c')
-rw-r--r--src/detection/cpuusage/cpuusage_linux.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/detection/cpuusage/cpuusage_linux.c b/src/detection/cpuusage/cpuusage_linux.c
new file mode 100644
index 0000000..fd2f43b
--- /dev/null
+++ b/src/detection/cpuusage/cpuusage_linux.c
@@ -0,0 +1,46 @@
+#include "fastfetch.h"
+#include "detection/cpuusage/cpuusage.h"
+#include "common/io.h"
+
+#include <stdio.h>
+#include <inttypes.h>
+
+const char* ffGetCpuUsageInfo(FFlist* cpuTimes) {
+ char buf[PROC_FILE_BUFFSIZ];
+ ssize_t nRead = ffReadFileData("/proc/stat", ARRAY_SIZE(buf) - 1, buf);
+ if (nRead < 0) {
+#ifdef __ANDROID__
+ return "Accessing \"/proc/stat\" is restricted on Android O+";
+#else
+ return "ffReadFileData(\"/proc/stat\", ARRAY_SIZE(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'))) {
+ 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 + irq + softirq;
+ uint64_t total = inUse + idle + iowait;
+
+ FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes);
+ *info = (FFCpuUsageInfo) {
+ .inUseAll = inUse,
+ .totalAll = total,
+ };
+ } else {
+ break;
+ }
+ start = token + 1;
+ }
+
+ return NULL;
+}