F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Cpu.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Darwin/Cpu.cpp
3 // \brief Darwin implementation for Os::Cpu
4 // ======================================================================
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>
11 #include <Fw/Types/Assert.hpp>
12 #include <Os/Darwin/Cpu.hpp>
13 
14 namespace Os {
15 namespace Darwin {
16 namespace Cpu {
17 
30 kern_return_t cpu_data_helper(processor_cpu_load_info_t& cpu_load_info,
31  FwSizeType& cpu_count,
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;
36  kern_return_t stat =
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;
40  return stat;
41 }
42 
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));
50 }
51 
63 kern_return_t cpu_by_index(FwSizeType cpu_index, FwSizeType& used, FwSizeType& total) {
64  processor_cpu_load_info_t cpu_load_info;
65  FwSizeType cpu_count = 0;
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);
68 
69  // Failure for CPU index
70  if (cpu_count <= cpu_index) {
71  if (KERN_SUCCESS == status) {
72  cpu_data_release(cpu_load_info, processor_msg_count);
73  }
74  status = KERN_FAILURE;
75  } else if (KERN_SUCCESS == status) {
76  processor_cpu_load_info per_cpu_info = cpu_load_info[cpu_index];
77 
78  // Total the ticks across the different states: idle, system, user, etc...
79  total = 0;
80  for (FwSizeType i = 0; i < CPU_STATE_MAX; i++) {
81  total += per_cpu_info.cpu_ticks[i];
82  }
83  used = total - per_cpu_info.cpu_ticks[CPU_STATE_IDLE];
84  cpu_data_release(cpu_load_info, processor_msg_count);
85  }
86  return status;
87 }
88 
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)) {
93  cpu_data_release(cpu_load_info, processor_msg_count);
94  return Status::OP_OK;
95  }
96  cpu_count = 0;
97  return Status::ERROR;
98 }
99 
101  kern_return_t status = cpu_by_index(cpu_index, ticks.used, ticks.total);
102  if (KERN_SUCCESS == status) {
103  return Status::OP_OK;
104  }
105  ticks.total = 1;
106  ticks.used = 1;
107  return Status::ERROR;
108 }
109 
111  return &this->m_handle;
112 }
113 
114 } // namespace Cpu
115 } // namespace Darwin
116 } // namespace Os
Operation succeeded.
Definition: Os.hpp:27
PlatformSizeType FwSizeType
cpu implementation
Definition: Cpu.hpp:60
Operation failed.
Definition: Os.hpp:28
Cpu variable handle parent.
Definition: Cpu.hpp:13
Status _getTicks(Os::Cpu::Ticks &ticks, FwSizeType cpu_index) override
Get the CPU tick information for a given CPU.
Definition: Cpu.cpp:100
Status _getCount(FwSizeType &cpu_count) override
Request the count of the CPUs detected by the system.
Definition: Cpu.cpp:89
CpuHandle * getHandle() override
returns the raw console handle
Definition: Cpu.cpp:110
Status
Generic OK/ERROR status.
Definition: Os.hpp:26
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
Definition: Cpu.cpp:30
kern_return_t cpu_by_index(FwSizeType cpu_index, FwSizeType &used, FwSizeType &total)
Query for a single CPU&#39;s ticks information.
Definition: Cpu.cpp:63
FwSizeType total
Total amount.
Definition: Os.hpp:34
FwSizeType used
Used amount.
Definition: Os.hpp:33
Generic used/total struct.
Definition: Os.hpp:32
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
Definition: Cpu.cpp:47