From aef251b7e5193c0906ea0aa6dbffed67df33aa42 Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Sun, 9 Nov 2025 19:34:02 -0400 Subject: [PATCH] IoUringEngn: add random dummy slot generator for debugging --- .../livoxGen1/ioUringAssemblyEngine.cpp | 71 ++++++++++++++++++- .../livoxGen1/ioUringAssemblyEngine.h | 12 +++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp index 4329b35..89175c2 100644 --- a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp +++ b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -63,7 +64,8 @@ eventfdFd(-1), eventfdDesc(nullptr), eventfd_value(0), stallTimer(parent_.device->componentThread->getIoService()), isAssembling(false), nDgramsPerStagingBufferFrame(nDgramsPerStagingBufferFrame_), -assembledSlotsTracker(nDgramsPerStagingBufferFrame_) +assembledSlotsTracker(nDgramsPerStagingBufferFrame_), +randomDevice(), randomGenerator(randomDevice()) {} bool IoUringAssemblyEngine::setup() @@ -664,6 +666,73 @@ void IoUringAssemblyEngine::fillUnAssembledSlotsWithDummyDgrams() } } +void IoUringAssemblyEngine::randomDummySlotFiller(AsynchronousLoop& loop) +{ + if (!frameAssemblyDesc) + { return; } + + // Check if there are already dummies (natural dummy instance) + uint32_t nSucceeded = loop.nSucceeded.load(); + uint32_t nTotal = loop.nTotal; + uint32_t nFailed = loop.nFailed.load(); + + if (nFailed > 0 || nSucceeded < nTotal) + { + std::cout << __func__ << ": Natural dummy instance detected (nSucceeded=" + << nSucceeded << ", nTotal=" << nTotal << ", nFailed=" << nFailed + << "), skipping artificial dummy creation" << std::endl; + return; + } + + // Randomly select a number of slots to make into dummies (less than total) + std::uniform_int_distribution numDummiesDist(1, nTotal - 1); + size_t numDummiesToCreate = numDummiesDist(randomGenerator); + + std::uniform_int_distribution slotIndexDist(0, nTotal - 1); + size_t dummiesCreated = 0; + size_t maxAttempts = nTotal * 10; // Safety limit to prevent infinite loop + size_t attempts = 0; + + // Mark random slots as unassembled + while (dummiesCreated < numDummiesToCreate && attempts < maxAttempts) + { + ++attempts; + size_t randomIndex = slotIndexDist(randomGenerator); + + // Skip if already unassembled, re-roll + if (randomIndex >= assembledSlotsTracker.size() + || !assembledSlotsTracker[randomIndex].assembled) + { + continue; + } + + // Mark as unassembled + assembledSlotsTracker[randomIndex].assembled = false; + ++dummiesCreated; + } + + if (dummiesCreated < numDummiesToCreate) + { + std::cerr << __func__ << ": Warning: Could only create " << dummiesCreated + << " dummy slots out of " << numDummiesToCreate + << " requested (max attempts reached)" << std::endl; + numDummiesToCreate = dummiesCreated; + } + + // Update the AsynchronousLoop to reflect the new number of dummies + // Since we only reach here when nSucceeded == nTotal and nFailed == 0, + // we can directly calculate the new values + uint32_t newSucceeded = nTotal - static_cast(numDummiesToCreate); + uint32_t newFailed = static_cast(numDummiesToCreate); + + loop.nSucceeded.store(newSucceeded); + loop.nFailed.store(newFailed); + + std::cout << __func__ << ": Artificially created " << numDummiesToCreate + << " dummy slots (nSucceeded: " << nTotal << " -> " + << newSucceeded << ", nFailed: 0 -> " << newFailed << ")" << std::endl; +} + void IoUringAssemblyEngine::printSlotBytes(size_t slotIndex, size_t nBytes) { if (!frameAssemblyDesc) diff --git a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.h b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.h index 749b79f..2525cea 100644 --- a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.h +++ b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,8 @@ public: static size_t computePointsPerDgram(int returnMode); static size_t computePointsPerFrame(int returnMode, size_t nDgramsPerFrame) { return computePointsPerDgram(returnMode) * nDgramsPerFrame; } + static bool compactionIsNeeded(uint32_t nSucceeded, uint32_t nTotal) + { return nSucceeded != 0 && nTotal != 0 && nSucceeded != nTotal; } private: PcloudStimulusBuffer& parent; @@ -86,13 +89,20 @@ private: // Track which slots have been successfully assembled and maintain persistent iovecs std::vector assembledSlotsTracker; + // Random number generation for dummy slot creation + std::random_device randomDevice; + std::mt19937 randomGenerator; + void fillUnAssembledSlotsWithDummyDgrams(); - void printSlotBytes(size_t slotIndex, size_t nBytes); + void randomDummySlotFiller(AsynchronousLoop& loop); void onEventfdRead( const boost::system::error_code& error, std::size_t bytes_transferred); class AssembleFrameReq; friend class AssembleFrameReq; + +public: + void printSlotBytes(size_t slotIndex, size_t nBytes); }; } // namespace stim_buff