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