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

37 lines
1.1 KiB
C
Raw Normal View History

2023-03-17 17:56:31 +08:00
#include "detection/cpuusage/cpuusage.h"
#include "util/mallocHelper.h"
2022-10-27 10:08:22 +08:00
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/resource.h>
#include <stdlib.h>
2022-10-27 10:08:22 +08:00
2023-10-10 16:00:23 +08:00
const char* ffGetCpuUsageInfo(FFlist* cpuTimes)
2022-10-27 10:08:22 +08:00
{
size_t neededLength = 0;
if(sysctlbyname("kern.cp_times", NULL, &neededLength, NULL, 0) != 0)
return "sysctlbyname(kern.cp_times, NULL) failed";
2022-10-27 10:08:22 +08:00
uint32_t coreCount = (uint32_t) (neededLength / (CPUSTATES * sizeof(uint64_t)));
assert(coreCount > 0);
FF_AUTO_FREE uint64_t (*cpTimes)[CPUSTATES] = malloc(neededLength);
if(sysctlbyname("kern.cp_times", cpTimes, &neededLength, NULL, 0) != 0)
return "sysctlbyname(kern.cp_times, cpTime) failed";
for (uint32_t i = 0; i < coreCount; ++i)
{
uint64_t* cpTime = cpTimes[i];
uint64_t inUse = cpTime[CP_USER] + cpTime[CP_NICE] + cpTime[CP_SYS];
2024-06-17 22:51:02 +08:00
uint64_t total = inUse + cpTime[CP_INTR] + cpTime[CP_IDLE];
2023-10-10 16:00:23 +08:00
FFCpuUsageInfo* info = (FFCpuUsageInfo*) ffListAdd(cpuTimes);
*info = (FFCpuUsageInfo) {
2024-06-17 22:51:02 +08:00
.inUseAll = inUse,
.totalAll = total,
2023-10-10 16:00:23 +08:00
};
}
2022-10-27 10:08:22 +08:00
return NULL;
}