GCC Code Coverage Report


Directory: ./
File: Svc/ComQueue/ComQueue.cpp
Date: 2026-11-03 22:10:14
Exec Total Coverage
Lines: 216 309 69.9%
Functions: 18 26 69.2%
Branches: 133 225 59.1%

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 1 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 3 times.
✓ Branch 2 taken 1 times.
4 for (FwIndexType i = 0; i < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->entries)); i++) {
26 3 this->entries[i].priority = 0;
27 3 this->entries[i].depth = 0;
28 3 this->entries[i].mode = Types::QUEUE_FIFO;
29 3 this->entries[i].overflowMode = Types::QUEUE_DROP_NEWEST;
30 }
31 1 }
32
33 1 ComQueue ::ComQueue(const char* const compName)
34 : ComQueueComponentBase(compName),
35 1 m_state(WAITING),
36 1 m_buffer_state(OWNED),
37 1 m_runQueued(false),
38 1 m_allocationId(static_cast<FwEnumStoreType>(-1)),
39 1 m_allocator(nullptr),
40
8/8
✓ Branch 2 taken 1 times.
✓ Branch 5 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 1 times.
7 m_allocation(nullptr) {
41 // Initialize throttles to "off" and managed-queue drop counters to zero
42
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
43 3 this->m_throttle[i] = false;
44 3 this->m_managedDrops[i] = 0;
45 }
46
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (FwIndexType i = 0; i < COM_PORT_COUNT; i++) {
47 2 this->m_comIngressDrops[i].store(0, std::memory_order_relaxed);
48 }
49
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (FwIndexType i = 0; i < BUFFER_PORT_COUNT; i++) {
50 1 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 1 }
55
56 2 ComQueue ::~ComQueue() {}
57
58 1 void ComQueue ::cleanup() {
59 // Deallocate memory ignoring error conditions
60
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if ((this->m_allocator != nullptr) && (this->m_allocation != nullptr)) {
61 1 this->m_allocator->deallocate(this->m_allocationId, this->m_allocation);
62 }
63 1 }
64
65 1 void ComQueue::configure(const QueueConfigurationTable& queueConfig,
66 FwEnumStoreType allocationId,
67 Fw::MemAllocator& allocator) {
68 1 FwIndexType currentPriorityIndex = 0;
69 1 FwSizeType totalAllocation = 0;
70
71 // Store/initialize allocator members
72 1 this->m_allocator = &allocator;
73 1 this->m_allocationId = allocationId;
74 1 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 3 times.
✓ Branch 1 taken 1 times.
4 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 12 for (FwIndexType entryIndex = 0;
88
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3 times.
12 entryIndex < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(queueConfig.entries)); entryIndex++) {
89 // Check for valid configuration entry
90 9 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 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 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 3 QueueMetadata& entry = this->m_prioritizedList[currentPriorityIndex];
98 3 entry.priority = queueConfig.entries[entryIndex].priority;
99 3 entry.depth = queueConfig.entries[entryIndex].depth;
100 3 entry.mode = queueConfig.entries[entryIndex].mode;
101 3 entry.overflowMode = queueConfig.entries[entryIndex].overflowMode;
102 3 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 2 times.
✓ Branch 1 taken 1 times.
3 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
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (entry.depth > 0) {
109 3 FW_ASSERT((std::numeric_limits<FwSizeType>::max() / entry.depth) >= entry.msgSize,
110 static_cast<FwAssertArgType>(entry.depth), static_cast<FwAssertArgType>(entry.msgSize));
111 3 FW_ASSERT(std::numeric_limits<FwSizeType>::max() - (entry.depth * entry.msgSize) >=
112 totalAllocation);
113 3 totalAllocation += entry.depth * entry.msgSize;
114 }
115 3 currentPriorityIndex++;
116 }
117 }
118 }
119 // At least one queue must be enabled; an all-zero table is the default-constructed (unconfigured) table
120 1 FW_ASSERT(totalAllocation > 0);
121 // Allocate a single chunk of memory from the memory allocator. Memory recover is neither needed nor used.
122 1 bool recoverable = false;
123 1 FwSizeType actualAllocation = totalAllocation;
124
1/1
✓ Branch 1 taken 1 times.
1 this->m_allocation = this->m_allocator->allocate(this->m_allocationId, actualAllocation, recoverable);
125 1 FW_ASSERT(this->m_allocation != nullptr);
126 1 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 1 FwSizeType allocationOffset = 0;
133
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
134 // Get current queue's allocation size and safety check the values
135 3 FwSizeType allocationSize = this->m_prioritizedList[i].depth * this->m_prioritizedList[i].msgSize;
136 3 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 3 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
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (this->m_prioritizedList[i].depth > 0) {
143 3 this->m_queues[this->m_prioritizedList[i].index].setup(
144 3 reinterpret_cast<U8*>(this->m_allocation) + allocationOffset, allocationSize,
145 3 this->m_prioritizedList[i].depth, this->m_prioritizedList[i].msgSize, this->m_prioritizedList[i].mode,
146
1/1
✓ Branch 1 taken 3 times.
3 this->m_prioritizedList[i].overflowMode);
147 }
148 3 allocationOffset += allocationSize;
149 }
150 // Safety check that all memory was used as expected
151 1 FW_ASSERT(allocationOffset == totalAllocation, static_cast<FwAssertArgType>(allocationOffset),
152 static_cast<FwAssertArgType>(totalAllocation));
153 1 }
154
155 // ----------------------------------------------------------------------
156 // Handler implementations for commands
157 // ----------------------------------------------------------------------
158
159 ✗ 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 ✗ FwIndexType queueIndex = this->getQueueNum(queueType, index);
165
166 // Validate queue index
167 ✗ if (queueIndex < 0 || queueIndex >= TOTAL_PORT_COUNT) {
168 ✗ this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
169 ✗ return;
170 }
171 ✗ FW_ASSERT(queueIndex >= 0 && queueIndex < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(queueIndex));
172
173 ✗ this->drainQueue(queueIndex);
174 ✗ this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
175 }
176
177 ✗ void ComQueue ::FLUSH_ALL_QUEUES_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
178 ✗ for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
179 ✗ this->drainQueue(i);
180 }
181 ✗ this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
182 ✗ }
183
184 ✗ 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 ✗ FwIndexType queueIndex = this->getQueueNum(queueType, index);
191
192 // Validate queue index
193 ✗ if (queueIndex < 0 || queueIndex >= TOTAL_PORT_COUNT) {
194 ✗ this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
195 ✗ return;
196 }
197
198 // Validate priority range
199 ✗ if (newPriority < 0 || newPriority >= TOTAL_PORT_COUNT) {
200 ✗ this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
201 ✗ return;
202 }
203
204 // Find our queue in the prioritized list & update the priority
205 ✗ for (FwIndexType prioIndex = 0; prioIndex < TOTAL_PORT_COUNT; prioIndex++) {
206 // Each entry must reference a valid queue index
207 ✗ 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 ✗ if (m_prioritizedList[prioIndex].index == queueIndex) {
211 ✗ m_prioritizedList[prioIndex].priority = newPriority;
212 ✗ 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 ✗ for (FwIndexType i = 0; i < TOTAL_PORT_COUNT - 1; i++) {
219 ✗ for (FwIndexType j = 0; (j < TOTAL_PORT_COUNT - i - 1) && (j < TOTAL_PORT_COUNT - 1); j++) {
220 ✗ if (m_prioritizedList[j].priority > m_prioritizedList[j + 1].priority) {
221 // Swap metadata
222 ✗ QueueMetadata temp = m_prioritizedList[j];
223 ✗ m_prioritizedList[j] = m_prioritizedList[j + 1];
224 ✗ m_prioritizedList[j + 1] = temp;
225 }
226 }
227 }
228
229 // Emit event for successful priority change
230 ✗ this->log_ACTIVITY_HI_QueuePriorityChanged(queueType, index, newPriority);
231
232 // Send command response
233 ✗ this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
234 }
235
236 // ----------------------------------------------------------------------
237 // Handler implementations for user-defined typed input ports
238 // ----------------------------------------------------------------------
239
240 822 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 822 FW_ASSERT(portNum >= 0 && portNum < COM_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
243 822 (void)this->enqueue(portNum, data);
244 822 }
245
246 30 void ComQueue::bufferQueueIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
247 30 FW_ASSERT(std::numeric_limits<FwIndexType>::max() - COM_PORT_COUNT > portNum);
248 30 const FwIndexType queueNum = static_cast<FwIndexType>(portNum + COM_PORT_COUNT);
249 // Ensure that the port number of bufferQueueIn is consistent with the expectation
250 30 FW_ASSERT(portNum >= 0 && portNum < BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
251 30 FW_ASSERT(queueNum < TOTAL_PORT_COUNT);
252 30 bool success = this->enqueue(queueNum, fwBuffer);
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!success) {
254 ✗ this->bufferReturnOut_out(portNum, fwBuffer);
255 }
256 30 }
257
258 853 void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condition) {
259
1/2
✓ Branch 0 taken 853 times.
✗ Branch 1 not taken.
853 switch (this->m_state) {
260 // On success, the queue should be processed. On failure, the component should still wait.
261 853 case WAITING:
262
1/2
✓ Branch 0 taken 853 times.
✗ Branch 1 not taken.
853 if (condition.e == Fw::Success::SUCCESS) {
263 853 this->m_state = READY;
264 853 this->processQueue();
265 // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
266 853 FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
267 static_cast<FwAssertArgType>(this->m_state));
268 } else {
269 ✗ this->m_state = WAITING;
270 }
271 853 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 853 }
279
280 262 void ComQueue::run_handler(const FwIndexType portNum, U32 context) {
281 262 bool expected = false;
282
1/2
✓ Branch 1 taken 262 times.
✗ Branch 2 not taken.
262 if (this->m_runQueued.compare_exchange_strong(expected, true)) {
283
1/1
✓ Branch 1 taken 262 times.
262 this->runInternal_internalInterfaceInvoke();
284 }
285 262 }
286
287 ✗ void ComQueue::runInternal_overflowHook() {
288 ✗ this->m_runQueued = false;
289 ✗ }
290
291 262 void ComQueue::runInternal_internalInterfaceHandler() {
292 262 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 1 taken 262 times.
262 ComQueueDepth comQueueDepth;
295 FW_ASSERT(comQueueDepth.SIZE <= COM_PORT_COUNT, static_cast<FwAssertArgType>(comQueueDepth.SIZE));
296
2/2
✓ Branch 0 taken 524 times.
✓ Branch 1 taken 262 times.
786 for (U32 i = 0; i < comQueueDepth.SIZE; i++) {
297 524 const FwIndexType queueNum = static_cast<FwIndexType>(i);
298
1/1
✓ Branch 1 taken 524 times.
524 comQueueDepth[i] = 0;
299
2/3
✓ Branch 1 taken 524 times.
✓ Branch 3 taken 524 times.
✗ Branch 4 not taken.
524 if (this->getQueueDepth(queueNum) > 0) {
300
2/2
✓ Branch 1 taken 524 times.
✓ Branch 4 taken 524 times.
524 comQueueDepth[i] = static_cast<U32>(this->m_queues[queueNum].get_high_water_mark());
301
1/1
✓ Branch 1 taken 524 times.
524 this->m_queues[queueNum].clear_high_water_mark();
302 }
303 }
304
2/2
✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
262 this->tlmWrite_comQueueDepth(comQueueDepth);
305
306 // Downlink the high-water marks for the Fw::Buffer array types
307
1/1
✓ Branch 1 taken 262 times.
262 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 262 times.
✓ Branch 1 taken 262 times.
524 for (U32 i = 0; i < buffQueueDepth.SIZE; i++) {
311 262 const FwIndexType queueNum = static_cast<FwIndexType>(i + COM_PORT_COUNT);
312
1/1
✓ Branch 1 taken 262 times.
262 buffQueueDepth[i] = 0;
313
2/3
✓ Branch 1 taken 262 times.
✓ Branch 3 taken 262 times.
✗ Branch 4 not taken.
262 if (this->getQueueDepth(queueNum) > 0) {
314
2/2
✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
262 buffQueueDepth[i] = static_cast<U32>(this->m_queues[queueNum].get_high_water_mark());
315
1/1
✓ Branch 1 taken 262 times.
262 this->m_queues[queueNum].clear_high_water_mark();
316 }
317 }
318
2/2
✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
262 this->tlmWrite_buffQueueDepth(buffQueueDepth);
319
320
1/1
✓ Branch 1 taken 262 times.
262 ComQueueIngressDrops comIngressDropped;
321 FW_ASSERT(comIngressDropped.SIZE <= COM_PORT_COUNT, static_cast<FwAssertArgType>(comIngressDropped.SIZE));
322
2/2
✓ Branch 0 taken 524 times.
✓ Branch 1 taken 262 times.
786 for (U32 i = 0; i < comIngressDropped.SIZE; i++) {
323
1/1
✓ Branch 1 taken 524 times.
1048 comIngressDropped[i] = this->m_comIngressDrops[i].load(std::memory_order_relaxed);
324 }
325
2/2
✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
262 this->tlmWrite_comIngressDropped(comIngressDropped);
326
327
1/1
✓ Branch 1 taken 262 times.
262 BuffQueueIngressDrops buffIngressDropped;
328 FW_ASSERT(buffIngressDropped.SIZE <= BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(buffIngressDropped.SIZE));
329
2/2
✓ Branch 0 taken 262 times.
✓ Branch 1 taken 262 times.
524 for (U32 i = 0; i < buffIngressDropped.SIZE; i++) {
330
1/1
✓ Branch 1 taken 262 times.
524 buffIngressDropped[i] = this->m_buffIngressDrops[i].load(std::memory_order_relaxed);
331 }
332
2/2
✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
262 this->tlmWrite_buffIngressDropped(buffIngressDropped);
333
334
1/1
✓ Branch 1 taken 262 times.
262 ComQueueManagedDrops comManagedDropped;
335 FW_ASSERT(comManagedDropped.SIZE <= COM_PORT_COUNT, static_cast<FwAssertArgType>(comManagedDropped.SIZE));
336
2/2
✓ Branch 0 taken 524 times.
✓ Branch 1 taken 262 times.
786 for (U32 i = 0; i < comManagedDropped.SIZE; i++) {
337
1/1
✓ Branch 1 taken 524 times.
524 comManagedDropped[i] = this->m_managedDrops[i];
338 }
339
2/2
✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
262 this->tlmWrite_comManagedDropped(comManagedDropped);
340
341
1/1
✓ Branch 1 taken 262 times.
262 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 262 times.
✓ Branch 1 taken 262 times.
524 for (U32 i = 0; i < buffManagedDropped.SIZE; i++) {
345
1/1
✓ Branch 1 taken 262 times.
262 buffManagedDropped[i] = this->m_managedDrops[COM_PORT_COUNT + i];
346 }
347
2/2
✓ Branch 1 taken 262 times.
✓ Branch 4 taken 262 times.
262 this->tlmWrite_buffManagedDropped(buffManagedDropped);
348 262 }
349
350 852 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 852 const BufferState previousState = this->m_buffer_state.exchange(OWNED);
354 852 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 852 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 852 FW_ASSERT(bufferReturnPortNum < BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(bufferReturnPortNum));
362
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 822 times.
852 if (bufferReturnPortNum >= 0) {
363 // It is a coding error not to connect the associated bufferReturnOut port for each dataReturnIn port
364 30 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 30 this->bufferReturnOut_out(bufferReturnPortNum, data);
368 }
369 852 }
370
371 // ----------------------------------------------------------------------
372 // Hook implementations for typed async input ports
373 // ----------------------------------------------------------------------
374
375 ✗ void ComQueue::comPacketQueueIn_overflowHook(FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
376 ✗ FW_ASSERT(portNum >= 0 && portNum < COM_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
377 (void)data;
378 (void)context;
379 ✗ this->m_comIngressDrops[portNum].fetch_add(1, std::memory_order_relaxed);
380 ✗ }
381
382 ✗ void ComQueue::bufferQueueIn_overflowHook(FwIndexType portNum, Fw::Buffer& fwBuffer) {
383 ✗ FW_ASSERT(portNum >= 0 && portNum < BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
384 ✗ this->m_buffIngressDrops[portNum].fetch_add(1, std::memory_order_relaxed);
385 ✗ this->bufferReturnOut_out(portNum, fwBuffer);
386 ✗ }
387
388 // ----------------------------------------------------------------------
389 // Private helper methods
390 // ----------------------------------------------------------------------
391
392 822 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 822 FW_ASSERT(queueNum >= 0 && queueNum < COM_PORT_COUNT, static_cast<FwAssertArgType>(queueNum));
396
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 822 times.
822 if (this->getQueueDepth(queueNum) == 0) {
397 ✗ return this->handleEnqueueStatus(queueNum, QueueType::COM_QUEUE, queueNum, false,
398 ✗ Fw::FW_SERIALIZE_NO_ROOM_LEFT);
399 }
400
401 822 const Fw::SerializeStatus status = this->m_queues[queueNum].enqueue(data);
402
2/2
✓ Branch 1 taken 822 times.
✓ Branch 4 taken 822 times.
822 return this->handleEnqueueStatus(queueNum, QueueType::COM_QUEUE, queueNum, false, status);
403 }
404
405 30 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 30 FW_ASSERT(queueNum >= COM_PORT_COUNT && queueNum < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(queueNum));
409 30 const FwIndexType portNum = static_cast<FwIndexType>(queueNum - COM_PORT_COUNT);
410
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (this->getQueueDepth(queueNum) == 0) {
411 ✗ return this->handleEnqueueStatus(queueNum, QueueType::BUFFER_QUEUE, portNum, false,
412 ✗ 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 30 bool preEmptiveOverflow = false;
419 30 Types::Queue& queue = this->m_queues[queueNum];
420
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 30 times.
120 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
421 210 if (this->m_prioritizedList[i].index == queueNum &&
422
4/6
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 90 times.
90 this->m_prioritizedList[i].overflowMode == Types::QUEUE_DROP_OLDEST &&
423 ✗ 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 ✗ Fw::Buffer droppedBuffer;
428 ✗ Fw::SerializeStatus dequeueStatus = queue.popFront(droppedBuffer);
429 ✗ FW_ASSERT(dequeueStatus == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(dequeueStatus));
430 ✗ this->bufferReturnOut_out(portNum, droppedBuffer);
431 ✗ preEmptiveOverflow = true;
432 ✗ break;
433 ✗ }
434 }
435
436 30 const Fw::SerializeStatus status = this->m_queues[queueNum].enqueue(data);
437
2/2
✓ Branch 1 taken 30 times.
✓ Branch 4 taken 30 times.
30 return this->handleEnqueueStatus(queueNum, QueueType::BUFFER_QUEUE, portNum, preEmptiveOverflow, status);
438 }
439
440 852 bool ComQueue::handleEnqueueStatus(const FwIndexType queueNum,
441 QueueType queueType,
442 const FwIndexType portNum,
443 const bool preEmptiveOverflow,
444 const Fw::SerializeStatus status) {
445
3/6
✓ Branch 0 taken 852 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 852 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 852 times.
852 if (preEmptiveOverflow || status == Fw::FW_SERIALIZE_NO_ROOM_LEFT ||
446 status == Fw::FW_SERIALIZE_DISCARDED_EXISTING) {
447 ✗ ++this->m_managedDrops[queueNum];
448 ✗ if (!this->m_throttle[queueNum]) {
449 ✗ this->log_WARNING_HI_QueueOverflow(queueType, portNum);
450 ✗ 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
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 389 times.
852 if (this->m_state == READY) {
456 463 this->processQueue();
457 }
458
459 // Check if the buffer was accepted or must be returned
460 852 return status != Fw::FW_SERIALIZE_NO_ROOM_LEFT;
461 }
462
463 822 void ComQueue::sendComBuffer(Fw::ComBuffer& comBuffer, FwIndexType queueIndex) {
464 822 FW_ASSERT(this->m_state == READY);
465
2/2
✓ Branch 1 taken 822 times.
✓ Branch 5 taken 822 times.
822 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 1 taken 822 times.
822 ComCfg::FrameContext context;
469 822 FwPacketDescriptorType descriptor = 0;
470
1/1
✓ Branch 1 taken 822 times.
822 Fw::SerializeStatus status = comBuffer.deserializeTo(descriptor);
471 822 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
472
1/1
✓ Branch 1 taken 822 times.
822 context.set_apid(static_cast<ComCfg::Apid::T>(descriptor));
473
1/1
✓ Branch 1 taken 822 times.
822 context.set_comQueueIndex(queueIndex);
474 822 const BufferState previousState = this->m_buffer_state.exchange(UNOWNED);
475 822 FW_ASSERT(previousState == OWNED, static_cast<FwAssertArgType>(previousState));
476
1/1
✓ Branch 1 taken 822 times.
822 this->dataOut_out(0, outBuffer, context);
477 // Set state to WAITING for the status to come back
478 822 this->m_state = WAITING;
479 822 }
480
481 30 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 30 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 1 taken 30 times.
30 ComCfg::FrameContext context;
487 FwPacketDescriptorType descriptor;
488
2/2
✓ Branch 1 taken 30 times.
✓ Branch 4 taken 30 times.
30 Fw::SerializeStatus status = buffer.getDeserializer().deserializeTo(descriptor);
489 30 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
490
1/1
✓ Branch 1 taken 30 times.
30 context.set_apid(static_cast<ComCfg::Apid::T>(descriptor));
491
1/1
✓ Branch 1 taken 30 times.
30 context.set_comQueueIndex(queueIndex);
492 30 const BufferState previousState = this->m_buffer_state.exchange(UNOWNED);
493 30 FW_ASSERT(previousState == OWNED, static_cast<FwAssertArgType>(previousState));
494
1/1
✓ Branch 1 taken 30 times.
30 this->dataOut_out(0, buffer, context);
495 // Set state to WAITING for the status to come back
496 30 this->m_state = WAITING;
497 30 }
498
499 ✗ void ComQueue::drainQueue(FwIndexType index) {
500 ✗ 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 ✗ if (this->getQueueDepth(index) == 0) {
503 ✗ return;
504 }
505 ✗ Types::Queue& queue = this->m_queues[index];
506
507 // Read all messages from the queue and discard them
508 ✗ Fw::SerializeStatus status = Fw::FW_SERIALIZE_OK;
509 ✗ const FwSizeType available = queue.getQueueSize();
510 ✗ for (FwSizeType i = 0; (i < available) && (status == Fw::FW_SERIALIZE_OK); i++) {
511 ✗ if (index < COM_PORT_COUNT) {
512 // Dequeueing deserializes the persisted Fw::ComBuffer from the queue's storage
513 ✗ Fw::ComBuffer comBuffer;
514 ✗ status = queue.dequeue(comBuffer);
515 ✗ } 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 ✗ Fw::Buffer buffer;
519 ✗ status = queue.dequeue(buffer);
520 ✗ this->bufferReturnOut_out(static_cast<FwIndexType>(index - COM_PORT_COUNT), buffer);
521 ✗ }
522 }
523 }
524
525 1316 void ComQueue::processQueue() {
526 1316 FwIndexType priorityIndex = 0;
527 1316 FwIndexType sendPriority = 0;
528 // Check that we are in the appropriate state
529 1316 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 2802 times.
✓ Branch 1 taken 464 times.
3266 for (priorityIndex = 0; priorityIndex < TOTAL_PORT_COUNT; priorityIndex++) {
534 2802 QueueMetadata& entry = this->m_prioritizedList[priorityIndex];
535 2802 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
5/6
✓ Branch 0 taken 2802 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1950 times.
✓ Branch 4 taken 852 times.
✓ Branch 5 taken 1950 times.
✓ Branch 6 taken 852 times.
2802 if ((entry.depth == 0) || (queue.getQueueSize() == 0)) {
539 1950 continue;
540 }
541
542 // Send out the message based on the type
543
2/2
✓ Branch 0 taken 822 times.
✓ Branch 1 taken 30 times.
852 if (entry.index < COM_PORT_COUNT) {
544 // Dequeue deserializes the persisted Fw::ComBuffer from the queue's storage
545 822 FW_ASSERT(this->m_buffer_state.load() == OWNED);
546 822 auto dequeue_status = queue.dequeue(this->m_dequeued_com_buffer);
547 822 FW_ASSERT(dequeue_status == Fw::SerializeStatus::FW_SERIALIZE_OK,
548 static_cast<FwAssertArgType>(dequeue_status));
549 822 this->sendComBuffer(this->m_dequeued_com_buffer, entry.index);
550 } else {
551
1/1
✓ Branch 1 taken 30 times.
30 Fw::Buffer buffer;
552
1/1
✓ Branch 1 taken 30 times.
30 auto dequeue_status = queue.dequeue(buffer);
553 30 FW_ASSERT(dequeue_status == Fw::SerializeStatus::FW_SERIALIZE_OK,
554 static_cast<FwAssertArgType>(dequeue_status));
555
1/1
✓ Branch 1 taken 30 times.
30 this->sendBuffer(buffer, entry.index);
556 30 }
557
558 // Update the throttle and the index that was just sent
559 852 this->m_throttle[entry.index] = false;
560
561 // Priority used in the next loop
562 852 sendPriority = entry.priority;
563 852 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 1316 for (priorityIndex++;
570
3/4
✓ Branch 0 taken 588 times.
✓ Branch 1 taken 728 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 588 times.
1316 priorityIndex < TOTAL_PORT_COUNT && (this->m_prioritizedList[priorityIndex].priority == sendPriority);
571 priorityIndex++) {
572 // Swap the previous entry with this one.
573 ✗ QueueMetadata temp = this->m_prioritizedList[priorityIndex];
574 ✗ this->m_prioritizedList[priorityIndex] = this->m_prioritizedList[priorityIndex - 1];
575 ✗ this->m_prioritizedList[priorityIndex - 1] = temp;
576 }
577 1316 }
578
579 ✗ 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 ✗ const FwIndexType portCount = (queueType == QueueType::COM_QUEUE) ? COM_PORT_COUNT : BUFFER_PORT_COUNT;
582 ✗ if (portNum < 0 || portNum >= portCount) {
583 ✗ return -1;
584 }
585 ✗ return static_cast<FwIndexType>(portNum + ((queueType == QueueType::COM_QUEUE) ? 0 : COM_PORT_COUNT));
586 }
587
588 1638 FwSizeType ComQueue::getQueueDepth(const FwIndexType queueNum) const {
589 1638 FW_ASSERT(queueNum >= 0 && queueNum < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(queueNum));
590
1/2
✓ Branch 0 taken 2982 times.
✗ Branch 1 not taken.
2982 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
591
2/2
✓ Branch 0 taken 1638 times.
✓ Branch 1 taken 1344 times.
2982 if (this->m_prioritizedList[i].index == queueNum) {
592 1638 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