Files
fastfetch/src/detection/cpuUsage/cpuUsage_apple.c
T

29 lines
977 B
C
Raw Normal View History

2022-09-21 10:47:15 +08:00
#include "fastfetch.h"
#include "cpuUsage.h"
#include <mach/processor_info.h>
#include <mach/mach_host.h>
2022-09-27 22:06:53 +08:00
const char* ffGetCpuUsageInfo(long* inUseAll, long* totalAll)
2022-09-21 10:47:15 +08:00
{
natural_t numCPUs = 0U;
processor_info_array_t cpuInfo;
mach_msg_type_number_t numCpuInfo;
2022-09-27 22:06:53 +08:00
*inUseAll = *totalAll = 0;
2022-09-21 10:47:15 +08:00
if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUs, &cpuInfo, &numCpuInfo) != KERN_SUCCESS)
return "host_processor_info() failed";
if (numCPUs * CPU_STATE_MAX != numCpuInfo)
return "Unexpected host_processor_info() result";
for (natural_t i = 0U; i < numCPUs; ++i) {
integer_t inUse = cpuInfo[CPU_STATE_MAX * i + CPU_STATE_USER]
+ cpuInfo[CPU_STATE_MAX * i + CPU_STATE_SYSTEM]
+ cpuInfo[CPU_STATE_MAX * i + CPU_STATE_NICE];
integer_t total = inUse + cpuInfo[CPU_STATE_MAX * i + CPU_STATE_IDLE];
*inUseAll += inUse;
*totalAll += total;
}
return NULL;
}