From 1b29f5b3a5fbdb209a7eecffab6a30f4eae471f2 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Mon, 17 Jun 2024 22:51:40 +0800 Subject: [PATCH] SunOS: support CPUUsage detection --- CMakeLists.txt | 2 +- src/detection/cpuusage/cpuusage_sunos.c | 38 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/detection/cpuusage/cpuusage_sunos.c diff --git a/CMakeLists.txt b/CMakeLists.txt index dc4a6485b..2d502d4ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -750,7 +750,7 @@ elseif(SunOS) src/detection/chassis/chassis_windows.c src/detection/cpu/cpu_sunos.c src/detection/cpucache/cpucache_nosupport.c - src/detection/cpuusage/cpuusage_nosupport.c + src/detection/cpuusage/cpuusage_sunos.c src/detection/cursor/cursor_linux.c src/detection/bluetooth/bluetooth_nosupport.c src/detection/disk/disk_sunos.c diff --git a/src/detection/cpuusage/cpuusage_sunos.c b/src/detection/cpuusage/cpuusage_sunos.c new file mode 100644 index 000000000..2e36adbfe --- /dev/null +++ b/src/detection/cpuusage/cpuusage_sunos.c @@ -0,0 +1,38 @@ +#include "fastfetch.h" +#include "detection/cpuusage/cpuusage.h" + +#include +#include + +static inline void kstatFreeWrap(kstat_ctl_t** pkc) +{ + assert(pkc); + if (*pkc) + kstat_close(*pkc); +} + +const char* ffGetCpuUsageInfo(FFlist* cpuTimes) +{ + __attribute__((__cleanup__(kstatFreeWrap))) kstat_ctl_t* kc = kstat_open(); + if (!kc) + return "kstat_open() failed"; + + for (int i = 0;; ++i) + { + kstat_t* ks = kstat_lookup(kc, "cpu_stat", i, NULL); + + cpu_stat_t cs; + if (!ks || kstat_read(kc, ks, &cs) < 0) + break; + + uint64_t inUse = cs.cpu_sysinfo.cpu[CPU_USER] + cs.cpu_sysinfo.cpu[CPU_KERNEL]; + uint64_t total = inUse + cs.cpu_sysinfo.cpu[CPU_IDLE] + cs.cpu_sysinfo.cpu[CPU_WAIT]; + + FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes); + *info = (FFCpuUsageInfo) { + .inUseAll = inUse, + .totalAll = total, + }; + } + return NULL; +}