summaryrefslogtreecommitdiffstats
path: root/src/detection/cpuusage/cpuusage_sunos.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/detection/cpuusage/cpuusage_sunos.c')
-rw-r--r--src/detection/cpuusage/cpuusage_sunos.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/detection/cpuusage/cpuusage_sunos.c b/src/detection/cpuusage/cpuusage_sunos.c
new file mode 100644
index 0000000..813d74d
--- /dev/null
+++ b/src/detection/cpuusage/cpuusage_sunos.c
@@ -0,0 +1,38 @@
+#include "fastfetch.h"
+#include "detection/cpuusage/cpuusage.h"
+
+#include <kstat.h>
+#include <sys/sysinfo.h>
+
+static inline void kstatFreeWrap(kstat_ctl_t** pkc) {
+ assert(pkc);
+ if (*pkc) {
+ kstat_close(*pkc);
+ }
+}
+
+const char* ffGetCpuUsageInfo(FFlist* cpuTimes) {
+ FF_A_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 = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes);
+ *info = (FFCpuUsageInfo) {
+ .inUseAll = inUse,
+ .totalAll = total,
+ };
+ }
+ return NULL;
+}