GCC Code Coverage Report


Directory: /home/runner/work/fprime/fprime/Svc/SystemResources/
File: SystemResources.cpp
Date: 2026-09-03 22:14:00
Exec Total Coverage
Lines: 72 79 91.1%
Functions: 7 8 87.5%
Branches: 30 44 68.2%

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