12 #include <mach/mach_error.h>
13 #include <mach/mach_host.h>
14 #include <mach/mach_init.h>
15 #include <mach/mach_types.h>
16 #include <mach/message.h>
34 mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
35 vm_statistics_data_t vmstat;
38 kern_return_t stat1 = host_statistics(mach_host_self(), HOST_VM_INFO,
reinterpret_cast<host_info_t
>(&vmstat), &count);
39 kern_return_t stat2 = host_page_size(mach_host_self(), &vmsize);
41 if (KERN_SUCCESS == stat1 and KERN_SUCCESS == stat2) {
43 used = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count;
44 total = used + vmstat.free_count;
50 return (stat1 == KERN_SUCCESS) ? stat2 : stat1;
63 kern_return_t
cpu_data_helper(processor_cpu_load_info_t& cpu_load_info, U32& cpu_count) {
64 mach_msg_type_number_t processor_msg_count;
65 kern_return_t stat = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count,
66 reinterpret_cast<processor_info_array_t*
>(&cpu_load_info), &processor_msg_count);
83 processor_cpu_load_info_t cpu_load_info;
88 if (cpu_count <= cpu_index) {
90 }
else if (KERN_SUCCESS == stat) {
91 FW_ASSERT(cpu_count > cpu_index, cpu_count, cpu_index);
92 processor_cpu_load_info per_cpu_info = cpu_load_info[(cpu_count > cpu_index) ? cpu_index : 0];
96 for (U32 i = 0; i < CPU_STATE_MAX; i++) {
97 total += per_cpu_info.cpu_ticks[i];
99 used = total - per_cpu_info.cpu_ticks[CPU_STATE_IDLE];
105 processor_cpu_load_info_t cpu_load_info;
117 kern_return_t stat =
cpu_by_index(cpu_index, cpu_ticks.used, cpu_ticks.total);
124 if (KERN_SUCCESS ==
vm_stat_helper(memory_util.used, memory_util.total)) {
128 memory_util.total = 1;
129 memory_util.used = 1;
PlatformSizeType FwSizeType
SystemResourcesStatus getCpuTicks(CpuTicks &ticks, U32 cpu_index=0)
Get the CPU tick information for a given CPU.
SystemResourcesStatus getMemUtil(MemUtil &memory_util)
Get system memory usage.
SystemResourcesStatus getCpuCount(U32 &cpu_count)
Request the count of the CPUs detected by the system.
@ SYSTEM_RESOURCES_OK
Call was successful.
@ SYSTEM_RESOURCES_ERROR
Call failed.
kern_return_t cpu_data_helper(processor_cpu_load_info_t &cpu_load_info, U32 &cpu_count)
helper around raw CPU capture API
kern_return_t cpu_by_index(U32 cpu_index, FwSizeType &used, FwSizeType &total)
Query for a single CPU's ticks information.
kern_return_t vm_stat_helper(FwSizeType &used, FwSizeType &total)
reads macOS virtual memory statistics for memory calculation