From 4cace320a75de835c08a87bb8aa3cfd0cd18ddfd Mon Sep 17 00:00:00 2001 From: Colm Donelan Date: Wed, 20 Nov 2019 14:59:12 +0000 Subject: IVGCVSW-4124 Replacing the "sleep_for" loop from FileOnlyProfilingConnection * Replacing the "sleep_for" loop in FileOnlyProfilingConnection with a producer consumer conditional mutex. * Reducing the times sleep loop times in FileOnlyProfilingDecoratorTests. Signed-off-by: Colm Donelan Change-Id: Ied2302b508b6e4e6b50809c77e3f19115449d0b6 --- src/profiling/FileOnlyProfilingConnection.cpp | 21 +++++++++++++-------- src/profiling/FileOnlyProfilingConnection.hpp | 4 ++++ .../test/FileOnlyProfilingDecoratorTests.cpp | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/profiling/FileOnlyProfilingConnection.cpp b/src/profiling/FileOnlyProfilingConnection.cpp index b32ae49911..004e27d30a 100644 --- a/src/profiling/FileOnlyProfilingConnection.cpp +++ b/src/profiling/FileOnlyProfilingConnection.cpp @@ -69,7 +69,11 @@ void FileOnlyProfilingConnection::SendConnectionAck() std::cout << "Sending connection acknowledgement." << std::endl; } std::unique_ptr uniqueNullPtr = nullptr; - m_PacketQueue.push(Packet(0x10000, 0, uniqueNullPtr)); + { + std::lock_guard lck(m_PacketAvailableMutex); + m_PacketQueue.push(Packet(0x10000, 0, uniqueNullPtr)); + } + m_ConditionPacketAvailable.notify_one(); } bool FileOnlyProfilingConnection::SendCounterSelectionPacket() @@ -94,7 +98,11 @@ bool FileOnlyProfilingConnection::SendCounterSelectionPacket() offset += uint16_t_size; } - m_PacketQueue.push(Packet(0x40000, bodySize, uniqueData)); + { + std::lock_guard lck(m_PacketAvailableMutex); + m_PacketQueue.push(Packet(0x40000, bodySize, uniqueData)); + } + m_ConditionPacketAvailable.notify_one(); return true; } @@ -155,13 +163,10 @@ bool FileOnlyProfilingConnection::WritePacket(const unsigned char* buffer, uint3 Packet FileOnlyProfilingConnection::ReadPacket(uint32_t timeout) { - uint16_t loopCount = 10; - uint32_t timeoutFraction = timeout / loopCount; - while (m_PacketQueue.empty()) + std::unique_lock lck(m_PacketAvailableMutex); + if (m_PacketQueue.empty()) { - std::this_thread::sleep_for(std::chrono::milliseconds(timeoutFraction)); - --loopCount; - if ((loopCount) == 0) + if(m_ConditionPacketAvailable.wait_for(lck, std::chrono::milliseconds(timeout)) == std::cv_status::timeout) { throw armnn::TimeoutException("Thread has timed out as per requested time limit"); } diff --git a/src/profiling/FileOnlyProfilingConnection.hpp b/src/profiling/FileOnlyProfilingConnection.hpp index 12a87a1535..d4477b6883 100644 --- a/src/profiling/FileOnlyProfilingConnection.hpp +++ b/src/profiling/FileOnlyProfilingConnection.hpp @@ -11,6 +11,7 @@ #include "ProfilingUtils.hpp" #include "Runtime.hpp" +#include #include #include @@ -75,6 +76,9 @@ private: std::vector m_IdList; std::queue m_PacketQueue; TargetEndianness m_Endianness; + + std::mutex m_PacketAvailableMutex; + std::condition_variable m_ConditionPacketAvailable; }; } // namespace profiling diff --git a/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp b/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp index 5524ed4fbf..d4907b6365 100644 --- a/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp +++ b/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp @@ -45,8 +45,8 @@ BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd) profilingService.Update(); profilingService.Update(); - uint32_t timeout = 2000; - uint32_t sleepTime = 50; + uint32_t timeout = 25; // Wait for a maximum of 25mSec + uint32_t sleepTime = 1; // in 1mSec intervals. uint32_t timeSlept = 0; // Give the profiling service sending thread time start executing and send the stream metadata. -- cgit v1.2.1