GCC Code Coverage Report


Directory: ./
File: Svc/SystemResources/SystemResources.cpp
Date: 2026-09-03 21:13:48
Exec Total Coverage
Lines: 76 79 96.2%
Functions: 8 8 100.0%
Branches: 34 46 73.9%

Line Branch Exec Source
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>
14 #include <Svc/SystemResources/SystemResources.hpp>
15 #include <cmath> //isnan()
16
17 namespace Svc {
18
19 // ----------------------------------------------------------------------
20 // Construction, initialization, and destruction
21 // ----------------------------------------------------------------------
22
23 2 SystemResources ::SystemResources(const char* const compName)
24 2 : SystemResourcesComponentBase(compName), m_cpu_count(0), m_enable(true) {
25 // Structure initializations
26 2 m_mem.used = 0;
27 2 m_mem.total = 0;
28
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
34 for (U32 i = 0; i < CPU_COUNT; i++) {
29 32 m_cpu[i].used = 0;
30 32 m_cpu[i].total = 0;
31 32 m_cpu_prev[i].used = 0;
32 32 m_cpu_prev[i].total = 0;
33 }
34
35
1/1
✓ Branch 4 taken 2 times.
2 const Os::Generic::Status countStatus = Os::Cpu::getCount(m_cpu_count);
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (countStatus == Os::Generic::ERROR) {
37 m_cpu_count = 0;
38 }
39
40 2 m_cpu_count = (m_cpu_count >= CPU_COUNT) ? CPU_COUNT : m_cpu_count;
41
42 2 m_cpu_tlm_functions[0] = &Svc::SystemResources::tlmWrite_CPU_00;
43 2 m_cpu_tlm_functions[1] = &Svc::SystemResources::tlmWrite_CPU_01;
44 2 m_cpu_tlm_functions[2] = &Svc::SystemResources::tlmWrite_CPU_02;
45 2 m_cpu_tlm_functions[3] = &Svc::SystemResources::tlmWrite_CPU_03;
46 2 m_cpu_tlm_functions[4] = &Svc::SystemResources::tlmWrite_CPU_04;
47 2 m_cpu_tlm_functions[5] = &Svc::SystemResources::tlmWrite_CPU_05;
48 2 m_cpu_tlm_functions[6] = &Svc::SystemResources::tlmWrite_CPU_06;
49 2 m_cpu_tlm_functions[7] = &Svc::SystemResources::tlmWrite_CPU_07;
50 2 m_cpu_tlm_functions[8] = &Svc::SystemResources::tlmWrite_CPU_08;
51 2 m_cpu_tlm_functions[9] = &Svc::SystemResources::tlmWrite_CPU_09;
52 2 m_cpu_tlm_functions[10] = &Svc::SystemResources::tlmWrite_CPU_10;
53 2 m_cpu_tlm_functions[11] = &Svc::SystemResources::tlmWrite_CPU_11;
54 2 m_cpu_tlm_functions[12] = &Svc::SystemResources::tlmWrite_CPU_12;
55 2 m_cpu_tlm_functions[13] = &Svc::SystemResources::tlmWrite_CPU_13;
56 2 m_cpu_tlm_functions[14] = &Svc::SystemResources::tlmWrite_CPU_14;
57 2 m_cpu_tlm_functions[15] = &Svc::SystemResources::tlmWrite_CPU_15;
58 2 }
59
60 4 SystemResources ::~SystemResources() {}
61
62 // ----------------------------------------------------------------------
63 // Handler implementations for user-defined typed input ports
64 // ----------------------------------------------------------------------
65
66 3 void SystemResources ::run_handler(const FwIndexType portNum, U32 tick_time_hz) {
67
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 times.
3 if (m_enable) {
68 2 Cpu();
69 2 Mem();
70 2 PhysMem();
71 }
72 3 }
73
74 // ----------------------------------------------------------------------
75 // Command handler implementations
76 // ----------------------------------------------------------------------
77
78 2 void SystemResources ::ENABLE_cmdHandler(const FwOpcodeType opCode,
79 const U32 cmdSeq,
80 const SystemResourceEnabled& enable) {
81 2 m_enable = (enable == SystemResourceEnabled::ENABLED);
82
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
83 2 }
84
85 8 F32 SystemResources::compCpuUtil(Os::Cpu::Ticks current, Os::Cpu::Ticks previous) {
86 8 F32 util = 100.0f;
87 // Prevent divide by zero on fast-sample
88
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if ((current.total - previous.total) != 0) {
89 // Compute CPU % Utilization
90 8 util = (static_cast<F32>(current.used - previous.used) / static_cast<F32>(current.total - previous.total)) *
91 100.0f;
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 util = std::isnan(util) ? 100.0f : util;
93 }
94 8 return util;
95 }
96
97 2 void SystemResources::Cpu() {
98 2 U32 count = 0;
99 2 F32 cpuAvg = 0;
100
101
3/4
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
10 for (U32 i = 0; i < m_cpu_count && i < CPU_COUNT; i++) {
102 8 Os::Cpu::Status status = Os::Cpu::getTicks(m_cpu[i], i);
103 // Best-effort calculations and telemetry
104
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (status == Os::Generic::OP_OK) {
105 // Skip the sample if the counters went backwards (e.g. counter reset)
106
2/4
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 8 times.
8 if ((m_cpu[i].used < m_cpu_prev[i].used) || (m_cpu[i].total < m_cpu_prev[i].total)) {
107 m_cpu_prev[i] = m_cpu[i];
108 continue;
109 }
110 8 F32 cpuUtil = compCpuUtil(m_cpu[i], m_cpu_prev[i]);
111 8 cpuAvg += cpuUtil;
112
113 // Send telemetry using telemetry output table
114 8 FW_ASSERT(this->m_cpu_tlm_functions[i] != nullptr);
115
3/4
✓ Branch 15 taken 8 times.
✗ Branch 16 not taken.
✓ Branch 38 taken 8 times.
✓ Branch 41 taken 8 times.
8 (this->*m_cpu_tlm_functions[i])(cpuUtil, Fw::Time());
116
117 // Store cpu used and total
118 8 m_cpu_prev[i] = m_cpu[i];
119 8 count++;
120 }
121 }
122
123
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 cpuAvg = (count == 0) ? 0.0f : (cpuAvg / static_cast<F32>(count));
124
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->tlmWrite_CPU(cpuAvg);
125 2 }
126
127 2 void SystemResources::Mem() {
128 2 const Os::Generic::Status memStatus = Os::Memory::getUsage(m_mem);
129
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (memStatus == Os::Generic::OP_OK) {
130
2/2
✓ Branch 6 taken 2 times.
✓ Branch 13 taken 2 times.
2 this->tlmWrite_MEMORY_TOTAL(m_mem.total / 1024);
131
2/2
✓ Branch 6 taken 2 times.
✓ Branch 13 taken 2 times.
2 this->tlmWrite_MEMORY_USED(m_mem.used / 1024);
132 }
133 2 }
134
135 2 void SystemResources::PhysMem() {
136 2 FwSizeType total = 0;
137 2 FwSizeType free = 0;
138
139
1/1
✓ Branch 1 taken 2 times.
2 const Os::FileSystem::Status freeSpaceStatus = Os::FileSystem::getFreeSpace("/", total, free);
140
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (freeSpaceStatus == Os::FileSystem::OP_OK) {
141
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->tlmWrite_NON_VOLATILE_FREE(free / 1024);
142
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->tlmWrite_NON_VOLATILE_TOTAL(total / 1024);
143 }
144 2 }
145
146 } // end namespace Svc
147