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

39 lines
993 B
C
Raw Normal View History

2024-06-17 22:51:40 +08:00
#include "fastfetch.h"
#include "detection/cpuusage/cpuusage.h"
#include <kstat.h>
#include <sys/sysinfo.h>
2026-03-29 09:28:49 +08:00
static inline void kstatFreeWrap(kstat_ctl_t** pkc) {
2024-06-17 22:51:40 +08:00
assert(pkc);
2026-03-29 09:28:49 +08:00
if (*pkc) {
2024-06-17 22:51:40 +08:00
kstat_close(*pkc);
2026-03-29 09:28:49 +08:00
}
2024-06-17 22:51:40 +08:00
}
2026-03-29 09:28:49 +08:00
const char* ffGetCpuUsageInfo(FFlist* cpuTimes) {
2026-04-09 16:20:59 +08:00
FF_A_CLEANUP(kstatFreeWrap) kstat_ctl_t* kc = kstat_open();
2026-03-29 09:28:49 +08:00
if (!kc) {
2024-06-17 22:51:40 +08:00
return "kstat_open() failed";
2026-03-29 09:28:49 +08:00
}
2024-06-17 22:51:40 +08:00
2026-03-29 09:28:49 +08:00
for (int i = 0;; ++i) {
2026-07-10 18:08:04 +08:00
kstat_t* ks = kstat_lookup(kc, "cpu_stat", i, nullptr);
2024-06-17 22:51:40 +08:00
cpu_stat_t cs;
2026-03-29 09:28:49 +08:00
if (!ks || kstat_read(kc, ks, &cs) < 0) {
2024-06-17 22:51:40 +08:00
break;
2026-03-29 09:28:49 +08:00
}
2024-06-17 22:51:40 +08:00
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];
2026-04-14 10:41:18 +08:00
FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes);
2024-06-17 22:51:40 +08:00
*info = (FFCpuUsageInfo) {
.inUseAll = inUse,
.totalAll = total,
};
}
2026-07-10 18:08:04 +08:00
return nullptr;
2024-06-17 22:51:40 +08:00
}