5 #include <mach/mach_error.h> 6 #include <mach/mach_host.h> 7 #include <mach/mach_init.h> 8 #include <mach/mach_types.h> 9 #include <mach/message.h> 10 #include <mach/vm_map.h> 32 mach_msg_type_number_t& processor_msg_count) {
33 static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<natural_t>::max(),
34 "FwSizeType cannot hold natural_t values");
35 natural_t cpu_count_natural = 0;
37 host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count_natural,
38 reinterpret_cast<processor_info_array_t*>(&cpu_load_info), &processor_msg_count);
39 cpu_count = cpu_count_natural;
47 void cpu_data_release(processor_cpu_load_info_t cpu_load_info, mach_msg_type_number_t processor_msg_count) {
48 (void)vm_deallocate(mach_task_self(),
reinterpret_cast<vm_address_t
>(cpu_load_info),
49 processor_msg_count *
sizeof(integer_t));
64 processor_cpu_load_info_t cpu_load_info;
66 mach_msg_type_number_t processor_msg_count = 0;
67 kern_return_t status =
cpu_data_helper(cpu_load_info, cpu_count, processor_msg_count);
70 if (cpu_count <= cpu_index) {
71 if (KERN_SUCCESS == status) {
74 status = KERN_FAILURE;
75 }
else if (KERN_SUCCESS == status) {
76 processor_cpu_load_info per_cpu_info = cpu_load_info[cpu_index];
80 for (
FwSizeType i = 0; i < CPU_STATE_MAX; i++) {
81 total += per_cpu_info.cpu_ticks[i];
83 used = total - per_cpu_info.cpu_ticks[CPU_STATE_IDLE];
90 processor_cpu_load_info_t cpu_load_info;
91 mach_msg_type_number_t processor_msg_count = 0;
92 if (KERN_SUCCESS ==
cpu_data_helper(cpu_load_info, cpu_count, processor_msg_count)) {
102 if (KERN_SUCCESS == status) {
111 return &this->m_handle;
PlatformSizeType FwSizeType
Cpu variable handle parent.
Status _getTicks(Os::Cpu::Ticks &ticks, FwSizeType cpu_index) override
Get the CPU tick information for a given CPU.
Status _getCount(FwSizeType &cpu_count) override
Request the count of the CPUs detected by the system.
CpuHandle * getHandle() override
returns the raw console handle
Status
Generic OK/ERROR status.
kern_return_t cpu_data_helper(processor_cpu_load_info_t &cpu_load_info, FwSizeType &cpu_count, mach_msg_type_number_t &processor_msg_count)
helper around raw CPU capture API
kern_return_t cpu_by_index(FwSizeType cpu_index, FwSizeType &used, FwSizeType &total)
Query for a single CPU's ticks information.
FwSizeType total
Total amount.
FwSizeType used
Used amount.
Generic used/total struct.
void cpu_data_release(processor_cpu_load_info_t cpu_load_info, mach_msg_type_number_t processor_msg_count)
release the CPU information allocated by the kernel inside cpu_data_helper