F´ Flight Software - C/C++ Documentation  devel
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 <Os/Darwin/Cpu.hpp>
6 #include <Fw/Types/Assert.hpp>
7 #include <mach/mach_error.h>
8 #include <mach/mach_host.h>
9 #include <mach/mach_init.h>
10 #include <mach/mach_types.h>
11 #include <mach/message.h>
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 = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count_natural,
31  reinterpret_cast<processor_info_array_t*>(&cpu_load_info), &processor_msg_count);
32  cpu_count = cpu_count_natural;
33  return stat;
34 }
35 
47 kern_return_t cpu_by_index(FwSizeType cpu_index, FwSizeType& used, FwSizeType& total) {
48  processor_cpu_load_info_t cpu_load_info;
49  FwSizeType cpu_count = 0;
50  kern_return_t status = cpu_data_helper(cpu_load_info, cpu_count);
51 
52  // Failure for CPU index
53  if (cpu_count <= cpu_index) {
54  status = KERN_FAILURE;
55  } else if (KERN_SUCCESS == status) {
56  processor_cpu_load_info per_cpu_info = cpu_load_info[cpu_index];
57 
58  // Total the ticks across the different states: idle, system, user, etc...
59  total = 0;
60  for (FwSizeType i = 0; i < CPU_STATE_MAX; i++) {
61  total += per_cpu_info.cpu_ticks[i];
62  }
63  used = total - per_cpu_info.cpu_ticks[CPU_STATE_IDLE];
64  }
65  return status;
66 }
67 
69  processor_cpu_load_info_t cpu_load_info;
70  if (KERN_SUCCESS == cpu_data_helper(cpu_load_info, cpu_count)) {
71  return Status::OP_OK;
72  }
73  cpu_count = 0;
74  return Status::ERROR;
75 }
76 
78  kern_return_t status = cpu_by_index(cpu_index, ticks.used, ticks.total);
79  if (KERN_SUCCESS == status) {
80  return Status::OP_OK;
81  }
82  ticks.total = 1;
83  ticks.used = 1;
84  return Status::ERROR;
85 }
86 
88  return &this->m_handle;
89 }
90 
91 } // namespace Cpu
92 } // namespace Darwin
93 } // namespace Os
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
Cpu variable handle parent.
Definition: Cpu.hpp:13
cpu implementation
Definition: Cpu.hpp:60
CpuHandle * getHandle() override
returns the raw console handle
Definition: Cpu.cpp:87
Status _getCount(FwSizeType &cpu_count) override
Request the count of the CPUs detected by the system.
Definition: Cpu.cpp:68
Status _getTicks(Os::Cpu::Ticks &ticks, FwSizeType cpu_index) override
Get the CPU tick information for a given CPU.
Definition: Cpu.cpp:77
kern_return_t cpu_by_index(FwSizeType cpu_index, FwSizeType &used, FwSizeType &total)
Query for a single CPU's ticks information.
Definition: Cpu.cpp:47
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
Status
Generic OK/ERROR status.
Definition: Os.hpp:25
@ OP_OK
Operation succeeded.
Definition: Os.hpp:26
@ ERROR
Operation failed.
Definition: Os.hpp:27
Generic used/total struct.
Definition: Os.hpp:31
FwSizeType total
Total amount.
Definition: Os.hpp:33
FwSizeType used
Used amount.
Definition: Os.hpp:32