2022-09-21 10:47:15 +08:00
|
|
|
#include "fastfetch.h"
|
2023-03-17 17:56:31 +08:00
|
|
|
#include "detection/cpuusage/cpuusage.h"
|
2022-09-21 10:47:15 +08:00
|
|
|
|
|
|
|
|
#include <mach/processor_info.h>
|
|
|
|
|
#include <mach/mach_host.h>
|
2025-03-25 20:03:09 +08:00
|
|
|
#include <mach/vm_map.h>
|
2022-09-21 10:47:15 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
const char* ffGetCpuUsageInfo(FFlist* cpuTimes) {
|
2023-10-10 11:14:04 +08:00
|
|
|
natural_t numCPUs = 0U;
|
|
|
|
|
processor_info_array_t cpuInfo;
|
|
|
|
|
mach_msg_type_number_t numCpuInfo;
|
2022-09-21 10:47:15 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUs, &cpuInfo, &numCpuInfo) != KERN_SUCCESS) {
|
2023-10-10 11:14:04 +08:00
|
|
|
return "host_processor_info() failed";
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
|
|
|
|
if (numCPUs * CPU_STATE_MAX != numCpuInfo) {
|
2023-10-10 11:14:04 +08:00
|
|
|
return "Unexpected host_processor_info() result";
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2022-09-21 10:47:15 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
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];
|
2023-10-10 11:14:04 +08:00
|
|
|
integer_t total = inUse + cpuInfo[CPU_STATE_MAX * i + CPU_STATE_IDLE];
|
2023-10-10 16:00:23 +08:00
|
|
|
|
2026-04-14 10:41:18 +08:00
|
|
|
FFCpuUsageInfo* info = FF_LIST_ADD(FFCpuUsageInfo, *cpuTimes);
|
2023-10-10 16:00:23 +08:00
|
|
|
*info = (FFCpuUsageInfo) {
|
2026-03-29 09:28:49 +08:00
|
|
|
.inUseAll = (uint64_t) inUse,
|
|
|
|
|
.totalAll = (uint64_t) total,
|
2023-10-10 16:00:23 +08:00
|
|
|
};
|
2023-10-10 11:14:04 +08:00
|
|
|
}
|
2025-03-25 20:03:09 +08:00
|
|
|
|
|
|
|
|
vm_deallocate(mach_task_self(), (vm_address_t) cpuInfo, numCpuInfo * sizeof(integer_t));
|
2022-09-21 10:47:15 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|