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 SystemResourcesComponentImpl.cpp
3 // \author sfregoso
4 // \brief cpp 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 
13 #include <cmath> //isnan()
15 #include <FpConfig.hpp>
16 
17 namespace Svc {
18 
19 // ----------------------------------------------------------------------
20 // Construction, initialization, and destruction
21 // ----------------------------------------------------------------------
22 
23 SystemResources ::SystemResources(const char* const compName)
24  : SystemResourcesComponentBase(compName), m_cpu_count(0), m_enable(true) {
25 
26  // Structure initializations
27  m_mem.used = 0;
28  m_mem.total = 0;
29  for (U32 i = 0; i < CPU_COUNT; i++) {
30  m_cpu[i].used = 0;
31  m_cpu[i].total = 0;
32  m_cpu_prev[i].used = 0;
33  m_cpu_prev[i].total = 0;
34  }
35 
36  if (Os::Cpu::getCount(m_cpu_count) == Os::Generic::ERROR) {
37  m_cpu_count = 0;
38  }
39 
40  m_cpu_count = (m_cpu_count >= CPU_COUNT) ? CPU_COUNT : m_cpu_count;
41 
42  m_cpu_tlm_functions[0] = &Svc::SystemResources::tlmWrite_CPU_00;
43  m_cpu_tlm_functions[1] = &Svc::SystemResources::tlmWrite_CPU_01;
44  m_cpu_tlm_functions[2] = &Svc::SystemResources::tlmWrite_CPU_02;
45  m_cpu_tlm_functions[3] = &Svc::SystemResources::tlmWrite_CPU_03;
46  m_cpu_tlm_functions[4] = &Svc::SystemResources::tlmWrite_CPU_04;
47  m_cpu_tlm_functions[5] = &Svc::SystemResources::tlmWrite_CPU_05;
48  m_cpu_tlm_functions[6] = &Svc::SystemResources::tlmWrite_CPU_06;
49  m_cpu_tlm_functions[7] = &Svc::SystemResources::tlmWrite_CPU_07;
50  m_cpu_tlm_functions[8] = &Svc::SystemResources::tlmWrite_CPU_08;
51  m_cpu_tlm_functions[9] = &Svc::SystemResources::tlmWrite_CPU_09;
52  m_cpu_tlm_functions[10] = &Svc::SystemResources::tlmWrite_CPU_10;
53  m_cpu_tlm_functions[11] = &Svc::SystemResources::tlmWrite_CPU_11;
54  m_cpu_tlm_functions[12] = &Svc::SystemResources::tlmWrite_CPU_12;
55  m_cpu_tlm_functions[13] = &Svc::SystemResources::tlmWrite_CPU_13;
56  m_cpu_tlm_functions[14] = &Svc::SystemResources::tlmWrite_CPU_14;
57  m_cpu_tlm_functions[15] = &Svc::SystemResources::tlmWrite_CPU_15;
58 }
59 
61 
62 // ----------------------------------------------------------------------
63 // Handler implementations for user-defined typed input ports
64 // ----------------------------------------------------------------------
65 
66 void SystemResources ::run_handler(const NATIVE_INT_TYPE portNum, U32 tick_time_hz) {
67  if (m_enable) {
68  Cpu();
69  Mem();
70  PhysMem();
71  }
72 }
73 
74 // ----------------------------------------------------------------------
75 // Command handler implementations
76 // ----------------------------------------------------------------------
77 
78 void SystemResources ::ENABLE_cmdHandler(const FwOpcodeType opCode,
79  const U32 cmdSeq,
80  SystemResourceEnabled enable) {
81  m_enable = (enable == SystemResourceEnabled::ENABLED);
82  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
83 }
84 
85 F32 SystemResources::compCpuUtil(Os::Cpu::Ticks current, Os::Cpu::Ticks previous) {
86  F32 util = 100.0f;
87  // Prevent divide by zero on fast-sample
88  if ((current.total - previous.total) != 0) {
89  // Compute CPU % Utilization
90  util = (static_cast<F32>(current.used - previous.used) / static_cast<F32>(current.total - previous.total)) *
91  100.0f;
92  util = std::isnan(util) ? 100.0f : util;
93  }
94  return util;
95 }
96 
97 void SystemResources::Cpu() {
98  U32 count = 0;
99  F32 cpuAvg = 0;
100 
101  for (U32 i = 0; i < m_cpu_count && i < CPU_COUNT; i++) {
102  Os::Cpu::Status status = Os::Cpu::getTicks(m_cpu[i], i);
103  // Best-effort calculations and telemetry
104  if (status == Os::Generic::OP_OK) {
105  F32 cpuUtil = compCpuUtil(m_cpu[i], m_cpu_prev[i]);
106  cpuAvg += cpuUtil;
107 
108  // Send telemetry using telemetry output table
109  FW_ASSERT(this->m_cpu_tlm_functions[i]);
110  (this->*m_cpu_tlm_functions[i])(cpuUtil, Fw::Time());
111 
112  // Store cpu used and total
113  m_cpu_prev[i] = m_cpu[i];
114  count++;
115  }
116  }
117 
118  cpuAvg = (count == 0) ? 0.0f : (cpuAvg / static_cast<F32>(count));
119  this->tlmWrite_CPU(cpuAvg);
120 }
121 
122 void SystemResources::Mem() {
124  this->tlmWrite_MEMORY_TOTAL(m_mem.total / 1024);
125  this->tlmWrite_MEMORY_USED(m_mem.used / 1024);
126  }
127 }
128 
129 void SystemResources::PhysMem() {
130  FwSizeType total = 0;
131  FwSizeType free = 0;
132 
133  if (Os::FileSystem::getFreeSpace("/", total, free) == Os::FileSystem::OP_OK) {
134  this->tlmWrite_NON_VOLATILE_FREE(free / 1024);
135  this->tlmWrite_NON_VOLATILE_TOTAL(total / 1024);
136  }
137 }
138 
139 } // end namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
float F32
32-bit floating point
Definition: BasicTypes.h:49
U32 FwOpcodeType
Definition: FpConfig.h:91
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
C++-compatible configuration header for fprime configuration.
@ OK
Command successfully executed.
Definition: Time.hpp:9
static Status getTicks(Ticks &ticks, FwSizeType cpu_index)
Get the CPU tick information for a given CPU.
Definition: Cpu.cpp:44
static Status getCount(FwSizeType &cpu_count)
Request the count of the CPUs detected by the system.
Definition: Cpu.cpp:40
static Status getFreeSpace(const char *path, FwSizeType &totalBytes, FwSizeType &freeBytes)
Get filesystem free and total space in bytes on the filesystem containing the specified path.
Definition: FileSystem.cpp:97
@ OP_OK
Operation was successful.
Definition: FileSystem.hpp:25
static Status getUsage(Usage &memory)
get system memory usage
Definition: Memory.cpp:30
Auto-generated base for SystemResources component.
void tlmWrite_CPU_01(F32 arg, Fw::Time _tlmTime=Fw::Time())
void cmdResponse_out(FwOpcodeType opCode, U32 cmdSeq, Fw::CmdResponse response)
Emit command response.
void tlmWrite_CPU_02(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_14(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_MEMORY_USED(U64 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_NON_VOLATILE_TOTAL(U64 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_NON_VOLATILE_FREE(U64 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_00(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_09(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_10(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_06(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_08(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_MEMORY_TOTAL(U64 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_07(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_15(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_04(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_12(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_05(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_11(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_03(F32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_CPU_13(F32 arg, Fw::Time _tlmTime=Fw::Time())
SystemResources(const char *const compName)
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