ohmu
·
2026-10-15
1#include "fastfetch.h"
2#include "common/debug.h"
3#include "detection/cpuusage/cpuusage.h"
4
5#include <mach/processor_info.h>
6#include <mach/mach_host.h>
7#include <mach/vm_map.h>
8
9const char* ffGetCpuUsageInfo(FFlist* cpuTimes) {
10 natural_t numCPUs = 0U;
11 processor_info_array_t cpuInfo;
12 mach_msg_type_number_t numCpuInfo;
13
14 if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUs, &cpuInfo, &numCpuInfo) != KERN_SUCCESS) {
15 FF_DEBUG("host_processor_info() failed");
16 return "host_processor_info() failed";
17 }
18 if (numCPUs * CPU_STATE_MAX != numCpuInfo) {
19 FF_DEBUG("Unexpected host_processor_info() result");
20 return "Unexpected host_processor_info() result";
21 }
22
23 for (natural_t i = 0U; i < numCPUs; ++i) {
24 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];
25 integer_t total = inUse + cpuInfo[CPU_STATE_MAX * i + CPU_STATE_IDLE];
26
27 FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes);
28 *info = (FFCpuUsageInfo) {
29 .inUseAll = (uint64_t) inUse,
30 .totalAll = (uint64_t) total,
31 };
32 }
33
34 vm_deallocate(mach_task_self(), (vm_address_t) cpuInfo, numCpuInfo * sizeof(integer_t));
35 return nullptr;
36}