GCC Code Coverage Report


Directory: Svc/ComQueue/
File: ComQueue.cpp
Date: 2026-11-03 21:18:15
Exec Total Coverage
Lines: 305 310 98.4%
Functions: 26 26 100.0%
Branches: 224 243 92.2%

Line Branch Exec Source
1 // ======================================================================
2 // \title ComQueue.cpp
3 // \author vbai
4 // \brief cpp file for ComQueue component implementation class
5 // ======================================================================
6
7 #include <Fw/Com/ComPacket.hpp>
8 #include <Fw/Types/Assert.hpp>
9 #include <Svc/ComQueue/ComQueue.hpp>
10 #include <type_traits>
11 #include "Fw/Types/BasicTypes.hpp"
12
13 namespace Svc {
14
15 // ----------------------------------------------------------------------
16 // Construction, initialization, and destruction
17 // ----------------------------------------------------------------------
18
19 using FwUnsignedIndexType = std::make_unsigned<FwIndexType>::type;
20
21 30 ComQueue ::QueueConfigurationTable ::QueueConfigurationTable() {
22 static_assert(static_cast<FwUnsignedIndexType>(std::numeric_limits<FwIndexType>::max()) >=
23 FW_NUM_ARRAY_ELEMENTS(this->entries),
24 "Number of entries must fit into FwIndexType");
25
2/2
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 30 times.
120 for (FwIndexType i = 0; i < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->entries)); i++) {
26 90 this->entries[i].priority = 0;
27 90 this->entries[i].depth = 0;
28 90 this->entries[i].mode = Types::QUEUE_FIFO;
29 90 this->entries[i].overflowMode = Types::QUEUE_DROP_NEWEST;
30 }
31 30 }
32
33 31 ComQueue ::ComQueue(const char* const compName)
34 : ComQueueComponentBase(compName),
35 31 m_state(WAITING),
36 31 m_buffer_state(OWNED),
37 31 m_runQueued(false),
38 31 m_allocationId(static_cast<FwEnumStoreType>(-1)),
39 31 m_allocator(nullptr),
40
8/8
✓ Branch 9 taken 31 times.
✓ Branch 15 taken 93 times.
✓ Branch 18 taken 93 times.
✓ Branch 19 taken 31 times.
✓ Branch 24 taken 62 times.
✓ Branch 25 taken 31 times.
✓ Branch 30 taken 31 times.
✓ Branch 31 taken 31 times.
248 m_allocation(nullptr) {
41 // Initialize throttles to "off" and managed-queue drop counters to zero
42
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 31 times.
124 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
43 93 this->m_throttle[i] = false;
44 93 this->m_managedDrops[i] = 0;
45 }
46
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 31 times.
93 for (FwIndexType i = 0; i < COM_PORT_COUNT; i++) {
47 62 this->m_comIngressDrops[i].store(0, std::memory_order_relaxed);
48 }
49
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 31 times.
62 for (FwIndexType i = 0; i < BUFFER_PORT_COUNT; i++) {
50 31 this->m_buffIngressDrops[i].store(0, std::memory_order_relaxed);
51 }
52
53 static_assert(TOTAL_PORT_COUNT >= 1, "ComQueue must have more than one port");
54 31 }
55
56 62 ComQueue ::~ComQueue() {}
57
58 30 void ComQueue ::cleanup() {
59 // Deallocate memory ignoring error conditions
60
3/4
✓ Branch 4 taken 29 times.
✓ Branch 5 taken 1 times.
✓ Branch 10 taken 29 times.
✗ Branch 11 not taken.
30 if ((this->m_allocator != nullptr) && (this->m_allocation != nullptr)) {
61 29 this->m_allocator->deallocate(this->m_allocationId, this->m_allocation);
62 }
63 30 }
64
65 29 void ComQueue::configure(const QueueConfigurationTable& queueConfig,
66 FwEnumStoreType allocationId,
67 Fw::MemAllocator& allocator) {
68 29 FwIndexType currentPriorityIndex = 0;
69 29 FwSizeType totalAllocation = 0;
70
71 // Store/initialize allocator members
72 29 this->m_allocator = &allocator;
73 29 this->m_allocationId = allocationId;
74 29 this->m_allocation = nullptr;
75
76 // Initializes the sorted queue metadata list in priority (sorted) order. This is accomplished by walking the
77 // priority values in priority order from 0 to TOTAL_PORT_COUNT. At each priory value, the supplied queue
78 // configuration table is walked and any entry matching the current priority values is used to add queue metadata to
79 // the prioritized list. This results in priority-sorted queue metadata objects that index back into the unsorted
80 // queue data structures.
81 //
82 // The total allocation size is tracked for passing to the allocation call and is a summation of
83 // (depth * message size) for each prioritized metadata object of (depth * message size)
84
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 29 times.
116 for (FwIndexType currentPriority = 0; currentPriority < TOTAL_PORT_COUNT; currentPriority++) {
85 // Walk each queue configuration entry and add them into the prioritized metadata list when matching the current
86 // priority value
87 348 for (FwIndexType entryIndex = 0;
88
2/2
✓ Branch 1 taken 261 times.
✓ Branch 2 taken 87 times.
348 entryIndex < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(queueConfig.entries)); entryIndex++) {
89 // Check for valid configuration entry
90 261 FW_ASSERT(queueConfig.entries[entryIndex].priority < TOTAL_PORT_COUNT,
91 static_cast<FwAssertArgType>(queueConfig.entries[entryIndex].priority),
92 static_cast<FwAssertArgType>(TOTAL_PORT_COUNT), static_cast<FwAssertArgType>(entryIndex));
93
2/2
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 174 times.
261 if (currentPriority == queueConfig.entries[entryIndex].priority) {
94 // Set up the queue metadata object in order to track priority, depth, index into the queue list of the
95 // backing queue object, and message size. Both index and message size are calculated where priority and
96 // depth are copied from the configuration object.
97 87 QueueMetadata& entry = this->m_prioritizedList[currentPriorityIndex];
98 87 entry.priority = queueConfig.entries[entryIndex].priority;
99 87 entry.depth = queueConfig.entries[entryIndex].depth;
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
87 entry.mode = queueConfig.entries[entryIndex].mode;
101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
87 entry.overflowMode = queueConfig.entries[entryIndex].overflowMode;
102 87 entry.index = entryIndex;
103 // Message size is determined by the type of object being stored, which in turn is determined by the
104 // index of the entry. Those lower than COM_PORT_COUNT are Fw::ComBuffers and those larger Fw::Buffer.
105
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 29 times.
87 entry.msgSize = (entryIndex < COM_PORT_COUNT) ? static_cast<FwSizeType>(Fw::ComBuffer::SERIALIZED_SIZE)
106 : static_cast<FwSizeType>(Fw::Buffer::SERIALIZED_SIZE);
107 // Overflow checks. A depth of 0 disables the queue and contributes no storage.
108
2/2
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 2 times.
87 if (entry.depth > 0) {
109 85 FW_ASSERT((std::numeric_limits<FwSizeType>::max() / entry.depth) >= entry.msgSize,
110 static_cast<FwAssertArgType>(entry.depth), static_cast<FwAssertArgType>(entry.msgSize));
111 85 FW_ASSERT(std::numeric_limits<FwSizeType>::max() - (entry.depth * entry.msgSize) >=
112 totalAllocation);
113 85 totalAllocation += entry.depth * entry.msgSize;
114 }
115 87 currentPriorityIndex++;
116 }
117 }
118 }
119 // At least one queue must be enabled; an all-zero table is the default-constructed (unconfigured) table
120 29 FW_ASSERT(totalAllocation > 0);
121 // Allocate a single chunk of memory from the memory allocator. Memory recover is neither needed nor used.
122 29 bool recoverable = false;
123 29 FwSizeType actualAllocation = totalAllocation;
124
1/1
✓ Branch 28 taken 29 times.
29 this->m_allocation = this->m_allocator->allocate(this->m_allocationId, actualAllocation, recoverable);
125 29 FW_ASSERT(this->m_allocation != nullptr);
126 29 FW_ASSERT(actualAllocation >= totalAllocation, static_cast<FwAssertArgType>(actualAllocation),
127 static_cast<FwAssertArgType>(totalAllocation));
128
129 // Each of the backing queue objects must be supplied memory to store the queued messages. These data regions are
130 // sub-portions of the total allocated data. This memory is passed out by looping through each queue in prioritized
131 // order and passing out the memory to each queue's setup method.
132 29 FwSizeType allocationOffset = 0;
133
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 29 times.
116 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
134 // Get current queue's allocation size and safety check the values
135 87 FwSizeType allocationSize = this->m_prioritizedList[i].depth * this->m_prioritizedList[i].msgSize;
136 87 FW_ASSERT(this->m_prioritizedList[i].index < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->m_queues)),
137 static_cast<FwAssertArgType>(this->m_prioritizedList[i].index));
138 87 FW_ASSERT((allocationSize + allocationOffset) <= totalAllocation, static_cast<FwAssertArgType>(allocationSize),
139 static_cast<FwAssertArgType>(allocationOffset), static_cast<FwAssertArgType>(totalAllocation));
140
141 // Setup queue's memory allocation, depth, and message size. Setup is skipped for a disabled (depth 0) queue
142
2/2
✓ Branch 5 taken 85 times.
✓ Branch 6 taken 2 times.
87 if (this->m_prioritizedList[i].depth > 0) {
143
3/5
✗ Branch 10 not taken.
✓ Branch 11 taken 85 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 85 times.
✓ Branch 20 taken 85 times.
510 this->m_queues[this->m_prioritizedList[i].index].setup(
144 85 reinterpret_cast<U8*>(this->m_allocation) + allocationOffset, allocationSize,
145 255 this->m_prioritizedList[i].depth, this->m_prioritizedList[i].msgSize, this->m_prioritizedList[i].mode,
146 85 this->m_prioritizedList[i].overflowMode);
147 }
148 87 allocationOffset += allocationSize;
149 }
150 // Safety check that all memory was used as expected
151 29 FW_ASSERT(allocationOffset == totalAllocation, static_cast<FwAssertArgType>(allocationOffset),
152 static_cast<FwAssertArgType>(totalAllocation));
153 29 }
154
155 // ----------------------------------------------------------------------
156 // Handler implementations for commands
157 // ----------------------------------------------------------------------
158
159 12 void ComQueue ::FLUSH_QUEUE_cmdHandler(FwOpcodeType opCode,
160 U32 cmdSeq,
161 const Svc::QueueType& queueType,
162 FwIndexType index) {
163 // Acquire the queue that we need to drain
164
1/1
✓ Branch 5 taken 12 times.
12 FwIndexType queueIndex = this->getQueueNum(queueType, index);
165
166 // Validate queue index
167
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
12 if (queueIndex < 0 || queueIndex >= TOTAL_PORT_COUNT) {
168
2/2
✓ Branch 6 taken 6 times.
✓ Branch 9 taken 6 times.
6 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
169 6 return;
170 }
171 6 FW_ASSERT(queueIndex >= 0 && queueIndex < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(queueIndex));
172
173 6 this->drainQueue(queueIndex);
174
2/2
✓ Branch 6 taken 6 times.
✓ Branch 9 taken 6 times.
6 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
175 }
176
177 2 void ComQueue ::FLUSH_ALL_QUEUES_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
178
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
179 6 this->drainQueue(i);
180 }
181
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
182 2 }
183
184 9 void ComQueue::SET_QUEUE_PRIORITY_cmdHandler(FwOpcodeType opCode,
185 U32 cmdSeq,
186 const Svc::QueueType& queueType,
187 FwIndexType index,
188 FwIndexType newPriority) {
189 // Acquire the queue we are to reprioritize
190
1/1
✓ Branch 5 taken 9 times.
9 FwIndexType queueIndex = this->getQueueNum(queueType, index);
191
192 // Validate queue index
193
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
9 if (queueIndex < 0 || queueIndex >= TOTAL_PORT_COUNT) {
194
2/2
✓ Branch 6 taken 6 times.
✓ Branch 9 taken 6 times.
6 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
195 6 return;
196 }
197
198 // Validate priority range
199
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 if (newPriority < 0 || newPriority >= TOTAL_PORT_COUNT) {
200
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
201 2 return;
202 }
203
204 // Find our queue in the prioritized list & update the priority
205
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (FwIndexType prioIndex = 0; prioIndex < TOTAL_PORT_COUNT; prioIndex++) {
206 // Each entry must reference a valid queue index
207 1 FW_ASSERT(m_prioritizedList[prioIndex].index >= 0 && m_prioritizedList[prioIndex].index < TOTAL_PORT_COUNT,
208 static_cast<FwAssertArgType>(m_prioritizedList[prioIndex].index));
209 // If the port based index matches, then update
210
1/2
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 if (m_prioritizedList[prioIndex].index == queueIndex) {
211 1 m_prioritizedList[prioIndex].priority = newPriority;
212 1 break; // Since we shouldn't find more than one queue at this port index
213 }
214 }
215
216 // Re-sort the prioritized list to maintain priority ordering
217 // Using simple bubble sort since TOTAL_PORT_COUNT is typically small
218
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT - 1; i++) {
219
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
5 for (FwIndexType j = 0; (j < TOTAL_PORT_COUNT - i - 1) && (j < TOTAL_PORT_COUNT - 1); j++) {
220
2/2
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 2 times.
3 if (m_prioritizedList[j].priority > m_prioritizedList[j + 1].priority) {
221 // Swap metadata
222 1 QueueMetadata temp = m_prioritizedList[j];
223 1 m_prioritizedList[j] = m_prioritizedList[j + 1];
224 1 m_prioritizedList[j + 1] = temp;
225 }
226 }
227 }
228
229 // Emit event for successful priority change
230 1 this->log_ACTIVITY_HI_QueuePriorityChanged(queueType, index, newPriority);
231
232 // Send command response
233
2/2
✓ Branch 6 taken 1 times.
✓ Branch 9 taken 1 times.
1 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
234 }
235
236 // ----------------------------------------------------------------------
237 // Handler implementations for user-defined typed input ports
238 // ----------------------------------------------------------------------
239
240 69 void ComQueue::comPacketQueueIn_handler(const FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
241 // Ensure that the port number of comPacketQueueIn is consistent with the expectation
242 69 FW_ASSERT(portNum >= 0 && portNum < COM_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
243 69 (void)this->enqueue(portNum, data);
244 69 }
245
246 39 void ComQueue::bufferQueueIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
247 39 FW_ASSERT(std::numeric_limits<FwIndexType>::max() - COM_PORT_COUNT > portNum);
248 39 const FwIndexType queueNum = static_cast<FwIndexType>(portNum + COM_PORT_COUNT);
249 // Ensure that the port number of bufferQueueIn is consistent with the expectation
250 39 FW_ASSERT(portNum >= 0 && portNum < BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
251 39 FW_ASSERT(queueNum < TOTAL_PORT_COUNT);
252 39 bool success = this->enqueue(queueNum, fwBuffer);
253
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 33 times.
39 if (!success) {
254 6 this->bufferReturnOut_out(portNum, fwBuffer);
255 }
256 39 }
257
258 57 void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condition) {
259
2/4
✗ Branch 3 not taken.
✓ Branch 4 taken 57 times.
✓ Branch 5 taken 57 times.
✗ Branch 6 not taken.
57 switch (this->m_state) {
260 // On success, the queue should be processed. On failure, the component should still wait.
261 57 case WAITING:
262
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 57 times.
✓ Branch 5 taken 48 times.
✓ Branch 6 taken 9 times.
57 if (condition.e == Fw::Success::SUCCESS) {
263 48 this->m_state = READY;
264 48 this->processQueue();
265 // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
266 48 FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
267 static_cast<FwAssertArgType>(this->m_state));
268 } else {
269 9 this->m_state = WAITING;
270 }
271 57 break;
272 // Both READY and unknown states should not be possible at this point. To receive a status message we must be
273 // one of the WAITING or RETRY states.
274 ✗ default:
275 ✗ FW_ASSERT(false, static_cast<FwAssertArgType>(this->m_state));
276 ✗ break;
277 }
278 57 }
279
280 10 void ComQueue::run_handler(const FwIndexType portNum, U32 context) {
281 10 bool expected = false;
282
1/2
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
10 if (this->m_runQueued.compare_exchange_strong(expected, true)) {
283
1/1
✓ Branch 5 taken 10 times.
10 this->runInternal_internalInterfaceInvoke();
284 }
285 10 }
286
287 1 void ComQueue::runInternal_overflowHook() {
288 1 this->m_runQueued = false;
289 1 }
290
291 9 void ComQueue::runInternal_internalInterfaceHandler() {
292 9 this->m_runQueued = false;
293 // Downlink the high-water marks for the Fw::ComBuffer array types. Disabled (depth 0) queues report 0.
294
1/1
✓ Branch 2 taken 9 times.
9 ComQueueDepth comQueueDepth;
295 FW_ASSERT(comQueueDepth.SIZE <= COM_PORT_COUNT, static_cast<FwAssertArgType>(comQueueDepth.SIZE));
296
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
27 for (U32 i = 0; i < comQueueDepth.SIZE; i++) {
297 18 const FwIndexType queueNum = static_cast<FwIndexType>(i);
298
1/1
✓ Branch 2 taken 18 times.
18 comQueueDepth[i] = 0;
299
3/3
✓ Branch 4 taken 18 times.
✓ Branch 6 taken 17 times.
✓ Branch 7 taken 1 times.
18 if (this->getQueueDepth(queueNum) > 0) {
300
2/2
✓ Branch 5 taken 17 times.
✓ Branch 9 taken 17 times.
17 comQueueDepth[i] = static_cast<U32>(this->m_queues[queueNum].get_high_water_mark());
301
1/1
✓ Branch 5 taken 17 times.
17 this->m_queues[queueNum].clear_high_water_mark();
302 }
303 }
304
2/2
✓ Branch 6 taken 9 times.
✓ Branch 9 taken 9 times.
9 this->tlmWrite_comQueueDepth(comQueueDepth);
305
306 // Downlink the high-water marks for the Fw::Buffer array types
307
1/1
✓ Branch 2 taken 9 times.
9 BuffQueueDepth buffQueueDepth;
308 FW_ASSERT((buffQueueDepth.SIZE + COM_PORT_COUNT) <= TOTAL_PORT_COUNT,
309 static_cast<FwAssertArgType>(buffQueueDepth.SIZE));
310
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for (U32 i = 0; i < buffQueueDepth.SIZE; i++) {
311 9 const FwIndexType queueNum = static_cast<FwIndexType>(i + COM_PORT_COUNT);
312
1/1
✓ Branch 2 taken 9 times.
9 buffQueueDepth[i] = 0;
313
3/3
✓ Branch 4 taken 9 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 1 times.
9 if (this->getQueueDepth(queueNum) > 0) {
314
2/2
✓ Branch 5 taken 8 times.
✓ Branch 9 taken 8 times.
8 buffQueueDepth[i] = static_cast<U32>(this->m_queues[queueNum].get_high_water_mark());
315
1/1
✓ Branch 5 taken 8 times.
8 this->m_queues[queueNum].clear_high_water_mark();
316 }
317 }
318
2/2
✓ Branch 6 taken 9 times.
✓ Branch 9 taken 9 times.
9 this->tlmWrite_buffQueueDepth(buffQueueDepth);
319
320
1/1
✓ Branch 2 taken 9 times.
9 ComQueueIngressDrops comIngressDropped;
321 FW_ASSERT(comIngressDropped.SIZE <= COM_PORT_COUNT, static_cast<FwAssertArgType>(comIngressDropped.SIZE));
322
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
27 for (U32 i = 0; i < comIngressDropped.SIZE; i++) {
323
1/1
✓ Branch 6 taken 18 times.
36 comIngressDropped[i] = this->m_comIngressDrops[i].load(std::memory_order_relaxed);
324 }
325
2/2
✓ Branch 6 taken 9 times.
✓ Branch 9 taken 9 times.
9 this->tlmWrite_comIngressDropped(comIngressDropped);
326
327
1/1
✓ Branch 2 taken 9 times.
9 BuffQueueIngressDrops buffIngressDropped;
328 FW_ASSERT(buffIngressDropped.SIZE <= BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(buffIngressDropped.SIZE));
329
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for (U32 i = 0; i < buffIngressDropped.SIZE; i++) {
330
1/1
✓ Branch 6 taken 9 times.
18 buffIngressDropped[i] = this->m_buffIngressDrops[i].load(std::memory_order_relaxed);
331 }
332
2/2
✓ Branch 6 taken 9 times.
✓ Branch 9 taken 9 times.
9 this->tlmWrite_buffIngressDropped(buffIngressDropped);
333
334
1/1
✓ Branch 2 taken 9 times.
9 ComQueueManagedDrops comManagedDropped;
335 FW_ASSERT(comManagedDropped.SIZE <= COM_PORT_COUNT, static_cast<FwAssertArgType>(comManagedDropped.SIZE));
336
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
27 for (U32 i = 0; i < comManagedDropped.SIZE; i++) {
337
1/1
✓ Branch 5 taken 18 times.
18 comManagedDropped[i] = this->m_managedDrops[i];
338 }
339
2/2
✓ Branch 6 taken 9 times.
✓ Branch 9 taken 9 times.
9 this->tlmWrite_comManagedDropped(comManagedDropped);
340
341
1/1
✓ Branch 2 taken 9 times.
9 BuffQueueManagedDrops buffManagedDropped;
342 FW_ASSERT((buffManagedDropped.SIZE + COM_PORT_COUNT) <= TOTAL_PORT_COUNT,
343 static_cast<FwAssertArgType>(buffManagedDropped.SIZE));
344
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for (U32 i = 0; i < buffManagedDropped.SIZE; i++) {
345
1/1
✓ Branch 5 taken 9 times.
9 buffManagedDropped[i] = this->m_managedDrops[COM_PORT_COUNT + i];
346 }
347
2/2
✓ Branch 6 taken 9 times.
✓ Branch 9 taken 9 times.
9 this->tlmWrite_buffManagedDropped(buffManagedDropped);
348 18 }
349
350 45 void ComQueue ::dataReturnIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
351 static_assert(std::numeric_limits<FwIndexType>::is_signed, "FwIndexType must be signed");
352 // This handler runs on the returning caller's thread: take ownership atomically
353 45 const BufferState previousState = this->m_buffer_state.exchange(OWNED);
354 45 FW_ASSERT(previousState == UNOWNED, static_cast<FwAssertArgType>(previousState));
355 // For the buffer queues, the index of the queue is portNum offset by COM_PORT_COUNT since
356 // the first COM_PORT_COUNT queues are for ComBuffer. So we have for buffer queues:
357 // queueNum = portNum + COM_PORT_COUNT
358 // Since queueNum is used as APID, we can retrieve the original portNum like such:
359 45 FwIndexType bufferReturnPortNum = static_cast<FwIndexType>(context.get_comQueueIndex() - ComQueue::COM_PORT_COUNT);
360 // Failing this assert means that context.apid was modified since ComQueue set it, which should not happen
361 45 FW_ASSERT(bufferReturnPortNum < BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(bufferReturnPortNum));
362
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 29 times.
45 if (bufferReturnPortNum >= 0) {
363 // It is a coding error not to connect the associated bufferReturnOut port for each dataReturnIn port
364 16 FW_ASSERT(this->isConnected_bufferReturnOut_OutputPort(bufferReturnPortNum),
365 static_cast<FwAssertArgType>(bufferReturnPortNum));
366 // If this is a buffer port, return the buffer to the BufferDownlink
367 16 this->bufferReturnOut_out(bufferReturnPortNum, data);
368 }
369 45 }
370
371 // ----------------------------------------------------------------------
372 // Hook implementations for typed async input ports
373 // ----------------------------------------------------------------------
374
375 20005 void ComQueue::comPacketQueueIn_overflowHook(FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
376 20005 FW_ASSERT(portNum >= 0 && portNum < COM_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
377 (void)data;
378 (void)context;
379 20005 this->m_comIngressDrops[portNum].fetch_add(1, std::memory_order_relaxed);
380 20005 }
381
382 4 void ComQueue::bufferQueueIn_overflowHook(FwIndexType portNum, Fw::Buffer& fwBuffer) {
383 4 FW_ASSERT(portNum >= 0 && portNum < BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
384 4 this->m_buffIngressDrops[portNum].fetch_add(1, std::memory_order_relaxed);
385 4 this->bufferReturnOut_out(portNum, fwBuffer);
386 4 }
387
388 // ----------------------------------------------------------------------
389 // Private helper methods
390 // ----------------------------------------------------------------------
391
392 69 bool ComQueue::enqueue(const FwIndexType queueNum, const Fw::ComBuffer& data) {
393 // Enqueue the given message onto the matching queue. When no space is available then emit the queue overflow event,
394 // set the appropriate throttle, and move on. A disabled (depth 0) queue has no space and always overflows.
395 69 FW_ASSERT(queueNum >= 0 && queueNum < COM_PORT_COUNT, static_cast<FwAssertArgType>(queueNum));
396
2/2
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 67 times.
69 if (this->getQueueDepth(queueNum) == 0) {
397
2/2
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
4 return this->handleEnqueueStatus(queueNum, QueueType::COM_QUEUE, queueNum, false,
398 2 Fw::FW_SERIALIZE_NO_ROOM_LEFT);
399 }
400
401 67 const Fw::SerializeStatus status = this->m_queues[queueNum].enqueue(data);
402
2/2
✓ Branch 5 taken 67 times.
✓ Branch 8 taken 67 times.
67 return this->handleEnqueueStatus(queueNum, QueueType::COM_QUEUE, queueNum, false, status);
403 }
404
405 39 bool ComQueue::enqueue(const FwIndexType queueNum, const Fw::Buffer& data) {
406 // Enqueue the given message onto the matching queue. When no space is available then emit the queue overflow event,
407 // set the appropriate throttle, and move on. A disabled (depth 0) queue has no space and always overflows.
408 39 FW_ASSERT(queueNum >= COM_PORT_COUNT && queueNum < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(queueNum));
409 39 const FwIndexType portNum = static_cast<FwIndexType>(queueNum - COM_PORT_COUNT);
410
2/2
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 38 times.
39 if (this->getQueueDepth(queueNum) == 0) {
411
2/2
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
2 return this->handleEnqueueStatus(queueNum, QueueType::BUFFER_QUEUE, portNum, false,
412 1 Fw::FW_SERIALIZE_NO_ROOM_LEFT);
413 }
414
415 // For buffer queues with DROP_OLDEST, check if the queue is full before enqueuing.
416 // If full, dequeue the oldest entry first so we can return buffer ownership before
417 // Queue::enqueue() silently discards it via rotate. This prevents buffer-pool leaks.
418 38 bool preEmptiveOverflow = false;
419 38 Types::Queue& queue = this->m_queues[queueNum];
420
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 36 times.
148 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
421 112 if (this->m_prioritizedList[i].index == queueNum &&
422
7/8
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 74 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 38 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 33 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 110 times.
117 this->m_prioritizedList[i].overflowMode == Types::QUEUE_DROP_OLDEST &&
423
2/2
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 3 times.
5 queue.getQueueSize() >= this->m_prioritizedList[i].depth) {
424 // Queue is full and will drop oldest; remove the front entry to return ownership.
425 // popFront() always removes from the front (oldest) regardless of queue mode,
426 // matching the rotate-based removal that Queue::enqueue() uses for DROP_OLDEST.
427
1/1
✓ Branch 2 taken 2 times.
2 Fw::Buffer droppedBuffer;
428
1/1
✓ Branch 2 taken 2 times.
2 Fw::SerializeStatus dequeueStatus = queue.popFront(droppedBuffer);
429 2 FW_ASSERT(dequeueStatus == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(dequeueStatus));
430
1/1
✓ Branch 5 taken 2 times.
2 this->bufferReturnOut_out(portNum, droppedBuffer);
431 2 preEmptiveOverflow = true;
432 2 break;
433 2 }
434 }
435
436 38 const Fw::SerializeStatus status = this->m_queues[queueNum].enqueue(data);
437
2/2
✓ Branch 5 taken 38 times.
✓ Branch 8 taken 38 times.
38 return this->handleEnqueueStatus(queueNum, QueueType::BUFFER_QUEUE, portNum, preEmptiveOverflow, status);
438 }
439
440 108 bool ComQueue::handleEnqueueStatus(const FwIndexType queueNum,
441 QueueType queueType,
442 const FwIndexType portNum,
443 const bool preEmptiveOverflow,
444 const Fw::SerializeStatus status) {
445
6/6
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 89 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 86 times.
108 if (preEmptiveOverflow || status == Fw::FW_SERIALIZE_NO_ROOM_LEFT ||
446 status == Fw::FW_SERIALIZE_DISCARDED_EXISTING) {
447 22 ++this->m_managedDrops[queueNum];
448
3/4
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 16 times.
✓ Branch 7 taken 6 times.
22 if (!this->m_throttle[queueNum]) {
449 16 this->log_WARNING_HI_QueueOverflow(queueType, portNum);
450 16 this->m_throttle[queueNum] = true;
451 }
452 }
453
454 // When the component is already in READY state process the queue to send out the next available message immediately
455
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 104 times.
108 if (this->m_state == READY) {
456 4 this->processQueue();
457 }
458
459 // Check if the buffer was accepted or must be returned
460 108 return status != Fw::FW_SERIALIZE_NO_ROOM_LEFT;
461 }
462
463 29 void ComQueue::sendComBuffer(Fw::ComBuffer& comBuffer, FwIndexType queueIndex) {
464 29 FW_ASSERT(this->m_state == READY);
465
2/2
✓ Branch 6 taken 29 times.
✓ Branch 14 taken 29 times.
29 Fw::Buffer outBuffer(comBuffer.getBuffAddr(), static_cast<Fw::Buffer::SizeType>(comBuffer.getSize()));
466
467 // Context value is used to determine what to do when the buffer returns on the dataReturnIn port
468
1/1
✓ Branch 2 taken 29 times.
29 ComCfg::FrameContext context;
469 29 FwPacketDescriptorType descriptor = 0;
470
1/1
✓ Branch 5 taken 29 times.
29 Fw::SerializeStatus status = comBuffer.deserializeTo(descriptor);
471 29 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
472
1/1
✓ Branch 2 taken 29 times.
29 context.set_apid(static_cast<ComCfg::Apid::T>(descriptor));
473
1/1
✓ Branch 2 taken 29 times.
29 context.set_comQueueIndex(queueIndex);
474 29 const BufferState previousState = this->m_buffer_state.exchange(UNOWNED);
475 29 FW_ASSERT(previousState == OWNED, static_cast<FwAssertArgType>(previousState));
476
1/1
✓ Branch 5 taken 29 times.
29 this->dataOut_out(0, outBuffer, context);
477 // Set state to WAITING for the status to come back
478 29 this->m_state = WAITING;
479 58 }
480
481 16 void ComQueue::sendBuffer(Fw::Buffer& buffer, FwIndexType queueIndex) {
482 // Retry buffer expected to be cleared as we are either transferring ownership or have already deallocated it.
483 16 FW_ASSERT(this->m_state == READY);
484
485 // Context value is used to determine what to do when the buffer returns on the dataReturnIn port
486
1/1
✓ Branch 2 taken 16 times.
16 ComCfg::FrameContext context;
487 16 FwPacketDescriptorType descriptor;
488
2/2
✓ Branch 2 taken 16 times.
✓ Branch 8 taken 16 times.
16 Fw::SerializeStatus status = buffer.getDeserializer().deserializeTo(descriptor);
489 16 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
490
1/1
✓ Branch 2 taken 16 times.
16 context.set_apid(static_cast<ComCfg::Apid::T>(descriptor));
491
1/1
✓ Branch 2 taken 16 times.
16 context.set_comQueueIndex(queueIndex);
492 16 const BufferState previousState = this->m_buffer_state.exchange(UNOWNED);
493 16 FW_ASSERT(previousState == OWNED, static_cast<FwAssertArgType>(previousState));
494
1/1
✓ Branch 5 taken 16 times.
16 this->dataOut_out(0, buffer, context);
495 // Set state to WAITING for the status to come back
496 16 this->m_state = WAITING;
497 32 }
498
499 12 void ComQueue::drainQueue(FwIndexType index) {
500 12 FW_ASSERT(index >= 0 && index < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(index));
501 // A disabled (depth 0) queue holds no messages and has no backing storage to drain
502
2/2
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 8 times.
12 if (this->getQueueDepth(index) == 0) {
503 4 return;
504 }
505 8 Types::Queue& queue = this->m_queues[index];
506
507 // Read all messages from the queue and discard them
508 8 Fw::SerializeStatus status = Fw::FW_SERIALIZE_OK;
509 8 const FwSizeType available = queue.getQueueSize();
510
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
15 for (FwSizeType i = 0; (i < available) && (status == Fw::FW_SERIALIZE_OK); i++) {
511
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 if (index < COM_PORT_COUNT) {
512 // Dequeueing deserializes the persisted Fw::ComBuffer from the queue's storage
513
1/1
✓ Branch 2 taken 4 times.
4 Fw::ComBuffer comBuffer;
514
1/1
✓ Branch 2 taken 4 times.
4 status = queue.dequeue(comBuffer);
515 4 } else {
516 // For buffer queues, if the buffer requires ownership return, return it via the bufferReturnOut port
517 // Dequeueing deserializes the persisted Fw::Buffer from the queue's storage
518
1/1
✓ Branch 2 taken 3 times.
3 Fw::Buffer buffer;
519
1/1
✓ Branch 2 taken 3 times.
3 status = queue.dequeue(buffer);
520
1/1
✓ Branch 5 taken 3 times.
3 this->bufferReturnOut_out(static_cast<FwIndexType>(index - COM_PORT_COUNT), buffer);
521 3 }
522 }
523 }
524
525 52 void ComQueue::processQueue() {
526 52 FwIndexType priorityIndex = 0;
527 52 FwIndexType sendPriority = 0;
528 // Check that we are in the appropriate state
529 52 FW_ASSERT(this->m_state == READY);
530
531 // Walk all the queues in priority order. Send the first message that is available in priority order. No balancing
532 // is done within this loop.
533
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 7 times.
96 for (priorityIndex = 0; priorityIndex < TOTAL_PORT_COUNT; priorityIndex++) {
534 89 QueueMetadata& entry = this->m_prioritizedList[priorityIndex];
535 89 Types::Queue& queue = this->m_queues[entry.index];
536
537 // Continue onto next prioritized queue if the current queue is disabled (depth 0) or holds no items
538
6/6
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 40 times.
✓ Branch 6 taken 45 times.
✓ Branch 7 taken 44 times.
✓ Branch 8 taken 45 times.
89 if ((entry.depth == 0) || (queue.getQueueSize() == 0)) {
539 44 continue;
540 }
541
542 // Send out the message based on the type
543
2/2
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 16 times.
45 if (entry.index < COM_PORT_COUNT) {
544 // Dequeue deserializes the persisted Fw::ComBuffer from the queue's storage
545 29 FW_ASSERT(this->m_buffer_state.load() == OWNED);
546 29 auto dequeue_status = queue.dequeue(this->m_dequeued_com_buffer);
547 29 FW_ASSERT(dequeue_status == Fw::SerializeStatus::FW_SERIALIZE_OK,
548 static_cast<FwAssertArgType>(dequeue_status));
549 29 this->sendComBuffer(this->m_dequeued_com_buffer, entry.index);
550 } else {
551
1/1
✓ Branch 2 taken 16 times.
16 Fw::Buffer buffer;
552
1/1
✓ Branch 2 taken 16 times.
16 auto dequeue_status = queue.dequeue(buffer);
553 16 FW_ASSERT(dequeue_status == Fw::SerializeStatus::FW_SERIALIZE_OK,
554 static_cast<FwAssertArgType>(dequeue_status));
555
1/1
✓ Branch 6 taken 16 times.
16 this->sendBuffer(buffer, entry.index);
556 16 }
557
558 // Update the throttle and the index that was just sent
559 45 this->m_throttle[entry.index] = false;
560
561 // Priority used in the next loop
562 45 sendPriority = entry.priority;
563 45 break;
564 }
565
566 // Starting on the priority entry after the one dispatched and continuing through the end of the set of entries that
567 // share the same priority, rotate those entries such that the currently dispatched queue is last and the rest are
568 // shifted up by one. This effectively round-robins the queues of the same priority.
569 54 for (priorityIndex++;
570
4/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 15 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 37 times.
54 priorityIndex < TOTAL_PORT_COUNT && (this->m_prioritizedList[priorityIndex].priority == sendPriority);
571 priorityIndex++) {
572 // Swap the previous entry with this one.
573 2 QueueMetadata temp = this->m_prioritizedList[priorityIndex];
574 2 this->m_prioritizedList[priorityIndex] = this->m_prioritizedList[priorityIndex - 1];
575 2 this->m_prioritizedList[priorityIndex - 1] = temp;
576 }
577 52 }
578
579 21 FwIndexType ComQueue::getQueueNum(Svc::QueueType queueType, FwIndexType portNum) {
580 // Validate against the per-type port count before folding so an invalid index cannot alias another queue type
581
2/2
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 10 times.
21 const FwIndexType portCount = (queueType == QueueType::COM_QUEUE) ? COM_PORT_COUNT : BUFFER_PORT_COUNT;
582
4/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 9 times.
21 if (portNum < 0 || portNum >= portCount) {
583 12 return -1;
584 }
585
2/2
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 4 times.
9 return static_cast<FwIndexType>(portNum + ((queueType == QueueType::COM_QUEUE) ? 0 : COM_PORT_COUNT));
586 }
587
588 147 FwSizeType ComQueue::getQueueDepth(const FwIndexType queueNum) const {
589 147 FW_ASSERT(queueNum >= 0 && queueNum < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(queueNum));
590
1/2
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
263 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
591
2/2
✓ Branch 5 taken 147 times.
✓ Branch 6 taken 116 times.
263 if (this->m_prioritizedList[i].index == queueNum) {
592 147 return this->m_prioritizedList[i].depth;
593 }
594 }
595 // configure() places exactly one metadata entry per queue in the prioritized list
596 ✗ FW_ASSERT(false, static_cast<FwAssertArgType>(queueNum));
597 ✗ return 0;
598 }
599 } // end namespace Svc
600