F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
SystemResources.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title MacOs/SystemResources.cpp
3 // \author mstarch
4 // \brief hpp file for SystemResources component implementation class
5 //
6 // \copyright
7 // Copyright 2021, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
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>
17 #include <Fw/Types/Assert.hpp>
18 #include <Os/SystemResources.hpp>
19 
20 namespace Os {
33 kern_return_t vm_stat_helper(FwSizeType& used, FwSizeType& total) {
34  mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
35  vm_statistics_data_t vmstat;
36  vm_size_t vmsize;
37 
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);
40 
41  if (KERN_SUCCESS == stat1 and KERN_SUCCESS == stat2) {
42  // Wired (permanently in RAM), active (recently used), and inactive (not recently used) pages
43  used = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count;
44  total = used + vmstat.free_count;
45 
46  // Pages to totals
47  used *= vmsize;
48  total *= vmsize;
49  }
50  return (stat1 == KERN_SUCCESS) ? stat2 : stat1;
51 }
52 
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);
67  return stat;
68 }
82 kern_return_t cpu_by_index(U32 cpu_index, FwSizeType& used, FwSizeType& total) {
83  processor_cpu_load_info_t cpu_load_info;
84  U32 cpu_count = 0;
85  kern_return_t stat = cpu_data_helper(cpu_load_info, cpu_count);
86 
87  // Failure for CPU index
88  if (cpu_count <= cpu_index) {
89  stat = KERN_FAILURE;
90  } else if (KERN_SUCCESS == stat) {
91  FW_ASSERT(cpu_count > cpu_index, static_cast<FwAssertArgType>(cpu_count), static_cast<FwAssertArgType>(cpu_index)); // Will fail if the CPU count changes while running
92  processor_cpu_load_info per_cpu_info = cpu_load_info[(cpu_count > cpu_index) ? cpu_index : 0];
93 
94  // Total the ticks across the different states: idle, system, user, etc...
95  total = 0;
96  for (U32 i = 0; i < CPU_STATE_MAX; i++) {
97  total += per_cpu_info.cpu_ticks[i];
98  }
99  used = total - per_cpu_info.cpu_ticks[CPU_STATE_IDLE];
100  }
101  return stat;
102 }
103 
105  processor_cpu_load_info_t cpu_load_info;
106  if (KERN_SUCCESS == cpu_data_helper(cpu_load_info, cpuCount)) {
107  return SYSTEM_RESOURCES_OK;
108  }
109  cpuCount = 0;
110  return SYSTEM_RESOURCES_ERROR;
111 }
112 
113 SystemResources::SystemResourcesStatus SystemResources::getCpuTicks(CpuTicks& cpu_ticks, U32 cpu_index) {
114  // Fallbacks in case of error
115  cpu_ticks.used = 1;
116  cpu_ticks.total = 1;
117  kern_return_t stat = cpu_by_index(cpu_index, cpu_ticks.used, cpu_ticks.total);
118  return (stat == KERN_SUCCESS) ? SYSTEM_RESOURCES_OK : SYSTEM_RESOURCES_ERROR;
119 }
120 
121 
123  // Call out VM helper
124  if (KERN_SUCCESS == vm_stat_helper(memory_util.used, memory_util.total)) {
125  return SYSTEM_RESOURCES_OK;
126  }
127  // Force something sensible, while preventing divide by zero
128  memory_util.total = 1;
129  memory_util.used = 1;
130  return SYSTEM_RESOURCES_ERROR;
131 }
132 } // namespace Os
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
PlatformSizeType FwSizeType
Definition: FpConfig.h:30
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