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 <Fw/Types/Assert.hpp>
11 #include <Os/Darwin/Cpu.hpp>
12 
13 namespace Os {
14 namespace Darwin {
15 namespace Cpu {
16 
25 kern_return_t cpu_data_helper(processor_cpu_load_info_t& cpu_load_info, FwSizeType& cpu_count) {
26  static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<natural_t>::max(),
27  "FwSizeType cannot hold natural_t values");
28  natural_t cpu_count_natural;
29  mach_msg_type_number_t processor_msg_count;
30  kern_return_t stat =
31  host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count_natural,
32  reinterpret_cast<processor_info_array_t*>(&cpu_load_info), &processor_msg_count);
33  cpu_count = cpu_count_natural;
34  return stat;
35 }
36 
48 kern_return_t cpu_by_index(FwSizeType cpu_index, FwSizeType& used, FwSizeType& total) {
49  processor_cpu_load_info_t cpu_load_info;
50  FwSizeType cpu_count = 0;
51  kern_return_t status = cpu_data_helper(cpu_load_info, cpu_count);
52 
53  // Failure for CPU index
54  if (cpu_count <= cpu_index) {
55  status = KERN_FAILURE;
56  } else if (KERN_SUCCESS == status) {
57  processor_cpu_load_info per_cpu_info = cpu_load_info[cpu_index];
58 
59  // Total the ticks across the different states: idle, system, user, etc...
60  total = 0;
61  for (FwSizeType i = 0; i < CPU_STATE_MAX; i++) {
62  total += per_cpu_info.cpu_ticks[i];
63  }
64  used = total - per_cpu_info.cpu_ticks[CPU_STATE_IDLE];
65  }
66  return status;
67 }
68 
70  processor_cpu_load_info_t cpu_load_info;
71  if (KERN_SUCCESS == cpu_data_helper(cpu_load_info, cpu_count)) {
72  return Status::OP_OK;
73  }
74  cpu_count = 0;
75  return Status::ERROR;
76 }
77 
79  kern_return_t status = cpu_by_index(cpu_index, ticks.used, ticks.total);
80  if (KERN_SUCCESS == status) {
81  return Status::OP_OK;
82  }
83  ticks.total = 1;
84  ticks.used = 1;
85  return Status::ERROR;
86 }
87 
89  return &this->m_handle;
90 }
91 
92 } // namespace Cpu
93 } // namespace Darwin
94 } // namespace Os
Operation succeeded.
Definition: Os.hpp:26
PlatformSizeType FwSizeType
cpu implementation
Definition: Cpu.hpp:60
Operation failed.
Definition: Os.hpp:27
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:78
Status _getCount(FwSizeType &cpu_count) override
Request the count of the CPUs detected by the system.
Definition: Cpu.cpp:69
CpuHandle * getHandle() override
returns the raw console handle
Definition: Cpu.cpp:88
Status
Generic OK/ERROR status.
Definition: Os.hpp:25
kern_return_t cpu_data_helper(processor_cpu_load_info_t &cpu_load_info, FwSizeType &cpu_count)
helper around raw CPU capture API
Definition: Cpu.cpp:25
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:48
FwSizeType total
Total amount.
Definition: Os.hpp:33
FwSizeType used
Used amount.
Definition: Os.hpp:32
Generic used/total struct.
Definition: Os.hpp:31